1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2014-2019 B.A.T.M.A.N. contributors:
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/errno.h>
15 #include <linux/etherdevice.h>
16 #include <linux/gfp.h>
17 #include <linux/icmpv6.h>
18 #include <linux/if_bridge.h>
19 #include <linux/if_ether.h>
20 #include <linux/igmp.h>
22 #include <linux/in6.h>
23 #include <linux/inetdevice.h>
25 #include <linux/ipv6.h>
26 #include <linux/jiffies.h>
27 #include <linux/kernel.h>
28 #include <linux/kref.h>
29 #include <linux/list.h>
30 #include <linux/lockdep.h>
31 #include <linux/netdevice.h>
32 #include <linux/netlink.h>
33 #include <linux/printk.h>
34 #include <linux/rculist.h>
35 #include <linux/rcupdate.h>
36 #include <linux/seq_file.h>
37 #include <linux/skbuff.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40 #include <linux/stddef.h>
41 #include <linux/string.h>
42 #include <linux/types.h>
43 #include <linux/workqueue.h>
44 #include <net/addrconf.h>
45 #include <net/genetlink.h>
46 #include <net/if_inet6.h>
49 #include <net/netlink.h>
51 #include <uapi/linux/batadv_packet.h>
52 #include <uapi/linux/batman_adv.h>
54 #include "hard-interface.h"
59 #include "soft-interface.h"
60 #include "translation-table.h"
63 static void batadv_mcast_mla_update(struct work_struct
*work
);
66 * batadv_mcast_start_timer() - schedule the multicast periodic worker
67 * @bat_priv: the bat priv with all the soft interface information
69 static void batadv_mcast_start_timer(struct batadv_priv
*bat_priv
)
71 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->mcast
.work
,
72 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD
));
76 * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
77 * @bat_priv: the bat priv with all the soft interface information
79 * Checks whether there is a bridge on top of our soft interface.
81 * Return: true if there is a bridge, false otherwise.
83 static bool batadv_mcast_has_bridge(struct batadv_priv
*bat_priv
)
85 struct net_device
*upper
= bat_priv
->soft_iface
;
89 upper
= netdev_master_upper_dev_get_rcu(upper
);
90 } while (upper
&& !(upper
->priv_flags
& IFF_EBRIDGE
));
97 * batadv_mcast_mla_flags_get() - get the new multicast flags
98 * @bat_priv: the bat priv with all the soft interface information
100 * Return: A set of flags for the current/next TVLV, querier and
103 static struct batadv_mcast_mla_flags
104 batadv_mcast_mla_flags_get(struct batadv_priv
*bat_priv
)
106 struct net_device
*dev
= bat_priv
->soft_iface
;
107 struct batadv_mcast_querier_state
*qr4
, *qr6
;
108 struct batadv_mcast_mla_flags mla_flags
;
110 memset(&mla_flags
, 0, sizeof(mla_flags
));
111 mla_flags
.enabled
= 1;
113 if (!batadv_mcast_has_bridge(bat_priv
))
116 mla_flags
.bridged
= 1;
117 qr4
= &mla_flags
.querier_ipv4
;
118 qr6
= &mla_flags
.querier_ipv6
;
120 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING
))
121 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
123 qr4
->exists
= br_multicast_has_querier_anywhere(dev
, ETH_P_IP
);
124 qr4
->shadowing
= br_multicast_has_querier_adjacent(dev
, ETH_P_IP
);
126 qr6
->exists
= br_multicast_has_querier_anywhere(dev
, ETH_P_IPV6
);
127 qr6
->shadowing
= br_multicast_has_querier_adjacent(dev
, ETH_P_IPV6
);
129 mla_flags
.tvlv_flags
|= BATADV_MCAST_WANT_ALL_UNSNOOPABLES
;
131 /* 1) If no querier exists at all, then multicast listeners on
132 * our local TT clients behind the bridge will keep silent.
133 * 2) If the selected querier is on one of our local TT clients,
134 * behind the bridge, then this querier might shadow multicast
135 * listeners on our local TT clients, behind this bridge.
137 * In both cases, we will signalize other batman nodes that
138 * we need all multicast traffic of the according protocol.
140 if (!qr4
->exists
|| qr4
->shadowing
)
141 mla_flags
.tvlv_flags
|= BATADV_MCAST_WANT_ALL_IPV4
;
143 if (!qr6
->exists
|| qr6
->shadowing
)
144 mla_flags
.tvlv_flags
|= BATADV_MCAST_WANT_ALL_IPV6
;
150 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
151 * @soft_iface: netdev struct of the mesh interface
153 * If the given soft interface has a bridge on top then the refcount
154 * of the according net device is increased.
156 * Return: NULL if no such bridge exists. Otherwise the net device of the
159 static struct net_device
*batadv_mcast_get_bridge(struct net_device
*soft_iface
)
161 struct net_device
*upper
= soft_iface
;
165 upper
= netdev_master_upper_dev_get_rcu(upper
);
166 } while (upper
&& !(upper
->priv_flags
& IFF_EBRIDGE
));
176 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
177 * @mcast_addr: the multicast address to check
178 * @mcast_list: the list with multicast addresses to search in
180 * Return: true if the given address is already in the given list.
181 * Otherwise returns false.
183 static bool batadv_mcast_mla_is_duplicate(u8
*mcast_addr
,
184 struct hlist_head
*mcast_list
)
186 struct batadv_hw_addr
*mcast_entry
;
188 hlist_for_each_entry(mcast_entry
, mcast_list
, list
)
189 if (batadv_compare_eth(mcast_entry
->addr
, mcast_addr
))
196 * batadv_mcast_mla_softif_get_ipv4() - get softif IPv4 multicast listeners
197 * @dev: the device to collect multicast addresses from
198 * @mcast_list: a list to put found addresses into
199 * @flags: flags indicating the new multicast state
201 * Collects multicast addresses of IPv4 multicast listeners residing
202 * on this kernel on the given soft interface, dev, in
203 * the given mcast_list. In general, multicast listeners provided by
204 * your multicast receiving applications run directly on this node.
206 * Return: -ENOMEM on memory allocation error or the number of
207 * items added to the mcast_list otherwise.
210 batadv_mcast_mla_softif_get_ipv4(struct net_device
*dev
,
211 struct hlist_head
*mcast_list
,
212 struct batadv_mcast_mla_flags
*flags
)
214 struct batadv_hw_addr
*new;
215 struct in_device
*in_dev
;
216 u8 mcast_addr
[ETH_ALEN
];
217 struct ip_mc_list
*pmc
;
220 if (flags
->tvlv_flags
& BATADV_MCAST_WANT_ALL_IPV4
)
225 in_dev
= __in_dev_get_rcu(dev
);
231 for (pmc
= rcu_dereference(in_dev
->mc_list
); pmc
;
232 pmc
= rcu_dereference(pmc
->next_rcu
)) {
233 ip_eth_mc_map(pmc
->multiaddr
, mcast_addr
);
235 if (batadv_mcast_mla_is_duplicate(mcast_addr
, mcast_list
))
238 new = kmalloc(sizeof(*new), GFP_ATOMIC
);
244 ether_addr_copy(new->addr
, mcast_addr
);
245 hlist_add_head(&new->list
, mcast_list
);
254 * batadv_mcast_mla_softif_get_ipv6() - get softif IPv6 multicast listeners
255 * @dev: the device to collect multicast addresses from
256 * @mcast_list: a list to put found addresses into
257 * @flags: flags indicating the new multicast state
259 * Collects multicast addresses of IPv6 multicast listeners residing
260 * on this kernel on the given soft interface, dev, in
261 * the given mcast_list. In general, multicast listeners provided by
262 * your multicast receiving applications run directly on this node.
264 * Return: -ENOMEM on memory allocation error or the number of
265 * items added to the mcast_list otherwise.
267 #if IS_ENABLED(CONFIG_IPV6)
269 batadv_mcast_mla_softif_get_ipv6(struct net_device
*dev
,
270 struct hlist_head
*mcast_list
,
271 struct batadv_mcast_mla_flags
*flags
)
273 struct batadv_hw_addr
*new;
274 struct inet6_dev
*in6_dev
;
275 u8 mcast_addr
[ETH_ALEN
];
276 struct ifmcaddr6
*pmc6
;
279 if (flags
->tvlv_flags
& BATADV_MCAST_WANT_ALL_IPV6
)
284 in6_dev
= __in6_dev_get(dev
);
290 read_lock_bh(&in6_dev
->lock
);
291 for (pmc6
= in6_dev
->mc_list
; pmc6
; pmc6
= pmc6
->next
) {
292 if (IPV6_ADDR_MC_SCOPE(&pmc6
->mca_addr
) <
293 IPV6_ADDR_SCOPE_LINKLOCAL
)
296 ipv6_eth_mc_map(&pmc6
->mca_addr
, mcast_addr
);
298 if (batadv_mcast_mla_is_duplicate(mcast_addr
, mcast_list
))
301 new = kmalloc(sizeof(*new), GFP_ATOMIC
);
307 ether_addr_copy(new->addr
, mcast_addr
);
308 hlist_add_head(&new->list
, mcast_list
);
311 read_unlock_bh(&in6_dev
->lock
);
318 batadv_mcast_mla_softif_get_ipv6(struct net_device
*dev
,
319 struct hlist_head
*mcast_list
,
320 struct batadv_mcast_mla_flags
*flags
)
327 * batadv_mcast_mla_softif_get() - get softif multicast listeners
328 * @dev: the device to collect multicast addresses from
329 * @mcast_list: a list to put found addresses into
330 * @flags: flags indicating the new multicast state
332 * Collects multicast addresses of multicast listeners residing
333 * on this kernel on the given soft interface, dev, in
334 * the given mcast_list. In general, multicast listeners provided by
335 * your multicast receiving applications run directly on this node.
337 * If there is a bridge interface on top of dev, collects from that one
338 * instead. Just like with IP addresses and routes, multicast listeners
339 * will(/should) register to the bridge interface instead of an
342 * Return: -ENOMEM on memory allocation error or the number of
343 * items added to the mcast_list otherwise.
346 batadv_mcast_mla_softif_get(struct net_device
*dev
,
347 struct hlist_head
*mcast_list
,
348 struct batadv_mcast_mla_flags
*flags
)
350 struct net_device
*bridge
= batadv_mcast_get_bridge(dev
);
356 ret4
= batadv_mcast_mla_softif_get_ipv4(dev
, mcast_list
, flags
);
360 ret6
= batadv_mcast_mla_softif_get_ipv6(dev
, mcast_list
, flags
);
374 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
375 * @dst: destination to write to - a multicast MAC address
376 * @src: source to read from - a multicast IP address
378 * Converts a given multicast IPv4/IPv6 address from a bridge
379 * to its matching multicast MAC address and copies it into the given
380 * destination buffer.
382 * Caller needs to make sure the destination buffer can hold
383 * at least ETH_ALEN bytes.
385 static void batadv_mcast_mla_br_addr_cpy(char *dst
, const struct br_ip
*src
)
387 if (src
->proto
== htons(ETH_P_IP
))
388 ip_eth_mc_map(src
->u
.ip4
, dst
);
389 #if IS_ENABLED(CONFIG_IPV6)
390 else if (src
->proto
== htons(ETH_P_IPV6
))
391 ipv6_eth_mc_map(&src
->u
.ip6
, dst
);
398 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
399 * @dev: a bridge slave whose bridge to collect multicast addresses from
400 * @mcast_list: a list to put found addresses into
401 * @flags: flags indicating the new multicast state
403 * Collects multicast addresses of multicast listeners residing
404 * on foreign, non-mesh devices which we gave access to our mesh via
405 * a bridge on top of the given soft interface, dev, in the given
408 * Return: -ENOMEM on memory allocation error or the number of
409 * items added to the mcast_list otherwise.
411 static int batadv_mcast_mla_bridge_get(struct net_device
*dev
,
412 struct hlist_head
*mcast_list
,
413 struct batadv_mcast_mla_flags
*flags
)
415 struct list_head bridge_mcast_list
= LIST_HEAD_INIT(bridge_mcast_list
);
416 bool all_ipv4
= flags
->tvlv_flags
& BATADV_MCAST_WANT_ALL_IPV4
;
417 bool all_ipv6
= flags
->tvlv_flags
& BATADV_MCAST_WANT_ALL_IPV6
;
418 struct br_ip_list
*br_ip_entry
, *tmp
;
419 struct batadv_hw_addr
*new;
420 u8 mcast_addr
[ETH_ALEN
];
423 /* we don't need to detect these devices/listeners, the IGMP/MLD
424 * snooping code of the Linux bridge already does that for us
426 ret
= br_multicast_list_adjacent(dev
, &bridge_mcast_list
);
430 list_for_each_entry(br_ip_entry
, &bridge_mcast_list
, list
) {
431 if (all_ipv4
&& br_ip_entry
->addr
.proto
== htons(ETH_P_IP
))
434 if (all_ipv6
&& br_ip_entry
->addr
.proto
== htons(ETH_P_IPV6
))
437 batadv_mcast_mla_br_addr_cpy(mcast_addr
, &br_ip_entry
->addr
);
438 if (batadv_mcast_mla_is_duplicate(mcast_addr
, mcast_list
))
441 new = kmalloc(sizeof(*new), GFP_ATOMIC
);
447 ether_addr_copy(new->addr
, mcast_addr
);
448 hlist_add_head(&new->list
, mcast_list
);
452 list_for_each_entry_safe(br_ip_entry
, tmp
, &bridge_mcast_list
, list
) {
453 list_del(&br_ip_entry
->list
);
461 * batadv_mcast_mla_list_free() - free a list of multicast addresses
462 * @mcast_list: the list to free
464 * Removes and frees all items in the given mcast_list.
466 static void batadv_mcast_mla_list_free(struct hlist_head
*mcast_list
)
468 struct batadv_hw_addr
*mcast_entry
;
469 struct hlist_node
*tmp
;
471 hlist_for_each_entry_safe(mcast_entry
, tmp
, mcast_list
, list
) {
472 hlist_del(&mcast_entry
->list
);
478 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
479 * @bat_priv: the bat priv with all the soft interface information
480 * @mcast_list: a list of addresses which should _not_ be removed
482 * Retracts the announcement of any multicast listener from the
483 * translation table except the ones listed in the given mcast_list.
485 * If mcast_list is NULL then all are retracted.
487 static void batadv_mcast_mla_tt_retract(struct batadv_priv
*bat_priv
,
488 struct hlist_head
*mcast_list
)
490 struct batadv_hw_addr
*mcast_entry
;
491 struct hlist_node
*tmp
;
493 hlist_for_each_entry_safe(mcast_entry
, tmp
, &bat_priv
->mcast
.mla_list
,
496 batadv_mcast_mla_is_duplicate(mcast_entry
->addr
,
500 batadv_tt_local_remove(bat_priv
, mcast_entry
->addr
,
502 "mcast TT outdated", false);
504 hlist_del(&mcast_entry
->list
);
510 * batadv_mcast_mla_tt_add() - add multicast listener announcements
511 * @bat_priv: the bat priv with all the soft interface information
512 * @mcast_list: a list of addresses which are going to get added
514 * Adds multicast listener announcements from the given mcast_list to the
515 * translation table if they have not been added yet.
517 static void batadv_mcast_mla_tt_add(struct batadv_priv
*bat_priv
,
518 struct hlist_head
*mcast_list
)
520 struct batadv_hw_addr
*mcast_entry
;
521 struct hlist_node
*tmp
;
526 hlist_for_each_entry_safe(mcast_entry
, tmp
, mcast_list
, list
) {
527 if (batadv_mcast_mla_is_duplicate(mcast_entry
->addr
,
528 &bat_priv
->mcast
.mla_list
))
531 if (!batadv_tt_local_add(bat_priv
->soft_iface
,
532 mcast_entry
->addr
, BATADV_NO_FLAGS
,
533 BATADV_NULL_IFINDEX
, BATADV_NO_MARK
))
536 hlist_del(&mcast_entry
->list
);
537 hlist_add_head(&mcast_entry
->list
, &bat_priv
->mcast
.mla_list
);
542 * batadv_mcast_querier_log() - debug output regarding the querier status on
544 * @bat_priv: the bat priv with all the soft interface information
545 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
546 * @old_state: the previous querier state on our link
547 * @new_state: the new querier state on our link
549 * Outputs debug messages to the logging facility with log level 'mcast'
550 * regarding changes to the querier status on the link which are relevant
551 * to our multicast optimizations.
553 * Usually this is about whether a querier appeared or vanished in
554 * our mesh or whether the querier is in the suboptimal position of being
555 * behind our local bridge segment: Snooping switches will directly
556 * forward listener reports to the querier, therefore batman-adv and
557 * the bridge will potentially not see these listeners - the querier is
558 * potentially shadowing listeners from us then.
560 * This is only interesting for nodes with a bridge on top of their
564 batadv_mcast_querier_log(struct batadv_priv
*bat_priv
, char *str_proto
,
565 struct batadv_mcast_querier_state
*old_state
,
566 struct batadv_mcast_querier_state
*new_state
)
568 if (!old_state
->exists
&& new_state
->exists
)
569 batadv_info(bat_priv
->soft_iface
, "%s Querier appeared\n",
571 else if (old_state
->exists
&& !new_state
->exists
)
572 batadv_info(bat_priv
->soft_iface
,
573 "%s Querier disappeared - multicast optimizations disabled\n",
575 else if (!bat_priv
->mcast
.mla_flags
.bridged
&& !new_state
->exists
)
576 batadv_info(bat_priv
->soft_iface
,
577 "No %s Querier present - multicast optimizations disabled\n",
580 if (new_state
->exists
) {
581 if ((!old_state
->shadowing
&& new_state
->shadowing
) ||
582 (!old_state
->exists
&& new_state
->shadowing
))
583 batadv_dbg(BATADV_DBG_MCAST
, bat_priv
,
584 "%s Querier is behind our bridged segment: Might shadow listeners\n",
586 else if (old_state
->shadowing
&& !new_state
->shadowing
)
587 batadv_dbg(BATADV_DBG_MCAST
, bat_priv
,
588 "%s Querier is not behind our bridged segment\n",
594 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
596 * @bat_priv: the bat priv with all the soft interface information
597 * @new_flags: flags indicating the new multicast state
599 * If no bridges are ever used on this node, then this function does nothing.
601 * Otherwise this function outputs debug information to the 'mcast' log level
602 * which might be relevant to our multicast optimizations.
604 * More precisely, it outputs information when a bridge interface is added or
605 * removed from a soft interface. And when a bridge is present, it further
606 * outputs information about the querier state which is relevant for the
607 * multicast flags this node is going to set.
610 batadv_mcast_bridge_log(struct batadv_priv
*bat_priv
,
611 struct batadv_mcast_mla_flags
*new_flags
)
613 struct batadv_mcast_mla_flags
*old_flags
= &bat_priv
->mcast
.mla_flags
;
615 if (!old_flags
->bridged
&& new_flags
->bridged
)
616 batadv_dbg(BATADV_DBG_MCAST
, bat_priv
,
617 "Bridge added: Setting Unsnoopables(U)-flag\n");
618 else if (old_flags
->bridged
&& !new_flags
->bridged
)
619 batadv_dbg(BATADV_DBG_MCAST
, bat_priv
,
620 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
622 if (new_flags
->bridged
) {
623 batadv_mcast_querier_log(bat_priv
, "IGMP",
624 &old_flags
->querier_ipv4
,
625 &new_flags
->querier_ipv4
);
626 batadv_mcast_querier_log(bat_priv
, "MLD",
627 &old_flags
->querier_ipv6
,
628 &new_flags
->querier_ipv6
);
633 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
634 * @bat_priv: the bat priv with all the soft interface information
635 * @flags: TVLV flags indicating the new multicast state
637 * Whenever the multicast TVLV flags this nodes announces change this notifies
638 * userspace via the 'mcast' log level.
640 static void batadv_mcast_flags_log(struct batadv_priv
*bat_priv
, u8 flags
)
642 bool old_enabled
= bat_priv
->mcast
.mla_flags
.enabled
;
643 u8 old_flags
= bat_priv
->mcast
.mla_flags
.tvlv_flags
;
644 char str_old_flags
[] = "[...]";
646 sprintf(str_old_flags
, "[%c%c%c]",
647 (old_flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
) ? 'U' : '.',
648 (old_flags
& BATADV_MCAST_WANT_ALL_IPV4
) ? '4' : '.',
649 (old_flags
& BATADV_MCAST_WANT_ALL_IPV6
) ? '6' : '.');
651 batadv_dbg(BATADV_DBG_MCAST
, bat_priv
,
652 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
653 old_enabled
? str_old_flags
: "<undefined>",
654 (flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
) ? 'U' : '.',
655 (flags
& BATADV_MCAST_WANT_ALL_IPV4
) ? '4' : '.',
656 (flags
& BATADV_MCAST_WANT_ALL_IPV6
) ? '6' : '.');
660 * batadv_mcast_mla_flags_update() - update multicast flags
661 * @bat_priv: the bat priv with all the soft interface information
662 * @flags: flags indicating the new multicast state
664 * Updates the own multicast tvlv with our current multicast related settings,
665 * capabilities and inabilities.
668 batadv_mcast_mla_flags_update(struct batadv_priv
*bat_priv
,
669 struct batadv_mcast_mla_flags
*flags
)
671 struct batadv_tvlv_mcast_data mcast_data
;
673 if (!memcmp(flags
, &bat_priv
->mcast
.mla_flags
, sizeof(*flags
)))
676 batadv_mcast_bridge_log(bat_priv
, flags
);
677 batadv_mcast_flags_log(bat_priv
, flags
->tvlv_flags
);
679 mcast_data
.flags
= flags
->tvlv_flags
;
680 memset(mcast_data
.reserved
, 0, sizeof(mcast_data
.reserved
));
682 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_MCAST
, 2,
683 &mcast_data
, sizeof(mcast_data
));
685 bat_priv
->mcast
.mla_flags
= *flags
;
689 * __batadv_mcast_mla_update() - update the own MLAs
690 * @bat_priv: the bat priv with all the soft interface information
692 * Updates the own multicast listener announcements in the translation
693 * table as well as the own, announced multicast tvlv container.
695 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
696 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
697 * ensured by the non-parallel execution of the worker this function
700 static void __batadv_mcast_mla_update(struct batadv_priv
*bat_priv
)
702 struct net_device
*soft_iface
= bat_priv
->soft_iface
;
703 struct hlist_head mcast_list
= HLIST_HEAD_INIT
;
704 struct batadv_mcast_mla_flags flags
;
707 flags
= batadv_mcast_mla_flags_get(bat_priv
);
709 ret
= batadv_mcast_mla_softif_get(soft_iface
, &mcast_list
, &flags
);
713 ret
= batadv_mcast_mla_bridge_get(soft_iface
, &mcast_list
, &flags
);
717 spin_lock(&bat_priv
->mcast
.mla_lock
);
718 batadv_mcast_mla_tt_retract(bat_priv
, &mcast_list
);
719 batadv_mcast_mla_tt_add(bat_priv
, &mcast_list
);
720 batadv_mcast_mla_flags_update(bat_priv
, &flags
);
721 spin_unlock(&bat_priv
->mcast
.mla_lock
);
724 batadv_mcast_mla_list_free(&mcast_list
);
728 * batadv_mcast_mla_update() - update the own MLAs
729 * @work: kernel work struct
731 * Updates the own multicast listener announcements in the translation
732 * table as well as the own, announced multicast tvlv container.
734 * In the end, reschedules the work timer.
736 static void batadv_mcast_mla_update(struct work_struct
*work
)
738 struct delayed_work
*delayed_work
;
739 struct batadv_priv_mcast
*priv_mcast
;
740 struct batadv_priv
*bat_priv
;
742 delayed_work
= to_delayed_work(work
);
743 priv_mcast
= container_of(delayed_work
, struct batadv_priv_mcast
, work
);
744 bat_priv
= container_of(priv_mcast
, struct batadv_priv
, mcast
);
746 __batadv_mcast_mla_update(bat_priv
);
747 batadv_mcast_start_timer(bat_priv
);
751 * batadv_mcast_is_report_ipv4() - check for IGMP reports
752 * @skb: the ethernet frame destined for the mesh
754 * This call might reallocate skb data.
756 * Checks whether the given frame is a valid IGMP report.
758 * Return: If so then true, otherwise false.
760 static bool batadv_mcast_is_report_ipv4(struct sk_buff
*skb
)
762 if (ip_mc_check_igmp(skb
) < 0)
765 switch (igmp_hdr(skb
)->type
) {
766 case IGMP_HOST_MEMBERSHIP_REPORT
:
767 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
768 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
776 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
778 * @bat_priv: the bat priv with all the soft interface information
779 * @skb: the IPv4 packet to check
780 * @is_unsnoopable: stores whether the destination is snoopable
782 * Checks whether the given IPv4 packet has the potential to be forwarded with a
783 * mode more optimal than classic flooding.
785 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
786 * allocation failure.
788 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv
*bat_priv
,
790 bool *is_unsnoopable
)
794 /* We might fail due to out-of-memory -> drop it */
795 if (!pskb_may_pull(skb
, sizeof(struct ethhdr
) + sizeof(*iphdr
)))
798 if (batadv_mcast_is_report_ipv4(skb
))
803 /* TODO: Implement Multicast Router Discovery (RFC4286),
804 * then allow scope > link local, too
806 if (!ipv4_is_local_multicast(iphdr
->daddr
))
809 /* link-local multicast listeners behind a bridge are
810 * not snoopable (see RFC4541, section 2.1.2.2)
812 *is_unsnoopable
= true;
818 * batadv_mcast_is_report_ipv6() - check for MLD reports
819 * @skb: the ethernet frame destined for the mesh
821 * This call might reallocate skb data.
823 * Checks whether the given frame is a valid MLD report.
825 * Return: If so then true, otherwise false.
827 static bool batadv_mcast_is_report_ipv6(struct sk_buff
*skb
)
829 if (ipv6_mc_check_mld(skb
) < 0)
832 switch (icmp6_hdr(skb
)->icmp6_type
) {
833 case ICMPV6_MGM_REPORT
:
834 case ICMPV6_MLD2_REPORT
:
842 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
844 * @bat_priv: the bat priv with all the soft interface information
845 * @skb: the IPv6 packet to check
846 * @is_unsnoopable: stores whether the destination is snoopable
848 * Checks whether the given IPv6 packet has the potential to be forwarded with a
849 * mode more optimal than classic flooding.
851 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
853 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv
*bat_priv
,
855 bool *is_unsnoopable
)
857 struct ipv6hdr
*ip6hdr
;
859 /* We might fail due to out-of-memory -> drop it */
860 if (!pskb_may_pull(skb
, sizeof(struct ethhdr
) + sizeof(*ip6hdr
)))
863 if (batadv_mcast_is_report_ipv6(skb
))
866 ip6hdr
= ipv6_hdr(skb
);
868 /* TODO: Implement Multicast Router Discovery (RFC4286),
869 * then allow scope > link local, too
871 if (IPV6_ADDR_MC_SCOPE(&ip6hdr
->daddr
) != IPV6_ADDR_SCOPE_LINKLOCAL
)
874 /* link-local-all-nodes multicast listeners behind a bridge are
875 * not snoopable (see RFC4541, section 3, paragraph 3)
877 if (ipv6_addr_is_ll_all_nodes(&ip6hdr
->daddr
))
878 *is_unsnoopable
= true;
884 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
885 * @bat_priv: the bat priv with all the soft interface information
886 * @skb: the multicast frame to check
887 * @is_unsnoopable: stores whether the destination is snoopable
889 * Checks whether the given multicast ethernet frame has the potential to be
890 * forwarded with a mode more optimal than classic flooding.
892 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
894 static int batadv_mcast_forw_mode_check(struct batadv_priv
*bat_priv
,
896 bool *is_unsnoopable
)
898 struct ethhdr
*ethhdr
= eth_hdr(skb
);
900 if (!atomic_read(&bat_priv
->multicast_mode
))
903 switch (ntohs(ethhdr
->h_proto
)) {
905 return batadv_mcast_forw_mode_check_ipv4(bat_priv
, skb
,
908 if (!IS_ENABLED(CONFIG_IPV6
))
911 return batadv_mcast_forw_mode_check_ipv6(bat_priv
, skb
,
919 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
921 * @bat_priv: the bat priv with all the soft interface information
922 * @ethhdr: ethernet header of a packet
924 * Return: the number of nodes which want all IPv4 multicast traffic if the
925 * given ethhdr is from an IPv4 packet or the number of nodes which want all
926 * IPv6 traffic if it matches an IPv6 packet.
928 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv
*bat_priv
,
929 struct ethhdr
*ethhdr
)
931 switch (ntohs(ethhdr
->h_proto
)) {
933 return atomic_read(&bat_priv
->mcast
.num_want_all_ipv4
);
935 return atomic_read(&bat_priv
->mcast
.num_want_all_ipv6
);
937 /* we shouldn't be here... */
943 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
944 * @bat_priv: the bat priv with all the soft interface information
945 * @ethhdr: the ether header containing the multicast destination
947 * Return: an orig_node matching the multicast address provided by ethhdr
948 * via a translation table lookup. This increases the returned nodes refcount.
950 static struct batadv_orig_node
*
951 batadv_mcast_forw_tt_node_get(struct batadv_priv
*bat_priv
,
952 struct ethhdr
*ethhdr
)
954 return batadv_transtable_search(bat_priv
, NULL
, ethhdr
->h_dest
,
959 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
960 * @bat_priv: the bat priv with all the soft interface information
962 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
963 * increases its refcount.
965 static struct batadv_orig_node
*
966 batadv_mcast_forw_ipv4_node_get(struct batadv_priv
*bat_priv
)
968 struct batadv_orig_node
*tmp_orig_node
, *orig_node
= NULL
;
971 hlist_for_each_entry_rcu(tmp_orig_node
,
972 &bat_priv
->mcast
.want_all_ipv4_list
,
973 mcast_want_all_ipv4_node
) {
974 if (!kref_get_unless_zero(&tmp_orig_node
->refcount
))
977 orig_node
= tmp_orig_node
;
986 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
987 * @bat_priv: the bat priv with all the soft interface information
989 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
990 * and increases its refcount.
992 static struct batadv_orig_node
*
993 batadv_mcast_forw_ipv6_node_get(struct batadv_priv
*bat_priv
)
995 struct batadv_orig_node
*tmp_orig_node
, *orig_node
= NULL
;
998 hlist_for_each_entry_rcu(tmp_orig_node
,
999 &bat_priv
->mcast
.want_all_ipv6_list
,
1000 mcast_want_all_ipv6_node
) {
1001 if (!kref_get_unless_zero(&tmp_orig_node
->refcount
))
1004 orig_node
= tmp_orig_node
;
1013 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
1014 * @bat_priv: the bat priv with all the soft interface information
1015 * @ethhdr: an ethernet header to determine the protocol family from
1017 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
1018 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
1019 * increases its refcount.
1021 static struct batadv_orig_node
*
1022 batadv_mcast_forw_ip_node_get(struct batadv_priv
*bat_priv
,
1023 struct ethhdr
*ethhdr
)
1025 switch (ntohs(ethhdr
->h_proto
)) {
1027 return batadv_mcast_forw_ipv4_node_get(bat_priv
);
1029 return batadv_mcast_forw_ipv6_node_get(bat_priv
);
1031 /* we shouldn't be here... */
1037 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
1038 * @bat_priv: the bat priv with all the soft interface information
1040 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
1041 * set and increases its refcount.
1043 static struct batadv_orig_node
*
1044 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv
*bat_priv
)
1046 struct batadv_orig_node
*tmp_orig_node
, *orig_node
= NULL
;
1049 hlist_for_each_entry_rcu(tmp_orig_node
,
1050 &bat_priv
->mcast
.want_all_unsnoopables_list
,
1051 mcast_want_all_unsnoopables_node
) {
1052 if (!kref_get_unless_zero(&tmp_orig_node
->refcount
))
1055 orig_node
= tmp_orig_node
;
1064 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
1065 * @bat_priv: the bat priv with all the soft interface information
1066 * @skb: The multicast packet to check
1067 * @orig: an originator to be set to forward the skb to
1069 * Return: the forwarding mode as enum batadv_forw_mode and in case of
1070 * BATADV_FORW_SINGLE set the orig to the single originator the skb
1071 * should be forwarded to.
1073 enum batadv_forw_mode
1074 batadv_mcast_forw_mode(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1075 struct batadv_orig_node
**orig
)
1077 int ret
, tt_count
, ip_count
, unsnoop_count
, total_count
;
1078 bool is_unsnoopable
= false;
1079 unsigned int mcast_fanout
;
1080 struct ethhdr
*ethhdr
;
1082 ret
= batadv_mcast_forw_mode_check(bat_priv
, skb
, &is_unsnoopable
);
1084 return BATADV_FORW_NONE
;
1086 return BATADV_FORW_ALL
;
1088 ethhdr
= eth_hdr(skb
);
1090 tt_count
= batadv_tt_global_hash_count(bat_priv
, ethhdr
->h_dest
,
1092 ip_count
= batadv_mcast_forw_want_all_ip_count(bat_priv
, ethhdr
);
1093 unsnoop_count
= !is_unsnoopable
? 0 :
1094 atomic_read(&bat_priv
->mcast
.num_want_all_unsnoopables
);
1096 total_count
= tt_count
+ ip_count
+ unsnoop_count
;
1098 switch (total_count
) {
1101 *orig
= batadv_mcast_forw_tt_node_get(bat_priv
, ethhdr
);
1103 *orig
= batadv_mcast_forw_ip_node_get(bat_priv
, ethhdr
);
1104 else if (unsnoop_count
)
1105 *orig
= batadv_mcast_forw_unsnoop_node_get(bat_priv
);
1108 return BATADV_FORW_SINGLE
;
1112 return BATADV_FORW_NONE
;
1114 mcast_fanout
= atomic_read(&bat_priv
->multicast_fanout
);
1116 if (!unsnoop_count
&& total_count
<= mcast_fanout
)
1117 return BATADV_FORW_SOME
;
1120 return BATADV_FORW_ALL
;
1124 * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1125 * @bat_priv: the bat priv with all the soft interface information
1126 * @skb: the multicast packet to transmit
1127 * @vid: the vlan identifier
1129 * Sends copies of a frame with multicast destination to any multicast
1130 * listener registered in the translation table. A transmission is performed
1131 * via a batman-adv unicast packet for each such destination node.
1133 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1137 batadv_mcast_forw_tt(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1140 int ret
= NET_XMIT_SUCCESS
;
1141 struct sk_buff
*newskb
;
1143 struct batadv_tt_orig_list_entry
*orig_entry
;
1145 struct batadv_tt_global_entry
*tt_global
;
1146 const u8
*addr
= eth_hdr(skb
)->h_dest
;
1148 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1153 hlist_for_each_entry_rcu(orig_entry
, &tt_global
->orig_list
, list
) {
1154 newskb
= skb_copy(skb
, GFP_ATOMIC
);
1156 ret
= NET_XMIT_DROP
;
1160 batadv_send_skb_unicast(bat_priv
, newskb
, BATADV_UNICAST
, 0,
1161 orig_entry
->orig_node
, vid
);
1165 batadv_tt_global_entry_put(tt_global
);
1172 * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1173 * @bat_priv: the bat priv with all the soft interface information
1174 * @skb: the multicast packet to transmit
1175 * @vid: the vlan identifier
1177 * Sends copies of a frame with multicast destination to any node with a
1178 * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1179 * batman-adv unicast packet for each such destination node.
1181 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1185 batadv_mcast_forw_want_all_ipv4(struct batadv_priv
*bat_priv
,
1186 struct sk_buff
*skb
, unsigned short vid
)
1188 struct batadv_orig_node
*orig_node
;
1189 int ret
= NET_XMIT_SUCCESS
;
1190 struct sk_buff
*newskb
;
1193 hlist_for_each_entry_rcu(orig_node
,
1194 &bat_priv
->mcast
.want_all_ipv4_list
,
1195 mcast_want_all_ipv4_node
) {
1196 newskb
= skb_copy(skb
, GFP_ATOMIC
);
1198 ret
= NET_XMIT_DROP
;
1202 batadv_send_skb_unicast(bat_priv
, newskb
, BATADV_UNICAST
, 0,
1210 * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1211 * @bat_priv: the bat priv with all the soft interface information
1212 * @skb: The multicast packet to transmit
1213 * @vid: the vlan identifier
1215 * Sends copies of a frame with multicast destination to any node with a
1216 * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1217 * batman-adv unicast packet for each such destination node.
1219 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1223 batadv_mcast_forw_want_all_ipv6(struct batadv_priv
*bat_priv
,
1224 struct sk_buff
*skb
, unsigned short vid
)
1226 struct batadv_orig_node
*orig_node
;
1227 int ret
= NET_XMIT_SUCCESS
;
1228 struct sk_buff
*newskb
;
1231 hlist_for_each_entry_rcu(orig_node
,
1232 &bat_priv
->mcast
.want_all_ipv6_list
,
1233 mcast_want_all_ipv6_node
) {
1234 newskb
= skb_copy(skb
, GFP_ATOMIC
);
1236 ret
= NET_XMIT_DROP
;
1240 batadv_send_skb_unicast(bat_priv
, newskb
, BATADV_UNICAST
, 0,
1248 * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1249 * @bat_priv: the bat priv with all the soft interface information
1250 * @skb: the multicast packet to transmit
1251 * @vid: the vlan identifier
1253 * Sends copies of a frame with multicast destination to any node with a
1254 * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1255 * transmission is performed via a batman-adv unicast packet for each such
1258 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1259 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1262 batadv_mcast_forw_want_all(struct batadv_priv
*bat_priv
,
1263 struct sk_buff
*skb
, unsigned short vid
)
1265 switch (ntohs(eth_hdr(skb
)->h_proto
)) {
1267 return batadv_mcast_forw_want_all_ipv4(bat_priv
, skb
, vid
);
1269 return batadv_mcast_forw_want_all_ipv6(bat_priv
, skb
, vid
);
1271 /* we shouldn't be here... */
1272 return NET_XMIT_DROP
;
1277 * batadv_mcast_forw_send() - send packet to any detected multicast recpient
1278 * @bat_priv: the bat priv with all the soft interface information
1279 * @skb: the multicast packet to transmit
1280 * @vid: the vlan identifier
1282 * Sends copies of a frame with multicast destination to any node that signaled
1283 * interest in it, that is either via the translation table or the according
1284 * want-all flags. A transmission is performed via a batman-adv unicast packet
1285 * for each such destination node.
1287 * The given skb is consumed/freed.
1289 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1290 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1292 int batadv_mcast_forw_send(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1297 ret
= batadv_mcast_forw_tt(bat_priv
, skb
, vid
);
1298 if (ret
!= NET_XMIT_SUCCESS
) {
1303 ret
= batadv_mcast_forw_want_all(bat_priv
, skb
, vid
);
1304 if (ret
!= NET_XMIT_SUCCESS
) {
1314 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1315 * @bat_priv: the bat priv with all the soft interface information
1316 * @orig: the orig_node which multicast state might have changed of
1317 * @mcast_flags: flags indicating the new multicast state
1319 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1320 * orig, has toggled then this method updates counter and list accordingly.
1322 * Caller needs to hold orig->mcast_handler_lock.
1324 static void batadv_mcast_want_unsnoop_update(struct batadv_priv
*bat_priv
,
1325 struct batadv_orig_node
*orig
,
1328 struct hlist_node
*node
= &orig
->mcast_want_all_unsnoopables_node
;
1329 struct hlist_head
*head
= &bat_priv
->mcast
.want_all_unsnoopables_list
;
1331 lockdep_assert_held(&orig
->mcast_handler_lock
);
1333 /* switched from flag unset to set */
1334 if (mcast_flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
&&
1335 !(orig
->mcast_flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
)) {
1336 atomic_inc(&bat_priv
->mcast
.num_want_all_unsnoopables
);
1338 spin_lock_bh(&bat_priv
->mcast
.want_lists_lock
);
1339 /* flag checks above + mcast_handler_lock prevents this */
1340 WARN_ON(!hlist_unhashed(node
));
1342 hlist_add_head_rcu(node
, head
);
1343 spin_unlock_bh(&bat_priv
->mcast
.want_lists_lock
);
1344 /* switched from flag set to unset */
1345 } else if (!(mcast_flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
) &&
1346 orig
->mcast_flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
) {
1347 atomic_dec(&bat_priv
->mcast
.num_want_all_unsnoopables
);
1349 spin_lock_bh(&bat_priv
->mcast
.want_lists_lock
);
1350 /* flag checks above + mcast_handler_lock prevents this */
1351 WARN_ON(hlist_unhashed(node
));
1353 hlist_del_init_rcu(node
);
1354 spin_unlock_bh(&bat_priv
->mcast
.want_lists_lock
);
1359 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1360 * @bat_priv: the bat priv with all the soft interface information
1361 * @orig: the orig_node which multicast state might have changed of
1362 * @mcast_flags: flags indicating the new multicast state
1364 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1365 * toggled then this method updates counter and list accordingly.
1367 * Caller needs to hold orig->mcast_handler_lock.
1369 static void batadv_mcast_want_ipv4_update(struct batadv_priv
*bat_priv
,
1370 struct batadv_orig_node
*orig
,
1373 struct hlist_node
*node
= &orig
->mcast_want_all_ipv4_node
;
1374 struct hlist_head
*head
= &bat_priv
->mcast
.want_all_ipv4_list
;
1376 lockdep_assert_held(&orig
->mcast_handler_lock
);
1378 /* switched from flag unset to set */
1379 if (mcast_flags
& BATADV_MCAST_WANT_ALL_IPV4
&&
1380 !(orig
->mcast_flags
& BATADV_MCAST_WANT_ALL_IPV4
)) {
1381 atomic_inc(&bat_priv
->mcast
.num_want_all_ipv4
);
1383 spin_lock_bh(&bat_priv
->mcast
.want_lists_lock
);
1384 /* flag checks above + mcast_handler_lock prevents this */
1385 WARN_ON(!hlist_unhashed(node
));
1387 hlist_add_head_rcu(node
, head
);
1388 spin_unlock_bh(&bat_priv
->mcast
.want_lists_lock
);
1389 /* switched from flag set to unset */
1390 } else if (!(mcast_flags
& BATADV_MCAST_WANT_ALL_IPV4
) &&
1391 orig
->mcast_flags
& BATADV_MCAST_WANT_ALL_IPV4
) {
1392 atomic_dec(&bat_priv
->mcast
.num_want_all_ipv4
);
1394 spin_lock_bh(&bat_priv
->mcast
.want_lists_lock
);
1395 /* flag checks above + mcast_handler_lock prevents this */
1396 WARN_ON(hlist_unhashed(node
));
1398 hlist_del_init_rcu(node
);
1399 spin_unlock_bh(&bat_priv
->mcast
.want_lists_lock
);
1404 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1405 * @bat_priv: the bat priv with all the soft interface information
1406 * @orig: the orig_node which multicast state might have changed of
1407 * @mcast_flags: flags indicating the new multicast state
1409 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1410 * toggled then this method updates counter and list accordingly.
1412 * Caller needs to hold orig->mcast_handler_lock.
1414 static void batadv_mcast_want_ipv6_update(struct batadv_priv
*bat_priv
,
1415 struct batadv_orig_node
*orig
,
1418 struct hlist_node
*node
= &orig
->mcast_want_all_ipv6_node
;
1419 struct hlist_head
*head
= &bat_priv
->mcast
.want_all_ipv6_list
;
1421 lockdep_assert_held(&orig
->mcast_handler_lock
);
1423 /* switched from flag unset to set */
1424 if (mcast_flags
& BATADV_MCAST_WANT_ALL_IPV6
&&
1425 !(orig
->mcast_flags
& BATADV_MCAST_WANT_ALL_IPV6
)) {
1426 atomic_inc(&bat_priv
->mcast
.num_want_all_ipv6
);
1428 spin_lock_bh(&bat_priv
->mcast
.want_lists_lock
);
1429 /* flag checks above + mcast_handler_lock prevents this */
1430 WARN_ON(!hlist_unhashed(node
));
1432 hlist_add_head_rcu(node
, head
);
1433 spin_unlock_bh(&bat_priv
->mcast
.want_lists_lock
);
1434 /* switched from flag set to unset */
1435 } else if (!(mcast_flags
& BATADV_MCAST_WANT_ALL_IPV6
) &&
1436 orig
->mcast_flags
& BATADV_MCAST_WANT_ALL_IPV6
) {
1437 atomic_dec(&bat_priv
->mcast
.num_want_all_ipv6
);
1439 spin_lock_bh(&bat_priv
->mcast
.want_lists_lock
);
1440 /* flag checks above + mcast_handler_lock prevents this */
1441 WARN_ON(hlist_unhashed(node
));
1443 hlist_del_init_rcu(node
);
1444 spin_unlock_bh(&bat_priv
->mcast
.want_lists_lock
);
1449 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1450 * @bat_priv: the bat priv with all the soft interface information
1451 * @orig: the orig_node of the ogm
1452 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1453 * @tvlv_value: tvlv buffer containing the multicast data
1454 * @tvlv_value_len: tvlv buffer length
1456 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv
*bat_priv
,
1457 struct batadv_orig_node
*orig
,
1462 bool orig_mcast_enabled
= !(flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
1463 u8 mcast_flags
= BATADV_NO_FLAGS
;
1465 if (orig_mcast_enabled
&& tvlv_value
&&
1466 tvlv_value_len
>= sizeof(mcast_flags
))
1467 mcast_flags
= *(u8
*)tvlv_value
;
1469 if (!orig_mcast_enabled
) {
1470 mcast_flags
|= BATADV_MCAST_WANT_ALL_IPV4
;
1471 mcast_flags
|= BATADV_MCAST_WANT_ALL_IPV6
;
1474 spin_lock_bh(&orig
->mcast_handler_lock
);
1476 if (orig_mcast_enabled
&&
1477 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST
, &orig
->capabilities
)) {
1478 set_bit(BATADV_ORIG_CAPA_HAS_MCAST
, &orig
->capabilities
);
1479 } else if (!orig_mcast_enabled
&&
1480 test_bit(BATADV_ORIG_CAPA_HAS_MCAST
, &orig
->capabilities
)) {
1481 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST
, &orig
->capabilities
);
1484 set_bit(BATADV_ORIG_CAPA_HAS_MCAST
, &orig
->capa_initialized
);
1486 batadv_mcast_want_unsnoop_update(bat_priv
, orig
, mcast_flags
);
1487 batadv_mcast_want_ipv4_update(bat_priv
, orig
, mcast_flags
);
1488 batadv_mcast_want_ipv6_update(bat_priv
, orig
, mcast_flags
);
1490 orig
->mcast_flags
= mcast_flags
;
1491 spin_unlock_bh(&orig
->mcast_handler_lock
);
1495 * batadv_mcast_init() - initialize the multicast optimizations structures
1496 * @bat_priv: the bat priv with all the soft interface information
1498 void batadv_mcast_init(struct batadv_priv
*bat_priv
)
1500 batadv_tvlv_handler_register(bat_priv
, batadv_mcast_tvlv_ogm_handler
,
1501 NULL
, BATADV_TVLV_MCAST
, 2,
1502 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
1504 INIT_DELAYED_WORK(&bat_priv
->mcast
.work
, batadv_mcast_mla_update
);
1505 batadv_mcast_start_timer(bat_priv
);
1508 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1510 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
1511 * @bat_priv: the bat priv with all the soft interface information
1512 * @seq: debugfs table seq_file struct
1514 * Prints our own multicast flags including a more specific reason why
1515 * they are set, that is prints the bridge and querier state too, to
1516 * the debugfs table specified via @seq.
1518 static void batadv_mcast_flags_print_header(struct batadv_priv
*bat_priv
,
1519 struct seq_file
*seq
)
1521 struct batadv_mcast_mla_flags
*mla_flags
= &bat_priv
->mcast
.mla_flags
;
1522 char querier4
, querier6
, shadowing4
, shadowing6
;
1523 bool bridged
= mla_flags
->bridged
;
1524 u8 flags
= mla_flags
->tvlv_flags
;
1527 querier4
= mla_flags
->querier_ipv4
.exists
? '.' : '4';
1528 querier6
= mla_flags
->querier_ipv6
.exists
? '.' : '6';
1529 shadowing4
= mla_flags
->querier_ipv4
.shadowing
? '4' : '.';
1530 shadowing6
= mla_flags
->querier_ipv6
.shadowing
? '6' : '.';
1538 seq_printf(seq
, "Multicast flags (own flags: [%c%c%c])\n",
1539 (flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
) ? 'U' : '.',
1540 (flags
& BATADV_MCAST_WANT_ALL_IPV4
) ? '4' : '.',
1541 (flags
& BATADV_MCAST_WANT_ALL_IPV6
) ? '6' : '.');
1542 seq_printf(seq
, "* Bridged [U]\t\t\t\t%c\n", bridged
? 'U' : '.');
1543 seq_printf(seq
, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1544 querier4
, querier6
);
1545 seq_printf(seq
, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1546 shadowing4
, shadowing6
);
1547 seq_puts(seq
, "-------------------------------------------\n");
1548 seq_printf(seq
, " %-10s %s\n", "Originator", "Flags");
1552 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
1553 * @seq: seq file to print on
1556 * This prints a table of (primary) originators and their according
1557 * multicast flags, including (in the header) our own.
1561 int batadv_mcast_flags_seq_print_text(struct seq_file
*seq
, void *offset
)
1563 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1564 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1565 struct batadv_hard_iface
*primary_if
;
1566 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1567 struct batadv_orig_node
*orig_node
;
1568 struct hlist_head
*head
;
1572 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1576 batadv_mcast_flags_print_header(bat_priv
, seq
);
1578 for (i
= 0; i
< hash
->size
; i
++) {
1579 head
= &hash
->table
[i
];
1582 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1583 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST
,
1584 &orig_node
->capa_initialized
))
1587 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST
,
1588 &orig_node
->capabilities
)) {
1589 seq_printf(seq
, "%pM -\n", orig_node
->orig
);
1593 flags
= orig_node
->mcast_flags
;
1595 seq_printf(seq
, "%pM [%c%c%c]\n", orig_node
->orig
,
1596 (flags
& BATADV_MCAST_WANT_ALL_UNSNOOPABLES
)
1598 (flags
& BATADV_MCAST_WANT_ALL_IPV4
)
1600 (flags
& BATADV_MCAST_WANT_ALL_IPV6
)
1606 batadv_hardif_put(primary_if
);
1613 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
1614 * @msg: buffer for the message
1615 * @bat_priv: the bat priv with all the soft interface information
1617 * Return: 0 or error code.
1619 int batadv_mcast_mesh_info_put(struct sk_buff
*msg
,
1620 struct batadv_priv
*bat_priv
)
1622 u32 flags
= bat_priv
->mcast
.mla_flags
.tvlv_flags
;
1623 u32 flags_priv
= BATADV_NO_FLAGS
;
1625 if (bat_priv
->mcast
.mla_flags
.bridged
) {
1626 flags_priv
|= BATADV_MCAST_FLAGS_BRIDGED
;
1628 if (bat_priv
->mcast
.mla_flags
.querier_ipv4
.exists
)
1629 flags_priv
|= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS
;
1630 if (bat_priv
->mcast
.mla_flags
.querier_ipv6
.exists
)
1631 flags_priv
|= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS
;
1632 if (bat_priv
->mcast
.mla_flags
.querier_ipv4
.shadowing
)
1633 flags_priv
|= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING
;
1634 if (bat_priv
->mcast
.mla_flags
.querier_ipv6
.shadowing
)
1635 flags_priv
|= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING
;
1638 if (nla_put_u32(msg
, BATADV_ATTR_MCAST_FLAGS
, flags
) ||
1639 nla_put_u32(msg
, BATADV_ATTR_MCAST_FLAGS_PRIV
, flags_priv
))
1646 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
1647 * to a netlink socket
1648 * @msg: buffer for the message
1649 * @portid: netlink port
1650 * @cb: Control block containing additional options
1651 * @orig_node: originator to dump the multicast flags of
1653 * Return: 0 or error code.
1656 batadv_mcast_flags_dump_entry(struct sk_buff
*msg
, u32 portid
,
1657 struct netlink_callback
*cb
,
1658 struct batadv_orig_node
*orig_node
)
1662 hdr
= genlmsg_put(msg
, portid
, cb
->nlh
->nlmsg_seq
,
1663 &batadv_netlink_family
, NLM_F_MULTI
,
1664 BATADV_CMD_GET_MCAST_FLAGS
);
1668 genl_dump_check_consistent(cb
, hdr
);
1670 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
,
1672 genlmsg_cancel(msg
, hdr
);
1676 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST
,
1677 &orig_node
->capabilities
)) {
1678 if (nla_put_u32(msg
, BATADV_ATTR_MCAST_FLAGS
,
1679 orig_node
->mcast_flags
)) {
1680 genlmsg_cancel(msg
, hdr
);
1685 genlmsg_end(msg
, hdr
);
1690 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
1691 * table to a netlink socket
1692 * @msg: buffer for the message
1693 * @portid: netlink port
1694 * @cb: Control block containing additional options
1695 * @hash: hash to dump
1696 * @bucket: bucket index to dump
1697 * @idx_skip: How many entries to skip
1699 * Return: 0 or error code.
1702 batadv_mcast_flags_dump_bucket(struct sk_buff
*msg
, u32 portid
,
1703 struct netlink_callback
*cb
,
1704 struct batadv_hashtable
*hash
,
1705 unsigned int bucket
, long *idx_skip
)
1707 struct batadv_orig_node
*orig_node
;
1710 spin_lock_bh(&hash
->list_locks
[bucket
]);
1711 cb
->seq
= atomic_read(&hash
->generation
) << 1 | 1;
1713 hlist_for_each_entry(orig_node
, &hash
->table
[bucket
], hash_entry
) {
1714 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST
,
1715 &orig_node
->capa_initialized
))
1718 if (idx
< *idx_skip
)
1721 if (batadv_mcast_flags_dump_entry(msg
, portid
, cb
, orig_node
)) {
1722 spin_unlock_bh(&hash
->list_locks
[bucket
]);
1731 spin_unlock_bh(&hash
->list_locks
[bucket
]);
1737 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1738 * @msg: buffer for the message
1739 * @portid: netlink port
1740 * @cb: Control block containing additional options
1741 * @bat_priv: the bat priv with all the soft interface information
1742 * @bucket: current bucket to dump
1743 * @idx: index in current bucket to the next entry to dump
1745 * Return: 0 or error code.
1748 __batadv_mcast_flags_dump(struct sk_buff
*msg
, u32 portid
,
1749 struct netlink_callback
*cb
,
1750 struct batadv_priv
*bat_priv
, long *bucket
, long *idx
)
1752 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1753 long bucket_tmp
= *bucket
;
1754 long idx_tmp
= *idx
;
1756 while (bucket_tmp
< hash
->size
) {
1757 if (batadv_mcast_flags_dump_bucket(msg
, portid
, cb
, hash
,
1765 *bucket
= bucket_tmp
;
1772 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
1774 * @cb: netlink callback structure
1775 * @primary_if: the primary interface pointer to return the result in
1777 * Return: 0 or error code.
1780 batadv_mcast_netlink_get_primary(struct netlink_callback
*cb
,
1781 struct batadv_hard_iface
**primary_if
)
1783 struct batadv_hard_iface
*hard_iface
= NULL
;
1784 struct net
*net
= sock_net(cb
->skb
->sk
);
1785 struct net_device
*soft_iface
;
1786 struct batadv_priv
*bat_priv
;
1790 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
, BATADV_ATTR_MESH_IFINDEX
);
1794 soft_iface
= dev_get_by_index(net
, ifindex
);
1795 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
1800 bat_priv
= netdev_priv(soft_iface
);
1802 hard_iface
= batadv_primary_if_get_selected(bat_priv
);
1803 if (!hard_iface
|| hard_iface
->if_status
!= BATADV_IF_ACTIVE
) {
1810 dev_put(soft_iface
);
1812 if (!ret
&& primary_if
)
1813 *primary_if
= hard_iface
;
1814 else if (hard_iface
)
1815 batadv_hardif_put(hard_iface
);
1821 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1822 * @msg: buffer for the message
1823 * @cb: callback structure containing arguments
1825 * Return: message length.
1827 int batadv_mcast_flags_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
1829 struct batadv_hard_iface
*primary_if
= NULL
;
1830 int portid
= NETLINK_CB(cb
->skb
).portid
;
1831 struct batadv_priv
*bat_priv
;
1832 long *bucket
= &cb
->args
[0];
1833 long *idx
= &cb
->args
[1];
1836 ret
= batadv_mcast_netlink_get_primary(cb
, &primary_if
);
1840 bat_priv
= netdev_priv(primary_if
->soft_iface
);
1841 ret
= __batadv_mcast_flags_dump(msg
, portid
, cb
, bat_priv
, bucket
, idx
);
1843 batadv_hardif_put(primary_if
);
1848 * batadv_mcast_free() - free the multicast optimizations structures
1849 * @bat_priv: the bat priv with all the soft interface information
1851 void batadv_mcast_free(struct batadv_priv
*bat_priv
)
1853 cancel_delayed_work_sync(&bat_priv
->mcast
.work
);
1855 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_MCAST
, 2);
1856 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_MCAST
, 2);
1858 /* safely calling outside of worker, as worker was canceled above */
1859 batadv_mcast_mla_tt_retract(bat_priv
, NULL
);
1863 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
1864 * @orig: the originator which is going to get purged
1866 void batadv_mcast_purge_orig(struct batadv_orig_node
*orig
)
1868 struct batadv_priv
*bat_priv
= orig
->bat_priv
;
1870 spin_lock_bh(&orig
->mcast_handler_lock
);
1872 batadv_mcast_want_unsnoop_update(bat_priv
, orig
, BATADV_NO_FLAGS
);
1873 batadv_mcast_want_ipv4_update(bat_priv
, orig
, BATADV_NO_FLAGS
);
1874 batadv_mcast_want_ipv6_update(bat_priv
, orig
, BATADV_NO_FLAGS
);
1876 spin_unlock_bh(&orig
->mcast_handler_lock
);