2085767dcd83400e263313fe29f43c256e8a5dd0
[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(5, 1, 0)
21
22 #include_next <linux/igmp.h>
23 #include_next <net/addrconf.h>
24
25 static inline int batadv_ipv6_mc_check_mld1(struct sk_buff *skb)
26 {
27 return ipv6_mc_check_mld(skb, NULL);
28 }
29
30 static inline int batadv_ipv6_mc_check_mld2(struct sk_buff *skb,
31 struct sk_buff **skb_trimmed)
32 {
33 return ipv6_mc_check_mld(skb, skb_trimmed);
34 }
35
36 #define ipv6_mc_check_mld_get(_1, _2, ipv6_mc_check_mld_name, ...) ipv6_mc_check_mld_name
37 #define ipv6_mc_check_mld(...) \
38 ipv6_mc_check_mld_get(__VA_ARGS__, batadv_ipv6_mc_check_mld2, batadv_ipv6_mc_check_mld1)(__VA_ARGS__)
39
40 static inline int batadv_ip_mc_check_igmp1(struct sk_buff *skb)
41 {
42 return ip_mc_check_igmp(skb, NULL);
43 }
44
45 static inline int batadv_ip_mc_check_igmp2(struct sk_buff *skb,
46 struct sk_buff **skb_trimmed)
47 {
48 return ip_mc_check_igmp(skb, skb_trimmed);
49 }
50
51 #define ip_mc_check_igmp_get(_1, _2, ip_mc_check_igmp_name, ...) ip_mc_check_igmp_name
52 #define ip_mc_check_igmp(...) \
53 ip_mc_check_igmp_get(__VA_ARGS__, batadv_ip_mc_check_igmp2, batadv_ip_mc_check_igmp1)(__VA_ARGS__)
54
55 #endif /* < KERNEL_VERSION(5, 1, 0) */
56
57
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
59
60 #include_next <linux/cache.h>
61
62 /* hack for netlink.c which marked the family ops as ro */
63 #ifdef __ro_after_init
64 #undef __ro_after_init
65 #endif
66 #define __ro_after_init
67
68 #endif /* < KERNEL_VERSION(4, 10, 0) */
69
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 9)
71
72 #include <linux/netdevice.h>
73
74 /* work around missing attribute needs_free_netdev and priv_destructor in
75 * net_device
76 */
77 #define ether_setup(dev) \
78 void batadv_softif_free2(struct net_device *dev) \
79 { \
80 batadv_softif_free(dev); \
81 free_netdev(dev); \
82 } \
83 void (*t1)(struct net_device *dev) __attribute__((unused)); \
84 bool t2 __attribute__((unused)); \
85 ether_setup(dev)
86 #define needs_free_netdev destructor = batadv_softif_free2; t2
87 #define priv_destructor destructor = batadv_softif_free2; t1
88
89 #endif /* < KERNEL_VERSION(4, 11, 9) */
90
91
92 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
93
94 #define batadv_softif_slave_add(__dev, __slave_dev, __extack) \
95 batadv_softif_slave_add(__dev, __slave_dev)
96
97 #endif /* < KERNEL_VERSION(4, 15, 0) */
98
99
100 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
101
102 static inline int batadv_access_ok(int type, const void __user *p,
103 unsigned long size)
104 {
105 return access_ok(type, p, size);
106 }
107
108 #ifdef access_ok
109 #undef access_ok
110 #endif
111
112 #define access_ok_get(_1, _2, _3 , access_ok_name, ...) access_ok_name
113 #define access_ok(...) \
114 access_ok_get(__VA_ARGS__, access_ok3, access_ok2)(__VA_ARGS__)
115
116 #define access_ok2(addr, size) batadv_access_ok(VERIFY_WRITE, (addr), (size))
117 #define access_ok3(type, addr, size) batadv_access_ok((type), (addr), (size))
118
119 #endif /* < KERNEL_VERSION(5, 0, 0) */
120
121 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0)
122
123 #ifndef fallthrough
124 #if __GNUC__ > 7 && !defined(__CHECKER__)
125 # define fallthrough __attribute__((__fallthrough__))
126 #else
127 # define fallthrough do {} while (0) /* fallthrough */
128 #endif
129 #endif
130
131 #endif /* < KERNEL_VERSION(5, 4, 0) */
132
133 /* <DECLARE_EWMA> */
134
135 #include <linux/version.h>
136 #include_next <linux/average.h>
137
138 #include <linux/bug.h>
139
140 #ifdef DECLARE_EWMA
141 #undef DECLARE_EWMA
142 #endif /* DECLARE_EWMA */
143
144 /*
145 * Exponentially weighted moving average (EWMA)
146 *
147 * This implements a fixed-precision EWMA algorithm, with both the
148 * precision and fall-off coefficient determined at compile-time
149 * and built into the generated helper funtions.
150 *
151 * The first argument to the macro is the name that will be used
152 * for the struct and helper functions.
153 *
154 * The second argument, the precision, expresses how many bits are
155 * used for the fractional part of the fixed-precision values.
156 *
157 * The third argument, the weight reciprocal, determines how the
158 * new values will be weighed vs. the old state, new values will
159 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
160 * that this parameter must be a power of two for efficiency.
161 */
162
163 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
164 struct ewma_##name { \
165 unsigned long internal; \
166 }; \
167 static inline void ewma_##name##_init(struct ewma_##name *e) \
168 { \
169 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
170 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
171 /* \
172 * Even if you want to feed it just 0/1 you should have \
173 * some bits for the non-fractional part... \
174 */ \
175 BUILD_BUG_ON((_precision) > 30); \
176 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
177 e->internal = 0; \
178 } \
179 static inline unsigned long \
180 ewma_##name##_read(struct ewma_##name *e) \
181 { \
182 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
183 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
184 BUILD_BUG_ON((_precision) > 30); \
185 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
186 return e->internal >> (_precision); \
187 } \
188 static inline void ewma_##name##_add(struct ewma_##name *e, \
189 unsigned long val) \
190 { \
191 unsigned long internal = READ_ONCE(e->internal); \
192 unsigned long weight_rcp = ilog2(_weight_rcp); \
193 unsigned long precision = _precision; \
194 \
195 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
196 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
197 BUILD_BUG_ON((_precision) > 30); \
198 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
199 \
200 WRITE_ONCE(e->internal, internal ? \
201 (((internal << weight_rcp) - internal) + \
202 (val << precision)) >> weight_rcp : \
203 (val << precision)); \
204 }
205
206 /* </DECLARE_EWMA> */