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