19cfc7bd6SSven Eckelmann /* Copyright (C) 2007-2012 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 15c6c8fea2SSven Eckelmann * along with this program; if not, write to the Free Software 16c6c8fea2SSven Eckelmann * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17c6c8fea2SSven Eckelmann * 02110-1301, USA 18c6c8fea2SSven Eckelmann */ 19c6c8fea2SSven Eckelmann 20c6c8fea2SSven Eckelmann #ifndef _NET_BATMAN_ADV_MAIN_H_ 21c6c8fea2SSven Eckelmann #define _NET_BATMAN_ADV_MAIN_H_ 22c6c8fea2SSven Eckelmann 23*42d0b044SSven Eckelmann #define BATADV_DRIVER_AUTHOR "Marek Lindner <lindner_marek@yahoo.de>, " \ 24c6c8fea2SSven Eckelmann "Simon Wunderlich <siwu@hrz.tu-chemnitz.de>" 25*42d0b044SSven Eckelmann #define BATADV_DRIVER_DESC "B.A.T.M.A.N. advanced" 26*42d0b044SSven Eckelmann #define BATADV_DRIVER_DEVICE "batman-adv" 27c6c8fea2SSven Eckelmann 28*42d0b044SSven Eckelmann #ifndef BATADV_SOURCE_VERSION 29*42d0b044SSven Eckelmann #define BATADV_SOURCE_VERSION "2012.3.0" 3044c4349aSSven Eckelmann #endif 31c6c8fea2SSven Eckelmann 32c6c8fea2SSven Eckelmann /* B.A.T.M.A.N. parameters */ 33c6c8fea2SSven Eckelmann 34*42d0b044SSven Eckelmann #define BATADV_TQ_MAX_VALUE 255 35*42d0b044SSven Eckelmann #define BATADV_JITTER 20 36c6c8fea2SSven Eckelmann 376e215fd8SSven Eckelmann /* Time To Live of broadcast messages */ 38*42d0b044SSven Eckelmann #define BATADV_TTL 50 396e215fd8SSven Eckelmann 406e215fd8SSven Eckelmann /* purge originators after time in seconds if no valid packet comes in 41*42d0b044SSven Eckelmann * -> TODO: check influence on BATADV_TQ_LOCAL_WINDOW_SIZE 429cfc7bd6SSven Eckelmann */ 43*42d0b044SSven Eckelmann #define BATADV_PURGE_TIMEOUT 200000 /* 200 seconds */ 44*42d0b044SSven Eckelmann #define BATADV_TT_LOCAL_TIMEOUT 3600000 /* in miliseconds */ 45*42d0b044SSven Eckelmann #define BATADV_TT_CLIENT_ROAM_TIMEOUT 600000 /* in miliseconds */ 46015758d0SAntonio Quartulli /* sliding packet range of received originator messages in sequence numbers 479cfc7bd6SSven Eckelmann * (should be a multiple of our word size) 489cfc7bd6SSven Eckelmann */ 49*42d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_WINDOW_SIZE 64 509cfc7bd6SSven Eckelmann /* miliseconds we have to keep pending tt_req */ 51*42d0b044SSven Eckelmann #define BATADV_TT_REQUEST_TIMEOUT 3000 52a73105b8SAntonio Quartulli 53*42d0b044SSven Eckelmann #define BATADV_TQ_GLOBAL_WINDOW_SIZE 5 54*42d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM 1 55*42d0b044SSven Eckelmann #define BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM 1 56*42d0b044SSven Eckelmann #define BATADV_TQ_TOTAL_BIDRECT_LIMIT 1 57c6c8fea2SSven Eckelmann 58*42d0b044SSven Eckelmann /* number of OGMs sent with the last tt diff */ 59*42d0b044SSven Eckelmann #define BATADV_TT_OGM_APPEND_MAX 3 60a73105b8SAntonio Quartulli 619cfc7bd6SSven Eckelmann /* Time in which a client can roam at most ROAMING_MAX_COUNT times in 629cfc7bd6SSven Eckelmann * miliseconds 639cfc7bd6SSven Eckelmann */ 64*42d0b044SSven Eckelmann #define BATADV_ROAMING_MAX_TIME 20000 65*42d0b044SSven Eckelmann #define BATADV_ROAMING_MAX_COUNT 5 66cc47f66eSAntonio Quartulli 67*42d0b044SSven Eckelmann #define BATADV_NO_FLAGS 0 68ecbd5321SMarek Lindner 69*42d0b044SSven Eckelmann #define BATADV_NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */ 70bc279080SAntonio Quartulli 71*42d0b044SSven Eckelmann #define BATADV_NUM_WORDS BITS_TO_LONGS(BATADV_TQ_LOCAL_WINDOW_SIZE) 72c6c8fea2SSven Eckelmann 73*42d0b044SSven Eckelmann #define BATADV_LOG_BUF_LEN 8192 /* has to be a power of 2 */ 74c6c8fea2SSven Eckelmann 75*42d0b044SSven Eckelmann #define BATADV_VIS_INTERVAL 5000 /* 5 seconds */ 76c6c8fea2SSven Eckelmann 776e215fd8SSven Eckelmann /* how much worse secondary interfaces may be to be considered as bonding 789cfc7bd6SSven Eckelmann * candidates 799cfc7bd6SSven Eckelmann */ 80*42d0b044SSven Eckelmann #define BATADV_BONDING_TQ_THRESHOLD 50 81c6c8fea2SSven Eckelmann 826e215fd8SSven Eckelmann /* should not be bigger than 512 bytes or change the size of 839cfc7bd6SSven Eckelmann * forw_packet->direct_link_flags 849cfc7bd6SSven Eckelmann */ 85*42d0b044SSven Eckelmann #define BATADV_MAX_AGGREGATION_BYTES 512 86*42d0b044SSven Eckelmann #define BATADV_MAX_AGGREGATION_MS 100 87c6c8fea2SSven Eckelmann 88*42d0b044SSven Eckelmann #define BATADV_BLA_PERIOD_LENGTH 10000 /* 10 seconds */ 89*42d0b044SSven Eckelmann #define BATADV_BLA_BACKBONE_TIMEOUT (BATADV_BLA_PERIOD_LENGTH * 3) 90*42d0b044SSven Eckelmann #define BATADV_BLA_CLAIM_TIMEOUT (BATADV_BLA_PERIOD_LENGTH * 10) 91fe2da6ffSSimon Wunderlich 92*42d0b044SSven Eckelmann #define BATADV_DUPLIST_SIZE 16 93*42d0b044SSven Eckelmann #define BATADV_DUPLIST_TIMEOUT 500 /* 500 ms */ 946e215fd8SSven Eckelmann /* don't reset again within 30 seconds */ 95*42d0b044SSven Eckelmann #define BATADV_RESET_PROTECTION_MS 30000 96*42d0b044SSven Eckelmann #define BATADV_EXPECTED_SEQNO_RANGE 65536 97c6c8fea2SSven Eckelmann 98e8958dbfSSven Eckelmann enum mesh_state { 99e8958dbfSSven Eckelmann MESH_INACTIVE, 100e8958dbfSSven Eckelmann MESH_ACTIVE, 101e8958dbfSSven Eckelmann MESH_DEACTIVATING 102e8958dbfSSven Eckelmann }; 103c6c8fea2SSven Eckelmann 104*42d0b044SSven Eckelmann #define BATADV_BCAST_QUEUE_LEN 256 105*42d0b044SSven Eckelmann #define BATADV_BATMAN_QUEUE_LEN 256 106c6c8fea2SSven Eckelmann 107c6bda689SAntonio Quartulli enum uev_action { 108c6bda689SAntonio Quartulli UEV_ADD = 0, 109c6bda689SAntonio Quartulli UEV_DEL, 110c6bda689SAntonio Quartulli UEV_CHANGE 111c6bda689SAntonio Quartulli }; 112c6bda689SAntonio Quartulli 113c6bda689SAntonio Quartulli enum uev_type { 114c6bda689SAntonio Quartulli UEV_GW = 0 115c6bda689SAntonio Quartulli }; 116c6bda689SAntonio Quartulli 117*42d0b044SSven Eckelmann #define BATADV_GW_THRESHOLD 50 11843676ab5SAntonio Quartulli 11921a1236bSSven Eckelmann /* Debug Messages */ 120c6c8fea2SSven Eckelmann #ifdef pr_fmt 121c6c8fea2SSven Eckelmann #undef pr_fmt 122c6c8fea2SSven Eckelmann #endif 1236e215fd8SSven Eckelmann /* Append 'batman-adv: ' before kernel messages */ 1246e215fd8SSven Eckelmann #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 125c6c8fea2SSven Eckelmann 1266e215fd8SSven Eckelmann /* all messages related to routing / flooding / broadcasting / etc */ 127e8958dbfSSven Eckelmann enum dbg_level { 128e8958dbfSSven Eckelmann DBG_BATMAN = 1 << 0, 129e8958dbfSSven Eckelmann DBG_ROUTES = 1 << 1, /* route added / changed / deleted */ 130a73105b8SAntonio Quartulli DBG_TT = 1 << 2, /* translation table operations */ 13123721387SSimon Wunderlich DBG_BLA = 1 << 3, /* bridge loop avoidance */ 13223721387SSimon Wunderlich DBG_ALL = 15 133e8958dbfSSven Eckelmann }; 134c6c8fea2SSven Eckelmann 13521a1236bSSven Eckelmann /* Kernel headers */ 136c6c8fea2SSven Eckelmann 137c6c8fea2SSven Eckelmann #include <linux/mutex.h> /* mutex */ 138c6c8fea2SSven Eckelmann #include <linux/module.h> /* needed by all modules */ 139c6c8fea2SSven Eckelmann #include <linux/netdevice.h> /* netdevice */ 140015758d0SAntonio Quartulli #include <linux/etherdevice.h> /* ethernet address classification */ 141c6c8fea2SSven Eckelmann #include <linux/if_ether.h> /* ethernet header */ 142c6c8fea2SSven Eckelmann #include <linux/poll.h> /* poll_table */ 143c6c8fea2SSven Eckelmann #include <linux/kthread.h> /* kernel threads */ 144c6c8fea2SSven Eckelmann #include <linux/pkt_sched.h> /* schedule types */ 145c6c8fea2SSven Eckelmann #include <linux/workqueue.h> /* workqueue */ 146f8214865SMartin Hundebøll #include <linux/percpu.h> 147c6c8fea2SSven Eckelmann #include <linux/slab.h> 148c6c8fea2SSven Eckelmann #include <net/sock.h> /* struct sock */ 149c6c8fea2SSven Eckelmann #include <linux/jiffies.h> 150c6c8fea2SSven Eckelmann #include <linux/seq_file.h> 151c6c8fea2SSven Eckelmann #include "types.h" 152c6c8fea2SSven Eckelmann 1533193e8fdSSven Eckelmann extern char batadv_routing_algo[]; 1543193e8fdSSven Eckelmann extern struct list_head batadv_hardif_list; 155c6c8fea2SSven Eckelmann 1563193e8fdSSven Eckelmann extern unsigned char batadv_broadcast_addr[]; 1573193e8fdSSven Eckelmann extern struct workqueue_struct *batadv_event_workqueue; 158c6c8fea2SSven Eckelmann 1593193e8fdSSven Eckelmann int batadv_mesh_init(struct net_device *soft_iface); 1603193e8fdSSven Eckelmann void batadv_mesh_free(struct net_device *soft_iface); 1613193e8fdSSven Eckelmann void batadv_inc_module_count(void); 1623193e8fdSSven Eckelmann void batadv_dec_module_count(void); 1633193e8fdSSven Eckelmann int batadv_is_my_mac(const uint8_t *addr); 1643193e8fdSSven Eckelmann int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev, 1653193e8fdSSven Eckelmann struct packet_type *ptype, 1663193e8fdSSven Eckelmann struct net_device *orig_dev); 1673193e8fdSSven Eckelmann int batadv_recv_handler_register(uint8_t packet_type, 168ffa995e0SMarek Lindner int (*recv_handler)(struct sk_buff *, 169ffa995e0SMarek Lindner struct hard_iface *)); 1703193e8fdSSven Eckelmann void batadv_recv_handler_unregister(uint8_t packet_type); 1713193e8fdSSven Eckelmann int batadv_algo_register(struct bat_algo_ops *bat_algo_ops); 1723193e8fdSSven Eckelmann int batadv_algo_select(struct bat_priv *bat_priv, char *name); 1733193e8fdSSven Eckelmann int batadv_algo_seq_print_text(struct seq_file *seq, void *offset); 174c6c8fea2SSven Eckelmann 175c6c8fea2SSven Eckelmann #ifdef CONFIG_BATMAN_ADV_DEBUG 17640a072d7SSven Eckelmann int batadv_debug_log(struct bat_priv *bat_priv, const char *fmt, ...) 17740a072d7SSven Eckelmann __printf(2, 3); 178c6c8fea2SSven Eckelmann 1791eda58bfSSven Eckelmann #define batadv_dbg(type, bat_priv, fmt, arg...) \ 180c6c8fea2SSven Eckelmann do { \ 181c6c8fea2SSven Eckelmann if (atomic_read(&bat_priv->log_level) & type) \ 18240a072d7SSven Eckelmann batadv_debug_log(bat_priv, fmt, ## arg);\ 183c6c8fea2SSven Eckelmann } \ 184c6c8fea2SSven Eckelmann while (0) 185c6c8fea2SSven Eckelmann #else /* !CONFIG_BATMAN_ADV_DEBUG */ 186d3a547beSSven Eckelmann __printf(3, 4) 1871eda58bfSSven Eckelmann static inline void batadv_dbg(int type __always_unused, 188aa0adb1aSSven Eckelmann struct bat_priv *bat_priv __always_unused, 189747e4221SSven Eckelmann const char *fmt __always_unused, ...) 190c6c8fea2SSven Eckelmann { 191c6c8fea2SSven Eckelmann } 192c6c8fea2SSven Eckelmann #endif 193c6c8fea2SSven Eckelmann 1943e34819eSSven Eckelmann #define batadv_info(net_dev, fmt, arg...) \ 195c6c8fea2SSven Eckelmann do { \ 196c6c8fea2SSven Eckelmann struct net_device *_netdev = (net_dev); \ 197c6c8fea2SSven Eckelmann struct bat_priv *_batpriv = netdev_priv(_netdev); \ 1981eda58bfSSven Eckelmann batadv_dbg(DBG_ALL, _batpriv, fmt, ## arg); \ 199c6c8fea2SSven Eckelmann pr_info("%s: " fmt, _netdev->name, ## arg); \ 200c6c8fea2SSven Eckelmann } while (0) 2013e34819eSSven Eckelmann #define batadv_err(net_dev, fmt, arg...) \ 202c6c8fea2SSven Eckelmann do { \ 203c6c8fea2SSven Eckelmann struct net_device *_netdev = (net_dev); \ 204c6c8fea2SSven Eckelmann struct bat_priv *_batpriv = netdev_priv(_netdev); \ 2051eda58bfSSven Eckelmann batadv_dbg(DBG_ALL, _batpriv, fmt, ## arg); \ 206c6c8fea2SSven Eckelmann pr_err("%s: " fmt, _netdev->name, ## arg); \ 207c6c8fea2SSven Eckelmann } while (0) 208c6c8fea2SSven Eckelmann 2099cfc7bd6SSven Eckelmann /* returns 1 if they are the same ethernet addr 21039901e71SMarek Lindner * 21139901e71SMarek Lindner * note: can't use compare_ether_addr() as it requires aligned memory 21239901e71SMarek Lindner */ 2131eda58bfSSven Eckelmann static inline int batadv_compare_eth(const void *data1, const void *data2) 21439901e71SMarek Lindner { 21539901e71SMarek Lindner return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); 21639901e71SMarek Lindner } 21739901e71SMarek Lindner 2189cfc7bd6SSven Eckelmann /* has_timed_out - compares current time (jiffies) and timestamp + timeout 219a04ccd59SMartin Hundebøll * @timestamp: base value to compare with (in jiffies) 220a04ccd59SMartin Hundebøll * @timeout: added to base value before comparing (in milliseconds) 221a04ccd59SMartin Hundebøll * 222a04ccd59SMartin Hundebøll * Returns true if current time is after timestamp + timeout 223a04ccd59SMartin Hundebøll */ 2241eda58bfSSven Eckelmann static inline bool batadv_has_timed_out(unsigned long timestamp, 2251eda58bfSSven Eckelmann unsigned int timeout) 226a04ccd59SMartin Hundebøll { 227a04ccd59SMartin Hundebøll return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout)); 228a04ccd59SMartin Hundebøll } 229747e4221SSven Eckelmann 2303e34819eSSven Eckelmann #define batadv_atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0) 2315f657ec0SAntonio Quartulli 232f5d33d37SAntonio Quartulli /* Returns the smallest signed integer in two's complement with the sizeof x */ 2333e34819eSSven Eckelmann #define batadv_smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u))) 234f5d33d37SAntonio Quartulli 235f5d33d37SAntonio Quartulli /* Checks if a sequence number x is a predecessor/successor of y. 236f5d33d37SAntonio Quartulli * they handle overflows/underflows and can correctly check for a 237f5d33d37SAntonio Quartulli * predecessor/successor unless the variable sequence number has grown by 238f5d33d37SAntonio Quartulli * more then 2**(bitwidth(x)-1)-1. 239f5d33d37SAntonio Quartulli * This means that for a uint8_t with the maximum value 255, it would think: 240f5d33d37SAntonio Quartulli * - when adding nothing - it is neither a predecessor nor a successor 241f5d33d37SAntonio Quartulli * - before adding more than 127 to the starting value - it is a predecessor, 242f5d33d37SAntonio Quartulli * - when adding 128 - it is neither a predecessor nor a successor, 2439cfc7bd6SSven Eckelmann * - after adding more than 127 to the starting value - it is a successor 2449cfc7bd6SSven Eckelmann */ 2453e34819eSSven Eckelmann #define batadv_seq_before(x, y) ({typeof(x) _d1 = (x); \ 2460bb85751SSven Eckelmann typeof(y) _d2 = (y); \ 2470bb85751SSven Eckelmann typeof(x) _dummy = (_d1 - _d2); \ 2480bb85751SSven Eckelmann (void) (&_d1 == &_d2); \ 2493e34819eSSven Eckelmann _dummy > batadv_smallest_signed_int(_dummy); }) 2503e34819eSSven Eckelmann #define batadv_seq_after(x, y) batadv_seq_before(y, x) 251f5d33d37SAntonio Quartulli 252f8214865SMartin Hundebøll /* Stop preemption on local cpu while incrementing the counter */ 253f8214865SMartin Hundebøll static inline void batadv_add_counter(struct bat_priv *bat_priv, size_t idx, 254f8214865SMartin Hundebøll size_t count) 255f8214865SMartin Hundebøll { 256f8214865SMartin Hundebøll int cpu = get_cpu(); 257f8214865SMartin Hundebøll per_cpu_ptr(bat_priv->bat_counters, cpu)[idx] += count; 258f8214865SMartin Hundebøll put_cpu(); 259f8214865SMartin Hundebøll } 260f8214865SMartin Hundebøll 261f8214865SMartin Hundebøll #define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1) 262f8214865SMartin Hundebøll 263f8214865SMartin Hundebøll /* Sum and return the cpu-local counters for index 'idx' */ 264f8214865SMartin Hundebøll static inline uint64_t batadv_sum_counter(struct bat_priv *bat_priv, size_t idx) 265f8214865SMartin Hundebøll { 266f8214865SMartin Hundebøll uint64_t *counters; 267f8214865SMartin Hundebøll int cpu; 268f8214865SMartin Hundebøll int sum = 0; 269f8214865SMartin Hundebøll 270f8214865SMartin Hundebøll for_each_possible_cpu(cpu) { 271f8214865SMartin Hundebøll counters = per_cpu_ptr(bat_priv->bat_counters, cpu); 272f8214865SMartin Hundebøll sum += counters[idx]; 273f8214865SMartin Hundebøll } 274f8214865SMartin Hundebøll 275f8214865SMartin Hundebøll return sum; 276f8214865SMartin Hundebøll } 277f8214865SMartin Hundebøll 278c6c8fea2SSven Eckelmann #endif /* _NET_BATMAN_ADV_MAIN_H_ */ 279