batman-adv: Drop redefinition of NL80211_STA_INFO_EXPECTED_THROUGHPUT
[feed/routing.git] / batman-adv / src / compat-hacks.h
1 /* Please avoid adding hacks here - instead add it to mac80211/backports.git */
2
3 #undef CONFIG_MODULE_STRIPPED
4
5 #include <linux/version.h> /* LINUX_VERSION_CODE */
6 #include <linux/types.h>
7
8 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
9
10 #define dev_get_iflink(_net_dev) ((_net_dev)->iflink)
11
12 #endif /* < KERNEL_VERSION(4, 1, 0) */
13
14 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
15
16 #include <linux/netdevice.h>
17
18 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
19 BUILD_BUG_ON(upper_priv != NULL); \
20 BUILD_BUG_ON(upper_info != NULL); \
21 BUILD_BUG_ON(extack != NULL); \
22 netdev_master_upper_dev_link(dev, upper_dev); \
23 })
24
25 #elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
26
27 #include <linux/netdevice.h>
28
29 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
30 BUILD_BUG_ON(extack != NULL); \
31 netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info); \
32 })
33
34 #endif /* < KERNEL_VERSION(4, 5, 0) */
35
36
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
38
39 /* wild hack for batadv_getlink_net only */
40 #define get_link_net get_xstats_size || 1 ? fallback_net : (struct net*)netdev->rtnl_link_ops->get_xstats_size
41
42 #endif /* < KERNEL_VERSION(4, 0, 0) */
43
44
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
46
47 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
48 unsigned int transport_len,
49 __sum16(*skb_chkf)(struct sk_buff *skb));
50
51 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed);
52
53 int ipv6_mc_check_mld(struct sk_buff *skb, struct sk_buff **skb_trimmed);
54
55 #endif /* < KERNEL_VERSION(4, 2, 0) */
56
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
58
59 #define IFF_NO_QUEUE 0; dev->tx_queue_len = 0
60
61 static inline bool hlist_fake(struct hlist_node *h)
62 {
63 return h->pprev == &h->next;
64 }
65
66 #endif /* < KERNEL_VERSION(4, 3, 0) */
67
68 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
69
70 #include <linux/ethtool.h>
71
72 #define ethtool_link_ksettings batadv_ethtool_link_ksettings
73
74 struct batadv_ethtool_link_ksettings {
75 struct {
76 __u32 speed;
77 __u8 duplex;
78 __u8 autoneg;
79 } base;
80 };
81
82 #define __ethtool_get_link_ksettings(__dev, __link_settings) \
83 batadv_ethtool_get_link_ksettings(__dev, __link_settings)
84
85 static inline int
86 batadv_ethtool_get_link_ksettings(struct net_device *dev,
87 struct ethtool_link_ksettings *link_ksettings)
88 {
89 struct ethtool_cmd cmd;
90 int ret;
91
92 memset(&cmd, 0, sizeof(cmd));
93 ret = __ethtool_get_settings(dev, &cmd);
94
95 if (ret != 0)
96 return ret;
97
98 link_ksettings->base.duplex = cmd.duplex;
99 link_ksettings->base.speed = ethtool_cmd_speed(&cmd);
100 link_ksettings->base.autoneg = cmd.autoneg;
101
102 return 0;
103 }
104
105 #endif /* < KERNEL_VERSION(4, 6, 0) */
106
107 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
108
109 #ifdef netif_trans_update
110 #undef netif_trans_update
111 #endif
112
113 #define netif_trans_update batadv_netif_trans_update
114 static inline void batadv_netif_trans_update(struct net_device *dev)
115 {
116 dev->trans_start = jiffies;
117 }
118
119 #endif /* < KERNEL_VERSION(4, 7, 0) */
120
121
122 #include_next <linux/netlink.h>
123
124 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
125
126 #include_next <net/netlink.h>
127
128 static inline bool batadv_nla_need_padding_for_64bit(struct sk_buff *skb);
129
130 static inline int batadv_nla_align_64bit(struct sk_buff *skb, int padattr)
131 {
132 if (batadv_nla_need_padding_for_64bit(skb) &&
133 !nla_reserve(skb, padattr, 0))
134 return -EMSGSIZE;
135
136 return 0;
137 }
138
139 static inline struct nlattr *batadv__nla_reserve_64bit(struct sk_buff *skb,
140 int attrtype,
141 int attrlen, int padattr)
142 {
143 if (batadv_nla_need_padding_for_64bit(skb))
144 batadv_nla_align_64bit(skb, padattr);
145
146 return __nla_reserve(skb, attrtype, attrlen);
147 }
148
149 static inline void batadv__nla_put_64bit(struct sk_buff *skb, int attrtype,
150 int attrlen, const void *data,
151 int padattr)
152 {
153 struct nlattr *nla;
154
155 nla = batadv__nla_reserve_64bit(skb, attrtype, attrlen, padattr);
156 memcpy(nla_data(nla), data, attrlen);
157 }
158
159 static inline bool batadv_nla_need_padding_for_64bit(struct sk_buff *skb)
160 {
161 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
162 /* The nlattr header is 4 bytes in size, that's why we test
163 * if the skb->data _is_ aligned. A NOP attribute, plus
164 * nlattr header for next attribute, will make nla_data()
165 * 8-byte aligned.
166 */
167 if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
168 return true;
169 #endif
170 return false;
171 }
172
173 static inline int batadv_nla_total_size_64bit(int payload)
174 {
175 return NLA_ALIGN(nla_attr_size(payload))
176 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
177 + NLA_ALIGN(nla_attr_size(0))
178 #endif
179 ;
180 }
181
182 static inline int batadv_nla_put_64bit(struct sk_buff *skb, int attrtype,
183 int attrlen, const void *data,
184 int padattr)
185 {
186 size_t len;
187
188 if (batadv_nla_need_padding_for_64bit(skb))
189 len = batadv_nla_total_size_64bit(attrlen);
190 else
191 len = nla_total_size(attrlen);
192 if (unlikely(skb_tailroom(skb) < len))
193 return -EMSGSIZE;
194
195 batadv__nla_put_64bit(skb, attrtype, attrlen, data, padattr);
196 return 0;
197 }
198
199 #ifdef nla_put_u64_64bit
200 #undef nla_put_u64_64bit
201 #endif
202
203 #define nla_put_u64_64bit(_skb, _attrtype, _value, _padattr) \
204 batadv_nla_put_u64_64bit(_skb, _attrtype, _value, _padattr)
205 static inline int batadv_nla_put_u64_64bit(struct sk_buff *skb, int attrtype,
206 u64 value, int padattr)
207 {
208 return batadv_nla_put_64bit(skb, attrtype, sizeof(u64), &value,
209 padattr);
210 }
211
212 #endif /* < KERNEL_VERSION(4, 7, 0) */
213
214
215 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
216
217 #include_next <linux/cache.h>
218
219 /* hack for netlink.c which marked the family ops as ro */
220 #ifdef __ro_after_init
221 #undef __ro_after_init
222 #endif
223 #define __ro_after_init
224
225 #endif /* < KERNEL_VERSION(4, 10, 0) */
226
227 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
228
229 #include <linux/netdevice.h>
230
231 /* work around missing attribute needs_free_netdev and priv_destructor in
232 * net_device
233 */
234 #define ether_setup(dev) \
235 void batadv_softif_free2(struct net_device *dev) \
236 { \
237 batadv_softif_free(dev); \
238 free_netdev(dev); \
239 } \
240 void (*t1)(struct net_device *dev) __attribute__((unused)); \
241 bool t2 __attribute__((unused)); \
242 ether_setup(dev)
243 #define needs_free_netdev destructor = batadv_softif_free2; t2
244 #define priv_destructor destructor = batadv_softif_free2; t1
245
246 #endif /* < KERNEL_VERSION(4, 11, 9) */
247
248 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
249
250 static inline void *batadv_skb_put(struct sk_buff *skb, unsigned int len)
251 {
252 return (void *)skb_put(skb, len);
253 }
254 #ifdef skb_put
255 #undef skb_put
256 #endif
257
258 #define skb_put batadv_skb_put
259
260 static inline void *batadv_skb_put_zero(struct sk_buff *skb, unsigned int len)
261 {
262 void *tmp = skb_put(skb, len);
263
264 memset(tmp, 0, len);
265
266 return tmp;
267 }
268 #ifdef skb_put_zero
269 #undef skb_put_zero
270 #endif
271
272 #define skb_put_zero batadv_skb_put_zero
273
274 static inline void *batadv_skb_put_data(struct sk_buff *skb, const void *data,
275 unsigned int len)
276 {
277 void *tmp = skb_put(skb, len);
278
279 memcpy(tmp, data, len);
280
281 return tmp;
282 }
283 #ifdef skb_put_data
284 #undef skb_put_data
285 #endif
286
287 #define skb_put_data batadv_skb_put_data
288
289 #endif /* < KERNEL_VERSION(4, 13, 0) */
290
291 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
292
293 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
294 batadv_softif_slave_add(__dev, __slave_dev)
295
296 #endif /* < KERNEL_VERSION(4, 15, 0) */
297
298 #ifndef from_timer
299
300 #define TIMER_DATA_TYPE unsigned long
301 #define TIMER_FUNC_TYPE void (*)(TIMER_DATA_TYPE)
302
303 static inline void timer_setup(struct timer_list *timer,
304 void (*callback)(struct timer_list *),
305 unsigned int flags)
306 {
307 __setup_timer(timer, (TIMER_FUNC_TYPE)callback,
308 (TIMER_DATA_TYPE)timer, flags);
309 }
310
311 #define from_timer(var, callback_timer, timer_fieldname) \
312 container_of(callback_timer, typeof(*var), timer_fieldname)
313
314 #endif /* !from_timer */
315
316 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
317
318
319 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
320
321 #include <net/cfg80211.h>
322
323 /* cfg80211 fix: https://patchwork.kernel.org/patch/10449857/ */
324 static inline int batadv_cfg80211_get_station(struct net_device *dev,
325 const u8 *mac_addr,
326 struct station_info *sinfo)
327 {
328 memset(sinfo, 0, sizeof(*sinfo));
329 return cfg80211_get_station(dev, mac_addr, sinfo);
330 }
331
332 #define cfg80211_get_station(dev, mac_addr, sinfo) \
333 batadv_cfg80211_get_station(dev, mac_addr, sinfo)
334
335 #endif /* < KERNEL_VERSION(4, 18, 0) */
336
337
338 #ifdef __CHECK_POLL
339 typedef unsigned __bitwise __poll_t;
340 #else
341 typedef unsigned __poll_t;
342 #endif
343
344 #endif /* < KERNEL_VERSION(4, 16, 0) */
345
346 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
347
348 static inline int batadv_access_ok(int type, const void __user *p,
349 unsigned long size)
350 {
351 return access_ok(type, p, size);
352 }
353
354 #ifdef access_ok
355 #undef access_ok
356 #endif
357
358 #define access_ok_get(_1, _2, _3 , access_ok_name, ...) access_ok_name
359 #define access_ok(...) \
360 access_ok_get(__VA_ARGS__, access_ok3, access_ok2)(__VA_ARGS__)
361
362 #define access_ok2(addr, size) batadv_access_ok(VERIFY_WRITE, (addr), (size))
363 #define access_ok3(type, addr, size) batadv_access_ok((type), (addr), (size))
364
365 #endif /* < KERNEL_VERSION(5, 0, 0) */
366
367 /* <DECLARE_EWMA> */
368
369 #include <linux/version.h>
370 #include_next <linux/average.h>
371
372 #include <linux/bug.h>
373
374 #ifdef DECLARE_EWMA
375 #undef DECLARE_EWMA
376 #endif /* DECLARE_EWMA */
377
378 /*
379 * Exponentially weighted moving average (EWMA)
380 *
381 * This implements a fixed-precision EWMA algorithm, with both the
382 * precision and fall-off coefficient determined at compile-time
383 * and built into the generated helper funtions.
384 *
385 * The first argument to the macro is the name that will be used
386 * for the struct and helper functions.
387 *
388 * The second argument, the precision, expresses how many bits are
389 * used for the fractional part of the fixed-precision values.
390 *
391 * The third argument, the weight reciprocal, determines how the
392 * new values will be weighed vs. the old state, new values will
393 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
394 * that this parameter must be a power of two for efficiency.
395 */
396
397 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
398 struct ewma_##name { \
399 unsigned long internal; \
400 }; \
401 static inline void ewma_##name##_init(struct ewma_##name *e) \
402 { \
403 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
404 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
405 /* \
406 * Even if you want to feed it just 0/1 you should have \
407 * some bits for the non-fractional part... \
408 */ \
409 BUILD_BUG_ON((_precision) > 30); \
410 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
411 e->internal = 0; \
412 } \
413 static inline unsigned long \
414 ewma_##name##_read(struct ewma_##name *e) \
415 { \
416 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
417 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
418 BUILD_BUG_ON((_precision) > 30); \
419 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
420 return e->internal >> (_precision); \
421 } \
422 static inline void ewma_##name##_add(struct ewma_##name *e, \
423 unsigned long val) \
424 { \
425 unsigned long internal = READ_ONCE(e->internal); \
426 unsigned long weight_rcp = ilog2(_weight_rcp); \
427 unsigned long precision = _precision; \
428 \
429 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
430 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
431 BUILD_BUG_ON((_precision) > 30); \
432 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
433 \
434 WRITE_ONCE(e->internal, internal ? \
435 (((internal << weight_rcp) - internal) + \
436 (val << precision)) >> weight_rcp : \
437 (val << precision)); \
438 }
439
440 /* </DECLARE_EWMA> */