main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_originator_free(bat_priv);
  131. batadv_nc_free(bat_priv);
  132. batadv_tt_free(bat_priv);
  133. batadv_bla_free(bat_priv);
  134. batadv_dat_free(bat_priv);
  135. free_percpu(bat_priv->bat_counters);
  136. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  137. }
  138. /**
  139. * batadv_is_my_mac - check if the given mac address belongs to any of the real
  140. * interfaces in the current mesh
  141. * @bat_priv: the bat priv with all the soft interface information
  142. * @addr: the address to check
  143. */
  144. int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr)
  145. {
  146. const struct batadv_hard_iface *hard_iface;
  147. rcu_read_lock();
  148. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  149. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  150. continue;
  151. if (hard_iface->soft_iface != bat_priv->soft_iface)
  152. continue;
  153. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  154. rcu_read_unlock();
  155. return 1;
  156. }
  157. }
  158. rcu_read_unlock();
  159. return 0;
  160. }
  161. /**
  162. * batadv_seq_print_text_primary_if_get - called from debugfs table printing
  163. * function that requires the primary interface
  164. * @seq: debugfs table seq_file struct
  165. *
  166. * Returns primary interface if found or NULL otherwise.
  167. */
  168. struct batadv_hard_iface *
  169. batadv_seq_print_text_primary_if_get(struct seq_file *seq)
  170. {
  171. struct net_device *net_dev = (struct net_device *)seq->private;
  172. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  173. struct batadv_hard_iface *primary_if;
  174. primary_if = batadv_primary_if_get_selected(bat_priv);
  175. if (!primary_if) {
  176. seq_printf(seq,
  177. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  178. net_dev->name);
  179. goto out;
  180. }
  181. if (primary_if->if_status == BATADV_IF_ACTIVE)
  182. goto out;
  183. seq_printf(seq,
  184. "BATMAN mesh %s disabled - primary interface not active\n",
  185. net_dev->name);
  186. batadv_hardif_free_ref(primary_if);
  187. primary_if = NULL;
  188. out:
  189. return primary_if;
  190. }
  191. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  192. struct batadv_hard_iface *recv_if)
  193. {
  194. return NET_RX_DROP;
  195. }
  196. /* incoming packets with the batman ethertype received on any active hard
  197. * interface
  198. */
  199. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  200. struct packet_type *ptype,
  201. struct net_device *orig_dev)
  202. {
  203. struct batadv_priv *bat_priv;
  204. struct batadv_ogm_packet *batadv_ogm_packet;
  205. struct batadv_hard_iface *hard_iface;
  206. uint8_t idx;
  207. int ret;
  208. hard_iface = container_of(ptype, struct batadv_hard_iface,
  209. batman_adv_ptype);
  210. skb = skb_share_check(skb, GFP_ATOMIC);
  211. /* skb was released by skb_share_check() */
  212. if (!skb)
  213. goto err_out;
  214. /* packet should hold at least type and version */
  215. if (unlikely(!pskb_may_pull(skb, 2)))
  216. goto err_free;
  217. /* expect a valid ethernet header here. */
  218. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  219. goto err_free;
  220. if (!hard_iface->soft_iface)
  221. goto err_free;
  222. bat_priv = netdev_priv(hard_iface->soft_iface);
  223. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  224. goto err_free;
  225. /* discard frames on not active interfaces */
  226. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  227. goto err_free;
  228. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  229. if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
  230. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  231. "Drop packet: incompatible batman version (%i)\n",
  232. batadv_ogm_packet->header.version);
  233. goto err_free;
  234. }
  235. /* all receive handlers return whether they received or reused
  236. * the supplied skb. if not, we have to free the skb.
  237. */
  238. idx = batadv_ogm_packet->header.packet_type;
  239. ret = (*batadv_rx_handler[idx])(skb, hard_iface);
  240. if (ret == NET_RX_DROP)
  241. kfree_skb(skb);
  242. /* return NET_RX_SUCCESS in any case as we
  243. * most probably dropped the packet for
  244. * routing-logical reasons.
  245. */
  246. return NET_RX_SUCCESS;
  247. err_free:
  248. kfree_skb(skb);
  249. err_out:
  250. return NET_RX_DROP;
  251. }
  252. static void batadv_recv_handler_init(void)
  253. {
  254. int i;
  255. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  256. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  257. /* batman icmp packet */
  258. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  259. /* unicast with 4 addresses packet */
  260. batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
  261. /* unicast packet */
  262. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  263. /* fragmented unicast packet */
  264. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_ucast_frag_packet;
  265. /* broadcast packet */
  266. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  267. /* vis packet */
  268. batadv_rx_handler[BATADV_VIS] = batadv_recv_vis_packet;
  269. /* Translation table query (request or response) */
  270. batadv_rx_handler[BATADV_TT_QUERY] = batadv_recv_tt_query;
  271. /* Roaming advertisement */
  272. batadv_rx_handler[BATADV_ROAM_ADV] = batadv_recv_roam_adv;
  273. }
  274. int
  275. batadv_recv_handler_register(uint8_t packet_type,
  276. int (*recv_handler)(struct sk_buff *,
  277. struct batadv_hard_iface *))
  278. {
  279. if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet)
  280. return -EBUSY;
  281. batadv_rx_handler[packet_type] = recv_handler;
  282. return 0;
  283. }
  284. void batadv_recv_handler_unregister(uint8_t packet_type)
  285. {
  286. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  287. }
  288. static struct batadv_algo_ops *batadv_algo_get(char *name)
  289. {
  290. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  291. hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  292. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  293. continue;
  294. bat_algo_ops = bat_algo_ops_tmp;
  295. break;
  296. }
  297. return bat_algo_ops;
  298. }
  299. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  300. {
  301. struct batadv_algo_ops *bat_algo_ops_tmp;
  302. int ret;
  303. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  304. if (bat_algo_ops_tmp) {
  305. pr_info("Trying to register already registered routing algorithm: %s\n",
  306. bat_algo_ops->name);
  307. ret = -EEXIST;
  308. goto out;
  309. }
  310. /* all algorithms must implement all ops (for now) */
  311. if (!bat_algo_ops->bat_iface_enable ||
  312. !bat_algo_ops->bat_iface_disable ||
  313. !bat_algo_ops->bat_iface_update_mac ||
  314. !bat_algo_ops->bat_primary_iface_set ||
  315. !bat_algo_ops->bat_ogm_schedule ||
  316. !bat_algo_ops->bat_ogm_emit) {
  317. pr_info("Routing algo '%s' does not implement required ops\n",
  318. bat_algo_ops->name);
  319. ret = -EINVAL;
  320. goto out;
  321. }
  322. INIT_HLIST_NODE(&bat_algo_ops->list);
  323. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  324. ret = 0;
  325. out:
  326. return ret;
  327. }
  328. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  329. {
  330. struct batadv_algo_ops *bat_algo_ops;
  331. int ret = -EINVAL;
  332. bat_algo_ops = batadv_algo_get(name);
  333. if (!bat_algo_ops)
  334. goto out;
  335. bat_priv->bat_algo_ops = bat_algo_ops;
  336. ret = 0;
  337. out:
  338. return ret;
  339. }
  340. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  341. {
  342. struct batadv_algo_ops *bat_algo_ops;
  343. seq_puts(seq, "Available routing algorithms:\n");
  344. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  345. seq_printf(seq, "%s\n", bat_algo_ops->name);
  346. }
  347. return 0;
  348. }
  349. /**
  350. * batadv_skb_crc32 - calculate CRC32 of the whole packet and skip bytes in
  351. * the header
  352. * @skb: skb pointing to fragmented socket buffers
  353. * @payload_ptr: Pointer to position inside the head buffer of the skb
  354. * marking the start of the data to be CRC'ed
  355. *
  356. * payload_ptr must always point to an address in the skb head buffer and not to
  357. * a fragment.
  358. */
  359. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  360. {
  361. u32 crc = 0;
  362. unsigned int from;
  363. unsigned int to = skb->len;
  364. struct skb_seq_state st;
  365. const u8 *data;
  366. unsigned int len;
  367. unsigned int consumed = 0;
  368. from = (unsigned int)(payload_ptr - skb->data);
  369. skb_prepare_seq_read(skb, from, to, &st);
  370. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  371. crc = crc32c(crc, data, len);
  372. consumed += len;
  373. }
  374. skb_abort_seq_read(&st);
  375. return htonl(crc);
  376. }
  377. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  378. {
  379. struct batadv_algo_ops *bat_algo_ops;
  380. char *algo_name = (char *)val;
  381. size_t name_len = strlen(algo_name);
  382. if (algo_name[name_len - 1] == '\n')
  383. algo_name[name_len - 1] = '\0';
  384. bat_algo_ops = batadv_algo_get(algo_name);
  385. if (!bat_algo_ops) {
  386. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  387. return -EINVAL;
  388. }
  389. return param_set_copystring(algo_name, kp);
  390. }
  391. static const struct kernel_param_ops batadv_param_ops_ra = {
  392. .set = batadv_param_set_ra,
  393. .get = param_get_string,
  394. };
  395. static struct kparam_string batadv_param_string_ra = {
  396. .maxlen = sizeof(batadv_routing_algo),
  397. .string = batadv_routing_algo,
  398. };
  399. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  400. 0644);
  401. module_init(batadv_init);
  402. module_exit(batadv_exit);
  403. MODULE_LICENSE("GPL");
  404. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  405. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  406. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  407. MODULE_VERSION(BATADV_SOURCE_VERSION);