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