main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #include "main.h"
  7. #include <linux/array_size.h>
  8. #include <linux/atomic.h>
  9. #include <linux/build_bug.h>
  10. #include <linux/byteorder/generic.h>
  11. #include <linux/container_of.h>
  12. #include <linux/crc32c.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/gfp.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/init.h>
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h>
  21. #include <linux/kobject.h>
  22. #include <linux/kref.h>
  23. #include <linux/list.h>
  24. #include <linux/minmax.h>
  25. #include <linux/module.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/printk.h>
  28. #include <linux/rculist.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/sprintf.h>
  34. #include <linux/stddef.h>
  35. #include <linux/string.h>
  36. #include <linux/workqueue.h>
  37. #include <net/dsfield.h>
  38. #include <net/genetlink.h>
  39. #include <net/rtnetlink.h>
  40. #include <uapi/linux/batadv_packet.h>
  41. #include <uapi/linux/batman_adv.h>
  42. #include "bat_algo.h"
  43. #include "bat_iv_ogm.h"
  44. #include "bat_v.h"
  45. #include "bridge_loop_avoidance.h"
  46. #include "distributed-arp-table.h"
  47. #include "gateway_client.h"
  48. #include "gateway_common.h"
  49. #include "hard-interface.h"
  50. #include "log.h"
  51. #include "multicast.h"
  52. #include "netlink.h"
  53. #include "network-coding.h"
  54. #include "originator.h"
  55. #include "routing.h"
  56. #include "send.h"
  57. #include "soft-interface.h"
  58. #include "tp_meter.h"
  59. #include "translation-table.h"
  60. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  61. * list traversals just rcu-locked
  62. */
  63. struct list_head batadv_hardif_list;
  64. unsigned int batadv_hardif_generation;
  65. static int (*batadv_rx_handler[256])(struct sk_buff *skb,
  66. struct batadv_hard_iface *recv_if);
  67. unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  68. struct workqueue_struct *batadv_event_workqueue;
  69. static void batadv_recv_handler_init(void);
  70. #define BATADV_UEV_TYPE_VAR "BATTYPE="
  71. #define BATADV_UEV_ACTION_VAR "BATACTION="
  72. #define BATADV_UEV_DATA_VAR "BATDATA="
  73. static char *batadv_uev_action_str[] = {
  74. "add",
  75. "del",
  76. "change",
  77. "loopdetect",
  78. };
  79. static char *batadv_uev_type_str[] = {
  80. "gw",
  81. "bla",
  82. };
  83. static int __init batadv_init(void)
  84. {
  85. int ret;
  86. ret = batadv_tt_cache_init();
  87. if (ret < 0)
  88. return ret;
  89. INIT_LIST_HEAD(&batadv_hardif_list);
  90. batadv_algo_init();
  91. batadv_recv_handler_init();
  92. batadv_v_init();
  93. batadv_iv_init();
  94. batadv_nc_init();
  95. batadv_tp_meter_init();
  96. batadv_event_workqueue = create_singlethread_workqueue("bat_events");
  97. if (!batadv_event_workqueue)
  98. goto err_create_wq;
  99. register_netdevice_notifier(&batadv_hard_if_notifier);
  100. rtnl_link_register(&batadv_link_ops);
  101. batadv_netlink_register();
  102. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  103. BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
  104. return 0;
  105. err_create_wq:
  106. batadv_tt_cache_destroy();
  107. return -ENOMEM;
  108. }
  109. static void __exit batadv_exit(void)
  110. {
  111. batadv_netlink_unregister();
  112. rtnl_link_unregister(&batadv_link_ops);
  113. unregister_netdevice_notifier(&batadv_hard_if_notifier);
  114. destroy_workqueue(batadv_event_workqueue);
  115. batadv_event_workqueue = NULL;
  116. rcu_barrier();
  117. batadv_tt_cache_destroy();
  118. }
  119. /**
  120. * batadv_mesh_init() - Initialize soft interface
  121. * @soft_iface: netdev struct of the soft interface
  122. *
  123. * Return: 0 on success or negative error number in case of failure
  124. */
  125. int batadv_mesh_init(struct net_device *soft_iface)
  126. {
  127. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  128. int ret;
  129. spin_lock_init(&bat_priv->forw_bat_list_lock);
  130. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  131. spin_lock_init(&bat_priv->tt.changes_list_lock);
  132. spin_lock_init(&bat_priv->tt.req_list_lock);
  133. spin_lock_init(&bat_priv->tt.roam_list_lock);
  134. spin_lock_init(&bat_priv->tt.last_changeset_lock);
  135. spin_lock_init(&bat_priv->tt.commit_lock);
  136. spin_lock_init(&bat_priv->gw.list_lock);
  137. #ifdef CONFIG_BATMAN_ADV_MCAST
  138. spin_lock_init(&bat_priv->mcast.mla_lock);
  139. spin_lock_init(&bat_priv->mcast.want_lists_lock);
  140. #endif
  141. spin_lock_init(&bat_priv->tvlv.container_list_lock);
  142. spin_lock_init(&bat_priv->tvlv.handler_list_lock);
  143. spin_lock_init(&bat_priv->softif_vlan_list_lock);
  144. spin_lock_init(&bat_priv->tp_list_lock);
  145. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  146. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  147. INIT_HLIST_HEAD(&bat_priv->gw.gateway_list);
  148. #ifdef CONFIG_BATMAN_ADV_MCAST
  149. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_unsnoopables_list);
  150. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv4_list);
  151. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv6_list);
  152. #endif
  153. INIT_LIST_HEAD(&bat_priv->tt.changes_list);
  154. INIT_HLIST_HEAD(&bat_priv->tt.req_list);
  155. INIT_LIST_HEAD(&bat_priv->tt.roam_list);
  156. #ifdef CONFIG_BATMAN_ADV_MCAST
  157. INIT_HLIST_HEAD(&bat_priv->mcast.mla_list);
  158. #endif
  159. INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
  160. INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
  161. INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
  162. INIT_HLIST_HEAD(&bat_priv->tp_list);
  163. bat_priv->gw.generation = 0;
  164. ret = batadv_originator_init(bat_priv);
  165. if (ret < 0) {
  166. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  167. goto err_orig;
  168. }
  169. ret = batadv_tt_init(bat_priv);
  170. if (ret < 0) {
  171. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  172. goto err_tt;
  173. }
  174. ret = batadv_v_mesh_init(bat_priv);
  175. if (ret < 0) {
  176. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  177. goto err_v;
  178. }
  179. ret = batadv_bla_init(bat_priv);
  180. if (ret < 0) {
  181. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  182. goto err_bla;
  183. }
  184. ret = batadv_dat_init(bat_priv);
  185. if (ret < 0) {
  186. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  187. goto err_dat;
  188. }
  189. ret = batadv_nc_mesh_init(bat_priv);
  190. if (ret < 0) {
  191. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  192. goto err_nc;
  193. }
  194. batadv_gw_init(bat_priv);
  195. batadv_mcast_init(bat_priv);
  196. atomic_set(&bat_priv->gw.reselect, 0);
  197. atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
  198. return 0;
  199. err_nc:
  200. batadv_dat_free(bat_priv);
  201. err_dat:
  202. batadv_bla_free(bat_priv);
  203. err_bla:
  204. batadv_v_mesh_free(bat_priv);
  205. err_v:
  206. batadv_tt_free(bat_priv);
  207. err_tt:
  208. batadv_originator_free(bat_priv);
  209. err_orig:
  210. batadv_purge_outstanding_packets(bat_priv, NULL);
  211. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  212. return ret;
  213. }
  214. /**
  215. * batadv_mesh_free() - Deinitialize soft interface
  216. * @soft_iface: netdev struct of the soft interface
  217. */
  218. void batadv_mesh_free(struct net_device *soft_iface)
  219. {
  220. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  221. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  222. batadv_purge_outstanding_packets(bat_priv, NULL);
  223. batadv_gw_node_free(bat_priv);
  224. batadv_v_mesh_free(bat_priv);
  225. batadv_nc_mesh_free(bat_priv);
  226. batadv_dat_free(bat_priv);
  227. batadv_bla_free(bat_priv);
  228. batadv_mcast_free(bat_priv);
  229. /* Free the TT and the originator tables only after having terminated
  230. * all the other depending components which may use these structures for
  231. * their purposes.
  232. */
  233. batadv_tt_free(bat_priv);
  234. /* Since the originator table clean up routine is accessing the TT
  235. * tables as well, it has to be invoked after the TT tables have been
  236. * freed and marked as empty. This ensures that no cleanup RCU callbacks
  237. * accessing the TT data are scheduled for later execution.
  238. */
  239. batadv_originator_free(bat_priv);
  240. batadv_gw_free(bat_priv);
  241. free_percpu(bat_priv->bat_counters);
  242. bat_priv->bat_counters = NULL;
  243. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  244. }
  245. /**
  246. * batadv_is_my_mac() - check if the given mac address belongs to any of the
  247. * real interfaces in the current mesh
  248. * @bat_priv: the bat priv with all the soft interface information
  249. * @addr: the address to check
  250. *
  251. * Return: 'true' if the mac address was found, false otherwise.
  252. */
  253. bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr)
  254. {
  255. const struct batadv_hard_iface *hard_iface;
  256. bool is_my_mac = false;
  257. rcu_read_lock();
  258. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  259. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  260. continue;
  261. if (hard_iface->soft_iface != bat_priv->soft_iface)
  262. continue;
  263. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  264. is_my_mac = true;
  265. break;
  266. }
  267. }
  268. rcu_read_unlock();
  269. return is_my_mac;
  270. }
  271. /**
  272. * batadv_max_header_len() - calculate maximum encapsulation overhead for a
  273. * payload packet
  274. *
  275. * Return: the maximum encapsulation overhead in bytes.
  276. */
  277. int batadv_max_header_len(void)
  278. {
  279. int header_len = 0;
  280. header_len = max_t(int, header_len,
  281. sizeof(struct batadv_unicast_packet));
  282. header_len = max_t(int, header_len,
  283. sizeof(struct batadv_unicast_4addr_packet));
  284. header_len = max_t(int, header_len,
  285. sizeof(struct batadv_bcast_packet));
  286. #ifdef CONFIG_BATMAN_ADV_NC
  287. header_len = max_t(int, header_len,
  288. sizeof(struct batadv_coded_packet));
  289. #endif
  290. return header_len + ETH_HLEN;
  291. }
  292. /**
  293. * batadv_skb_set_priority() - sets skb priority according to packet content
  294. * @skb: the packet to be sent
  295. * @offset: offset to the packet content
  296. *
  297. * This function sets a value between 256 and 263 (802.1d priority), which
  298. * can be interpreted by the cfg80211 or other drivers.
  299. */
  300. void batadv_skb_set_priority(struct sk_buff *skb, int offset)
  301. {
  302. struct iphdr ip_hdr_tmp, *ip_hdr;
  303. struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
  304. struct ethhdr ethhdr_tmp, *ethhdr;
  305. struct vlan_ethhdr *vhdr, vhdr_tmp;
  306. u32 prio;
  307. /* already set, do nothing */
  308. if (skb->priority >= 256 && skb->priority <= 263)
  309. return;
  310. ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), &ethhdr_tmp);
  311. if (!ethhdr)
  312. return;
  313. switch (ethhdr->h_proto) {
  314. case htons(ETH_P_8021Q):
  315. vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr),
  316. sizeof(*vhdr), &vhdr_tmp);
  317. if (!vhdr)
  318. return;
  319. prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
  320. prio = prio >> VLAN_PRIO_SHIFT;
  321. break;
  322. case htons(ETH_P_IP):
  323. ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  324. sizeof(*ip_hdr), &ip_hdr_tmp);
  325. if (!ip_hdr)
  326. return;
  327. prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
  328. break;
  329. case htons(ETH_P_IPV6):
  330. ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  331. sizeof(*ip6_hdr), &ip6_hdr_tmp);
  332. if (!ip6_hdr)
  333. return;
  334. prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
  335. break;
  336. default:
  337. return;
  338. }
  339. skb->priority = prio + 256;
  340. }
  341. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  342. struct batadv_hard_iface *recv_if)
  343. {
  344. kfree_skb(skb);
  345. return NET_RX_DROP;
  346. }
  347. /* incoming packets with the batman ethertype received on any active hard
  348. * interface
  349. */
  350. /**
  351. * batadv_batman_skb_recv() - Handle incoming message from an hard interface
  352. * @skb: the received packet
  353. * @dev: the net device that the packet was received on
  354. * @ptype: packet type of incoming packet (ETH_P_BATMAN)
  355. * @orig_dev: the original receive net device (e.g. bonded device)
  356. *
  357. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  358. */
  359. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  360. struct packet_type *ptype,
  361. struct net_device *orig_dev)
  362. {
  363. struct batadv_priv *bat_priv;
  364. struct batadv_ogm_packet *batadv_ogm_packet;
  365. struct batadv_hard_iface *hard_iface;
  366. u8 idx;
  367. hard_iface = container_of(ptype, struct batadv_hard_iface,
  368. batman_adv_ptype);
  369. /* Prevent processing a packet received on an interface which is getting
  370. * shut down otherwise the packet may trigger de-reference errors
  371. * further down in the receive path.
  372. */
  373. if (!kref_get_unless_zero(&hard_iface->refcount))
  374. goto err_out;
  375. skb = skb_share_check(skb, GFP_ATOMIC);
  376. /* skb was released by skb_share_check() */
  377. if (!skb)
  378. goto err_put;
  379. /* packet should hold at least type and version */
  380. if (unlikely(!pskb_may_pull(skb, 2)))
  381. goto err_free;
  382. /* expect a valid ethernet header here. */
  383. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  384. goto err_free;
  385. if (!hard_iface->soft_iface)
  386. goto err_free;
  387. bat_priv = netdev_priv(hard_iface->soft_iface);
  388. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  389. goto err_free;
  390. /* discard frames on not active interfaces */
  391. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  392. goto err_free;
  393. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  394. if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
  395. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  396. "Drop packet: incompatible batman version (%i)\n",
  397. batadv_ogm_packet->version);
  398. goto err_free;
  399. }
  400. /* reset control block to avoid left overs from previous users */
  401. memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
  402. idx = batadv_ogm_packet->packet_type;
  403. (*batadv_rx_handler[idx])(skb, hard_iface);
  404. batadv_hardif_put(hard_iface);
  405. /* return NET_RX_SUCCESS in any case as we
  406. * most probably dropped the packet for
  407. * routing-logical reasons.
  408. */
  409. return NET_RX_SUCCESS;
  410. err_free:
  411. kfree_skb(skb);
  412. err_put:
  413. batadv_hardif_put(hard_iface);
  414. err_out:
  415. return NET_RX_DROP;
  416. }
  417. static void batadv_recv_handler_init(void)
  418. {
  419. int i;
  420. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  421. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  422. for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
  423. batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
  424. /* compile time checks for sizes */
  425. BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
  426. BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
  427. BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
  428. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
  429. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
  430. BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
  431. BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
  432. BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
  433. BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
  434. BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
  435. BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
  436. BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
  437. BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
  438. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
  439. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
  440. BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
  441. i = sizeof_field(struct sk_buff, cb);
  442. BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i);
  443. /* broadcast packet */
  444. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  445. /* multicast packet */
  446. batadv_rx_handler[BATADV_MCAST] = batadv_recv_mcast_packet;
  447. /* unicast packets ... */
  448. /* unicast with 4 addresses packet */
  449. batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
  450. /* unicast packet */
  451. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  452. /* unicast tvlv packet */
  453. batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
  454. /* batman icmp packet */
  455. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  456. /* Fragmented packets */
  457. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
  458. }
  459. /**
  460. * batadv_recv_handler_register() - Register handler for batman-adv packet type
  461. * @packet_type: batadv_packettype which should be handled
  462. * @recv_handler: receive handler for the packet type
  463. *
  464. * Return: 0 on success or negative error number in case of failure
  465. */
  466. int
  467. batadv_recv_handler_register(u8 packet_type,
  468. int (*recv_handler)(struct sk_buff *,
  469. struct batadv_hard_iface *))
  470. {
  471. int (*curr)(struct sk_buff *skb,
  472. struct batadv_hard_iface *recv_if);
  473. curr = batadv_rx_handler[packet_type];
  474. if (curr != batadv_recv_unhandled_packet &&
  475. curr != batadv_recv_unhandled_unicast_packet)
  476. return -EBUSY;
  477. batadv_rx_handler[packet_type] = recv_handler;
  478. return 0;
  479. }
  480. /**
  481. * batadv_recv_handler_unregister() - Unregister handler for packet type
  482. * @packet_type: batadv_packettype which should no longer be handled
  483. */
  484. void batadv_recv_handler_unregister(u8 packet_type)
  485. {
  486. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  487. }
  488. /**
  489. * batadv_skb_crc32() - calculate CRC32 of the whole packet and skip bytes in
  490. * the header
  491. * @skb: skb pointing to fragmented socket buffers
  492. * @payload_ptr: Pointer to position inside the head buffer of the skb
  493. * marking the start of the data to be CRC'ed
  494. *
  495. * payload_ptr must always point to an address in the skb head buffer and not to
  496. * a fragment.
  497. *
  498. * Return: big endian crc32c of the checksummed data
  499. */
  500. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  501. {
  502. u32 crc = 0;
  503. unsigned int from;
  504. unsigned int to = skb->len;
  505. struct skb_seq_state st;
  506. const u8 *data;
  507. unsigned int len;
  508. unsigned int consumed = 0;
  509. from = (unsigned int)(payload_ptr - skb->data);
  510. skb_prepare_seq_read(skb, from, to, &st);
  511. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  512. crc = crc32c(crc, data, len);
  513. consumed += len;
  514. }
  515. return htonl(crc);
  516. }
  517. /**
  518. * batadv_get_vid() - extract the VLAN identifier from skb if any
  519. * @skb: the buffer containing the packet
  520. * @header_len: length of the batman header preceding the ethernet header
  521. *
  522. * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the
  523. * skb is vlan tagged. Otherwise BATADV_NO_FLAGS.
  524. */
  525. unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
  526. {
  527. struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
  528. struct vlan_ethhdr *vhdr;
  529. unsigned short vid;
  530. if (ethhdr->h_proto != htons(ETH_P_8021Q))
  531. return BATADV_NO_FLAGS;
  532. if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
  533. return BATADV_NO_FLAGS;
  534. vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
  535. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  536. vid |= BATADV_VLAN_HAS_TAG;
  537. return vid;
  538. }
  539. /**
  540. * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan
  541. * @bat_priv: the bat priv with all the soft interface information
  542. * @vid: the VLAN identifier for which the AP isolation attributed as to be
  543. * looked up
  544. *
  545. * Return: true if AP isolation is on for the VLAN identified by vid, false
  546. * otherwise
  547. */
  548. bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
  549. {
  550. bool ap_isolation_enabled = false;
  551. struct batadv_softif_vlan *vlan;
  552. /* if the AP isolation is requested on a VLAN, then check for its
  553. * setting in the proper VLAN private data structure
  554. */
  555. vlan = batadv_softif_vlan_get(bat_priv, vid);
  556. if (vlan) {
  557. ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
  558. batadv_softif_vlan_put(vlan);
  559. }
  560. return ap_isolation_enabled;
  561. }
  562. /**
  563. * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
  564. * @bat_priv: the bat priv with all the soft interface information
  565. * @type: subsystem type of event. Stored in uevent's BATTYPE
  566. * @action: action type of event. Stored in uevent's BATACTION
  567. * @data: string with additional information to the event (ignored for
  568. * BATADV_UEV_DEL). Stored in uevent's BATDATA
  569. *
  570. * Return: 0 on success or negative error number in case of failure
  571. */
  572. int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
  573. enum batadv_uev_action action, const char *data)
  574. {
  575. int ret = -ENOMEM;
  576. struct kobject *bat_kobj;
  577. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  578. bat_kobj = &bat_priv->soft_iface->dev.kobj;
  579. uevent_env[0] = kasprintf(GFP_ATOMIC,
  580. "%s%s", BATADV_UEV_TYPE_VAR,
  581. batadv_uev_type_str[type]);
  582. if (!uevent_env[0])
  583. goto report_error;
  584. uevent_env[1] = kasprintf(GFP_ATOMIC,
  585. "%s%s", BATADV_UEV_ACTION_VAR,
  586. batadv_uev_action_str[action]);
  587. if (!uevent_env[1])
  588. goto free_first_env;
  589. /* If the event is DEL, ignore the data field */
  590. if (action != BATADV_UEV_DEL) {
  591. uevent_env[2] = kasprintf(GFP_ATOMIC,
  592. "%s%s", BATADV_UEV_DATA_VAR, data);
  593. if (!uevent_env[2])
  594. goto free_second_env;
  595. }
  596. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  597. kfree(uevent_env[2]);
  598. free_second_env:
  599. kfree(uevent_env[1]);
  600. free_first_env:
  601. kfree(uevent_env[0]);
  602. if (ret)
  603. report_error:
  604. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  605. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  606. batadv_uev_type_str[type],
  607. batadv_uev_action_str[action],
  608. (action == BATADV_UEV_DEL ? "NULL" : data), ret);
  609. return ret;
  610. }
  611. module_init(batadv_init);
  612. module_exit(batadv_exit);
  613. MODULE_LICENSE("GPL");
  614. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  615. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  616. MODULE_VERSION(BATADV_SOURCE_VERSION);
  617. MODULE_ALIAS_RTNL_LINK("batadv");
  618. MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME);