1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2014-2018 B.A.T.M.A.N. contributors:
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "multicast.h"
22 #include <linux/atomic.h>
23 #include <linux/bitops.h>
24 #include <linux/bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/errno.h>
27 #include <linux/etherdevice.h>
28 #include <linux/gfp.h>
29 #include <linux/icmpv6.h>
30 #include <linux/if_bridge.h>
31 #include <linux/if_ether.h>
32 #include <linux/igmp.h>
34 #include <linux/in6.h>
36 #include <linux/ipv6.h>
37 #include <linux/jiffies.h>
38 #include <linux/kernel.h>
39 #include <linux/kref.h>
40 #include <linux/list.h>
41 #include <linux/lockdep.h>
42 #include <linux/netdevice.h>
43 #include <linux/netlink.h>
44 #include <linux/printk.h>
45 #include <linux/rculist.h>
46 #include <linux/rcupdate.h>
47 #include <linux/seq_file.h>
48 #include <linux/skbuff.h>
49 #include <linux/slab.h>
50 #include <linux/spinlock.h>
51 #include <linux/stddef.h>
52 #include <linux/string.h>
53 #include <linux/types.h>
54 #include <linux/workqueue.h>
55 #include <net/addrconf.h>
56 #include <net/genetlink.h>
57 #include <net/if_inet6.h>
60 #include <net/netlink.h>
62 #include <uapi/linux/batadv_packet.h>
63 #include <uapi/linux/batman_adv.h>
65 #include "bridge_loop_avoidance.h"
66 #include "hard-interface.h"
71 #include "soft-interface.h"
72 #include "translation-table.h"
75 static void batadv_mcast_mla_update(struct work_struct *work);
78 * batadv_mcast_start_timer() - schedule the multicast periodic worker
79 * @bat_priv: the bat priv with all the soft interface information
81 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
83 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
84 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
88 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
89 * @soft_iface: netdev struct of the mesh interface
91 * If the given soft interface has a bridge on top then the refcount
92 * of the according net device is increased.
94 * Return: NULL if no such bridge exists. Otherwise the net device of the
97 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
99 struct net_device *upper = soft_iface;
103 upper = netdev_master_upper_dev_get_rcu(upper);
104 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
114 * batadv_mcast_addr_is_ipv4() - check if multicast MAC is IPv4
115 * @addr: the MAC address to check
117 * Return: True, if MAC address is one reserved for IPv4 multicast, false
120 static bool batadv_mcast_addr_is_ipv4(const u8 *addr)
122 static const u8 prefix[] = {0x01, 0x00, 0x5E};
124 return memcmp(prefix, addr, sizeof(prefix)) == 0;
128 * batadv_mcast_addr_is_ipv6() - check if multicast MAC is IPv6
129 * @addr: the MAC address to check
131 * Return: True, if MAC address is one reserved for IPv6 multicast, false
134 static bool batadv_mcast_addr_is_ipv6(const u8 *addr)
136 static const u8 prefix[] = {0x33, 0x33};
138 return memcmp(prefix, addr, sizeof(prefix)) == 0;
142 * batadv_mcast_mla_softif_get() - get softif multicast listeners
143 * @bat_priv: the bat priv with all the soft interface information
144 * @dev: the device to collect multicast addresses from
145 * @mcast_list: a list to put found addresses into
147 * Collects multicast addresses of multicast listeners residing
148 * on this kernel on the given soft interface, dev, in
149 * the given mcast_list. In general, multicast listeners provided by
150 * your multicast receiving applications run directly on this node.
152 * If there is a bridge interface on top of dev, collects from that one
153 * instead. Just like with IP addresses and routes, multicast listeners
154 * will(/should) register to the bridge interface instead of an
157 * Return: -ENOMEM on memory allocation error or the number of
158 * items added to the mcast_list otherwise.
160 static int batadv_mcast_mla_softif_get(struct batadv_priv *bat_priv,
161 struct net_device *dev,
162 struct hlist_head *mcast_list)
164 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
165 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
166 struct net_device *bridge = batadv_mcast_get_bridge(dev);
167 struct netdev_hw_addr *mc_list_entry;
168 struct batadv_hw_addr *new;
171 netif_addr_lock_bh(bridge ? bridge : dev);
172 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
173 if (all_ipv4 && batadv_mcast_addr_is_ipv4(mc_list_entry->addr))
176 if (all_ipv6 && batadv_mcast_addr_is_ipv6(mc_list_entry->addr))
179 new = kmalloc(sizeof(*new), GFP_ATOMIC);
185 ether_addr_copy(new->addr, mc_list_entry->addr);
186 hlist_add_head(&new->list, mcast_list);
189 netif_addr_unlock_bh(bridge ? bridge : dev);
198 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
199 * @mcast_addr: the multicast address to check
200 * @mcast_list: the list with multicast addresses to search in
202 * Return: true if the given address is already in the given list.
203 * Otherwise returns false.
205 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
206 struct hlist_head *mcast_list)
208 struct batadv_hw_addr *mcast_entry;
210 hlist_for_each_entry(mcast_entry, mcast_list, list)
211 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
218 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
219 * @dst: destination to write to - a multicast MAC address
220 * @src: source to read from - a multicast IP address
222 * Converts a given multicast IPv4/IPv6 address from a bridge
223 * to its matching multicast MAC address and copies it into the given
224 * destination buffer.
226 * Caller needs to make sure the destination buffer can hold
227 * at least ETH_ALEN bytes.
229 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
231 if (src->proto == htons(ETH_P_IP))
232 ip_eth_mc_map(src->u.ip4, dst);
233 #if IS_ENABLED(CONFIG_IPV6)
234 else if (src->proto == htons(ETH_P_IPV6))
235 ipv6_eth_mc_map(&src->u.ip6, dst);
242 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
243 * @bat_priv: the bat priv with all the soft interface information
244 * @dev: a bridge slave whose bridge to collect multicast addresses from
245 * @mcast_list: a list to put found addresses into
247 * Collects multicast addresses of multicast listeners residing
248 * on foreign, non-mesh devices which we gave access to our mesh via
249 * a bridge on top of the given soft interface, dev, in the given
252 * Return: -ENOMEM on memory allocation error or the number of
253 * items added to the mcast_list otherwise.
255 static int batadv_mcast_mla_bridge_get(struct batadv_priv *bat_priv,
256 struct net_device *dev,
257 struct hlist_head *mcast_list)
259 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
260 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
261 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
262 struct br_ip_list *br_ip_entry, *tmp;
263 struct batadv_hw_addr *new;
264 u8 mcast_addr[ETH_ALEN];
267 /* we don't need to detect these devices/listeners, the IGMP/MLD
268 * snooping code of the Linux bridge already does that for us
270 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
274 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
275 if (all_ipv4 && br_ip_entry->addr.proto == htons(ETH_P_IP))
278 if (all_ipv6 && br_ip_entry->addr.proto == htons(ETH_P_IPV6))
281 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
282 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
285 new = kmalloc(sizeof(*new), GFP_ATOMIC);
291 ether_addr_copy(new->addr, mcast_addr);
292 hlist_add_head(&new->list, mcast_list);
296 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
297 list_del(&br_ip_entry->list);
305 * batadv_mcast_mla_list_free() - free a list of multicast addresses
306 * @mcast_list: the list to free
308 * Removes and frees all items in the given mcast_list.
310 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
312 struct batadv_hw_addr *mcast_entry;
313 struct hlist_node *tmp;
315 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
316 hlist_del(&mcast_entry->list);
322 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
323 * @bat_priv: the bat priv with all the soft interface information
324 * @mcast_list: a list of addresses which should _not_ be removed
326 * Retracts the announcement of any multicast listener from the
327 * translation table except the ones listed in the given mcast_list.
329 * If mcast_list is NULL then all are retracted.
331 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
332 struct hlist_head *mcast_list)
334 struct batadv_hw_addr *mcast_entry;
335 struct hlist_node *tmp;
337 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
340 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
344 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
346 "mcast TT outdated", false);
348 hlist_del(&mcast_entry->list);
354 * batadv_mcast_mla_tt_add() - add multicast listener announcements
355 * @bat_priv: the bat priv with all the soft interface information
356 * @mcast_list: a list of addresses which are going to get added
358 * Adds multicast listener announcements from the given mcast_list to the
359 * translation table if they have not been added yet.
361 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
362 struct hlist_head *mcast_list)
364 struct batadv_hw_addr *mcast_entry;
365 struct hlist_node *tmp;
370 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
371 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
372 &bat_priv->mcast.mla_list))
375 if (!batadv_tt_local_add(bat_priv->soft_iface,
376 mcast_entry->addr, BATADV_NO_FLAGS,
377 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
380 hlist_del(&mcast_entry->list);
381 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
386 * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
387 * @bat_priv: the bat priv with all the soft interface information
389 * Checks whether there is a bridge on top of our soft interface.
391 * Return: true if there is a bridge, false otherwise.
393 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
395 struct net_device *upper = bat_priv->soft_iface;
399 upper = netdev_master_upper_dev_get_rcu(upper);
400 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
407 * batadv_mcast_querier_log() - debug output regarding the querier status on
409 * @bat_priv: the bat priv with all the soft interface information
410 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
411 * @old_state: the previous querier state on our link
412 * @new_state: the new querier state on our link
414 * Outputs debug messages to the logging facility with log level 'mcast'
415 * regarding changes to the querier status on the link which are relevant
416 * to our multicast optimizations.
418 * Usually this is about whether a querier appeared or vanished in
419 * our mesh or whether the querier is in the suboptimal position of being
420 * behind our local bridge segment: Snooping switches will directly
421 * forward listener reports to the querier, therefore batman-adv and
422 * the bridge will potentially not see these listeners - the querier is
423 * potentially shadowing listeners from us then.
425 * This is only interesting for nodes with a bridge on top of their
429 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
430 struct batadv_mcast_querier_state *old_state,
431 struct batadv_mcast_querier_state *new_state)
433 if (!old_state->exists && new_state->exists)
434 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
436 else if (old_state->exists && !new_state->exists)
437 batadv_info(bat_priv->soft_iface,
438 "%s Querier disappeared - multicast optimizations disabled\n",
440 else if (!bat_priv->mcast.bridged && !new_state->exists)
441 batadv_info(bat_priv->soft_iface,
442 "No %s Querier present - multicast optimizations disabled\n",
445 if (new_state->exists) {
446 if ((!old_state->shadowing && new_state->shadowing) ||
447 (!old_state->exists && new_state->shadowing))
448 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
449 "%s Querier is behind our bridged segment: Might shadow listeners\n",
451 else if (old_state->shadowing && !new_state->shadowing)
452 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
453 "%s Querier is not behind our bridged segment\n",
459 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
461 * @bat_priv: the bat priv with all the soft interface information
462 * @bridged: a flag about whether the soft interface is currently bridged or not
463 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
464 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
466 * If no bridges are ever used on this node, then this function does nothing.
468 * Otherwise this function outputs debug information to the 'mcast' log level
469 * which might be relevant to our multicast optimizations.
471 * More precisely, it outputs information when a bridge interface is added or
472 * removed from a soft interface. And when a bridge is present, it further
473 * outputs information about the querier state which is relevant for the
474 * multicast flags this node is going to set.
477 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
478 struct batadv_mcast_querier_state *querier_ipv4,
479 struct batadv_mcast_querier_state *querier_ipv6)
481 if (!bat_priv->mcast.bridged && bridged)
482 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
483 "Bridge added: Setting Unsnoopables(U)-flag\n");
484 else if (bat_priv->mcast.bridged && !bridged)
485 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
486 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
489 batadv_mcast_querier_log(bat_priv, "IGMP",
490 &bat_priv->mcast.querier_ipv4,
492 batadv_mcast_querier_log(bat_priv, "MLD",
493 &bat_priv->mcast.querier_ipv6,
499 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
500 * @bat_priv: the bat priv with all the soft interface information
501 * @flags: flags indicating the new multicast state
503 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
504 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
506 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
508 u8 old_flags = bat_priv->mcast.flags;
509 char str_old_flags[] = "[...]";
511 sprintf(str_old_flags, "[%c%c%c]",
512 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
513 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
514 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
516 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
517 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
518 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
519 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
520 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
521 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
525 * batadv_mcast_mla_tvlv_update() - update multicast tvlv
526 * @bat_priv: the bat priv with all the soft interface information
528 * Updates the own multicast tvlv with our current multicast related settings,
529 * capabilities and inabilities.
531 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
534 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
536 struct batadv_tvlv_mcast_data mcast_data;
537 struct batadv_mcast_querier_state querier4 = {false, false};
538 struct batadv_mcast_querier_state querier6 = {false, false};
539 struct net_device *dev = bat_priv->soft_iface;
542 mcast_data.flags = BATADV_NO_FLAGS;
543 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
545 bridged = batadv_mcast_has_bridge(bat_priv);
549 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
550 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
552 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
553 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
555 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
556 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
558 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
560 /* 1) If no querier exists at all, then multicast listeners on
561 * our local TT clients behind the bridge will keep silent.
562 * 2) If the selected querier is on one of our local TT clients,
563 * behind the bridge, then this querier might shadow multicast
564 * listeners on our local TT clients, behind this bridge.
566 * In both cases, we will signalize other batman nodes that
567 * we need all multicast traffic of the according protocol.
569 if (!querier4.exists || querier4.shadowing)
570 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
572 if (!querier6.exists || querier6.shadowing)
573 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
576 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
578 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
579 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
581 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
582 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
584 bat_priv->mcast.bridged = bridged;
586 if (!bat_priv->mcast.enabled ||
587 mcast_data.flags != bat_priv->mcast.flags) {
588 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
589 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
590 &mcast_data, sizeof(mcast_data));
591 bat_priv->mcast.flags = mcast_data.flags;
592 bat_priv->mcast.enabled = true;
595 return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
596 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
600 * __batadv_mcast_mla_update() - update the own MLAs
601 * @bat_priv: the bat priv with all the soft interface information
603 * Updates the own multicast listener announcements in the translation
604 * table as well as the own, announced multicast tvlv container.
606 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
607 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
608 * ensured by the non-parallel execution of the worker this function
611 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
613 struct net_device *soft_iface = bat_priv->soft_iface;
614 struct hlist_head mcast_list = HLIST_HEAD_INIT;
617 if (!batadv_mcast_mla_tvlv_update(bat_priv))
620 ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
624 ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
629 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
630 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
633 batadv_mcast_mla_list_free(&mcast_list);
637 * batadv_mcast_mla_update() - update the own MLAs
638 * @work: kernel work struct
640 * Updates the own multicast listener announcements in the translation
641 * table as well as the own, announced multicast tvlv container.
643 * In the end, reschedules the work timer.
645 static void batadv_mcast_mla_update(struct work_struct *work)
647 struct delayed_work *delayed_work;
648 struct batadv_priv_mcast *priv_mcast;
649 struct batadv_priv *bat_priv;
651 delayed_work = to_delayed_work(work);
652 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
653 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
655 spin_lock(&bat_priv->mcast.mla_lock);
656 __batadv_mcast_mla_update(bat_priv);
657 spin_unlock(&bat_priv->mcast.mla_lock);
659 batadv_mcast_start_timer(bat_priv);
663 * batadv_mcast_is_report_ipv4() - check for IGMP reports
664 * @skb: the ethernet frame destined for the mesh
666 * This call might reallocate skb data.
668 * Checks whether the given frame is a valid IGMP report.
670 * Return: If so then true, otherwise false.
672 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
674 if (ip_mc_check_igmp(skb, NULL) < 0)
677 switch (igmp_hdr(skb)->type) {
678 case IGMP_HOST_MEMBERSHIP_REPORT:
679 case IGMPV2_HOST_MEMBERSHIP_REPORT:
680 case IGMPV3_HOST_MEMBERSHIP_REPORT:
688 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
690 * @bat_priv: the bat priv with all the soft interface information
691 * @skb: the IPv4 packet to check
692 * @is_unsnoopable: stores whether the destination is snoopable
694 * Checks whether the given IPv4 packet has the potential to be forwarded with a
695 * mode more optimal than classic flooding.
697 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
698 * allocation failure.
700 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
702 bool *is_unsnoopable)
706 /* We might fail due to out-of-memory -> drop it */
707 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
710 if (batadv_mcast_is_report_ipv4(skb))
715 /* TODO: Implement Multicast Router Discovery (RFC4286),
716 * then allow scope > link local, too
718 if (!ipv4_is_local_multicast(iphdr->daddr))
721 /* link-local multicast listeners behind a bridge are
722 * not snoopable (see RFC4541, section 2.1.2.2)
724 *is_unsnoopable = true;
730 * batadv_mcast_is_report_ipv6() - check for MLD reports
731 * @skb: the ethernet frame destined for the mesh
733 * This call might reallocate skb data.
735 * Checks whether the given frame is a valid MLD report.
737 * Return: If so then true, otherwise false.
739 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
741 if (ipv6_mc_check_mld(skb, NULL) < 0)
744 switch (icmp6_hdr(skb)->icmp6_type) {
745 case ICMPV6_MGM_REPORT:
746 case ICMPV6_MLD2_REPORT:
754 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
756 * @bat_priv: the bat priv with all the soft interface information
757 * @skb: the IPv6 packet to check
758 * @is_unsnoopable: stores whether the destination is snoopable
760 * Checks whether the given IPv6 packet has the potential to be forwarded with a
761 * mode more optimal than classic flooding.
763 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
765 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
767 bool *is_unsnoopable)
769 struct ipv6hdr *ip6hdr;
771 /* We might fail due to out-of-memory -> drop it */
772 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
775 if (batadv_mcast_is_report_ipv6(skb))
778 ip6hdr = ipv6_hdr(skb);
780 /* TODO: Implement Multicast Router Discovery (RFC4286),
781 * then allow scope > link local, too
783 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
786 /* link-local-all-nodes multicast listeners behind a bridge are
787 * not snoopable (see RFC4541, section 3, paragraph 3)
789 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
790 *is_unsnoopable = true;
796 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
797 * @bat_priv: the bat priv with all the soft interface information
798 * @skb: the multicast frame to check
799 * @is_unsnoopable: stores whether the destination is snoopable
801 * Checks whether the given multicast ethernet frame has the potential to be
802 * forwarded with a mode more optimal than classic flooding.
804 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
806 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
808 bool *is_unsnoopable)
810 struct ethhdr *ethhdr = eth_hdr(skb);
812 if (!atomic_read(&bat_priv->multicast_mode))
815 switch (ntohs(ethhdr->h_proto)) {
817 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
820 if (!IS_ENABLED(CONFIG_IPV6))
823 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
831 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
833 * @bat_priv: the bat priv with all the soft interface information
834 * @ethhdr: ethernet header of a packet
836 * Return: the number of nodes which want all IPv4 multicast traffic if the
837 * given ethhdr is from an IPv4 packet or the number of nodes which want all
838 * IPv6 traffic if it matches an IPv6 packet.
840 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
841 struct ethhdr *ethhdr)
843 switch (ntohs(ethhdr->h_proto)) {
845 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
847 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
849 /* we shouldn't be here... */
855 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
856 * @bat_priv: the bat priv with all the soft interface information
857 * @ethhdr: the ether header containing the multicast destination
859 * Return: an orig_node matching the multicast address provided by ethhdr
860 * via a translation table lookup. This increases the returned nodes refcount.
862 static struct batadv_orig_node *
863 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
864 struct ethhdr *ethhdr)
866 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
871 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
872 * @bat_priv: the bat priv with all the soft interface information
874 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
875 * increases its refcount.
877 static struct batadv_orig_node *
878 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
880 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
883 hlist_for_each_entry_rcu(tmp_orig_node,
884 &bat_priv->mcast.want_all_ipv4_list,
885 mcast_want_all_ipv4_node) {
886 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
889 orig_node = tmp_orig_node;
898 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
899 * @bat_priv: the bat priv with all the soft interface information
901 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
902 * and increases its refcount.
904 static struct batadv_orig_node *
905 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
907 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
910 hlist_for_each_entry_rcu(tmp_orig_node,
911 &bat_priv->mcast.want_all_ipv6_list,
912 mcast_want_all_ipv6_node) {
913 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
916 orig_node = tmp_orig_node;
925 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
926 * @bat_priv: the bat priv with all the soft interface information
927 * @ethhdr: an ethernet header to determine the protocol family from
929 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
930 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
931 * increases its refcount.
933 static struct batadv_orig_node *
934 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
935 struct ethhdr *ethhdr)
937 switch (ntohs(ethhdr->h_proto)) {
939 return batadv_mcast_forw_ipv4_node_get(bat_priv);
941 return batadv_mcast_forw_ipv6_node_get(bat_priv);
943 /* we shouldn't be here... */
949 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
950 * @bat_priv: the bat priv with all the soft interface information
952 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
953 * set and increases its refcount.
955 static struct batadv_orig_node *
956 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
958 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
961 hlist_for_each_entry_rcu(tmp_orig_node,
962 &bat_priv->mcast.want_all_unsnoopables_list,
963 mcast_want_all_unsnoopables_node) {
964 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
967 orig_node = tmp_orig_node;
976 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
977 * @bat_priv: the bat priv with all the soft interface information
978 * @skb: The multicast packet to check
979 * @orig: an originator to be set to forward the skb to
981 * Return: the forwarding mode as enum batadv_forw_mode and in case of
982 * BATADV_FORW_SINGLE set the orig to the single originator the skb
983 * should be forwarded to.
985 enum batadv_forw_mode
986 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
987 struct batadv_orig_node **orig)
989 int ret, tt_count, ip_count, unsnoop_count, total_count;
990 bool is_unsnoopable = false;
991 struct ethhdr *ethhdr;
993 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
995 return BATADV_FORW_NONE;
997 return BATADV_FORW_ALL;
999 ethhdr = eth_hdr(skb);
1001 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1003 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1004 unsnoop_count = !is_unsnoopable ? 0 :
1005 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
1007 total_count = tt_count + ip_count + unsnoop_count;
1009 switch (total_count) {
1012 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1014 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1015 else if (unsnoop_count)
1016 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1019 return BATADV_FORW_SINGLE;
1023 return BATADV_FORW_NONE;
1025 return BATADV_FORW_ALL;
1030 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1031 * @bat_priv: the bat priv with all the soft interface information
1032 * @skb: the multicast packet to send
1033 * @vid: the vlan identifier
1034 * @orig_node: the originator to send the packet to
1036 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1038 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1039 struct sk_buff *skb,
1041 struct batadv_orig_node *orig_node)
1043 /* Avoid sending multicast-in-unicast packets to other BLA
1044 * gateways - they already got the frame from the LAN side
1045 * we share with them.
1046 * TODO: Refactor to take BLA into account earlier, to avoid
1047 * reducing the mcast_fanout count.
1049 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1051 return NET_XMIT_SUCCESS;
1054 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1059 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1060 * @bat_priv: the bat priv with all the soft interface information
1061 * @orig: the orig_node which multicast state might have changed of
1062 * @mcast_flags: flags indicating the new multicast state
1064 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1065 * orig, has toggled then this method updates counter and list accordingly.
1067 * Caller needs to hold orig->mcast_handler_lock.
1069 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1070 struct batadv_orig_node *orig,
1073 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1074 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1076 lockdep_assert_held(&orig->mcast_handler_lock);
1078 /* switched from flag unset to set */
1079 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1080 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1081 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1083 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1084 /* flag checks above + mcast_handler_lock prevents this */
1085 WARN_ON(!hlist_unhashed(node));
1087 hlist_add_head_rcu(node, head);
1088 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1089 /* switched from flag set to unset */
1090 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1091 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1092 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1094 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1095 /* flag checks above + mcast_handler_lock prevents this */
1096 WARN_ON(hlist_unhashed(node));
1098 hlist_del_init_rcu(node);
1099 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1104 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1105 * @bat_priv: the bat priv with all the soft interface information
1106 * @orig: the orig_node which multicast state might have changed of
1107 * @mcast_flags: flags indicating the new multicast state
1109 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1110 * toggled then this method updates counter and list accordingly.
1112 * Caller needs to hold orig->mcast_handler_lock.
1114 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1115 struct batadv_orig_node *orig,
1118 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1119 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1121 lockdep_assert_held(&orig->mcast_handler_lock);
1123 /* switched from flag unset to set */
1124 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1125 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1126 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1128 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1129 /* flag checks above + mcast_handler_lock prevents this */
1130 WARN_ON(!hlist_unhashed(node));
1132 hlist_add_head_rcu(node, head);
1133 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1134 /* switched from flag set to unset */
1135 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1136 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1137 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1139 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1140 /* flag checks above + mcast_handler_lock prevents this */
1141 WARN_ON(hlist_unhashed(node));
1143 hlist_del_init_rcu(node);
1144 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1149 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1150 * @bat_priv: the bat priv with all the soft interface information
1151 * @orig: the orig_node which multicast state might have changed of
1152 * @mcast_flags: flags indicating the new multicast state
1154 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1155 * toggled then this method updates counter and list accordingly.
1157 * Caller needs to hold orig->mcast_handler_lock.
1159 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1160 struct batadv_orig_node *orig,
1163 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1164 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1166 lockdep_assert_held(&orig->mcast_handler_lock);
1168 /* switched from flag unset to set */
1169 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1170 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1171 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1173 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1174 /* flag checks above + mcast_handler_lock prevents this */
1175 WARN_ON(!hlist_unhashed(node));
1177 hlist_add_head_rcu(node, head);
1178 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1179 /* switched from flag set to unset */
1180 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1181 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1182 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1184 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1185 /* flag checks above + mcast_handler_lock prevents this */
1186 WARN_ON(hlist_unhashed(node));
1188 hlist_del_init_rcu(node);
1189 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1194 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1195 * @bat_priv: the bat priv with all the soft interface information
1196 * @orig: the orig_node of the ogm
1197 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1198 * @tvlv_value: tvlv buffer containing the multicast data
1199 * @tvlv_value_len: tvlv buffer length
1201 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1202 struct batadv_orig_node *orig,
1207 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1208 u8 mcast_flags = BATADV_NO_FLAGS;
1210 if (orig_mcast_enabled && tvlv_value &&
1211 tvlv_value_len >= sizeof(mcast_flags))
1212 mcast_flags = *(u8 *)tvlv_value;
1214 if (!orig_mcast_enabled) {
1215 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1216 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1219 spin_lock_bh(&orig->mcast_handler_lock);
1221 if (orig_mcast_enabled &&
1222 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1223 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1224 } else if (!orig_mcast_enabled &&
1225 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1226 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1229 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1231 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1232 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1233 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1235 orig->mcast_flags = mcast_flags;
1236 spin_unlock_bh(&orig->mcast_handler_lock);
1240 * batadv_mcast_init() - initialize the multicast optimizations structures
1241 * @bat_priv: the bat priv with all the soft interface information
1243 void batadv_mcast_init(struct batadv_priv *bat_priv)
1245 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1246 NULL, BATADV_TVLV_MCAST, 2,
1247 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1249 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1250 batadv_mcast_start_timer(bat_priv);
1253 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1255 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
1256 * @bat_priv: the bat priv with all the soft interface information
1257 * @seq: debugfs table seq_file struct
1259 * Prints our own multicast flags including a more specific reason why
1260 * they are set, that is prints the bridge and querier state too, to
1261 * the debugfs table specified via @seq.
1263 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1264 struct seq_file *seq)
1266 u8 flags = bat_priv->mcast.flags;
1267 char querier4, querier6, shadowing4, shadowing6;
1268 bool bridged = bat_priv->mcast.bridged;
1271 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1272 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1273 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1274 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1282 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1283 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1284 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1285 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1286 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1287 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1288 querier4, querier6);
1289 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1290 shadowing4, shadowing6);
1291 seq_puts(seq, "-------------------------------------------\n");
1292 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1296 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
1297 * @seq: seq file to print on
1300 * This prints a table of (primary) originators and their according
1301 * multicast flags, including (in the header) our own.
1305 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1307 struct net_device *net_dev = (struct net_device *)seq->private;
1308 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1309 struct batadv_hard_iface *primary_if;
1310 struct batadv_hashtable *hash = bat_priv->orig_hash;
1311 struct batadv_orig_node *orig_node;
1312 struct hlist_head *head;
1316 primary_if = batadv_seq_print_text_primary_if_get(seq);
1320 batadv_mcast_flags_print_header(bat_priv, seq);
1322 for (i = 0; i < hash->size; i++) {
1323 head = &hash->table[i];
1326 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1327 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1328 &orig_node->capa_initialized))
1331 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1332 &orig_node->capabilities)) {
1333 seq_printf(seq, "%pM -\n", orig_node->orig);
1337 flags = orig_node->mcast_flags;
1339 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1340 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1342 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1344 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1350 batadv_hardif_put(primary_if);
1357 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
1358 * @msg: buffer for the message
1359 * @bat_priv: the bat priv with all the soft interface information
1361 * Return: 0 or error code.
1363 int batadv_mcast_mesh_info_put(struct sk_buff *msg,
1364 struct batadv_priv *bat_priv)
1366 u32 flags = bat_priv->mcast.flags;
1367 u32 flags_priv = BATADV_NO_FLAGS;
1369 if (bat_priv->mcast.bridged) {
1370 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
1372 if (bat_priv->mcast.querier_ipv4.exists)
1373 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
1374 if (bat_priv->mcast.querier_ipv6.exists)
1375 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
1376 if (bat_priv->mcast.querier_ipv4.shadowing)
1377 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
1378 if (bat_priv->mcast.querier_ipv6.shadowing)
1379 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
1382 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
1383 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
1390 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
1391 * to a netlink socket
1392 * @msg: buffer for the message
1393 * @portid: netlink port
1394 * @seq: Sequence number of netlink message
1395 * @orig_node: originator to dump the multicast flags of
1397 * Return: 0 or error code.
1400 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1401 struct batadv_orig_node *orig_node)
1405 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1406 NLM_F_MULTI, BATADV_CMD_GET_MCAST_FLAGS);
1410 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1412 genlmsg_cancel(msg, hdr);
1416 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1417 &orig_node->capabilities)) {
1418 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
1419 orig_node->mcast_flags)) {
1420 genlmsg_cancel(msg, hdr);
1425 genlmsg_end(msg, hdr);
1430 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
1431 * table to a netlink socket
1432 * @msg: buffer for the message
1433 * @portid: netlink port
1434 * @seq: Sequence number of netlink message
1435 * @head: bucket to dump
1436 * @idx_skip: How many entries to skip
1438 * Return: 0 or error code.
1441 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1442 struct hlist_head *head, long *idx_skip)
1444 struct batadv_orig_node *orig_node;
1448 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1449 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1450 &orig_node->capa_initialized))
1453 if (idx < *idx_skip)
1456 if (batadv_mcast_flags_dump_entry(msg, portid, seq,
1473 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1474 * @msg: buffer for the message
1475 * @portid: netlink port
1476 * @seq: Sequence number of netlink message
1477 * @bat_priv: the bat priv with all the soft interface information
1478 * @bucket: current bucket to dump
1479 * @idx: index in current bucket to the next entry to dump
1481 * Return: 0 or error code.
1484 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid, u32 seq,
1485 struct batadv_priv *bat_priv, long *bucket, long *idx)
1487 struct batadv_hashtable *hash = bat_priv->orig_hash;
1488 long bucket_tmp = *bucket;
1489 struct hlist_head *head;
1490 long idx_tmp = *idx;
1492 while (bucket_tmp < hash->size) {
1493 head = &hash->table[bucket_tmp];
1495 if (batadv_mcast_flags_dump_bucket(msg, portid, seq, head,
1503 *bucket = bucket_tmp;
1510 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
1512 * @cb: netlink callback structure
1513 * @primary_if: the primary interface pointer to return the result in
1515 * Return: 0 or error code.
1518 batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
1519 struct batadv_hard_iface **primary_if)
1521 struct batadv_hard_iface *hard_iface = NULL;
1522 struct net *net = sock_net(cb->skb->sk);
1523 struct net_device *soft_iface;
1524 struct batadv_priv *bat_priv;
1528 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1532 soft_iface = dev_get_by_index(net, ifindex);
1533 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1538 bat_priv = netdev_priv(soft_iface);
1540 hard_iface = batadv_primary_if_get_selected(bat_priv);
1541 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
1548 dev_put(soft_iface);
1550 if (!ret && primary_if)
1551 *primary_if = hard_iface;
1552 else if (hard_iface)
1553 batadv_hardif_put(hard_iface);
1559 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1560 * @msg: buffer for the message
1561 * @cb: callback structure containing arguments
1563 * Return: message length.
1565 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
1567 struct batadv_hard_iface *primary_if = NULL;
1568 int portid = NETLINK_CB(cb->skb).portid;
1569 struct batadv_priv *bat_priv;
1570 long *bucket = &cb->args[0];
1571 long *idx = &cb->args[1];
1574 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
1578 bat_priv = netdev_priv(primary_if->soft_iface);
1579 ret = __batadv_mcast_flags_dump(msg, portid, cb->nlh->nlmsg_seq,
1580 bat_priv, bucket, idx);
1582 batadv_hardif_put(primary_if);
1587 * batadv_mcast_free() - free the multicast optimizations structures
1588 * @bat_priv: the bat priv with all the soft interface information
1590 void batadv_mcast_free(struct batadv_priv *bat_priv)
1592 cancel_delayed_work_sync(&bat_priv->mcast.work);
1594 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1595 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1597 /* safely calling outside of worker, as worker was canceled above */
1598 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1602 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
1603 * @orig: the originator which is going to get purged
1605 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1607 struct batadv_priv *bat_priv = orig->bat_priv;
1609 spin_lock_bh(&orig->mcast_handler_lock);
1611 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1612 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1613 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1615 spin_unlock_bh(&orig->mcast_handler_lock);