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 "hard-interface.h"
69 #include "soft-interface.h"
70 #include "translation-table.h"
73 static void batadv_mcast_mla_update(struct work_struct *work);
76 * batadv_mcast_start_timer() - schedule the multicast periodic worker
77 * @bat_priv: the bat priv with all the soft interface information
79 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
81 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
82 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
86 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
87 * @soft_iface: netdev struct of the mesh interface
89 * If the given soft interface has a bridge on top then the refcount
90 * of the according net device is increased.
92 * Return: NULL if no such bridge exists. Otherwise the net device of the
95 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
97 struct net_device *upper = soft_iface;
101 upper = netdev_master_upper_dev_get_rcu(upper);
102 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
112 * batadv_mcast_addr_is_ipv4() - check if multicast MAC is IPv4
113 * @addr: the MAC address to check
115 * Return: True, if MAC address is one reserved for IPv4 multicast, false
118 static bool batadv_mcast_addr_is_ipv4(const u8 *addr)
120 static const u8 prefix[] = {0x01, 0x00, 0x5E};
122 return memcmp(prefix, addr, sizeof(prefix)) == 0;
126 * batadv_mcast_addr_is_ipv6() - check if multicast MAC is IPv6
127 * @addr: the MAC address to check
129 * Return: True, if MAC address is one reserved for IPv6 multicast, false
132 static bool batadv_mcast_addr_is_ipv6(const u8 *addr)
134 static const u8 prefix[] = {0x33, 0x33};
136 return memcmp(prefix, addr, sizeof(prefix)) == 0;
140 * batadv_mcast_mla_softif_get() - get softif multicast listeners
141 * @bat_priv: the bat priv with all the soft interface information
142 * @dev: the device to collect multicast addresses from
143 * @mcast_list: a list to put found addresses into
145 * Collects multicast addresses of multicast listeners residing
146 * on this kernel on the given soft interface, dev, in
147 * the given mcast_list. In general, multicast listeners provided by
148 * your multicast receiving applications run directly on this node.
150 * If there is a bridge interface on top of dev, collects from that one
151 * instead. Just like with IP addresses and routes, multicast listeners
152 * will(/should) register to the bridge interface instead of an
155 * Return: -ENOMEM on memory allocation error or the number of
156 * items added to the mcast_list otherwise.
158 static int batadv_mcast_mla_softif_get(struct batadv_priv *bat_priv,
159 struct net_device *dev,
160 struct hlist_head *mcast_list)
162 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
163 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
164 struct net_device *bridge = batadv_mcast_get_bridge(dev);
165 struct netdev_hw_addr *mc_list_entry;
166 struct batadv_hw_addr *new;
169 netif_addr_lock_bh(bridge ? bridge : dev);
170 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
171 if (all_ipv4 && batadv_mcast_addr_is_ipv4(mc_list_entry->addr))
174 if (all_ipv6 && batadv_mcast_addr_is_ipv6(mc_list_entry->addr))
177 new = kmalloc(sizeof(*new), GFP_ATOMIC);
183 ether_addr_copy(new->addr, mc_list_entry->addr);
184 hlist_add_head(&new->list, mcast_list);
187 netif_addr_unlock_bh(bridge ? bridge : dev);
196 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
197 * @mcast_addr: the multicast address to check
198 * @mcast_list: the list with multicast addresses to search in
200 * Return: true if the given address is already in the given list.
201 * Otherwise returns false.
203 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
204 struct hlist_head *mcast_list)
206 struct batadv_hw_addr *mcast_entry;
208 hlist_for_each_entry(mcast_entry, mcast_list, list)
209 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
216 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
217 * @dst: destination to write to - a multicast MAC address
218 * @src: source to read from - a multicast IP address
220 * Converts a given multicast IPv4/IPv6 address from a bridge
221 * to its matching multicast MAC address and copies it into the given
222 * destination buffer.
224 * Caller needs to make sure the destination buffer can hold
225 * at least ETH_ALEN bytes.
227 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
229 if (src->proto == htons(ETH_P_IP))
230 ip_eth_mc_map(src->u.ip4, dst);
231 #if IS_ENABLED(CONFIG_IPV6)
232 else if (src->proto == htons(ETH_P_IPV6))
233 ipv6_eth_mc_map(&src->u.ip6, dst);
240 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
241 * @bat_priv: the bat priv with all the soft interface information
242 * @dev: a bridge slave whose bridge to collect multicast addresses from
243 * @mcast_list: a list to put found addresses into
245 * Collects multicast addresses of multicast listeners residing
246 * on foreign, non-mesh devices which we gave access to our mesh via
247 * a bridge on top of the given soft interface, dev, in the given
250 * Return: -ENOMEM on memory allocation error or the number of
251 * items added to the mcast_list otherwise.
253 static int batadv_mcast_mla_bridge_get(struct batadv_priv *bat_priv,
254 struct net_device *dev,
255 struct hlist_head *mcast_list)
257 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
258 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
259 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
260 struct br_ip_list *br_ip_entry, *tmp;
261 struct batadv_hw_addr *new;
262 u8 mcast_addr[ETH_ALEN];
265 /* we don't need to detect these devices/listeners, the IGMP/MLD
266 * snooping code of the Linux bridge already does that for us
268 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
272 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
273 if (all_ipv4 && br_ip_entry->addr.proto == htons(ETH_P_IP))
276 if (all_ipv6 && br_ip_entry->addr.proto == htons(ETH_P_IPV6))
279 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
280 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
283 new = kmalloc(sizeof(*new), GFP_ATOMIC);
289 ether_addr_copy(new->addr, mcast_addr);
290 hlist_add_head(&new->list, mcast_list);
294 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
295 list_del(&br_ip_entry->list);
303 * batadv_mcast_mla_list_free() - free a list of multicast addresses
304 * @mcast_list: the list to free
306 * Removes and frees all items in the given mcast_list.
308 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
310 struct batadv_hw_addr *mcast_entry;
311 struct hlist_node *tmp;
313 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
314 hlist_del(&mcast_entry->list);
320 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
321 * @bat_priv: the bat priv with all the soft interface information
322 * @mcast_list: a list of addresses which should _not_ be removed
324 * Retracts the announcement of any multicast listener from the
325 * translation table except the ones listed in the given mcast_list.
327 * If mcast_list is NULL then all are retracted.
329 * Do not call outside of the mcast worker! (or cancel mcast worker first)
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 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
339 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
342 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
346 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
348 "mcast TT outdated", false);
350 hlist_del(&mcast_entry->list);
356 * batadv_mcast_mla_tt_add() - add multicast listener announcements
357 * @bat_priv: the bat priv with all the soft interface information
358 * @mcast_list: a list of addresses which are going to get added
360 * Adds multicast listener announcements from the given mcast_list to the
361 * translation table if they have not been added yet.
363 * Do not call outside of the mcast worker! (or cancel mcast worker first)
365 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
366 struct hlist_head *mcast_list)
368 struct batadv_hw_addr *mcast_entry;
369 struct hlist_node *tmp;
371 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
376 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
377 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
378 &bat_priv->mcast.mla_list))
381 if (!batadv_tt_local_add(bat_priv->soft_iface,
382 mcast_entry->addr, BATADV_NO_FLAGS,
383 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
386 hlist_del(&mcast_entry->list);
387 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
392 * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
393 * @bat_priv: the bat priv with all the soft interface information
395 * Checks whether there is a bridge on top of our soft interface.
397 * Return: true if there is a bridge, false otherwise.
399 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
401 struct net_device *upper = bat_priv->soft_iface;
405 upper = netdev_master_upper_dev_get_rcu(upper);
406 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
413 * batadv_mcast_querier_log() - debug output regarding the querier status on
415 * @bat_priv: the bat priv with all the soft interface information
416 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
417 * @old_state: the previous querier state on our link
418 * @new_state: the new querier state on our link
420 * Outputs debug messages to the logging facility with log level 'mcast'
421 * regarding changes to the querier status on the link which are relevant
422 * to our multicast optimizations.
424 * Usually this is about whether a querier appeared or vanished in
425 * our mesh or whether the querier is in the suboptimal position of being
426 * behind our local bridge segment: Snooping switches will directly
427 * forward listener reports to the querier, therefore batman-adv and
428 * the bridge will potentially not see these listeners - the querier is
429 * potentially shadowing listeners from us then.
431 * This is only interesting for nodes with a bridge on top of their
435 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
436 struct batadv_mcast_querier_state *old_state,
437 struct batadv_mcast_querier_state *new_state)
439 if (!old_state->exists && new_state->exists)
440 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
442 else if (old_state->exists && !new_state->exists)
443 batadv_info(bat_priv->soft_iface,
444 "%s Querier disappeared - multicast optimizations disabled\n",
446 else if (!bat_priv->mcast.bridged && !new_state->exists)
447 batadv_info(bat_priv->soft_iface,
448 "No %s Querier present - multicast optimizations disabled\n",
451 if (new_state->exists) {
452 if ((!old_state->shadowing && new_state->shadowing) ||
453 (!old_state->exists && new_state->shadowing))
454 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
455 "%s Querier is behind our bridged segment: Might shadow listeners\n",
457 else if (old_state->shadowing && !new_state->shadowing)
458 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
459 "%s Querier is not behind our bridged segment\n",
465 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
467 * @bat_priv: the bat priv with all the soft interface information
468 * @bridged: a flag about whether the soft interface is currently bridged or not
469 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
470 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
472 * If no bridges are ever used on this node, then this function does nothing.
474 * Otherwise this function outputs debug information to the 'mcast' log level
475 * which might be relevant to our multicast optimizations.
477 * More precisely, it outputs information when a bridge interface is added or
478 * removed from a soft interface. And when a bridge is present, it further
479 * outputs information about the querier state which is relevant for the
480 * multicast flags this node is going to set.
483 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
484 struct batadv_mcast_querier_state *querier_ipv4,
485 struct batadv_mcast_querier_state *querier_ipv6)
487 if (!bat_priv->mcast.bridged && bridged)
488 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
489 "Bridge added: Setting Unsnoopables(U)-flag\n");
490 else if (bat_priv->mcast.bridged && !bridged)
491 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
492 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
495 batadv_mcast_querier_log(bat_priv, "IGMP",
496 &bat_priv->mcast.querier_ipv4,
498 batadv_mcast_querier_log(bat_priv, "MLD",
499 &bat_priv->mcast.querier_ipv6,
505 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
506 * @bat_priv: the bat priv with all the soft interface information
507 * @flags: flags indicating the new multicast state
509 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
510 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
512 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
514 u8 old_flags = bat_priv->mcast.flags;
515 char str_old_flags[] = "[...]";
517 sprintf(str_old_flags, "[%c%c%c]",
518 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
519 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
520 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
522 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
523 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
524 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
525 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
526 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
527 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
531 * batadv_mcast_mla_tvlv_update() - update multicast tvlv
532 * @bat_priv: the bat priv with all the soft interface information
534 * Updates the own multicast tvlv with our current multicast related settings,
535 * capabilities and inabilities.
537 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
540 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
542 struct batadv_tvlv_mcast_data mcast_data;
543 struct batadv_mcast_querier_state querier4 = {false, false};
544 struct batadv_mcast_querier_state querier6 = {false, false};
545 struct net_device *dev = bat_priv->soft_iface;
548 mcast_data.flags = BATADV_NO_FLAGS;
549 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
551 bridged = batadv_mcast_has_bridge(bat_priv);
555 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
556 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
558 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
559 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
561 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
562 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
564 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
566 /* 1) If no querier exists at all, then multicast listeners on
567 * our local TT clients behind the bridge will keep silent.
568 * 2) If the selected querier is on one of our local TT clients,
569 * behind the bridge, then this querier might shadow multicast
570 * listeners on our local TT clients, behind this bridge.
572 * In both cases, we will signalize other batman nodes that
573 * we need all multicast traffic of the according protocol.
575 if (!querier4.exists || querier4.shadowing)
576 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
578 if (!querier6.exists || querier6.shadowing)
579 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
582 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
584 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
585 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
587 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
588 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
590 bat_priv->mcast.bridged = bridged;
592 if (!bat_priv->mcast.enabled ||
593 mcast_data.flags != bat_priv->mcast.flags) {
594 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
595 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
596 &mcast_data, sizeof(mcast_data));
597 bat_priv->mcast.flags = mcast_data.flags;
598 bat_priv->mcast.enabled = true;
601 return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
602 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
606 * __batadv_mcast_mla_update() - update the own MLAs
607 * @bat_priv: the bat priv with all the soft interface information
609 * Updates the own multicast listener announcements in the translation
610 * table as well as the own, announced multicast tvlv container.
612 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
613 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
614 * ensured by the non-parallel execution of the worker this function
617 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
619 struct net_device *soft_iface = bat_priv->soft_iface;
620 struct hlist_head mcast_list = HLIST_HEAD_INIT;
623 if (!batadv_mcast_mla_tvlv_update(bat_priv))
626 ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
630 ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
635 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
636 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
639 batadv_mcast_mla_list_free(&mcast_list);
643 * batadv_mcast_mla_update() - update the own MLAs
644 * @work: kernel work struct
646 * Updates the own multicast listener announcements in the translation
647 * table as well as the own, announced multicast tvlv container.
649 * In the end, reschedules the work timer.
651 static void batadv_mcast_mla_update(struct work_struct *work)
653 struct delayed_work *delayed_work;
654 struct batadv_priv_mcast *priv_mcast;
655 struct batadv_priv *bat_priv;
657 delayed_work = to_delayed_work(work);
658 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
659 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
661 __batadv_mcast_mla_update(bat_priv);
662 batadv_mcast_start_timer(bat_priv);
666 * batadv_mcast_is_report_ipv4() - check for IGMP reports
667 * @skb: the ethernet frame destined for the mesh
669 * This call might reallocate skb data.
671 * Checks whether the given frame is a valid IGMP report.
673 * Return: If so then true, otherwise false.
675 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
677 if (ip_mc_check_igmp(skb, NULL) < 0)
680 switch (igmp_hdr(skb)->type) {
681 case IGMP_HOST_MEMBERSHIP_REPORT:
682 case IGMPV2_HOST_MEMBERSHIP_REPORT:
683 case IGMPV3_HOST_MEMBERSHIP_REPORT:
691 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
693 * @bat_priv: the bat priv with all the soft interface information
694 * @skb: the IPv4 packet to check
695 * @is_unsnoopable: stores whether the destination is snoopable
697 * Checks whether the given IPv4 packet has the potential to be forwarded with a
698 * mode more optimal than classic flooding.
700 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
701 * allocation failure.
703 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
705 bool *is_unsnoopable)
709 /* We might fail due to out-of-memory -> drop it */
710 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
713 if (batadv_mcast_is_report_ipv4(skb))
718 /* TODO: Implement Multicast Router Discovery (RFC4286),
719 * then allow scope > link local, too
721 if (!ipv4_is_local_multicast(iphdr->daddr))
724 /* link-local multicast listeners behind a bridge are
725 * not snoopable (see RFC4541, section 2.1.2.2)
727 *is_unsnoopable = true;
733 * batadv_mcast_is_report_ipv6() - check for MLD reports
734 * @skb: the ethernet frame destined for the mesh
736 * This call might reallocate skb data.
738 * Checks whether the given frame is a valid MLD report.
740 * Return: If so then true, otherwise false.
742 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
744 if (ipv6_mc_check_mld(skb, NULL) < 0)
747 switch (icmp6_hdr(skb)->icmp6_type) {
748 case ICMPV6_MGM_REPORT:
749 case ICMPV6_MLD2_REPORT:
757 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
759 * @bat_priv: the bat priv with all the soft interface information
760 * @skb: the IPv6 packet to check
761 * @is_unsnoopable: stores whether the destination is snoopable
763 * Checks whether the given IPv6 packet has the potential to be forwarded with a
764 * mode more optimal than classic flooding.
766 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
768 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
770 bool *is_unsnoopable)
772 struct ipv6hdr *ip6hdr;
774 /* We might fail due to out-of-memory -> drop it */
775 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
778 if (batadv_mcast_is_report_ipv6(skb))
781 ip6hdr = ipv6_hdr(skb);
783 /* TODO: Implement Multicast Router Discovery (RFC4286),
784 * then allow scope > link local, too
786 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
789 /* link-local-all-nodes multicast listeners behind a bridge are
790 * not snoopable (see RFC4541, section 3, paragraph 3)
792 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
793 *is_unsnoopable = true;
799 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
800 * @bat_priv: the bat priv with all the soft interface information
801 * @skb: the multicast frame to check
802 * @is_unsnoopable: stores whether the destination is snoopable
804 * Checks whether the given multicast ethernet frame has the potential to be
805 * forwarded with a mode more optimal than classic flooding.
807 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
809 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
811 bool *is_unsnoopable)
813 struct ethhdr *ethhdr = eth_hdr(skb);
815 if (!atomic_read(&bat_priv->multicast_mode))
818 if (atomic_read(&bat_priv->mcast.num_disabled))
821 switch (ntohs(ethhdr->h_proto)) {
823 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
826 if (!IS_ENABLED(CONFIG_IPV6))
829 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
837 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
839 * @bat_priv: the bat priv with all the soft interface information
840 * @ethhdr: ethernet header of a packet
842 * Return: the number of nodes which want all IPv4 multicast traffic if the
843 * given ethhdr is from an IPv4 packet or the number of nodes which want all
844 * IPv6 traffic if it matches an IPv6 packet.
846 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
847 struct ethhdr *ethhdr)
849 switch (ntohs(ethhdr->h_proto)) {
851 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
853 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
855 /* we shouldn't be here... */
861 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
862 * @bat_priv: the bat priv with all the soft interface information
863 * @ethhdr: the ether header containing the multicast destination
865 * Return: an orig_node matching the multicast address provided by ethhdr
866 * via a translation table lookup. This increases the returned nodes refcount.
868 static struct batadv_orig_node *
869 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
870 struct ethhdr *ethhdr)
872 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
877 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
878 * @bat_priv: the bat priv with all the soft interface information
880 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
881 * increases its refcount.
883 static struct batadv_orig_node *
884 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
886 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
889 hlist_for_each_entry_rcu(tmp_orig_node,
890 &bat_priv->mcast.want_all_ipv4_list,
891 mcast_want_all_ipv4_node) {
892 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
895 orig_node = tmp_orig_node;
904 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
905 * @bat_priv: the bat priv with all the soft interface information
907 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
908 * and increases its refcount.
910 static struct batadv_orig_node *
911 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
913 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
916 hlist_for_each_entry_rcu(tmp_orig_node,
917 &bat_priv->mcast.want_all_ipv6_list,
918 mcast_want_all_ipv6_node) {
919 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
922 orig_node = tmp_orig_node;
931 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
932 * @bat_priv: the bat priv with all the soft interface information
933 * @ethhdr: an ethernet header to determine the protocol family from
935 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
936 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
937 * increases its refcount.
939 static struct batadv_orig_node *
940 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
941 struct ethhdr *ethhdr)
943 switch (ntohs(ethhdr->h_proto)) {
945 return batadv_mcast_forw_ipv4_node_get(bat_priv);
947 return batadv_mcast_forw_ipv6_node_get(bat_priv);
949 /* we shouldn't be here... */
955 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
956 * @bat_priv: the bat priv with all the soft interface information
958 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
959 * set and increases its refcount.
961 static struct batadv_orig_node *
962 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
964 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
967 hlist_for_each_entry_rcu(tmp_orig_node,
968 &bat_priv->mcast.want_all_unsnoopables_list,
969 mcast_want_all_unsnoopables_node) {
970 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
973 orig_node = tmp_orig_node;
982 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
983 * @bat_priv: the bat priv with all the soft interface information
984 * @skb: The multicast packet to check
985 * @orig: an originator to be set to forward the skb to
987 * Return: the forwarding mode as enum batadv_forw_mode and in case of
988 * BATADV_FORW_SINGLE set the orig to the single originator the skb
989 * should be forwarded to.
991 enum batadv_forw_mode
992 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
993 struct batadv_orig_node **orig)
995 int ret, tt_count, ip_count, unsnoop_count, total_count;
996 bool is_unsnoopable = false;
997 struct ethhdr *ethhdr;
999 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
1001 return BATADV_FORW_NONE;
1003 return BATADV_FORW_ALL;
1005 ethhdr = eth_hdr(skb);
1007 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1009 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1010 unsnoop_count = !is_unsnoopable ? 0 :
1011 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
1013 total_count = tt_count + ip_count + unsnoop_count;
1015 switch (total_count) {
1018 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1020 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1021 else if (unsnoop_count)
1022 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1025 return BATADV_FORW_SINGLE;
1029 return BATADV_FORW_NONE;
1031 return BATADV_FORW_ALL;
1036 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1037 * @bat_priv: the bat priv with all the soft interface information
1038 * @orig: the orig_node which multicast state might have changed of
1039 * @mcast_flags: flags indicating the new multicast state
1041 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1042 * orig, has toggled then this method updates counter and list accordingly.
1044 * Caller needs to hold orig->mcast_handler_lock.
1046 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1047 struct batadv_orig_node *orig,
1050 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1051 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1053 lockdep_assert_held(&orig->mcast_handler_lock);
1055 /* switched from flag unset to set */
1056 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1057 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1058 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1060 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1061 /* flag checks above + mcast_handler_lock prevents this */
1062 WARN_ON(!hlist_unhashed(node));
1064 hlist_add_head_rcu(node, head);
1065 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1066 /* switched from flag set to unset */
1067 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1068 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1069 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1071 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1072 /* flag checks above + mcast_handler_lock prevents this */
1073 WARN_ON(hlist_unhashed(node));
1075 hlist_del_init_rcu(node);
1076 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1081 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1082 * @bat_priv: the bat priv with all the soft interface information
1083 * @orig: the orig_node which multicast state might have changed of
1084 * @mcast_flags: flags indicating the new multicast state
1086 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1087 * toggled then this method updates counter and list accordingly.
1089 * Caller needs to hold orig->mcast_handler_lock.
1091 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1092 struct batadv_orig_node *orig,
1095 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1096 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1098 lockdep_assert_held(&orig->mcast_handler_lock);
1100 /* switched from flag unset to set */
1101 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1102 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1103 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1105 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1106 /* flag checks above + mcast_handler_lock prevents this */
1107 WARN_ON(!hlist_unhashed(node));
1109 hlist_add_head_rcu(node, head);
1110 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1111 /* switched from flag set to unset */
1112 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1113 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1114 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1116 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1117 /* flag checks above + mcast_handler_lock prevents this */
1118 WARN_ON(hlist_unhashed(node));
1120 hlist_del_init_rcu(node);
1121 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1126 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1127 * @bat_priv: the bat priv with all the soft interface information
1128 * @orig: the orig_node which multicast state might have changed of
1129 * @mcast_flags: flags indicating the new multicast state
1131 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1132 * toggled then this method updates counter and list accordingly.
1134 * Caller needs to hold orig->mcast_handler_lock.
1136 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1137 struct batadv_orig_node *orig,
1140 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1141 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1143 lockdep_assert_held(&orig->mcast_handler_lock);
1145 /* switched from flag unset to set */
1146 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1147 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1148 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1150 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1151 /* flag checks above + mcast_handler_lock prevents this */
1152 WARN_ON(!hlist_unhashed(node));
1154 hlist_add_head_rcu(node, head);
1155 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1156 /* switched from flag set to unset */
1157 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1158 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1159 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1161 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1162 /* flag checks above + mcast_handler_lock prevents this */
1163 WARN_ON(hlist_unhashed(node));
1165 hlist_del_init_rcu(node);
1166 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1171 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1172 * @bat_priv: the bat priv with all the soft interface information
1173 * @orig: the orig_node of the ogm
1174 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1175 * @tvlv_value: tvlv buffer containing the multicast data
1176 * @tvlv_value_len: tvlv buffer length
1178 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1179 struct batadv_orig_node *orig,
1184 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1185 u8 mcast_flags = BATADV_NO_FLAGS;
1186 bool orig_initialized;
1188 if (orig_mcast_enabled && tvlv_value &&
1189 tvlv_value_len >= sizeof(mcast_flags))
1190 mcast_flags = *(u8 *)tvlv_value;
1192 spin_lock_bh(&orig->mcast_handler_lock);
1193 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1194 &orig->capa_initialized);
1196 /* If mcast support is turned on decrease the disabled mcast node
1197 * counter only if we had increased it for this node before. If this
1198 * is a completely new orig_node no need to decrease the counter.
1200 if (orig_mcast_enabled &&
1201 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1202 if (orig_initialized)
1203 atomic_dec(&bat_priv->mcast.num_disabled);
1204 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1205 /* If mcast support is being switched off or if this is an initial
1206 * OGM without mcast support then increase the disabled mcast
1209 } else if (!orig_mcast_enabled &&
1210 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1211 !orig_initialized)) {
1212 atomic_inc(&bat_priv->mcast.num_disabled);
1213 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1216 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1218 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1219 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1220 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1222 orig->mcast_flags = mcast_flags;
1223 spin_unlock_bh(&orig->mcast_handler_lock);
1227 * batadv_mcast_init() - initialize the multicast optimizations structures
1228 * @bat_priv: the bat priv with all the soft interface information
1230 void batadv_mcast_init(struct batadv_priv *bat_priv)
1232 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1233 NULL, BATADV_TVLV_MCAST, 2,
1234 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1236 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1237 batadv_mcast_start_timer(bat_priv);
1240 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1242 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
1243 * @bat_priv: the bat priv with all the soft interface information
1244 * @seq: debugfs table seq_file struct
1246 * Prints our own multicast flags including a more specific reason why
1247 * they are set, that is prints the bridge and querier state too, to
1248 * the debugfs table specified via @seq.
1250 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1251 struct seq_file *seq)
1253 u8 flags = bat_priv->mcast.flags;
1254 char querier4, querier6, shadowing4, shadowing6;
1255 bool bridged = bat_priv->mcast.bridged;
1258 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1259 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1260 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1261 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1269 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1270 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1271 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1272 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1273 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1274 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1275 querier4, querier6);
1276 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1277 shadowing4, shadowing6);
1278 seq_puts(seq, "-------------------------------------------\n");
1279 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1283 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
1284 * @seq: seq file to print on
1287 * This prints a table of (primary) originators and their according
1288 * multicast flags, including (in the header) our own.
1292 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1294 struct net_device *net_dev = (struct net_device *)seq->private;
1295 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1296 struct batadv_hard_iface *primary_if;
1297 struct batadv_hashtable *hash = bat_priv->orig_hash;
1298 struct batadv_orig_node *orig_node;
1299 struct hlist_head *head;
1303 primary_if = batadv_seq_print_text_primary_if_get(seq);
1307 batadv_mcast_flags_print_header(bat_priv, seq);
1309 for (i = 0; i < hash->size; i++) {
1310 head = &hash->table[i];
1313 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1314 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1315 &orig_node->capa_initialized))
1318 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1319 &orig_node->capabilities)) {
1320 seq_printf(seq, "%pM -\n", orig_node->orig);
1324 flags = orig_node->mcast_flags;
1326 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1327 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1329 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1331 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1337 batadv_hardif_put(primary_if);
1344 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
1345 * @msg: buffer for the message
1346 * @bat_priv: the bat priv with all the soft interface information
1348 * Return: 0 or error code.
1350 int batadv_mcast_mesh_info_put(struct sk_buff *msg,
1351 struct batadv_priv *bat_priv)
1353 u32 flags = bat_priv->mcast.flags;
1354 u32 flags_priv = BATADV_NO_FLAGS;
1356 if (bat_priv->mcast.bridged) {
1357 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
1359 if (bat_priv->mcast.querier_ipv4.exists)
1360 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
1361 if (bat_priv->mcast.querier_ipv6.exists)
1362 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
1363 if (bat_priv->mcast.querier_ipv4.shadowing)
1364 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
1365 if (bat_priv->mcast.querier_ipv6.shadowing)
1366 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
1369 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
1370 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
1377 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
1378 * to a netlink socket
1379 * @msg: buffer for the message
1380 * @portid: netlink port
1381 * @seq: Sequence number of netlink message
1382 * @orig_node: originator to dump the multicast flags of
1384 * Return: 0 or error code.
1387 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1388 struct batadv_orig_node *orig_node)
1392 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1393 NLM_F_MULTI, BATADV_CMD_GET_MCAST_FLAGS);
1397 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1399 genlmsg_cancel(msg, hdr);
1403 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1404 &orig_node->capabilities)) {
1405 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
1406 orig_node->mcast_flags)) {
1407 genlmsg_cancel(msg, hdr);
1412 genlmsg_end(msg, hdr);
1417 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
1418 * table to a netlink socket
1419 * @msg: buffer for the message
1420 * @portid: netlink port
1421 * @seq: Sequence number of netlink message
1422 * @head: bucket to dump
1423 * @idx_skip: How many entries to skip
1425 * Return: 0 or error code.
1428 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1429 struct hlist_head *head, long *idx_skip)
1431 struct batadv_orig_node *orig_node;
1435 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1436 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1437 &orig_node->capa_initialized))
1440 if (idx < *idx_skip)
1443 if (batadv_mcast_flags_dump_entry(msg, portid, seq,
1460 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1461 * @msg: buffer for the message
1462 * @portid: netlink port
1463 * @seq: Sequence number of netlink message
1464 * @bat_priv: the bat priv with all the soft interface information
1465 * @bucket: current bucket to dump
1466 * @idx: index in current bucket to the next entry to dump
1468 * Return: 0 or error code.
1471 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid, u32 seq,
1472 struct batadv_priv *bat_priv, long *bucket, long *idx)
1474 struct batadv_hashtable *hash = bat_priv->orig_hash;
1475 long bucket_tmp = *bucket;
1476 struct hlist_head *head;
1477 long idx_tmp = *idx;
1479 while (bucket_tmp < hash->size) {
1480 head = &hash->table[bucket_tmp];
1482 if (batadv_mcast_flags_dump_bucket(msg, portid, seq, head,
1490 *bucket = bucket_tmp;
1497 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
1499 * @cb: netlink callback structure
1500 * @primary_if: the primary interface pointer to return the result in
1502 * Return: 0 or error code.
1505 batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
1506 struct batadv_hard_iface **primary_if)
1508 struct batadv_hard_iface *hard_iface = NULL;
1509 struct net *net = sock_net(cb->skb->sk);
1510 struct net_device *soft_iface;
1511 struct batadv_priv *bat_priv;
1515 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1519 soft_iface = dev_get_by_index(net, ifindex);
1520 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1525 bat_priv = netdev_priv(soft_iface);
1527 hard_iface = batadv_primary_if_get_selected(bat_priv);
1528 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
1535 dev_put(soft_iface);
1537 if (!ret && primary_if)
1538 *primary_if = hard_iface;
1539 else if (hard_iface)
1540 batadv_hardif_put(hard_iface);
1546 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
1547 * @msg: buffer for the message
1548 * @cb: callback structure containing arguments
1550 * Return: message length.
1552 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
1554 struct batadv_hard_iface *primary_if = NULL;
1555 int portid = NETLINK_CB(cb->skb).portid;
1556 struct batadv_priv *bat_priv;
1557 long *bucket = &cb->args[0];
1558 long *idx = &cb->args[1];
1561 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
1565 bat_priv = netdev_priv(primary_if->soft_iface);
1566 ret = __batadv_mcast_flags_dump(msg, portid, cb->nlh->nlmsg_seq,
1567 bat_priv, bucket, idx);
1569 batadv_hardif_put(primary_if);
1574 * batadv_mcast_free() - free the multicast optimizations structures
1575 * @bat_priv: the bat priv with all the soft interface information
1577 void batadv_mcast_free(struct batadv_priv *bat_priv)
1579 cancel_delayed_work_sync(&bat_priv->mcast.work);
1581 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1582 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1584 /* safely calling outside of worker, as worker was canceled above */
1585 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1589 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
1590 * @orig: the originator which is going to get purged
1592 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1594 struct batadv_priv *bat_priv = orig->bat_priv;
1596 spin_lock_bh(&orig->mcast_handler_lock);
1598 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1599 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1600 atomic_dec(&bat_priv->mcast.num_disabled);
1602 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1603 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1604 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1606 spin_unlock_bh(&orig->mcast_handler_lock);