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/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/stddef.h>
48 #include <linux/string.h>
49 #include <linux/types.h>
50 #include <net/addrconf.h>
51 #include <net/if_inet6.h>
56 #include "translation-table.h"
59 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
60 * @soft_iface: netdev struct of the mesh interface
62 * If the given soft interface has a bridge on top then the refcount
63 * of the according net device is increased.
65 * Return: NULL if no such bridge exists. Otherwise the net device of the
68 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
70 struct net_device *upper = soft_iface;
74 upper = netdev_master_upper_dev_get_rcu(upper);
75 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
85 * batadv_mcast_mla_softif_get - get softif multicast listeners
86 * @dev: the device to collect multicast addresses from
87 * @mcast_list: a list to put found addresses into
89 * Collects multicast addresses of multicast listeners residing
90 * on this kernel on the given soft interface, dev, in
91 * the given mcast_list. In general, multicast listeners provided by
92 * your multicast receiving applications run directly on this node.
94 * If there is a bridge interface on top of dev, collects from that one
95 * instead. Just like with IP addresses and routes, multicast listeners
96 * will(/should) register to the bridge interface instead of an
99 * Return: -ENOMEM on memory allocation error or the number of
100 * items added to the mcast_list otherwise.
102 static int batadv_mcast_mla_softif_get(struct net_device *dev,
103 struct hlist_head *mcast_list)
105 struct net_device *bridge = batadv_mcast_get_bridge(dev);
106 struct netdev_hw_addr *mc_list_entry;
107 struct batadv_hw_addr *new;
110 netif_addr_lock_bh(bridge ? bridge : dev);
111 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
112 new = kmalloc(sizeof(*new), GFP_ATOMIC);
118 ether_addr_copy(new->addr, mc_list_entry->addr);
119 hlist_add_head(&new->list, mcast_list);
122 netif_addr_unlock_bh(bridge ? bridge : dev);
131 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
132 * @mcast_addr: the multicast address to check
133 * @mcast_list: the list with multicast addresses to search in
135 * Return: true if the given address is already in the given list.
136 * Otherwise returns false.
138 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
139 struct hlist_head *mcast_list)
141 struct batadv_hw_addr *mcast_entry;
143 hlist_for_each_entry(mcast_entry, mcast_list, list)
144 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
151 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
152 * @dst: destination to write to - a multicast MAC address
153 * @src: source to read from - a multicast IP address
155 * Converts a given multicast IPv4/IPv6 address from a bridge
156 * to its matching multicast MAC address and copies it into the given
157 * destination buffer.
159 * Caller needs to make sure the destination buffer can hold
160 * at least ETH_ALEN bytes.
162 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
164 if (src->proto == htons(ETH_P_IP))
165 ip_eth_mc_map(src->u.ip4, dst);
166 #if IS_ENABLED(CONFIG_IPV6)
167 else if (src->proto == htons(ETH_P_IPV6))
168 ipv6_eth_mc_map(&src->u.ip6, dst);
175 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
176 * @dev: a bridge slave whose bridge to collect multicast addresses from
177 * @mcast_list: a list to put found addresses into
179 * Collects multicast addresses of multicast listeners residing
180 * on foreign, non-mesh devices which we gave access to our mesh via
181 * a bridge on top of the given soft interface, dev, in the given
184 * Return: -ENOMEM on memory allocation error or the number of
185 * items added to the mcast_list otherwise.
187 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
188 struct hlist_head *mcast_list)
190 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
191 struct br_ip_list *br_ip_entry, *tmp;
192 struct batadv_hw_addr *new;
193 u8 mcast_addr[ETH_ALEN];
196 /* we don't need to detect these devices/listeners, the IGMP/MLD
197 * snooping code of the Linux bridge already does that for us
199 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
203 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
204 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
205 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
208 new = kmalloc(sizeof(*new), GFP_ATOMIC);
214 ether_addr_copy(new->addr, mcast_addr);
215 hlist_add_head(&new->list, mcast_list);
219 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
220 list_del(&br_ip_entry->list);
228 * batadv_mcast_mla_list_free - free a list of multicast addresses
229 * @bat_priv: the bat priv with all the soft interface information
230 * @mcast_list: the list to free
232 * Removes and frees all items in the given mcast_list.
234 static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
235 struct hlist_head *mcast_list)
237 struct batadv_hw_addr *mcast_entry;
238 struct hlist_node *tmp;
240 lockdep_assert_held(&bat_priv->tt.commit_lock);
242 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
243 hlist_del(&mcast_entry->list);
249 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
250 * @bat_priv: the bat priv with all the soft interface information
251 * @mcast_list: a list of addresses which should _not_ be removed
253 * Retracts the announcement of any multicast listener from the
254 * translation table except the ones listed in the given mcast_list.
256 * If mcast_list is NULL then all are retracted.
258 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
259 struct hlist_head *mcast_list)
261 struct batadv_hw_addr *mcast_entry;
262 struct hlist_node *tmp;
264 lockdep_assert_held(&bat_priv->tt.commit_lock);
266 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
269 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
273 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
275 "mcast TT outdated", false);
277 hlist_del(&mcast_entry->list);
283 * batadv_mcast_mla_tt_add - add multicast listener announcements
284 * @bat_priv: the bat priv with all the soft interface information
285 * @mcast_list: a list of addresses which are going to get added
287 * Adds multicast listener announcements from the given mcast_list to the
288 * translation table if they have not been added yet.
290 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
291 struct hlist_head *mcast_list)
293 struct batadv_hw_addr *mcast_entry;
294 struct hlist_node *tmp;
296 lockdep_assert_held(&bat_priv->tt.commit_lock);
301 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
302 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
303 &bat_priv->mcast.mla_list))
306 if (!batadv_tt_local_add(bat_priv->soft_iface,
307 mcast_entry->addr, BATADV_NO_FLAGS,
308 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
311 hlist_del(&mcast_entry->list);
312 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
317 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
318 * @bat_priv: the bat priv with all the soft interface information
320 * Checks whether there is a bridge on top of our soft interface.
322 * Return: true if there is a bridge, false otherwise.
324 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
326 struct net_device *upper = bat_priv->soft_iface;
330 upper = netdev_master_upper_dev_get_rcu(upper);
331 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
338 * batadv_mcast_querier_log - debug output regarding the querier status on link
339 * @bat_priv: the bat priv with all the soft interface information
340 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
341 * @old_state: the previous querier state on our link
342 * @new_state: the new querier state on our link
344 * Outputs debug messages to the logging facility with log level 'mcast'
345 * regarding changes to the querier status on the link which are relevant
346 * to our multicast optimizations.
348 * Usually this is about whether a querier appeared or vanished in
349 * our mesh or whether the querier is in the suboptimal position of being
350 * behind our local bridge segment: Snooping switches will directly
351 * forward listener reports to the querier, therefore batman-adv and
352 * the bridge will potentially not see these listeners - the querier is
353 * potentially shadowing listeners from us then.
355 * This is only interesting for nodes with a bridge on top of their
359 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
360 struct batadv_mcast_querier_state *old_state,
361 struct batadv_mcast_querier_state *new_state)
363 if (!old_state->exists && new_state->exists)
364 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
366 else if (old_state->exists && !new_state->exists)
367 batadv_info(bat_priv->soft_iface,
368 "%s Querier disappeared - multicast optimizations disabled\n",
370 else if (!bat_priv->mcast.bridged && !new_state->exists)
371 batadv_info(bat_priv->soft_iface,
372 "No %s Querier present - multicast optimizations disabled\n",
375 if (new_state->exists) {
376 if ((!old_state->shadowing && new_state->shadowing) ||
377 (!old_state->exists && new_state->shadowing))
378 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
379 "%s Querier is behind our bridged segment: Might shadow listeners\n",
381 else if (old_state->shadowing && !new_state->shadowing)
382 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
383 "%s Querier is not behind our bridged segment\n",
389 * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
390 * @bat_priv: the bat priv with all the soft interface information
391 * @bridged: a flag about whether the soft interface is currently bridged or not
392 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
393 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
395 * If no bridges are ever used on this node, then this function does nothing.
397 * Otherwise this function outputs debug information to the 'mcast' log level
398 * which might be relevant to our multicast optimizations.
400 * More precisely, it outputs information when a bridge interface is added or
401 * removed from a soft interface. And when a bridge is present, it further
402 * outputs information about the querier state which is relevant for the
403 * multicast flags this node is going to set.
406 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
407 struct batadv_mcast_querier_state *querier_ipv4,
408 struct batadv_mcast_querier_state *querier_ipv6)
410 if (!bat_priv->mcast.bridged && bridged)
411 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
412 "Bridge added: Setting Unsnoopables(U)-flag\n");
413 else if (bat_priv->mcast.bridged && !bridged)
414 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
415 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
418 batadv_mcast_querier_log(bat_priv, "IGMP",
419 &bat_priv->mcast.querier_ipv4,
421 batadv_mcast_querier_log(bat_priv, "MLD",
422 &bat_priv->mcast.querier_ipv6,
428 * batadv_mcast_flags_logs - output debug information about mcast flag changes
429 * @bat_priv: the bat priv with all the soft interface information
430 * @flags: flags indicating the new multicast state
432 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
433 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
435 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
437 u8 old_flags = bat_priv->mcast.flags;
438 char str_old_flags[] = "[...]";
440 sprintf(str_old_flags, "[%c%c%c]",
441 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
442 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
443 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
445 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
446 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
447 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
448 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
449 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
450 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
454 * batadv_mcast_mla_tvlv_update - update multicast tvlv
455 * @bat_priv: the bat priv with all the soft interface information
457 * Updates the own multicast tvlv with our current multicast related settings,
458 * capabilities and inabilities.
460 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
463 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
465 struct batadv_tvlv_mcast_data mcast_data;
466 struct batadv_mcast_querier_state querier4 = {false, false};
467 struct batadv_mcast_querier_state querier6 = {false, false};
468 struct net_device *dev = bat_priv->soft_iface;
471 mcast_data.flags = BATADV_NO_FLAGS;
472 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
474 bridged = batadv_mcast_has_bridge(bat_priv);
478 #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
479 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
482 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
483 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
485 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
486 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
488 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
490 /* 1) If no querier exists at all, then multicast listeners on
491 * our local TT clients behind the bridge will keep silent.
492 * 2) If the selected querier is on one of our local TT clients,
493 * behind the bridge, then this querier might shadow multicast
494 * listeners on our local TT clients, behind this bridge.
496 * In both cases, we will signalize other batman nodes that
497 * we need all multicast traffic of the according protocol.
499 if (!querier4.exists || querier4.shadowing)
500 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
502 if (!querier6.exists || querier6.shadowing)
503 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
506 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
508 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
509 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
511 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
512 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
514 bat_priv->mcast.bridged = bridged;
516 if (!bat_priv->mcast.enabled ||
517 mcast_data.flags != bat_priv->mcast.flags) {
518 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
519 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
520 &mcast_data, sizeof(mcast_data));
521 bat_priv->mcast.flags = mcast_data.flags;
522 bat_priv->mcast.enabled = true;
525 return !(mcast_data.flags &
526 (BATADV_MCAST_WANT_ALL_IPV4 + BATADV_MCAST_WANT_ALL_IPV6));
530 * batadv_mcast_mla_update - update the own MLAs
531 * @bat_priv: the bat priv with all the soft interface information
533 * Updates the own multicast listener announcements in the translation
534 * table as well as the own, announced multicast tvlv container.
536 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
538 struct net_device *soft_iface = bat_priv->soft_iface;
539 struct hlist_head mcast_list = HLIST_HEAD_INIT;
542 if (!batadv_mcast_mla_tvlv_update(bat_priv))
545 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
549 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
554 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
555 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
558 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
562 * batadv_mcast_is_report_ipv4 - check for IGMP reports
563 * @skb: the ethernet frame destined for the mesh
565 * This call might reallocate skb data.
567 * Checks whether the given frame is a valid IGMP report.
569 * Return: If so then true, otherwise false.
571 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
573 if (ip_mc_check_igmp(skb, NULL) < 0)
576 switch (igmp_hdr(skb)->type) {
577 case IGMP_HOST_MEMBERSHIP_REPORT:
578 case IGMPV2_HOST_MEMBERSHIP_REPORT:
579 case IGMPV3_HOST_MEMBERSHIP_REPORT:
587 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
588 * @bat_priv: the bat priv with all the soft interface information
589 * @skb: the IPv4 packet to check
590 * @is_unsnoopable: stores whether the destination is snoopable
592 * Checks whether the given IPv4 packet has the potential to be forwarded with a
593 * mode more optimal than classic flooding.
595 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
596 * allocation failure.
598 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
600 bool *is_unsnoopable)
604 /* We might fail due to out-of-memory -> drop it */
605 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
608 if (batadv_mcast_is_report_ipv4(skb))
613 /* TODO: Implement Multicast Router Discovery (RFC4286),
614 * then allow scope > link local, too
616 if (!ipv4_is_local_multicast(iphdr->daddr))
619 /* link-local multicast listeners behind a bridge are
620 * not snoopable (see RFC4541, section 2.1.2.2)
622 *is_unsnoopable = true;
627 #if IS_ENABLED(CONFIG_IPV6)
629 * batadv_mcast_is_report_ipv6 - check for MLD reports
630 * @skb: the ethernet frame destined for the mesh
632 * This call might reallocate skb data.
634 * Checks whether the given frame is a valid MLD report.
636 * Return: If so then true, otherwise false.
638 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
640 if (ipv6_mc_check_mld(skb, NULL) < 0)
643 switch (icmp6_hdr(skb)->icmp6_type) {
644 case ICMPV6_MGM_REPORT:
645 case ICMPV6_MLD2_REPORT:
653 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
654 * @bat_priv: the bat priv with all the soft interface information
655 * @skb: the IPv6 packet to check
656 * @is_unsnoopable: stores whether the destination is snoopable
658 * Checks whether the given IPv6 packet has the potential to be forwarded with a
659 * mode more optimal than classic flooding.
661 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
663 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
665 bool *is_unsnoopable)
667 struct ipv6hdr *ip6hdr;
669 /* We might fail due to out-of-memory -> drop it */
670 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
673 if (batadv_mcast_is_report_ipv6(skb))
676 ip6hdr = ipv6_hdr(skb);
678 /* TODO: Implement Multicast Router Discovery (RFC4286),
679 * then allow scope > link local, too
681 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
684 /* link-local-all-nodes multicast listeners behind a bridge are
685 * not snoopable (see RFC4541, section 3, paragraph 3)
687 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
688 *is_unsnoopable = true;
695 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
696 * @bat_priv: the bat priv with all the soft interface information
697 * @skb: the multicast frame to check
698 * @is_unsnoopable: stores whether the destination is snoopable
700 * Checks whether the given multicast ethernet frame has the potential to be
701 * forwarded with a 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(struct batadv_priv *bat_priv,
707 bool *is_unsnoopable)
709 struct ethhdr *ethhdr = eth_hdr(skb);
711 if (!atomic_read(&bat_priv->multicast_mode))
714 if (atomic_read(&bat_priv->mcast.num_disabled))
717 switch (ntohs(ethhdr->h_proto)) {
719 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
721 #if IS_ENABLED(CONFIG_IPV6)
723 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
732 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
734 * @bat_priv: the bat priv with all the soft interface information
735 * @ethhdr: ethernet header of a packet
737 * Return: the number of nodes which want all IPv4 multicast traffic if the
738 * given ethhdr is from an IPv4 packet or the number of nodes which want all
739 * IPv6 traffic if it matches an IPv6 packet.
741 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
742 struct ethhdr *ethhdr)
744 switch (ntohs(ethhdr->h_proto)) {
746 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
748 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
750 /* we shouldn't be here... */
756 * batadv_mcast_forw_tt_node_get - get a multicast tt node
757 * @bat_priv: the bat priv with all the soft interface information
758 * @ethhdr: the ether header containing the multicast destination
760 * Return: an orig_node matching the multicast address provided by ethhdr
761 * via a translation table lookup. This increases the returned nodes refcount.
763 static struct batadv_orig_node *
764 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
765 struct ethhdr *ethhdr)
767 return batadv_transtable_search(bat_priv, ethhdr->h_source,
768 ethhdr->h_dest, BATADV_NO_FLAGS);
772 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
773 * @bat_priv: the bat priv with all the soft interface information
775 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
776 * increases its refcount.
778 static struct batadv_orig_node *
779 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
781 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
784 hlist_for_each_entry_rcu(tmp_orig_node,
785 &bat_priv->mcast.want_all_ipv4_list,
786 mcast_want_all_ipv4_node) {
787 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
790 orig_node = tmp_orig_node;
799 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
800 * @bat_priv: the bat priv with all the soft interface information
802 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
803 * and increases its refcount.
805 static struct batadv_orig_node *
806 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
808 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
811 hlist_for_each_entry_rcu(tmp_orig_node,
812 &bat_priv->mcast.want_all_ipv6_list,
813 mcast_want_all_ipv6_node) {
814 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
817 orig_node = tmp_orig_node;
826 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
827 * @bat_priv: the bat priv with all the soft interface information
828 * @ethhdr: an ethernet header to determine the protocol family from
830 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
831 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
832 * increases its refcount.
834 static struct batadv_orig_node *
835 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
836 struct ethhdr *ethhdr)
838 switch (ntohs(ethhdr->h_proto)) {
840 return batadv_mcast_forw_ipv4_node_get(bat_priv);
842 return batadv_mcast_forw_ipv6_node_get(bat_priv);
844 /* we shouldn't be here... */
850 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
851 * @bat_priv: the bat priv with all the soft interface information
853 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
854 * set and increases its refcount.
856 static struct batadv_orig_node *
857 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
859 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
862 hlist_for_each_entry_rcu(tmp_orig_node,
863 &bat_priv->mcast.want_all_unsnoopables_list,
864 mcast_want_all_unsnoopables_node) {
865 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
868 orig_node = tmp_orig_node;
877 * batadv_mcast_forw_mode - check on how to forward a multicast packet
878 * @bat_priv: the bat priv with all the soft interface information
879 * @skb: The multicast packet to check
880 * @orig: an originator to be set to forward the skb to
882 * Return: the forwarding mode as enum batadv_forw_mode and in case of
883 * BATADV_FORW_SINGLE set the orig to the single originator the skb
884 * should be forwarded to.
886 enum batadv_forw_mode
887 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
888 struct batadv_orig_node **orig)
890 int ret, tt_count, ip_count, unsnoop_count, total_count;
891 bool is_unsnoopable = false;
892 struct ethhdr *ethhdr;
894 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
896 return BATADV_FORW_NONE;
898 return BATADV_FORW_ALL;
900 ethhdr = eth_hdr(skb);
902 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
904 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
905 unsnoop_count = !is_unsnoopable ? 0 :
906 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
908 total_count = tt_count + ip_count + unsnoop_count;
910 switch (total_count) {
913 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
915 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
916 else if (unsnoop_count)
917 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
920 return BATADV_FORW_SINGLE;
924 return BATADV_FORW_NONE;
926 return BATADV_FORW_ALL;
931 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
932 * @bat_priv: the bat priv with all the soft interface information
933 * @orig: the orig_node which multicast state might have changed of
934 * @mcast_flags: flags indicating the new multicast state
936 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
937 * orig, has toggled then this method updates counter and list accordingly.
939 * Caller needs to hold orig->mcast_handler_lock.
941 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
942 struct batadv_orig_node *orig,
945 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
946 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
948 lockdep_assert_held(&orig->mcast_handler_lock);
950 /* switched from flag unset to set */
951 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
952 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
953 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
955 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
956 /* flag checks above + mcast_handler_lock prevents this */
957 WARN_ON(!hlist_unhashed(node));
959 hlist_add_head_rcu(node, head);
960 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
961 /* switched from flag set to unset */
962 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
963 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
964 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
966 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
967 /* flag checks above + mcast_handler_lock prevents this */
968 WARN_ON(hlist_unhashed(node));
970 hlist_del_init_rcu(node);
971 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
976 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
977 * @bat_priv: the bat priv with all the soft interface information
978 * @orig: the orig_node which multicast state might have changed of
979 * @mcast_flags: flags indicating the new multicast state
981 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
982 * toggled then this method updates counter and list accordingly.
984 * Caller needs to hold orig->mcast_handler_lock.
986 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
987 struct batadv_orig_node *orig,
990 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
991 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
993 lockdep_assert_held(&orig->mcast_handler_lock);
995 /* switched from flag unset to set */
996 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
997 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
998 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1000 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1001 /* flag checks above + mcast_handler_lock prevents this */
1002 WARN_ON(!hlist_unhashed(node));
1004 hlist_add_head_rcu(node, head);
1005 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1006 /* switched from flag set to unset */
1007 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1008 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1009 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1011 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1012 /* flag checks above + mcast_handler_lock prevents this */
1013 WARN_ON(hlist_unhashed(node));
1015 hlist_del_init_rcu(node);
1016 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1021 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1022 * @bat_priv: the bat priv with all the soft interface information
1023 * @orig: the orig_node which multicast state might have changed of
1024 * @mcast_flags: flags indicating the new multicast state
1026 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1027 * toggled then this method updates counter and list accordingly.
1029 * Caller needs to hold orig->mcast_handler_lock.
1031 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1032 struct batadv_orig_node *orig,
1035 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1036 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1038 lockdep_assert_held(&orig->mcast_handler_lock);
1040 /* switched from flag unset to set */
1041 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1042 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1043 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1045 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1046 /* flag checks above + mcast_handler_lock prevents this */
1047 WARN_ON(!hlist_unhashed(node));
1049 hlist_add_head_rcu(node, head);
1050 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1051 /* switched from flag set to unset */
1052 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1053 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1054 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1056 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1057 /* flag checks above + mcast_handler_lock prevents this */
1058 WARN_ON(hlist_unhashed(node));
1060 hlist_del_init_rcu(node);
1061 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1066 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
1067 * @bat_priv: the bat priv with all the soft interface information
1068 * @orig: the orig_node of the ogm
1069 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1070 * @tvlv_value: tvlv buffer containing the multicast data
1071 * @tvlv_value_len: tvlv buffer length
1073 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1074 struct batadv_orig_node *orig,
1079 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1080 u8 mcast_flags = BATADV_NO_FLAGS;
1081 bool orig_initialized;
1083 if (orig_mcast_enabled && tvlv_value &&
1084 (tvlv_value_len >= sizeof(mcast_flags)))
1085 mcast_flags = *(u8 *)tvlv_value;
1087 spin_lock_bh(&orig->mcast_handler_lock);
1088 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1089 &orig->capa_initialized);
1091 /* If mcast support is turned on decrease the disabled mcast node
1092 * counter only if we had increased it for this node before. If this
1093 * is a completely new orig_node no need to decrease the counter.
1095 if (orig_mcast_enabled &&
1096 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1097 if (orig_initialized)
1098 atomic_dec(&bat_priv->mcast.num_disabled);
1099 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1100 /* If mcast support is being switched off or if this is an initial
1101 * OGM without mcast support then increase the disabled mcast
1104 } else if (!orig_mcast_enabled &&
1105 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1106 !orig_initialized)) {
1107 atomic_inc(&bat_priv->mcast.num_disabled);
1108 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1111 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1113 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1114 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1115 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1117 orig->mcast_flags = mcast_flags;
1118 spin_unlock_bh(&orig->mcast_handler_lock);
1122 * batadv_mcast_init - initialize the multicast optimizations structures
1123 * @bat_priv: the bat priv with all the soft interface information
1125 void batadv_mcast_init(struct batadv_priv *bat_priv)
1127 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1128 NULL, BATADV_TVLV_MCAST, 2,
1129 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1133 * batadv_mcast_free - free the multicast optimizations structures
1134 * @bat_priv: the bat priv with all the soft interface information
1136 void batadv_mcast_free(struct batadv_priv *bat_priv)
1138 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1139 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1141 spin_lock_bh(&bat_priv->tt.commit_lock);
1142 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1143 spin_unlock_bh(&bat_priv->tt.commit_lock);
1147 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1148 * @orig: the originator which is going to get purged
1150 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1152 struct batadv_priv *bat_priv = orig->bat_priv;
1154 spin_lock_bh(&orig->mcast_handler_lock);
1156 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1157 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1158 atomic_dec(&bat_priv->mcast.num_disabled);
1160 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1161 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1162 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1164 spin_unlock_bh(&orig->mcast_handler_lock);