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