1 /* Copyright (C) 2014-2016 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/kernel.h>
37 #include <linux/kref.h>
38 #include <linux/list.h>
39 #include <linux/lockdep.h>
40 #include <linux/netdevice.h>
41 #include <linux/printk.h>
42 #include <linux/rculist.h>
43 #include <linux/rcupdate.h>
44 #include <linux/seq_file.h>
45 #include <linux/skbuff.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/stddef.h>
49 #include <linux/string.h>
50 #include <linux/types.h>
51 #include <net/addrconf.h>
52 #include <net/if_inet6.h>
56 #include "bridge_loop_avoidance.h"
57 #include "hard-interface.h"
62 #include "translation-table.h"
66 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
67 * @soft_iface: netdev struct of the mesh interface
69 * If the given soft interface has a bridge on top then the refcount
70 * of the according net device is increased.
72 * Return: NULL if no such bridge exists. Otherwise the net device of the
75 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
77 struct net_device *upper = soft_iface;
81 upper = netdev_master_upper_dev_get_rcu(upper);
82 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
92 * batadv_mcast_mla_softif_get - get softif multicast listeners
93 * @dev: the device to collect multicast addresses from
94 * @mcast_list: a list to put found addresses into
96 * Collects multicast addresses of multicast listeners residing
97 * on this kernel on the given soft interface, dev, in
98 * the given mcast_list. In general, multicast listeners provided by
99 * your multicast receiving applications run directly on this node.
101 * If there is a bridge interface on top of dev, collects from that one
102 * instead. Just like with IP addresses and routes, multicast listeners
103 * will(/should) register to the bridge interface instead of an
106 * Return: -ENOMEM on memory allocation error or the number of
107 * items added to the mcast_list otherwise.
109 static int batadv_mcast_mla_softif_get(struct net_device *dev,
110 struct hlist_head *mcast_list)
112 struct net_device *bridge = batadv_mcast_get_bridge(dev);
113 struct netdev_hw_addr *mc_list_entry;
114 struct batadv_hw_addr *new;
117 netif_addr_lock_bh(bridge ? bridge : dev);
118 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
119 new = kmalloc(sizeof(*new), GFP_ATOMIC);
125 ether_addr_copy(new->addr, mc_list_entry->addr);
126 hlist_add_head(&new->list, mcast_list);
129 netif_addr_unlock_bh(bridge ? bridge : dev);
138 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
139 * @mcast_addr: the multicast address to check
140 * @mcast_list: the list with multicast addresses to search in
142 * Return: true if the given address is already in the given list.
143 * Otherwise returns false.
145 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
146 struct hlist_head *mcast_list)
148 struct batadv_hw_addr *mcast_entry;
150 hlist_for_each_entry(mcast_entry, mcast_list, list)
151 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
158 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
159 * @dst: destination to write to - a multicast MAC address
160 * @src: source to read from - a multicast IP address
162 * Converts a given multicast IPv4/IPv6 address from a bridge
163 * to its matching multicast MAC address and copies it into the given
164 * destination buffer.
166 * Caller needs to make sure the destination buffer can hold
167 * at least ETH_ALEN bytes.
169 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
171 if (src->proto == htons(ETH_P_IP))
172 ip_eth_mc_map(src->u.ip4, dst);
173 #if IS_ENABLED(CONFIG_IPV6)
174 else if (src->proto == htons(ETH_P_IPV6))
175 ipv6_eth_mc_map(&src->u.ip6, dst);
182 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
183 * @dev: a bridge slave whose bridge to collect multicast addresses from
184 * @mcast_list: a list to put found addresses into
186 * Collects multicast addresses of multicast listeners residing
187 * on foreign, non-mesh devices which we gave access to our mesh via
188 * a bridge on top of the given soft interface, dev, in the given
191 * Return: -ENOMEM on memory allocation error or the number of
192 * items added to the mcast_list otherwise.
194 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
195 struct hlist_head *mcast_list)
197 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
198 struct br_ip_list *br_ip_entry, *tmp;
199 struct batadv_hw_addr *new;
200 u8 mcast_addr[ETH_ALEN];
203 /* we don't need to detect these devices/listeners, the IGMP/MLD
204 * snooping code of the Linux bridge already does that for us
206 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
210 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
211 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
212 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
215 new = kmalloc(sizeof(*new), GFP_ATOMIC);
221 ether_addr_copy(new->addr, mcast_addr);
222 hlist_add_head(&new->list, mcast_list);
226 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
227 list_del(&br_ip_entry->list);
235 * batadv_mcast_mla_list_free - free a list of multicast addresses
236 * @bat_priv: the bat priv with all the soft interface information
237 * @mcast_list: the list to free
239 * Removes and frees all items in the given mcast_list.
241 static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
242 struct hlist_head *mcast_list)
244 struct batadv_hw_addr *mcast_entry;
245 struct hlist_node *tmp;
247 lockdep_assert_held(&bat_priv->tt.commit_lock);
249 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
250 hlist_del(&mcast_entry->list);
256 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
257 * @bat_priv: the bat priv with all the soft interface information
258 * @mcast_list: a list of addresses which should _not_ be removed
260 * Retracts the announcement of any multicast listener from the
261 * translation table except the ones listed in the given mcast_list.
263 * If mcast_list is NULL then all are retracted.
265 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
266 struct hlist_head *mcast_list)
268 struct batadv_hw_addr *mcast_entry;
269 struct hlist_node *tmp;
271 lockdep_assert_held(&bat_priv->tt.commit_lock);
273 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
276 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
280 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
282 "mcast TT outdated", false);
284 hlist_del(&mcast_entry->list);
290 * batadv_mcast_mla_tt_add - add multicast listener announcements
291 * @bat_priv: the bat priv with all the soft interface information
292 * @mcast_list: a list of addresses which are going to get added
294 * Adds multicast listener announcements from the given mcast_list to the
295 * translation table if they have not been added yet.
297 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
298 struct hlist_head *mcast_list)
300 struct batadv_hw_addr *mcast_entry;
301 struct hlist_node *tmp;
303 lockdep_assert_held(&bat_priv->tt.commit_lock);
308 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
309 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
310 &bat_priv->mcast.mla_list))
313 if (!batadv_tt_local_add(bat_priv->soft_iface,
314 mcast_entry->addr, BATADV_NO_FLAGS,
315 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
318 hlist_del(&mcast_entry->list);
319 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
324 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
325 * @bat_priv: the bat priv with all the soft interface information
327 * Checks whether there is a bridge on top of our soft interface.
329 * Return: true if there is a bridge, false otherwise.
331 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
333 struct net_device *upper = bat_priv->soft_iface;
337 upper = netdev_master_upper_dev_get_rcu(upper);
338 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
345 * batadv_mcast_querier_log - debug output regarding the querier status on link
346 * @bat_priv: the bat priv with all the soft interface information
347 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
348 * @old_state: the previous querier state on our link
349 * @new_state: the new querier state on our link
351 * Outputs debug messages to the logging facility with log level 'mcast'
352 * regarding changes to the querier status on the link which are relevant
353 * to our multicast optimizations.
355 * Usually this is about whether a querier appeared or vanished in
356 * our mesh or whether the querier is in the suboptimal position of being
357 * behind our local bridge segment: Snooping switches will directly
358 * forward listener reports to the querier, therefore batman-adv and
359 * the bridge will potentially not see these listeners - the querier is
360 * potentially shadowing listeners from us then.
362 * This is only interesting for nodes with a bridge on top of their
366 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
367 struct batadv_mcast_querier_state *old_state,
368 struct batadv_mcast_querier_state *new_state)
370 if (!old_state->exists && new_state->exists)
371 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
373 else if (old_state->exists && !new_state->exists)
374 batadv_info(bat_priv->soft_iface,
375 "%s Querier disappeared - multicast optimizations disabled\n",
377 else if (!bat_priv->mcast.bridged && !new_state->exists)
378 batadv_info(bat_priv->soft_iface,
379 "No %s Querier present - multicast optimizations disabled\n",
382 if (new_state->exists) {
383 if ((!old_state->shadowing && new_state->shadowing) ||
384 (!old_state->exists && new_state->shadowing))
385 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
386 "%s Querier is behind our bridged segment: Might shadow listeners\n",
388 else if (old_state->shadowing && !new_state->shadowing)
389 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
390 "%s Querier is not behind our bridged segment\n",
396 * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
397 * @bat_priv: the bat priv with all the soft interface information
398 * @bridged: a flag about whether the soft interface is currently bridged or not
399 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
400 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
402 * If no bridges are ever used on this node, then this function does nothing.
404 * Otherwise this function outputs debug information to the 'mcast' log level
405 * which might be relevant to our multicast optimizations.
407 * More precisely, it outputs information when a bridge interface is added or
408 * removed from a soft interface. And when a bridge is present, it further
409 * outputs information about the querier state which is relevant for the
410 * multicast flags this node is going to set.
413 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
414 struct batadv_mcast_querier_state *querier_ipv4,
415 struct batadv_mcast_querier_state *querier_ipv6)
417 if (!bat_priv->mcast.bridged && bridged)
418 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
419 "Bridge added: Setting Unsnoopables(U)-flag\n");
420 else if (bat_priv->mcast.bridged && !bridged)
421 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
422 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
425 batadv_mcast_querier_log(bat_priv, "IGMP",
426 &bat_priv->mcast.querier_ipv4,
428 batadv_mcast_querier_log(bat_priv, "MLD",
429 &bat_priv->mcast.querier_ipv6,
435 * batadv_mcast_flags_logs - output debug information about mcast flag changes
436 * @bat_priv: the bat priv with all the soft interface information
437 * @flags: flags indicating the new multicast state
439 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
440 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
442 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
444 u8 old_flags = bat_priv->mcast.flags;
445 char str_old_flags[] = "[...]";
447 sprintf(str_old_flags, "[%c%c%c]",
448 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
449 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
450 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
452 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
453 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
454 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
455 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
456 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
457 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
461 * batadv_mcast_mla_tvlv_update - update multicast tvlv
462 * @bat_priv: the bat priv with all the soft interface information
464 * Updates the own multicast tvlv with our current multicast related settings,
465 * capabilities and inabilities.
467 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
470 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
472 struct batadv_tvlv_mcast_data mcast_data;
473 struct batadv_mcast_querier_state querier4 = {false, false};
474 struct batadv_mcast_querier_state querier6 = {false, false};
475 struct net_device *dev = bat_priv->soft_iface;
478 mcast_data.flags = BATADV_NO_FLAGS;
479 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
481 bridged = batadv_mcast_has_bridge(bat_priv);
485 #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
486 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
489 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
490 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
492 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
493 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
495 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
497 /* 1) If no querier exists at all, then multicast listeners on
498 * our local TT clients behind the bridge will keep silent.
499 * 2) If the selected querier is on one of our local TT clients,
500 * behind the bridge, then this querier might shadow multicast
501 * listeners on our local TT clients, behind this bridge.
503 * In both cases, we will signalize other batman nodes that
504 * we need all multicast traffic of the according protocol.
506 if (!querier4.exists || querier4.shadowing)
507 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
509 if (!querier6.exists || querier6.shadowing)
510 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
513 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
515 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
516 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
518 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
519 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
521 bat_priv->mcast.bridged = bridged;
523 if (!bat_priv->mcast.enabled ||
524 mcast_data.flags != bat_priv->mcast.flags) {
525 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
526 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
527 &mcast_data, sizeof(mcast_data));
528 bat_priv->mcast.flags = mcast_data.flags;
529 bat_priv->mcast.enabled = true;
532 return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
533 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
537 * batadv_mcast_mla_update - update the own MLAs
538 * @bat_priv: the bat priv with all the soft interface information
540 * Updates the own multicast listener announcements in the translation
541 * table as well as the own, announced multicast tvlv container.
543 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
545 struct net_device *soft_iface = bat_priv->soft_iface;
546 struct hlist_head mcast_list = HLIST_HEAD_INIT;
549 if (!batadv_mcast_mla_tvlv_update(bat_priv))
552 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
556 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
561 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
562 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
565 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
569 * batadv_mcast_is_report_ipv4 - check for IGMP reports
570 * @skb: the ethernet frame destined for the mesh
572 * This call might reallocate skb data.
574 * Checks whether the given frame is a valid IGMP report.
576 * Return: If so then true, otherwise false.
578 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
580 if (ip_mc_check_igmp(skb, NULL) < 0)
583 switch (igmp_hdr(skb)->type) {
584 case IGMP_HOST_MEMBERSHIP_REPORT:
585 case IGMPV2_HOST_MEMBERSHIP_REPORT:
586 case IGMPV3_HOST_MEMBERSHIP_REPORT:
594 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
595 * @bat_priv: the bat priv with all the soft interface information
596 * @skb: the IPv4 packet to check
597 * @is_unsnoopable: stores whether the destination is snoopable
599 * Checks whether the given IPv4 packet has the potential to be forwarded with a
600 * mode more optimal than classic flooding.
602 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
603 * allocation failure.
605 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
607 bool *is_unsnoopable)
611 /* We might fail due to out-of-memory -> drop it */
612 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
615 if (batadv_mcast_is_report_ipv4(skb))
620 /* TODO: Implement Multicast Router Discovery (RFC4286),
621 * then allow scope > link local, too
623 if (!ipv4_is_local_multicast(iphdr->daddr))
626 /* link-local multicast listeners behind a bridge are
627 * not snoopable (see RFC4541, section 2.1.2.2)
629 *is_unsnoopable = true;
634 #if IS_ENABLED(CONFIG_IPV6)
636 * batadv_mcast_is_report_ipv6 - check for MLD reports
637 * @skb: the ethernet frame destined for the mesh
639 * This call might reallocate skb data.
641 * Checks whether the given frame is a valid MLD report.
643 * Return: If so then true, otherwise false.
645 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
647 if (ipv6_mc_check_mld(skb, NULL) < 0)
650 switch (icmp6_hdr(skb)->icmp6_type) {
651 case ICMPV6_MGM_REPORT:
652 case ICMPV6_MLD2_REPORT:
660 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
661 * @bat_priv: the bat priv with all the soft interface information
662 * @skb: the IPv6 packet to check
663 * @is_unsnoopable: stores whether the destination is snoopable
665 * Checks whether the given IPv6 packet has the potential to be forwarded with a
666 * mode more optimal than classic flooding.
668 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
670 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
672 bool *is_unsnoopable)
674 struct ipv6hdr *ip6hdr;
676 /* We might fail due to out-of-memory -> drop it */
677 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
680 if (batadv_mcast_is_report_ipv6(skb))
683 ip6hdr = ipv6_hdr(skb);
685 /* TODO: Implement Multicast Router Discovery (RFC4286),
686 * then allow scope > link local, too
688 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
691 /* link-local-all-nodes multicast listeners behind a bridge are
692 * not snoopable (see RFC4541, section 3, paragraph 3)
694 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
695 *is_unsnoopable = true;
702 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
703 * @bat_priv: the bat priv with all the soft interface information
704 * @skb: the multicast frame to check
705 * @is_unsnoopable: stores whether the destination is snoopable
707 * Checks whether the given multicast ethernet frame has the potential to be
708 * forwarded with a mode more optimal than classic flooding.
710 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
712 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
714 bool *is_unsnoopable)
716 struct ethhdr *ethhdr = eth_hdr(skb);
718 if (!atomic_read(&bat_priv->multicast_mode))
721 if (atomic_read(&bat_priv->mcast.num_disabled))
724 switch (ntohs(ethhdr->h_proto)) {
726 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
728 #if IS_ENABLED(CONFIG_IPV6)
730 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
739 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
741 * @bat_priv: the bat priv with all the soft interface information
742 * @ethhdr: ethernet header of a packet
744 * Return: the number of nodes which want all IPv4 multicast traffic if the
745 * given ethhdr is from an IPv4 packet or the number of nodes which want all
746 * IPv6 traffic if it matches an IPv6 packet.
748 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
749 struct ethhdr *ethhdr)
751 switch (ntohs(ethhdr->h_proto)) {
753 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
755 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
757 /* we shouldn't be here... */
763 * batadv_mcast_forw_tt_node_get - get a multicast tt node
764 * @bat_priv: the bat priv with all the soft interface information
765 * @ethhdr: the ether header containing the multicast destination
767 * Return: an orig_node matching the multicast address provided by ethhdr
768 * via a translation table lookup. This increases the returned nodes refcount.
770 static struct batadv_orig_node *
771 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
772 struct ethhdr *ethhdr)
774 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
779 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
780 * @bat_priv: the bat priv with all the soft interface information
782 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
783 * increases its refcount.
785 static struct batadv_orig_node *
786 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
788 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
791 hlist_for_each_entry_rcu(tmp_orig_node,
792 &bat_priv->mcast.want_all_ipv4_list,
793 mcast_want_all_ipv4_node) {
794 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
797 orig_node = tmp_orig_node;
806 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
807 * @bat_priv: the bat priv with all the soft interface information
809 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
810 * and increases its refcount.
812 static struct batadv_orig_node *
813 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
815 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
818 hlist_for_each_entry_rcu(tmp_orig_node,
819 &bat_priv->mcast.want_all_ipv6_list,
820 mcast_want_all_ipv6_node) {
821 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
824 orig_node = tmp_orig_node;
833 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
834 * @bat_priv: the bat priv with all the soft interface information
835 * @ethhdr: an ethernet header to determine the protocol family from
837 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
838 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
839 * increases its refcount.
841 static struct batadv_orig_node *
842 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
843 struct ethhdr *ethhdr)
845 switch (ntohs(ethhdr->h_proto)) {
847 return batadv_mcast_forw_ipv4_node_get(bat_priv);
849 return batadv_mcast_forw_ipv6_node_get(bat_priv);
851 /* we shouldn't be here... */
857 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
858 * @bat_priv: the bat priv with all the soft interface information
860 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
861 * set and increases its refcount.
863 static struct batadv_orig_node *
864 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
866 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
869 hlist_for_each_entry_rcu(tmp_orig_node,
870 &bat_priv->mcast.want_all_unsnoopables_list,
871 mcast_want_all_unsnoopables_node) {
872 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
875 orig_node = tmp_orig_node;
884 * batadv_mcast_forw_mode - check on how to forward a multicast packet
885 * @bat_priv: the bat priv with all the soft interface information
886 * @skb: The multicast packet to check
887 * @orig: an originator to be set to forward the skb to
889 * Return: the forwarding mode as enum batadv_forw_mode and in case of
890 * BATADV_FORW_SINGLE set the orig to the single originator the skb
891 * should be forwarded to.
893 enum batadv_forw_mode
894 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
895 struct batadv_orig_node **orig)
897 int ret, tt_count, ip_count, unsnoop_count, total_count;
898 bool is_unsnoopable = false;
899 struct ethhdr *ethhdr;
901 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
903 return BATADV_FORW_NONE;
905 return BATADV_FORW_ALL;
907 ethhdr = eth_hdr(skb);
909 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
911 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
912 unsnoop_count = !is_unsnoopable ? 0 :
913 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
915 total_count = tt_count + ip_count + unsnoop_count;
917 switch (total_count) {
920 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
922 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
923 else if (unsnoop_count)
924 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
927 return BATADV_FORW_SINGLE;
931 return BATADV_FORW_NONE;
933 return BATADV_FORW_ALL;
938 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
939 * @bat_priv: the bat priv with all the soft interface information
940 * @orig: the orig_node which multicast state might have changed of
941 * @mcast_flags: flags indicating the new multicast state
943 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
944 * orig, has toggled then this method updates counter and list accordingly.
946 * Caller needs to hold orig->mcast_handler_lock.
948 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
949 struct batadv_orig_node *orig,
952 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
953 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
955 lockdep_assert_held(&orig->mcast_handler_lock);
957 /* switched from flag unset to set */
958 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
959 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
960 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
962 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
963 /* flag checks above + mcast_handler_lock prevents this */
964 WARN_ON(!hlist_unhashed(node));
966 hlist_add_head_rcu(node, head);
967 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
968 /* switched from flag set to unset */
969 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
970 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
971 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
973 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
974 /* flag checks above + mcast_handler_lock prevents this */
975 WARN_ON(hlist_unhashed(node));
977 hlist_del_init_rcu(node);
978 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
983 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
984 * @bat_priv: the bat priv with all the soft interface information
985 * @orig: the orig_node which multicast state might have changed of
986 * @mcast_flags: flags indicating the new multicast state
988 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
989 * toggled then this method updates counter and list accordingly.
991 * Caller needs to hold orig->mcast_handler_lock.
993 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
994 struct batadv_orig_node *orig,
997 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
998 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1000 lockdep_assert_held(&orig->mcast_handler_lock);
1002 /* switched from flag unset to set */
1003 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1004 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1005 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1007 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1008 /* flag checks above + mcast_handler_lock prevents this */
1009 WARN_ON(!hlist_unhashed(node));
1011 hlist_add_head_rcu(node, head);
1012 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1013 /* switched from flag set to unset */
1014 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1015 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1016 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1018 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1019 /* flag checks above + mcast_handler_lock prevents this */
1020 WARN_ON(hlist_unhashed(node));
1022 hlist_del_init_rcu(node);
1023 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1028 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1029 * @bat_priv: the bat priv with all the soft interface information
1030 * @orig: the orig_node which multicast state might have changed of
1031 * @mcast_flags: flags indicating the new multicast state
1033 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1034 * toggled then this method updates counter and list accordingly.
1036 * Caller needs to hold orig->mcast_handler_lock.
1038 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1039 struct batadv_orig_node *orig,
1042 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1043 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1045 lockdep_assert_held(&orig->mcast_handler_lock);
1047 /* switched from flag unset to set */
1048 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1049 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1050 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1052 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1053 /* flag checks above + mcast_handler_lock prevents this */
1054 WARN_ON(!hlist_unhashed(node));
1056 hlist_add_head_rcu(node, head);
1057 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1058 /* switched from flag set to unset */
1059 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1060 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1061 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1063 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1064 /* flag checks above + mcast_handler_lock prevents this */
1065 WARN_ON(hlist_unhashed(node));
1067 hlist_del_init_rcu(node);
1068 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1073 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
1074 * @bat_priv: the bat priv with all the soft interface information
1075 * @orig: the orig_node of the ogm
1076 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1077 * @tvlv_value: tvlv buffer containing the multicast data
1078 * @tvlv_value_len: tvlv buffer length
1080 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1081 struct batadv_orig_node *orig,
1086 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1087 u8 mcast_flags = BATADV_NO_FLAGS;
1088 bool orig_initialized;
1090 if (orig_mcast_enabled && tvlv_value &&
1091 (tvlv_value_len >= sizeof(mcast_flags)))
1092 mcast_flags = *(u8 *)tvlv_value;
1094 spin_lock_bh(&orig->mcast_handler_lock);
1095 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1096 &orig->capa_initialized);
1098 /* If mcast support is turned on decrease the disabled mcast node
1099 * counter only if we had increased it for this node before. If this
1100 * is a completely new orig_node no need to decrease the counter.
1102 if (orig_mcast_enabled &&
1103 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1104 if (orig_initialized)
1105 atomic_dec(&bat_priv->mcast.num_disabled);
1106 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1107 /* If mcast support is being switched off or if this is an initial
1108 * OGM without mcast support then increase the disabled mcast
1111 } else if (!orig_mcast_enabled &&
1112 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1113 !orig_initialized)) {
1114 atomic_inc(&bat_priv->mcast.num_disabled);
1115 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1118 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1120 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1121 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1122 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1124 orig->mcast_flags = mcast_flags;
1125 spin_unlock_bh(&orig->mcast_handler_lock);
1129 * batadv_mcast_init - initialize the multicast optimizations structures
1130 * @bat_priv: the bat priv with all the soft interface information
1132 void batadv_mcast_init(struct batadv_priv *bat_priv)
1134 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1135 NULL, BATADV_TVLV_MCAST, 2,
1136 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1139 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1141 * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1142 * @bat_priv: the bat priv with all the soft interface information
1143 * @seq: debugfs table seq_file struct
1145 * Prints our own multicast flags including a more specific reason why
1146 * they are set, that is prints the bridge and querier state too, to
1147 * the debugfs table specified via @seq.
1149 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1150 struct seq_file *seq)
1152 u8 flags = bat_priv->mcast.flags;
1153 char querier4, querier6, shadowing4, shadowing6;
1154 bool bridged = bat_priv->mcast.bridged;
1157 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1158 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1159 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1160 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1168 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1169 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1170 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1171 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1172 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1173 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1174 querier4, querier6);
1175 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1176 shadowing4, shadowing6);
1177 seq_puts(seq, "-------------------------------------------\n");
1178 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1182 * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1183 * @seq: seq file to print on
1186 * This prints a table of (primary) originators and their according
1187 * multicast flags, including (in the header) our own.
1191 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1193 struct net_device *net_dev = (struct net_device *)seq->private;
1194 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1195 struct batadv_hard_iface *primary_if;
1196 struct batadv_hashtable *hash = bat_priv->orig_hash;
1197 struct batadv_orig_node *orig_node;
1198 struct hlist_head *head;
1202 primary_if = batadv_seq_print_text_primary_if_get(seq);
1206 batadv_mcast_flags_print_header(bat_priv, seq);
1208 for (i = 0; i < hash->size; i++) {
1209 head = &hash->table[i];
1212 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1213 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1214 &orig_node->capa_initialized))
1217 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1218 &orig_node->capabilities)) {
1219 seq_printf(seq, "%pM -\n", orig_node->orig);
1223 flags = orig_node->mcast_flags;
1225 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1226 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1228 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1230 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1236 batadv_hardif_put(primary_if);
1243 * batadv_mcast_free - free the multicast optimizations structures
1244 * @bat_priv: the bat priv with all the soft interface information
1246 void batadv_mcast_free(struct batadv_priv *bat_priv)
1248 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1249 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1251 spin_lock_bh(&bat_priv->tt.commit_lock);
1252 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1253 spin_unlock_bh(&bat_priv->tt.commit_lock);
1257 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1258 * @bat_priv: the bat priv with all the soft interface information
1259 * @skb: the multicast packet to send
1260 * @vid: the vlan identifier
1261 * @orig_node: the originator to send the packet to
1263 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1265 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1266 struct sk_buff *skb,
1268 struct batadv_orig_node *orig_node)
1270 /* Avoid sending multicast-in-unicast packets to other BLA
1271 * gateways - they already got the frame from the LAN side
1272 * we share with them.
1273 * TODO: Refactor to take BLA into account earlier, to avoid
1274 * reducing the mcast_fanout count.
1276 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1278 return NET_XMIT_SUCCESS;
1281 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1286 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1287 * @orig: the originator which is going to get purged
1289 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1291 struct batadv_priv *bat_priv = orig->bat_priv;
1293 spin_lock_bh(&orig->mcast_handler_lock);
1295 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1296 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1297 atomic_dec(&bat_priv->mcast.num_disabled);
1299 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1300 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1301 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1303 spin_unlock_bh(&orig->mcast_handler_lock);