blob: 82723b5dce618cab41e70ed404ca26e61f084201 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#ifndef _NET_BATMAN_ADV_MAIN_H_
23#define _NET_BATMAN_ADV_MAIN_H_
24
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#define DRIVER_AUTHOR "Marek Lindner <lindner_marek@yahoo.de>, " \
26 "Simon Wunderlich <siwu@hrz.tu-chemnitz.de>"
27#define DRIVER_DESC "B.A.T.M.A.N. advanced"
28#define DRIVER_DEVICE "batman-adv"
29
Sven Eckelmann44c43492011-07-05 10:42:51 +020030#ifndef SOURCE_VERSION
Sven Eckelmannfdc8ff12012-02-05 15:23:55 +010031#define SOURCE_VERSION "2012.1.0"
Sven Eckelmann44c43492011-07-05 10:42:51 +020032#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000033
34/* B.A.T.M.A.N. parameters */
35
36#define TQ_MAX_VALUE 255
37#define JITTER 20
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
Sven Eckelmann6e215fd2011-05-08 12:45:45 +020039 /* Time To Live of broadcast messages */
40#define TTL 50
41
42/* purge originators after time in seconds if no valid packet comes in
43 * -> TODO: check influence on TQ_LOCAL_WINDOW_SIZE */
Marek Lindner032b7962011-12-20 19:30:40 +080044#define PURGE_TIMEOUT 200000 /* 200 seconds */
45#define TT_LOCAL_TIMEOUT 3600000 /* in miliseconds */
46#define TT_CLIENT_ROAM_TIMEOUT 600000 /* in miliseconds */
Antonio Quartulli015758d2011-07-09 17:52:13 +020047/* sliding packet range of received originator messages in sequence numbers
Sven Eckelmann6e215fd2011-05-08 12:45:45 +020048 * (should be a multiple of our word size) */
49#define TQ_LOCAL_WINDOW_SIZE 64
Marek Lindner032b7962011-12-20 19:30:40 +080050#define TT_REQUEST_TIMEOUT 3000 /* miliseconds we have to keep
51 * pending tt_req */
Antonio Quartullia73105b2011-04-27 14:27:44 +020052
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053#define TQ_GLOBAL_WINDOW_SIZE 5
54#define TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
55#define TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
56#define TQ_TOTAL_BIDRECT_LIMIT 1
57
Antonio Quartullia73105b2011-04-27 14:27:44 +020058#define TT_OGM_APPEND_MAX 3 /* number of OGMs sent with the last tt diff */
59
Marek Lindner032b7962011-12-20 19:30:40 +080060#define ROAMING_MAX_TIME 20000 /* Time in which a client can roam at most
61 * ROAMING_MAX_COUNT times in miliseconds*/
Antonio Quartullicc47f662011-04-27 14:27:57 +020062#define ROAMING_MAX_COUNT 5
63
Marek Lindnerecbd5322011-06-09 17:13:09 +020064#define NO_FLAGS 0
65
Antonio Quartullibc279082011-07-07 15:35:35 +020066#define NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
67
Sven Eckelmann0079d2c2012-02-04 17:34:52 +010068#define NUM_WORDS BITS_TO_LONGS(TQ_LOCAL_WINDOW_SIZE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070#define LOG_BUF_LEN 8192 /* has to be a power of 2 */
71
72#define VIS_INTERVAL 5000 /* 5 seconds */
73
Sven Eckelmann6e215fd2011-05-08 12:45:45 +020074/* how much worse secondary interfaces may be to be considered as bonding
75 * candidates */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076#define BONDING_TQ_THRESHOLD 50
77
Sven Eckelmann6e215fd2011-05-08 12:45:45 +020078/* should not be bigger than 512 bytes or change the size of
79 * forw_packet->direct_link_flags */
80#define MAX_AGGREGATION_BYTES 512
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081#define MAX_AGGREGATION_MS 100
82
Simon Wunderlich23721382012-01-22 20:00:19 +010083#define BLA_PERIOD_LENGTH 10000 /* 10 seconds */
84#define BLA_BACKBONE_TIMEOUT (BLA_PERIOD_LENGTH * 3)
85#define BLA_CLAIM_TIMEOUT (BLA_PERIOD_LENGTH * 10)
Sven Eckelmann6e215fd2011-05-08 12:45:45 +020086/* don't reset again within 30 seconds */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087#define RESET_PROTECTION_MS 30000
88#define EXPECTED_SEQNO_RANGE 65536
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089
Sven Eckelmanne8958db2011-06-04 11:26:00 +020090enum mesh_state {
91 MESH_INACTIVE,
92 MESH_ACTIVE,
93 MESH_DEACTIVATING
94};
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095
96#define BCAST_QUEUE_LEN 256
97#define BATMAN_QUEUE_LEN 256
98
Antonio Quartullic6bda682011-04-26 18:26:01 +020099enum uev_action {
100 UEV_ADD = 0,
101 UEV_DEL,
102 UEV_CHANGE
103};
104
105enum uev_type {
106 UEV_GW = 0
107};
108
Antonio Quartulli43676ab2011-04-26 21:31:45 +0200109#define GW_THRESHOLD 50
110
Sven Eckelmann21a12362012-03-07 09:07:46 +0100111/* Debug Messages */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112#ifdef pr_fmt
113#undef pr_fmt
114#endif
Sven Eckelmann6e215fd2011-05-08 12:45:45 +0200115/* Append 'batman-adv: ' before kernel messages */
116#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000117
Sven Eckelmann6e215fd2011-05-08 12:45:45 +0200118/* all messages related to routing / flooding / broadcasting / etc */
Sven Eckelmanne8958db2011-06-04 11:26:00 +0200119enum dbg_level {
120 DBG_BATMAN = 1 << 0,
121 DBG_ROUTES = 1 << 1, /* route added / changed / deleted */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200122 DBG_TT = 1 << 2, /* translation table operations */
Simon Wunderlich23721382012-01-22 20:00:19 +0100123 DBG_BLA = 1 << 3, /* bridge loop avoidance */
124 DBG_ALL = 15
Sven Eckelmanne8958db2011-06-04 11:26:00 +0200125};
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126
Sven Eckelmann21a12362012-03-07 09:07:46 +0100127/* Kernel headers */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128
129#include <linux/mutex.h> /* mutex */
130#include <linux/module.h> /* needed by all modules */
131#include <linux/netdevice.h> /* netdevice */
Antonio Quartulli015758d2011-07-09 17:52:13 +0200132#include <linux/etherdevice.h> /* ethernet address classification */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133#include <linux/if_ether.h> /* ethernet header */
134#include <linux/poll.h> /* poll_table */
135#include <linux/kthread.h> /* kernel threads */
136#include <linux/pkt_sched.h> /* schedule types */
137#include <linux/workqueue.h> /* workqueue */
138#include <linux/slab.h>
139#include <net/sock.h> /* struct sock */
140#include <linux/jiffies.h>
141#include <linux/seq_file.h>
142#include "types.h"
143
Marek Lindner1c280472011-11-28 17:40:17 +0800144extern char bat_routing_algo[];
Marek Lindner4389e472011-02-18 12:33:19 +0000145extern struct list_head hardif_list;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146
147extern unsigned char broadcast_addr[];
148extern struct workqueue_struct *bat_event_workqueue;
149
150int mesh_init(struct net_device *soft_iface);
151void mesh_free(struct net_device *soft_iface);
152void inc_module_count(void);
153void dec_module_count(void);
Sven Eckelmann747e4222011-05-14 23:14:50 +0200154int is_my_mac(const uint8_t *addr);
Marek Lindner1c280472011-11-28 17:40:17 +0800155int bat_algo_register(struct bat_algo_ops *bat_algo_ops);
156int bat_algo_select(struct bat_priv *bat_priv, char *name);
157int bat_algo_seq_print_text(struct seq_file *seq, void *offset);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158
159#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann747e4222011-05-14 23:14:50 +0200160int debug_log(struct bat_priv *bat_priv, const char *fmt, ...) __printf(2, 3);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
162#define bat_dbg(type, bat_priv, fmt, arg...) \
163 do { \
164 if (atomic_read(&bat_priv->log_level) & type) \
165 debug_log(bat_priv, fmt, ## arg); \
166 } \
167 while (0)
168#else /* !CONFIG_BATMAN_ADV_DEBUG */
Sven Eckelmannd3a547b2011-05-14 23:14:46 +0200169__printf(3, 4)
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200170static inline void bat_dbg(int type __always_unused,
Sven Eckelmannaa0adb12011-01-15 14:39:43 +0000171 struct bat_priv *bat_priv __always_unused,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200172 const char *fmt __always_unused, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173{
174}
175#endif
176
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177#define bat_info(net_dev, fmt, arg...) \
178 do { \
179 struct net_device *_netdev = (net_dev); \
180 struct bat_priv *_batpriv = netdev_priv(_netdev); \
181 bat_dbg(DBG_ALL, _batpriv, fmt, ## arg); \
182 pr_info("%s: " fmt, _netdev->name, ## arg); \
183 } while (0)
184#define bat_err(net_dev, fmt, arg...) \
185 do { \
186 struct net_device *_netdev = (net_dev); \
187 struct bat_priv *_batpriv = netdev_priv(_netdev); \
188 bat_dbg(DBG_ALL, _batpriv, fmt, ## arg); \
189 pr_err("%s: " fmt, _netdev->name, ## arg); \
190 } while (0)
191
Marek Lindner39901e72011-02-18 12:28:08 +0000192/**
193 * returns 1 if they are the same ethernet addr
194 *
195 * note: can't use compare_ether_addr() as it requires aligned memory
196 */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200197
198static inline int compare_eth(const void *data1, const void *data2)
Marek Lindner39901e72011-02-18 12:28:08 +0000199{
200 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
201}
202
Martin Hundebølla04ccd52011-12-08 13:32:41 +0100203/**
204 * has_timed_out - compares current time (jiffies) and timestamp + timeout
205 * @timestamp: base value to compare with (in jiffies)
206 * @timeout: added to base value before comparing (in milliseconds)
207 *
208 * Returns true if current time is after timestamp + timeout
209 */
210static inline bool has_timed_out(unsigned long timestamp, unsigned int timeout)
211{
212 return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout));
213}
Sven Eckelmann747e4222011-05-14 23:14:50 +0200214
Antonio Quartulli5f657ec2011-04-20 09:52:56 +0200215#define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
216
Antonio Quartullif5d33d32011-05-18 09:20:50 +0200217/* Returns the smallest signed integer in two's complement with the sizeof x */
218#define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
219
220/* Checks if a sequence number x is a predecessor/successor of y.
221 * they handle overflows/underflows and can correctly check for a
222 * predecessor/successor unless the variable sequence number has grown by
223 * more then 2**(bitwidth(x)-1)-1.
224 * This means that for a uint8_t with the maximum value 255, it would think:
225 * - when adding nothing - it is neither a predecessor nor a successor
226 * - before adding more than 127 to the starting value - it is a predecessor,
227 * - when adding 128 - it is neither a predecessor nor a successor,
228 * - after adding more than 127 to the starting value - it is a successor */
Sven Eckelmann0bb85752011-05-19 21:43:08 +0200229#define seq_before(x, y) ({typeof(x) _d1 = (x); \
230 typeof(y) _d2 = (y); \
231 typeof(x) _dummy = (_d1 - _d2); \
232 (void) (&_d1 == &_d2); \
233 _dummy > smallest_signed_int(_dummy); })
Antonio Quartullif5d33d32011-05-18 09:20:50 +0200234#define seq_after(x, y) seq_before(y, x)
235
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236#endif /* _NET_BATMAN_ADV_MAIN_H_ */