main.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  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. #ifndef _NET_BATMAN_ADV_MAIN_H_
  22. #define _NET_BATMAN_ADV_MAIN_H_
  23. #define DRIVER_AUTHOR "Marek Lindner <lindner_marek@yahoo.de>, " \
  24. "Simon Wunderlich <siwu@hrz.tu-chemnitz.de>"
  25. #define DRIVER_DESC "B.A.T.M.A.N. advanced"
  26. #define DRIVER_DEVICE "batman-adv"
  27. #ifndef SOURCE_VERSION
  28. #define SOURCE_VERSION "2012.3.0"
  29. #endif
  30. /* B.A.T.M.A.N. parameters */
  31. #define TQ_MAX_VALUE 255
  32. #define JITTER 20
  33. /* Time To Live of broadcast messages */
  34. #define TTL 50
  35. /* purge originators after time in seconds if no valid packet comes in
  36. * -> TODO: check influence on TQ_LOCAL_WINDOW_SIZE */
  37. #define PURGE_TIMEOUT 200000 /* 200 seconds */
  38. #define TT_LOCAL_TIMEOUT 3600000 /* in miliseconds */
  39. #define TT_CLIENT_ROAM_TIMEOUT 600000 /* in miliseconds */
  40. /* sliding packet range of received originator messages in sequence numbers
  41. * (should be a multiple of our word size) */
  42. #define TQ_LOCAL_WINDOW_SIZE 64
  43. #define TT_REQUEST_TIMEOUT 3000 /* miliseconds we have to keep
  44. * pending tt_req */
  45. #define TQ_GLOBAL_WINDOW_SIZE 5
  46. #define TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
  47. #define TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
  48. #define TQ_TOTAL_BIDRECT_LIMIT 1
  49. #define TT_OGM_APPEND_MAX 3 /* number of OGMs sent with the last tt diff */
  50. #define ROAMING_MAX_TIME 20000 /* Time in which a client can roam at most
  51. * ROAMING_MAX_COUNT times in miliseconds*/
  52. #define ROAMING_MAX_COUNT 5
  53. #define NO_FLAGS 0
  54. #define NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
  55. #define NUM_WORDS BITS_TO_LONGS(TQ_LOCAL_WINDOW_SIZE)
  56. #define LOG_BUF_LEN 8192 /* has to be a power of 2 */
  57. #define VIS_INTERVAL 5000 /* 5 seconds */
  58. /* how much worse secondary interfaces may be to be considered as bonding
  59. * candidates */
  60. #define BONDING_TQ_THRESHOLD 50
  61. /* should not be bigger than 512 bytes or change the size of
  62. * forw_packet->direct_link_flags */
  63. #define MAX_AGGREGATION_BYTES 512
  64. #define MAX_AGGREGATION_MS 100
  65. #define BLA_PERIOD_LENGTH 10000 /* 10 seconds */
  66. #define BLA_BACKBONE_TIMEOUT (BLA_PERIOD_LENGTH * 3)
  67. #define BLA_CLAIM_TIMEOUT (BLA_PERIOD_LENGTH * 10)
  68. #define DUPLIST_SIZE 16
  69. #define DUPLIST_TIMEOUT 500 /* 500 ms */
  70. /* don't reset again within 30 seconds */
  71. #define RESET_PROTECTION_MS 30000
  72. #define EXPECTED_SEQNO_RANGE 65536
  73. enum mesh_state {
  74. MESH_INACTIVE,
  75. MESH_ACTIVE,
  76. MESH_DEACTIVATING
  77. };
  78. #define BCAST_QUEUE_LEN 256
  79. #define BATMAN_QUEUE_LEN 256
  80. enum uev_action {
  81. UEV_ADD = 0,
  82. UEV_DEL,
  83. UEV_CHANGE
  84. };
  85. enum uev_type {
  86. UEV_GW = 0
  87. };
  88. #define GW_THRESHOLD 50
  89. /* Debug Messages */
  90. #ifdef pr_fmt
  91. #undef pr_fmt
  92. #endif
  93. /* Append 'batman-adv: ' before kernel messages */
  94. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  95. /* all messages related to routing / flooding / broadcasting / etc */
  96. enum dbg_level {
  97. DBG_BATMAN = 1 << 0,
  98. DBG_ROUTES = 1 << 1, /* route added / changed / deleted */
  99. DBG_TT = 1 << 2, /* translation table operations */
  100. DBG_BLA = 1 << 3, /* bridge loop avoidance */
  101. DBG_ALL = 15
  102. };
  103. /* Kernel headers */
  104. #include <linux/mutex.h> /* mutex */
  105. #include <linux/module.h> /* needed by all modules */
  106. #include <linux/netdevice.h> /* netdevice */
  107. #include <linux/etherdevice.h> /* ethernet address classification */
  108. #include <linux/if_ether.h> /* ethernet header */
  109. #include <linux/poll.h> /* poll_table */
  110. #include <linux/kthread.h> /* kernel threads */
  111. #include <linux/pkt_sched.h> /* schedule types */
  112. #include <linux/workqueue.h> /* workqueue */
  113. #include <linux/percpu.h>
  114. #include <linux/slab.h>
  115. #include <net/sock.h> /* struct sock */
  116. #include <linux/jiffies.h>
  117. #include <linux/seq_file.h>
  118. #include "types.h"
  119. extern char bat_routing_algo[];
  120. extern struct list_head hardif_list;
  121. extern unsigned char broadcast_addr[];
  122. extern struct workqueue_struct *bat_event_workqueue;
  123. int mesh_init(struct net_device *soft_iface);
  124. void mesh_free(struct net_device *soft_iface);
  125. void inc_module_count(void);
  126. void dec_module_count(void);
  127. int is_my_mac(const uint8_t *addr);
  128. int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  129. struct packet_type *ptype, struct net_device *orig_dev);
  130. int recv_handler_register(uint8_t packet_type,
  131. int (*recv_handler)(struct sk_buff *,
  132. struct hard_iface *));
  133. void recv_handler_unregister(uint8_t packet_type);
  134. int bat_algo_register(struct bat_algo_ops *bat_algo_ops);
  135. int bat_algo_select(struct bat_priv *bat_priv, char *name);
  136. int bat_algo_seq_print_text(struct seq_file *seq, void *offset);
  137. #ifdef CONFIG_BATMAN_ADV_DEBUG
  138. int debug_log(struct bat_priv *bat_priv, const char *fmt, ...) __printf(2, 3);
  139. #define bat_dbg(type, bat_priv, fmt, arg...) \
  140. do { \
  141. if (atomic_read(&bat_priv->log_level) & type) \
  142. debug_log(bat_priv, fmt, ## arg); \
  143. } \
  144. while (0)
  145. #else /* !CONFIG_BATMAN_ADV_DEBUG */
  146. __printf(3, 4)
  147. static inline void bat_dbg(int type __always_unused,
  148. struct bat_priv *bat_priv __always_unused,
  149. const char *fmt __always_unused, ...)
  150. {
  151. }
  152. #endif
  153. #define bat_info(net_dev, fmt, arg...) \
  154. do { \
  155. struct net_device *_netdev = (net_dev); \
  156. struct bat_priv *_batpriv = netdev_priv(_netdev); \
  157. bat_dbg(DBG_ALL, _batpriv, fmt, ## arg); \
  158. pr_info("%s: " fmt, _netdev->name, ## arg); \
  159. } while (0)
  160. #define bat_err(net_dev, fmt, arg...) \
  161. do { \
  162. struct net_device *_netdev = (net_dev); \
  163. struct bat_priv *_batpriv = netdev_priv(_netdev); \
  164. bat_dbg(DBG_ALL, _batpriv, fmt, ## arg); \
  165. pr_err("%s: " fmt, _netdev->name, ## arg); \
  166. } while (0)
  167. /**
  168. * returns 1 if they are the same ethernet addr
  169. *
  170. * note: can't use compare_ether_addr() as it requires aligned memory
  171. */
  172. static inline int compare_eth(const void *data1, const void *data2)
  173. {
  174. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  175. }
  176. /**
  177. * has_timed_out - compares current time (jiffies) and timestamp + timeout
  178. * @timestamp: base value to compare with (in jiffies)
  179. * @timeout: added to base value before comparing (in milliseconds)
  180. *
  181. * Returns true if current time is after timestamp + timeout
  182. */
  183. static inline bool has_timed_out(unsigned long timestamp, unsigned int timeout)
  184. {
  185. return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout));
  186. }
  187. #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
  188. /* Returns the smallest signed integer in two's complement with the sizeof x */
  189. #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
  190. /* Checks if a sequence number x is a predecessor/successor of y.
  191. * they handle overflows/underflows and can correctly check for a
  192. * predecessor/successor unless the variable sequence number has grown by
  193. * more then 2**(bitwidth(x)-1)-1.
  194. * This means that for a uint8_t with the maximum value 255, it would think:
  195. * - when adding nothing - it is neither a predecessor nor a successor
  196. * - before adding more than 127 to the starting value - it is a predecessor,
  197. * - when adding 128 - it is neither a predecessor nor a successor,
  198. * - after adding more than 127 to the starting value - it is a successor */
  199. #define seq_before(x, y) ({typeof(x) _d1 = (x); \
  200. typeof(y) _d2 = (y); \
  201. typeof(x) _dummy = (_d1 - _d2); \
  202. (void) (&_d1 == &_d2); \
  203. _dummy > smallest_signed_int(_dummy); })
  204. #define seq_after(x, y) seq_before(y, x)
  205. /* Stop preemption on local cpu while incrementing the counter */
  206. static inline void batadv_add_counter(struct bat_priv *bat_priv, size_t idx,
  207. size_t count)
  208. {
  209. int cpu = get_cpu();
  210. per_cpu_ptr(bat_priv->bat_counters, cpu)[idx] += count;
  211. put_cpu();
  212. }
  213. #define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1)
  214. /* Sum and return the cpu-local counters for index 'idx' */
  215. static inline uint64_t batadv_sum_counter(struct bat_priv *bat_priv, size_t idx)
  216. {
  217. uint64_t *counters;
  218. int cpu;
  219. int sum = 0;
  220. for_each_possible_cpu(cpu) {
  221. counters = per_cpu_ptr(bat_priv->bat_counters, cpu);
  222. sum += counters[idx];
  223. }
  224. return sum;
  225. }
  226. #endif /* _NET_BATMAN_ADV_MAIN_H_ */