main.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. bat_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. spin_lock_init(&bat_priv->forw_bat_list_lock);
  77. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  78. spin_lock_init(&bat_priv->tt_changes_list_lock);
  79. spin_lock_init(&bat_priv->tt_req_list_lock);
  80. spin_lock_init(&bat_priv->tt_roam_list_lock);
  81. spin_lock_init(&bat_priv->tt_buff_lock);
  82. spin_lock_init(&bat_priv->gw_list_lock);
  83. spin_lock_init(&bat_priv->vis_hash_lock);
  84. spin_lock_init(&bat_priv->vis_list_lock);
  85. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  86. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  87. INIT_HLIST_HEAD(&bat_priv->gw_list);
  88. INIT_LIST_HEAD(&bat_priv->tt_changes_list);
  89. INIT_LIST_HEAD(&bat_priv->tt_req_list);
  90. INIT_LIST_HEAD(&bat_priv->tt_roam_list);
  91. if (originator_init(bat_priv) < 1)
  92. goto err;
  93. if (tt_init(bat_priv) < 1)
  94. goto err;
  95. tt_local_add(soft_iface, soft_iface->dev_addr, NULL_IFINDEX);
  96. if (vis_init(bat_priv) < 1)
  97. goto err;
  98. if (bla_init(bat_priv) < 1)
  99. goto err;
  100. atomic_set(&bat_priv->gw_reselect, 0);
  101. atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
  102. goto end;
  103. err:
  104. mesh_free(soft_iface);
  105. return -1;
  106. end:
  107. return 0;
  108. }
  109. void mesh_free(struct net_device *soft_iface)
  110. {
  111. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  112. atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
  113. purge_outstanding_packets(bat_priv, NULL);
  114. vis_quit(bat_priv);
  115. gw_node_purge(bat_priv);
  116. originator_free(bat_priv);
  117. tt_free(bat_priv);
  118. bla_free(bat_priv);
  119. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  120. }
  121. void inc_module_count(void)
  122. {
  123. try_module_get(THIS_MODULE);
  124. }
  125. void dec_module_count(void)
  126. {
  127. module_put(THIS_MODULE);
  128. }
  129. int is_my_mac(const uint8_t *addr)
  130. {
  131. const struct hard_iface *hard_iface;
  132. rcu_read_lock();
  133. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  134. if (hard_iface->if_status != IF_ACTIVE)
  135. continue;
  136. if (compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  137. rcu_read_unlock();
  138. return 1;
  139. }
  140. }
  141. rcu_read_unlock();
  142. return 0;
  143. }
  144. static int recv_unhandled_packet(struct sk_buff *skb,
  145. struct hard_iface *recv_if)
  146. {
  147. return NET_RX_DROP;
  148. }
  149. /* incoming packets with the batman ethertype received on any active hard
  150. * interface
  151. */
  152. int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  153. struct packet_type *ptype, struct net_device *orig_dev)
  154. {
  155. struct bat_priv *bat_priv;
  156. struct batman_ogm_packet *batman_ogm_packet;
  157. struct hard_iface *hard_iface;
  158. uint8_t idx;
  159. int ret;
  160. hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
  161. skb = skb_share_check(skb, GFP_ATOMIC);
  162. /* skb was released by skb_share_check() */
  163. if (!skb)
  164. goto err_out;
  165. /* packet should hold at least type and version */
  166. if (unlikely(!pskb_may_pull(skb, 2)))
  167. goto err_free;
  168. /* expect a valid ethernet header here. */
  169. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  170. goto err_free;
  171. if (!hard_iface->soft_iface)
  172. goto err_free;
  173. bat_priv = netdev_priv(hard_iface->soft_iface);
  174. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  175. goto err_free;
  176. /* discard frames on not active interfaces */
  177. if (hard_iface->if_status != IF_ACTIVE)
  178. goto err_free;
  179. batman_ogm_packet = (struct batman_ogm_packet *)skb->data;
  180. if (batman_ogm_packet->header.version != COMPAT_VERSION) {
  181. bat_dbg(DBG_BATMAN, bat_priv,
  182. "Drop packet: incompatible batman version (%i)\n",
  183. batman_ogm_packet->header.version);
  184. goto err_free;
  185. }
  186. /* all receive handlers return whether they received or reused
  187. * the supplied skb. if not, we have to free the skb.
  188. */
  189. idx = batman_ogm_packet->header.packet_type;
  190. ret = (*recv_packet_handler[idx])(skb, hard_iface);
  191. if (ret == NET_RX_DROP)
  192. kfree_skb(skb);
  193. /* return NET_RX_SUCCESS in any case as we
  194. * most probably dropped the packet for
  195. * routing-logical reasons.
  196. */
  197. return NET_RX_SUCCESS;
  198. err_free:
  199. kfree_skb(skb);
  200. err_out:
  201. return NET_RX_DROP;
  202. }
  203. static void recv_handler_init(void)
  204. {
  205. int i;
  206. for (i = 0; i < ARRAY_SIZE(recv_packet_handler); i++)
  207. recv_packet_handler[i] = recv_unhandled_packet;
  208. /* batman icmp packet */
  209. recv_packet_handler[BAT_ICMP] = recv_icmp_packet;
  210. /* unicast packet */
  211. recv_packet_handler[BAT_UNICAST] = recv_unicast_packet;
  212. /* fragmented unicast packet */
  213. recv_packet_handler[BAT_UNICAST_FRAG] = recv_ucast_frag_packet;
  214. /* broadcast packet */
  215. recv_packet_handler[BAT_BCAST] = recv_bcast_packet;
  216. /* vis packet */
  217. recv_packet_handler[BAT_VIS] = recv_vis_packet;
  218. /* Translation table query (request or response) */
  219. recv_packet_handler[BAT_TT_QUERY] = recv_tt_query;
  220. /* Roaming advertisement */
  221. recv_packet_handler[BAT_ROAM_ADV] = recv_roam_adv;
  222. }
  223. int recv_handler_register(uint8_t packet_type,
  224. int (*recv_handler)(struct sk_buff *,
  225. struct hard_iface *))
  226. {
  227. if (recv_packet_handler[packet_type] != &recv_unhandled_packet)
  228. return -EBUSY;
  229. recv_packet_handler[packet_type] = recv_handler;
  230. return 0;
  231. }
  232. void recv_handler_unregister(uint8_t packet_type)
  233. {
  234. recv_packet_handler[packet_type] = recv_unhandled_packet;
  235. }
  236. static struct bat_algo_ops *bat_algo_get(char *name)
  237. {
  238. struct bat_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  239. struct hlist_node *node;
  240. hlist_for_each_entry(bat_algo_ops_tmp, node, &bat_algo_list, list) {
  241. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  242. continue;
  243. bat_algo_ops = bat_algo_ops_tmp;
  244. break;
  245. }
  246. return bat_algo_ops;
  247. }
  248. int bat_algo_register(struct bat_algo_ops *bat_algo_ops)
  249. {
  250. struct bat_algo_ops *bat_algo_ops_tmp;
  251. int ret = -1;
  252. bat_algo_ops_tmp = bat_algo_get(bat_algo_ops->name);
  253. if (bat_algo_ops_tmp) {
  254. pr_info("Trying to register already registered routing algorithm: %s\n",
  255. bat_algo_ops->name);
  256. goto out;
  257. }
  258. /* all algorithms must implement all ops (for now) */
  259. if (!bat_algo_ops->bat_iface_enable ||
  260. !bat_algo_ops->bat_iface_disable ||
  261. !bat_algo_ops->bat_iface_update_mac ||
  262. !bat_algo_ops->bat_primary_iface_set ||
  263. !bat_algo_ops->bat_ogm_schedule ||
  264. !bat_algo_ops->bat_ogm_emit) {
  265. pr_info("Routing algo '%s' does not implement required ops\n",
  266. bat_algo_ops->name);
  267. goto out;
  268. }
  269. INIT_HLIST_NODE(&bat_algo_ops->list);
  270. hlist_add_head(&bat_algo_ops->list, &bat_algo_list);
  271. ret = 0;
  272. out:
  273. return ret;
  274. }
  275. int bat_algo_select(struct bat_priv *bat_priv, char *name)
  276. {
  277. struct bat_algo_ops *bat_algo_ops;
  278. int ret = -1;
  279. bat_algo_ops = bat_algo_get(name);
  280. if (!bat_algo_ops)
  281. goto out;
  282. bat_priv->bat_algo_ops = bat_algo_ops;
  283. ret = 0;
  284. out:
  285. return ret;
  286. }
  287. int bat_algo_seq_print_text(struct seq_file *seq, void *offset)
  288. {
  289. struct bat_algo_ops *bat_algo_ops;
  290. struct hlist_node *node;
  291. seq_printf(seq, "Available routing algorithms:\n");
  292. hlist_for_each_entry(bat_algo_ops, node, &bat_algo_list, list) {
  293. seq_printf(seq, "%s\n", bat_algo_ops->name);
  294. }
  295. return 0;
  296. }
  297. static int param_set_ra(const char *val, const struct kernel_param *kp)
  298. {
  299. struct bat_algo_ops *bat_algo_ops;
  300. bat_algo_ops = bat_algo_get((char *)val);
  301. if (!bat_algo_ops) {
  302. pr_err("Routing algorithm '%s' is not supported\n", val);
  303. return -EINVAL;
  304. }
  305. return param_set_copystring(val, kp);
  306. }
  307. static const struct kernel_param_ops param_ops_ra = {
  308. .set = param_set_ra,
  309. .get = param_get_string,
  310. };
  311. static struct kparam_string __param_string_ra = {
  312. .maxlen = sizeof(bat_routing_algo),
  313. .string = bat_routing_algo,
  314. };
  315. module_param_cb(routing_algo, &param_ops_ra, &__param_string_ra, 0644);
  316. module_init(batman_init);
  317. module_exit(batman_exit);
  318. MODULE_LICENSE("GPL");
  319. MODULE_AUTHOR(DRIVER_AUTHOR);
  320. MODULE_DESCRIPTION(DRIVER_DESC);
  321. MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
  322. MODULE_VERSION(SOURCE_VERSION);