1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2014-2019 B.A.T.M.A.N. contributors:
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/errno.h>
15 #include <linux/etherdevice.h>
16 #include <linux/gfp.h>
17 #include <linux/icmpv6.h>
18 #include <linux/if_bridge.h>
19 #include <linux/if_ether.h>
20 #include <linux/igmp.h>
22 #include <linux/in6.h>
24 #include <linux/ipv6.h>
25 #include <linux/jiffies.h>
26 #include <linux/kernel.h>
27 #include <linux/kref.h>
28 #include <linux/list.h>
29 #include <linux/lockdep.h>
30 #include <linux/netdevice.h>
31 #include <linux/netlink.h>
32 #include <linux/printk.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/seq_file.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 "hard-interface.h"
57 #include "soft-interface.h"
58 #include "translation-table.h"
61 static void batadv_mcast_mla_update(struct work_struct *work);
64 * batadv_mcast_start_timer() - schedule the multicast periodic worker
65 * @bat_priv: the bat priv with all the soft interface information
67 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
69 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
70 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
74 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
75 * @soft_iface: netdev struct of the mesh interface
77 * If the given soft interface has a bridge on top then the refcount
78 * of the according net device is increased.
80 * Return: NULL if no such bridge exists. Otherwise the net device of the
83 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
85 struct net_device *upper = soft_iface;
89 upper = netdev_master_upper_dev_get_rcu(upper);
90 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
100 * batadv_mcast_addr_is_ipv4() - check if multicast MAC is IPv4
101 * @addr: the MAC address to check
103 * Return: True, if MAC address is one reserved for IPv4 multicast, false
106 static bool batadv_mcast_addr_is_ipv4(const u8 *addr)
108 static const u8 prefix[] = {0x01, 0x00, 0x5E};
110 return memcmp(prefix, addr, sizeof(prefix)) == 0;
114 * batadv_mcast_addr_is_ipv6() - check if multicast MAC is IPv6
115 * @addr: the MAC address to check
117 * Return: True, if MAC address is one reserved for IPv6 multicast, false
120 static bool batadv_mcast_addr_is_ipv6(const u8 *addr)
122 static const u8 prefix[] = {0x33, 0x33};
124 return memcmp(prefix, addr, sizeof(prefix)) == 0;
128 * batadv_mcast_mla_softif_get() - get softif multicast listeners
129 * @bat_priv: the bat priv with all the soft interface information
130 * @dev: the device to collect multicast addresses from
131 * @mcast_list: a list to put found addresses into
133 * Collects multicast addresses of multicast listeners residing
134 * on this kernel on the given soft interface, dev, in
135 * the given mcast_list. In general, multicast listeners provided by
136 * your multicast receiving applications run directly on this node.
138 * If there is a bridge interface on top of dev, collects from that one
139 * instead. Just like with IP addresses and routes, multicast listeners
140 * will(/should) register to the bridge interface instead of an
143 * Return: -ENOMEM on memory allocation error or the number of
144 * items added to the mcast_list otherwise.
146 static int batadv_mcast_mla_softif_get(struct batadv_priv *bat_priv,
147 struct net_device *dev,
148 struct hlist_head *mcast_list)
150 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
151 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
152 struct net_device *bridge = batadv_mcast_get_bridge(dev);
153 struct netdev_hw_addr *mc_list_entry;
154 struct batadv_hw_addr *new;
157 netif_addr_lock_bh(bridge ? bridge : dev);
158 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
159 if (all_ipv4 && batadv_mcast_addr_is_ipv4(mc_list_entry->addr))
162 if (all_ipv6 && batadv_mcast_addr_is_ipv6(mc_list_entry->addr))
165 new = kmalloc(sizeof(*new), GFP_ATOMIC);
171 ether_addr_copy(new->addr, mc_list_entry->addr);
172 hlist_add_head(&new->list, mcast_list);
175 netif_addr_unlock_bh(bridge ? bridge : dev);
184 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
185 * @mcast_addr: the multicast address to check
186 * @mcast_list: the list with multicast addresses to search in
188 * Return: true if the given address is already in the given list.
189 * Otherwise returns false.
191 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
192 struct hlist_head *mcast_list)
194 struct batadv_hw_addr *mcast_entry;
196 hlist_for_each_entry(mcast_entry, mcast_list, list)
197 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
204 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
205 * @dst: destination to write to - a multicast MAC address
206 * @src: source to read from - a multicast IP address
208 * Converts a given multicast IPv4/IPv6 address from a bridge
209 * to its matching multicast MAC address and copies it into the given
210 * destination buffer.
212 * Caller needs to make sure the destination buffer can hold
213 * at least ETH_ALEN bytes.
215 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
217 if (src->proto == htons(ETH_P_IP))
218 ip_eth_mc_map(src->u.ip4, dst);
219 #if IS_ENABLED(CONFIG_IPV6)
220 else if (src->proto == htons(ETH_P_IPV6))
221 ipv6_eth_mc_map(&src->u.ip6, dst);
228 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
229 * @bat_priv: the bat priv with all the soft interface information
230 * @dev: a bridge slave whose bridge to collect multicast addresses from
231 * @mcast_list: a list to put found addresses into
233 * Collects multicast addresses of multicast listeners residing
234 * on foreign, non-mesh devices which we gave access to our mesh via
235 * a bridge on top of the given soft interface, dev, in the given
238 * Return: -ENOMEM on memory allocation error or the number of
239 * items added to the mcast_list otherwise.
241 static int batadv_mcast_mla_bridge_get(struct batadv_priv *bat_priv,
242 struct net_device *dev,
243 struct hlist_head *mcast_list)
245 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
246 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
247 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
248 struct br_ip_list *br_ip_entry, *tmp;
249 struct batadv_hw_addr *new;
250 u8 mcast_addr[ETH_ALEN];
253 /* we don't need to detect these devices/listeners, the IGMP/MLD
254 * snooping code of the Linux bridge already does that for us
256 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
260 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
261 if (all_ipv4 && br_ip_entry->addr.proto == htons(ETH_P_IP))
264 if (all_ipv6 && br_ip_entry->addr.proto == htons(ETH_P_IPV6))
267 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
268 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
271 new = kmalloc(sizeof(*new), GFP_ATOMIC);
277 ether_addr_copy(new->addr, mcast_addr);
278 hlist_add_head(&new->list, mcast_list);
282 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
283 list_del(&br_ip_entry->list);
291 * batadv_mcast_mla_list_free() - free a list of multicast addresses
292 * @mcast_list: the list to free
294 * Removes and frees all items in the given mcast_list.
296 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
298 struct batadv_hw_addr *mcast_entry;
299 struct hlist_node *tmp;
301 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
302 hlist_del(&mcast_entry->list);
308 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
309 * @bat_priv: the bat priv with all the soft interface information
310 * @mcast_list: a list of addresses which should _not_ be removed
312 * Retracts the announcement of any multicast listener from the
313 * translation table except the ones listed in the given mcast_list.
315 * If mcast_list is NULL then all are retracted.
317 * Do not call outside of the mcast worker! (or cancel mcast worker first)
319 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
320 struct hlist_head *mcast_list)
322 struct batadv_hw_addr *mcast_entry;
323 struct hlist_node *tmp;
325 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
327 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
330 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
334 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
336 "mcast TT outdated", false);
338 hlist_del(&mcast_entry->list);
344 * batadv_mcast_mla_tt_add() - add multicast listener announcements
345 * @bat_priv: the bat priv with all the soft interface information
346 * @mcast_list: a list of addresses which are going to get added
348 * Adds multicast listener announcements from the given mcast_list to the
349 * translation table if they have not been added yet.
351 * Do not call outside of the mcast worker! (or cancel mcast worker first)
353 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
354 struct hlist_head *mcast_list)
356 struct batadv_hw_addr *mcast_entry;
357 struct hlist_node *tmp;
359 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
364 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
365 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
366 &bat_priv->mcast.mla_list))
369 if (!batadv_tt_local_add(bat_priv->soft_iface,
370 mcast_entry->addr, BATADV_NO_FLAGS,
371 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
374 hlist_del(&mcast_entry->list);
375 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
380 * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
381 * @bat_priv: the bat priv with all the soft interface information
383 * Checks whether there is a bridge on top of our soft interface.
385 * Return: true if there is a bridge, false otherwise.
387 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
389 struct net_device *upper = bat_priv->soft_iface;
393 upper = netdev_master_upper_dev_get_rcu(upper);
394 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
401 * batadv_mcast_querier_log() - debug output regarding the querier status on
403 * @bat_priv: the bat priv with all the soft interface information
404 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
405 * @old_state: the previous querier state on our link
406 * @new_state: the new querier state on our link
408 * Outputs debug messages to the logging facility with log level 'mcast'
409 * regarding changes to the querier status on the link which are relevant
410 * to our multicast optimizations.
412 * Usually this is about whether a querier appeared or vanished in
413 * our mesh or whether the querier is in the suboptimal position of being
414 * behind our local bridge segment: Snooping switches will directly
415 * forward listener reports to the querier, therefore batman-adv and
416 * the bridge will potentially not see these listeners - the querier is
417 * potentially shadowing listeners from us then.
419 * This is only interesting for nodes with a bridge on top of their
423 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
424 struct batadv_mcast_querier_state *old_state,
425 struct batadv_mcast_querier_state *new_state)
427 if (!old_state->exists && new_state->exists)
428 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
430 else if (old_state->exists && !new_state->exists)
431 batadv_info(bat_priv->soft_iface,
432 "%s Querier disappeared - multicast optimizations disabled\n",
434 else if (!bat_priv->mcast.bridged && !new_state->exists)
435 batadv_info(bat_priv->soft_iface,
436 "No %s Querier present - multicast optimizations disabled\n",
439 if (new_state->exists) {
440 if ((!old_state->shadowing && new_state->shadowing) ||
441 (!old_state->exists && new_state->shadowing))
442 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
443 "%s Querier is behind our bridged segment: Might shadow listeners\n",
445 else if (old_state->shadowing && !new_state->shadowing)
446 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
447 "%s Querier is not behind our bridged segment\n",
453 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
455 * @bat_priv: the bat priv with all the soft interface information
456 * @bridged: a flag about whether the soft interface is currently bridged or not
457 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
458 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
460 * If no bridges are ever used on this node, then this function does nothing.
462 * Otherwise this function outputs debug information to the 'mcast' log level
463 * which might be relevant to our multicast optimizations.
465 * More precisely, it outputs information when a bridge interface is added or
466 * removed from a soft interface. And when a bridge is present, it further
467 * outputs information about the querier state which is relevant for the
468 * multicast flags this node is going to set.
471 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
472 struct batadv_mcast_querier_state *querier_ipv4,
473 struct batadv_mcast_querier_state *querier_ipv6)
475 if (!bat_priv->mcast.bridged && bridged)
476 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
477 "Bridge added: Setting Unsnoopables(U)-flag\n");
478 else if (bat_priv->mcast.bridged && !bridged)
479 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
480 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
483 batadv_mcast_querier_log(bat_priv, "IGMP",
484 &bat_priv->mcast.querier_ipv4,
486 batadv_mcast_querier_log(bat_priv, "MLD",
487 &bat_priv->mcast.querier_ipv6,
493 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
494 * @bat_priv: the bat priv with all the soft interface information
495 * @flags: flags indicating the new multicast state
497 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
498 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
500 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
502 u8 old_flags = bat_priv->mcast.flags;
503 char str_old_flags[] = "[...]";
505 sprintf(str_old_flags, "[%c%c%c]",
506 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
507 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
508 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
510 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
511 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
512 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
513 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
514 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
515 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
519 * batadv_mcast_mla_tvlv_update() - update multicast tvlv
520 * @bat_priv: the bat priv with all the soft interface information
522 * Updates the own multicast tvlv with our current multicast related settings,
523 * capabilities and inabilities.
525 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
528 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
530 struct batadv_tvlv_mcast_data mcast_data;
531 struct batadv_mcast_querier_state querier4 = {false, false};
532 struct batadv_mcast_querier_state querier6 = {false, false};
533 struct net_device *dev = bat_priv->soft_iface;
536 mcast_data.flags = BATADV_NO_FLAGS;
537 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
539 bridged = batadv_mcast_has_bridge(bat_priv);
543 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
544 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
546 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
547 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
549 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
550 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
552 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
554 /* 1) If no querier exists at all, then multicast listeners on
555 * our local TT clients behind the bridge will keep silent.
556 * 2) If the selected querier is on one of our local TT clients,
557 * behind the bridge, then this querier might shadow multicast
558 * listeners on our local TT clients, behind this bridge.
560 * In both cases, we will signalize other batman nodes that
561 * we need all multicast traffic of the according protocol.
563 if (!querier4.exists || querier4.shadowing)
564 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
566 if (!querier6.exists || querier6.shadowing)
567 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
570 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
572 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
573 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
575 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
576 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
578 bat_priv->mcast.bridged = bridged;
580 if (!bat_priv->mcast.enabled ||
581 mcast_data.flags != bat_priv->mcast.flags) {
582 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
583 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
584 &mcast_data, sizeof(mcast_data));
585 bat_priv->mcast.flags = mcast_data.flags;
586 bat_priv->mcast.enabled = true;
589 return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
590 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
594 * __batadv_mcast_mla_update() - update the own MLAs
595 * @bat_priv: the bat priv with all the soft interface information
597 * Updates the own multicast listener announcements in the translation
598 * table as well as the own, announced multicast tvlv container.
600 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
601 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
602 * ensured by the non-parallel execution of the worker this function
605 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
607 struct net_device *soft_iface = bat_priv->soft_iface;
608 struct hlist_head mcast_list = HLIST_HEAD_INIT;
611 if (!batadv_mcast_mla_tvlv_update(bat_priv))
614 ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
618 ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
623 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
624 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
627 batadv_mcast_mla_list_free(&mcast_list);
631 * batadv_mcast_mla_update() - update the own MLAs
632 * @work: kernel work struct
634 * Updates the own multicast listener announcements in the translation
635 * table as well as the own, announced multicast tvlv container.
637 * In the end, reschedules the work timer.
639 static void batadv_mcast_mla_update(struct work_struct *work)
641 struct delayed_work *delayed_work;
642 struct batadv_priv_mcast *priv_mcast;
643 struct batadv_priv *bat_priv;
645 delayed_work = to_delayed_work(work);
646 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
647 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
649 __batadv_mcast_mla_update(bat_priv);
650 batadv_mcast_start_timer(bat_priv);
654 * batadv_mcast_is_report_ipv4() - check for IGMP reports
655 * @skb: the ethernet frame destined for the mesh
657 * This call might reallocate skb data.
659 * Checks whether the given frame is a valid IGMP report.
661 * Return: If so then true, otherwise false.
663 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
665 if (ip_mc_check_igmp(skb) < 0)
668 switch (igmp_hdr(skb)->type) {
669 case IGMP_HOST_MEMBERSHIP_REPORT:
670 case IGMPV2_HOST_MEMBERSHIP_REPORT:
671 case IGMPV3_HOST_MEMBERSHIP_REPORT:
679 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
681 * @bat_priv: the bat priv with all the soft interface information
682 * @skb: the IPv4 packet to check
683 * @is_unsnoopable: stores whether the destination is snoopable
685 * Checks whether the given IPv4 packet has the potential to be forwarded with a
686 * mode more optimal than classic flooding.
688 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
689 * allocation failure.
691 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
693 bool *is_unsnoopable)
697 /* We might fail due to out-of-memory -> drop it */
698 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
701 if (batadv_mcast_is_report_ipv4(skb))
706 /* TODO: Implement Multicast Router Discovery (RFC4286),
707 * then allow scope > link local, too
709 if (!ipv4_is_local_multicast(iphdr->daddr))
712 /* link-local multicast listeners behind a bridge are
713 * not snoopable (see RFC4541, section 2.1.2.2)
715 *is_unsnoopable = true;
721 * batadv_mcast_is_report_ipv6() - check for MLD reports
722 * @skb: the ethernet frame destined for the mesh
724 * This call might reallocate skb data.
726 * Checks whether the given frame is a valid MLD report.
728 * Return: If so then true, otherwise false.
730 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
732 if (ipv6_mc_check_mld(skb) < 0)
735 switch (icmp6_hdr(skb)->icmp6_type) {
736 case ICMPV6_MGM_REPORT:
737 case ICMPV6_MLD2_REPORT:
745 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
747 * @bat_priv: the bat priv with all the soft interface information
748 * @skb: the IPv6 packet to check
749 * @is_unsnoopable: stores whether the destination is snoopable
751 * Checks whether the given IPv6 packet has the potential to be forwarded with a
752 * mode more optimal than classic flooding.
754 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
756 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
758 bool *is_unsnoopable)
760 struct ipv6hdr *ip6hdr;
762 /* We might fail due to out-of-memory -> drop it */
763 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
766 if (batadv_mcast_is_report_ipv6(skb))
769 ip6hdr = ipv6_hdr(skb);
771 /* TODO: Implement Multicast Router Discovery (RFC4286),
772 * then allow scope > link local, too
774 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
777 /* link-local-all-nodes multicast listeners behind a bridge are
778 * not snoopable (see RFC4541, section 3, paragraph 3)
780 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
781 *is_unsnoopable = true;
787 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
788 * @bat_priv: the bat priv with all the soft interface information
789 * @skb: the multicast frame to check
790 * @is_unsnoopable: stores whether the destination is snoopable
792 * Checks whether the given multicast ethernet frame has the potential to be
793 * forwarded with a mode more optimal than classic flooding.
795 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
797 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
799 bool *is_unsnoopable)
801 struct ethhdr *ethhdr = eth_hdr(skb);
803 if (!atomic_read(&bat_priv->multicast_mode))
806 switch (ntohs(ethhdr->h_proto)) {
808 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
811 if (!IS_ENABLED(CONFIG_IPV6))
814 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
822 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
824 * @bat_priv: the bat priv with all the soft interface information
825 * @ethhdr: ethernet header of a packet
827 * Return: the number of nodes which want all IPv4 multicast traffic if the
828 * given ethhdr is from an IPv4 packet or the number of nodes which want all
829 * IPv6 traffic if it matches an IPv6 packet.
831 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
832 struct ethhdr *ethhdr)
834 switch (ntohs(ethhdr->h_proto)) {
836 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
838 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
840 /* we shouldn't be here... */
846 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
847 * @bat_priv: the bat priv with all the soft interface information
848 * @ethhdr: the ether header containing the multicast destination
850 * Return: an orig_node matching the multicast address provided by ethhdr
851 * via a translation table lookup. This increases the returned nodes refcount.
853 static struct batadv_orig_node *
854 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
855 struct ethhdr *ethhdr)
857 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
862 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
863 * @bat_priv: the bat priv with all the soft interface information
865 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
866 * increases its refcount.
868 static struct batadv_orig_node *
869 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
871 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
874 hlist_for_each_entry_rcu(tmp_orig_node,
875 &bat_priv->mcast.want_all_ipv4_list,
876 mcast_want_all_ipv4_node) {
877 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
880 orig_node = tmp_orig_node;
889 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
890 * @bat_priv: the bat priv with all the soft interface information
892 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
893 * and increases its refcount.
895 static struct batadv_orig_node *
896 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
898 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
901 hlist_for_each_entry_rcu(tmp_orig_node,
902 &bat_priv->mcast.want_all_ipv6_list,
903 mcast_want_all_ipv6_node) {
904 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
907 orig_node = tmp_orig_node;
916 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
917 * @bat_priv: the bat priv with all the soft interface information
918 * @ethhdr: an ethernet header to determine the protocol family from
920 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
921 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
922 * increases its refcount.
924 static struct batadv_orig_node *
925 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
926 struct ethhdr *ethhdr)
928 switch (ntohs(ethhdr->h_proto)) {
930 return batadv_mcast_forw_ipv4_node_get(bat_priv);
932 return batadv_mcast_forw_ipv6_node_get(bat_priv);
934 /* we shouldn't be here... */
940 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
941 * @bat_priv: the bat priv with all the soft interface information
943 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
944 * set and increases its refcount.
946 static struct batadv_orig_node *
947 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
949 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
952 hlist_for_each_entry_rcu(tmp_orig_node,
953 &bat_priv->mcast.want_all_unsnoopables_list,
954 mcast_want_all_unsnoopables_node) {
955 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
958 orig_node = tmp_orig_node;
967 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
968 * @bat_priv: the bat priv with all the soft interface information
969 * @skb: The multicast packet to check
970 * @orig: an originator to be set to forward the skb to
972 * Return: the forwarding mode as enum batadv_forw_mode and in case of
973 * BATADV_FORW_SINGLE set the orig to the single originator the skb
974 * should be forwarded to.
976 enum batadv_forw_mode
977 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
978 struct batadv_orig_node **orig)
980 int ret, tt_count, ip_count, unsnoop_count, total_count;
981 bool is_unsnoopable = false;
982 struct ethhdr *ethhdr;
984 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
986 return BATADV_FORW_NONE;
988 return BATADV_FORW_ALL;
990 ethhdr = eth_hdr(skb);
992 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
994 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
995 unsnoop_count = !is_unsnoopable ? 0 :
996 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
998 total_count = tt_count + ip_count + unsnoop_count;
1000 switch (total_count) {
1003 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1005 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1006 else if (unsnoop_count)
1007 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1010 return BATADV_FORW_SINGLE;
1014 return BATADV_FORW_NONE;
1016 return BATADV_FORW_ALL;
1021 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1022 * @bat_priv: the bat priv with all the soft interface information
1023 * @orig: the orig_node which multicast state might have changed of
1024 * @mcast_flags: flags indicating the new multicast state
1026 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1027 * orig, has toggled then this method updates counter and list accordingly.
1029 * Caller needs to hold orig->mcast_handler_lock.
1031 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1032 struct batadv_orig_node *orig,
1035 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1036 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1038 lockdep_assert_held(&orig->mcast_handler_lock);
1040 /* switched from flag unset to set */
1041 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1042 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1043 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1045 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1046 /* flag checks above + mcast_handler_lock prevents this */
1047 WARN_ON(!hlist_unhashed(node));
1049 hlist_add_head_rcu(node, head);
1050 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1051 /* switched from flag set to unset */
1052 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1053 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1054 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1056 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1057 /* flag checks above + mcast_handler_lock prevents this */
1058 WARN_ON(hlist_unhashed(node));
1060 hlist_del_init_rcu(node);
1061 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1066 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1067 * @bat_priv: the bat priv with all the soft interface information
1068 * @orig: the orig_node which multicast state might have changed of
1069 * @mcast_flags: flags indicating the new multicast state
1071 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1072 * toggled then this method updates counter and list accordingly.
1074 * Caller needs to hold orig->mcast_handler_lock.
1076 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1077 struct batadv_orig_node *orig,
1080 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1081 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1083 lockdep_assert_held(&orig->mcast_handler_lock);
1085 /* switched from flag unset to set */
1086 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1087 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1088 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1090 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1091 /* flag checks above + mcast_handler_lock prevents this */
1092 WARN_ON(!hlist_unhashed(node));
1094 hlist_add_head_rcu(node, head);
1095 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1096 /* switched from flag set to unset */
1097 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1098 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1099 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1101 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1102 /* flag checks above + mcast_handler_lock prevents this */
1103 WARN_ON(hlist_unhashed(node));
1105 hlist_del_init_rcu(node);
1106 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1111 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1112 * @bat_priv: the bat priv with all the soft interface information
1113 * @orig: the orig_node which multicast state might have changed of
1114 * @mcast_flags: flags indicating the new multicast state
1116 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1117 * toggled then this method updates counter and list accordingly.
1119 * Caller needs to hold orig->mcast_handler_lock.
1121 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1122 struct batadv_orig_node *orig,
1125 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1126 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1128 lockdep_assert_held(&orig->mcast_handler_lock);
1130 /* switched from flag unset to set */
1131 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1132 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1133 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1135 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1136 /* flag checks above + mcast_handler_lock prevents this */
1137 WARN_ON(!hlist_unhashed(node));
1139 hlist_add_head_rcu(node, head);
1140 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1141 /* switched from flag set to unset */
1142 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1143 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1144 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1146 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1147 /* flag checks above + mcast_handler_lock prevents this */
1148 WARN_ON(hlist_unhashed(node));
1150 hlist_del_init_rcu(node);
1151 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1156 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1157 * @bat_priv: the bat priv with all the soft interface information
1158 * @orig: the orig_node of the ogm
1159 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1160 * @tvlv_value: tvlv buffer containing the multicast data
1161 * @tvlv_value_len: tvlv buffer length
1163 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1164 struct batadv_orig_node *orig,
1169 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1170 u8 mcast_flags = BATADV_NO_FLAGS;
1172 if (orig_mcast_enabled && tvlv_value &&
1173 tvlv_value_len >= sizeof(mcast_flags))
1174 mcast_flags = *(u8 *)tvlv_value;
1176 if (!orig_mcast_enabled) {
1177 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1178 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1181 spin_lock_bh(&orig->mcast_handler_lock);
1183 if (orig_mcast_enabled &&
1184 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1185 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1186 } else if (!orig_mcast_enabled &&
1187 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1188 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1191 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1193 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1194 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1195 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1197 orig->mcast_flags = mcast_flags;
1198 spin_unlock_bh(&orig->mcast_handler_lock);
1202 * batadv_mcast_init() - initialize the multicast optimizations structures
1203 * @bat_priv: the bat priv with all the soft interface information
1205 void batadv_mcast_init(struct batadv_priv *bat_priv)
1207 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1208 NULL, BATADV_TVLV_MCAST, 2,
1209 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1211 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1212 batadv_mcast_start_timer(bat_priv);
1215 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1217 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
1218 * @bat_priv: the bat priv with all the soft interface information
1219 * @seq: debugfs table seq_file struct
1221 * Prints our own multicast flags including a more specific reason why
1222 * they are set, that is prints the bridge and querier state too, to
1223 * the debugfs table specified via @seq.
1225 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1226 struct seq_file *seq)
1228 u8 flags = bat_priv->mcast.flags;
1229 char querier4, querier6, shadowing4, shadowing6;
1230 bool bridged = bat_priv->mcast.bridged;
1233 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1234 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1235 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1236 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1244 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1245 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1246 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1247 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1248 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1249 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1250 querier4, querier6);
1251 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1252 shadowing4, shadowing6);
1253 seq_puts(seq, "-------------------------------------------\n");
1254 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1258 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
1259 * @seq: seq file to print on
1262 * This prints a table of (primary) originators and their according
1263 * multicast flags, including (in the header) our own.
1267 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1269 struct net_device *net_dev = (struct net_device *)seq->private;
1270 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1271 struct batadv_hard_iface *primary_if;
1272 struct batadv_hashtable *hash = bat_priv->orig_hash;
1273 struct batadv_orig_node *orig_node;
1274 struct hlist_head *head;
1278 primary_if = batadv_seq_print_text_primary_if_get(seq);
1282 batadv_mcast_flags_print_header(bat_priv, seq);
1284 for (i = 0; i < hash->size; i++) {
1285 head = &hash->table[i];
1288 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1289 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1290 &orig_node->capa_initialized))
1293 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1294 &orig_node->capabilities)) {
1295 seq_printf(seq, "%pM -\n", orig_node->orig);
1299 flags = orig_node->mcast_flags;
1301 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1302 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1304 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1306 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1312 batadv_hardif_put(primary_if);
1319 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
1320 * @msg: buffer for the message
1321 * @bat_priv: the bat priv with all the soft interface information
1323 * Return: 0 or error code.
1325 int batadv_mcast_mesh_info_put(struct sk_buff *msg,
1326 struct batadv_priv *bat_priv)
1328 u32 flags = bat_priv->mcast.flags;
1329 u32 flags_priv = BATADV_NO_FLAGS;
1331 if (bat_priv->mcast.bridged) {
1332 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
1334 if (bat_priv->mcast.querier_ipv4.exists)
1335 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
1336 if (bat_priv->mcast.querier_ipv6.exists)
1337 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
1338 if (bat_priv->mcast.querier_ipv4.shadowing)
1339 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
1340 if (bat_priv->mcast.querier_ipv6.shadowing)
1341 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
1344 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
1345 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
1352 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
1353 * to a netlink socket
1354 * @msg: buffer for the message
1355 * @portid: netlink port
1356 * @cb: Control block containing additional options
1357 * @orig_node: originator to dump the multicast flags of
1359 * Return: 0 or error code.
1362 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
1363 struct netlink_callback *cb,
1364 struct batadv_orig_node *orig_node)
1368 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1369 &batadv_netlink_family, NLM_F_MULTI,
1370 BATADV_CMD_GET_MCAST_FLAGS);
1374 genl_dump_check_consistent(cb, hdr);
1376 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1378 genlmsg_cancel(msg, hdr);
1382 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1383 &orig_node->capabilities)) {
1384 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
1385 orig_node->mcast_flags)) {
1386 genlmsg_cancel(msg, hdr);
1391 genlmsg_end(msg, hdr);
1396 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
1397 * table to a netlink socket
1398 * @msg: buffer for the message
1399 * @portid: netlink port
1400 * @cb: Control block containing additional options
1401 * @hash: hash to dump
1402 * @bucket: bucket index to dump
1403 * @idx_skip: How many entries to skip
1405 * Return: 0 or error code.
1408 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
1409 struct netlink_callback *cb,
1410 struct batadv_hashtable *hash,
1411 unsigned int bucket, long *idx_skip)
1413 struct batadv_orig_node *orig_node;
1416 spin_lock_bh(&hash->list_locks[bucket]);
1417 cb->seq = atomic_read(&hash->generation) << 1 | 1;
1419 hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
1420 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1421 &orig_node->capa_initialized))
1424 if (idx < *idx_skip)
1427 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
1428 spin_unlock_bh(&hash->list_locks[bucket]);
1437 spin_unlock_bh(&hash->list_locks[bucket]);
1443 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1444 * @msg: buffer for the message
1445 * @portid: netlink port
1446 * @cb: Control block containing additional options
1447 * @bat_priv: the bat priv with all the soft interface information
1448 * @bucket: current bucket to dump
1449 * @idx: index in current bucket to the next entry to dump
1451 * Return: 0 or error code.
1454 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
1455 struct netlink_callback *cb,
1456 struct batadv_priv *bat_priv, long *bucket, long *idx)
1458 struct batadv_hashtable *hash = bat_priv->orig_hash;
1459 long bucket_tmp = *bucket;
1460 long idx_tmp = *idx;
1462 while (bucket_tmp < hash->size) {
1463 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
1471 *bucket = bucket_tmp;
1478 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
1480 * @cb: netlink callback structure
1481 * @primary_if: the primary interface pointer to return the result in
1483 * Return: 0 or error code.
1486 batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
1487 struct batadv_hard_iface **primary_if)
1489 struct batadv_hard_iface *hard_iface = NULL;
1490 struct net *net = sock_net(cb->skb->sk);
1491 struct net_device *soft_iface;
1492 struct batadv_priv *bat_priv;
1496 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1500 soft_iface = dev_get_by_index(net, ifindex);
1501 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1506 bat_priv = netdev_priv(soft_iface);
1508 hard_iface = batadv_primary_if_get_selected(bat_priv);
1509 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
1516 dev_put(soft_iface);
1518 if (!ret && primary_if)
1519 *primary_if = hard_iface;
1520 else if (hard_iface)
1521 batadv_hardif_put(hard_iface);
1527 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1528 * @msg: buffer for the message
1529 * @cb: callback structure containing arguments
1531 * Return: message length.
1533 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
1535 struct batadv_hard_iface *primary_if = NULL;
1536 int portid = NETLINK_CB(cb->skb).portid;
1537 struct batadv_priv *bat_priv;
1538 long *bucket = &cb->args[0];
1539 long *idx = &cb->args[1];
1542 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
1546 bat_priv = netdev_priv(primary_if->soft_iface);
1547 ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
1549 batadv_hardif_put(primary_if);
1554 * batadv_mcast_free() - free the multicast optimizations structures
1555 * @bat_priv: the bat priv with all the soft interface information
1557 void batadv_mcast_free(struct batadv_priv *bat_priv)
1559 cancel_delayed_work_sync(&bat_priv->mcast.work);
1561 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1562 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1564 /* safely calling outside of worker, as worker was canceled above */
1565 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1569 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
1570 * @orig: the originator which is going to get purged
1572 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1574 struct batadv_priv *bat_priv = orig->bat_priv;
1576 spin_lock_bh(&orig->mcast_handler_lock);
1578 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1579 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1580 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1582 spin_unlock_bh(&orig->mcast_handler_lock);