main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include "main.h"
  22. #include "bat_sysfs.h"
  23. #include "bat_debugfs.h"
  24. #include "routing.h"
  25. #include "send.h"
  26. #include "originator.h"
  27. #include "soft-interface.h"
  28. #include "icmp_socket.h"
  29. #include "translation-table.h"
  30. #include "hard-interface.h"
  31. #include "gateway_client.h"
  32. #include "bridge_loop_avoidance.h"
  33. #include "vis.h"
  34. #include "hash.h"
  35. #include "bat_algo.h"
  36. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  37. * list traversals just rcu-locked */
  38. struct list_head hardif_list;
  39. static int (*recv_packet_handler[256])(struct sk_buff *, struct hard_iface *);
  40. char bat_routing_algo[20] = "BATMAN_IV";
  41. static struct hlist_head bat_algo_list;
  42. unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  43. struct workqueue_struct *bat_event_workqueue;
  44. static void recv_handler_init(void);
  45. static int __init batman_init(void)
  46. {
  47. INIT_LIST_HEAD(&hardif_list);
  48. INIT_HLIST_HEAD(&bat_algo_list);
  49. recv_handler_init();
  50. batadv_iv_init();
  51. /* the name should not be longer than 10 chars - see
  52. * http://lwn.net/Articles/23634/ */
  53. bat_event_workqueue = create_singlethread_workqueue("bat_events");
  54. if (!bat_event_workqueue)
  55. return -ENOMEM;
  56. bat_socket_init();
  57. debugfs_init();
  58. register_netdevice_notifier(&hard_if_notifier);
  59. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  60. SOURCE_VERSION, COMPAT_VERSION);
  61. return 0;
  62. }
  63. static void __exit batman_exit(void)
  64. {
  65. debugfs_destroy();
  66. unregister_netdevice_notifier(&hard_if_notifier);
  67. hardif_remove_interfaces();
  68. flush_workqueue(bat_event_workqueue);
  69. destroy_workqueue(bat_event_workqueue);
  70. bat_event_workqueue = NULL;
  71. rcu_barrier();
  72. }
  73. int mesh_init(struct net_device *soft_iface)
  74. {
  75. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  76. int ret;
  77. spin_lock_init(&bat_priv->forw_bat_list_lock);
  78. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  79. spin_lock_init(&bat_priv->tt_changes_list_lock);
  80. spin_lock_init(&bat_priv->tt_req_list_lock);
  81. spin_lock_init(&bat_priv->tt_roam_list_lock);
  82. spin_lock_init(&bat_priv->tt_buff_lock);
  83. spin_lock_init(&bat_priv->gw_list_lock);
  84. spin_lock_init(&bat_priv->vis_hash_lock);
  85. spin_lock_init(&bat_priv->vis_list_lock);
  86. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  87. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  88. INIT_HLIST_HEAD(&bat_priv->gw_list);
  89. INIT_LIST_HEAD(&bat_priv->tt_changes_list);
  90. INIT_LIST_HEAD(&bat_priv->tt_req_list);
  91. INIT_LIST_HEAD(&bat_priv->tt_roam_list);
  92. ret = originator_init(bat_priv);
  93. if (ret < 0)
  94. goto err;
  95. ret = tt_init(bat_priv);
  96. if (ret < 0)
  97. goto err;
  98. tt_local_add(soft_iface, soft_iface->dev_addr, NULL_IFINDEX);
  99. ret = vis_init(bat_priv);
  100. if (ret < 0)
  101. goto err;
  102. ret = bla_init(bat_priv);
  103. if (ret < 0)
  104. goto err;
  105. atomic_set(&bat_priv->gw_reselect, 0);
  106. atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
  107. return 0;
  108. err:
  109. mesh_free(soft_iface);
  110. return ret;
  111. }
  112. void mesh_free(struct net_device *soft_iface)
  113. {
  114. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  115. atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
  116. purge_outstanding_packets(bat_priv, NULL);
  117. vis_quit(bat_priv);
  118. gw_node_purge(bat_priv);
  119. originator_free(bat_priv);
  120. tt_free(bat_priv);
  121. bla_free(bat_priv);
  122. free_percpu(bat_priv->bat_counters);
  123. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  124. }
  125. void inc_module_count(void)
  126. {
  127. try_module_get(THIS_MODULE);
  128. }
  129. void dec_module_count(void)
  130. {
  131. module_put(THIS_MODULE);
  132. }
  133. int is_my_mac(const uint8_t *addr)
  134. {
  135. const struct hard_iface *hard_iface;
  136. rcu_read_lock();
  137. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  138. if (hard_iface->if_status != IF_ACTIVE)
  139. continue;
  140. if (compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  141. rcu_read_unlock();
  142. return 1;
  143. }
  144. }
  145. rcu_read_unlock();
  146. return 0;
  147. }
  148. static int recv_unhandled_packet(struct sk_buff *skb,
  149. struct hard_iface *recv_if)
  150. {
  151. return NET_RX_DROP;
  152. }
  153. /* incoming packets with the batman ethertype received on any active hard
  154. * interface
  155. */
  156. int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  157. struct packet_type *ptype, struct net_device *orig_dev)
  158. {
  159. struct bat_priv *bat_priv;
  160. struct batman_ogm_packet *batman_ogm_packet;
  161. struct hard_iface *hard_iface;
  162. uint8_t idx;
  163. int ret;
  164. hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
  165. skb = skb_share_check(skb, GFP_ATOMIC);
  166. /* skb was released by skb_share_check() */
  167. if (!skb)
  168. goto err_out;
  169. /* packet should hold at least type and version */
  170. if (unlikely(!pskb_may_pull(skb, 2)))
  171. goto err_free;
  172. /* expect a valid ethernet header here. */
  173. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  174. goto err_free;
  175. if (!hard_iface->soft_iface)
  176. goto err_free;
  177. bat_priv = netdev_priv(hard_iface->soft_iface);
  178. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  179. goto err_free;
  180. /* discard frames on not active interfaces */
  181. if (hard_iface->if_status != IF_ACTIVE)
  182. goto err_free;
  183. batman_ogm_packet = (struct batman_ogm_packet *)skb->data;
  184. if (batman_ogm_packet->header.version != COMPAT_VERSION) {
  185. bat_dbg(DBG_BATMAN, bat_priv,
  186. "Drop packet: incompatible batman version (%i)\n",
  187. batman_ogm_packet->header.version);
  188. goto err_free;
  189. }
  190. /* all receive handlers return whether they received or reused
  191. * the supplied skb. if not, we have to free the skb.
  192. */
  193. idx = batman_ogm_packet->header.packet_type;
  194. ret = (*recv_packet_handler[idx])(skb, hard_iface);
  195. if (ret == NET_RX_DROP)
  196. kfree_skb(skb);
  197. /* return NET_RX_SUCCESS in any case as we
  198. * most probably dropped the packet for
  199. * routing-logical reasons.
  200. */
  201. return NET_RX_SUCCESS;
  202. err_free:
  203. kfree_skb(skb);
  204. err_out:
  205. return NET_RX_DROP;
  206. }
  207. static void recv_handler_init(void)
  208. {
  209. int i;
  210. for (i = 0; i < ARRAY_SIZE(recv_packet_handler); i++)
  211. recv_packet_handler[i] = recv_unhandled_packet;
  212. /* batman icmp packet */
  213. recv_packet_handler[BAT_ICMP] = recv_icmp_packet;
  214. /* unicast packet */
  215. recv_packet_handler[BAT_UNICAST] = recv_unicast_packet;
  216. /* fragmented unicast packet */
  217. recv_packet_handler[BAT_UNICAST_FRAG] = recv_ucast_frag_packet;
  218. /* broadcast packet */
  219. recv_packet_handler[BAT_BCAST] = recv_bcast_packet;
  220. /* vis packet */
  221. recv_packet_handler[BAT_VIS] = recv_vis_packet;
  222. /* Translation table query (request or response) */
  223. recv_packet_handler[BAT_TT_QUERY] = recv_tt_query;
  224. /* Roaming advertisement */
  225. recv_packet_handler[BAT_ROAM_ADV] = recv_roam_adv;
  226. }
  227. int recv_handler_register(uint8_t packet_type,
  228. int (*recv_handler)(struct sk_buff *,
  229. struct hard_iface *))
  230. {
  231. if (recv_packet_handler[packet_type] != &recv_unhandled_packet)
  232. return -EBUSY;
  233. recv_packet_handler[packet_type] = recv_handler;
  234. return 0;
  235. }
  236. void recv_handler_unregister(uint8_t packet_type)
  237. {
  238. recv_packet_handler[packet_type] = recv_unhandled_packet;
  239. }
  240. static struct bat_algo_ops *bat_algo_get(char *name)
  241. {
  242. struct bat_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  243. struct hlist_node *node;
  244. hlist_for_each_entry(bat_algo_ops_tmp, node, &bat_algo_list, list) {
  245. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  246. continue;
  247. bat_algo_ops = bat_algo_ops_tmp;
  248. break;
  249. }
  250. return bat_algo_ops;
  251. }
  252. int bat_algo_register(struct bat_algo_ops *bat_algo_ops)
  253. {
  254. struct bat_algo_ops *bat_algo_ops_tmp;
  255. int ret;
  256. bat_algo_ops_tmp = bat_algo_get(bat_algo_ops->name);
  257. if (bat_algo_ops_tmp) {
  258. pr_info("Trying to register already registered routing algorithm: %s\n",
  259. bat_algo_ops->name);
  260. ret = -EEXIST;
  261. goto out;
  262. }
  263. /* all algorithms must implement all ops (for now) */
  264. if (!bat_algo_ops->bat_iface_enable ||
  265. !bat_algo_ops->bat_iface_disable ||
  266. !bat_algo_ops->bat_iface_update_mac ||
  267. !bat_algo_ops->bat_primary_iface_set ||
  268. !bat_algo_ops->bat_ogm_schedule ||
  269. !bat_algo_ops->bat_ogm_emit) {
  270. pr_info("Routing algo '%s' does not implement required ops\n",
  271. bat_algo_ops->name);
  272. ret = -EINVAL;
  273. goto out;
  274. }
  275. INIT_HLIST_NODE(&bat_algo_ops->list);
  276. hlist_add_head(&bat_algo_ops->list, &bat_algo_list);
  277. ret = 0;
  278. out:
  279. return ret;
  280. }
  281. int bat_algo_select(struct bat_priv *bat_priv, char *name)
  282. {
  283. struct bat_algo_ops *bat_algo_ops;
  284. int ret = -EINVAL;
  285. bat_algo_ops = bat_algo_get(name);
  286. if (!bat_algo_ops)
  287. goto out;
  288. bat_priv->bat_algo_ops = bat_algo_ops;
  289. ret = 0;
  290. out:
  291. return ret;
  292. }
  293. int bat_algo_seq_print_text(struct seq_file *seq, void *offset)
  294. {
  295. struct bat_algo_ops *bat_algo_ops;
  296. struct hlist_node *node;
  297. seq_printf(seq, "Available routing algorithms:\n");
  298. hlist_for_each_entry(bat_algo_ops, node, &bat_algo_list, list) {
  299. seq_printf(seq, "%s\n", bat_algo_ops->name);
  300. }
  301. return 0;
  302. }
  303. static int param_set_ra(const char *val, const struct kernel_param *kp)
  304. {
  305. struct bat_algo_ops *bat_algo_ops;
  306. char *algo_name = (char *)val;
  307. size_t name_len = strlen(algo_name);
  308. if (algo_name[name_len - 1] == '\n')
  309. algo_name[name_len - 1] = '\0';
  310. bat_algo_ops = bat_algo_get(algo_name);
  311. if (!bat_algo_ops) {
  312. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  313. return -EINVAL;
  314. }
  315. return param_set_copystring(algo_name, kp);
  316. }
  317. static const struct kernel_param_ops param_ops_ra = {
  318. .set = param_set_ra,
  319. .get = param_get_string,
  320. };
  321. static struct kparam_string __param_string_ra = {
  322. .maxlen = sizeof(bat_routing_algo),
  323. .string = bat_routing_algo,
  324. };
  325. module_param_cb(routing_algo, &param_ops_ra, &__param_string_ra, 0644);
  326. module_init(batman_init);
  327. module_exit(batman_exit);
  328. MODULE_LICENSE("GPL");
  329. MODULE_AUTHOR(DRIVER_AUTHOR);
  330. MODULE_DESCRIPTION(DRIVER_DESC);
  331. MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
  332. MODULE_VERSION(SOURCE_VERSION);