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 */
batadv_init(void)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 */
batadv_exit(void)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 */
batadv_mesh_init(struct net_device * mesh_iface)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 */
batadv_mesh_free(struct net_device * mesh_iface)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 */
batadv_is_my_mac(struct batadv_priv * bat_priv,const u8 * addr)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 */
batadv_max_header_len(void)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 */
batadv_skb_set_priority(struct sk_buff * skb,int offset)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), ðhdr_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 */
batadv_recv_unhandled_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)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 */
batadv_batman_skb_recv(struct sk_buff * skb,struct net_device * dev,struct packet_type * ptype,struct net_device * orig_dev)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 /* Merged fragments re-enter here with reused skb metadata. */
496 skb->dev = dev;
497 skb->skb_iif = dev->ifindex;
498
499 /* packet should hold at least type and version */
500 if (unlikely(!pskb_may_pull(skb, 2)))
501 goto err_free;
502
503 /* expect a valid ethernet header here. */
504 if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
505 goto err_free;
506
507 bat_priv = netdev_priv(hard_iface->mesh_iface);
508
509 if (READ_ONCE(bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
510 goto err_free;
511
512 /* discard frames on not active interfaces */
513 if (hard_iface->if_status != BATADV_IF_ACTIVE)
514 goto err_free;
515
516 batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
517
518 if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
519 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
520 "Drop packet: incompatible batman version (%i)\n",
521 batadv_ogm_packet->version);
522 goto err_free;
523 }
524
525 /* reset control block to avoid left overs from previous users */
526 memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
527
528 idx = batadv_ogm_packet->packet_type;
529 (*batadv_rx_handler[idx])(skb, hard_iface);
530
531 batadv_hardif_put(hard_iface);
532
533 /* return NET_RX_SUCCESS in any case as we
534 * most probably dropped the packet for
535 * routing-logical reasons.
536 */
537 return NET_RX_SUCCESS;
538
539 err_free:
540 kfree_skb(skb);
541 err_put:
542 batadv_hardif_put(hard_iface);
543 err_out:
544 return NET_RX_DROP;
545 }
546
547 /**
548 * batadv_recv_handler_init() - initialise the RX handler dispatch table
549 *
550 * Initialise all entries of the RX handler table as either "unhandled" or with
551 * protocol indepentend handlers, and perform compile-time size sanity checks on
552 * all on-wire packet structs.
553 */
batadv_recv_handler_init(void)554 static void batadv_recv_handler_init(void)
555 {
556 int i;
557
558 for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
559 batadv_rx_handler[i] = batadv_recv_unhandled_packet;
560
561 for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
562 batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
563
564 /* compile time checks for sizes */
565 BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
566 BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
567 BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
568 BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
569 BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
570 BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
571 BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
572 BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
573 BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
574 BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
575 BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
576 BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
577 BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
578 BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
579 BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
580 BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
581
582 i = sizeof_field(struct sk_buff, cb);
583 BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i);
584
585 /* broadcast packet */
586 batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
587 /* multicast packet */
588 batadv_rx_handler[BATADV_MCAST] = batadv_recv_mcast_packet;
589
590 /* unicast packets ... */
591 /* unicast with 4 addresses packet */
592 batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
593 /* unicast packet */
594 batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
595 /* unicast tvlv packet */
596 batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
597 /* batman icmp packet */
598 batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
599 /* Fragmented packets */
600 batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
601 }
602
603 /**
604 * batadv_recv_handler_register() - Register handler for batman-adv packet type
605 * @packet_type: batadv_packettype which should be handled
606 * @recv_handler: receive handler for the packet type
607 *
608 * Return: 0 on success or negative error number in case of failure
609 */
610 int
batadv_recv_handler_register(u8 packet_type,int (* recv_handler)(struct sk_buff *,struct batadv_hard_iface *))611 batadv_recv_handler_register(u8 packet_type,
612 int (*recv_handler)(struct sk_buff *,
613 struct batadv_hard_iface *))
614 {
615 int (*curr)(struct sk_buff *skb,
616 struct batadv_hard_iface *recv_if);
617 curr = batadv_rx_handler[packet_type];
618
619 if (curr != batadv_recv_unhandled_packet &&
620 curr != batadv_recv_unhandled_unicast_packet)
621 return -EBUSY;
622
623 batadv_rx_handler[packet_type] = recv_handler;
624 return 0;
625 }
626
627 /**
628 * batadv_recv_handler_unregister() - Unregister handler for packet type
629 * @packet_type: batadv_packettype which should no longer be handled
630 */
batadv_recv_handler_unregister(u8 packet_type)631 void batadv_recv_handler_unregister(u8 packet_type)
632 {
633 batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
634 }
635
636 /**
637 * batadv_get_vid() - extract the VLAN identifier from skb if any
638 * @skb: the buffer containing the packet
639 * @header_len: length of the batman header preceding the ethernet header
640 *
641 * The caller must ensure that at least @header_len + ETH_HLEN bytes are
642 * accessible after skb->data.
643 *
644 * Warning: This function may reallocate the skb data buffer via
645 * pskb_may_pull()/... Any pointer into the skb data (e.g. obtained from skb->data
646 * or eth_hdr()) before this call must be considered invalid afterwards and has
647 * to be reacquired.
648 *
649 * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the
650 * skb is vlan tagged. Otherwise BATADV_NO_FLAGS.
651 */
batadv_get_vid(struct sk_buff * skb,size_t header_len)652 unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
653 {
654 struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
655 struct vlan_ethhdr *vhdr;
656 unsigned short vid;
657
658 if (ethhdr->h_proto != htons(ETH_P_8021Q))
659 return BATADV_NO_FLAGS;
660
661 if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
662 return BATADV_NO_FLAGS;
663
664 vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
665 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
666
667 /* VID 0 is only used to indicate "priority tag" frames which only
668 * contain priority information and no VID.
669 */
670 if (vid == 0)
671 return BATADV_NO_FLAGS;
672
673 vid |= BATADV_VLAN_HAS_TAG;
674
675 return vid;
676 }
677
678 /**
679 * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan
680 * @bat_priv: the bat priv with all the mesh interface information
681 * @vid: the VLAN identifier for which the AP isolation attributed as to be
682 * looked up
683 *
684 * Return: true if AP isolation is on for the VLAN identified by vid, false
685 * otherwise
686 */
batadv_vlan_ap_isola_get(struct batadv_priv * bat_priv,unsigned short vid)687 bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
688 {
689 bool ap_isolation_enabled = false;
690 struct batadv_meshif_vlan *vlan;
691
692 /* if the AP isolation is requested on a VLAN, then check for its
693 * setting in the proper VLAN private data structure
694 */
695 vlan = batadv_meshif_vlan_get(bat_priv, vid);
696 if (vlan) {
697 ap_isolation_enabled = READ_ONCE(vlan->ap_isolation);
698 batadv_meshif_vlan_put(vlan);
699 }
700
701 return ap_isolation_enabled;
702 }
703
704 /**
705 * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
706 * @bat_priv: the bat priv with all the mesh interface information
707 * @type: subsystem type of event. Stored in uevent's BATTYPE
708 * @action: action type of event. Stored in uevent's BATACTION
709 * @data: string with additional information to the event (ignored for
710 * BATADV_UEV_DEL). Stored in uevent's BATDATA
711 *
712 * Return: 0 on success or negative error number in case of failure
713 */
batadv_throw_uevent(struct batadv_priv * bat_priv,enum batadv_uev_type type,enum batadv_uev_action action,const char * data)714 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
715 enum batadv_uev_action action, const char *data)
716 {
717 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
718 struct kobject *bat_kobj;
719 int ret = -ENOMEM;
720
721 bat_kobj = &bat_priv->mesh_iface->dev.kobj;
722
723 uevent_env[0] = kasprintf(GFP_ATOMIC,
724 "%s%s", BATADV_UEV_TYPE_VAR,
725 batadv_uev_type_str[type]);
726 if (!uevent_env[0])
727 goto report_error;
728
729 uevent_env[1] = kasprintf(GFP_ATOMIC,
730 "%s%s", BATADV_UEV_ACTION_VAR,
731 batadv_uev_action_str[action]);
732 if (!uevent_env[1])
733 goto free_first_env;
734
735 /* If the event is DEL, ignore the data field */
736 if (action != BATADV_UEV_DEL) {
737 uevent_env[2] = kasprintf(GFP_ATOMIC,
738 "%s%s", BATADV_UEV_DATA_VAR, data);
739 if (!uevent_env[2])
740 goto free_second_env;
741 }
742
743 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
744 kfree(uevent_env[2]);
745 free_second_env:
746 kfree(uevent_env[1]);
747 free_first_env:
748 kfree(uevent_env[0]);
749
750 if (ret)
751 report_error:
752 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
753 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
754 batadv_uev_type_str[type],
755 batadv_uev_action_str[action],
756 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
757 return ret;
758 }
759
760 module_init(batadv_init);
761 module_exit(batadv_exit);
762
763 MODULE_LICENSE("GPL");
764
765 MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
766 MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
767 MODULE_ALIAS_RTNL_LINK("batadv");
768 MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME);
769