xref: /linux/net/batman-adv/main.c (revision 9d19ca5d0e8b4a3f4b2eaa14e86a25f1c93ff35b)
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 	struct batadv_meshif_vlan *vlan;
263 
264 	WRITE_ONCE(bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
265 
266 	batadv_purge_outstanding_packets(bat_priv, NULL);
267 	batadv_tp_stop_all(bat_priv);
268 
269 	batadv_gw_node_free(bat_priv);
270 
271 	batadv_v_mesh_free(bat_priv);
272 	batadv_dat_free(bat_priv);
273 	batadv_bla_free(bat_priv);
274 
275 	batadv_mcast_free(bat_priv);
276 
277 	/* destroy the "untagged" VLAN */
278 	vlan = batadv_meshif_vlan_get(bat_priv, BATADV_NO_FLAGS);
279 	if (vlan) {
280 		batadv_meshif_destroy_vlan(bat_priv, vlan);
281 		batadv_meshif_vlan_put(vlan);
282 	}
283 
284 	/* Free the TT and the originator tables only after having terminated
285 	 * all the other depending components which may use these structures for
286 	 * their purposes.
287 	 */
288 	batadv_tt_free(bat_priv);
289 
290 	/* Since the originator table clean up routine is accessing the TT
291 	 * tables as well, it has to be invoked after the TT tables have been
292 	 * freed and marked as empty. This ensures that no cleanup RCU callbacks
293 	 * accessing the TT data are scheduled for later execution.
294 	 */
295 	batadv_originator_free(bat_priv);
296 
297 	batadv_gw_free(bat_priv);
298 
299 	free_percpu(bat_priv->bat_counters);
300 	bat_priv->bat_counters = NULL;
301 
302 	WRITE_ONCE(bat_priv->mesh_state, BATADV_MESH_INACTIVE);
303 }
304 
305 /**
306  * batadv_is_my_mac() - check if the given mac address belongs to any of the
307  *  real interfaces in the current mesh
308  * @bat_priv: the bat priv with all the mesh interface information
309  * @addr: the address to check
310  *
311  * Return: 'true' if the mac address was found, false otherwise.
312  */
313 bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr)
314 {
315 	const struct batadv_hard_iface *hard_iface;
316 	struct list_head *iter;
317 	bool is_my_mac = false;
318 
319 	rcu_read_lock();
320 	netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
321 		if (hard_iface->if_status != BATADV_IF_ACTIVE)
322 			continue;
323 
324 		if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
325 			is_my_mac = true;
326 			break;
327 		}
328 	}
329 	rcu_read_unlock();
330 	return is_my_mac;
331 }
332 
333 /**
334  * batadv_max_header_len() - calculate maximum encapsulation overhead for a
335  *  payload packet
336  *
337  * Return: the maximum encapsulation overhead in bytes.
338  */
339 int batadv_max_header_len(void)
340 {
341 	int header_len = 0;
342 
343 	header_len = max_t(int, header_len,
344 			   sizeof(struct batadv_unicast_packet));
345 	header_len = max_t(int, header_len,
346 			   sizeof(struct batadv_unicast_4addr_packet));
347 	header_len = max_t(int, header_len,
348 			   sizeof(struct batadv_bcast_packet));
349 
350 	return header_len + ETH_HLEN;
351 }
352 
353 /**
354  * batadv_skb_set_priority() - sets skb priority according to packet content
355  * @skb: the packet to be sent
356  * @offset: offset to the packet content
357  *
358  * This function sets a value between 256 and 263 (802.1d priority), which
359  * can be interpreted by the cfg80211 or other drivers.
360  */
361 void batadv_skb_set_priority(struct sk_buff *skb, int offset)
362 {
363 	struct iphdr ip_hdr_tmp, *ip_hdr;
364 	struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
365 	struct ethhdr ethhdr_tmp, *ethhdr;
366 	struct vlan_ethhdr *vhdr, vhdr_tmp;
367 	u32 prio;
368 
369 	/* already set, do nothing */
370 	if (skb->priority >= 256 && skb->priority <= 263)
371 		return;
372 
373 	ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), &ethhdr_tmp);
374 	if (!ethhdr)
375 		return;
376 
377 	switch (ethhdr->h_proto) {
378 	case htons(ETH_P_8021Q):
379 		vhdr = skb_header_pointer(skb, offset,
380 					  sizeof(*vhdr), &vhdr_tmp);
381 		if (!vhdr)
382 			return;
383 		prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
384 		prio = prio >> VLAN_PRIO_SHIFT;
385 		break;
386 	case htons(ETH_P_IP):
387 		ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
388 					    sizeof(*ip_hdr), &ip_hdr_tmp);
389 		if (!ip_hdr)
390 			return;
391 		prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
392 		break;
393 	case htons(ETH_P_IPV6):
394 		ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
395 					     sizeof(*ip6_hdr), &ip6_hdr_tmp);
396 		if (!ip6_hdr)
397 			return;
398 		prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
399 		break;
400 	default:
401 		return;
402 	}
403 
404 	skb->priority = prio + 256;
405 }
406 
407 static int batadv_recv_unhandled_packet(struct sk_buff *skb,
408 					struct batadv_hard_iface *recv_if)
409 {
410 	kfree_skb(skb);
411 
412 	return NET_RX_DROP;
413 }
414 
415 /* incoming packets with the batman ethertype received on any active hard
416  * interface
417  */
418 
419 /**
420  * batadv_batman_skb_recv() - Handle incoming message from an hard interface
421  * @skb: the received packet
422  * @dev: the net device that the packet was received on
423  * @ptype: packet type of incoming packet (ETH_P_BATMAN)
424  * @orig_dev: the original receive net device (e.g. bonded device)
425  *
426  * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
427  */
428 int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
429 			   struct packet_type *ptype,
430 			   struct net_device *orig_dev)
431 {
432 	struct batadv_priv *bat_priv;
433 	struct batadv_ogm_packet *batadv_ogm_packet;
434 	struct batadv_hard_iface *hard_iface;
435 	u8 idx;
436 
437 	hard_iface = container_of(ptype, struct batadv_hard_iface,
438 				  batman_adv_ptype);
439 
440 	/* Prevent processing a packet received on an interface which is getting
441 	 * shut down otherwise the packet may trigger de-reference errors
442 	 * further down in the receive path.
443 	 */
444 	if (!kref_get_unless_zero(&hard_iface->refcount))
445 		goto err_out;
446 
447 	skb = skb_share_check(skb, GFP_ATOMIC);
448 
449 	/* skb was released by skb_share_check() */
450 	if (!skb)
451 		goto err_put;
452 
453 	/* packet should hold at least type and version */
454 	if (unlikely(!pskb_may_pull(skb, 2)))
455 		goto err_free;
456 
457 	/* expect a valid ethernet header here. */
458 	if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
459 		goto err_free;
460 
461 	if (!hard_iface->mesh_iface)
462 		goto err_free;
463 
464 	bat_priv = netdev_priv(hard_iface->mesh_iface);
465 
466 	if (READ_ONCE(bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
467 		goto err_free;
468 
469 	/* discard frames on not active interfaces */
470 	if (hard_iface->if_status != BATADV_IF_ACTIVE)
471 		goto err_free;
472 
473 	batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
474 
475 	if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
476 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
477 			   "Drop packet: incompatible batman version (%i)\n",
478 			   batadv_ogm_packet->version);
479 		goto err_free;
480 	}
481 
482 	/* reset control block to avoid left overs from previous users */
483 	memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
484 
485 	idx = batadv_ogm_packet->packet_type;
486 	(*batadv_rx_handler[idx])(skb, hard_iface);
487 
488 	batadv_hardif_put(hard_iface);
489 
490 	/* return NET_RX_SUCCESS in any case as we
491 	 * most probably dropped the packet for
492 	 * routing-logical reasons.
493 	 */
494 	return NET_RX_SUCCESS;
495 
496 err_free:
497 	kfree_skb(skb);
498 err_put:
499 	batadv_hardif_put(hard_iface);
500 err_out:
501 	return NET_RX_DROP;
502 }
503 
504 static void batadv_recv_handler_init(void)
505 {
506 	int i;
507 
508 	for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
509 		batadv_rx_handler[i] = batadv_recv_unhandled_packet;
510 
511 	for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
512 		batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
513 
514 	/* compile time checks for sizes */
515 	BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
516 	BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
517 	BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
518 	BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
519 	BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
520 	BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
521 	BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
522 	BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
523 	BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
524 	BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
525 	BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
526 	BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
527 	BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
528 	BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
529 	BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
530 	BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
531 
532 	i = sizeof_field(struct sk_buff, cb);
533 	BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i);
534 
535 	/* broadcast packet */
536 	batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
537 	/* multicast packet */
538 	batadv_rx_handler[BATADV_MCAST] = batadv_recv_mcast_packet;
539 
540 	/* unicast packets ... */
541 	/* unicast with 4 addresses packet */
542 	batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
543 	/* unicast packet */
544 	batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
545 	/* unicast tvlv packet */
546 	batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
547 	/* batman icmp packet */
548 	batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
549 	/* Fragmented packets */
550 	batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
551 }
552 
553 /**
554  * batadv_recv_handler_register() - Register handler for batman-adv packet type
555  * @packet_type: batadv_packettype which should be handled
556  * @recv_handler: receive handler for the packet type
557  *
558  * Return: 0 on success or negative error number in case of failure
559  */
560 int
561 batadv_recv_handler_register(u8 packet_type,
562 			     int (*recv_handler)(struct sk_buff *,
563 						 struct batadv_hard_iface *))
564 {
565 	int (*curr)(struct sk_buff *skb,
566 		    struct batadv_hard_iface *recv_if);
567 	curr = batadv_rx_handler[packet_type];
568 
569 	if (curr != batadv_recv_unhandled_packet &&
570 	    curr != batadv_recv_unhandled_unicast_packet)
571 		return -EBUSY;
572 
573 	batadv_rx_handler[packet_type] = recv_handler;
574 	return 0;
575 }
576 
577 /**
578  * batadv_recv_handler_unregister() - Unregister handler for packet type
579  * @packet_type: batadv_packettype which should no longer be handled
580  */
581 void batadv_recv_handler_unregister(u8 packet_type)
582 {
583 	batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
584 }
585 
586 /**
587  * batadv_get_vid() - extract the VLAN identifier from skb if any
588  * @skb: the buffer containing the packet
589  * @header_len: length of the batman header preceding the ethernet header
590  *
591  * The caller must ensure that at least @header_len + ETH_HLEN bytes are
592  * accessible after skb->data.
593  *
594  * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the
595  * skb is vlan tagged. Otherwise BATADV_NO_FLAGS.
596  */
597 unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
598 {
599 	struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
600 	struct vlan_ethhdr *vhdr;
601 	unsigned short vid;
602 
603 	if (ethhdr->h_proto != htons(ETH_P_8021Q))
604 		return BATADV_NO_FLAGS;
605 
606 	if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
607 		return BATADV_NO_FLAGS;
608 
609 	vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
610 	vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
611 
612 	/* VID 0 is only used to indicate "priority tag" frames which only
613 	 * contain priority information and no VID.
614 	 */
615 	if (vid == 0)
616 		return BATADV_NO_FLAGS;
617 
618 	vid |= BATADV_VLAN_HAS_TAG;
619 
620 	return vid;
621 }
622 
623 /**
624  * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan
625  * @bat_priv: the bat priv with all the mesh interface information
626  * @vid: the VLAN identifier for which the AP isolation attributed as to be
627  *  looked up
628  *
629  * Return: true if AP isolation is on for the VLAN identified by vid, false
630  * otherwise
631  */
632 bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
633 {
634 	bool ap_isolation_enabled = false;
635 	struct batadv_meshif_vlan *vlan;
636 
637 	/* if the AP isolation is requested on a VLAN, then check for its
638 	 * setting in the proper VLAN private data structure
639 	 */
640 	vlan = batadv_meshif_vlan_get(bat_priv, vid);
641 	if (vlan) {
642 		ap_isolation_enabled = READ_ONCE(vlan->ap_isolation);
643 		batadv_meshif_vlan_put(vlan);
644 	}
645 
646 	return ap_isolation_enabled;
647 }
648 
649 /**
650  * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
651  * @bat_priv: the bat priv with all the mesh interface information
652  * @type: subsystem type of event. Stored in uevent's BATTYPE
653  * @action: action type of event. Stored in uevent's BATACTION
654  * @data: string with additional information to the event (ignored for
655  *  BATADV_UEV_DEL). Stored in uevent's BATDATA
656  *
657  * Return: 0 on success or negative error number in case of failure
658  */
659 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
660 			enum batadv_uev_action action, const char *data)
661 {
662 	int ret = -ENOMEM;
663 	struct kobject *bat_kobj;
664 	char *uevent_env[4] = { NULL, NULL, NULL, NULL };
665 
666 	bat_kobj = &bat_priv->mesh_iface->dev.kobj;
667 
668 	uevent_env[0] = kasprintf(GFP_ATOMIC,
669 				  "%s%s", BATADV_UEV_TYPE_VAR,
670 				  batadv_uev_type_str[type]);
671 	if (!uevent_env[0])
672 		goto report_error;
673 
674 	uevent_env[1] = kasprintf(GFP_ATOMIC,
675 				  "%s%s", BATADV_UEV_ACTION_VAR,
676 				  batadv_uev_action_str[action]);
677 	if (!uevent_env[1])
678 		goto free_first_env;
679 
680 	/* If the event is DEL, ignore the data field */
681 	if (action != BATADV_UEV_DEL) {
682 		uevent_env[2] = kasprintf(GFP_ATOMIC,
683 					  "%s%s", BATADV_UEV_DATA_VAR, data);
684 		if (!uevent_env[2])
685 			goto free_second_env;
686 	}
687 
688 	ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
689 	kfree(uevent_env[2]);
690 free_second_env:
691 	kfree(uevent_env[1]);
692 free_first_env:
693 	kfree(uevent_env[0]);
694 
695 	if (ret)
696 report_error:
697 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
698 			   "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
699 			   batadv_uev_type_str[type],
700 			   batadv_uev_action_str[action],
701 			   (action == BATADV_UEV_DEL ? "NULL" : data), ret);
702 	return ret;
703 }
704 
705 module_init(batadv_init);
706 module_exit(batadv_exit);
707 
708 MODULE_LICENSE("GPL");
709 
710 MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
711 MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
712 MODULE_ALIAS_RTNL_LINK("batadv");
713 MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME);
714