main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 "sysfs.h"
  21. #include "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 batadv_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. batadv_event_workqueue = create_singlethread_workqueue("bat_events");
  52. if (!batadv_event_workqueue)
  53. return -ENOMEM;
  54. batadv_socket_init();
  55. batadv_debugfs_init();
  56. register_netdevice_notifier(&batadv_hard_if_notifier);
  57. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  58. BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
  59. return 0;
  60. }
  61. static void __exit batadv_exit(void)
  62. {
  63. batadv_debugfs_destroy();
  64. unregister_netdevice_notifier(&batadv_hard_if_notifier);
  65. batadv_hardif_remove_interfaces();
  66. flush_workqueue(batadv_event_workqueue);
  67. destroy_workqueue(batadv_event_workqueue);
  68. batadv_event_workqueue = NULL;
  69. rcu_barrier();
  70. }
  71. int batadv_mesh_init(struct net_device *soft_iface)
  72. {
  73. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  74. int ret;
  75. spin_lock_init(&bat_priv->forw_bat_list_lock);
  76. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  77. spin_lock_init(&bat_priv->tt.changes_list_lock);
  78. spin_lock_init(&bat_priv->tt.req_list_lock);
  79. spin_lock_init(&bat_priv->tt.roam_list_lock);
  80. spin_lock_init(&bat_priv->tt.last_changeset_lock);
  81. spin_lock_init(&bat_priv->gw.list_lock);
  82. spin_lock_init(&bat_priv->vis.hash_lock);
  83. spin_lock_init(&bat_priv->vis.list_lock);
  84. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  85. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  86. INIT_HLIST_HEAD(&bat_priv->gw.list);
  87. INIT_LIST_HEAD(&bat_priv->tt.changes_list);
  88. INIT_LIST_HEAD(&bat_priv->tt.req_list);
  89. INIT_LIST_HEAD(&bat_priv->tt.roam_list);
  90. ret = batadv_originator_init(bat_priv);
  91. if (ret < 0)
  92. goto err;
  93. ret = batadv_tt_init(bat_priv);
  94. if (ret < 0)
  95. goto err;
  96. batadv_tt_local_add(soft_iface, soft_iface->dev_addr,
  97. BATADV_NULL_IFINDEX);
  98. ret = batadv_vis_init(bat_priv);
  99. if (ret < 0)
  100. goto err;
  101. ret = batadv_bla_init(bat_priv);
  102. if (ret < 0)
  103. goto err;
  104. atomic_set(&bat_priv->gw.reselect, 0);
  105. atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
  106. return 0;
  107. err:
  108. batadv_mesh_free(soft_iface);
  109. return ret;
  110. }
  111. void batadv_mesh_free(struct net_device *soft_iface)
  112. {
  113. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  114. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  115. batadv_purge_outstanding_packets(bat_priv, NULL);
  116. batadv_vis_quit(bat_priv);
  117. batadv_gw_node_purge(bat_priv);
  118. batadv_originator_free(bat_priv);
  119. batadv_tt_free(bat_priv);
  120. batadv_bla_free(bat_priv);
  121. free_percpu(bat_priv->bat_counters);
  122. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  123. }
  124. int batadv_is_my_mac(const uint8_t *addr)
  125. {
  126. const struct batadv_hard_iface *hard_iface;
  127. rcu_read_lock();
  128. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  129. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  130. continue;
  131. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  132. rcu_read_unlock();
  133. return 1;
  134. }
  135. }
  136. rcu_read_unlock();
  137. return 0;
  138. }
  139. /**
  140. * batadv_seq_print_text_primary_if_get - called from debugfs table printing
  141. * function that requires the primary interface
  142. * @seq: debugfs table seq_file struct
  143. *
  144. * Returns primary interface if found or NULL otherwise.
  145. */
  146. struct batadv_hard_iface *
  147. batadv_seq_print_text_primary_if_get(struct seq_file *seq)
  148. {
  149. struct net_device *net_dev = (struct net_device *)seq->private;
  150. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  151. struct batadv_hard_iface *primary_if;
  152. primary_if = batadv_primary_if_get_selected(bat_priv);
  153. if (!primary_if) {
  154. seq_printf(seq,
  155. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  156. net_dev->name);
  157. goto out;
  158. }
  159. if (primary_if->if_status == BATADV_IF_ACTIVE)
  160. goto out;
  161. seq_printf(seq,
  162. "BATMAN mesh %s disabled - primary interface not active\n",
  163. net_dev->name);
  164. batadv_hardif_free_ref(primary_if);
  165. primary_if = NULL;
  166. out:
  167. return primary_if;
  168. }
  169. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  170. struct batadv_hard_iface *recv_if)
  171. {
  172. return NET_RX_DROP;
  173. }
  174. /* incoming packets with the batman ethertype received on any active hard
  175. * interface
  176. */
  177. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  178. struct packet_type *ptype,
  179. struct net_device *orig_dev)
  180. {
  181. struct batadv_priv *bat_priv;
  182. struct batadv_ogm_packet *batadv_ogm_packet;
  183. struct batadv_hard_iface *hard_iface;
  184. uint8_t idx;
  185. int ret;
  186. hard_iface = container_of(ptype, struct batadv_hard_iface,
  187. batman_adv_ptype);
  188. skb = skb_share_check(skb, GFP_ATOMIC);
  189. /* skb was released by skb_share_check() */
  190. if (!skb)
  191. goto err_out;
  192. /* packet should hold at least type and version */
  193. if (unlikely(!pskb_may_pull(skb, 2)))
  194. goto err_free;
  195. /* expect a valid ethernet header here. */
  196. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  197. goto err_free;
  198. if (!hard_iface->soft_iface)
  199. goto err_free;
  200. bat_priv = netdev_priv(hard_iface->soft_iface);
  201. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  202. goto err_free;
  203. /* discard frames on not active interfaces */
  204. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  205. goto err_free;
  206. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  207. if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
  208. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  209. "Drop packet: incompatible batman version (%i)\n",
  210. batadv_ogm_packet->header.version);
  211. goto err_free;
  212. }
  213. /* all receive handlers return whether they received or reused
  214. * the supplied skb. if not, we have to free the skb.
  215. */
  216. idx = batadv_ogm_packet->header.packet_type;
  217. ret = (*batadv_rx_handler[idx])(skb, hard_iface);
  218. if (ret == NET_RX_DROP)
  219. kfree_skb(skb);
  220. /* return NET_RX_SUCCESS in any case as we
  221. * most probably dropped the packet for
  222. * routing-logical reasons.
  223. */
  224. return NET_RX_SUCCESS;
  225. err_free:
  226. kfree_skb(skb);
  227. err_out:
  228. return NET_RX_DROP;
  229. }
  230. static void batadv_recv_handler_init(void)
  231. {
  232. int i;
  233. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  234. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  235. /* batman icmp packet */
  236. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  237. /* unicast packet */
  238. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  239. /* fragmented unicast packet */
  240. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_ucast_frag_packet;
  241. /* broadcast packet */
  242. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  243. /* vis packet */
  244. batadv_rx_handler[BATADV_VIS] = batadv_recv_vis_packet;
  245. /* Translation table query (request or response) */
  246. batadv_rx_handler[BATADV_TT_QUERY] = batadv_recv_tt_query;
  247. /* Roaming advertisement */
  248. batadv_rx_handler[BATADV_ROAM_ADV] = batadv_recv_roam_adv;
  249. }
  250. int
  251. batadv_recv_handler_register(uint8_t packet_type,
  252. int (*recv_handler)(struct sk_buff *,
  253. struct batadv_hard_iface *))
  254. {
  255. if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet)
  256. return -EBUSY;
  257. batadv_rx_handler[packet_type] = recv_handler;
  258. return 0;
  259. }
  260. void batadv_recv_handler_unregister(uint8_t packet_type)
  261. {
  262. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  263. }
  264. static struct batadv_algo_ops *batadv_algo_get(char *name)
  265. {
  266. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  267. struct hlist_node *node;
  268. hlist_for_each_entry(bat_algo_ops_tmp, node, &batadv_algo_list, list) {
  269. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  270. continue;
  271. bat_algo_ops = bat_algo_ops_tmp;
  272. break;
  273. }
  274. return bat_algo_ops;
  275. }
  276. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  277. {
  278. struct batadv_algo_ops *bat_algo_ops_tmp;
  279. int ret;
  280. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  281. if (bat_algo_ops_tmp) {
  282. pr_info("Trying to register already registered routing algorithm: %s\n",
  283. bat_algo_ops->name);
  284. ret = -EEXIST;
  285. goto out;
  286. }
  287. /* all algorithms must implement all ops (for now) */
  288. if (!bat_algo_ops->bat_iface_enable ||
  289. !bat_algo_ops->bat_iface_disable ||
  290. !bat_algo_ops->bat_iface_update_mac ||
  291. !bat_algo_ops->bat_primary_iface_set ||
  292. !bat_algo_ops->bat_ogm_schedule ||
  293. !bat_algo_ops->bat_ogm_emit) {
  294. pr_info("Routing algo '%s' does not implement required ops\n",
  295. bat_algo_ops->name);
  296. ret = -EINVAL;
  297. goto out;
  298. }
  299. INIT_HLIST_NODE(&bat_algo_ops->list);
  300. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  301. ret = 0;
  302. out:
  303. return ret;
  304. }
  305. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  306. {
  307. struct batadv_algo_ops *bat_algo_ops;
  308. int ret = -EINVAL;
  309. bat_algo_ops = batadv_algo_get(name);
  310. if (!bat_algo_ops)
  311. goto out;
  312. bat_priv->bat_algo_ops = bat_algo_ops;
  313. ret = 0;
  314. out:
  315. return ret;
  316. }
  317. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  318. {
  319. struct batadv_algo_ops *bat_algo_ops;
  320. struct hlist_node *node;
  321. seq_printf(seq, "Available routing algorithms:\n");
  322. hlist_for_each_entry(bat_algo_ops, node, &batadv_algo_list, list) {
  323. seq_printf(seq, "%s\n", bat_algo_ops->name);
  324. }
  325. return 0;
  326. }
  327. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  328. {
  329. struct batadv_algo_ops *bat_algo_ops;
  330. char *algo_name = (char *)val;
  331. size_t name_len = strlen(algo_name);
  332. if (algo_name[name_len - 1] == '\n')
  333. algo_name[name_len - 1] = '\0';
  334. bat_algo_ops = batadv_algo_get(algo_name);
  335. if (!bat_algo_ops) {
  336. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  337. return -EINVAL;
  338. }
  339. return param_set_copystring(algo_name, kp);
  340. }
  341. static const struct kernel_param_ops batadv_param_ops_ra = {
  342. .set = batadv_param_set_ra,
  343. .get = param_get_string,
  344. };
  345. static struct kparam_string batadv_param_string_ra = {
  346. .maxlen = sizeof(batadv_routing_algo),
  347. .string = batadv_routing_algo,
  348. };
  349. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  350. 0644);
  351. module_init(batadv_init);
  352. module_exit(batadv_exit);
  353. MODULE_LICENSE("GPL");
  354. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  355. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  356. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  357. MODULE_VERSION(BATADV_SOURCE_VERSION);