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(3, 16, 0)
16 /* Linux 3.15 misses the uapi include.... */
17 #include <uapi/linux/nl80211.h>
19 #endif /* < KERNEL_VERSION(3, 16, 0) */
21 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)
23 #include <linux/netdevice.h>
25 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
26 BUILD_BUG_ON(upper_priv != NULL); \
27 BUILD_BUG_ON(upper_info != NULL); \
28 BUILD_BUG_ON(extack != NULL); \
29 netdev_set_master(dev, upper_dev); \
32 #elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
34 #include <linux/netdevice.h>
36 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
37 BUILD_BUG_ON(upper_priv != NULL); \
38 BUILD_BUG_ON(upper_info != NULL); \
39 BUILD_BUG_ON(extack != NULL); \
40 netdev_master_upper_dev_link(dev, upper_dev); \
43 #elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
45 #include <linux/netdevice.h>
47 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
48 BUILD_BUG_ON(extack != NULL); \
49 netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info); \
52 #endif /* < KERNEL_VERSION(4, 5, 0) */
55 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
57 /* for batadv_v_elp_get_throughput which would have used
58 * STATION_INFO_EXPECTED_THROUGHPUT in Linux 4.0.0
60 #define NL80211_STA_INFO_EXPECTED_THROUGHPUT 28
62 /* wild hack for batadv_getlink_net only */
63 #define get_link_net get_xstats_size || 1 ? fallback_net : (struct net*)netdev->rtnl_link_ops->get_xstats_size
65 #endif /* < KERNEL_VERSION(4, 0, 0) */
68 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
70 struct sk_buff
*skb_checksum_trimmed(struct sk_buff
*skb
,
71 unsigned int transport_len
,
72 __sum16(*skb_chkf
)(struct sk_buff
*skb
));
74 int ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
);
76 int ipv6_mc_check_mld(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
);
78 #endif /* < KERNEL_VERSION(4, 2, 0) */
80 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
82 #define IFF_NO_QUEUE 0; dev->tx_queue_len = 0
84 static inline bool hlist_fake(struct hlist_node
*h
)
86 return h
->pprev
== &h
->next
;
89 #endif /* < KERNEL_VERSION(4, 3, 0) */
91 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
93 #include <linux/ethtool.h>
95 #define ethtool_link_ksettings batadv_ethtool_link_ksettings
97 struct batadv_ethtool_link_ksettings
{
104 #define __ethtool_get_link_ksettings(__dev, __link_settings) \
105 batadv_ethtool_get_link_ksettings(__dev, __link_settings)
108 batadv_ethtool_get_link_ksettings(struct net_device
*dev
,
109 struct ethtool_link_ksettings
*link_ksettings
)
111 struct ethtool_cmd cmd
;
114 memset(&cmd
, 0, sizeof(cmd
));
115 ret
= __ethtool_get_settings(dev
, &cmd
);
120 link_ksettings
->base
.duplex
= cmd
.duplex
;
121 link_ksettings
->base
.speed
= ethtool_cmd_speed(&cmd
);
126 #endif /* < KERNEL_VERSION(4, 6, 0) */
128 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
130 #ifdef netif_trans_update
131 #undef netif_trans_update
134 #define netif_trans_update batadv_netif_trans_update
135 static inline void batadv_netif_trans_update(struct net_device
*dev
)
137 dev
->trans_start
= jiffies
;
140 #endif /* < KERNEL_VERSION(4, 7, 0) */
143 #include_next <linux/netlink.h>
145 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
147 #include_next <net/netlink.h>
149 static inline bool batadv_nla_need_padding_for_64bit(struct sk_buff
*skb
);
151 static inline int batadv_nla_align_64bit(struct sk_buff
*skb
, int padattr
)
153 if (batadv_nla_need_padding_for_64bit(skb
) &&
154 !nla_reserve(skb
, padattr
, 0))
160 static inline struct nlattr
*batadv__nla_reserve_64bit(struct sk_buff
*skb
,
162 int attrlen
, int padattr
)
164 if (batadv_nla_need_padding_for_64bit(skb
))
165 batadv_nla_align_64bit(skb
, padattr
);
167 return __nla_reserve(skb
, attrtype
, attrlen
);
170 static inline void batadv__nla_put_64bit(struct sk_buff
*skb
, int attrtype
,
171 int attrlen
, const void *data
,
176 nla
= batadv__nla_reserve_64bit(skb
, attrtype
, attrlen
, padattr
);
177 memcpy(nla_data(nla
), data
, attrlen
);
180 static inline bool batadv_nla_need_padding_for_64bit(struct sk_buff
*skb
)
182 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
183 /* The nlattr header is 4 bytes in size, that's why we test
184 * if the skb->data _is_ aligned. A NOP attribute, plus
185 * nlattr header for next attribute, will make nla_data()
188 if (IS_ALIGNED((unsigned long)skb_tail_pointer(skb
), 8))
194 static inline int batadv_nla_total_size_64bit(int payload
)
196 return NLA_ALIGN(nla_attr_size(payload
))
197 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
198 + NLA_ALIGN(nla_attr_size(0))
203 static inline int batadv_nla_put_64bit(struct sk_buff
*skb
, int attrtype
,
204 int attrlen
, const void *data
,
209 if (batadv_nla_need_padding_for_64bit(skb
))
210 len
= batadv_nla_total_size_64bit(attrlen
);
212 len
= nla_total_size(attrlen
);
213 if (unlikely(skb_tailroom(skb
) < len
))
216 batadv__nla_put_64bit(skb
, attrtype
, attrlen
, data
, padattr
);
220 #ifdef nla_put_u64_64bit
221 #undef nla_put_u64_64bit
224 #define nla_put_u64_64bit(_skb, _attrtype, _value, _padattr) \
225 batadv_nla_put_u64_64bit(_skb, _attrtype, _value, _padattr)
226 static inline int batadv_nla_put_u64_64bit(struct sk_buff
*skb
, int attrtype
,
227 u64 value
, int padattr
)
229 return batadv_nla_put_64bit(skb
, attrtype
, sizeof(u64
), &value
,
233 #endif /* < KERNEL_VERSION(4, 7, 0) */
236 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
238 #include_next <linux/cache.h>
240 /* hack for netlink.c which marked the family ops as ro */
241 #ifdef __ro_after_init
242 #undef __ro_after_init
244 #define __ro_after_init
246 #endif /* < KERNEL_VERSION(4, 10, 0) */
248 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
250 #include <linux/netdevice.h>
252 /* work around missing attribute needs_free_netdev and priv_destructor in
255 #define ether_setup(dev) \
256 void batadv_softif_free2(struct net_device *dev) \
258 batadv_softif_free(dev); \
261 void (*t1)(struct net_device *dev) __attribute__((unused)); \
262 bool t2 __attribute__((unused)); \
264 #define needs_free_netdev destructor = batadv_softif_free2; t2
265 #define priv_destructor destructor = batadv_softif_free2; t1
267 #endif /* < KERNEL_VERSION(4, 11, 9) */
269 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
271 static inline void *batadv_skb_put(struct sk_buff
*skb
, unsigned int len
)
273 return (void *)skb_put(skb
, len
);
279 #define skb_put batadv_skb_put
281 static inline void *batadv_skb_put_zero(struct sk_buff
*skb
, unsigned int len
)
283 void *tmp
= skb_put(skb
, len
);
293 #define skb_put_zero batadv_skb_put_zero
295 static inline void *batadv_skb_put_data(struct sk_buff
*skb
, const void *data
,
298 void *tmp
= skb_put(skb
, len
);
300 memcpy(tmp
, data
, len
);
308 #define skb_put_data batadv_skb_put_data
310 #endif /* < KERNEL_VERSION(4, 13, 0) */
312 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
314 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
315 batadv_softif_slave_add(__dev, __slave_dev)
317 #endif /* < KERNEL_VERSION(4, 15, 0) */
321 #define TIMER_DATA_TYPE unsigned long
322 #define TIMER_FUNC_TYPE void (*)(TIMER_DATA_TYPE)
324 static inline void timer_setup(struct timer_list
*timer
,
325 void (*callback
)(struct timer_list
*),
328 __setup_timer(timer
, (TIMER_FUNC_TYPE
)callback
,
329 (TIMER_DATA_TYPE
)timer
, flags
);
332 #define from_timer(var, callback_timer, timer_fieldname) \
333 container_of(callback_timer, typeof(*var), timer_fieldname)
335 #endif /* !from_timer */
337 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
340 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 0)
342 #include <net/cfg80211.h>
344 /* cfg80211 fix: https://patchwork.kernel.org/patch/10449857/ */
345 static inline int batadv_cfg80211_get_station(struct net_device
*dev
,
347 struct station_info
*sinfo
)
349 memset(sinfo
, 0, sizeof(*sinfo
));
350 return cfg80211_get_station(dev
, mac_addr
, sinfo
);
353 #define cfg80211_get_station(dev, mac_addr, sinfo) \
354 batadv_cfg80211_get_station(dev, mac_addr, sinfo)
356 #endif /* < KERNEL_VERSION(4, 18, 0) */
360 typedef unsigned __bitwise __poll_t
;
362 typedef unsigned __poll_t
;
365 #endif /* < KERNEL_VERSION(4, 16, 0) */
369 #include <linux/version.h>
370 #include_next <linux/average.h>
372 #include <linux/bug.h>
376 #endif /* DECLARE_EWMA */
379 * Exponentially weighted moving average (EWMA)
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.
385 * The first argument to the macro is the name that will be used
386 * for the struct and helper functions.
388 * The second argument, the precision, expresses how many bits are
389 * used for the fractional part of the fixed-precision values.
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.
397 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
398 struct ewma_##name { \
399 unsigned long internal; \
401 static inline void ewma_##name##_init(struct ewma_##name *e) \
403 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
404 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
406 * Even if you want to feed it just 0/1 you should have \
407 * some bits for the non-fractional part... \
409 BUILD_BUG_ON((_precision) > 30); \
410 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
413 static inline unsigned long \
414 ewma_##name##_read(struct ewma_##name *e) \
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); \
422 static inline void ewma_##name##_add(struct ewma_##name *e, \
425 unsigned long internal = READ_ONCE(e->internal); \
426 unsigned long weight_rcp = ilog2(_weight_rcp); \
427 unsigned long precision = _precision; \
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); \
434 WRITE_ONCE(e->internal, internal ? \
435 (((internal << weight_rcp) - internal) + \
436 (val << precision)) >> weight_rcp : \
437 (val << precision)); \
440 /* </DECLARE_EWMA> */