d8de483a10243c77b2c5f49720a39dedb1404f01
[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);
52 int ipv6_mc_check_mld(struct sk_buff *skb);
53
54 #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)
55
56 #include_next <linux/igmp.h>
57 #include_next <net/addrconf.h>
58
59 static inline int batadv_ipv6_mc_check_mld1(struct sk_buff *skb)
60 {
61 return ipv6_mc_check_mld(skb, NULL);
62 }
63
64 static inline int batadv_ipv6_mc_check_mld2(struct sk_buff *skb,
65 struct sk_buff **skb_trimmed)
66 {
67 return ipv6_mc_check_mld(skb, skb_trimmed);
68 }
69
70 #define ipv6_mc_check_mld_get(_1, _2, ipv6_mc_check_mld_name, ...) ipv6_mc_check_mld_name
71 #define ipv6_mc_check_mld(...) \
72 ipv6_mc_check_mld_get(__VA_ARGS__, batadv_ipv6_mc_check_mld2, batadv_ipv6_mc_check_mld1)(__VA_ARGS__)
73
74 static inline int batadv_ip_mc_check_igmp1(struct sk_buff *skb)
75 {
76 return ip_mc_check_igmp(skb, NULL);
77 }
78
79 static inline int batadv_ip_mc_check_igmp2(struct sk_buff *skb,
80 struct sk_buff **skb_trimmed)
81 {
82 return ip_mc_check_igmp(skb, skb_trimmed);
83 }
84
85 #define ip_mc_check_igmp_get(_1, _2, ip_mc_check_igmp_name, ...) ip_mc_check_igmp_name
86 #define ip_mc_check_igmp(...) \
87 ip_mc_check_igmp_get(__VA_ARGS__, batadv_ip_mc_check_igmp2, batadv_ip_mc_check_igmp1)(__VA_ARGS__)
88
89 #endif /* < KERNEL_VERSION(4, 2, 0) */
90
91 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0)
92
93 #define IFF_NO_QUEUE 0; dev->tx_queue_len = 0
94
95 static inline bool hlist_fake(struct hlist_node *h)
96 {
97 return h->pprev == &h->next;
98 }
99
100 #endif /* < KERNEL_VERSION(4, 3, 0) */
101
102 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
103
104 #include <linux/ethtool.h>
105
106 #define ethtool_link_ksettings batadv_ethtool_link_ksettings
107
108 struct batadv_ethtool_link_ksettings {
109 struct {
110 __u32 speed;
111 __u8 duplex;
112 __u8 autoneg;
113 } base;
114 };
115
116 #define __ethtool_get_link_ksettings(__dev, __link_settings) \
117 batadv_ethtool_get_link_ksettings(__dev, __link_settings)
118
119 static inline int
120 batadv_ethtool_get_link_ksettings(struct net_device *dev,
121 struct ethtool_link_ksettings *link_ksettings)
122 {
123 struct ethtool_cmd cmd;
124 int ret;
125
126 memset(&cmd, 0, sizeof(cmd));
127 ret = __ethtool_get_settings(dev, &cmd);
128
129 if (ret != 0)
130 return ret;
131
132 link_ksettings->base.duplex = cmd.duplex;
133 link_ksettings->base.speed = ethtool_cmd_speed(&cmd);
134 link_ksettings->base.autoneg = cmd.autoneg;
135
136 return 0;
137 }
138
139 #endif /* < KERNEL_VERSION(4, 6, 0) */
140
141
142 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
143
144 #include_next <linux/cache.h>
145
146 /* hack for netlink.c which marked the family ops as ro */
147 #ifdef __ro_after_init
148 #undef __ro_after_init
149 #endif
150 #define __ro_after_init
151
152 #endif /* < KERNEL_VERSION(4, 10, 0) */
153
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
155
156 #include <linux/netdevice.h>
157
158 /* work around missing attribute needs_free_netdev and priv_destructor in
159 * net_device
160 */
161 #define ether_setup(dev) \
162 void batadv_softif_free2(struct net_device *dev) \
163 { \
164 batadv_softif_free(dev); \
165 free_netdev(dev); \
166 } \
167 void (*t1)(struct net_device *dev) __attribute__((unused)); \
168 bool t2 __attribute__((unused)); \
169 ether_setup(dev)
170 #define needs_free_netdev destructor = batadv_softif_free2; t2
171 #define priv_destructor destructor = batadv_softif_free2; t1
172
173 #endif /* < KERNEL_VERSION(4, 11, 9) */
174
175
176 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
177
178 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
179 batadv_softif_slave_add(__dev, __slave_dev)
180
181 #endif /* < KERNEL_VERSION(4, 15, 0) */
182
183
184 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
185
186 static inline int batadv_access_ok(int type, const void __user *p,
187 unsigned long size)
188 {
189 return access_ok(type, p, size);
190 }
191
192 #ifdef access_ok
193 #undef access_ok
194 #endif
195
196 #define access_ok_get(_1, _2, _3 , access_ok_name, ...) access_ok_name
197 #define access_ok(...) \
198 access_ok_get(__VA_ARGS__, access_ok3, access_ok2)(__VA_ARGS__)
199
200 #define access_ok2(addr, size) batadv_access_ok(VERIFY_WRITE, (addr), (size))
201 #define access_ok3(type, addr, size) batadv_access_ok((type), (addr), (size))
202
203 #endif /* < KERNEL_VERSION(5, 0, 0) */
204
205 /* <DECLARE_EWMA> */
206
207 #include <linux/version.h>
208 #include_next <linux/average.h>
209
210 #include <linux/bug.h>
211
212 #ifdef DECLARE_EWMA
213 #undef DECLARE_EWMA
214 #endif /* DECLARE_EWMA */
215
216 /*
217 * Exponentially weighted moving average (EWMA)
218 *
219 * This implements a fixed-precision EWMA algorithm, with both the
220 * precision and fall-off coefficient determined at compile-time
221 * and built into the generated helper funtions.
222 *
223 * The first argument to the macro is the name that will be used
224 * for the struct and helper functions.
225 *
226 * The second argument, the precision, expresses how many bits are
227 * used for the fractional part of the fixed-precision values.
228 *
229 * The third argument, the weight reciprocal, determines how the
230 * new values will be weighed vs. the old state, new values will
231 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
232 * that this parameter must be a power of two for efficiency.
233 */
234
235 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
236 struct ewma_##name { \
237 unsigned long internal; \
238 }; \
239 static inline void ewma_##name##_init(struct ewma_##name *e) \
240 { \
241 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
242 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
243 /* \
244 * Even if you want to feed it just 0/1 you should have \
245 * some bits for the non-fractional part... \
246 */ \
247 BUILD_BUG_ON((_precision) > 30); \
248 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
249 e->internal = 0; \
250 } \
251 static inline unsigned long \
252 ewma_##name##_read(struct ewma_##name *e) \
253 { \
254 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
255 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
256 BUILD_BUG_ON((_precision) > 30); \
257 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
258 return e->internal >> (_precision); \
259 } \
260 static inline void ewma_##name##_add(struct ewma_##name *e, \
261 unsigned long val) \
262 { \
263 unsigned long internal = READ_ONCE(e->internal); \
264 unsigned long weight_rcp = ilog2(_weight_rcp); \
265 unsigned long precision = _precision; \
266 \
267 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
268 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
269 BUILD_BUG_ON((_precision) > 30); \
270 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
271 \
272 WRITE_ONCE(e->internal, internal ? \
273 (((internal << weight_rcp) - internal) + \
274 (val << precision)) >> weight_rcp : \
275 (val << precision)); \
276 }
277
278 /* </DECLARE_EWMA> */