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