1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "multicast.h"
20#include "main.h"
21
22#include <linux/atomic.h>
23#include <linux/bitops.h>
24#include <linux/bug.h>
25#include <linux/byteorder/generic.h>
26#include <linux/errno.h>
27#include <linux/etherdevice.h>
28#include <linux/gfp.h>
29#include <linux/icmpv6.h>
30#include <linux/if_bridge.h>
31#include <linux/if_ether.h>
32#include <linux/igmp.h>
33#include <linux/in.h>
34#include <linux/in6.h>
35#include <linux/ip.h>
36#include <linux/ipv6.h>
37#include <linux/jiffies.h>
38#include <linux/kernel.h>
39#include <linux/kref.h>
40#include <linux/list.h>
41#include <linux/lockdep.h>
42#include <linux/netdevice.h>
43#include <linux/netlink.h>
44#include <linux/printk.h>
45#include <linux/rculist.h>
46#include <linux/rcupdate.h>
47#include <linux/seq_file.h>
48#include <linux/skbuff.h>
49#include <linux/slab.h>
50#include <linux/spinlock.h>
51#include <linux/stddef.h>
52#include <linux/string.h>
53#include <linux/types.h>
54#include <linux/workqueue.h>
55#include <net/addrconf.h>
56#include <net/genetlink.h>
57#include <net/if_inet6.h>
58#include <net/ip.h>
59#include <net/ipv6.h>
60#include <net/netlink.h>
61#include <net/sock.h>
62#include <uapi/linux/batadv_packet.h>
63#include <uapi/linux/batman_adv.h>
64
65#include "hard-interface.h"
66#include "hash.h"
67#include "log.h"
68#include "netlink.h"
69#include "soft-interface.h"
70#include "translation-table.h"
71#include "tvlv.h"
72
73static void batadv_mcast_mla_update(struct work_struct *work);
74
75
76
77
78
79static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
80{
81 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
82 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
83}
84
85
86
87
88
89
90
91
92
93
94
95static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
96{
97 struct net_device *upper = soft_iface;
98
99 rcu_read_lock();
100 do {
101 upper = netdev_master_upper_dev_get_rcu(upper);
102 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
103
104 if (upper)
105 dev_hold(upper);
106 rcu_read_unlock();
107
108 return upper;
109}
110
111
112
113
114
115
116
117
118static bool batadv_mcast_addr_is_ipv4(const u8 *addr)
119{
120 static const u8 prefix[] = {0x01, 0x00, 0x5E};
121
122 return memcmp(prefix, addr, sizeof(prefix)) == 0;
123}
124
125
126
127
128
129
130
131
132static bool batadv_mcast_addr_is_ipv6(const u8 *addr)
133{
134 static const u8 prefix[] = {0x33, 0x33};
135
136 return memcmp(prefix, addr, sizeof(prefix)) == 0;
137}
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158static int batadv_mcast_mla_softif_get(struct batadv_priv *bat_priv,
159 struct net_device *dev,
160 struct hlist_head *mcast_list)
161{
162 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
163 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
164 struct net_device *bridge = batadv_mcast_get_bridge(dev);
165 struct netdev_hw_addr *mc_list_entry;
166 struct batadv_hw_addr *new;
167 int ret = 0;
168
169 netif_addr_lock_bh(bridge ? bridge : dev);
170 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
171 if (all_ipv4 && batadv_mcast_addr_is_ipv4(mc_list_entry->addr))
172 continue;
173
174 if (all_ipv6 && batadv_mcast_addr_is_ipv6(mc_list_entry->addr))
175 continue;
176
177 new = kmalloc(sizeof(*new), GFP_ATOMIC);
178 if (!new) {
179 ret = -ENOMEM;
180 break;
181 }
182
183 ether_addr_copy(new->addr, mc_list_entry->addr);
184 hlist_add_head(&new->list, mcast_list);
185 ret++;
186 }
187 netif_addr_unlock_bh(bridge ? bridge : dev);
188
189 if (bridge)
190 dev_put(bridge);
191
192 return ret;
193}
194
195
196
197
198
199
200
201
202
203static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
204 struct hlist_head *mcast_list)
205{
206 struct batadv_hw_addr *mcast_entry;
207
208 hlist_for_each_entry(mcast_entry, mcast_list, list)
209 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
210 return true;
211
212 return false;
213}
214
215
216
217
218
219
220
221
222
223
224
225
226
227static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
228{
229 if (src->proto == htons(ETH_P_IP))
230 ip_eth_mc_map(src->u.ip4, dst);
231#if IS_ENABLED(CONFIG_IPV6)
232 else if (src->proto == htons(ETH_P_IPV6))
233 ipv6_eth_mc_map(&src->u.ip6, dst);
234#endif
235 else
236 eth_zero_addr(dst);
237}
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253static int batadv_mcast_mla_bridge_get(struct batadv_priv *bat_priv,
254 struct net_device *dev,
255 struct hlist_head *mcast_list)
256{
257 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
258 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
259 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
260 struct br_ip_list *br_ip_entry, *tmp;
261 struct batadv_hw_addr *new;
262 u8 mcast_addr[ETH_ALEN];
263 int ret;
264
265
266
267
268 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
269 if (ret < 0)
270 goto out;
271
272 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
273 if (all_ipv4 && br_ip_entry->addr.proto == htons(ETH_P_IP))
274 continue;
275
276 if (all_ipv6 && br_ip_entry->addr.proto == htons(ETH_P_IPV6))
277 continue;
278
279 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
280 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
281 continue;
282
283 new = kmalloc(sizeof(*new), GFP_ATOMIC);
284 if (!new) {
285 ret = -ENOMEM;
286 break;
287 }
288
289 ether_addr_copy(new->addr, mcast_addr);
290 hlist_add_head(&new->list, mcast_list);
291 }
292
293out:
294 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
295 list_del(&br_ip_entry->list);
296 kfree(br_ip_entry);
297 }
298
299 return ret;
300}
301
302
303
304
305
306
307
308static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
309{
310 struct batadv_hw_addr *mcast_entry;
311 struct hlist_node *tmp;
312
313 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
314 hlist_del(&mcast_entry->list);
315 kfree(mcast_entry);
316 }
317}
318
319
320
321
322
323
324
325
326
327
328
329
330
331static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
332 struct hlist_head *mcast_list)
333{
334 struct batadv_hw_addr *mcast_entry;
335 struct hlist_node *tmp;
336
337 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
338
339 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
340 list) {
341 if (mcast_list &&
342 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
343 mcast_list))
344 continue;
345
346 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
347 BATADV_NO_FLAGS,
348 "mcast TT outdated", false);
349
350 hlist_del(&mcast_entry->list);
351 kfree(mcast_entry);
352 }
353}
354
355
356
357
358
359
360
361
362
363
364
365static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
366 struct hlist_head *mcast_list)
367{
368 struct batadv_hw_addr *mcast_entry;
369 struct hlist_node *tmp;
370
371 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
372
373 if (!mcast_list)
374 return;
375
376 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
377 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
378 &bat_priv->mcast.mla_list))
379 continue;
380
381 if (!batadv_tt_local_add(bat_priv->soft_iface,
382 mcast_entry->addr, BATADV_NO_FLAGS,
383 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
384 continue;
385
386 hlist_del(&mcast_entry->list);
387 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
388 }
389}
390
391
392
393
394
395
396
397
398
399static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
400{
401 struct net_device *upper = bat_priv->soft_iface;
402
403 rcu_read_lock();
404 do {
405 upper = netdev_master_upper_dev_get_rcu(upper);
406 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
407 rcu_read_unlock();
408
409 return upper;
410}
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434static void
435batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
436 struct batadv_mcast_querier_state *old_state,
437 struct batadv_mcast_querier_state *new_state)
438{
439 if (!old_state->exists && new_state->exists)
440 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
441 str_proto);
442 else if (old_state->exists && !new_state->exists)
443 batadv_info(bat_priv->soft_iface,
444 "%s Querier disappeared - multicast optimizations disabled\n",
445 str_proto);
446 else if (!bat_priv->mcast.bridged && !new_state->exists)
447 batadv_info(bat_priv->soft_iface,
448 "No %s Querier present - multicast optimizations disabled\n",
449 str_proto);
450
451 if (new_state->exists) {
452 if ((!old_state->shadowing && new_state->shadowing) ||
453 (!old_state->exists && new_state->shadowing))
454 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
455 "%s Querier is behind our bridged segment: Might shadow listeners\n",
456 str_proto);
457 else if (old_state->shadowing && !new_state->shadowing)
458 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
459 "%s Querier is not behind our bridged segment\n",
460 str_proto);
461 }
462}
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482static void
483batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
484 struct batadv_mcast_querier_state *querier_ipv4,
485 struct batadv_mcast_querier_state *querier_ipv6)
486{
487 if (!bat_priv->mcast.bridged && bridged)
488 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
489 "Bridge added: Setting Unsnoopables(U)-flag\n");
490 else if (bat_priv->mcast.bridged && !bridged)
491 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
492 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
493
494 if (bridged) {
495 batadv_mcast_querier_log(bat_priv, "IGMP",
496 &bat_priv->mcast.querier_ipv4,
497 querier_ipv4);
498 batadv_mcast_querier_log(bat_priv, "MLD",
499 &bat_priv->mcast.querier_ipv6,
500 querier_ipv6);
501 }
502}
503
504
505
506
507
508
509
510
511
512static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
513{
514 u8 old_flags = bat_priv->mcast.flags;
515 char str_old_flags[] = "[...]";
516
517 sprintf(str_old_flags, "[%c%c%c]",
518 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
519 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
520 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
521
522 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
523 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
524 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
525 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
526 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
527 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
528}
529
530
531
532
533
534
535
536
537
538
539
540static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
541{
542 struct batadv_tvlv_mcast_data mcast_data;
543 struct batadv_mcast_querier_state querier4 = {false, false};
544 struct batadv_mcast_querier_state querier6 = {false, false};
545 struct net_device *dev = bat_priv->soft_iface;
546 bool bridged;
547
548 mcast_data.flags = BATADV_NO_FLAGS;
549 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
550
551 bridged = batadv_mcast_has_bridge(bat_priv);
552 if (!bridged)
553 goto update;
554
555 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
556 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
557
558 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
559 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
560
561 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
562 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
563
564 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
565
566
567
568
569
570
571
572
573
574
575 if (!querier4.exists || querier4.shadowing)
576 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
577
578 if (!querier6.exists || querier6.shadowing)
579 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
580
581update:
582 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
583
584 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
585 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
586
587 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
588 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
589
590 bat_priv->mcast.bridged = bridged;
591
592 if (!bat_priv->mcast.enabled ||
593 mcast_data.flags != bat_priv->mcast.flags) {
594 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
595 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
596 &mcast_data, sizeof(mcast_data));
597 bat_priv->mcast.flags = mcast_data.flags;
598 bat_priv->mcast.enabled = true;
599 }
600
601 return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
602 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
603}
604
605
606
607
608
609
610
611
612
613
614
615
616
617static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
618{
619 struct net_device *soft_iface = bat_priv->soft_iface;
620 struct hlist_head mcast_list = HLIST_HEAD_INIT;
621 int ret;
622
623 if (!batadv_mcast_mla_tvlv_update(bat_priv))
624 goto update;
625
626 ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
627 if (ret < 0)
628 goto out;
629
630 ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
631 if (ret < 0)
632 goto out;
633
634update:
635 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
636 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
637
638out:
639 batadv_mcast_mla_list_free(&mcast_list);
640}
641
642
643
644
645
646
647
648
649
650
651static void batadv_mcast_mla_update(struct work_struct *work)
652{
653 struct delayed_work *delayed_work;
654 struct batadv_priv_mcast *priv_mcast;
655 struct batadv_priv *bat_priv;
656
657 delayed_work = to_delayed_work(work);
658 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
659 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
660
661 __batadv_mcast_mla_update(bat_priv);
662 batadv_mcast_start_timer(bat_priv);
663}
664
665
666
667
668
669
670
671
672
673
674
675static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
676{
677 if (ip_mc_check_igmp(skb, NULL) < 0)
678 return false;
679
680 switch (igmp_hdr(skb)->type) {
681 case IGMP_HOST_MEMBERSHIP_REPORT:
682 case IGMPV2_HOST_MEMBERSHIP_REPORT:
683 case IGMPV3_HOST_MEMBERSHIP_REPORT:
684 return true;
685 }
686
687 return false;
688}
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
704 struct sk_buff *skb,
705 bool *is_unsnoopable)
706{
707 struct iphdr *iphdr;
708
709
710 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
711 return -ENOMEM;
712
713 if (batadv_mcast_is_report_ipv4(skb))
714 return -EINVAL;
715
716 iphdr = ip_hdr(skb);
717
718
719
720
721 if (!ipv4_is_local_multicast(iphdr->daddr))
722 return -EINVAL;
723
724
725
726
727 *is_unsnoopable = true;
728
729 return 0;
730}
731
732
733
734
735
736
737
738
739
740
741
742static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
743{
744 if (ipv6_mc_check_mld(skb, NULL) < 0)
745 return false;
746
747 switch (icmp6_hdr(skb)->icmp6_type) {
748 case ICMPV6_MGM_REPORT:
749 case ICMPV6_MLD2_REPORT:
750 return true;
751 }
752
753 return false;
754}
755
756
757
758
759
760
761
762
763
764
765
766
767
768static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
769 struct sk_buff *skb,
770 bool *is_unsnoopable)
771{
772 struct ipv6hdr *ip6hdr;
773
774
775 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
776 return -ENOMEM;
777
778 if (batadv_mcast_is_report_ipv6(skb))
779 return -EINVAL;
780
781 ip6hdr = ipv6_hdr(skb);
782
783
784
785
786 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
787 return -EINVAL;
788
789
790
791
792 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
793 *is_unsnoopable = true;
794
795 return 0;
796}
797
798
799
800
801
802
803
804
805
806
807
808
809static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
810 struct sk_buff *skb,
811 bool *is_unsnoopable)
812{
813 struct ethhdr *ethhdr = eth_hdr(skb);
814
815 if (!atomic_read(&bat_priv->multicast_mode))
816 return -EINVAL;
817
818 if (atomic_read(&bat_priv->mcast.num_disabled))
819 return -EINVAL;
820
821 switch (ntohs(ethhdr->h_proto)) {
822 case ETH_P_IP:
823 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
824 is_unsnoopable);
825 case ETH_P_IPV6:
826 if (!IS_ENABLED(CONFIG_IPV6))
827 return -EINVAL;
828
829 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
830 is_unsnoopable);
831 default:
832 return -EINVAL;
833 }
834}
835
836
837
838
839
840
841
842
843
844
845
846static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
847 struct ethhdr *ethhdr)
848{
849 switch (ntohs(ethhdr->h_proto)) {
850 case ETH_P_IP:
851 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
852 case ETH_P_IPV6:
853 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
854 default:
855
856 return 0;
857 }
858}
859
860
861
862
863
864
865
866
867
868static struct batadv_orig_node *
869batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
870 struct ethhdr *ethhdr)
871{
872 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
873 BATADV_NO_FLAGS);
874}
875
876
877
878
879
880
881
882
883static struct batadv_orig_node *
884batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
885{
886 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
887
888 rcu_read_lock();
889 hlist_for_each_entry_rcu(tmp_orig_node,
890 &bat_priv->mcast.want_all_ipv4_list,
891 mcast_want_all_ipv4_node) {
892 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
893 continue;
894
895 orig_node = tmp_orig_node;
896 break;
897 }
898 rcu_read_unlock();
899
900 return orig_node;
901}
902
903
904
905
906
907
908
909
910static struct batadv_orig_node *
911batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
912{
913 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
914
915 rcu_read_lock();
916 hlist_for_each_entry_rcu(tmp_orig_node,
917 &bat_priv->mcast.want_all_ipv6_list,
918 mcast_want_all_ipv6_node) {
919 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
920 continue;
921
922 orig_node = tmp_orig_node;
923 break;
924 }
925 rcu_read_unlock();
926
927 return orig_node;
928}
929
930
931
932
933
934
935
936
937
938
939static struct batadv_orig_node *
940batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
941 struct ethhdr *ethhdr)
942{
943 switch (ntohs(ethhdr->h_proto)) {
944 case ETH_P_IP:
945 return batadv_mcast_forw_ipv4_node_get(bat_priv);
946 case ETH_P_IPV6:
947 return batadv_mcast_forw_ipv6_node_get(bat_priv);
948 default:
949
950 return NULL;
951 }
952}
953
954
955
956
957
958
959
960
961static struct batadv_orig_node *
962batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
963{
964 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
965
966 rcu_read_lock();
967 hlist_for_each_entry_rcu(tmp_orig_node,
968 &bat_priv->mcast.want_all_unsnoopables_list,
969 mcast_want_all_unsnoopables_node) {
970 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
971 continue;
972
973 orig_node = tmp_orig_node;
974 break;
975 }
976 rcu_read_unlock();
977
978 return orig_node;
979}
980
981
982
983
984
985
986
987
988
989
990
991enum batadv_forw_mode
992batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
993 struct batadv_orig_node **orig)
994{
995 int ret, tt_count, ip_count, unsnoop_count, total_count;
996 bool is_unsnoopable = false;
997 struct ethhdr *ethhdr;
998
999 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
1000 if (ret == -ENOMEM)
1001 return BATADV_FORW_NONE;
1002 else if (ret < 0)
1003 return BATADV_FORW_ALL;
1004
1005 ethhdr = eth_hdr(skb);
1006
1007 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1008 BATADV_NO_FLAGS);
1009 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1010 unsnoop_count = !is_unsnoopable ? 0 :
1011 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
1012
1013 total_count = tt_count + ip_count + unsnoop_count;
1014
1015 switch (total_count) {
1016 case 1:
1017 if (tt_count)
1018 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1019 else if (ip_count)
1020 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1021 else if (unsnoop_count)
1022 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1023
1024 if (*orig)
1025 return BATADV_FORW_SINGLE;
1026
1027
1028 case 0:
1029 return BATADV_FORW_NONE;
1030 default:
1031 return BATADV_FORW_ALL;
1032 }
1033}
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1047 struct batadv_orig_node *orig,
1048 u8 mcast_flags)
1049{
1050 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1051 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1052
1053 lockdep_assert_held(&orig->mcast_handler_lock);
1054
1055
1056 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1057 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1058 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1059
1060 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1061
1062 WARN_ON(!hlist_unhashed(node));
1063
1064 hlist_add_head_rcu(node, head);
1065 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1066
1067 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1068 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1069 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1070
1071 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1072
1073 WARN_ON(hlist_unhashed(node));
1074
1075 hlist_del_init_rcu(node);
1076 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1077 }
1078}
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1092 struct batadv_orig_node *orig,
1093 u8 mcast_flags)
1094{
1095 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1096 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1097
1098 lockdep_assert_held(&orig->mcast_handler_lock);
1099
1100
1101 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1102 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1103 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1104
1105 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1106
1107 WARN_ON(!hlist_unhashed(node));
1108
1109 hlist_add_head_rcu(node, head);
1110 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1111
1112 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1113 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1114 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1115
1116 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1117
1118 WARN_ON(hlist_unhashed(node));
1119
1120 hlist_del_init_rcu(node);
1121 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1122 }
1123}
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1137 struct batadv_orig_node *orig,
1138 u8 mcast_flags)
1139{
1140 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1141 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1142
1143 lockdep_assert_held(&orig->mcast_handler_lock);
1144
1145
1146 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1147 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1148 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1149
1150 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1151
1152 WARN_ON(!hlist_unhashed(node));
1153
1154 hlist_add_head_rcu(node, head);
1155 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1156
1157 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1158 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1159 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1160
1161 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1162
1163 WARN_ON(hlist_unhashed(node));
1164
1165 hlist_del_init_rcu(node);
1166 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1167 }
1168}
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1179 struct batadv_orig_node *orig,
1180 u8 flags,
1181 void *tvlv_value,
1182 u16 tvlv_value_len)
1183{
1184 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1185 u8 mcast_flags = BATADV_NO_FLAGS;
1186 bool orig_initialized;
1187
1188 if (orig_mcast_enabled && tvlv_value &&
1189 tvlv_value_len >= sizeof(mcast_flags))
1190 mcast_flags = *(u8 *)tvlv_value;
1191
1192 spin_lock_bh(&orig->mcast_handler_lock);
1193 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1194 &orig->capa_initialized);
1195
1196
1197
1198
1199
1200 if (orig_mcast_enabled &&
1201 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1202 if (orig_initialized)
1203 atomic_dec(&bat_priv->mcast.num_disabled);
1204 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1205
1206
1207
1208
1209 } else if (!orig_mcast_enabled &&
1210 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1211 !orig_initialized)) {
1212 atomic_inc(&bat_priv->mcast.num_disabled);
1213 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1214 }
1215
1216 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1217
1218 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1219 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1220 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1221
1222 orig->mcast_flags = mcast_flags;
1223 spin_unlock_bh(&orig->mcast_handler_lock);
1224}
1225
1226
1227
1228
1229
1230void batadv_mcast_init(struct batadv_priv *bat_priv)
1231{
1232 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1233 NULL, BATADV_TVLV_MCAST, 2,
1234 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1235
1236 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1237 batadv_mcast_start_timer(bat_priv);
1238}
1239
1240#ifdef CONFIG_BATMAN_ADV_DEBUGFS
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1251 struct seq_file *seq)
1252{
1253 u8 flags = bat_priv->mcast.flags;
1254 char querier4, querier6, shadowing4, shadowing6;
1255 bool bridged = bat_priv->mcast.bridged;
1256
1257 if (bridged) {
1258 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1259 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1260 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1261 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1262 } else {
1263 querier4 = '?';
1264 querier6 = '?';
1265 shadowing4 = '?';
1266 shadowing6 = '?';
1267 }
1268
1269 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1270 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1271 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1272 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1273 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1274 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1275 querier4, querier6);
1276 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1277 shadowing4, shadowing6);
1278 seq_puts(seq, "-------------------------------------------\n");
1279 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1280}
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1293{
1294 struct net_device *net_dev = (struct net_device *)seq->private;
1295 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1296 struct batadv_hard_iface *primary_if;
1297 struct batadv_hashtable *hash = bat_priv->orig_hash;
1298 struct batadv_orig_node *orig_node;
1299 struct hlist_head *head;
1300 u8 flags;
1301 u32 i;
1302
1303 primary_if = batadv_seq_print_text_primary_if_get(seq);
1304 if (!primary_if)
1305 return 0;
1306
1307 batadv_mcast_flags_print_header(bat_priv, seq);
1308
1309 for (i = 0; i < hash->size; i++) {
1310 head = &hash->table[i];
1311
1312 rcu_read_lock();
1313 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1314 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1315 &orig_node->capa_initialized))
1316 continue;
1317
1318 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1319 &orig_node->capabilities)) {
1320 seq_printf(seq, "%pM -\n", orig_node->orig);
1321 continue;
1322 }
1323
1324 flags = orig_node->mcast_flags;
1325
1326 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1327 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1328 ? 'U' : '.',
1329 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1330 ? '4' : '.',
1331 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1332 ? '6' : '.');
1333 }
1334 rcu_read_unlock();
1335 }
1336
1337 batadv_hardif_put(primary_if);
1338
1339 return 0;
1340}
1341#endif
1342
1343
1344
1345
1346
1347
1348
1349
1350int batadv_mcast_mesh_info_put(struct sk_buff *msg,
1351 struct batadv_priv *bat_priv)
1352{
1353 u32 flags = bat_priv->mcast.flags;
1354 u32 flags_priv = BATADV_NO_FLAGS;
1355
1356 if (bat_priv->mcast.bridged) {
1357 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
1358
1359 if (bat_priv->mcast.querier_ipv4.exists)
1360 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
1361 if (bat_priv->mcast.querier_ipv6.exists)
1362 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
1363 if (bat_priv->mcast.querier_ipv4.shadowing)
1364 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
1365 if (bat_priv->mcast.querier_ipv6.shadowing)
1366 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
1367 }
1368
1369 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
1370 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
1371 return -EMSGSIZE;
1372
1373 return 0;
1374}
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386static int
1387batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1388 struct batadv_orig_node *orig_node)
1389{
1390 void *hdr;
1391
1392 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1393 NLM_F_MULTI, BATADV_CMD_GET_MCAST_FLAGS);
1394 if (!hdr)
1395 return -ENOBUFS;
1396
1397 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1398 orig_node->orig)) {
1399 genlmsg_cancel(msg, hdr);
1400 return -EMSGSIZE;
1401 }
1402
1403 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1404 &orig_node->capabilities)) {
1405 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
1406 orig_node->mcast_flags)) {
1407 genlmsg_cancel(msg, hdr);
1408 return -EMSGSIZE;
1409 }
1410 }
1411
1412 genlmsg_end(msg, hdr);
1413 return 0;
1414}
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427static int
1428batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1429 struct hlist_head *head, long *idx_skip)
1430{
1431 struct batadv_orig_node *orig_node;
1432 long idx = 0;
1433
1434 rcu_read_lock();
1435 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1436 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1437 &orig_node->capa_initialized))
1438 continue;
1439
1440 if (idx < *idx_skip)
1441 goto skip;
1442
1443 if (batadv_mcast_flags_dump_entry(msg, portid, seq,
1444 orig_node)) {
1445 rcu_read_unlock();
1446 *idx_skip = idx;
1447
1448 return -EMSGSIZE;
1449 }
1450
1451skip:
1452 idx++;
1453 }
1454 rcu_read_unlock();
1455
1456 return 0;
1457}
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470static int
1471__batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid, u32 seq,
1472 struct batadv_priv *bat_priv, long *bucket, long *idx)
1473{
1474 struct batadv_hashtable *hash = bat_priv->orig_hash;
1475 long bucket_tmp = *bucket;
1476 struct hlist_head *head;
1477 long idx_tmp = *idx;
1478
1479 while (bucket_tmp < hash->size) {
1480 head = &hash->table[bucket_tmp];
1481
1482 if (batadv_mcast_flags_dump_bucket(msg, portid, seq, head,
1483 &idx_tmp))
1484 break;
1485
1486 bucket_tmp++;
1487 idx_tmp = 0;
1488 }
1489
1490 *bucket = bucket_tmp;
1491 *idx = idx_tmp;
1492
1493 return msg->len;
1494}
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504static int
1505batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
1506 struct batadv_hard_iface **primary_if)
1507{
1508 struct batadv_hard_iface *hard_iface = NULL;
1509 struct net *net = sock_net(cb->skb->sk);
1510 struct net_device *soft_iface;
1511 struct batadv_priv *bat_priv;
1512 int ifindex;
1513 int ret = 0;
1514
1515 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1516 if (!ifindex)
1517 return -EINVAL;
1518
1519 soft_iface = dev_get_by_index(net, ifindex);
1520 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1521 ret = -ENODEV;
1522 goto out;
1523 }
1524
1525 bat_priv = netdev_priv(soft_iface);
1526
1527 hard_iface = batadv_primary_if_get_selected(bat_priv);
1528 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
1529 ret = -ENOENT;
1530 goto out;
1531 }
1532
1533out:
1534 if (soft_iface)
1535 dev_put(soft_iface);
1536
1537 if (!ret && primary_if)
1538 *primary_if = hard_iface;
1539 else if (hard_iface)
1540 batadv_hardif_put(hard_iface);
1541
1542 return ret;
1543}
1544
1545
1546
1547
1548
1549
1550
1551
1552int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
1553{
1554 struct batadv_hard_iface *primary_if = NULL;
1555 int portid = NETLINK_CB(cb->skb).portid;
1556 struct batadv_priv *bat_priv;
1557 long *bucket = &cb->args[0];
1558 long *idx = &cb->args[1];
1559 int ret;
1560
1561 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
1562 if (ret)
1563 return ret;
1564
1565 bat_priv = netdev_priv(primary_if->soft_iface);
1566 ret = __batadv_mcast_flags_dump(msg, portid, cb->nlh->nlmsg_seq,
1567 bat_priv, bucket, idx);
1568
1569 batadv_hardif_put(primary_if);
1570 return ret;
1571}
1572
1573
1574
1575
1576
1577void batadv_mcast_free(struct batadv_priv *bat_priv)
1578{
1579 cancel_delayed_work_sync(&bat_priv->mcast.work);
1580
1581 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1582 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1583
1584
1585 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1586}
1587
1588
1589
1590
1591
1592void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1593{
1594 struct batadv_priv *bat_priv = orig->bat_priv;
1595
1596 spin_lock_bh(&orig->mcast_handler_lock);
1597
1598 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1599 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1600 atomic_dec(&bat_priv->mcast.num_disabled);
1601
1602 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1603 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1604 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1605
1606 spin_unlock_bh(&orig->mcast_handler_lock);
1607}
1608