Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 567db7b | 2012-01-01 00:41:38 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 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 Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 25 | #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 Eckelmann | 44c4349 | 2011-07-05 10:42:51 +0200 | [diff] [blame] | 30 | #ifndef SOURCE_VERSION |
Sven Eckelmann | 284fefd | 2012-06-17 13:26:37 +0200 | [diff] [blame] | 31 | #define SOURCE_VERSION "2012.3.0" |
Sven Eckelmann | 44c4349 | 2011-07-05 10:42:51 +0200 | [diff] [blame] | 32 | #endif |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 33 | |
| 34 | /* B.A.T.M.A.N. parameters */ |
| 35 | |
| 36 | #define TQ_MAX_VALUE 255 |
| 37 | #define JITTER 20 |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 38 | |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 39 | /* 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 Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 44 | #define PURGE_TIMEOUT 200000 /* 200 seconds */ |
| 45 | #define TT_LOCAL_TIMEOUT 3600000 /* in miliseconds */ |
| 46 | #define TT_CLIENT_ROAM_TIMEOUT 600000 /* in miliseconds */ |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 47 | /* sliding packet range of received originator messages in sequence numbers |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 48 | * (should be a multiple of our word size) */ |
| 49 | #define TQ_LOCAL_WINDOW_SIZE 64 |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 50 | #define TT_REQUEST_TIMEOUT 3000 /* miliseconds we have to keep |
| 51 | * pending tt_req */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 52 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 53 | #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 Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 58 | #define TT_OGM_APPEND_MAX 3 /* number of OGMs sent with the last tt diff */ |
| 59 | |
Marek Lindner | 032b796 | 2011-12-20 19:30:40 +0800 | [diff] [blame] | 60 | #define ROAMING_MAX_TIME 20000 /* Time in which a client can roam at most |
| 61 | * ROAMING_MAX_COUNT times in miliseconds*/ |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 62 | #define ROAMING_MAX_COUNT 5 |
| 63 | |
Marek Lindner | ecbd532 | 2011-06-09 17:13:09 +0200 | [diff] [blame] | 64 | #define NO_FLAGS 0 |
| 65 | |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 66 | #define NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */ |
| 67 | |
Sven Eckelmann | 0079d2c | 2012-02-04 17:34:52 +0100 | [diff] [blame] | 68 | #define NUM_WORDS BITS_TO_LONGS(TQ_LOCAL_WINDOW_SIZE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 69 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 70 | #define LOG_BUF_LEN 8192 /* has to be a power of 2 */ |
| 71 | |
| 72 | #define VIS_INTERVAL 5000 /* 5 seconds */ |
| 73 | |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 74 | /* how much worse secondary interfaces may be to be considered as bonding |
| 75 | * candidates */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 76 | #define BONDING_TQ_THRESHOLD 50 |
| 77 | |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 78 | /* 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 Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 81 | #define MAX_AGGREGATION_MS 100 |
| 82 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 83 | #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) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 86 | |
| 87 | #define DUPLIST_SIZE 16 |
| 88 | #define DUPLIST_TIMEOUT 500 /* 500 ms */ |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 89 | /* don't reset again within 30 seconds */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 90 | #define RESET_PROTECTION_MS 30000 |
| 91 | #define EXPECTED_SEQNO_RANGE 65536 |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 92 | |
Sven Eckelmann | e8958db | 2011-06-04 11:26:00 +0200 | [diff] [blame] | 93 | enum mesh_state { |
| 94 | MESH_INACTIVE, |
| 95 | MESH_ACTIVE, |
| 96 | MESH_DEACTIVATING |
| 97 | }; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 98 | |
| 99 | #define BCAST_QUEUE_LEN 256 |
| 100 | #define BATMAN_QUEUE_LEN 256 |
| 101 | |
Antonio Quartulli | c6bda68 | 2011-04-26 18:26:01 +0200 | [diff] [blame] | 102 | enum uev_action { |
| 103 | UEV_ADD = 0, |
| 104 | UEV_DEL, |
| 105 | UEV_CHANGE |
| 106 | }; |
| 107 | |
| 108 | enum uev_type { |
| 109 | UEV_GW = 0 |
| 110 | }; |
| 111 | |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 112 | #define GW_THRESHOLD 50 |
| 113 | |
Sven Eckelmann | 21a1236 | 2012-03-07 09:07:46 +0100 | [diff] [blame] | 114 | /* Debug Messages */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 115 | #ifdef pr_fmt |
| 116 | #undef pr_fmt |
| 117 | #endif |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 118 | /* Append 'batman-adv: ' before kernel messages */ |
| 119 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 120 | |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 121 | /* all messages related to routing / flooding / broadcasting / etc */ |
Sven Eckelmann | e8958db | 2011-06-04 11:26:00 +0200 | [diff] [blame] | 122 | enum dbg_level { |
| 123 | DBG_BATMAN = 1 << 0, |
| 124 | DBG_ROUTES = 1 << 1, /* route added / changed / deleted */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 125 | DBG_TT = 1 << 2, /* translation table operations */ |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 126 | DBG_BLA = 1 << 3, /* bridge loop avoidance */ |
| 127 | DBG_ALL = 15 |
Sven Eckelmann | e8958db | 2011-06-04 11:26:00 +0200 | [diff] [blame] | 128 | }; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 129 | |
Sven Eckelmann | 21a1236 | 2012-03-07 09:07:46 +0100 | [diff] [blame] | 130 | /* Kernel headers */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 131 | |
| 132 | #include <linux/mutex.h> /* mutex */ |
| 133 | #include <linux/module.h> /* needed by all modules */ |
| 134 | #include <linux/netdevice.h> /* netdevice */ |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 135 | #include <linux/etherdevice.h> /* ethernet address classification */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 136 | #include <linux/if_ether.h> /* ethernet header */ |
| 137 | #include <linux/poll.h> /* poll_table */ |
| 138 | #include <linux/kthread.h> /* kernel threads */ |
| 139 | #include <linux/pkt_sched.h> /* schedule types */ |
| 140 | #include <linux/workqueue.h> /* workqueue */ |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 141 | #include <linux/percpu.h> |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 142 | #include <linux/slab.h> |
| 143 | #include <net/sock.h> /* struct sock */ |
| 144 | #include <linux/jiffies.h> |
| 145 | #include <linux/seq_file.h> |
| 146 | #include "types.h" |
| 147 | |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 148 | extern char bat_routing_algo[]; |
Marek Lindner | 4389e47 | 2011-02-18 12:33:19 +0000 | [diff] [blame] | 149 | extern struct list_head hardif_list; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | |
| 151 | extern unsigned char broadcast_addr[]; |
| 152 | extern struct workqueue_struct *bat_event_workqueue; |
| 153 | |
| 154 | int mesh_init(struct net_device *soft_iface); |
| 155 | void mesh_free(struct net_device *soft_iface); |
| 156 | void inc_module_count(void); |
| 157 | void dec_module_count(void); |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 158 | int is_my_mac(const uint8_t *addr); |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 159 | int batman_skb_recv(struct sk_buff *skb, struct net_device *dev, |
| 160 | struct packet_type *ptype, struct net_device *orig_dev); |
| 161 | int recv_handler_register(uint8_t packet_type, |
| 162 | int (*recv_handler)(struct sk_buff *, |
| 163 | struct hard_iface *)); |
| 164 | void recv_handler_unregister(uint8_t packet_type); |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 165 | int bat_algo_register(struct bat_algo_ops *bat_algo_ops); |
| 166 | int bat_algo_select(struct bat_priv *bat_priv, char *name); |
| 167 | int bat_algo_seq_print_text(struct seq_file *seq, void *offset); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 168 | |
| 169 | #ifdef CONFIG_BATMAN_ADV_DEBUG |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame^] | 170 | int batadv_debug_log(struct bat_priv *bat_priv, const char *fmt, ...) |
| 171 | __printf(2, 3); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 172 | |
| 173 | #define bat_dbg(type, bat_priv, fmt, arg...) \ |
| 174 | do { \ |
| 175 | if (atomic_read(&bat_priv->log_level) & type) \ |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame^] | 176 | batadv_debug_log(bat_priv, fmt, ## arg);\ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 177 | } \ |
| 178 | while (0) |
| 179 | #else /* !CONFIG_BATMAN_ADV_DEBUG */ |
Sven Eckelmann | d3a547b | 2011-05-14 23:14:46 +0200 | [diff] [blame] | 180 | __printf(3, 4) |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 181 | static inline void bat_dbg(int type __always_unused, |
Sven Eckelmann | aa0adb1 | 2011-01-15 14:39:43 +0000 | [diff] [blame] | 182 | struct bat_priv *bat_priv __always_unused, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 183 | const char *fmt __always_unused, ...) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 184 | { |
| 185 | } |
| 186 | #endif |
| 187 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 188 | #define bat_info(net_dev, fmt, arg...) \ |
| 189 | do { \ |
| 190 | struct net_device *_netdev = (net_dev); \ |
| 191 | struct bat_priv *_batpriv = netdev_priv(_netdev); \ |
| 192 | bat_dbg(DBG_ALL, _batpriv, fmt, ## arg); \ |
| 193 | pr_info("%s: " fmt, _netdev->name, ## arg); \ |
| 194 | } while (0) |
| 195 | #define bat_err(net_dev, fmt, arg...) \ |
| 196 | do { \ |
| 197 | struct net_device *_netdev = (net_dev); \ |
| 198 | struct bat_priv *_batpriv = netdev_priv(_netdev); \ |
| 199 | bat_dbg(DBG_ALL, _batpriv, fmt, ## arg); \ |
| 200 | pr_err("%s: " fmt, _netdev->name, ## arg); \ |
| 201 | } while (0) |
| 202 | |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 203 | /** |
| 204 | * returns 1 if they are the same ethernet addr |
| 205 | * |
| 206 | * note: can't use compare_ether_addr() as it requires aligned memory |
| 207 | */ |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 208 | |
| 209 | static inline int compare_eth(const void *data1, const void *data2) |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 210 | { |
| 211 | return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); |
| 212 | } |
| 213 | |
Martin Hundebøll | a04ccd5 | 2011-12-08 13:32:41 +0100 | [diff] [blame] | 214 | /** |
| 215 | * has_timed_out - compares current time (jiffies) and timestamp + timeout |
| 216 | * @timestamp: base value to compare with (in jiffies) |
| 217 | * @timeout: added to base value before comparing (in milliseconds) |
| 218 | * |
| 219 | * Returns true if current time is after timestamp + timeout |
| 220 | */ |
| 221 | static inline bool has_timed_out(unsigned long timestamp, unsigned int timeout) |
| 222 | { |
| 223 | return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout)); |
| 224 | } |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 225 | |
Antonio Quartulli | 5f657ec | 2011-04-20 09:52:56 +0200 | [diff] [blame] | 226 | #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0) |
| 227 | |
Antonio Quartulli | f5d33d3 | 2011-05-18 09:20:50 +0200 | [diff] [blame] | 228 | /* Returns the smallest signed integer in two's complement with the sizeof x */ |
| 229 | #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u))) |
| 230 | |
| 231 | /* Checks if a sequence number x is a predecessor/successor of y. |
| 232 | * they handle overflows/underflows and can correctly check for a |
| 233 | * predecessor/successor unless the variable sequence number has grown by |
| 234 | * more then 2**(bitwidth(x)-1)-1. |
| 235 | * This means that for a uint8_t with the maximum value 255, it would think: |
| 236 | * - when adding nothing - it is neither a predecessor nor a successor |
| 237 | * - before adding more than 127 to the starting value - it is a predecessor, |
| 238 | * - when adding 128 - it is neither a predecessor nor a successor, |
| 239 | * - after adding more than 127 to the starting value - it is a successor */ |
Sven Eckelmann | 0bb85751 | 2011-05-19 21:43:08 +0200 | [diff] [blame] | 240 | #define seq_before(x, y) ({typeof(x) _d1 = (x); \ |
| 241 | typeof(y) _d2 = (y); \ |
| 242 | typeof(x) _dummy = (_d1 - _d2); \ |
| 243 | (void) (&_d1 == &_d2); \ |
| 244 | _dummy > smallest_signed_int(_dummy); }) |
Antonio Quartulli | f5d33d3 | 2011-05-18 09:20:50 +0200 | [diff] [blame] | 245 | #define seq_after(x, y) seq_before(y, x) |
| 246 | |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 247 | /* Stop preemption on local cpu while incrementing the counter */ |
| 248 | static inline void batadv_add_counter(struct bat_priv *bat_priv, size_t idx, |
| 249 | size_t count) |
| 250 | { |
| 251 | int cpu = get_cpu(); |
| 252 | per_cpu_ptr(bat_priv->bat_counters, cpu)[idx] += count; |
| 253 | put_cpu(); |
| 254 | } |
| 255 | |
| 256 | #define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1) |
| 257 | |
| 258 | /* Sum and return the cpu-local counters for index 'idx' */ |
| 259 | static inline uint64_t batadv_sum_counter(struct bat_priv *bat_priv, size_t idx) |
| 260 | { |
| 261 | uint64_t *counters; |
| 262 | int cpu; |
| 263 | int sum = 0; |
| 264 | |
| 265 | for_each_possible_cpu(cpu) { |
| 266 | counters = per_cpu_ptr(bat_priv->bat_counters, cpu); |
| 267 | sum += counters[idx]; |
| 268 | } |
| 269 | |
| 270 | return sum; |
| 271 | } |
| 272 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 273 | #endif /* _NET_BATMAN_ADV_MAIN_H_ */ |