1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 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/skbuff.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/stddef.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
42 #include <linux/workqueue.h>
43 #include <net/addrconf.h>
44 #include <net/genetlink.h>
45 #include <net/if_inet6.h>
48 #include <net/netlink.h>
50 #include <uapi/linux/batadv_packet.h>
51 #include <uapi/linux/batman_adv.h>
53 #include "bridge_loop_avoidance.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_get_bridge() - get the bridge on top of the softif if it exists
77 * @soft_iface: netdev struct of the mesh interface
79 * If the given soft interface has a bridge on top then the refcount
80 * of the according net device is increased.
82 * Return: NULL if no such bridge exists. Otherwise the net device of the
85 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
87 struct net_device *upper = soft_iface;
91 upper = netdev_master_upper_dev_get_rcu(upper);
92 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
102 * batadv_mcast_mla_rtr_flags_softif_get_ipv4() - get mcast router flags from
104 * @dev: the interface to check
106 * Checks the presence of an IPv4 multicast router on this node.
108 * Caller needs to hold rcu read lock.
110 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
112 static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv4(struct net_device *dev)
114 struct in_device *in_dev = __in_dev_get_rcu(dev);
116 if (in_dev && IN_DEV_MFORWARD(in_dev))
117 return BATADV_NO_FLAGS;
119 return BATADV_MCAST_WANT_NO_RTR4;
123 * batadv_mcast_mla_rtr_flags_softif_get_ipv6() - get mcast router flags from
125 * @dev: the interface to check
127 * Checks the presence of an IPv6 multicast router on this node.
129 * Caller needs to hold rcu read lock.
131 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
133 #if IS_ENABLED(CONFIG_IPV6_MROUTE)
134 static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
136 struct inet6_dev *in6_dev = __in6_dev_get(dev);
138 if (in6_dev && in6_dev->cnf.mc_forwarding)
139 return BATADV_NO_FLAGS;
141 return BATADV_MCAST_WANT_NO_RTR6;
145 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
147 return BATADV_MCAST_WANT_NO_RTR6;
152 * batadv_mcast_mla_rtr_flags_softif_get() - get mcast router flags from node
153 * @bat_priv: the bat priv with all the soft interface information
154 * @bridge: bridge interface on top of the soft_iface if present,
155 * otherwise pass NULL
157 * Checks the presence of IPv4 and IPv6 multicast routers on this
161 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
162 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
163 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
164 * The former two OR'd: no multicast router is present
166 static u8 batadv_mcast_mla_rtr_flags_softif_get(struct batadv_priv *bat_priv,
167 struct net_device *bridge)
169 struct net_device *dev = bridge ? bridge : bat_priv->soft_iface;
170 u8 flags = BATADV_NO_FLAGS;
174 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv4(dev);
175 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv6(dev);
183 * batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge
184 * @bat_priv: the bat priv with all the soft interface information
185 * @bridge: bridge interface on top of the soft_iface if present,
186 * otherwise pass NULL
188 * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
191 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
192 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
193 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
194 * The former two OR'd: no multicast router is present
196 static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
197 struct net_device *bridge)
199 struct net_device *dev = bat_priv->soft_iface;
200 u8 flags = BATADV_NO_FLAGS;
203 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
205 if (!br_multicast_has_router_adjacent(dev, ETH_P_IP))
206 flags |= BATADV_MCAST_WANT_NO_RTR4;
207 if (!br_multicast_has_router_adjacent(dev, ETH_P_IPV6))
208 flags |= BATADV_MCAST_WANT_NO_RTR6;
214 * batadv_mcast_mla_rtr_flags_get() - get multicast router flags
215 * @bat_priv: the bat priv with all the soft interface information
216 * @bridge: bridge interface on top of the soft_iface if present,
217 * otherwise pass NULL
219 * Checks the presence of IPv4 and IPv6 multicast routers on this
220 * node or behind its bridge.
223 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
224 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
225 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
226 * The former two OR'd: no multicast router is present
228 static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
229 struct net_device *bridge)
231 u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
233 flags &= batadv_mcast_mla_rtr_flags_softif_get(bat_priv, bridge);
234 flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
240 * batadv_mcast_mla_flags_get() - get the new multicast flags
241 * @bat_priv: the bat priv with all the soft interface information
243 * Return: A set of flags for the current/next TVLV, querier and
246 static struct batadv_mcast_mla_flags
247 batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
249 struct net_device *dev = bat_priv->soft_iface;
250 struct batadv_mcast_querier_state *qr4, *qr6;
251 struct batadv_mcast_mla_flags mla_flags;
252 struct net_device *bridge;
254 bridge = batadv_mcast_get_bridge(dev);
256 memset(&mla_flags, 0, sizeof(mla_flags));
257 mla_flags.enabled = 1;
258 mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv,
266 mla_flags.bridged = 1;
267 qr4 = &mla_flags.querier_ipv4;
268 qr6 = &mla_flags.querier_ipv6;
270 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
271 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
273 qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
274 qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
276 qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
277 qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
279 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
281 /* 1) If no querier exists at all, then multicast listeners on
282 * our local TT clients behind the bridge will keep silent.
283 * 2) If the selected querier is on one of our local TT clients,
284 * behind the bridge, then this querier might shadow multicast
285 * listeners on our local TT clients, behind this bridge.
287 * In both cases, we will signalize other batman nodes that
288 * we need all multicast traffic of the according protocol.
290 if (!qr4->exists || qr4->shadowing) {
291 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4;
292 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4;
295 if (!qr6->exists || qr6->shadowing) {
296 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6;
297 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6;
304 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
305 * @mcast_addr: the multicast address to check
306 * @mcast_list: the list with multicast addresses to search in
308 * Return: true if the given address is already in the given list.
309 * Otherwise returns false.
311 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
312 struct hlist_head *mcast_list)
314 struct batadv_hw_addr *mcast_entry;
316 hlist_for_each_entry(mcast_entry, mcast_list, list)
317 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
324 * batadv_mcast_mla_softif_get_ipv4() - get softif IPv4 multicast listeners
325 * @dev: the device to collect multicast addresses from
326 * @mcast_list: a list to put found addresses into
327 * @flags: flags indicating the new multicast state
329 * Collects multicast addresses of IPv4 multicast listeners residing
330 * on this kernel on the given soft interface, dev, in
331 * the given mcast_list. In general, multicast listeners provided by
332 * your multicast receiving applications run directly on this node.
334 * Return: -ENOMEM on memory allocation error or the number of
335 * items added to the mcast_list otherwise.
338 batadv_mcast_mla_softif_get_ipv4(struct net_device *dev,
339 struct hlist_head *mcast_list,
340 struct batadv_mcast_mla_flags *flags)
342 struct batadv_hw_addr *new;
343 struct in_device *in_dev;
344 u8 mcast_addr[ETH_ALEN];
345 struct ip_mc_list *pmc;
348 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
353 in_dev = __in_dev_get_rcu(dev);
359 for (pmc = rcu_dereference(in_dev->mc_list); pmc;
360 pmc = rcu_dereference(pmc->next_rcu)) {
361 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
362 ipv4_is_local_multicast(pmc->multiaddr))
365 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
366 !ipv4_is_local_multicast(pmc->multiaddr))
369 ip_eth_mc_map(pmc->multiaddr, mcast_addr);
371 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
374 new = kmalloc(sizeof(*new), GFP_ATOMIC);
380 ether_addr_copy(new->addr, mcast_addr);
381 hlist_add_head(&new->list, mcast_list);
390 * batadv_mcast_mla_softif_get_ipv6() - get softif IPv6 multicast listeners
391 * @dev: the device to collect multicast addresses from
392 * @mcast_list: a list to put found addresses into
393 * @flags: flags indicating the new multicast state
395 * Collects multicast addresses of IPv6 multicast listeners residing
396 * on this kernel on the given soft interface, dev, in
397 * the given mcast_list. In general, multicast listeners provided by
398 * your multicast receiving applications run directly on this node.
400 * Return: -ENOMEM on memory allocation error or the number of
401 * items added to the mcast_list otherwise.
403 #if IS_ENABLED(CONFIG_IPV6)
405 batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
406 struct hlist_head *mcast_list,
407 struct batadv_mcast_mla_flags *flags)
409 struct batadv_hw_addr *new;
410 struct inet6_dev *in6_dev;
411 u8 mcast_addr[ETH_ALEN];
412 struct ifmcaddr6 *pmc6;
415 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
420 in6_dev = __in6_dev_get(dev);
426 for (pmc6 = rcu_dereference(in6_dev->mc_list);
428 pmc6 = rcu_dereference(pmc6->next)) {
429 if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) <
430 IPV6_ADDR_SCOPE_LINKLOCAL)
433 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
434 ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
437 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
438 IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) >
439 IPV6_ADDR_SCOPE_LINKLOCAL)
442 ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
444 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
447 new = kmalloc(sizeof(*new), GFP_ATOMIC);
453 ether_addr_copy(new->addr, mcast_addr);
454 hlist_add_head(&new->list, mcast_list);
463 batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
464 struct hlist_head *mcast_list,
465 struct batadv_mcast_mla_flags *flags)
472 * batadv_mcast_mla_softif_get() - get softif multicast listeners
473 * @dev: the device to collect multicast addresses from
474 * @mcast_list: a list to put found addresses into
475 * @flags: flags indicating the new multicast state
477 * Collects multicast addresses of multicast listeners residing
478 * on this kernel on the given soft interface, dev, in
479 * the given mcast_list. In general, multicast listeners provided by
480 * your multicast receiving applications run directly on this node.
482 * If there is a bridge interface on top of dev, collect from that one
483 * instead. Just like with IP addresses and routes, multicast listeners
484 * will(/should) register to the bridge interface instead of an
487 * Return: -ENOMEM on memory allocation error or the number of
488 * items added to the mcast_list otherwise.
491 batadv_mcast_mla_softif_get(struct net_device *dev,
492 struct hlist_head *mcast_list,
493 struct batadv_mcast_mla_flags *flags)
495 struct net_device *bridge = batadv_mcast_get_bridge(dev);
501 ret4 = batadv_mcast_mla_softif_get_ipv4(dev, mcast_list, flags);
505 ret6 = batadv_mcast_mla_softif_get_ipv6(dev, mcast_list, flags);
519 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
520 * @dst: destination to write to - a multicast MAC address
521 * @src: source to read from - a multicast IP address
523 * Converts a given multicast IPv4/IPv6 address from a bridge
524 * to its matching multicast MAC address and copies it into the given
525 * destination buffer.
527 * Caller needs to make sure the destination buffer can hold
528 * at least ETH_ALEN bytes.
530 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
532 if (src->proto == htons(ETH_P_IP))
533 ip_eth_mc_map(src->dst.ip4, dst);
534 #if IS_ENABLED(CONFIG_IPV6)
535 else if (src->proto == htons(ETH_P_IPV6))
536 ipv6_eth_mc_map(&src->dst.ip6, dst);
543 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
544 * @dev: a bridge slave whose bridge to collect multicast addresses from
545 * @mcast_list: a list to put found addresses into
546 * @flags: flags indicating the new multicast state
548 * Collects multicast addresses of multicast listeners residing
549 * on foreign, non-mesh devices which we gave access to our mesh via
550 * a bridge on top of the given soft interface, dev, in the given
553 * Return: -ENOMEM on memory allocation error or the number of
554 * items added to the mcast_list otherwise.
556 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
557 struct hlist_head *mcast_list,
558 struct batadv_mcast_mla_flags *flags)
560 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
561 struct br_ip_list *br_ip_entry, *tmp;
562 u8 tvlv_flags = flags->tvlv_flags;
563 struct batadv_hw_addr *new;
564 u8 mcast_addr[ETH_ALEN];
567 /* we don't need to detect these devices/listeners, the IGMP/MLD
568 * snooping code of the Linux bridge already does that for us
570 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
574 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
575 if (br_ip_entry->addr.proto == htons(ETH_P_IP)) {
576 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
579 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
580 ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
583 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
584 !ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
588 #if IS_ENABLED(CONFIG_IPV6)
589 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) {
590 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
593 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
594 ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.dst.ip6))
597 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
598 IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.dst.ip6) >
599 IPV6_ADDR_SCOPE_LINKLOCAL)
604 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
605 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
608 new = kmalloc(sizeof(*new), GFP_ATOMIC);
614 ether_addr_copy(new->addr, mcast_addr);
615 hlist_add_head(&new->list, mcast_list);
619 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
620 list_del(&br_ip_entry->list);
628 * batadv_mcast_mla_list_free() - free a list of multicast addresses
629 * @mcast_list: the list to free
631 * Removes and frees all items in the given mcast_list.
633 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
635 struct batadv_hw_addr *mcast_entry;
636 struct hlist_node *tmp;
638 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
639 hlist_del(&mcast_entry->list);
645 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
646 * @bat_priv: the bat priv with all the soft interface information
647 * @mcast_list: a list of addresses which should _not_ be removed
649 * Retracts the announcement of any multicast listener from the
650 * translation table except the ones listed in the given mcast_list.
652 * If mcast_list is NULL then all are retracted.
654 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
655 struct hlist_head *mcast_list)
657 struct batadv_hw_addr *mcast_entry;
658 struct hlist_node *tmp;
660 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
663 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
667 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
669 "mcast TT outdated", false);
671 hlist_del(&mcast_entry->list);
677 * batadv_mcast_mla_tt_add() - add multicast listener announcements
678 * @bat_priv: the bat priv with all the soft interface information
679 * @mcast_list: a list of addresses which are going to get added
681 * Adds multicast listener announcements from the given mcast_list to the
682 * translation table if they have not been added yet.
684 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
685 struct hlist_head *mcast_list)
687 struct batadv_hw_addr *mcast_entry;
688 struct hlist_node *tmp;
693 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
694 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
695 &bat_priv->mcast.mla_list))
698 if (!batadv_tt_local_add(bat_priv->soft_iface,
699 mcast_entry->addr, BATADV_NO_FLAGS,
700 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
703 hlist_del(&mcast_entry->list);
704 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
709 * batadv_mcast_querier_log() - debug output regarding the querier status on
711 * @bat_priv: the bat priv with all the soft interface information
712 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
713 * @old_state: the previous querier state on our link
714 * @new_state: the new querier state on our link
716 * Outputs debug messages to the logging facility with log level 'mcast'
717 * regarding changes to the querier status on the link which are relevant
718 * to our multicast optimizations.
720 * Usually this is about whether a querier appeared or vanished in
721 * our mesh or whether the querier is in the suboptimal position of being
722 * behind our local bridge segment: Snooping switches will directly
723 * forward listener reports to the querier, therefore batman-adv and
724 * the bridge will potentially not see these listeners - the querier is
725 * potentially shadowing listeners from us then.
727 * This is only interesting for nodes with a bridge on top of their
731 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
732 struct batadv_mcast_querier_state *old_state,
733 struct batadv_mcast_querier_state *new_state)
735 if (!old_state->exists && new_state->exists)
736 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
738 else if (old_state->exists && !new_state->exists)
739 batadv_info(bat_priv->soft_iface,
740 "%s Querier disappeared - multicast optimizations disabled\n",
742 else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists)
743 batadv_info(bat_priv->soft_iface,
744 "No %s Querier present - multicast optimizations disabled\n",
747 if (new_state->exists) {
748 if ((!old_state->shadowing && new_state->shadowing) ||
749 (!old_state->exists && new_state->shadowing))
750 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
751 "%s Querier is behind our bridged segment: Might shadow listeners\n",
753 else if (old_state->shadowing && !new_state->shadowing)
754 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
755 "%s Querier is not behind our bridged segment\n",
761 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
763 * @bat_priv: the bat priv with all the soft interface information
764 * @new_flags: flags indicating the new multicast state
766 * If no bridges are ever used on this node, then this function does nothing.
768 * Otherwise this function outputs debug information to the 'mcast' log level
769 * which might be relevant to our multicast optimizations.
771 * More precisely, it outputs information when a bridge interface is added or
772 * removed from a soft interface. And when a bridge is present, it further
773 * outputs information about the querier state which is relevant for the
774 * multicast flags this node is going to set.
777 batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
778 struct batadv_mcast_mla_flags *new_flags)
780 struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
782 if (!old_flags->bridged && new_flags->bridged)
783 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
784 "Bridge added: Setting Unsnoopables(U)-flag\n");
785 else if (old_flags->bridged && !new_flags->bridged)
786 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
787 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
789 if (new_flags->bridged) {
790 batadv_mcast_querier_log(bat_priv, "IGMP",
791 &old_flags->querier_ipv4,
792 &new_flags->querier_ipv4);
793 batadv_mcast_querier_log(bat_priv, "MLD",
794 &old_flags->querier_ipv6,
795 &new_flags->querier_ipv6);
800 * batadv_mcast_flags_log() - output debug information about mcast flag changes
801 * @bat_priv: the bat priv with all the soft interface information
802 * @flags: TVLV flags indicating the new multicast state
804 * Whenever the multicast TVLV flags this node announces change, this function
805 * should be used to notify userspace about the change.
807 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
809 bool old_enabled = bat_priv->mcast.mla_flags.enabled;
810 u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags;
811 char str_old_flags[] = "[.... . ]";
813 sprintf(str_old_flags, "[%c%c%c%s%s]",
814 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
815 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
816 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
817 !(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
818 !(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
820 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
821 "Changing multicast flags from '%s' to '[%c%c%c%s%s]'\n",
822 old_enabled ? str_old_flags : "<undefined>",
823 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
824 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
825 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
826 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
827 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
831 * batadv_mcast_mla_flags_update() - update multicast flags
832 * @bat_priv: the bat priv with all the soft interface information
833 * @flags: flags indicating the new multicast state
835 * Updates the own multicast tvlv with our current multicast related settings,
836 * capabilities and inabilities.
839 batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
840 struct batadv_mcast_mla_flags *flags)
842 struct batadv_tvlv_mcast_data mcast_data;
844 if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
847 batadv_mcast_bridge_log(bat_priv, flags);
848 batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
850 mcast_data.flags = flags->tvlv_flags;
851 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
853 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
854 &mcast_data, sizeof(mcast_data));
856 bat_priv->mcast.mla_flags = *flags;
860 * __batadv_mcast_mla_update() - update the own MLAs
861 * @bat_priv: the bat priv with all the soft interface information
863 * Updates the own multicast listener announcements in the translation
864 * table as well as the own, announced multicast tvlv container.
866 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
867 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
868 * ensured by the non-parallel execution of the worker this function
871 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
873 struct net_device *soft_iface = bat_priv->soft_iface;
874 struct hlist_head mcast_list = HLIST_HEAD_INIT;
875 struct batadv_mcast_mla_flags flags;
878 flags = batadv_mcast_mla_flags_get(bat_priv);
880 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list, &flags);
884 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list, &flags);
888 spin_lock(&bat_priv->mcast.mla_lock);
889 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
890 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
891 batadv_mcast_mla_flags_update(bat_priv, &flags);
892 spin_unlock(&bat_priv->mcast.mla_lock);
895 batadv_mcast_mla_list_free(&mcast_list);
899 * batadv_mcast_mla_update() - update the own MLAs
900 * @work: kernel work struct
902 * Updates the own multicast listener announcements in the translation
903 * table as well as the own, announced multicast tvlv container.
905 * In the end, reschedules the work timer.
907 static void batadv_mcast_mla_update(struct work_struct *work)
909 struct delayed_work *delayed_work;
910 struct batadv_priv_mcast *priv_mcast;
911 struct batadv_priv *bat_priv;
913 delayed_work = to_delayed_work(work);
914 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
915 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
917 __batadv_mcast_mla_update(bat_priv);
918 batadv_mcast_start_timer(bat_priv);
922 * batadv_mcast_is_report_ipv4() - check for IGMP reports
923 * @skb: the ethernet frame destined for the mesh
925 * This call might reallocate skb data.
927 * Checks whether the given frame is a valid IGMP report.
929 * Return: If so then true, otherwise false.
931 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
933 if (ip_mc_check_igmp(skb) < 0)
936 switch (igmp_hdr(skb)->type) {
937 case IGMP_HOST_MEMBERSHIP_REPORT:
938 case IGMPV2_HOST_MEMBERSHIP_REPORT:
939 case IGMPV3_HOST_MEMBERSHIP_REPORT:
947 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
949 * @bat_priv: the bat priv with all the soft interface information
950 * @skb: the IPv4 packet to check
951 * @is_unsnoopable: stores whether the destination is snoopable
952 * @is_routable: stores whether the destination is routable
954 * Checks whether the given IPv4 packet has the potential to be forwarded with a
955 * mode more optimal than classic flooding.
957 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
958 * allocation failure.
960 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
962 bool *is_unsnoopable,
967 /* We might fail due to out-of-memory -> drop it */
968 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
971 if (batadv_mcast_is_report_ipv4(skb))
976 /* link-local multicast listeners behind a bridge are
977 * not snoopable (see RFC4541, section 2.1.2.2)
979 if (ipv4_is_local_multicast(iphdr->daddr))
980 *is_unsnoopable = true;
982 *is_routable = ETH_P_IP;
988 * batadv_mcast_is_report_ipv6() - check for MLD reports
989 * @skb: the ethernet frame destined for the mesh
991 * This call might reallocate skb data.
993 * Checks whether the given frame is a valid MLD report.
995 * Return: If so then true, otherwise false.
997 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
999 if (ipv6_mc_check_mld(skb) < 0)
1002 switch (icmp6_hdr(skb)->icmp6_type) {
1003 case ICMPV6_MGM_REPORT:
1004 case ICMPV6_MLD2_REPORT:
1012 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
1014 * @bat_priv: the bat priv with all the soft interface information
1015 * @skb: the IPv6 packet to check
1016 * @is_unsnoopable: stores whether the destination is snoopable
1017 * @is_routable: stores whether the destination is routable
1019 * Checks whether the given IPv6 packet has the potential to be forwarded with a
1020 * mode more optimal than classic flooding.
1022 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1024 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
1025 struct sk_buff *skb,
1026 bool *is_unsnoopable,
1029 struct ipv6hdr *ip6hdr;
1031 /* We might fail due to out-of-memory -> drop it */
1032 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
1035 if (batadv_mcast_is_report_ipv6(skb))
1038 ip6hdr = ipv6_hdr(skb);
1040 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1043 /* link-local-all-nodes multicast listeners behind a bridge are
1044 * not snoopable (see RFC4541, section 3, paragraph 3)
1046 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
1047 *is_unsnoopable = true;
1048 else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL)
1049 *is_routable = ETH_P_IPV6;
1055 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
1056 * @bat_priv: the bat priv with all the soft interface information
1057 * @skb: the multicast frame to check
1058 * @is_unsnoopable: stores whether the destination is snoopable
1059 * @is_routable: stores whether the destination is routable
1061 * Checks whether the given multicast ethernet frame has the potential to be
1062 * forwarded with a mode more optimal than classic flooding.
1064 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1066 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
1067 struct sk_buff *skb,
1068 bool *is_unsnoopable,
1071 struct ethhdr *ethhdr = eth_hdr(skb);
1073 if (!atomic_read(&bat_priv->multicast_mode))
1076 switch (ntohs(ethhdr->h_proto)) {
1078 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
1082 if (!IS_ENABLED(CONFIG_IPV6))
1085 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
1094 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
1096 * @bat_priv: the bat priv with all the soft interface information
1097 * @ethhdr: ethernet header of a packet
1099 * Return: the number of nodes which want all IPv4 multicast traffic if the
1100 * given ethhdr is from an IPv4 packet or the number of nodes which want all
1101 * IPv6 traffic if it matches an IPv6 packet.
1103 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
1104 struct ethhdr *ethhdr)
1106 switch (ntohs(ethhdr->h_proto)) {
1108 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
1110 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
1112 /* we shouldn't be here... */
1118 * batadv_mcast_forw_rtr_count() - count nodes with a multicast router
1119 * @bat_priv: the bat priv with all the soft interface information
1120 * @protocol: the ethernet protocol type to count multicast routers for
1122 * Return: the number of nodes which want all routable IPv4 multicast traffic
1123 * if the protocol is ETH_P_IP or the number of nodes which want all routable
1124 * IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0.
1127 static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
1132 return atomic_read(&bat_priv->mcast.num_want_all_rtr4);
1134 return atomic_read(&bat_priv->mcast.num_want_all_rtr6);
1141 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
1142 * @bat_priv: the bat priv with all the soft interface information
1143 * @ethhdr: the ether header containing the multicast destination
1145 * Return: an orig_node matching the multicast address provided by ethhdr
1146 * via a translation table lookup. This increases the returned nodes refcount.
1148 static struct batadv_orig_node *
1149 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
1150 struct ethhdr *ethhdr)
1152 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
1157 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
1158 * @bat_priv: the bat priv with all the soft interface information
1160 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
1161 * increases its refcount.
1163 static struct batadv_orig_node *
1164 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
1166 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1169 hlist_for_each_entry_rcu(tmp_orig_node,
1170 &bat_priv->mcast.want_all_ipv4_list,
1171 mcast_want_all_ipv4_node) {
1172 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1175 orig_node = tmp_orig_node;
1184 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
1185 * @bat_priv: the bat priv with all the soft interface information
1187 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
1188 * and increases its refcount.
1190 static struct batadv_orig_node *
1191 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
1193 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1196 hlist_for_each_entry_rcu(tmp_orig_node,
1197 &bat_priv->mcast.want_all_ipv6_list,
1198 mcast_want_all_ipv6_node) {
1199 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1202 orig_node = tmp_orig_node;
1211 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
1212 * @bat_priv: the bat priv with all the soft interface information
1213 * @ethhdr: an ethernet header to determine the protocol family from
1215 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
1216 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, sets and
1217 * increases its refcount.
1219 static struct batadv_orig_node *
1220 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
1221 struct ethhdr *ethhdr)
1223 switch (ntohs(ethhdr->h_proto)) {
1225 return batadv_mcast_forw_ipv4_node_get(bat_priv);
1227 return batadv_mcast_forw_ipv6_node_get(bat_priv);
1229 /* we shouldn't be here... */
1235 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
1236 * @bat_priv: the bat priv with all the soft interface information
1238 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
1239 * set and increases its refcount.
1241 static struct batadv_orig_node *
1242 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
1244 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1247 hlist_for_each_entry_rcu(tmp_orig_node,
1248 &bat_priv->mcast.want_all_unsnoopables_list,
1249 mcast_want_all_unsnoopables_node) {
1250 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1253 orig_node = tmp_orig_node;
1262 * batadv_mcast_forw_rtr4_node_get() - get a node with an ipv4 mcast router flag
1263 * @bat_priv: the bat priv with all the soft interface information
1265 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR4 flag unset and
1266 * increases its refcount.
1268 static struct batadv_orig_node *
1269 batadv_mcast_forw_rtr4_node_get(struct batadv_priv *bat_priv)
1271 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1274 hlist_for_each_entry_rcu(tmp_orig_node,
1275 &bat_priv->mcast.want_all_rtr4_list,
1276 mcast_want_all_rtr4_node) {
1277 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1280 orig_node = tmp_orig_node;
1289 * batadv_mcast_forw_rtr6_node_get() - get a node with an ipv6 mcast router flag
1290 * @bat_priv: the bat priv with all the soft interface information
1292 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR6 flag unset
1293 * and increases its refcount.
1295 static struct batadv_orig_node *
1296 batadv_mcast_forw_rtr6_node_get(struct batadv_priv *bat_priv)
1298 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1301 hlist_for_each_entry_rcu(tmp_orig_node,
1302 &bat_priv->mcast.want_all_rtr6_list,
1303 mcast_want_all_rtr6_node) {
1304 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1307 orig_node = tmp_orig_node;
1316 * batadv_mcast_forw_rtr_node_get() - get a node with an ipv4/ipv6 router flag
1317 * @bat_priv: the bat priv with all the soft interface information
1318 * @ethhdr: an ethernet header to determine the protocol family from
1320 * Return: an orig_node which has no BATADV_MCAST_WANT_NO_RTR4 or
1321 * BATADV_MCAST_WANT_NO_RTR6 flag, depending on the provided ethhdr, set and
1322 * increases its refcount.
1324 static struct batadv_orig_node *
1325 batadv_mcast_forw_rtr_node_get(struct batadv_priv *bat_priv,
1326 struct ethhdr *ethhdr)
1328 switch (ntohs(ethhdr->h_proto)) {
1330 return batadv_mcast_forw_rtr4_node_get(bat_priv);
1332 return batadv_mcast_forw_rtr6_node_get(bat_priv);
1334 /* we shouldn't be here... */
1340 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
1341 * @bat_priv: the bat priv with all the soft interface information
1342 * @skb: The multicast packet to check
1343 * @orig: an originator to be set to forward the skb to
1345 * Return: the forwarding mode as enum batadv_forw_mode and in case of
1346 * BATADV_FORW_SINGLE set the orig to the single originator the skb
1347 * should be forwarded to.
1349 enum batadv_forw_mode
1350 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
1351 struct batadv_orig_node **orig)
1353 int ret, tt_count, ip_count, unsnoop_count, total_count;
1354 bool is_unsnoopable = false;
1355 unsigned int mcast_fanout;
1356 struct ethhdr *ethhdr;
1357 int is_routable = 0;
1360 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable,
1363 return BATADV_FORW_NONE;
1365 return BATADV_FORW_ALL;
1367 ethhdr = eth_hdr(skb);
1369 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1371 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1372 unsnoop_count = !is_unsnoopable ? 0 :
1373 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
1374 rtr_count = batadv_mcast_forw_rtr_count(bat_priv, is_routable);
1376 total_count = tt_count + ip_count + unsnoop_count + rtr_count;
1378 switch (total_count) {
1381 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1383 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1384 else if (unsnoop_count)
1385 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1387 *orig = batadv_mcast_forw_rtr_node_get(bat_priv,
1391 return BATADV_FORW_SINGLE;
1395 return BATADV_FORW_NONE;
1397 mcast_fanout = atomic_read(&bat_priv->multicast_fanout);
1399 if (!unsnoop_count && total_count <= mcast_fanout)
1400 return BATADV_FORW_SOME;
1403 return BATADV_FORW_ALL;
1407 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1408 * @bat_priv: the bat priv with all the soft interface information
1409 * @skb: the multicast packet to send
1410 * @vid: the vlan identifier
1411 * @orig_node: the originator to send the packet to
1413 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1415 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1416 struct sk_buff *skb,
1418 struct batadv_orig_node *orig_node)
1420 /* Avoid sending multicast-in-unicast packets to other BLA
1421 * gateways - they already got the frame from the LAN side
1422 * we share with them.
1423 * TODO: Refactor to take BLA into account earlier, to avoid
1424 * reducing the mcast_fanout count.
1426 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1428 return NET_XMIT_SUCCESS;
1431 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1436 * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1437 * @bat_priv: the bat priv with all the soft interface information
1438 * @skb: the multicast packet to transmit
1439 * @vid: the vlan identifier
1441 * Sends copies of a frame with multicast destination to any multicast
1442 * listener registered in the translation table. A transmission is performed
1443 * via a batman-adv unicast packet for each such destination node.
1445 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1449 batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
1452 int ret = NET_XMIT_SUCCESS;
1453 struct sk_buff *newskb;
1455 struct batadv_tt_orig_list_entry *orig_entry;
1457 struct batadv_tt_global_entry *tt_global;
1458 const u8 *addr = eth_hdr(skb)->h_dest;
1460 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
1465 hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
1466 newskb = skb_copy(skb, GFP_ATOMIC);
1468 ret = NET_XMIT_DROP;
1472 batadv_mcast_forw_send_orig(bat_priv, newskb, vid,
1473 orig_entry->orig_node);
1477 batadv_tt_global_entry_put(tt_global);
1484 * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1485 * @bat_priv: the bat priv with all the soft interface information
1486 * @skb: the multicast packet to transmit
1487 * @vid: the vlan identifier
1489 * Sends copies of a frame with multicast destination to any node with a
1490 * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1491 * batman-adv unicast packet for each such destination node.
1493 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1497 batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
1498 struct sk_buff *skb, unsigned short vid)
1500 struct batadv_orig_node *orig_node;
1501 int ret = NET_XMIT_SUCCESS;
1502 struct sk_buff *newskb;
1505 hlist_for_each_entry_rcu(orig_node,
1506 &bat_priv->mcast.want_all_ipv4_list,
1507 mcast_want_all_ipv4_node) {
1508 newskb = skb_copy(skb, GFP_ATOMIC);
1510 ret = NET_XMIT_DROP;
1514 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1521 * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1522 * @bat_priv: the bat priv with all the soft interface information
1523 * @skb: The multicast packet to transmit
1524 * @vid: the vlan identifier
1526 * Sends copies of a frame with multicast destination to any node with a
1527 * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1528 * batman-adv unicast packet for each such destination node.
1530 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1534 batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
1535 struct sk_buff *skb, unsigned short vid)
1537 struct batadv_orig_node *orig_node;
1538 int ret = NET_XMIT_SUCCESS;
1539 struct sk_buff *newskb;
1542 hlist_for_each_entry_rcu(orig_node,
1543 &bat_priv->mcast.want_all_ipv6_list,
1544 mcast_want_all_ipv6_node) {
1545 newskb = skb_copy(skb, GFP_ATOMIC);
1547 ret = NET_XMIT_DROP;
1551 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1558 * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1559 * @bat_priv: the bat priv with all the soft interface information
1560 * @skb: the multicast packet to transmit
1561 * @vid: the vlan identifier
1563 * Sends copies of a frame with multicast destination to any node with a
1564 * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1565 * transmission is performed via a batman-adv unicast packet for each such
1568 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1569 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1572 batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
1573 struct sk_buff *skb, unsigned short vid)
1575 switch (ntohs(eth_hdr(skb)->h_proto)) {
1577 return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
1579 return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
1581 /* we shouldn't be here... */
1582 return NET_XMIT_DROP;
1587 * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
1588 * @bat_priv: the bat priv with all the soft interface information
1589 * @skb: the multicast packet to transmit
1590 * @vid: the vlan identifier
1592 * Sends copies of a frame with multicast destination to any node with a
1593 * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
1594 * batman-adv unicast packet for each such destination node.
1596 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1600 batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
1601 struct sk_buff *skb, unsigned short vid)
1603 struct batadv_orig_node *orig_node;
1604 int ret = NET_XMIT_SUCCESS;
1605 struct sk_buff *newskb;
1608 hlist_for_each_entry_rcu(orig_node,
1609 &bat_priv->mcast.want_all_rtr4_list,
1610 mcast_want_all_rtr4_node) {
1611 newskb = skb_copy(skb, GFP_ATOMIC);
1613 ret = NET_XMIT_DROP;
1617 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1624 * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
1625 * @bat_priv: the bat priv with all the soft interface information
1626 * @skb: The multicast packet to transmit
1627 * @vid: the vlan identifier
1629 * Sends copies of a frame with multicast destination to any node with a
1630 * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
1631 * batman-adv unicast packet for each such destination node.
1633 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1637 batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
1638 struct sk_buff *skb, unsigned short vid)
1640 struct batadv_orig_node *orig_node;
1641 int ret = NET_XMIT_SUCCESS;
1642 struct sk_buff *newskb;
1645 hlist_for_each_entry_rcu(orig_node,
1646 &bat_priv->mcast.want_all_rtr6_list,
1647 mcast_want_all_rtr6_node) {
1648 newskb = skb_copy(skb, GFP_ATOMIC);
1650 ret = NET_XMIT_DROP;
1654 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1661 * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
1662 * @bat_priv: the bat priv with all the soft interface information
1663 * @skb: the multicast packet to transmit
1664 * @vid: the vlan identifier
1666 * Sends copies of a frame with multicast destination to any node with a
1667 * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
1668 * transmission is performed via a batman-adv unicast packet for each such
1671 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1672 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1675 batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
1676 struct sk_buff *skb, unsigned short vid)
1678 switch (ntohs(eth_hdr(skb)->h_proto)) {
1680 return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
1682 return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
1684 /* we shouldn't be here... */
1685 return NET_XMIT_DROP;
1690 * batadv_mcast_forw_send() - send packet to any detected multicast recipient
1691 * @bat_priv: the bat priv with all the soft interface information
1692 * @skb: the multicast packet to transmit
1693 * @vid: the vlan identifier
1695 * Sends copies of a frame with multicast destination to any node that signaled
1696 * interest in it, that is either via the translation table or the according
1697 * want-all flags. A transmission is performed via a batman-adv unicast packet
1698 * for each such destination node.
1700 * The given skb is consumed/freed.
1702 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1703 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1705 int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
1710 ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
1711 if (ret != NET_XMIT_SUCCESS) {
1716 ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
1717 if (ret != NET_XMIT_SUCCESS) {
1722 ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
1723 if (ret != NET_XMIT_SUCCESS) {
1733 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1734 * @bat_priv: the bat priv with all the soft interface information
1735 * @orig: the orig_node which multicast state might have changed of
1736 * @mcast_flags: flags indicating the new multicast state
1738 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1739 * orig, has toggled then this method updates the counter and the list
1742 * Caller needs to hold orig->mcast_handler_lock.
1744 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1745 struct batadv_orig_node *orig,
1748 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1749 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1751 lockdep_assert_held(&orig->mcast_handler_lock);
1753 /* switched from flag unset to set */
1754 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1755 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1756 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1758 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1759 /* flag checks above + mcast_handler_lock prevents this */
1760 WARN_ON(!hlist_unhashed(node));
1762 hlist_add_head_rcu(node, head);
1763 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1764 /* switched from flag set to unset */
1765 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1766 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1767 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1769 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1770 /* flag checks above + mcast_handler_lock prevents this */
1771 WARN_ON(hlist_unhashed(node));
1773 hlist_del_init_rcu(node);
1774 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1779 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1780 * @bat_priv: the bat priv with all the soft interface information
1781 * @orig: the orig_node which multicast state might have changed of
1782 * @mcast_flags: flags indicating the new multicast state
1784 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1785 * toggled then this method updates the counter and the list accordingly.
1787 * Caller needs to hold orig->mcast_handler_lock.
1789 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1790 struct batadv_orig_node *orig,
1793 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1794 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1796 lockdep_assert_held(&orig->mcast_handler_lock);
1798 /* switched from flag unset to set */
1799 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1800 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1801 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1803 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1804 /* flag checks above + mcast_handler_lock prevents this */
1805 WARN_ON(!hlist_unhashed(node));
1807 hlist_add_head_rcu(node, head);
1808 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1809 /* switched from flag set to unset */
1810 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1811 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1812 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1814 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1815 /* flag checks above + mcast_handler_lock prevents this */
1816 WARN_ON(hlist_unhashed(node));
1818 hlist_del_init_rcu(node);
1819 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1824 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1825 * @bat_priv: the bat priv with all the soft interface information
1826 * @orig: the orig_node which multicast state might have changed of
1827 * @mcast_flags: flags indicating the new multicast state
1829 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1830 * toggled then this method updates the counter and the list accordingly.
1832 * Caller needs to hold orig->mcast_handler_lock.
1834 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1835 struct batadv_orig_node *orig,
1838 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1839 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1841 lockdep_assert_held(&orig->mcast_handler_lock);
1843 /* switched from flag unset to set */
1844 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1845 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1846 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1848 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1849 /* flag checks above + mcast_handler_lock prevents this */
1850 WARN_ON(!hlist_unhashed(node));
1852 hlist_add_head_rcu(node, head);
1853 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1854 /* switched from flag set to unset */
1855 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1856 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1857 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1859 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1860 /* flag checks above + mcast_handler_lock prevents this */
1861 WARN_ON(hlist_unhashed(node));
1863 hlist_del_init_rcu(node);
1864 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1869 * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
1870 * @bat_priv: the bat priv with all the soft interface information
1871 * @orig: the orig_node which multicast state might have changed of
1872 * @mcast_flags: flags indicating the new multicast state
1874 * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
1875 * toggled then this method updates the counter and the list accordingly.
1877 * Caller needs to hold orig->mcast_handler_lock.
1879 static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
1880 struct batadv_orig_node *orig,
1883 struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
1884 struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
1886 lockdep_assert_held(&orig->mcast_handler_lock);
1888 /* switched from flag set to unset */
1889 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
1890 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
1891 atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
1893 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1894 /* flag checks above + mcast_handler_lock prevents this */
1895 WARN_ON(!hlist_unhashed(node));
1897 hlist_add_head_rcu(node, head);
1898 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1899 /* switched from flag unset to set */
1900 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
1901 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
1902 atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
1904 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1905 /* flag checks above + mcast_handler_lock prevents this */
1906 WARN_ON(hlist_unhashed(node));
1908 hlist_del_init_rcu(node);
1909 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1914 * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
1915 * @bat_priv: the bat priv with all the soft interface information
1916 * @orig: the orig_node which multicast state might have changed of
1917 * @mcast_flags: flags indicating the new multicast state
1919 * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
1920 * toggled then this method updates the counter and the list accordingly.
1922 * Caller needs to hold orig->mcast_handler_lock.
1924 static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
1925 struct batadv_orig_node *orig,
1928 struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
1929 struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
1931 lockdep_assert_held(&orig->mcast_handler_lock);
1933 /* switched from flag set to unset */
1934 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
1935 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
1936 atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
1938 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1939 /* flag checks above + mcast_handler_lock prevents this */
1940 WARN_ON(!hlist_unhashed(node));
1942 hlist_add_head_rcu(node, head);
1943 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1944 /* switched from flag unset to set */
1945 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
1946 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
1947 atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
1949 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1950 /* flag checks above + mcast_handler_lock prevents this */
1951 WARN_ON(hlist_unhashed(node));
1953 hlist_del_init_rcu(node);
1954 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1959 * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
1960 * @enabled: whether the originator has multicast TVLV support enabled
1961 * @tvlv_value: tvlv buffer containing the multicast flags
1962 * @tvlv_value_len: tvlv buffer length
1964 * Return: multicast flags for the given tvlv buffer
1967 batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
1969 u8 mcast_flags = BATADV_NO_FLAGS;
1971 if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
1972 mcast_flags = *(u8 *)tvlv_value;
1975 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1976 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1979 /* remove redundant flags to avoid sending duplicate packets later */
1980 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
1981 mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
1983 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
1984 mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
1990 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1991 * @bat_priv: the bat priv with all the soft interface information
1992 * @orig: the orig_node of the ogm
1993 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1994 * @tvlv_value: tvlv buffer containing the multicast data
1995 * @tvlv_value_len: tvlv buffer length
1997 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1998 struct batadv_orig_node *orig,
2003 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2006 mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
2007 tvlv_value, tvlv_value_len);
2009 spin_lock_bh(&orig->mcast_handler_lock);
2011 if (orig_mcast_enabled &&
2012 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2013 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2014 } else if (!orig_mcast_enabled &&
2015 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2016 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2019 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
2021 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
2022 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
2023 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
2024 batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
2025 batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
2027 orig->mcast_flags = mcast_flags;
2028 spin_unlock_bh(&orig->mcast_handler_lock);
2032 * batadv_mcast_init() - initialize the multicast optimizations structures
2033 * @bat_priv: the bat priv with all the soft interface information
2035 void batadv_mcast_init(struct batadv_priv *bat_priv)
2037 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
2038 NULL, BATADV_TVLV_MCAST, 2,
2039 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2041 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
2042 batadv_mcast_start_timer(bat_priv);
2046 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
2047 * @msg: buffer for the message
2048 * @bat_priv: the bat priv with all the soft interface information
2050 * Return: 0 or error code.
2052 int batadv_mcast_mesh_info_put(struct sk_buff *msg,
2053 struct batadv_priv *bat_priv)
2055 u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
2056 u32 flags_priv = BATADV_NO_FLAGS;
2058 if (bat_priv->mcast.mla_flags.bridged) {
2059 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
2061 if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
2062 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
2063 if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
2064 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
2065 if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
2066 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
2067 if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
2068 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
2071 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
2072 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
2079 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
2080 * to a netlink socket
2081 * @msg: buffer for the message
2082 * @portid: netlink port
2083 * @cb: Control block containing additional options
2084 * @orig_node: originator to dump the multicast flags of
2086 * Return: 0 or error code.
2089 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
2090 struct netlink_callback *cb,
2091 struct batadv_orig_node *orig_node)
2095 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2096 &batadv_netlink_family, NLM_F_MULTI,
2097 BATADV_CMD_GET_MCAST_FLAGS);
2101 genl_dump_check_consistent(cb, hdr);
2103 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2105 genlmsg_cancel(msg, hdr);
2109 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2110 &orig_node->capabilities)) {
2111 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
2112 orig_node->mcast_flags)) {
2113 genlmsg_cancel(msg, hdr);
2118 genlmsg_end(msg, hdr);
2123 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
2124 * table to a netlink socket
2125 * @msg: buffer for the message
2126 * @portid: netlink port
2127 * @cb: Control block containing additional options
2128 * @hash: hash to dump
2129 * @bucket: bucket index to dump
2130 * @idx_skip: How many entries to skip
2132 * Return: 0 or error code.
2135 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
2136 struct netlink_callback *cb,
2137 struct batadv_hashtable *hash,
2138 unsigned int bucket, long *idx_skip)
2140 struct batadv_orig_node *orig_node;
2143 spin_lock_bh(&hash->list_locks[bucket]);
2144 cb->seq = atomic_read(&hash->generation) << 1 | 1;
2146 hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
2147 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2148 &orig_node->capa_initialized))
2151 if (idx < *idx_skip)
2154 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
2155 spin_unlock_bh(&hash->list_locks[bucket]);
2164 spin_unlock_bh(&hash->list_locks[bucket]);
2170 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2171 * @msg: buffer for the message
2172 * @portid: netlink port
2173 * @cb: Control block containing additional options
2174 * @bat_priv: the bat priv with all the soft interface information
2175 * @bucket: current bucket to dump
2176 * @idx: index in current bucket to the next entry to dump
2178 * Return: 0 or error code.
2181 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
2182 struct netlink_callback *cb,
2183 struct batadv_priv *bat_priv, long *bucket, long *idx)
2185 struct batadv_hashtable *hash = bat_priv->orig_hash;
2186 long bucket_tmp = *bucket;
2187 long idx_tmp = *idx;
2189 while (bucket_tmp < hash->size) {
2190 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
2191 bucket_tmp, &idx_tmp))
2198 *bucket = bucket_tmp;
2205 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
2207 * @cb: netlink callback structure
2208 * @primary_if: the primary interface pointer to return the result in
2210 * Return: 0 or error code.
2213 batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
2214 struct batadv_hard_iface **primary_if)
2216 struct batadv_hard_iface *hard_iface = NULL;
2217 struct net *net = sock_net(cb->skb->sk);
2218 struct net_device *soft_iface;
2219 struct batadv_priv *bat_priv;
2223 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2227 soft_iface = dev_get_by_index(net, ifindex);
2228 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2233 bat_priv = netdev_priv(soft_iface);
2235 hard_iface = batadv_primary_if_get_selected(bat_priv);
2236 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
2243 dev_put(soft_iface);
2245 if (!ret && primary_if)
2246 *primary_if = hard_iface;
2247 else if (hard_iface)
2248 batadv_hardif_put(hard_iface);
2254 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2255 * @msg: buffer for the message
2256 * @cb: callback structure containing arguments
2258 * Return: message length.
2260 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
2262 struct batadv_hard_iface *primary_if = NULL;
2263 int portid = NETLINK_CB(cb->skb).portid;
2264 struct batadv_priv *bat_priv;
2265 long *bucket = &cb->args[0];
2266 long *idx = &cb->args[1];
2269 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
2273 bat_priv = netdev_priv(primary_if->soft_iface);
2274 ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
2276 batadv_hardif_put(primary_if);
2281 * batadv_mcast_free() - free the multicast optimizations structures
2282 * @bat_priv: the bat priv with all the soft interface information
2284 void batadv_mcast_free(struct batadv_priv *bat_priv)
2286 cancel_delayed_work_sync(&bat_priv->mcast.work);
2288 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2289 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2291 /* safely calling outside of worker, as worker was canceled above */
2292 batadv_mcast_mla_tt_retract(bat_priv, NULL);
2296 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
2297 * @orig: the originator which is going to get purged
2299 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
2301 struct batadv_priv *bat_priv = orig->bat_priv;
2303 spin_lock_bh(&orig->mcast_handler_lock);
2305 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
2306 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
2307 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
2308 batadv_mcast_want_rtr4_update(bat_priv, orig,
2309 BATADV_MCAST_WANT_NO_RTR4);
2310 batadv_mcast_want_rtr6_update(bat_priv, orig,
2311 BATADV_MCAST_WANT_NO_RTR6);
2313 spin_unlock_bh(&orig->mcast_handler_lock);