main.c 12 KB

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