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