1 /* Copyright (C) 2014-2017 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "multicast.h"
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
28 #include <linux/icmpv6.h>
29 #include <linux/if_bridge.h>
30 #include <linux/if_ether.h>
31 #include <linux/igmp.h>
33 #include <linux/in6.h>
35 #include <linux/ipv6.h>
36 #include <linux/jiffies.h>
37 #include <linux/kernel.h>
38 #include <linux/kref.h>
39 #include <linux/list.h>
40 #include <linux/lockdep.h>
41 #include <linux/netdevice.h>
42 #include <linux/printk.h>
43 #include <linux/rculist.h>
44 #include <linux/rcupdate.h>
45 #include <linux/seq_file.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/stddef.h>
50 #include <linux/string.h>
51 #include <linux/types.h>
52 #include <linux/workqueue.h>
53 #include <net/addrconf.h>
54 #include <net/if_inet6.h>
58 #include "bridge_loop_avoidance.h"
59 #include "hard-interface.h"
64 #include "translation-table.h"
67 static void batadv_mcast_mla_update(struct work_struct *work);
70 * batadv_mcast_start_timer - schedule the multicast periodic worker
71 * @bat_priv: the bat priv with all the soft interface information
73 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
75 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
76 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
80 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
81 * @soft_iface: netdev struct of the mesh interface
83 * If the given soft interface has a bridge on top then the refcount
84 * of the according net device is increased.
86 * Return: NULL if no such bridge exists. Otherwise the net device of the
89 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
91 struct net_device *upper = soft_iface;
95 upper = netdev_master_upper_dev_get_rcu(upper);
96 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
106 * batadv_mcast_mla_softif_get - get softif multicast listeners
107 * @dev: the device to collect multicast addresses from
108 * @mcast_list: a list to put found addresses into
110 * Collects multicast addresses of multicast listeners residing
111 * on this kernel on the given soft interface, dev, in
112 * the given mcast_list. In general, multicast listeners provided by
113 * your multicast receiving applications run directly on this node.
115 * If there is a bridge interface on top of dev, collects from that one
116 * instead. Just like with IP addresses and routes, multicast listeners
117 * will(/should) register to the bridge interface instead of an
120 * Return: -ENOMEM on memory allocation error or the number of
121 * items added to the mcast_list otherwise.
123 static int batadv_mcast_mla_softif_get(struct net_device *dev,
124 struct hlist_head *mcast_list)
126 struct net_device *bridge = batadv_mcast_get_bridge(dev);
127 struct netdev_hw_addr *mc_list_entry;
128 struct batadv_hw_addr *new;
131 netif_addr_lock_bh(bridge ? bridge : dev);
132 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
133 new = kmalloc(sizeof(*new), GFP_ATOMIC);
139 ether_addr_copy(new->addr, mc_list_entry->addr);
140 hlist_add_head(&new->list, mcast_list);
143 netif_addr_unlock_bh(bridge ? bridge : dev);
152 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
153 * @mcast_addr: the multicast address to check
154 * @mcast_list: the list with multicast addresses to search in
156 * Return: true if the given address is already in the given list.
157 * Otherwise returns false.
159 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
160 struct hlist_head *mcast_list)
162 struct batadv_hw_addr *mcast_entry;
164 hlist_for_each_entry(mcast_entry, mcast_list, list)
165 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
172 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
173 * @dst: destination to write to - a multicast MAC address
174 * @src: source to read from - a multicast IP address
176 * Converts a given multicast IPv4/IPv6 address from a bridge
177 * to its matching multicast MAC address and copies it into the given
178 * destination buffer.
180 * Caller needs to make sure the destination buffer can hold
181 * at least ETH_ALEN bytes.
183 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
185 if (src->proto == htons(ETH_P_IP))
186 ip_eth_mc_map(src->u.ip4, dst);
187 #if IS_ENABLED(CONFIG_IPV6)
188 else if (src->proto == htons(ETH_P_IPV6))
189 ipv6_eth_mc_map(&src->u.ip6, dst);
196 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
197 * @dev: a bridge slave whose bridge to collect multicast addresses from
198 * @mcast_list: a list to put found addresses into
200 * Collects multicast addresses of multicast listeners residing
201 * on foreign, non-mesh devices which we gave access to our mesh via
202 * a bridge on top of the given soft interface, dev, in the given
205 * Return: -ENOMEM on memory allocation error or the number of
206 * items added to the mcast_list otherwise.
208 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
209 struct hlist_head *mcast_list)
211 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
212 struct br_ip_list *br_ip_entry, *tmp;
213 struct batadv_hw_addr *new;
214 u8 mcast_addr[ETH_ALEN];
217 /* we don't need to detect these devices/listeners, the IGMP/MLD
218 * snooping code of the Linux bridge already does that for us
220 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
224 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
225 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
226 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
229 new = kmalloc(sizeof(*new), GFP_ATOMIC);
235 ether_addr_copy(new->addr, mcast_addr);
236 hlist_add_head(&new->list, mcast_list);
240 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
241 list_del(&br_ip_entry->list);
249 * batadv_mcast_mla_list_free - free a list of multicast addresses
250 * @mcast_list: the list to free
252 * Removes and frees all items in the given mcast_list.
254 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
256 struct batadv_hw_addr *mcast_entry;
257 struct hlist_node *tmp;
259 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
260 hlist_del(&mcast_entry->list);
266 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
267 * @bat_priv: the bat priv with all the soft interface information
268 * @mcast_list: a list of addresses which should _not_ be removed
270 * Retracts the announcement of any multicast listener from the
271 * translation table except the ones listed in the given mcast_list.
273 * If mcast_list is NULL then all are retracted.
275 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
276 struct hlist_head *mcast_list)
278 struct batadv_hw_addr *mcast_entry;
279 struct hlist_node *tmp;
281 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
284 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
288 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
290 "mcast TT outdated", false);
292 hlist_del(&mcast_entry->list);
298 * batadv_mcast_mla_tt_add - add multicast listener announcements
299 * @bat_priv: the bat priv with all the soft interface information
300 * @mcast_list: a list of addresses which are going to get added
302 * Adds multicast listener announcements from the given mcast_list to the
303 * translation table if they have not been added yet.
305 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
306 struct hlist_head *mcast_list)
308 struct batadv_hw_addr *mcast_entry;
309 struct hlist_node *tmp;
314 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
315 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
316 &bat_priv->mcast.mla_list))
319 if (!batadv_tt_local_add(bat_priv->soft_iface,
320 mcast_entry->addr, BATADV_NO_FLAGS,
321 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
324 hlist_del(&mcast_entry->list);
325 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
330 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
331 * @bat_priv: the bat priv with all the soft interface information
333 * Checks whether there is a bridge on top of our soft interface.
335 * Return: true if there is a bridge, false otherwise.
337 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
339 struct net_device *upper = bat_priv->soft_iface;
343 upper = netdev_master_upper_dev_get_rcu(upper);
344 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
351 * batadv_mcast_querier_log - debug output regarding the querier status on link
352 * @bat_priv: the bat priv with all the soft interface information
353 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
354 * @old_state: the previous querier state on our link
355 * @new_state: the new querier state on our link
357 * Outputs debug messages to the logging facility with log level 'mcast'
358 * regarding changes to the querier status on the link which are relevant
359 * to our multicast optimizations.
361 * Usually this is about whether a querier appeared or vanished in
362 * our mesh or whether the querier is in the suboptimal position of being
363 * behind our local bridge segment: Snooping switches will directly
364 * forward listener reports to the querier, therefore batman-adv and
365 * the bridge will potentially not see these listeners - the querier is
366 * potentially shadowing listeners from us then.
368 * This is only interesting for nodes with a bridge on top of their
372 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
373 struct batadv_mcast_querier_state *old_state,
374 struct batadv_mcast_querier_state *new_state)
376 if (!old_state->exists && new_state->exists)
377 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
379 else if (old_state->exists && !new_state->exists)
380 batadv_info(bat_priv->soft_iface,
381 "%s Querier disappeared - multicast optimizations disabled\n",
383 else if (!bat_priv->mcast.bridged && !new_state->exists)
384 batadv_info(bat_priv->soft_iface,
385 "No %s Querier present - multicast optimizations disabled\n",
388 if (new_state->exists) {
389 if ((!old_state->shadowing && new_state->shadowing) ||
390 (!old_state->exists && new_state->shadowing))
391 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
392 "%s Querier is behind our bridged segment: Might shadow listeners\n",
394 else if (old_state->shadowing && !new_state->shadowing)
395 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
396 "%s Querier is not behind our bridged segment\n",
402 * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
403 * @bat_priv: the bat priv with all the soft interface information
404 * @bridged: a flag about whether the soft interface is currently bridged or not
405 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
406 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
408 * If no bridges are ever used on this node, then this function does nothing.
410 * Otherwise this function outputs debug information to the 'mcast' log level
411 * which might be relevant to our multicast optimizations.
413 * More precisely, it outputs information when a bridge interface is added or
414 * removed from a soft interface. And when a bridge is present, it further
415 * outputs information about the querier state which is relevant for the
416 * multicast flags this node is going to set.
419 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
420 struct batadv_mcast_querier_state *querier_ipv4,
421 struct batadv_mcast_querier_state *querier_ipv6)
423 if (!bat_priv->mcast.bridged && bridged)
424 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
425 "Bridge added: Setting Unsnoopables(U)-flag\n");
426 else if (bat_priv->mcast.bridged && !bridged)
427 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
428 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
431 batadv_mcast_querier_log(bat_priv, "IGMP",
432 &bat_priv->mcast.querier_ipv4,
434 batadv_mcast_querier_log(bat_priv, "MLD",
435 &bat_priv->mcast.querier_ipv6,
441 * batadv_mcast_flags_logs - output debug information about mcast flag changes
442 * @bat_priv: the bat priv with all the soft interface information
443 * @flags: flags indicating the new multicast state
445 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
446 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
448 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
450 u8 old_flags = bat_priv->mcast.flags;
451 char str_old_flags[] = "[...]";
453 sprintf(str_old_flags, "[%c%c%c]",
454 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
455 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
456 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
458 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
459 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
460 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
461 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
462 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
463 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
467 * batadv_mcast_mla_tvlv_update - update multicast tvlv
468 * @bat_priv: the bat priv with all the soft interface information
470 * Updates the own multicast tvlv with our current multicast related settings,
471 * capabilities and inabilities.
473 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
476 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
478 struct batadv_tvlv_mcast_data mcast_data;
479 struct batadv_mcast_querier_state querier4 = {false, false};
480 struct batadv_mcast_querier_state querier6 = {false, false};
481 struct net_device *dev = bat_priv->soft_iface;
484 mcast_data.flags = BATADV_NO_FLAGS;
485 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
487 bridged = batadv_mcast_has_bridge(bat_priv);
491 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
492 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
494 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
495 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
497 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
498 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
500 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
502 /* 1) If no querier exists at all, then multicast listeners on
503 * our local TT clients behind the bridge will keep silent.
504 * 2) If the selected querier is on one of our local TT clients,
505 * behind the bridge, then this querier might shadow multicast
506 * listeners on our local TT clients, behind this bridge.
508 * In both cases, we will signalize other batman nodes that
509 * we need all multicast traffic of the according protocol.
511 if (!querier4.exists || querier4.shadowing)
512 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
514 if (!querier6.exists || querier6.shadowing)
515 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
518 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
520 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
521 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
523 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
524 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
526 bat_priv->mcast.bridged = bridged;
528 if (!bat_priv->mcast.enabled ||
529 mcast_data.flags != bat_priv->mcast.flags) {
530 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
531 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
532 &mcast_data, sizeof(mcast_data));
533 bat_priv->mcast.flags = mcast_data.flags;
534 bat_priv->mcast.enabled = true;
537 return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
538 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
542 * __batadv_mcast_mla_update - update the own MLAs
543 * @bat_priv: the bat priv with all the soft interface information
545 * Updates the own multicast listener announcements in the translation
546 * table as well as the own, announced multicast tvlv container.
548 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
549 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
550 * ensured by the non-parallel execution of the worker this function
553 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
555 struct net_device *soft_iface = bat_priv->soft_iface;
556 struct hlist_head mcast_list = HLIST_HEAD_INIT;
559 if (!batadv_mcast_mla_tvlv_update(bat_priv))
562 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
566 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
571 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
572 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
575 batadv_mcast_mla_list_free(&mcast_list);
579 * batadv_mcast_mla_update - update the own MLAs
580 * @work: kernel work struct
582 * Updates the own multicast listener announcements in the translation
583 * table as well as the own, announced multicast tvlv container.
585 * In the end, reschedules the work timer.
587 static void batadv_mcast_mla_update(struct work_struct *work)
589 struct delayed_work *delayed_work;
590 struct batadv_priv_mcast *priv_mcast;
591 struct batadv_priv *bat_priv;
593 delayed_work = to_delayed_work(work);
594 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
595 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
597 spin_lock(&bat_priv->mcast.mla_lock);
598 __batadv_mcast_mla_update(bat_priv);
599 spin_unlock(&bat_priv->mcast.mla_lock);
601 batadv_mcast_start_timer(bat_priv);
605 * batadv_mcast_is_report_ipv4 - check for IGMP reports
606 * @skb: the ethernet frame destined for the mesh
608 * This call might reallocate skb data.
610 * Checks whether the given frame is a valid IGMP report.
612 * Return: If so then true, otherwise false.
614 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
616 if (ip_mc_check_igmp(skb, NULL) < 0)
619 switch (igmp_hdr(skb)->type) {
620 case IGMP_HOST_MEMBERSHIP_REPORT:
621 case IGMPV2_HOST_MEMBERSHIP_REPORT:
622 case IGMPV3_HOST_MEMBERSHIP_REPORT:
630 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
631 * @bat_priv: the bat priv with all the soft interface information
632 * @skb: the IPv4 packet to check
633 * @is_unsnoopable: stores whether the destination is snoopable
635 * Checks whether the given IPv4 packet has the potential to be forwarded with a
636 * mode more optimal than classic flooding.
638 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
639 * allocation failure.
641 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
643 bool *is_unsnoopable)
647 /* We might fail due to out-of-memory -> drop it */
648 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
651 if (batadv_mcast_is_report_ipv4(skb))
656 /* TODO: Implement Multicast Router Discovery (RFC4286),
657 * then allow scope > link local, too
659 if (!ipv4_is_local_multicast(iphdr->daddr))
662 /* link-local multicast listeners behind a bridge are
663 * not snoopable (see RFC4541, section 2.1.2.2)
665 *is_unsnoopable = true;
671 * batadv_mcast_is_report_ipv6 - check for MLD reports
672 * @skb: the ethernet frame destined for the mesh
674 * This call might reallocate skb data.
676 * Checks whether the given frame is a valid MLD report.
678 * Return: If so then true, otherwise false.
680 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
682 if (ipv6_mc_check_mld(skb, NULL) < 0)
685 switch (icmp6_hdr(skb)->icmp6_type) {
686 case ICMPV6_MGM_REPORT:
687 case ICMPV6_MLD2_REPORT:
695 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
696 * @bat_priv: the bat priv with all the soft interface information
697 * @skb: the IPv6 packet to check
698 * @is_unsnoopable: stores whether the destination is snoopable
700 * Checks whether the given IPv6 packet has the potential to be forwarded with a
701 * mode more optimal than classic flooding.
703 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
705 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
707 bool *is_unsnoopable)
709 struct ipv6hdr *ip6hdr;
711 /* We might fail due to out-of-memory -> drop it */
712 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
715 if (batadv_mcast_is_report_ipv6(skb))
718 ip6hdr = ipv6_hdr(skb);
720 /* TODO: Implement Multicast Router Discovery (RFC4286),
721 * then allow scope > link local, too
723 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
726 /* link-local-all-nodes multicast listeners behind a bridge are
727 * not snoopable (see RFC4541, section 3, paragraph 3)
729 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
730 *is_unsnoopable = true;
736 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
737 * @bat_priv: the bat priv with all the soft interface information
738 * @skb: the multicast frame to check
739 * @is_unsnoopable: stores whether the destination is snoopable
741 * Checks whether the given multicast ethernet frame has the potential to be
742 * forwarded with a mode more optimal than classic flooding.
744 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
746 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
748 bool *is_unsnoopable)
750 struct ethhdr *ethhdr = eth_hdr(skb);
752 if (!atomic_read(&bat_priv->multicast_mode))
755 if (atomic_read(&bat_priv->mcast.num_disabled))
758 switch (ntohs(ethhdr->h_proto)) {
760 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
763 if (!IS_ENABLED(CONFIG_IPV6))
766 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
774 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
776 * @bat_priv: the bat priv with all the soft interface information
777 * @ethhdr: ethernet header of a packet
779 * Return: the number of nodes which want all IPv4 multicast traffic if the
780 * given ethhdr is from an IPv4 packet or the number of nodes which want all
781 * IPv6 traffic if it matches an IPv6 packet.
783 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
784 struct ethhdr *ethhdr)
786 switch (ntohs(ethhdr->h_proto)) {
788 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
790 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
792 /* we shouldn't be here... */
798 * batadv_mcast_forw_tt_node_get - get a multicast tt node
799 * @bat_priv: the bat priv with all the soft interface information
800 * @ethhdr: the ether header containing the multicast destination
802 * Return: an orig_node matching the multicast address provided by ethhdr
803 * via a translation table lookup. This increases the returned nodes refcount.
805 static struct batadv_orig_node *
806 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
807 struct ethhdr *ethhdr)
809 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
814 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
815 * @bat_priv: the bat priv with all the soft interface information
817 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
818 * increases its refcount.
820 static struct batadv_orig_node *
821 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
823 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
826 hlist_for_each_entry_rcu(tmp_orig_node,
827 &bat_priv->mcast.want_all_ipv4_list,
828 mcast_want_all_ipv4_node) {
829 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
832 orig_node = tmp_orig_node;
841 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
842 * @bat_priv: the bat priv with all the soft interface information
844 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
845 * and increases its refcount.
847 static struct batadv_orig_node *
848 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
850 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
853 hlist_for_each_entry_rcu(tmp_orig_node,
854 &bat_priv->mcast.want_all_ipv6_list,
855 mcast_want_all_ipv6_node) {
856 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
859 orig_node = tmp_orig_node;
868 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
869 * @bat_priv: the bat priv with all the soft interface information
870 * @ethhdr: an ethernet header to determine the protocol family from
872 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
873 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
874 * increases its refcount.
876 static struct batadv_orig_node *
877 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
878 struct ethhdr *ethhdr)
880 switch (ntohs(ethhdr->h_proto)) {
882 return batadv_mcast_forw_ipv4_node_get(bat_priv);
884 return batadv_mcast_forw_ipv6_node_get(bat_priv);
886 /* we shouldn't be here... */
892 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
893 * @bat_priv: the bat priv with all the soft interface information
895 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
896 * set and increases its refcount.
898 static struct batadv_orig_node *
899 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
901 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
904 hlist_for_each_entry_rcu(tmp_orig_node,
905 &bat_priv->mcast.want_all_unsnoopables_list,
906 mcast_want_all_unsnoopables_node) {
907 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
910 orig_node = tmp_orig_node;
919 * batadv_mcast_forw_mode - check on how to forward a multicast packet
920 * @bat_priv: the bat priv with all the soft interface information
921 * @skb: The multicast packet to check
922 * @orig: an originator to be set to forward the skb to
924 * Return: the forwarding mode as enum batadv_forw_mode and in case of
925 * BATADV_FORW_SINGLE set the orig to the single originator the skb
926 * should be forwarded to.
928 enum batadv_forw_mode
929 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
930 struct batadv_orig_node **orig)
932 int ret, tt_count, ip_count, unsnoop_count, total_count;
933 bool is_unsnoopable = false;
934 struct ethhdr *ethhdr;
936 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
938 return BATADV_FORW_NONE;
940 return BATADV_FORW_ALL;
942 ethhdr = eth_hdr(skb);
944 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
946 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
947 unsnoop_count = !is_unsnoopable ? 0 :
948 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
950 total_count = tt_count + ip_count + unsnoop_count;
952 switch (total_count) {
955 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
957 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
958 else if (unsnoop_count)
959 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
962 return BATADV_FORW_SINGLE;
966 return BATADV_FORW_NONE;
968 return BATADV_FORW_ALL;
973 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
974 * @bat_priv: the bat priv with all the soft interface information
975 * @orig: the orig_node which multicast state might have changed of
976 * @mcast_flags: flags indicating the new multicast state
978 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
979 * orig, has toggled then this method updates counter and list accordingly.
981 * Caller needs to hold orig->mcast_handler_lock.
983 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
984 struct batadv_orig_node *orig,
987 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
988 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
990 lockdep_assert_held(&orig->mcast_handler_lock);
992 /* switched from flag unset to set */
993 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
994 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
995 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
997 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
998 /* flag checks above + mcast_handler_lock prevents this */
999 WARN_ON(!hlist_unhashed(node));
1001 hlist_add_head_rcu(node, head);
1002 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1003 /* switched from flag set to unset */
1004 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1005 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1006 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1008 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1009 /* flag checks above + mcast_handler_lock prevents this */
1010 WARN_ON(hlist_unhashed(node));
1012 hlist_del_init_rcu(node);
1013 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1018 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
1019 * @bat_priv: the bat priv with all the soft interface information
1020 * @orig: the orig_node which multicast state might have changed of
1021 * @mcast_flags: flags indicating the new multicast state
1023 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1024 * toggled then this method updates counter and list accordingly.
1026 * Caller needs to hold orig->mcast_handler_lock.
1028 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1029 struct batadv_orig_node *orig,
1032 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1033 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1035 lockdep_assert_held(&orig->mcast_handler_lock);
1037 /* switched from flag unset to set */
1038 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1039 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1040 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1042 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1043 /* flag checks above + mcast_handler_lock prevents this */
1044 WARN_ON(!hlist_unhashed(node));
1046 hlist_add_head_rcu(node, head);
1047 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1048 /* switched from flag set to unset */
1049 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1050 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1051 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1053 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1054 /* flag checks above + mcast_handler_lock prevents this */
1055 WARN_ON(hlist_unhashed(node));
1057 hlist_del_init_rcu(node);
1058 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1063 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1064 * @bat_priv: the bat priv with all the soft interface information
1065 * @orig: the orig_node which multicast state might have changed of
1066 * @mcast_flags: flags indicating the new multicast state
1068 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1069 * toggled then this method updates counter and list accordingly.
1071 * Caller needs to hold orig->mcast_handler_lock.
1073 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1074 struct batadv_orig_node *orig,
1077 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1078 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1080 lockdep_assert_held(&orig->mcast_handler_lock);
1082 /* switched from flag unset to set */
1083 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1084 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1085 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1087 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1088 /* flag checks above + mcast_handler_lock prevents this */
1089 WARN_ON(!hlist_unhashed(node));
1091 hlist_add_head_rcu(node, head);
1092 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1093 /* switched from flag set to unset */
1094 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1095 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1096 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1098 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1099 /* flag checks above + mcast_handler_lock prevents this */
1100 WARN_ON(hlist_unhashed(node));
1102 hlist_del_init_rcu(node);
1103 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1108 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
1109 * @bat_priv: the bat priv with all the soft interface information
1110 * @orig: the orig_node of the ogm
1111 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1112 * @tvlv_value: tvlv buffer containing the multicast data
1113 * @tvlv_value_len: tvlv buffer length
1115 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1116 struct batadv_orig_node *orig,
1121 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1122 u8 mcast_flags = BATADV_NO_FLAGS;
1123 bool orig_initialized;
1125 if (orig_mcast_enabled && tvlv_value &&
1126 (tvlv_value_len >= sizeof(mcast_flags)))
1127 mcast_flags = *(u8 *)tvlv_value;
1129 spin_lock_bh(&orig->mcast_handler_lock);
1130 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1131 &orig->capa_initialized);
1133 /* If mcast support is turned on decrease the disabled mcast node
1134 * counter only if we had increased it for this node before. If this
1135 * is a completely new orig_node no need to decrease the counter.
1137 if (orig_mcast_enabled &&
1138 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1139 if (orig_initialized)
1140 atomic_dec(&bat_priv->mcast.num_disabled);
1141 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1142 /* If mcast support is being switched off or if this is an initial
1143 * OGM without mcast support then increase the disabled mcast
1146 } else if (!orig_mcast_enabled &&
1147 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1148 !orig_initialized)) {
1149 atomic_inc(&bat_priv->mcast.num_disabled);
1150 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1153 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1155 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1156 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1157 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1159 orig->mcast_flags = mcast_flags;
1160 spin_unlock_bh(&orig->mcast_handler_lock);
1164 * batadv_mcast_init - initialize the multicast optimizations structures
1165 * @bat_priv: the bat priv with all the soft interface information
1167 void batadv_mcast_init(struct batadv_priv *bat_priv)
1169 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1170 NULL, BATADV_TVLV_MCAST, 2,
1171 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1173 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1174 batadv_mcast_start_timer(bat_priv);
1177 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1179 * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1180 * @bat_priv: the bat priv with all the soft interface information
1181 * @seq: debugfs table seq_file struct
1183 * Prints our own multicast flags including a more specific reason why
1184 * they are set, that is prints the bridge and querier state too, to
1185 * the debugfs table specified via @seq.
1187 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1188 struct seq_file *seq)
1190 u8 flags = bat_priv->mcast.flags;
1191 char querier4, querier6, shadowing4, shadowing6;
1192 bool bridged = bat_priv->mcast.bridged;
1195 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1196 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1197 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1198 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1206 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1207 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1208 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1209 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1210 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1211 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1212 querier4, querier6);
1213 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1214 shadowing4, shadowing6);
1215 seq_puts(seq, "-------------------------------------------\n");
1216 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1220 * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1221 * @seq: seq file to print on
1224 * This prints a table of (primary) originators and their according
1225 * multicast flags, including (in the header) our own.
1229 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1231 struct net_device *net_dev = (struct net_device *)seq->private;
1232 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1233 struct batadv_hard_iface *primary_if;
1234 struct batadv_hashtable *hash = bat_priv->orig_hash;
1235 struct batadv_orig_node *orig_node;
1236 struct hlist_head *head;
1240 primary_if = batadv_seq_print_text_primary_if_get(seq);
1244 batadv_mcast_flags_print_header(bat_priv, seq);
1246 for (i = 0; i < hash->size; i++) {
1247 head = &hash->table[i];
1250 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1251 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1252 &orig_node->capa_initialized))
1255 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1256 &orig_node->capabilities)) {
1257 seq_printf(seq, "%pM -\n", orig_node->orig);
1261 flags = orig_node->mcast_flags;
1263 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1264 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1266 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1268 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1274 batadv_hardif_put(primary_if);
1281 * batadv_mcast_free - free the multicast optimizations structures
1282 * @bat_priv: the bat priv with all the soft interface information
1284 void batadv_mcast_free(struct batadv_priv *bat_priv)
1286 cancel_delayed_work_sync(&bat_priv->mcast.work);
1288 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1289 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1291 /* safely calling outside of worker, as worker was canceled above */
1292 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1296 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1297 * @bat_priv: the bat priv with all the soft interface information
1298 * @skb: the multicast packet to send
1299 * @vid: the vlan identifier
1300 * @orig_node: the originator to send the packet to
1302 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1304 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1305 struct sk_buff *skb,
1307 struct batadv_orig_node *orig_node)
1309 /* Avoid sending multicast-in-unicast packets to other BLA
1310 * gateways - they already got the frame from the LAN side
1311 * we share with them.
1312 * TODO: Refactor to take BLA into account earlier, to avoid
1313 * reducing the mcast_fanout count.
1315 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1317 return NET_XMIT_SUCCESS;
1320 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1325 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1326 * @orig: the originator which is going to get purged
1328 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1330 struct batadv_priv *bat_priv = orig->bat_priv;
1332 spin_lock_bh(&orig->mcast_handler_lock);
1334 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1335 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1336 atomic_dec(&bat_priv->mcast.num_disabled);
1338 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1339 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1340 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1342 spin_unlock_bh(&orig->mcast_handler_lock);