main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "bat_sysfs.h"
  21. #include "bat_debugfs.h"
  22. #include "routing.h"
  23. #include "send.h"
  24. #include "originator.h"
  25. #include "soft-interface.h"
  26. #include "icmp_socket.h"
  27. #include "translation-table.h"
  28. #include "hard-interface.h"
  29. #include "gateway_client.h"
  30. #include "bridge_loop_avoidance.h"
  31. #include "vis.h"
  32. #include "hash.h"
  33. #include "bat_algo.h"
  34. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  35. * list traversals just rcu-locked
  36. */
  37. struct list_head batadv_hardif_list;
  38. static int (*batadv_rx_handler[256])(struct sk_buff *,
  39. struct hard_iface *);
  40. char batadv_routing_algo[20] = "BATMAN_IV";
  41. static struct hlist_head batadv_algo_list;
  42. unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  43. struct workqueue_struct *batadv_event_workqueue;
  44. static void batadv_recv_handler_init(void);
  45. static int __init batadv_init(void)
  46. {
  47. INIT_LIST_HEAD(&batadv_hardif_list);
  48. INIT_HLIST_HEAD(&batadv_algo_list);
  49. batadv_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. */
  54. batadv_event_workqueue = create_singlethread_workqueue("bat_events");
  55. if (!batadv_event_workqueue)
  56. return -ENOMEM;
  57. batadv_socket_init();
  58. batadv_debugfs_init();
  59. register_netdevice_notifier(&batadv_hard_if_notifier);
  60. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  61. BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
  62. return 0;
  63. }
  64. static void __exit batadv_exit(void)
  65. {
  66. batadv_debugfs_destroy();
  67. unregister_netdevice_notifier(&batadv_hard_if_notifier);
  68. batadv_hardif_remove_interfaces();
  69. flush_workqueue(batadv_event_workqueue);
  70. destroy_workqueue(batadv_event_workqueue);
  71. batadv_event_workqueue = NULL;
  72. rcu_barrier();
  73. }
  74. int batadv_mesh_init(struct net_device *soft_iface)
  75. {
  76. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  77. int ret;
  78. spin_lock_init(&bat_priv->forw_bat_list_lock);
  79. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  80. spin_lock_init(&bat_priv->tt_changes_list_lock);
  81. spin_lock_init(&bat_priv->tt_req_list_lock);
  82. spin_lock_init(&bat_priv->tt_roam_list_lock);
  83. spin_lock_init(&bat_priv->tt_buff_lock);
  84. spin_lock_init(&bat_priv->gw_list_lock);
  85. spin_lock_init(&bat_priv->vis_hash_lock);
  86. spin_lock_init(&bat_priv->vis_list_lock);
  87. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  88. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  89. INIT_HLIST_HEAD(&bat_priv->gw_list);
  90. INIT_LIST_HEAD(&bat_priv->tt_changes_list);
  91. INIT_LIST_HEAD(&bat_priv->tt_req_list);
  92. INIT_LIST_HEAD(&bat_priv->tt_roam_list);
  93. ret = batadv_originator_init(bat_priv);
  94. if (ret < 0)
  95. goto err;
  96. ret = batadv_tt_init(bat_priv);
  97. if (ret < 0)
  98. goto err;
  99. batadv_tt_local_add(soft_iface, soft_iface->dev_addr,
  100. BATADV_NULL_IFINDEX);
  101. ret = batadv_vis_init(bat_priv);
  102. if (ret < 0)
  103. goto err;
  104. ret = batadv_bla_init(bat_priv);
  105. if (ret < 0)
  106. goto err;
  107. atomic_set(&bat_priv->gw_reselect, 0);
  108. atomic_set(&bat_priv->mesh_state, MESH_ACTIVE);
  109. return 0;
  110. err:
  111. batadv_mesh_free(soft_iface);
  112. return ret;
  113. }
  114. void batadv_mesh_free(struct net_device *soft_iface)
  115. {
  116. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  117. atomic_set(&bat_priv->mesh_state, MESH_DEACTIVATING);
  118. batadv_purge_outstanding_packets(bat_priv, NULL);
  119. batadv_vis_quit(bat_priv);
  120. batadv_gw_node_purge(bat_priv);
  121. batadv_originator_free(bat_priv);
  122. batadv_tt_free(bat_priv);
  123. batadv_bla_free(bat_priv);
  124. free_percpu(bat_priv->bat_counters);
  125. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  126. }
  127. void batadv_inc_module_count(void)
  128. {
  129. try_module_get(THIS_MODULE);
  130. }
  131. void batadv_dec_module_count(void)
  132. {
  133. module_put(THIS_MODULE);
  134. }
  135. int batadv_is_my_mac(const uint8_t *addr)
  136. {
  137. const struct hard_iface *hard_iface;
  138. rcu_read_lock();
  139. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  140. if (hard_iface->if_status != IF_ACTIVE)
  141. continue;
  142. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  143. rcu_read_unlock();
  144. return 1;
  145. }
  146. }
  147. rcu_read_unlock();
  148. return 0;
  149. }
  150. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  151. struct hard_iface *recv_if)
  152. {
  153. return NET_RX_DROP;
  154. }
  155. /* incoming packets with the batman ethertype received on any active hard
  156. * interface
  157. */
  158. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  159. struct packet_type *ptype,
  160. struct net_device *orig_dev)
  161. {
  162. struct bat_priv *bat_priv;
  163. struct batman_ogm_packet *batman_ogm_packet;
  164. struct hard_iface *hard_iface;
  165. uint8_t idx;
  166. int ret;
  167. hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
  168. skb = skb_share_check(skb, GFP_ATOMIC);
  169. /* skb was released by skb_share_check() */
  170. if (!skb)
  171. goto err_out;
  172. /* packet should hold at least type and version */
  173. if (unlikely(!pskb_may_pull(skb, 2)))
  174. goto err_free;
  175. /* expect a valid ethernet header here. */
  176. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  177. goto err_free;
  178. if (!hard_iface->soft_iface)
  179. goto err_free;
  180. bat_priv = netdev_priv(hard_iface->soft_iface);
  181. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  182. goto err_free;
  183. /* discard frames on not active interfaces */
  184. if (hard_iface->if_status != IF_ACTIVE)
  185. goto err_free;
  186. batman_ogm_packet = (struct batman_ogm_packet *)skb->data;
  187. if (batman_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
  188. batadv_dbg(DBG_BATMAN, bat_priv,
  189. "Drop packet: incompatible batman version (%i)\n",
  190. batman_ogm_packet->header.version);
  191. goto err_free;
  192. }
  193. /* all receive handlers return whether they received or reused
  194. * the supplied skb. if not, we have to free the skb.
  195. */
  196. idx = batman_ogm_packet->header.packet_type;
  197. ret = (*batadv_rx_handler[idx])(skb, hard_iface);
  198. if (ret == NET_RX_DROP)
  199. kfree_skb(skb);
  200. /* return NET_RX_SUCCESS in any case as we
  201. * most probably dropped the packet for
  202. * routing-logical reasons.
  203. */
  204. return NET_RX_SUCCESS;
  205. err_free:
  206. kfree_skb(skb);
  207. err_out:
  208. return NET_RX_DROP;
  209. }
  210. static void batadv_recv_handler_init(void)
  211. {
  212. int i;
  213. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  214. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  215. /* batman icmp packet */
  216. batadv_rx_handler[BAT_ICMP] = batadv_recv_icmp_packet;
  217. /* unicast packet */
  218. batadv_rx_handler[BAT_UNICAST] = batadv_recv_unicast_packet;
  219. /* fragmented unicast packet */
  220. batadv_rx_handler[BAT_UNICAST_FRAG] = batadv_recv_ucast_frag_packet;
  221. /* broadcast packet */
  222. batadv_rx_handler[BAT_BCAST] = batadv_recv_bcast_packet;
  223. /* vis packet */
  224. batadv_rx_handler[BAT_VIS] = batadv_recv_vis_packet;
  225. /* Translation table query (request or response) */
  226. batadv_rx_handler[BAT_TT_QUERY] = batadv_recv_tt_query;
  227. /* Roaming advertisement */
  228. batadv_rx_handler[BAT_ROAM_ADV] = batadv_recv_roam_adv;
  229. }
  230. int batadv_recv_handler_register(uint8_t packet_type,
  231. int (*recv_handler)(struct sk_buff *,
  232. struct hard_iface *))
  233. {
  234. if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet)
  235. return -EBUSY;
  236. batadv_rx_handler[packet_type] = recv_handler;
  237. return 0;
  238. }
  239. void batadv_recv_handler_unregister(uint8_t packet_type)
  240. {
  241. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  242. }
  243. static struct bat_algo_ops *batadv_algo_get(char *name)
  244. {
  245. struct bat_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  246. struct hlist_node *node;
  247. hlist_for_each_entry(bat_algo_ops_tmp, node, &batadv_algo_list, list) {
  248. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  249. continue;
  250. bat_algo_ops = bat_algo_ops_tmp;
  251. break;
  252. }
  253. return bat_algo_ops;
  254. }
  255. int batadv_algo_register(struct bat_algo_ops *bat_algo_ops)
  256. {
  257. struct bat_algo_ops *bat_algo_ops_tmp;
  258. int ret;
  259. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  260. if (bat_algo_ops_tmp) {
  261. pr_info("Trying to register already registered routing algorithm: %s\n",
  262. bat_algo_ops->name);
  263. ret = -EEXIST;
  264. goto out;
  265. }
  266. /* all algorithms must implement all ops (for now) */
  267. if (!bat_algo_ops->bat_iface_enable ||
  268. !bat_algo_ops->bat_iface_disable ||
  269. !bat_algo_ops->bat_iface_update_mac ||
  270. !bat_algo_ops->bat_primary_iface_set ||
  271. !bat_algo_ops->bat_ogm_schedule ||
  272. !bat_algo_ops->bat_ogm_emit) {
  273. pr_info("Routing algo '%s' does not implement required ops\n",
  274. bat_algo_ops->name);
  275. ret = -EINVAL;
  276. goto out;
  277. }
  278. INIT_HLIST_NODE(&bat_algo_ops->list);
  279. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  280. ret = 0;
  281. out:
  282. return ret;
  283. }
  284. int batadv_algo_select(struct bat_priv *bat_priv, char *name)
  285. {
  286. struct bat_algo_ops *bat_algo_ops;
  287. int ret = -EINVAL;
  288. bat_algo_ops = batadv_algo_get(name);
  289. if (!bat_algo_ops)
  290. goto out;
  291. bat_priv->bat_algo_ops = bat_algo_ops;
  292. ret = 0;
  293. out:
  294. return ret;
  295. }
  296. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  297. {
  298. struct bat_algo_ops *bat_algo_ops;
  299. struct hlist_node *node;
  300. seq_printf(seq, "Available routing algorithms:\n");
  301. hlist_for_each_entry(bat_algo_ops, node, &batadv_algo_list, list) {
  302. seq_printf(seq, "%s\n", bat_algo_ops->name);
  303. }
  304. return 0;
  305. }
  306. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  307. {
  308. struct bat_algo_ops *bat_algo_ops;
  309. char *algo_name = (char *)val;
  310. size_t name_len = strlen(algo_name);
  311. if (algo_name[name_len - 1] == '\n')
  312. algo_name[name_len - 1] = '\0';
  313. bat_algo_ops = batadv_algo_get(algo_name);
  314. if (!bat_algo_ops) {
  315. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  316. return -EINVAL;
  317. }
  318. return param_set_copystring(algo_name, kp);
  319. }
  320. static const struct kernel_param_ops batadv_param_ops_ra = {
  321. .set = batadv_param_set_ra,
  322. .get = param_get_string,
  323. };
  324. static struct kparam_string batadv_param_string_ra = {
  325. .maxlen = sizeof(batadv_routing_algo),
  326. .string = batadv_routing_algo,
  327. };
  328. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  329. 0644);
  330. module_init(batadv_init);
  331. module_exit(batadv_exit);
  332. MODULE_LICENSE("GPL");
  333. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  334. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  335. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  336. MODULE_VERSION(BATADV_SOURCE_VERSION);