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