main.c 15 KB

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