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