main.c 10.0 KB

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