main.c 19 KB

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