1 /* Please avoid adding hacks here - instead add it to mac80211/backports.git */
3 #undef CONFIG_MODULE_STRIPPED
5 #include <linux/version.h> /* LINUX_VERSION_CODE */
6 #include <linux/types.h>
8 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
10 #define dev_get_iflink(_net_dev) ((_net_dev)->iflink)
12 #endif /* < KERNEL_VERSION(4, 1, 0) */
14 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
16 #include <linux/netdevice.h>
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); \
25 #elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
27 #include <linux/netdevice.h>
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); \
34 #endif /* < KERNEL_VERSION(4, 5, 0) */
37 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
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
42 #endif /* < KERNEL_VERSION(4, 0, 0) */
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
47 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
48 unsigned int transport_len
,
49 __sum16(*skb_chkf
)(struct sk_buff
*skb
));
51 int ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
);
53 int ipv6_mc_check_mld(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
);
55 #endif /* < KERNEL_VERSION(4, 2, 0) */
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
59 #define IFF_NO_QUEUE 0; dev->tx_queue_len = 0
61 static inline bool hlist_fake(struct hlist_node
*h
)
63 return h
->pprev
== &h
->next
;
66 #endif /* < KERNEL_VERSION(4, 3, 0) */
68 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
70 #include <linux/ethtool.h>
72 #define ethtool_link_ksettings batadv_ethtool_link_ksettings
74 struct batadv_ethtool_link_ksettings
{
82 #define __ethtool_get_link_ksettings(__dev, __link_settings) \
83 batadv_ethtool_get_link_ksettings(__dev, __link_settings)
86 batadv_ethtool_get_link_ksettings(struct net_device
*dev
,
87 struct ethtool_link_ksettings
*link_ksettings
)
89 struct ethtool_cmd cmd
;
92 memset(&cmd
, 0, sizeof(cmd
));
93 ret
= __ethtool_get_settings(dev
, &cmd
);
98 link_ksettings
->base
.duplex
= cmd
.duplex
;
99 link_ksettings
->base
.speed
= ethtool_cmd_speed(&cmd
);
100 link_ksettings
->base
.autoneg
= cmd
.autoneg
;
105 #endif /* < KERNEL_VERSION(4, 6, 0) */
108 #include_next <linux/netlink.h>
110 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
112 #include_next <net/netlink.h>
114 static inline bool batadv_nla_need_padding_for_64bit(struct sk_buff
*skb
);
116 static inline int batadv_nla_align_64bit(struct sk_buff
*skb
, int padattr
)
118 if (batadv_nla_need_padding_for_64bit(skb
) &&
119 !nla_reserve(skb
, padattr
, 0))
125 static inline struct nlattr
*batadv__nla_reserve_64bit(struct sk_buff
*skb
,
127 int attrlen
, int padattr
)
129 if (batadv_nla_need_padding_for_64bit(skb
))
130 batadv_nla_align_64bit(skb
, padattr
);
132 return __nla_reserve(skb
, attrtype
, attrlen
);
135 static inline void batadv__nla_put_64bit(struct sk_buff
*skb
, int attrtype
,
136 int attrlen
, const void *data
,
141 nla
= batadv__nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
142 memcpy(nla_data(nla
), data
, attrlen
);
145 static inline bool batadv_nla_need_padding_for_64bit(struct sk_buff
*skb
)
147 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
148 /* The nlattr header is 4 bytes in size, that's why we test
149 * if the skb->data _is_ aligned. A NOP attribute, plus
150 * nlattr header for next attribute, will make nla_data()
153 if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb
), 8))
159 static inline int batadv_nla_total_size_64bit(int payload
)
161 return NLA_ALIGN(nla_attr_size(payload
))
162 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
163 + NLA_ALIGN(nla_attr_size(0))
168 static inline int batadv_nla_put_64bit(struct sk_buff
*skb
, int attrtype
,
169 int attrlen
, const void *data
,
174 if (batadv_nla_need_padding_for_64bit(skb
))
175 len
= batadv_nla_total_size_64bit(attrlen
);
177 len
= nla_total_size(attrlen
);
178 if (unlikely(skb_tailroom(skb
) < len
))
181 batadv__nla_put_64bit(skb
, attrtype
, attrlen
, data
, padattr
);
185 #ifdef nla_put_u64_64bit
186 #undef nla_put_u64_64bit
189 #define nla_put_u64_64bit(_skb, _attrtype, _value, _padattr) \
190 batadv_nla_put_u64_64bit(_skb, _attrtype, _value, _padattr)
191 static inline int batadv_nla_put_u64_64bit(struct sk_buff
*skb
, int attrtype
,
192 u64 value
, int padattr
)
194 return batadv_nla_put_64bit(skb
, attrtype
, sizeof(u64
), &value
,
198 #endif /* < KERNEL_VERSION(4, 7, 0) */
201 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
203 #include_next <linux/cache.h>
205 /* hack for netlink.c which marked the family ops as ro */
206 #ifdef __ro_after_init
207 #undef __ro_after_init
209 #define __ro_after_init
211 #endif /* < KERNEL_VERSION(4, 10, 0) */
213 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
215 #include <linux/netdevice.h>
217 /* work around missing attribute needs_free_netdev and priv_destructor in
220 #define ether_setup(dev) \
221 void batadv_softif_free2(struct net_device *dev) \
223 batadv_softif_free(dev); \
226 void (*t1)(struct net_device *dev) __attribute__((unused)); \
227 bool t2 __attribute__((unused)); \
229 #define needs_free_netdev destructor = batadv_softif_free2; t2
230 #define priv_destructor destructor = batadv_softif_free2; t1
232 #endif /* < KERNEL_VERSION(4, 11, 9) */
234 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
236 static inline void *batadv_skb_put(struct sk_buff
*skb
, unsigned int len
)
238 return (void *)skb_put(skb
, len
);
244 #define skb_put batadv_skb_put
246 static inline void *batadv_skb_put_zero(struct sk_buff
*skb
, unsigned int len
)
248 void *tmp
= skb_put(skb
, len
);
258 #define skb_put_zero batadv_skb_put_zero
260 static inline void *batadv_skb_put_data(struct sk_buff
*skb
, const void *data
,
263 void *tmp
= skb_put(skb
, len
);
265 memcpy(tmp
, data
, len
);
273 #define skb_put_data batadv_skb_put_data
275 #endif /* < KERNEL_VERSION(4, 13, 0) */
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
279 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
280 batadv_softif_slave_add(__dev, __slave_dev)
282 #endif /* < KERNEL_VERSION(4, 15, 0) */
286 #define TIMER_DATA_TYPE unsigned long
287 #define TIMER_FUNC_TYPE void (*)(TIMER_DATA_TYPE)
289 static inline void timer_setup(struct timer_list
*timer
,
290 void (*callback
)(struct timer_list
*),
293 __setup_timer(timer
, (TIMER_FUNC_TYPE
)callback
,
294 (TIMER_DATA_TYPE
)timer
, flags
);
297 #define from_timer(var, callback_timer, timer_fieldname) \
298 container_of(callback_timer, typeof(*var), timer_fieldname)
300 #endif /* !from_timer */
302 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
305 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
307 #include <net/cfg80211.h>
309 /* cfg80211 fix: https://patchwork.kernel.org/patch/10449857/ */
310 static inline int batadv_cfg80211_get_station(struct net_device
*dev
,
312 struct station_info
*sinfo
)
314 memset(sinfo
, 0, sizeof(*sinfo
));
315 return cfg80211_get_station(dev
, mac_addr
, sinfo
);
318 #define cfg80211_get_station(dev, mac_addr, sinfo) \
319 batadv_cfg80211_get_station(dev, mac_addr, sinfo)
321 #endif /* < KERNEL_VERSION(4, 18, 0) */
325 typedef unsigned __bitwise __poll_t
;
327 typedef unsigned __poll_t
;
330 #endif /* < KERNEL_VERSION(4, 16, 0) */
332 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
334 static inline int batadv_access_ok(int type
, const void __user
*p
,
337 return access_ok(type
, p
, size
);
344 #define access_ok_get(_1, _2, _3 , access_ok_name, ...) access_ok_name
345 #define access_ok(...) \
346 access_ok_get(__VA_ARGS__, access_ok3, access_ok2)(__VA_ARGS__)
348 #define access_ok2(addr, size) batadv_access_ok(VERIFY_WRITE, (addr), (size))
349 #define access_ok3(type, addr, size) batadv_access_ok((type), (addr), (size))
351 #endif /* < KERNEL_VERSION(5, 0, 0) */
355 #include <linux/version.h>
356 #include_next <linux/average.h>
358 #include <linux/bug.h>
362 #endif /* DECLARE_EWMA */
365 * Exponentially weighted moving average (EWMA)
367 * This implements a fixed-precision EWMA algorithm, with both the
368 * precision and fall-off coefficient determined at compile-time
369 * and built into the generated helper funtions.
371 * The first argument to the macro is the name that will be used
372 * for the struct and helper functions.
374 * The second argument, the precision, expresses how many bits are
375 * used for the fractional part of the fixed-precision values.
377 * The third argument, the weight reciprocal, determines how the
378 * new values will be weighed vs. the old state, new values will
379 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
380 * that this parameter must be a power of two for efficiency.
383 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
384 struct ewma_##name { \
385 unsigned long internal; \
387 static inline void ewma_##name##_init(struct ewma_##name *e) \
389 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
390 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
392 * Even if you want to feed it just 0/1 you should have \
393 * some bits for the non-fractional part... \
395 BUILD_BUG_ON((_precision) > 30); \
396 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
399 static inline unsigned long \
400 ewma_##name##_read(struct ewma_##name *e) \
402 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
403 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
404 BUILD_BUG_ON((_precision) > 30); \
405 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
406 return e->internal >> (_precision); \
408 static inline void ewma_##name##_add(struct ewma_##name *e, \
411 unsigned long internal = READ_ONCE(e->internal); \
412 unsigned long weight_rcp = ilog2(_weight_rcp); \
413 unsigned long precision = _precision; \
415 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
416 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
417 BUILD_BUG_ON((_precision) > 30); \
418 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
420 WRITE_ONCE(e->internal, internal ? \
421 (((internal << weight_rcp) - internal) + \
422 (val << precision)) >> weight_rcp : \
423 (val << precision)); \
426 /* </DECLARE_EWMA> */