17db7d9f3SSven Eckelmann /* SPDX-License-Identifier: GPL-2.0 */
2cfa55c6dSSven Eckelmann /* Copyright (C) B.A.T.M.A.N. contributors:
3c6c8fea2SSven Eckelmann *
4c6c8fea2SSven Eckelmann * Marek Lindner, Simon Wunderlich
5c6c8fea2SSven Eckelmann */
6c6c8fea2SSven Eckelmann
7c6c8fea2SSven Eckelmann #ifndef _NET_BATMAN_ADV_MAIN_H_
8c6c8fea2SSven Eckelmann #define _NET_BATMAN_ADV_MAIN_H_
9c6c8fea2SSven Eckelmann
10bc58eeefSMarek Lindner #define BATADV_DRIVER_AUTHOR "Marek Lindner <mareklindner@neomailbox.ch>, " \
11c679ff8fSSimon Wunderlich "Simon Wunderlich <sw@simonwunderlich.de>"
1242d0b044SSven Eckelmann #define BATADV_DRIVER_DESC "B.A.T.M.A.N. advanced"
1342d0b044SSven Eckelmann #define BATADV_DRIVER_DEVICE "batman-adv"
14c6c8fea2SSven Eckelmann
1542d0b044SSven Eckelmann #ifndef BATADV_SOURCE_VERSION
16*3ae5cb7eSSimon Wunderlich #define BATADV_SOURCE_VERSION "2024.2"
1744c4349aSSven Eckelmann #endif
18c6c8fea2SSven Eckelmann
19c6c8fea2SSven Eckelmann /* B.A.T.M.A.N. parameters */
20c6c8fea2SSven Eckelmann
2142d0b044SSven Eckelmann #define BATADV_TQ_MAX_VALUE 255
220da00359SAntonio Quartulli #define BATADV_THROUGHPUT_MAX_VALUE 0xFFFFFFFF
2342d0b044SSven Eckelmann #define BATADV_JITTER 20
24c6c8fea2SSven Eckelmann
256e215fd8SSven Eckelmann /* Time To Live of broadcast messages */
2642d0b044SSven Eckelmann #define BATADV_TTL 50
276e215fd8SSven Eckelmann
2881f02683SSimon Wunderlich /* maximum sequence number age of broadcast messages */
2981f02683SSimon Wunderlich #define BATADV_BCAST_MAX_AGE 64
3081f02683SSimon Wunderlich
316e215fd8SSven Eckelmann /* purge originators after time in seconds if no valid packet comes in
3242d0b044SSven Eckelmann * -> TODO: check influence on BATADV_TQ_LOCAL_WINDOW_SIZE
339cfc7bd6SSven Eckelmann */
3442d0b044SSven Eckelmann #define BATADV_PURGE_TIMEOUT 200000 /* 200 seconds */
357cf4d520SAntonio Quartulli #define BATADV_TT_LOCAL_TIMEOUT 600000 /* in milliseconds */
36a51fb9b2SAntonio Quartulli #define BATADV_TT_CLIENT_ROAM_TIMEOUT 600000 /* in milliseconds */
3730cfd02bSAntonio Quartulli #define BATADV_TT_CLIENT_TEMP_TIMEOUT 600000 /* in milliseconds */
3872414442SAntonio Quartulli #define BATADV_TT_WORK_PERIOD 5000 /* 5 seconds */
3972414442SAntonio Quartulli #define BATADV_ORIG_WORK_PERIOD 1000 /* 1 second */
40cbebd363SLinus Lüssing #define BATADV_MCAST_WORK_PERIOD 500 /* 0.5 seconds */
412f1dfbe1SAntonio Quartulli #define BATADV_DAT_ENTRY_TIMEOUT (5 * 60000) /* 5 mins in milliseconds */
42015758d0SAntonio Quartulli /* sliding packet range of received originator messages in sequence numbers
439cfc7bd6SSven Eckelmann * (should be a multiple of our word size)
449cfc7bd6SSven Eckelmann */
4542d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_WINDOW_SIZE 64
46a51fb9b2SAntonio Quartulli /* milliseconds we have to keep pending tt_req */
4742d0b044SSven Eckelmann #define BATADV_TT_REQUEST_TIMEOUT 3000
48a73105b8SAntonio Quartulli
4942d0b044SSven Eckelmann #define BATADV_TQ_GLOBAL_WINDOW_SIZE 5
5042d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
5142d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
5242d0b044SSven Eckelmann #define BATADV_TQ_TOTAL_BIDRECT_LIMIT 1
53c6c8fea2SSven Eckelmann
54162bd64cSLinus Luessing /* B.A.T.M.A.N. V */
55c833484eSAntonio Quartulli #define BATADV_THROUGHPUT_DEFAULT_VALUE 10 /* 1 Mbps */
568d2d499eSAntonio Quartulli #define BATADV_ELP_PROBES_PER_NODE 2
578d2d499eSAntonio Quartulli #define BATADV_ELP_MIN_PROBE_SIZE 200 /* bytes */
588d2d499eSAntonio Quartulli #define BATADV_ELP_PROBE_MAX_TX_DIFF 100 /* milliseconds */
59162bd64cSLinus Luessing #define BATADV_ELP_MAX_AGE 64
609323158eSAntonio Quartulli #define BATADV_OGM_MAX_ORIGDIFF 5
619323158eSAntonio Quartulli #define BATADV_OGM_MAX_AGE 64
62162bd64cSLinus Luessing
6342d0b044SSven Eckelmann /* number of OGMs sent with the last tt diff */
6442d0b044SSven Eckelmann #define BATADV_TT_OGM_APPEND_MAX 3
65a73105b8SAntonio Quartulli
669cfc7bd6SSven Eckelmann /* Time in which a client can roam at most ROAMING_MAX_COUNT times in
67a51fb9b2SAntonio Quartulli * milliseconds
689cfc7bd6SSven Eckelmann */
6942d0b044SSven Eckelmann #define BATADV_ROAMING_MAX_TIME 20000
7042d0b044SSven Eckelmann #define BATADV_ROAMING_MAX_COUNT 5
71cc47f66eSAntonio Quartulli
7242d0b044SSven Eckelmann #define BATADV_NO_FLAGS 0
73ecbd5321SMarek Lindner
7442d0b044SSven Eckelmann #define BATADV_NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
75bc279080SAntonio Quartulli
769464d071SAntonio Quartulli #define BATADV_NO_MARK 0
779464d071SAntonio Quartulli
7889652331SSimon Wunderlich /* default interface for multi interface operation. The default interface is
7989652331SSimon Wunderlich * used for communication which originated locally (i.e. is not forwarded)
8089652331SSimon Wunderlich * or where special forwarding is not desired/necessary.
8189652331SSimon Wunderlich */
8289652331SSimon Wunderlich #define BATADV_IF_DEFAULT ((struct batadv_hard_iface *)NULL)
8389652331SSimon Wunderlich
8442d0b044SSven Eckelmann #define BATADV_NUM_WORDS BITS_TO_LONGS(BATADV_TQ_LOCAL_WINDOW_SIZE)
85c6c8fea2SSven Eckelmann
8642d0b044SSven Eckelmann #define BATADV_LOG_BUF_LEN 8192 /* has to be a power of 2 */
87c6c8fea2SSven Eckelmann
88caf65bfcSMatthias Schiffer /* number of packets to send for broadcasts on different interface types */
89caf65bfcSMatthias Schiffer #define BATADV_NUM_BCASTS_DEFAULT 1
90caf65bfcSMatthias Schiffer #define BATADV_NUM_BCASTS_WIRELESS 3
91caf65bfcSMatthias Schiffer
9233a3bb4aSAntonio Quartulli /* length of the single packet used by the TP meter */
9333a3bb4aSAntonio Quartulli #define BATADV_TP_PACKET_LEN ETH_DATA_LEN
9433a3bb4aSAntonio Quartulli
95c384ea3eSAntonio Quartulli /* msecs after which an ARP_REQUEST is sent in broadcast as fallback */
96c384ea3eSAntonio Quartulli #define ARP_REQ_DELAY 250
97785ea114SAntonio Quartulli /* numbers of originator to contact for any PUT/GET DHT operation */
98785ea114SAntonio Quartulli #define BATADV_DAT_CANDIDATES_NUM 3
99785ea114SAntonio Quartulli
100a0e28775SMartin Hundebøll /* BATADV_TQ_SIMILARITY_THRESHOLD - TQ points that a secondary metric can differ
101c43c981eSAntonio Quartulli * at most from the primary one in order to be still considered acceptable
102c43c981eSAntonio Quartulli */
103c43c981eSAntonio Quartulli #define BATADV_TQ_SIMILARITY_THRESHOLD 50
104c43c981eSAntonio Quartulli
1056e215fd8SSven Eckelmann /* should not be bigger than 512 bytes or change the size of
1069cfc7bd6SSven Eckelmann * forw_packet->direct_link_flags
1079cfc7bd6SSven Eckelmann */
10842d0b044SSven Eckelmann #define BATADV_MAX_AGGREGATION_BYTES 512
10942d0b044SSven Eckelmann #define BATADV_MAX_AGGREGATION_MS 100
110c6c8fea2SSven Eckelmann
11142d0b044SSven Eckelmann #define BATADV_BLA_PERIOD_LENGTH 10000 /* 10 seconds */
112e1544f3cSSimon Wunderlich #define BATADV_BLA_BACKBONE_TIMEOUT (BATADV_BLA_PERIOD_LENGTH * 6)
11342d0b044SSven Eckelmann #define BATADV_BLA_CLAIM_TIMEOUT (BATADV_BLA_PERIOD_LENGTH * 10)
11428709878SSimon Wunderlich #define BATADV_BLA_WAIT_PERIODS 3
115cd9c7bfbSSimon Wunderlich #define BATADV_BLA_LOOPDETECT_PERIODS 6
116cd9c7bfbSSimon Wunderlich #define BATADV_BLA_LOOPDETECT_TIMEOUT 3000 /* 3 seconds */
117fe2da6ffSSimon Wunderlich
11842d0b044SSven Eckelmann #define BATADV_DUPLIST_SIZE 16
11942d0b044SSven Eckelmann #define BATADV_DUPLIST_TIMEOUT 500 /* 500 ms */
1206e215fd8SSven Eckelmann /* don't reset again within 30 seconds */
12142d0b044SSven Eckelmann #define BATADV_RESET_PROTECTION_MS 30000
12242d0b044SSven Eckelmann #define BATADV_EXPECTED_SEQNO_RANGE 65536
123c6c8fea2SSven Eckelmann
124d56b1705SMartin Hundebøll #define BATADV_NC_NODE_TIMEOUT 10000 /* Milliseconds */
125d56b1705SMartin Hundebøll
12633a3bb4aSAntonio Quartulli /**
12733a3bb4aSAntonio Quartulli * BATADV_TP_MAX_NUM - maximum number of simultaneously active tp sessions
12833a3bb4aSAntonio Quartulli */
12933a3bb4aSAntonio Quartulli #define BATADV_TP_MAX_NUM 5
13033a3bb4aSAntonio Quartulli
13173844a8cSSven Eckelmann /**
13273844a8cSSven Eckelmann * enum batadv_mesh_state - State of a soft interface
13373844a8cSSven Eckelmann */
13439c75a51SSven Eckelmann enum batadv_mesh_state {
13573844a8cSSven Eckelmann /** @BATADV_MESH_INACTIVE: soft interface is not yet running */
13639c75a51SSven Eckelmann BATADV_MESH_INACTIVE,
13773844a8cSSven Eckelmann
13873844a8cSSven Eckelmann /** @BATADV_MESH_ACTIVE: interface is up and running */
13939c75a51SSven Eckelmann BATADV_MESH_ACTIVE,
14073844a8cSSven Eckelmann
14173844a8cSSven Eckelmann /** @BATADV_MESH_DEACTIVATING: interface is getting shut down */
14239c75a51SSven Eckelmann BATADV_MESH_DEACTIVATING,
143e8958dbfSSven Eckelmann };
144c6c8fea2SSven Eckelmann
14542d0b044SSven Eckelmann #define BATADV_BCAST_QUEUE_LEN 256
14642d0b044SSven Eckelmann #define BATADV_BATMAN_QUEUE_LEN 256
147c6c8fea2SSven Eckelmann
14873844a8cSSven Eckelmann /**
14973844a8cSSven Eckelmann * enum batadv_uev_action - action type of uevent
15073844a8cSSven Eckelmann */
15139c75a51SSven Eckelmann enum batadv_uev_action {
15273844a8cSSven Eckelmann /** @BATADV_UEV_ADD: gateway was selected (after none was selected) */
15339c75a51SSven Eckelmann BATADV_UEV_ADD = 0,
15473844a8cSSven Eckelmann
15573844a8cSSven Eckelmann /**
15673844a8cSSven Eckelmann * @BATADV_UEV_DEL: selected gateway was removed and none is selected
15773844a8cSSven Eckelmann * anymore
15873844a8cSSven Eckelmann */
15939c75a51SSven Eckelmann BATADV_UEV_DEL,
16073844a8cSSven Eckelmann
16173844a8cSSven Eckelmann /**
16273844a8cSSven Eckelmann * @BATADV_UEV_CHANGE: a different gateway was selected as based gateway
16373844a8cSSven Eckelmann */
16439c75a51SSven Eckelmann BATADV_UEV_CHANGE,
16573844a8cSSven Eckelmann
16673844a8cSSven Eckelmann /**
16773844a8cSSven Eckelmann * @BATADV_UEV_LOOPDETECT: loop was detected which cannot be handled by
16873844a8cSSven Eckelmann * bridge loop avoidance
16973844a8cSSven Eckelmann */
170cd9c7bfbSSimon Wunderlich BATADV_UEV_LOOPDETECT,
171c6bda689SAntonio Quartulli };
172c6bda689SAntonio Quartulli
17373844a8cSSven Eckelmann /**
17473844a8cSSven Eckelmann * enum batadv_uev_type - Type of uevent
17573844a8cSSven Eckelmann */
17639c75a51SSven Eckelmann enum batadv_uev_type {
17773844a8cSSven Eckelmann /** @BATADV_UEV_GW: selected gateway was modified */
17839c75a51SSven Eckelmann BATADV_UEV_GW = 0,
17973844a8cSSven Eckelmann
18073844a8cSSven Eckelmann /** @BATADV_UEV_BLA: bridge loop avoidance event */
181cd9c7bfbSSimon Wunderlich BATADV_UEV_BLA,
182c6bda689SAntonio Quartulli };
183c6bda689SAntonio Quartulli
18442d0b044SSven Eckelmann #define BATADV_GW_THRESHOLD 50
18543676ab5SAntonio Quartulli
186610bfc6bSMartin Hundebøll /* Number of fragment chains for each orig_node */
187610bfc6bSMartin Hundebøll #define BATADV_FRAG_BUFFER_COUNT 8
188610bfc6bSMartin Hundebøll /* Maximum number of fragments for one packet */
189610bfc6bSMartin Hundebøll #define BATADV_FRAG_MAX_FRAGMENTS 16
190610bfc6bSMartin Hundebøll /* Maxumim size of each fragment */
1918ea026b1SMatthias Schiffer #define BATADV_FRAG_MAX_FRAG_SIZE 1280
192610bfc6bSMartin Hundebøll /* Time to keep fragments while waiting for rest of the fragments */
193610bfc6bSMartin Hundebøll #define BATADV_FRAG_TIMEOUT 10000
194610bfc6bSMartin Hundebøll
195785ea114SAntonio Quartulli #define BATADV_DAT_CANDIDATE_NOT_FOUND 0
196785ea114SAntonio Quartulli #define BATADV_DAT_CANDIDATE_ORIG 1
197785ea114SAntonio Quartulli
19821a1236bSSven Eckelmann /* Debug Messages */
199c6c8fea2SSven Eckelmann #ifdef pr_fmt
200c6c8fea2SSven Eckelmann #undef pr_fmt
201c6c8fea2SSven Eckelmann #endif
2026e215fd8SSven Eckelmann /* Append 'batman-adv: ' before kernel messages */
2036e215fd8SSven Eckelmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
204c6c8fea2SSven Eckelmann
20521a1236bSSven Eckelmann /* Kernel headers */
206c6c8fea2SSven Eckelmann
20747d4522dSSven Eckelmann #include <linux/atomic.h>
2081e2c2a4fSSven Eckelmann #include <linux/compiler.h>
2091e2c2a4fSSven Eckelmann #include <linux/etherdevice.h>
210fcafa5e7SSven Eckelmann #include <linux/if_vlan.h>
211fcafa5e7SSven Eckelmann #include <linux/jiffies.h>
21268a600deSSven Eckelmann #include <linux/netdevice.h>
213fcafa5e7SSven Eckelmann #include <linux/percpu.h>
21468a600deSSven Eckelmann #include <linux/skbuff.h>
2151e2c2a4fSSven Eckelmann #include <linux/types.h>
216fec149f5SSven Eckelmann #include <uapi/linux/batadv_packet.h>
21716052789SAntonio Quartulli
218c6c8fea2SSven Eckelmann #include "types.h"
21968a600deSSven Eckelmann #include "main.h"
2201e2c2a4fSSven Eckelmann
221f7a2bd65SSven Eckelmann /**
2227e9a8c2cSSven Eckelmann * batadv_print_vid() - return printable version of vid information
223f7a2bd65SSven Eckelmann * @vid: the VLAN identifier
224f7a2bd65SSven Eckelmann *
225f7a2bd65SSven Eckelmann * Return: -1 when no VLAN is used, VLAN id otherwise
226f7a2bd65SSven Eckelmann */
batadv_print_vid(unsigned short vid)227f7a2bd65SSven Eckelmann static inline int batadv_print_vid(unsigned short vid)
228f7a2bd65SSven Eckelmann {
229f7a2bd65SSven Eckelmann if (vid & BATADV_VLAN_HAS_TAG)
230f7a2bd65SSven Eckelmann return (int)(vid & VLAN_VID_MASK);
231f7a2bd65SSven Eckelmann else
232f7a2bd65SSven Eckelmann return -1;
233f7a2bd65SSven Eckelmann }
234eb2deb6bSAntonio Quartulli
2353193e8fdSSven Eckelmann extern struct list_head batadv_hardif_list;
236fb69be69SSven Eckelmann extern unsigned int batadv_hardif_generation;
237c6c8fea2SSven Eckelmann
2383193e8fdSSven Eckelmann extern unsigned char batadv_broadcast_addr[];
2393193e8fdSSven Eckelmann extern struct workqueue_struct *batadv_event_workqueue;
240c6c8fea2SSven Eckelmann
2413193e8fdSSven Eckelmann int batadv_mesh_init(struct net_device *soft_iface);
2423193e8fdSSven Eckelmann void batadv_mesh_free(struct net_device *soft_iface);
2436b5e971aSSven Eckelmann bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr);
244411d6ed9SMarek Lindner int batadv_max_header_len(void);
245c54f38c9SSimon Wunderlich void batadv_skb_set_priority(struct sk_buff *skb, int offset);
2463193e8fdSSven Eckelmann int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
2473193e8fdSSven Eckelmann struct packet_type *ptype,
2483193e8fdSSven Eckelmann struct net_device *orig_dev);
24956303d34SSven Eckelmann int
2506b5e971aSSven Eckelmann batadv_recv_handler_register(u8 packet_type,
251ffa995e0SMarek Lindner int (*recv_handler)(struct sk_buff *,
25256303d34SSven Eckelmann struct batadv_hard_iface *));
2536b5e971aSSven Eckelmann void batadv_recv_handler_unregister(u8 packet_type);
25495a066d8SSven Eckelmann __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr);
255c6c8fea2SSven Eckelmann
2560aca86cdSAntonio Quartulli /**
2577e9a8c2cSSven Eckelmann * batadv_compare_eth() - Compare two not u16 aligned Ethernet addresses
2587afcbbefSSven Eckelmann * @data1: Pointer to a six-byte array containing the Ethernet address
2597afcbbefSSven Eckelmann * @data2: Pointer other six-byte array containing the Ethernet address
26039901e71SMarek Lindner *
261c3923b7aSJoe Perches * note: can't use ether_addr_equal() as it requires aligned memory
26262fe710fSSven Eckelmann *
2634b426b10SSven Eckelmann * Return: true if they are the same ethernet addr
26439901e71SMarek Lindner */
batadv_compare_eth(const void * data1,const void * data2)265f2d5cf2aSMarkus Pargmann static inline bool batadv_compare_eth(const void *data1, const void *data2)
26639901e71SMarek Lindner {
267d9cd4fe5SJoe Perches return ether_addr_equal_unaligned(data1, data2);
26839901e71SMarek Lindner }
26939901e71SMarek Lindner
2702c53040fSBen Hutchings /**
2717e9a8c2cSSven Eckelmann * batadv_has_timed_out() - compares current time (jiffies) and timestamp +
2726d030de8SAntonio Quartulli * timeout
273a04ccd59SMartin Hundebøll * @timestamp: base value to compare with (in jiffies)
274a04ccd59SMartin Hundebøll * @timeout: added to base value before comparing (in milliseconds)
275a04ccd59SMartin Hundebøll *
27662fe710fSSven Eckelmann * Return: true if current time is after timestamp + timeout
277a04ccd59SMartin Hundebøll */
batadv_has_timed_out(unsigned long timestamp,unsigned int timeout)2781eda58bfSSven Eckelmann static inline bool batadv_has_timed_out(unsigned long timestamp,
2791eda58bfSSven Eckelmann unsigned int timeout)
280a04ccd59SMartin Hundebøll {
281a04ccd59SMartin Hundebøll return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout));
282a04ccd59SMartin Hundebøll }
283747e4221SSven Eckelmann
284e57acf8eSSven Eckelmann /**
285e57acf8eSSven Eckelmann * batadv_atomic_dec_not_zero() - Decrease unless the number is 0
286e57acf8eSSven Eckelmann * @v: pointer of type atomic_t
287e57acf8eSSven Eckelmann *
288e57acf8eSSven Eckelmann * Return: non-zero if v was not 0, and zero otherwise.
289e57acf8eSSven Eckelmann */
2903e34819eSSven Eckelmann #define batadv_atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
2915f657ec0SAntonio Quartulli
292e57acf8eSSven Eckelmann /**
293e57acf8eSSven Eckelmann * batadv_smallest_signed_int() - Returns the smallest signed integer in two's
294e57acf8eSSven Eckelmann * complement with the sizeof x
295e57acf8eSSven Eckelmann * @x: type of integer
296e57acf8eSSven Eckelmann *
297e57acf8eSSven Eckelmann * Return: smallest signed integer of type
298e57acf8eSSven Eckelmann */
2993e34819eSSven Eckelmann #define batadv_smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
300f5d33d37SAntonio Quartulli
301e57acf8eSSven Eckelmann /**
302e57acf8eSSven Eckelmann * batadv_seq_before() - Checks if a sequence number x is a predecessor of y
303e57acf8eSSven Eckelmann * @x: potential predecessor of @y
304e57acf8eSSven Eckelmann * @y: value to compare @x against
305e57acf8eSSven Eckelmann *
306e57acf8eSSven Eckelmann * It handles overflows/underflows and can correctly check for a predecessor
307bccb48c8SSven Eckelmann * unless the variable sequence number has grown by more than
308e57acf8eSSven Eckelmann * 2**(bitwidth(x)-1)-1.
309e57acf8eSSven Eckelmann *
3106b5e971aSSven Eckelmann * This means that for a u8 with the maximum value 255, it would think:
311e57acf8eSSven Eckelmann *
312e57acf8eSSven Eckelmann * * when adding nothing - it is neither a predecessor nor a successor
313e57acf8eSSven Eckelmann * * before adding more than 127 to the starting value - it is a predecessor,
314e57acf8eSSven Eckelmann * * when adding 128 - it is neither a predecessor nor a successor,
315e57acf8eSSven Eckelmann * * after adding more than 127 to the starting value - it is a successor
316e57acf8eSSven Eckelmann *
317e57acf8eSSven Eckelmann * Return: true when x is a predecessor of y, false otherwise
3189cfc7bd6SSven Eckelmann */
31984d0a394SSven Eckelmann #define batadv_seq_before(x, y) ({ \
32084d0a394SSven Eckelmann typeof(x)_d1 = (x); \
3210bb85751SSven Eckelmann typeof(y)_d2 = (y); \
3220bb85751SSven Eckelmann typeof(x)_dummy = (_d1 - _d2); \
3230bb85751SSven Eckelmann (void)(&_d1 == &_d2); \
32484d0a394SSven Eckelmann _dummy > batadv_smallest_signed_int(_dummy); \
32584d0a394SSven Eckelmann })
326e57acf8eSSven Eckelmann
327e57acf8eSSven Eckelmann /**
328e57acf8eSSven Eckelmann * batadv_seq_after() - Checks if a sequence number x is a successor of y
329bccb48c8SSven Eckelmann * @x: potential successor of @y
330e57acf8eSSven Eckelmann * @y: value to compare @x against
331e57acf8eSSven Eckelmann *
332e57acf8eSSven Eckelmann * It handles overflows/underflows and can correctly check for a successor
333bccb48c8SSven Eckelmann * unless the variable sequence number has grown by more than
334e57acf8eSSven Eckelmann * 2**(bitwidth(x)-1)-1.
335e57acf8eSSven Eckelmann *
336e57acf8eSSven Eckelmann * This means that for a u8 with the maximum value 255, it would think:
337e57acf8eSSven Eckelmann *
338e57acf8eSSven Eckelmann * * when adding nothing - it is neither a predecessor nor a successor
339e57acf8eSSven Eckelmann * * before adding more than 127 to the starting value - it is a predecessor,
340e57acf8eSSven Eckelmann * * when adding 128 - it is neither a predecessor nor a successor,
341e57acf8eSSven Eckelmann * * after adding more than 127 to the starting value - it is a successor
342e57acf8eSSven Eckelmann *
343e57acf8eSSven Eckelmann * Return: true when x is a successor of y, false otherwise
344e57acf8eSSven Eckelmann */
3453e34819eSSven Eckelmann #define batadv_seq_after(x, y) batadv_seq_before(y, x)
346f5d33d37SAntonio Quartulli
347e57acf8eSSven Eckelmann /**
348e57acf8eSSven Eckelmann * batadv_add_counter() - Add to per cpu statistics counter of soft interface
349e57acf8eSSven Eckelmann * @bat_priv: the bat priv with all the soft interface information
350e57acf8eSSven Eckelmann * @idx: counter index which should be modified
351e57acf8eSSven Eckelmann * @count: value to increase counter by
352e57acf8eSSven Eckelmann *
353e57acf8eSSven Eckelmann * Stop preemption on local cpu while incrementing the counter
354e57acf8eSSven Eckelmann */
batadv_add_counter(struct batadv_priv * bat_priv,size_t idx,size_t count)35556303d34SSven Eckelmann static inline void batadv_add_counter(struct batadv_priv *bat_priv, size_t idx,
356f8214865SMartin Hundebøll size_t count)
357f8214865SMartin Hundebøll {
35856917443SShan Wei this_cpu_add(bat_priv->bat_counters[idx], count);
359f8214865SMartin Hundebøll }
360f8214865SMartin Hundebøll
361e57acf8eSSven Eckelmann /**
362e57acf8eSSven Eckelmann * batadv_inc_counter() - Increase per cpu statistics counter of soft interface
363e57acf8eSSven Eckelmann * @b: the bat priv with all the soft interface information
364e57acf8eSSven Eckelmann * @i: counter index which should be modified
365e57acf8eSSven Eckelmann */
366f8214865SMartin Hundebøll #define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1)
367f8214865SMartin Hundebøll
368e57acf8eSSven Eckelmann /**
369e57acf8eSSven Eckelmann * BATADV_SKB_CB() - Get batadv_skb_cb from skb control buffer
370e57acf8eSSven Eckelmann * @__skb: skb holding the control buffer
371e57acf8eSSven Eckelmann *
372e57acf8eSSven Eckelmann * The members of the control buffer are defined in struct batadv_skb_cb in
373e57acf8eSSven Eckelmann * types.h. The macro is inspired by the similar macro TCP_SKB_CB() in tcp.h.
374e57acf8eSSven Eckelmann *
375e57acf8eSSven Eckelmann * Return: pointer to the batadv_skb_cb of the skb
3763c12de9aSMartin Hundebøll */
3773c12de9aSMartin Hundebøll #define BATADV_SKB_CB(__skb) ((struct batadv_skb_cb *)&((__skb)->cb[0]))
3783c12de9aSMartin Hundebøll
379c018ad3dSAntonio Quartulli unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len);
380eceb22aeSAntonio Quartulli bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid);
3810fa4c30dSSven Eckelmann int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
3820fa4c30dSSven Eckelmann enum batadv_uev_action action, const char *data);
383ef261577SMarek Lindner
384c6c8fea2SSven Eckelmann #endif /* _NET_BATMAN_ADV_MAIN_H_ */
385