xref: /linux/net/batman-adv/main.h (revision 78c1930198fc63f2d4761848cbe148c5b2958b01)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  */
6 
7 #ifndef _NET_BATMAN_ADV_MAIN_H_
8 #define _NET_BATMAN_ADV_MAIN_H_
9 
10 #define BATADV_DRIVER_AUTHOR "Marek Lindner <marek.lindner@mailbox.org>, " \
11 			     "Simon Wunderlich <sw@simonwunderlich.de>"
12 #define BATADV_DRIVER_DESC   "B.A.T.M.A.N. advanced"
13 #define BATADV_DRIVER_DEVICE "batman-adv"
14 
15 /* B.A.T.M.A.N. parameters */
16 
17 #define BATADV_TQ_MAX_VALUE 255
18 #define BATADV_THROUGHPUT_MAX_VALUE 0xFFFFFFFF
19 #define BATADV_JITTER 20
20 
21 #define BATADV_MAX_MTU (ETH_MAX_MTU - batadv_max_header_len())
22 
23 /* Time To Live of broadcast messages */
24 #define BATADV_TTL 50
25 
26 /* maximum sequence number age of broadcast messages */
27 #define BATADV_BCAST_MAX_AGE 64
28 
29 /* purge originators after time in seconds if no valid packet comes in
30  * -> TODO: check influence on BATADV_TQ_LOCAL_WINDOW_SIZE
31  */
32 #define BATADV_PURGE_TIMEOUT 200000 /* 200 seconds */
33 #define BATADV_TT_LOCAL_TIMEOUT 600000 /* in milliseconds */
34 #define BATADV_TT_CLIENT_ROAM_TIMEOUT 600000 /* in milliseconds */
35 #define BATADV_TT_CLIENT_TEMP_TIMEOUT 600000 /* in milliseconds */
36 #define BATADV_TT_WORK_PERIOD 5000 /* 5 seconds */
37 #define BATADV_ORIG_WORK_PERIOD 1000 /* 1 second */
38 #define BATADV_MCAST_WORK_PERIOD 500 /* 0.5 seconds */
39 #define BATADV_DAT_ENTRY_TIMEOUT (5 * 60000) /* 5 mins in milliseconds */
40 /* sliding packet range of received originator messages in sequence numbers
41  * (should be a multiple of our word size)
42  */
43 #define BATADV_TQ_LOCAL_WINDOW_SIZE 64
44 /* milliseconds we have to keep pending tt_req */
45 #define BATADV_TT_REQUEST_TIMEOUT 3000
46 
47 #define BATADV_TQ_GLOBAL_WINDOW_SIZE 5
48 #define BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
49 #define BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
50 #define BATADV_TQ_TOTAL_BIDRECT_LIMIT 1
51 
52 /* B.A.T.M.A.N. V */
53 #define BATADV_THROUGHPUT_DEFAULT_VALUE 10 /* 1 Mbps */
54 #define BATADV_ELP_PROBES_PER_NODE 2
55 #define BATADV_ELP_MIN_PROBE_SIZE 200 /* bytes */
56 #define BATADV_ELP_PROBE_MAX_TX_DIFF 100 /* milliseconds */
57 #define BATADV_ELP_MAX_AGE 64
58 #define BATADV_OGM_MAX_ORIGDIFF 5
59 #define BATADV_OGM_MAX_AGE 64
60 
61 /* number of OGMs sent with the last tt diff */
62 #define BATADV_TT_OGM_APPEND_MAX 3
63 
64 /* Time in which a client can roam at most ROAMING_MAX_COUNT times in
65  * milliseconds
66  */
67 #define BATADV_ROAMING_MAX_TIME 20000
68 #define BATADV_ROAMING_MAX_COUNT 5
69 
70 #define BATADV_NO_FLAGS 0
71 
72 #define BATADV_NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
73 
74 #define BATADV_NO_MARK 0
75 
76 /* default interface for multi interface operation. The default interface is
77  * used for communication which originated locally (i.e. is not forwarded)
78  * or where special forwarding is not desired/necessary.
79  */
80 #define BATADV_IF_DEFAULT	((struct batadv_hard_iface *)NULL)
81 
82 #define BATADV_NUM_WORDS BITS_TO_LONGS(BATADV_TQ_LOCAL_WINDOW_SIZE)
83 
84 #define BATADV_LOG_BUF_LEN 8192	  /* has to be a power of 2 */
85 
86 /* number of packets to send for broadcasts on different interface types */
87 #define BATADV_NUM_BCASTS_DEFAULT 1
88 #define BATADV_NUM_BCASTS_WIRELESS 3
89 
90 /* length of the single packet used by the TP meter */
91 #define BATADV_TP_PACKET_LEN ETH_DATA_LEN
92 
93 /* msecs after which an ARP_REQUEST is sent in broadcast as fallback */
94 #define ARP_REQ_DELAY 250
95 /* numbers of originator to contact for any PUT/GET DHT operation */
96 #define BATADV_DAT_CANDIDATES_NUM 3
97 
98 /* BATADV_TQ_SIMILARITY_THRESHOLD - TQ points that a secondary metric can differ
99  * at most from the primary one in order to be still considered acceptable
100  */
101 #define BATADV_TQ_SIMILARITY_THRESHOLD 50
102 
103 #define BATADV_MAX_AGGREGATION_PACKETS 32
104 #define BATADV_MAX_AGGREGATION_BYTES 512
105 #define BATADV_MAX_AGGREGATION_MS 100
106 
107 #define BATADV_BLA_PERIOD_LENGTH	10000	/* 10 seconds */
108 #define BATADV_BLA_BACKBONE_TIMEOUT	(BATADV_BLA_PERIOD_LENGTH * 6)
109 #define BATADV_BLA_CLAIM_TIMEOUT	(BATADV_BLA_PERIOD_LENGTH * 10)
110 #define BATADV_BLA_WAIT_PERIODS		3
111 #define BATADV_BLA_LOOPDETECT_PERIODS	6
112 #define BATADV_BLA_LOOPDETECT_TIMEOUT	3000	/* 3 seconds */
113 
114 #define BATADV_DUPLIST_SIZE		16
115 #define BATADV_DUPLIST_TIMEOUT		500	/* 500 ms */
116 /* don't reset again within 30 seconds */
117 #define BATADV_RESET_PROTECTION_MS 30000
118 #define BATADV_EXPECTED_SEQNO_RANGE	65536
119 
120 /**
121  * BATADV_TP_MAX_NUM - maximum number of simultaneously active tp sessions
122  */
123 #define BATADV_TP_MAX_NUM 5
124 
125 /**
126  * enum batadv_mesh_state - State of a mesh interface
127  */
128 enum batadv_mesh_state {
129 	/** @BATADV_MESH_INACTIVE: mesh interface is not yet running */
130 	BATADV_MESH_INACTIVE,
131 
132 	/** @BATADV_MESH_ACTIVE: interface is up and running */
133 	BATADV_MESH_ACTIVE,
134 
135 	/** @BATADV_MESH_DEACTIVATING: interface is getting shut down */
136 	BATADV_MESH_DEACTIVATING,
137 };
138 
139 #define BATADV_BCAST_QUEUE_LEN		256
140 #define BATADV_BATMAN_QUEUE_LEN	256
141 
142 /**
143  * enum batadv_uev_action - action type of uevent
144  */
145 enum batadv_uev_action {
146 	/** @BATADV_UEV_ADD: gateway was selected (after none was selected) */
147 	BATADV_UEV_ADD = 0,
148 
149 	/**
150 	 * @BATADV_UEV_DEL: selected gateway was removed and none is selected
151 	 * anymore
152 	 */
153 	BATADV_UEV_DEL,
154 
155 	/**
156 	 * @BATADV_UEV_CHANGE: a different gateway was selected as based gateway
157 	 */
158 	BATADV_UEV_CHANGE,
159 
160 	/**
161 	 * @BATADV_UEV_LOOPDETECT: loop was detected which cannot be handled by
162 	 * bridge loop avoidance
163 	 */
164 	BATADV_UEV_LOOPDETECT,
165 };
166 
167 /**
168  * enum batadv_uev_type - Type of uevent
169  */
170 enum batadv_uev_type {
171 	/** @BATADV_UEV_GW: selected gateway was modified */
172 	BATADV_UEV_GW = 0,
173 
174 	/** @BATADV_UEV_BLA: bridge loop avoidance event */
175 	BATADV_UEV_BLA,
176 };
177 
178 #define BATADV_GW_THRESHOLD	50
179 
180 /* Number of fragment chains for each orig_node */
181 #define BATADV_FRAG_BUFFER_COUNT 8
182 /* Maximum number of fragments for one packet */
183 #define BATADV_FRAG_MAX_FRAGMENTS 16
184 /* Maxumim size of each fragment */
185 #define BATADV_FRAG_MAX_FRAG_SIZE 1280
186 /* Time to keep fragments while waiting for rest of the fragments */
187 #define BATADV_FRAG_TIMEOUT 10000
188 
189 #define BATADV_DAT_CANDIDATE_NOT_FOUND	0
190 #define BATADV_DAT_CANDIDATE_ORIG	1
191 
192 /* Debug Messages */
193 #ifdef pr_fmt
194 #undef pr_fmt
195 #endif
196 /* Append 'batman-adv: ' before kernel messages */
197 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
198 
199 /* Kernel headers */
200 
201 #include <linux/atomic.h>
202 #include <linux/compiler.h>
203 #include <linux/etherdevice.h>
204 #include <linux/if_vlan.h>
205 #include <linux/jiffies.h>
206 #include <linux/netdevice.h>
207 #include <linux/percpu.h>
208 #include <linux/skbuff.h>
209 #include <linux/types.h>
210 #include <uapi/linux/batadv_packet.h>
211 
212 #include "types.h"
213 #include "main.h"
214 
215 /**
216  * batadv_print_vid() - return printable version of vid information
217  * @vid: the VLAN identifier
218  *
219  * Return: -1 when no VLAN is used, VLAN id otherwise
220  */
221 static inline int batadv_print_vid(unsigned short vid)
222 {
223 	if (vid & BATADV_VLAN_HAS_TAG)
224 		return (int)(vid & VLAN_VID_MASK);
225 	else
226 		return -1;
227 }
228 
229 extern struct list_head batadv_hardif_list;
230 extern unsigned int batadv_hardif_generation;
231 
232 extern struct workqueue_struct *batadv_event_workqueue;
233 
234 int batadv_mesh_init(struct net_device *mesh_iface);
235 void batadv_mesh_free(struct net_device *mesh_iface);
236 bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr);
237 int batadv_max_header_len(void);
238 void batadv_skb_set_priority(struct sk_buff *skb, int offset);
239 int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
240 			   struct packet_type *ptype,
241 			   struct net_device *orig_dev);
242 int
243 batadv_recv_handler_register(u8 packet_type,
244 			     int (*recv_handler)(struct sk_buff *,
245 						 struct batadv_hard_iface *));
246 void batadv_recv_handler_unregister(u8 packet_type);
247 
248 /**
249  * batadv_compare_eth() - Compare two not u16 aligned Ethernet addresses
250  * @data1: Pointer to a six-byte array containing the Ethernet address
251  * @data2: Pointer other six-byte array containing the Ethernet address
252  *
253  * note: can't use ether_addr_equal() as it requires aligned memory
254  *
255  * Return: true if they are the same ethernet addr
256  */
257 static inline bool batadv_compare_eth(const void *data1, const void *data2)
258 {
259 	return ether_addr_equal_unaligned(data1, data2);
260 }
261 
262 /**
263  * batadv_has_timed_out() - compares current time (jiffies) and timestamp +
264  *  timeout
265  * @timestamp:		base value to compare with (in jiffies)
266  * @timeout:		added to base value before comparing (in milliseconds)
267  *
268  * Return: true if current time is after timestamp + timeout
269  */
270 static inline bool batadv_has_timed_out(unsigned long timestamp,
271 					unsigned int timeout)
272 {
273 	return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout));
274 }
275 
276 /**
277  * batadv_atomic_dec_not_zero() - Decrease unless the number is 0
278  * @v: pointer of type atomic_t
279  *
280  * Return: non-zero if v was not 0, and zero otherwise.
281  */
282 #define batadv_atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
283 
284 /**
285  * batadv_smallest_signed_int() - Returns the smallest signed integer in two's
286  *  complement with the sizeof x
287  * @x: type of integer
288  *
289  * Return: smallest signed integer of type
290  */
291 #define batadv_smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
292 
293 /**
294  * batadv_seq_before() - Checks if a sequence number x is a predecessor of y
295  * @x: potential predecessor of @y
296  * @y: value to compare @x against
297  *
298  * It handles overflows/underflows and can correctly check for a predecessor
299  * unless the variable sequence number has grown by more than
300  * 2**(bitwidth(x)-1)-1.
301  *
302  * This means that for a u8 with the maximum value 255, it would think:
303  *
304  * * when adding nothing - it is neither a predecessor nor a successor
305  * * before adding more than 127 to the starting value - it is a predecessor,
306  * * when adding 128 - it is neither a predecessor nor a successor,
307  * * after adding more than 127 to the starting value - it is a successor
308  *
309  * Return: true when x is a predecessor of y, false otherwise
310  */
311 #define batadv_seq_before(x, y) ({ \
312 	typeof(x)_d1 = (x); \
313 	typeof(y)_d2 = (y); \
314 	typeof(x)_dummy = (_d1 - _d2); \
315 	(void)(&_d1 == &_d2); \
316 	_dummy > batadv_smallest_signed_int(_dummy); \
317 })
318 
319 /**
320  * batadv_seq_after() - Checks if a sequence number x is a successor of y
321  * @x: potential successor of @y
322  * @y: value to compare @x against
323  *
324  * It handles overflows/underflows and can correctly check for a successor
325  * unless the variable sequence number has grown by more than
326  * 2**(bitwidth(x)-1)-1.
327  *
328  * This means that for a u8 with the maximum value 255, it would think:
329  *
330  * * when adding nothing - it is neither a predecessor nor a successor
331  * * before adding more than 127 to the starting value - it is a predecessor,
332  * * when adding 128 - it is neither a predecessor nor a successor,
333  * * after adding more than 127 to the starting value - it is a successor
334  *
335  * Return: true when x is a successor of y, false otherwise
336  */
337 #define batadv_seq_after(x, y) batadv_seq_before(y, x)
338 
339 /**
340  * batadv_add_counter() - Add to per cpu statistics counter of mesh interface
341  * @bat_priv: the bat priv with all the mesh interface information
342  * @idx: counter index which should be modified
343  * @count: value to increase counter by
344  *
345  * Stop preemption on local cpu while incrementing the counter
346  */
347 static inline void batadv_add_counter(struct batadv_priv *bat_priv, size_t idx,
348 				      size_t count)
349 {
350 	this_cpu_add(bat_priv->bat_counters[idx], count);
351 }
352 
353 /**
354  * batadv_inc_counter() - Increase per cpu statistics counter of mesh interface
355  * @b: the bat priv with all the mesh interface information
356  * @i: counter index which should be modified
357  */
358 #define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1)
359 
360 /**
361  * BATADV_SKB_CB() - Get batadv_skb_cb from skb control buffer
362  * @__skb: skb holding the control buffer
363  *
364  * The members of the control buffer are defined in struct batadv_skb_cb in
365  * types.h. The macro is inspired by the similar macro TCP_SKB_CB() in tcp.h.
366  *
367  * Return: pointer to the batadv_skb_cb of the skb
368  */
369 #define BATADV_SKB_CB(__skb)       ((struct batadv_skb_cb *)&((__skb)->cb[0]))
370 
371 unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len);
372 bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid);
373 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
374 			enum batadv_uev_action action, const char *data);
375 
376 #endif /* _NET_BATMAN_ADV_MAIN_H_ */
377