batman-adv: update to version 2023.1
[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_IS_LESS(5, 14, 0)
9
10 #include <linux/if_bridge.h>
11 #include <net/addrconf.h>
12
13 #if IS_ENABLED(CONFIG_IPV6)
14 static inline bool
15 br_multicast_has_router_adjacent(struct net_device *dev, int proto)
16 {
17 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
18 struct br_ip_list *br_ip_entry, *tmp;
19 int ret;
20
21 if (proto != ETH_P_IPV6)
22 return true;
23
24 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
25 if (ret < 0)
26 return true;
27
28 ret = false;
29
30 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
31 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
32 ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.dst.ip6))
33 ret = true;
34
35 list_del(&br_ip_entry->list);
36 kfree(br_ip_entry);
37 }
38
39 return ret;
40 }
41 #else
42 static inline bool
43 br_multicast_has_router_adjacent(struct net_device *dev, int proto)
44 {
45 return true;
46 }
47 #endif
48
49 #endif /* LINUX_VERSION_IS_LESS(5, 14, 0) */
50
51 #if LINUX_VERSION_IS_LESS(6, 0, 0)
52
53 #define __vstring(item, fmt, ap) __dynamic_array(char, item, 256)
54 #define __assign_vstr(dst, fmt, va) \
55 WARN_ON_ONCE(vsnprintf(__get_dynamic_array(dst), 256, fmt, *va) >= 256)
56
57 #endif /* LINUX_VERSION_IS_LESS(6, 0, 0) */
58
59 #if LINUX_VERSION_IS_LESS(6, 2, 0)
60
61 #include <linux/random.h>
62
63 #define genl_split_ops genl_ops
64
65 static inline u32 batadv_get_random_u32_below(u32 ep_ro)
66 {
67 return prandom_u32_max(ep_ro);
68 }
69
70 #define get_random_u32_below batadv_get_random_u32_below
71
72 #endif /* LINUX_VERSION_IS_LESS(6, 2, 0) */
73
74 #if LINUX_VERSION_IS_LESS(6, 4, 0)
75
76 #include <linux/if_vlan.h>
77
78 /* Prefer this version in TX path, instead of
79 * skb_reset_mac_header() + vlan_eth_hdr()
80 */
81 static inline struct vlan_ethhdr *skb_vlan_eth_hdr(const struct sk_buff *skb)
82 {
83 return (struct vlan_ethhdr *)skb->data;
84 }
85
86 #endif /* LINUX_VERSION_IS_LESS(6, 4, 0) */
87
88 /* <DECLARE_EWMA> */
89
90 #include <linux/version.h>
91 #include_next <linux/average.h>
92
93 #include <linux/bug.h>
94
95 #ifdef DECLARE_EWMA
96 #undef DECLARE_EWMA
97 #endif /* DECLARE_EWMA */
98
99 /*
100 * Exponentially weighted moving average (EWMA)
101 *
102 * This implements a fixed-precision EWMA algorithm, with both the
103 * precision and fall-off coefficient determined at compile-time
104 * and built into the generated helper funtions.
105 *
106 * The first argument to the macro is the name that will be used
107 * for the struct and helper functions.
108 *
109 * The second argument, the precision, expresses how many bits are
110 * used for the fractional part of the fixed-precision values.
111 *
112 * The third argument, the weight reciprocal, determines how the
113 * new values will be weighed vs. the old state, new values will
114 * get weight 1/weight_rcp and old values 1-1/weight_rcp. Note
115 * that this parameter must be a power of two for efficiency.
116 */
117
118 #define DECLARE_EWMA(name, _precision, _weight_rcp) \
119 struct ewma_##name { \
120 unsigned long internal; \
121 }; \
122 static inline void ewma_##name##_init(struct ewma_##name *e) \
123 { \
124 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
125 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
126 /* \
127 * Even if you want to feed it just 0/1 you should have \
128 * some bits for the non-fractional part... \
129 */ \
130 BUILD_BUG_ON((_precision) > 30); \
131 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
132 e->internal = 0; \
133 } \
134 static inline unsigned long \
135 ewma_##name##_read(struct ewma_##name *e) \
136 { \
137 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
138 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
139 BUILD_BUG_ON((_precision) > 30); \
140 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
141 return e->internal >> (_precision); \
142 } \
143 static inline void ewma_##name##_add(struct ewma_##name *e, \
144 unsigned long val) \
145 { \
146 unsigned long internal = READ_ONCE(e->internal); \
147 unsigned long weight_rcp = ilog2(_weight_rcp); \
148 unsigned long precision = _precision; \
149 \
150 BUILD_BUG_ON(!__builtin_constant_p(_precision)); \
151 BUILD_BUG_ON(!__builtin_constant_p(_weight_rcp)); \
152 BUILD_BUG_ON((_precision) > 30); \
153 BUILD_BUG_ON_NOT_POWER_OF_2(_weight_rcp); \
154 \
155 WRITE_ONCE(e->internal, internal ? \
156 (((internal << weight_rcp) - internal) + \
157 (val << precision)) >> weight_rcp : \
158 (val << precision)); \
159 }
160
161 /* </DECLARE_EWMA> */