main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* Copyright (C) 2007-2013 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 <linux/crc32c.h>
  20. #include <linux/highmem.h>
  21. #include "main.h"
  22. #include "sysfs.h"
  23. #include "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 "distributed-arp-table.h"
  34. #include "vis.h"
  35. #include "hash.h"
  36. #include "bat_algo.h"
  37. #include "network-coding.h"
  38. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  39. * list traversals just rcu-locked
  40. */
  41. struct list_head batadv_hardif_list;
  42. static int (*batadv_rx_handler[256])(struct sk_buff *,
  43. struct batadv_hard_iface *);
  44. char batadv_routing_algo[20] = "BATMAN_IV";
  45. static struct hlist_head batadv_algo_list;
  46. unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  47. struct workqueue_struct *batadv_event_workqueue;
  48. static void batadv_recv_handler_init(void);
  49. static int __init batadv_init(void)
  50. {
  51. INIT_LIST_HEAD(&batadv_hardif_list);
  52. INIT_HLIST_HEAD(&batadv_algo_list);
  53. batadv_recv_handler_init();
  54. batadv_iv_init();
  55. batadv_event_workqueue = create_singlethread_workqueue("bat_events");
  56. if (!batadv_event_workqueue)
  57. return -ENOMEM;
  58. batadv_socket_init();
  59. batadv_debugfs_init();
  60. register_netdevice_notifier(&batadv_hard_if_notifier);
  61. rtnl_link_register(&batadv_link_ops);
  62. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  63. BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
  64. return 0;
  65. }
  66. static void __exit batadv_exit(void)
  67. {
  68. batadv_debugfs_destroy();
  69. rtnl_link_unregister(&batadv_link_ops);
  70. unregister_netdevice_notifier(&batadv_hard_if_notifier);
  71. batadv_hardif_remove_interfaces();
  72. flush_workqueue(batadv_event_workqueue);
  73. destroy_workqueue(batadv_event_workqueue);
  74. batadv_event_workqueue = NULL;
  75. rcu_barrier();
  76. }
  77. int batadv_mesh_init(struct net_device *soft_iface)
  78. {
  79. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  80. int ret;
  81. spin_lock_init(&bat_priv->forw_bat_list_lock);
  82. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  83. spin_lock_init(&bat_priv->tt.changes_list_lock);
  84. spin_lock_init(&bat_priv->tt.req_list_lock);
  85. spin_lock_init(&bat_priv->tt.roam_list_lock);
  86. spin_lock_init(&bat_priv->tt.last_changeset_lock);
  87. spin_lock_init(&bat_priv->gw.list_lock);
  88. spin_lock_init(&bat_priv->vis.hash_lock);
  89. spin_lock_init(&bat_priv->vis.list_lock);
  90. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  91. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  92. INIT_HLIST_HEAD(&bat_priv->gw.list);
  93. INIT_LIST_HEAD(&bat_priv->tt.changes_list);
  94. INIT_LIST_HEAD(&bat_priv->tt.req_list);
  95. INIT_LIST_HEAD(&bat_priv->tt.roam_list);
  96. ret = batadv_originator_init(bat_priv);
  97. if (ret < 0)
  98. goto err;
  99. ret = batadv_tt_init(bat_priv);
  100. if (ret < 0)
  101. goto err;
  102. batadv_tt_local_add(soft_iface, soft_iface->dev_addr,
  103. BATADV_NULL_IFINDEX);
  104. ret = batadv_vis_init(bat_priv);
  105. if (ret < 0)
  106. goto err;
  107. ret = batadv_bla_init(bat_priv);
  108. if (ret < 0)
  109. goto err;
  110. ret = batadv_dat_init(bat_priv);
  111. if (ret < 0)
  112. goto err;
  113. ret = batadv_nc_init(bat_priv);
  114. if (ret < 0)
  115. goto err;
  116. atomic_set(&bat_priv->gw.reselect, 0);
  117. atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
  118. return 0;
  119. err:
  120. batadv_mesh_free(soft_iface);
  121. return ret;
  122. }
  123. void batadv_mesh_free(struct net_device *soft_iface)
  124. {
  125. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  126. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  127. batadv_purge_outstanding_packets(bat_priv, NULL);
  128. batadv_vis_quit(bat_priv);
  129. batadv_gw_node_purge(bat_priv);
  130. batadv_nc_free(bat_priv);
  131. batadv_dat_free(bat_priv);
  132. batadv_bla_free(bat_priv);
  133. /* Free the TT and the originator tables only after having terminated
  134. * all the other depending components which may use these structures for
  135. * their purposes.
  136. */
  137. batadv_tt_free(bat_priv);
  138. /* Since the originator table clean up routine is accessing the TT
  139. * tables as well, it has to be invoked after the TT tables have been
  140. * freed and marked as empty. This ensures that no cleanup RCU callbacks
  141. * accessing the TT data are scheduled for later execution.
  142. */
  143. batadv_originator_free(bat_priv);
  144. free_percpu(bat_priv->bat_counters);
  145. bat_priv->bat_counters = NULL;
  146. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  147. }
  148. /**
  149. * batadv_is_my_mac - check if the given mac address belongs to any of the real
  150. * interfaces in the current mesh
  151. * @bat_priv: the bat priv with all the soft interface information
  152. * @addr: the address to check
  153. */
  154. int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr)
  155. {
  156. const struct batadv_hard_iface *hard_iface;
  157. rcu_read_lock();
  158. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  159. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  160. continue;
  161. if (hard_iface->soft_iface != bat_priv->soft_iface)
  162. continue;
  163. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  164. rcu_read_unlock();
  165. return 1;
  166. }
  167. }
  168. rcu_read_unlock();
  169. return 0;
  170. }
  171. /**
  172. * batadv_seq_print_text_primary_if_get - called from debugfs table printing
  173. * function that requires the primary interface
  174. * @seq: debugfs table seq_file struct
  175. *
  176. * Returns primary interface if found or NULL otherwise.
  177. */
  178. struct batadv_hard_iface *
  179. batadv_seq_print_text_primary_if_get(struct seq_file *seq)
  180. {
  181. struct net_device *net_dev = (struct net_device *)seq->private;
  182. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  183. struct batadv_hard_iface *primary_if;
  184. primary_if = batadv_primary_if_get_selected(bat_priv);
  185. if (!primary_if) {
  186. seq_printf(seq,
  187. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  188. net_dev->name);
  189. goto out;
  190. }
  191. if (primary_if->if_status == BATADV_IF_ACTIVE)
  192. goto out;
  193. seq_printf(seq,
  194. "BATMAN mesh %s disabled - primary interface not active\n",
  195. net_dev->name);
  196. batadv_hardif_free_ref(primary_if);
  197. primary_if = NULL;
  198. out:
  199. return primary_if;
  200. }
  201. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  202. struct batadv_hard_iface *recv_if)
  203. {
  204. return NET_RX_DROP;
  205. }
  206. /* incoming packets with the batman ethertype received on any active hard
  207. * interface
  208. */
  209. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  210. struct packet_type *ptype,
  211. struct net_device *orig_dev)
  212. {
  213. struct batadv_priv *bat_priv;
  214. struct batadv_ogm_packet *batadv_ogm_packet;
  215. struct batadv_hard_iface *hard_iface;
  216. uint8_t idx;
  217. int ret;
  218. hard_iface = container_of(ptype, struct batadv_hard_iface,
  219. batman_adv_ptype);
  220. skb = skb_share_check(skb, GFP_ATOMIC);
  221. /* skb was released by skb_share_check() */
  222. if (!skb)
  223. goto err_out;
  224. /* packet should hold at least type and version */
  225. if (unlikely(!pskb_may_pull(skb, 2)))
  226. goto err_free;
  227. /* expect a valid ethernet header here. */
  228. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  229. goto err_free;
  230. if (!hard_iface->soft_iface)
  231. goto err_free;
  232. bat_priv = netdev_priv(hard_iface->soft_iface);
  233. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  234. goto err_free;
  235. /* discard frames on not active interfaces */
  236. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  237. goto err_free;
  238. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  239. if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
  240. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  241. "Drop packet: incompatible batman version (%i)\n",
  242. batadv_ogm_packet->header.version);
  243. goto err_free;
  244. }
  245. /* all receive handlers return whether they received or reused
  246. * the supplied skb. if not, we have to free the skb.
  247. */
  248. idx = batadv_ogm_packet->header.packet_type;
  249. ret = (*batadv_rx_handler[idx])(skb, hard_iface);
  250. if (ret == NET_RX_DROP)
  251. kfree_skb(skb);
  252. /* return NET_RX_SUCCESS in any case as we
  253. * most probably dropped the packet for
  254. * routing-logical reasons.
  255. */
  256. return NET_RX_SUCCESS;
  257. err_free:
  258. kfree_skb(skb);
  259. err_out:
  260. return NET_RX_DROP;
  261. }
  262. static void batadv_recv_handler_init(void)
  263. {
  264. int i;
  265. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  266. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  267. /* batman icmp packet */
  268. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  269. /* unicast with 4 addresses packet */
  270. batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
  271. /* unicast packet */
  272. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  273. /* fragmented unicast packet */
  274. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_ucast_frag_packet;
  275. /* broadcast packet */
  276. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  277. /* vis packet */
  278. batadv_rx_handler[BATADV_VIS] = batadv_recv_vis_packet;
  279. /* Translation table query (request or response) */
  280. batadv_rx_handler[BATADV_TT_QUERY] = batadv_recv_tt_query;
  281. /* Roaming advertisement */
  282. batadv_rx_handler[BATADV_ROAM_ADV] = batadv_recv_roam_adv;
  283. }
  284. int
  285. batadv_recv_handler_register(uint8_t packet_type,
  286. int (*recv_handler)(struct sk_buff *,
  287. struct batadv_hard_iface *))
  288. {
  289. if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet)
  290. return -EBUSY;
  291. batadv_rx_handler[packet_type] = recv_handler;
  292. return 0;
  293. }
  294. void batadv_recv_handler_unregister(uint8_t packet_type)
  295. {
  296. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  297. }
  298. static struct batadv_algo_ops *batadv_algo_get(char *name)
  299. {
  300. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  301. hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  302. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  303. continue;
  304. bat_algo_ops = bat_algo_ops_tmp;
  305. break;
  306. }
  307. return bat_algo_ops;
  308. }
  309. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  310. {
  311. struct batadv_algo_ops *bat_algo_ops_tmp;
  312. int ret;
  313. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  314. if (bat_algo_ops_tmp) {
  315. pr_info("Trying to register already registered routing algorithm: %s\n",
  316. bat_algo_ops->name);
  317. ret = -EEXIST;
  318. goto out;
  319. }
  320. /* all algorithms must implement all ops (for now) */
  321. if (!bat_algo_ops->bat_iface_enable ||
  322. !bat_algo_ops->bat_iface_disable ||
  323. !bat_algo_ops->bat_iface_update_mac ||
  324. !bat_algo_ops->bat_primary_iface_set ||
  325. !bat_algo_ops->bat_ogm_schedule ||
  326. !bat_algo_ops->bat_ogm_emit) {
  327. pr_info("Routing algo '%s' does not implement required ops\n",
  328. bat_algo_ops->name);
  329. ret = -EINVAL;
  330. goto out;
  331. }
  332. INIT_HLIST_NODE(&bat_algo_ops->list);
  333. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  334. ret = 0;
  335. out:
  336. return ret;
  337. }
  338. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  339. {
  340. struct batadv_algo_ops *bat_algo_ops;
  341. int ret = -EINVAL;
  342. bat_algo_ops = batadv_algo_get(name);
  343. if (!bat_algo_ops)
  344. goto out;
  345. bat_priv->bat_algo_ops = bat_algo_ops;
  346. ret = 0;
  347. out:
  348. return ret;
  349. }
  350. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  351. {
  352. struct batadv_algo_ops *bat_algo_ops;
  353. seq_puts(seq, "Available routing algorithms:\n");
  354. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  355. seq_printf(seq, "%s\n", bat_algo_ops->name);
  356. }
  357. return 0;
  358. }
  359. /**
  360. * batadv_skb_crc32 - calculate CRC32 of the whole packet and skip bytes in
  361. * the header
  362. * @skb: skb pointing to fragmented socket buffers
  363. * @payload_ptr: Pointer to position inside the head buffer of the skb
  364. * marking the start of the data to be CRC'ed
  365. *
  366. * payload_ptr must always point to an address in the skb head buffer and not to
  367. * a fragment.
  368. */
  369. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  370. {
  371. u32 crc = 0;
  372. unsigned int from;
  373. unsigned int to = skb->len;
  374. struct skb_seq_state st;
  375. const u8 *data;
  376. unsigned int len;
  377. unsigned int consumed = 0;
  378. from = (unsigned int)(payload_ptr - skb->data);
  379. skb_prepare_seq_read(skb, from, to, &st);
  380. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  381. crc = crc32c(crc, data, len);
  382. consumed += len;
  383. }
  384. return htonl(crc);
  385. }
  386. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  387. {
  388. struct batadv_algo_ops *bat_algo_ops;
  389. char *algo_name = (char *)val;
  390. size_t name_len = strlen(algo_name);
  391. if (name_len > 0 && algo_name[name_len - 1] == '\n')
  392. algo_name[name_len - 1] = '\0';
  393. bat_algo_ops = batadv_algo_get(algo_name);
  394. if (!bat_algo_ops) {
  395. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  396. return -EINVAL;
  397. }
  398. return param_set_copystring(algo_name, kp);
  399. }
  400. static const struct kernel_param_ops batadv_param_ops_ra = {
  401. .set = batadv_param_set_ra,
  402. .get = param_get_string,
  403. };
  404. static struct kparam_string batadv_param_string_ra = {
  405. .maxlen = sizeof(batadv_routing_algo),
  406. .string = batadv_routing_algo,
  407. };
  408. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  409. 0644);
  410. module_init(batadv_init);
  411. module_exit(batadv_exit);
  412. MODULE_LICENSE("GPL");
  413. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  414. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  415. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  416. MODULE_VERSION(BATADV_SOURCE_VERSION);