batman-adv: upgrade package to latest release 2020.4
[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, 15, 0)
9
10 #include <linux/netdevice.h>
11
12 #define netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info, extack) ({\
13 BUILD_BUG_ON(extack != NULL); \
14 netdev_master_upper_dev_link(dev, upper_dev, upper_priv, upper_info); \
15 })
16
17 #endif /* < KERNEL_VERSION(4, 15, 0) */
18
19
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
21
22 #ifndef sizeof_field
23 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
24 #endif
25
26 #endif /* < KERNEL_VERSION(4, 16, 0) */
27
28
29 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)
30
31 #include_next <linux/igmp.h>
32 #include_next <net/addrconf.h>
33
34 static inline int batadv_ipv6_mc_check_mld1(struct sk_buff *skb)
35 {
36 return ipv6_mc_check_mld(skb, NULL);
37 }
38
39 static inline int batadv_ipv6_mc_check_mld2(struct sk_buff *skb,
40 struct sk_buff **skb_trimmed)
41 {
42 return ipv6_mc_check_mld(skb, skb_trimmed);
43 }
44
45 #define ipv6_mc_check_mld_get(_1, _2, ipv6_mc_check_mld_name, ...) ipv6_mc_check_mld_name
46 #define ipv6_mc_check_mld(...) \
47 ipv6_mc_check_mld_get(__VA_ARGS__, batadv_ipv6_mc_check_mld2, batadv_ipv6_mc_check_mld1)(__VA_ARGS__)
48
49 static inline int batadv_ip_mc_check_igmp1(struct sk_buff *skb)
50 {
51 return ip_mc_check_igmp(skb, NULL);
52 }
53
54 static inline int batadv_ip_mc_check_igmp2(struct sk_buff *skb,
55 struct sk_buff **skb_trimmed)
56 {
57 return ip_mc_check_igmp(skb, skb_trimmed);
58 }
59
60 #define ip_mc_check_igmp_get(_1, _2, ip_mc_check_igmp_name, ...) ip_mc_check_igmp_name
61 #define ip_mc_check_igmp(...) \
62 ip_mc_check_igmp_get(__VA_ARGS__, batadv_ip_mc_check_igmp2, batadv_ip_mc_check_igmp1)(__VA_ARGS__)
63
64 #endif /* < KERNEL_VERSION(5, 1, 0) */
65
66
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
68
69 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
70 batadv_softif_slave_add(__dev, __slave_dev)
71
72 #endif /* < KERNEL_VERSION(4, 15, 0) */
73
74
75 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
76
77 static inline int batadv_access_ok(int type, const void __user *p,
78 unsigned long size)
79 {
80 return access_ok(type, p, size);
81 }
82
83 #ifdef access_ok
84 #undef access_ok
85 #endif
86
87 #define access_ok_get(_1, _2, _3 , access_ok_name, ...) access_ok_name
88 #define access_ok(...) \
89 access_ok_get(__VA_ARGS__, access_ok3, access_ok2)(__VA_ARGS__)
90
91 #define access_ok2(addr, size) batadv_access_ok(VERIFY_WRITE, (addr), (size))
92 #define access_ok3(type, addr, size) batadv_access_ok((type), (addr), (size))
93
94 #endif /* < KERNEL_VERSION(5, 0, 0) */
95
96 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0)
97
98 #ifndef fallthrough
99 #if __GNUC__ > 7 && !defined(__CHECKER__)
100 # define fallthrough __attribute__((__fallthrough__))
101 #else
102 # define fallthrough do {} while (0) /* fallthrough */
103 #endif
104 #endif
105
106 #endif /* < KERNEL_VERSION(5, 4, 0) */
107
108 #if LINUX_VERSION_IS_LESS(5, 10, 0)
109
110 #include <linux/if_bridge.h>
111
112 struct batadv_br_ip {
113 union {
114 __be32 ip4;
115 #if IS_ENABLED(CONFIG_IPV6)
116 struct in6_addr ip6;
117 #endif
118 } dst;
119 __be16 proto;
120 __u16 vid;
121 };
122
123 struct batadv_br_ip_list {
124 struct list_head list;
125 struct batadv_br_ip addr;
126 };
127
128 #if 0
129 /* "static" dropped to force compiler to evaluate it as part of multicast.c
130 * might need to be added again and then called in some kind of dummy
131 * compat.c in case this header is included in multiple files.
132 */
133 inline void __batadv_br_ip_list_check(void)
134 {
135 BUILD_BUG_ON(sizeof(struct batadv_br_ip_list) != sizeof(struct br_ip_list));
136 BUILD_BUG_ON(offsetof(struct batadv_br_ip_list, list) != offsetof(struct br_ip_list, list));
137 BUILD_BUG_ON(offsetof(struct batadv_br_ip_list, addr) != offsetof(struct br_ip_list, addr));
138
139 BUILD_BUG_ON(sizeof(struct batadv_br_ip) != sizeof(struct br_ip));
140 BUILD_BUG_ON(offsetof(struct batadv_br_ip, dst.ip4) != offsetof(struct br_ip, u.ip4));
141 BUILD_BUG_ON(offsetof(struct batadv_br_ip, dst.ip6) != offsetof(struct br_ip, u.ip6));
142 BUILD_BUG_ON(offsetof(struct batadv_br_ip, proto) != offsetof(struct br_ip, proto));
143 BUILD_BUG_ON(offsetof(struct batadv_br_ip, vid) != offsetof(struct br_ip, vid));
144 }
145 #endif
146
147 #define br_ip batadv_br_ip
148 #define br_ip_list batadv_br_ip_list
149
150 #endif /* LINUX_VERSION_IS_LESS(5, 10, 0) */
151
152 /* <DECLARE_EWMA> */
153
154 #include <linux/version.h>
155 #include_next <linux/average.h>
156
157 #include <linux/bug.h>
158
159 #ifdef DECLARE_EWMA
160 #undef DECLARE_EWMA
161 #endif /* DECLARE_EWMA */
162
163 /*
164 * Exponentially weighted moving average (EWMA)
165 *
166 * This implements a fixed-precision EWMA algorithm, with both the
167 * precision and fall-off coefficient determined at compile-time
168 * and built into the generated helper funtions.
169 *
170 * The first argument to the macro is the name that will be used
171 * for the struct and helper functions.
172 *
173 * The second argument, the precision, expresses how many bits are
174 * used for the fractional part of the fixed-precision values.
175 *
176 * The third argument, the weight reciprocal, determines how the
177 * new values will be weighed vs. the old state, new values will
178 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
179 * that this parameter must be a power of two for efficiency.
180 */
181
182 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
183 struct ewma_##name { \
184 unsigned long internal; \
185 }; \
186 static inline void ewma_##name##_init(struct ewma_##name *e) \
187 { \
188 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
189 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
190 /* \
191 * Even if you want to feed it just 0/1 you should have \
192 * some bits for the non-fractional part... \
193 */ \
194 BUILD_BUG_ON((_precision) > 30); \
195 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
196 e->internal = 0; \
197 } \
198 static inline unsigned long \
199 ewma_##name##_read(struct ewma_##name *e) \
200 { \
201 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
202 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
203 BUILD_BUG_ON((_precision) > 30); \
204 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
205 return e->internal >> (_precision); \
206 } \
207 static inline void ewma_##name##_add(struct ewma_##name *e, \
208 unsigned long val) \
209 { \
210 unsigned long internal = READ_ONCE(e->internal); \
211 unsigned long weight_rcp = ilog2(_weight_rcp); \
212 unsigned long precision = _precision; \
213 \
214 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
215 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
216 BUILD_BUG_ON((_precision) > 30); \
217 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
218 \
219 WRITE_ONCE(e->internal, internal ? \
220 (((internal << weight_rcp) - internal) + \
221 (val << precision)) >> weight_rcp : \
222 (val << precision)); \
223 }
224
225 /* </DECLARE_EWMA> */