xref: /linux/net/batman-adv/main.h (revision c833484e5f3872a38fe232c663586069d5ad9645)
10046b040SSven Eckelmann /* Copyright (C) 2007-2016  B.A.T.M.A.N. contributors:
2c6c8fea2SSven Eckelmann  *
3c6c8fea2SSven Eckelmann  * Marek Lindner, Simon Wunderlich
4c6c8fea2SSven Eckelmann  *
5c6c8fea2SSven Eckelmann  * This program is free software; you can redistribute it and/or
6c6c8fea2SSven Eckelmann  * modify it under the terms of version 2 of the GNU General Public
7c6c8fea2SSven Eckelmann  * License as published by the Free Software Foundation.
8c6c8fea2SSven Eckelmann  *
9c6c8fea2SSven Eckelmann  * This program is distributed in the hope that it will be useful, but
10c6c8fea2SSven Eckelmann  * WITHOUT ANY WARRANTY; without even the implied warranty of
11c6c8fea2SSven Eckelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12c6c8fea2SSven Eckelmann  * General Public License for more details.
13c6c8fea2SSven Eckelmann  *
14c6c8fea2SSven Eckelmann  * You should have received a copy of the GNU General Public License
15ebf38fb7SAntonio Quartulli  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16c6c8fea2SSven Eckelmann  */
17c6c8fea2SSven Eckelmann 
18c6c8fea2SSven Eckelmann #ifndef _NET_BATMAN_ADV_MAIN_H_
19c6c8fea2SSven Eckelmann #define _NET_BATMAN_ADV_MAIN_H_
20c6c8fea2SSven Eckelmann 
21bc58eeefSMarek Lindner #define BATADV_DRIVER_AUTHOR "Marek Lindner <mareklindner@neomailbox.ch>, " \
22c679ff8fSSimon Wunderlich 			     "Simon Wunderlich <sw@simonwunderlich.de>"
2342d0b044SSven Eckelmann #define BATADV_DRIVER_DESC   "B.A.T.M.A.N. advanced"
2442d0b044SSven Eckelmann #define BATADV_DRIVER_DEVICE "batman-adv"
25c6c8fea2SSven Eckelmann 
2642d0b044SSven Eckelmann #ifndef BATADV_SOURCE_VERSION
274a4d045eSSimon Wunderlich #define BATADV_SOURCE_VERSION "2016.0"
2844c4349aSSven Eckelmann #endif
29c6c8fea2SSven Eckelmann 
30c6c8fea2SSven Eckelmann /* B.A.T.M.A.N. parameters */
31c6c8fea2SSven Eckelmann 
3242d0b044SSven Eckelmann #define BATADV_TQ_MAX_VALUE 255
330da00359SAntonio Quartulli #define BATADV_THROUGHPUT_MAX_VALUE 0xFFFFFFFF
3442d0b044SSven Eckelmann #define BATADV_JITTER 20
35c6c8fea2SSven Eckelmann 
366e215fd8SSven Eckelmann /* Time To Live of broadcast messages */
3742d0b044SSven Eckelmann #define BATADV_TTL 50
386e215fd8SSven Eckelmann 
3981f02683SSimon Wunderlich /* maximum sequence number age of broadcast messages */
4081f02683SSimon Wunderlich #define BATADV_BCAST_MAX_AGE 64
4181f02683SSimon Wunderlich 
426e215fd8SSven Eckelmann /* purge originators after time in seconds if no valid packet comes in
4342d0b044SSven Eckelmann  * -> TODO: check influence on BATADV_TQ_LOCAL_WINDOW_SIZE
449cfc7bd6SSven Eckelmann  */
4542d0b044SSven Eckelmann #define BATADV_PURGE_TIMEOUT 200000 /* 200 seconds */
467cf4d520SAntonio Quartulli #define BATADV_TT_LOCAL_TIMEOUT 600000 /* in milliseconds */
47a51fb9b2SAntonio Quartulli #define BATADV_TT_CLIENT_ROAM_TIMEOUT 600000 /* in milliseconds */
4830cfd02bSAntonio Quartulli #define BATADV_TT_CLIENT_TEMP_TIMEOUT 600000 /* in milliseconds */
4972414442SAntonio Quartulli #define BATADV_TT_WORK_PERIOD 5000 /* 5 seconds */
5072414442SAntonio Quartulli #define BATADV_ORIG_WORK_PERIOD 1000 /* 1 second */
512f1dfbe1SAntonio Quartulli #define BATADV_DAT_ENTRY_TIMEOUT (5 * 60000) /* 5 mins in milliseconds */
52015758d0SAntonio Quartulli /* sliding packet range of received originator messages in sequence numbers
539cfc7bd6SSven Eckelmann  * (should be a multiple of our word size)
549cfc7bd6SSven Eckelmann  */
5542d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_WINDOW_SIZE 64
56a51fb9b2SAntonio Quartulli /* milliseconds we have to keep pending tt_req */
5742d0b044SSven Eckelmann #define BATADV_TT_REQUEST_TIMEOUT 3000
58a73105b8SAntonio Quartulli 
5942d0b044SSven Eckelmann #define BATADV_TQ_GLOBAL_WINDOW_SIZE 5
6042d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
6142d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
6242d0b044SSven Eckelmann #define BATADV_TQ_TOTAL_BIDRECT_LIMIT 1
63c6c8fea2SSven Eckelmann 
64162bd64cSLinus Luessing /* B.A.T.M.A.N. V */
65*c833484eSAntonio Quartulli #define BATADV_THROUGHPUT_DEFAULT_VALUE 10 /* 1 Mbps */
66162bd64cSLinus Luessing #define BATADV_ELP_MAX_AGE 64
679323158eSAntonio Quartulli #define BATADV_OGM_MAX_ORIGDIFF 5
689323158eSAntonio Quartulli #define BATADV_OGM_MAX_AGE 64
69162bd64cSLinus Luessing 
7042d0b044SSven Eckelmann /* number of OGMs sent with the last tt diff */
7142d0b044SSven Eckelmann #define BATADV_TT_OGM_APPEND_MAX 3
72a73105b8SAntonio Quartulli 
739cfc7bd6SSven Eckelmann /* Time in which a client can roam at most ROAMING_MAX_COUNT times in
74a51fb9b2SAntonio Quartulli  * milliseconds
759cfc7bd6SSven Eckelmann  */
7642d0b044SSven Eckelmann #define BATADV_ROAMING_MAX_TIME 20000
7742d0b044SSven Eckelmann #define BATADV_ROAMING_MAX_COUNT 5
78cc47f66eSAntonio Quartulli 
7942d0b044SSven Eckelmann #define BATADV_NO_FLAGS 0
80ecbd5321SMarek Lindner 
8142d0b044SSven Eckelmann #define BATADV_NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
82bc279080SAntonio Quartulli 
839464d071SAntonio Quartulli #define BATADV_NO_MARK 0
849464d071SAntonio Quartulli 
8589652331SSimon Wunderlich /* default interface for multi interface operation. The default interface is
8689652331SSimon Wunderlich  * used for communication which originated locally (i.e. is not forwarded)
8789652331SSimon Wunderlich  * or where special forwarding is not desired/necessary.
8889652331SSimon Wunderlich  */
8989652331SSimon Wunderlich #define BATADV_IF_DEFAULT	((struct batadv_hard_iface *)NULL)
9089652331SSimon Wunderlich 
9142d0b044SSven Eckelmann #define BATADV_NUM_WORDS BITS_TO_LONGS(BATADV_TQ_LOCAL_WINDOW_SIZE)
92c6c8fea2SSven Eckelmann 
9342d0b044SSven Eckelmann #define BATADV_LOG_BUF_LEN 8192	  /* has to be a power of 2 */
94c6c8fea2SSven Eckelmann 
95caf65bfcSMatthias Schiffer /* number of packets to send for broadcasts on different interface types */
96caf65bfcSMatthias Schiffer #define BATADV_NUM_BCASTS_DEFAULT 1
97caf65bfcSMatthias Schiffer #define BATADV_NUM_BCASTS_WIRELESS 3
98caf65bfcSMatthias Schiffer #define BATADV_NUM_BCASTS_MAX 3
99caf65bfcSMatthias Schiffer 
100c384ea3eSAntonio Quartulli /* msecs after which an ARP_REQUEST is sent in broadcast as fallback */
101c384ea3eSAntonio Quartulli #define ARP_REQ_DELAY 250
102785ea114SAntonio Quartulli /* numbers of originator to contact for any PUT/GET DHT operation */
103785ea114SAntonio Quartulli #define BATADV_DAT_CANDIDATES_NUM 3
104785ea114SAntonio Quartulli 
105a0e28775SMartin Hundebøll /* BATADV_TQ_SIMILARITY_THRESHOLD - TQ points that a secondary metric can differ
106c43c981eSAntonio Quartulli  * at most from the primary one in order to be still considered acceptable
107c43c981eSAntonio Quartulli  */
108c43c981eSAntonio Quartulli #define BATADV_TQ_SIMILARITY_THRESHOLD 50
109c43c981eSAntonio Quartulli 
1106e215fd8SSven Eckelmann /* should not be bigger than 512 bytes or change the size of
1119cfc7bd6SSven Eckelmann  * forw_packet->direct_link_flags
1129cfc7bd6SSven Eckelmann  */
11342d0b044SSven Eckelmann #define BATADV_MAX_AGGREGATION_BYTES 512
11442d0b044SSven Eckelmann #define BATADV_MAX_AGGREGATION_MS 100
115c6c8fea2SSven Eckelmann 
11642d0b044SSven Eckelmann #define BATADV_BLA_PERIOD_LENGTH	10000	/* 10 seconds */
117e1544f3cSSimon Wunderlich #define BATADV_BLA_BACKBONE_TIMEOUT	(BATADV_BLA_PERIOD_LENGTH * 6)
11842d0b044SSven Eckelmann #define BATADV_BLA_CLAIM_TIMEOUT	(BATADV_BLA_PERIOD_LENGTH * 10)
11928709878SSimon Wunderlich #define BATADV_BLA_WAIT_PERIODS		3
120fe2da6ffSSimon Wunderlich 
12142d0b044SSven Eckelmann #define BATADV_DUPLIST_SIZE		16
12242d0b044SSven Eckelmann #define BATADV_DUPLIST_TIMEOUT		500	/* 500 ms */
1236e215fd8SSven Eckelmann /* don't reset again within 30 seconds */
12442d0b044SSven Eckelmann #define BATADV_RESET_PROTECTION_MS 30000
12542d0b044SSven Eckelmann #define BATADV_EXPECTED_SEQNO_RANGE	65536
126c6c8fea2SSven Eckelmann 
127d56b1705SMartin Hundebøll #define BATADV_NC_NODE_TIMEOUT 10000 /* Milliseconds */
128d56b1705SMartin Hundebøll 
12939c75a51SSven Eckelmann enum batadv_mesh_state {
13039c75a51SSven Eckelmann 	BATADV_MESH_INACTIVE,
13139c75a51SSven Eckelmann 	BATADV_MESH_ACTIVE,
13239c75a51SSven Eckelmann 	BATADV_MESH_DEACTIVATING,
133e8958dbfSSven Eckelmann };
134c6c8fea2SSven Eckelmann 
13542d0b044SSven Eckelmann #define BATADV_BCAST_QUEUE_LEN		256
13642d0b044SSven Eckelmann #define BATADV_BATMAN_QUEUE_LEN	256
137c6c8fea2SSven Eckelmann 
13839c75a51SSven Eckelmann enum batadv_uev_action {
13939c75a51SSven Eckelmann 	BATADV_UEV_ADD = 0,
14039c75a51SSven Eckelmann 	BATADV_UEV_DEL,
14139c75a51SSven Eckelmann 	BATADV_UEV_CHANGE,
142c6bda689SAntonio Quartulli };
143c6bda689SAntonio Quartulli 
14439c75a51SSven Eckelmann enum batadv_uev_type {
14539c75a51SSven Eckelmann 	BATADV_UEV_GW = 0,
146c6bda689SAntonio Quartulli };
147c6bda689SAntonio Quartulli 
14842d0b044SSven Eckelmann #define BATADV_GW_THRESHOLD	50
14943676ab5SAntonio Quartulli 
150610bfc6bSMartin Hundebøll /* Number of fragment chains for each orig_node */
151610bfc6bSMartin Hundebøll #define BATADV_FRAG_BUFFER_COUNT 8
152610bfc6bSMartin Hundebøll /* Maximum number of fragments for one packet */
153610bfc6bSMartin Hundebøll #define BATADV_FRAG_MAX_FRAGMENTS 16
154610bfc6bSMartin Hundebøll /* Maxumim size of each fragment */
155610bfc6bSMartin Hundebøll #define BATADV_FRAG_MAX_FRAG_SIZE 1400
156610bfc6bSMartin Hundebøll /* Time to keep fragments while waiting for rest of the fragments */
157610bfc6bSMartin Hundebøll #define BATADV_FRAG_TIMEOUT 10000
158610bfc6bSMartin Hundebøll 
159785ea114SAntonio Quartulli #define BATADV_DAT_CANDIDATE_NOT_FOUND	0
160785ea114SAntonio Quartulli #define BATADV_DAT_CANDIDATE_ORIG	1
161785ea114SAntonio Quartulli 
16221a1236bSSven Eckelmann /* Debug Messages */
163c6c8fea2SSven Eckelmann #ifdef pr_fmt
164c6c8fea2SSven Eckelmann #undef pr_fmt
165c6c8fea2SSven Eckelmann #endif
1666e215fd8SSven Eckelmann /* Append 'batman-adv: ' before kernel messages */
1676e215fd8SSven Eckelmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
168c6c8fea2SSven Eckelmann 
16921a1236bSSven Eckelmann /* Kernel headers */
170c6c8fea2SSven Eckelmann 
1711e2c2a4fSSven Eckelmann #include <linux/atomic.h>
1721e2c2a4fSSven Eckelmann #include <linux/bitops.h> /* for packet.h */
1731e2c2a4fSSven Eckelmann #include <linux/compiler.h>
1741e2c2a4fSSven Eckelmann #include <linux/cpumask.h>
1751e2c2a4fSSven Eckelmann #include <linux/etherdevice.h>
1761e2c2a4fSSven Eckelmann #include <linux/if_ether.h> /* for packet.h */
1771e2c2a4fSSven Eckelmann #include <linux/netdevice.h>
1781e2c2a4fSSven Eckelmann #include <linux/printk.h>
1791e2c2a4fSSven Eckelmann #include <linux/types.h>
180f8214865SMartin Hundebøll #include <linux/percpu.h>
181c6c8fea2SSven Eckelmann #include <linux/jiffies.h>
18216052789SAntonio Quartulli #include <linux/if_vlan.h>
18316052789SAntonio Quartulli 
184c6c8fea2SSven Eckelmann #include "types.h"
185c6c8fea2SSven Eckelmann 
1861e2c2a4fSSven Eckelmann struct batadv_ogm_packet;
1871e2c2a4fSSven Eckelmann struct seq_file;
1881e2c2a4fSSven Eckelmann struct sk_buff;
1891e2c2a4fSSven Eckelmann 
190a2f2b6cdSSven Eckelmann #define BATADV_PRINT_VID(vid) ((vid & BATADV_VLAN_HAS_TAG) ? \
191eb2deb6bSAntonio Quartulli 			       (int)(vid & VLAN_VID_MASK) : -1)
192eb2deb6bSAntonio Quartulli 
1933193e8fdSSven Eckelmann extern char batadv_routing_algo[];
1943193e8fdSSven Eckelmann extern struct list_head batadv_hardif_list;
195c6c8fea2SSven Eckelmann 
1963193e8fdSSven Eckelmann extern unsigned char batadv_broadcast_addr[];
1973193e8fdSSven Eckelmann extern struct workqueue_struct *batadv_event_workqueue;
198c6c8fea2SSven Eckelmann 
1993193e8fdSSven Eckelmann int batadv_mesh_init(struct net_device *soft_iface);
2003193e8fdSSven Eckelmann void batadv_mesh_free(struct net_device *soft_iface);
2016b5e971aSSven Eckelmann bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr);
20230da63a6SMarek Lindner struct batadv_hard_iface *
20330da63a6SMarek Lindner batadv_seq_print_text_primary_if_get(struct seq_file *seq);
204411d6ed9SMarek Lindner int batadv_max_header_len(void);
205c54f38c9SSimon Wunderlich void batadv_skb_set_priority(struct sk_buff *skb, int offset);
2063193e8fdSSven Eckelmann int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
2073193e8fdSSven Eckelmann 			   struct packet_type *ptype,
2083193e8fdSSven Eckelmann 			   struct net_device *orig_dev);
20956303d34SSven Eckelmann int
2106b5e971aSSven Eckelmann batadv_recv_handler_register(u8 packet_type,
211ffa995e0SMarek Lindner 			     int (*recv_handler)(struct sk_buff *,
21256303d34SSven Eckelmann 						 struct batadv_hard_iface *));
2136b5e971aSSven Eckelmann void batadv_recv_handler_unregister(u8 packet_type);
21456303d34SSven Eckelmann int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops);
21556303d34SSven Eckelmann int batadv_algo_select(struct batadv_priv *bat_priv, char *name);
2163193e8fdSSven Eckelmann int batadv_algo_seq_print_text(struct seq_file *seq, void *offset);
21795a066d8SSven Eckelmann __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr);
218c6c8fea2SSven Eckelmann 
2190aca86cdSAntonio Quartulli /**
2200aca86cdSAntonio Quartulli  * enum batadv_dbg_level - available log levels
2210aca86cdSAntonio Quartulli  * @BATADV_DBG_BATMAN: OGM and TQ computations related messages
2220aca86cdSAntonio Quartulli  * @BATADV_DBG_ROUTES: route added / changed / deleted
2230aca86cdSAntonio Quartulli  * @BATADV_DBG_TT: translation table messages
2240aca86cdSAntonio Quartulli  * @BATADV_DBG_BLA: bridge loop avoidance messages
2250e861a3cSAntonio Quartulli  * @BATADV_DBG_DAT: ARP snooping and DAT related messages
226d353d8d4SMartin Hundebøll  * @BATADV_DBG_NC: network coding related messages
2270aca86cdSAntonio Quartulli  * @BATADV_DBG_ALL: the union of all the above log levels
2280aca86cdSAntonio Quartulli  */
2298de47de5SSven Eckelmann enum batadv_dbg_level {
2308de47de5SSven Eckelmann 	BATADV_DBG_BATMAN = BIT(0),
2310aca86cdSAntonio Quartulli 	BATADV_DBG_ROUTES = BIT(1),
2320aca86cdSAntonio Quartulli 	BATADV_DBG_TT	  = BIT(2),
2330aca86cdSAntonio Quartulli 	BATADV_DBG_BLA    = BIT(3),
2340e861a3cSAntonio Quartulli 	BATADV_DBG_DAT    = BIT(4),
235d353d8d4SMartin Hundebøll 	BATADV_DBG_NC	  = BIT(5),
236d353d8d4SMartin Hundebøll 	BATADV_DBG_ALL    = 63,
2378de47de5SSven Eckelmann };
2388de47de5SSven Eckelmann 
239c6c8fea2SSven Eckelmann #ifdef CONFIG_BATMAN_ADV_DEBUG
24056303d34SSven Eckelmann int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
24140a072d7SSven Eckelmann __printf(2, 3);
242c6c8fea2SSven Eckelmann 
24323c4ec10SAndré Gaul /* possibly ratelimited debug output */
24423c4ec10SAndré Gaul #define _batadv_dbg(type, bat_priv, ratelimited, fmt, arg...)	\
245c6c8fea2SSven Eckelmann 	do {							\
24623c4ec10SAndré Gaul 		if (atomic_read(&bat_priv->log_level) & type && \
24723c4ec10SAndré Gaul 		    (!ratelimited || net_ratelimit()))		\
24840a072d7SSven Eckelmann 			batadv_debug_log(bat_priv, fmt, ## arg);\
249c6c8fea2SSven Eckelmann 	}							\
250c6c8fea2SSven Eckelmann 	while (0)
251c6c8fea2SSven Eckelmann #else /* !CONFIG_BATMAN_ADV_DEBUG */
25223c4ec10SAndré Gaul __printf(4, 5)
25323c4ec10SAndré Gaul static inline void _batadv_dbg(int type __always_unused,
25456303d34SSven Eckelmann 			       struct batadv_priv *bat_priv __always_unused,
25523c4ec10SAndré Gaul 			       int ratelimited __always_unused,
256747e4221SSven Eckelmann 			       const char *fmt __always_unused, ...)
257c6c8fea2SSven Eckelmann {
258c6c8fea2SSven Eckelmann }
259c6c8fea2SSven Eckelmann #endif
260c6c8fea2SSven Eckelmann 
26123c4ec10SAndré Gaul #define batadv_dbg(type, bat_priv, arg...) \
26223c4ec10SAndré Gaul 	_batadv_dbg(type, bat_priv, 0, ## arg)
26323c4ec10SAndré Gaul #define batadv_dbg_ratelimited(type, bat_priv, arg...) \
26423c4ec10SAndré Gaul 	_batadv_dbg(type, bat_priv, 1, ## arg)
26523c4ec10SAndré Gaul 
2663e34819eSSven Eckelmann #define batadv_info(net_dev, fmt, arg...)				\
267c6c8fea2SSven Eckelmann 	do {								\
268c6c8fea2SSven Eckelmann 		struct net_device *_netdev = (net_dev);                 \
26956303d34SSven Eckelmann 		struct batadv_priv *_batpriv = netdev_priv(_netdev);    \
27039c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg);	\
271c6c8fea2SSven Eckelmann 		pr_info("%s: " fmt, _netdev->name, ## arg);		\
272c6c8fea2SSven Eckelmann 	} while (0)
2733e34819eSSven Eckelmann #define batadv_err(net_dev, fmt, arg...)				\
274c6c8fea2SSven Eckelmann 	do {								\
275c6c8fea2SSven Eckelmann 		struct net_device *_netdev = (net_dev);                 \
27656303d34SSven Eckelmann 		struct batadv_priv *_batpriv = netdev_priv(_netdev);    \
27739c75a51SSven Eckelmann 		batadv_dbg(BATADV_DBG_ALL, _batpriv, fmt, ## arg);	\
278c6c8fea2SSven Eckelmann 		pr_err("%s: " fmt, _netdev->name, ## arg);		\
279c6c8fea2SSven Eckelmann 	} while (0)
280c6c8fea2SSven Eckelmann 
28162fe710fSSven Eckelmann /**
28262fe710fSSven Eckelmann  * batadv_compare_eth - Compare two not u16 aligned Ethernet addresses
2837afcbbefSSven Eckelmann  * @data1: Pointer to a six-byte array containing the Ethernet address
2847afcbbefSSven Eckelmann  * @data2: Pointer other six-byte array containing the Ethernet address
28539901e71SMarek Lindner  *
286c3923b7aSJoe Perches  * note: can't use ether_addr_equal() as it requires aligned memory
28762fe710fSSven Eckelmann  *
28862fe710fSSven Eckelmann  * Return: 1 if they are the same ethernet addr
28939901e71SMarek Lindner  */
290f2d5cf2aSMarkus Pargmann static inline bool batadv_compare_eth(const void *data1, const void *data2)
29139901e71SMarek Lindner {
292d9cd4fe5SJoe Perches 	return ether_addr_equal_unaligned(data1, data2);
29339901e71SMarek Lindner }
29439901e71SMarek Lindner 
2952c53040fSBen Hutchings /**
2962c53040fSBen Hutchings  * has_timed_out - compares current time (jiffies) and timestamp + timeout
297a04ccd59SMartin Hundebøll  * @timestamp:		base value to compare with (in jiffies)
298a04ccd59SMartin Hundebøll  * @timeout:		added to base value before comparing (in milliseconds)
299a04ccd59SMartin Hundebøll  *
30062fe710fSSven Eckelmann  * Return: true if current time is after timestamp + timeout
301a04ccd59SMartin Hundebøll  */
3021eda58bfSSven Eckelmann static inline bool batadv_has_timed_out(unsigned long timestamp,
3031eda58bfSSven Eckelmann 					unsigned int timeout)
304a04ccd59SMartin Hundebøll {
305a04ccd59SMartin Hundebøll 	return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout));
306a04ccd59SMartin Hundebøll }
307747e4221SSven Eckelmann 
3083e34819eSSven Eckelmann #define batadv_atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
3095f657ec0SAntonio Quartulli 
310f5d33d37SAntonio Quartulli /* Returns the smallest signed integer in two's complement with the sizeof x */
3113e34819eSSven Eckelmann #define batadv_smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
312f5d33d37SAntonio Quartulli 
313f5d33d37SAntonio Quartulli /* Checks if a sequence number x is a predecessor/successor of y.
314f5d33d37SAntonio Quartulli  * they handle overflows/underflows and can correctly check for a
315f5d33d37SAntonio Quartulli  * predecessor/successor unless the variable sequence number has grown by
316f5d33d37SAntonio Quartulli  * more then 2**(bitwidth(x)-1)-1.
3176b5e971aSSven Eckelmann  * This means that for a u8 with the maximum value 255, it would think:
318f5d33d37SAntonio Quartulli  *  - when adding nothing - it is neither a predecessor nor a successor
319f5d33d37SAntonio Quartulli  *  - before adding more than 127 to the starting value - it is a predecessor,
320f5d33d37SAntonio Quartulli  *  - when adding 128 - it is neither a predecessor nor a successor,
3219cfc7bd6SSven Eckelmann  *  - after adding more than 127 to the starting value - it is a successor
3229cfc7bd6SSven Eckelmann  */
3233e34819eSSven Eckelmann #define batadv_seq_before(x, y) ({typeof(x)_d1 = (x); \
3240bb85751SSven Eckelmann 				 typeof(y)_d2 = (y); \
3250bb85751SSven Eckelmann 				 typeof(x)_dummy = (_d1 - _d2); \
3260bb85751SSven Eckelmann 				 (void)(&_d1 == &_d2); \
3273e34819eSSven Eckelmann 				 _dummy > batadv_smallest_signed_int(_dummy); })
3283e34819eSSven Eckelmann #define batadv_seq_after(x, y) batadv_seq_before(y, x)
329f5d33d37SAntonio Quartulli 
330f8214865SMartin Hundebøll /* Stop preemption on local cpu while incrementing the counter */
33156303d34SSven Eckelmann static inline void batadv_add_counter(struct batadv_priv *bat_priv, size_t idx,
332f8214865SMartin Hundebøll 				      size_t count)
333f8214865SMartin Hundebøll {
33456917443SShan Wei 	this_cpu_add(bat_priv->bat_counters[idx], count);
335f8214865SMartin Hundebøll }
336f8214865SMartin Hundebøll 
337f8214865SMartin Hundebøll #define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1)
338f8214865SMartin Hundebøll 
33962fe710fSSven Eckelmann /**
34062fe710fSSven Eckelmann  * batadv_sum_counter - Sum the cpu-local counters for index 'idx'
3417afcbbefSSven Eckelmann  * @bat_priv: the bat priv with all the soft interface information
3427afcbbefSSven Eckelmann  * @idx: index of counter to sum up
34362fe710fSSven Eckelmann  *
34462fe710fSSven Eckelmann  * Return: sum of all cpu-local counters
34562fe710fSSven Eckelmann  */
3466b5e971aSSven Eckelmann static inline u64 batadv_sum_counter(struct batadv_priv *bat_priv,  size_t idx)
347f8214865SMartin Hundebøll {
3486b5e971aSSven Eckelmann 	u64 *counters, sum = 0;
349f8214865SMartin Hundebøll 	int cpu;
350f8214865SMartin Hundebøll 
351f8214865SMartin Hundebøll 	for_each_possible_cpu(cpu) {
352f8214865SMartin Hundebøll 		counters = per_cpu_ptr(bat_priv->bat_counters, cpu);
353f8214865SMartin Hundebøll 		sum += counters[idx];
354f8214865SMartin Hundebøll 	}
355f8214865SMartin Hundebøll 
356f8214865SMartin Hundebøll 	return sum;
357f8214865SMartin Hundebøll }
358f8214865SMartin Hundebøll 
3593c12de9aSMartin Hundebøll /* Define a macro to reach the control buffer of the skb. The members of the
3603c12de9aSMartin Hundebøll  * control buffer are defined in struct batadv_skb_cb in types.h.
3613c12de9aSMartin Hundebøll  * The macro is inspired by the similar macro TCP_SKB_CB() in tcp.h.
3623c12de9aSMartin Hundebøll  */
3633c12de9aSMartin Hundebøll #define BATADV_SKB_CB(__skb)       ((struct batadv_skb_cb *)&((__skb)->cb[0]))
3643c12de9aSMartin Hundebøll 
365ef261577SMarek Lindner void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
3666b5e971aSSven Eckelmann 				    u8 type, u8 version,
3676b5e971aSSven Eckelmann 				    void *tvlv_value, u16 tvlv_value_len);
3686b5e971aSSven Eckelmann u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
369ef261577SMarek Lindner 				     unsigned char **packet_buff,
3706b5e971aSSven Eckelmann 				     int *packet_buff_len, int packet_min_len);
371ef261577SMarek Lindner void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
372ef261577SMarek Lindner 			     struct batadv_ogm_packet *batadv_ogm_packet,
373ef261577SMarek Lindner 			     struct batadv_orig_node *orig_node);
374ef261577SMarek Lindner void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
3756b5e971aSSven Eckelmann 				      u8 type, u8 version);
376ef261577SMarek Lindner 
377ef261577SMarek Lindner void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
378ef261577SMarek Lindner 				  void (*optr)(struct batadv_priv *bat_priv,
379ef261577SMarek Lindner 					       struct batadv_orig_node *orig,
3806b5e971aSSven Eckelmann 					       u8 flags,
381ef261577SMarek Lindner 					       void *tvlv_value,
3826b5e971aSSven Eckelmann 					       u16 tvlv_value_len),
383ef261577SMarek Lindner 				  int (*uptr)(struct batadv_priv *bat_priv,
3846b5e971aSSven Eckelmann 					      u8 *src, u8 *dst,
385ef261577SMarek Lindner 					      void *tvlv_value,
3866b5e971aSSven Eckelmann 					      u16 tvlv_value_len),
3876b5e971aSSven Eckelmann 				  u8 type, u8 version, u8 flags);
388ef261577SMarek Lindner void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
3896b5e971aSSven Eckelmann 				    u8 type, u8 version);
390ef261577SMarek Lindner int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
391ef261577SMarek Lindner 				   bool ogm_source,
392ef261577SMarek Lindner 				   struct batadv_orig_node *orig_node,
3936b5e971aSSven Eckelmann 				   u8 *src, u8 *dst,
3946b5e971aSSven Eckelmann 				   void *tvlv_buff, u16 tvlv_buff_len);
3956b5e971aSSven Eckelmann void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
3966b5e971aSSven Eckelmann 			      u8 *dst, u8 type, u8 version,
3976b5e971aSSven Eckelmann 			      void *tvlv_value, u16 tvlv_value_len);
398c018ad3dSAntonio Quartulli unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len);
399eceb22aeSAntonio Quartulli bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid);
400ef261577SMarek Lindner 
401c6c8fea2SSven Eckelmann #endif /* _NET_BATMAN_ADV_MAIN_H_ */
402