xref: /linux/net/batman-adv/main.h (revision a73105b8d4c765d9ebfb664d0a66802127d8e4c7)
1c6c8fea2SSven Eckelmann /*
264afe353SSven Eckelmann  * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
3c6c8fea2SSven Eckelmann  *
4c6c8fea2SSven Eckelmann  * Marek Lindner, Simon Wunderlich
5c6c8fea2SSven Eckelmann  *
6c6c8fea2SSven Eckelmann  * This program is free software; you can redistribute it and/or
7c6c8fea2SSven Eckelmann  * modify it under the terms of version 2 of the GNU General Public
8c6c8fea2SSven Eckelmann  * License as published by the Free Software Foundation.
9c6c8fea2SSven Eckelmann  *
10c6c8fea2SSven Eckelmann  * This program is distributed in the hope that it will be useful, but
11c6c8fea2SSven Eckelmann  * WITHOUT ANY WARRANTY; without even the implied warranty of
12c6c8fea2SSven Eckelmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13c6c8fea2SSven Eckelmann  * General Public License for more details.
14c6c8fea2SSven Eckelmann  *
15c6c8fea2SSven Eckelmann  * You should have received a copy of the GNU General Public License
16c6c8fea2SSven Eckelmann  * along with this program; if not, write to the Free Software
17c6c8fea2SSven Eckelmann  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18c6c8fea2SSven Eckelmann  * 02110-1301, USA
19c6c8fea2SSven Eckelmann  *
20c6c8fea2SSven Eckelmann  */
21c6c8fea2SSven Eckelmann 
22c6c8fea2SSven Eckelmann #ifndef _NET_BATMAN_ADV_MAIN_H_
23c6c8fea2SSven Eckelmann #define _NET_BATMAN_ADV_MAIN_H_
24c6c8fea2SSven Eckelmann 
25c6c8fea2SSven Eckelmann #define DRIVER_AUTHOR "Marek Lindner <lindner_marek@yahoo.de>, " \
26c6c8fea2SSven Eckelmann 		      "Simon Wunderlich <siwu@hrz.tu-chemnitz.de>"
27c6c8fea2SSven Eckelmann #define DRIVER_DESC   "B.A.T.M.A.N. advanced"
28c6c8fea2SSven Eckelmann #define DRIVER_DEVICE "batman-adv"
29c6c8fea2SSven Eckelmann 
30c6c8fea2SSven Eckelmann #define SOURCE_VERSION "next"
31c6c8fea2SSven Eckelmann 
32c6c8fea2SSven Eckelmann 
33c6c8fea2SSven Eckelmann /* B.A.T.M.A.N. parameters */
34c6c8fea2SSven Eckelmann 
35c6c8fea2SSven Eckelmann #define TQ_MAX_VALUE 255
36c6c8fea2SSven Eckelmann #define JITTER 20
37c6c8fea2SSven Eckelmann 
386e215fd8SSven Eckelmann  /* Time To Live of broadcast messages */
396e215fd8SSven Eckelmann #define TTL 50
406e215fd8SSven Eckelmann 
416e215fd8SSven Eckelmann /* purge originators after time in seconds if no valid packet comes in
426e215fd8SSven Eckelmann  * -> TODO: check influence on TQ_LOCAL_WINDOW_SIZE */
436e215fd8SSven Eckelmann #define PURGE_TIMEOUT 200
442dafb49dSAntonio Quartulli #define TT_LOCAL_TIMEOUT 3600 /* in seconds */
45c6c8fea2SSven Eckelmann 
466e215fd8SSven Eckelmann /* sliding packet range of received originator messages in squence numbers
476e215fd8SSven Eckelmann  * (should be a multiple of our word size) */
486e215fd8SSven Eckelmann #define TQ_LOCAL_WINDOW_SIZE 64
49*a73105b8SAntonio Quartulli #define TT_REQUEST_TIMEOUT 3 /* seconds we have to keep pending tt_req */
50*a73105b8SAntonio Quartulli 
51c6c8fea2SSven Eckelmann #define TQ_GLOBAL_WINDOW_SIZE 5
52c6c8fea2SSven Eckelmann #define TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
53c6c8fea2SSven Eckelmann #define TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
54c6c8fea2SSven Eckelmann #define TQ_TOTAL_BIDRECT_LIMIT 1
55c6c8fea2SSven Eckelmann 
56*a73105b8SAntonio Quartulli #define TT_OGM_APPEND_MAX 3 /* number of OGMs sent with the last tt diff */
57*a73105b8SAntonio Quartulli 
58ecbd5321SMarek Lindner #define NO_FLAGS 0
59ecbd5321SMarek Lindner 
60c6c8fea2SSven Eckelmann #define NUM_WORDS (TQ_LOCAL_WINDOW_SIZE / WORD_BIT_SIZE)
61c6c8fea2SSven Eckelmann 
62c6c8fea2SSven Eckelmann #define LOG_BUF_LEN 8192	  /* has to be a power of 2 */
63c6c8fea2SSven Eckelmann 
64c6c8fea2SSven Eckelmann #define VIS_INTERVAL 5000	/* 5 seconds */
65c6c8fea2SSven Eckelmann 
666e215fd8SSven Eckelmann /* how much worse secondary interfaces may be to be considered as bonding
676e215fd8SSven Eckelmann  * candidates */
68c6c8fea2SSven Eckelmann #define BONDING_TQ_THRESHOLD	50
69c6c8fea2SSven Eckelmann 
706e215fd8SSven Eckelmann /* should not be bigger than 512 bytes or change the size of
71c6c8fea2SSven Eckelmann  * forw_packet->direct_link_flags */
726e215fd8SSven Eckelmann #define MAX_AGGREGATION_BYTES 512
73c6c8fea2SSven Eckelmann #define MAX_AGGREGATION_MS 100
74c6c8fea2SSven Eckelmann 
75c6c8fea2SSven Eckelmann #define SOFTIF_NEIGH_TIMEOUT 180000 /* 3 minutes */
76c6c8fea2SSven Eckelmann 
776e215fd8SSven Eckelmann /* don't reset again within 30 seconds */
78c6c8fea2SSven Eckelmann #define RESET_PROTECTION_MS 30000
79c6c8fea2SSven Eckelmann #define EXPECTED_SEQNO_RANGE	65536
80c6c8fea2SSven Eckelmann 
81e8958dbfSSven Eckelmann enum mesh_state {
82e8958dbfSSven Eckelmann 	MESH_INACTIVE,
83e8958dbfSSven Eckelmann 	MESH_ACTIVE,
84e8958dbfSSven Eckelmann 	MESH_DEACTIVATING
85e8958dbfSSven Eckelmann };
86c6c8fea2SSven Eckelmann 
87c6c8fea2SSven Eckelmann #define BCAST_QUEUE_LEN		256
88c6c8fea2SSven Eckelmann #define BATMAN_QUEUE_LEN	256
89c6c8fea2SSven Eckelmann 
90c6c8fea2SSven Eckelmann /*
91c6c8fea2SSven Eckelmann  * Debug Messages
92c6c8fea2SSven Eckelmann  */
93c6c8fea2SSven Eckelmann #ifdef pr_fmt
94c6c8fea2SSven Eckelmann #undef pr_fmt
95c6c8fea2SSven Eckelmann #endif
966e215fd8SSven Eckelmann /* Append 'batman-adv: ' before kernel messages */
976e215fd8SSven Eckelmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c6c8fea2SSven Eckelmann 
996e215fd8SSven Eckelmann /* all messages related to routing / flooding / broadcasting / etc */
100e8958dbfSSven Eckelmann enum dbg_level {
101e8958dbfSSven Eckelmann 	DBG_BATMAN = 1 << 0,
102e8958dbfSSven Eckelmann 	DBG_ROUTES = 1 << 1, /* route added / changed / deleted */
103*a73105b8SAntonio Quartulli 	DBG_TT	   = 1 << 2, /* translation table operations */
104*a73105b8SAntonio Quartulli 	DBG_ALL    = 7
105e8958dbfSSven Eckelmann };
106c6c8fea2SSven Eckelmann 
107c6c8fea2SSven Eckelmann 
108c6c8fea2SSven Eckelmann /*
109c6c8fea2SSven Eckelmann  *  Vis
110c6c8fea2SSven Eckelmann  */
111c6c8fea2SSven Eckelmann 
112c6c8fea2SSven Eckelmann /*
113c6c8fea2SSven Eckelmann  * Kernel headers
114c6c8fea2SSven Eckelmann  */
115c6c8fea2SSven Eckelmann 
116c6c8fea2SSven Eckelmann #include <linux/mutex.h>	/* mutex */
117c6c8fea2SSven Eckelmann #include <linux/module.h>	/* needed by all modules */
118c6c8fea2SSven Eckelmann #include <linux/netdevice.h>	/* netdevice */
119c6c8fea2SSven Eckelmann #include <linux/etherdevice.h>  /* ethernet address classifaction */
120c6c8fea2SSven Eckelmann #include <linux/if_ether.h>	/* ethernet header */
121c6c8fea2SSven Eckelmann #include <linux/poll.h>		/* poll_table */
122c6c8fea2SSven Eckelmann #include <linux/kthread.h>	/* kernel threads */
123c6c8fea2SSven Eckelmann #include <linux/pkt_sched.h>	/* schedule types */
124c6c8fea2SSven Eckelmann #include <linux/workqueue.h>	/* workqueue */
125c6c8fea2SSven Eckelmann #include <linux/slab.h>
126c6c8fea2SSven Eckelmann #include <net/sock.h>		/* struct sock */
127c6c8fea2SSven Eckelmann #include <linux/jiffies.h>
128c6c8fea2SSven Eckelmann #include <linux/seq_file.h>
129c6c8fea2SSven Eckelmann #include "types.h"
130c6c8fea2SSven Eckelmann 
131c6c8fea2SSven Eckelmann #ifndef REVISION_VERSION
132c6c8fea2SSven Eckelmann #define REVISION_VERSION_STR ""
133c6c8fea2SSven Eckelmann #else
134c6c8fea2SSven Eckelmann #define REVISION_VERSION_STR " "REVISION_VERSION
135c6c8fea2SSven Eckelmann #endif
136c6c8fea2SSven Eckelmann 
1374389e47aSMarek Lindner extern struct list_head hardif_list;
138c6c8fea2SSven Eckelmann 
139c6c8fea2SSven Eckelmann extern unsigned char broadcast_addr[];
140c6c8fea2SSven Eckelmann extern struct workqueue_struct *bat_event_workqueue;
141c6c8fea2SSven Eckelmann 
142c6c8fea2SSven Eckelmann int mesh_init(struct net_device *soft_iface);
143c6c8fea2SSven Eckelmann void mesh_free(struct net_device *soft_iface);
144c6c8fea2SSven Eckelmann void inc_module_count(void);
145c6c8fea2SSven Eckelmann void dec_module_count(void);
146747e4221SSven Eckelmann int is_my_mac(const uint8_t *addr);
147c6c8fea2SSven Eckelmann 
148c6c8fea2SSven Eckelmann #ifdef CONFIG_BATMAN_ADV_DEBUG
149747e4221SSven Eckelmann int debug_log(struct bat_priv *bat_priv, const char *fmt, ...) __printf(2, 3);
150c6c8fea2SSven Eckelmann 
151c6c8fea2SSven Eckelmann #define bat_dbg(type, bat_priv, fmt, arg...)			\
152c6c8fea2SSven Eckelmann 	do {							\
153c6c8fea2SSven Eckelmann 		if (atomic_read(&bat_priv->log_level) & type)	\
154c6c8fea2SSven Eckelmann 			debug_log(bat_priv, fmt, ## arg);	\
155c6c8fea2SSven Eckelmann 	}							\
156c6c8fea2SSven Eckelmann 	while (0)
157c6c8fea2SSven Eckelmann #else /* !CONFIG_BATMAN_ADV_DEBUG */
158d3a547beSSven Eckelmann __printf(3, 4)
159b4e17054SSven Eckelmann static inline void bat_dbg(int type __always_unused,
160aa0adb1aSSven Eckelmann 			   struct bat_priv *bat_priv __always_unused,
161747e4221SSven Eckelmann 			   const char *fmt __always_unused, ...)
162c6c8fea2SSven Eckelmann {
163c6c8fea2SSven Eckelmann }
164c6c8fea2SSven Eckelmann #endif
165c6c8fea2SSven Eckelmann 
166c6c8fea2SSven Eckelmann #define bat_info(net_dev, fmt, arg...)					\
167c6c8fea2SSven Eckelmann 	do {								\
168c6c8fea2SSven Eckelmann 		struct net_device *_netdev = (net_dev);                 \
169c6c8fea2SSven Eckelmann 		struct bat_priv *_batpriv = netdev_priv(_netdev);       \
170c6c8fea2SSven Eckelmann 		bat_dbg(DBG_ALL, _batpriv, fmt, ## arg);		\
171c6c8fea2SSven Eckelmann 		pr_info("%s: " fmt, _netdev->name, ## arg);		\
172c6c8fea2SSven Eckelmann 	} while (0)
173c6c8fea2SSven Eckelmann #define bat_err(net_dev, fmt, arg...)					\
174c6c8fea2SSven Eckelmann 	do {								\
175c6c8fea2SSven Eckelmann 		struct net_device *_netdev = (net_dev);                 \
176c6c8fea2SSven Eckelmann 		struct bat_priv *_batpriv = netdev_priv(_netdev);       \
177c6c8fea2SSven Eckelmann 		bat_dbg(DBG_ALL, _batpriv, fmt, ## arg);		\
178c6c8fea2SSven Eckelmann 		pr_err("%s: " fmt, _netdev->name, ## arg);		\
179c6c8fea2SSven Eckelmann 	} while (0)
180c6c8fea2SSven Eckelmann 
18139901e71SMarek Lindner /**
18239901e71SMarek Lindner  * returns 1 if they are the same ethernet addr
18339901e71SMarek Lindner  *
18439901e71SMarek Lindner  * note: can't use compare_ether_addr() as it requires aligned memory
18539901e71SMarek Lindner  */
186747e4221SSven Eckelmann 
187747e4221SSven Eckelmann static inline int compare_eth(const void *data1, const void *data2)
18839901e71SMarek Lindner {
18939901e71SMarek Lindner 	return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
19039901e71SMarek Lindner }
19139901e71SMarek Lindner 
192747e4221SSven Eckelmann 
1935f657ec0SAntonio Quartulli #define atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
1945f657ec0SAntonio Quartulli 
195f5d33d37SAntonio Quartulli /* Returns the smallest signed integer in two's complement with the sizeof x */
196f5d33d37SAntonio Quartulli #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
197f5d33d37SAntonio Quartulli 
198f5d33d37SAntonio Quartulli /* Checks if a sequence number x is a predecessor/successor of y.
199f5d33d37SAntonio Quartulli  * they handle overflows/underflows and can correctly check for a
200f5d33d37SAntonio Quartulli  * predecessor/successor unless the variable sequence number has grown by
201f5d33d37SAntonio Quartulli  * more then 2**(bitwidth(x)-1)-1.
202f5d33d37SAntonio Quartulli  * This means that for a uint8_t with the maximum value 255, it would think:
203f5d33d37SAntonio Quartulli  *  - when adding nothing - it is neither a predecessor nor a successor
204f5d33d37SAntonio Quartulli  *  - before adding more than 127 to the starting value - it is a predecessor,
205f5d33d37SAntonio Quartulli  *  - when adding 128 - it is neither a predecessor nor a successor,
206f5d33d37SAntonio Quartulli  *  - after adding more than 127 to the starting value - it is a successor */
2070bb85751SSven Eckelmann #define seq_before(x, y) ({typeof(x) _d1 = (x); \
2080bb85751SSven Eckelmann 			  typeof(y) _d2 = (y); \
2090bb85751SSven Eckelmann 			  typeof(x) _dummy = (_d1 - _d2); \
2100bb85751SSven Eckelmann 			  (void) (&_d1 == &_d2); \
211f5d33d37SAntonio Quartulli 			  _dummy > smallest_signed_int(_dummy); })
212f5d33d37SAntonio Quartulli #define seq_after(x, y) seq_before(y, x)
213f5d33d37SAntonio Quartulli 
214c6c8fea2SSven Eckelmann #endif /* _NET_BATMAN_ADV_MAIN_H_ */
215