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/kref.h>
37 #include <linux/list.h>
38 #include <linux/lockdep.h>
39 #include <linux/netdevice.h>
40 #include <linux/printk.h>
41 #include <linux/rculist.h>
42 #include <linux/rcupdate.h>
43 #include <linux/skbuff.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
46 #include <linux/stddef.h>
47 #include <linux/string.h>
48 #include <linux/types.h>
49 #include <net/addrconf.h>
50 #include <net/if_inet6.h>
55 #include "translation-table.h"
58 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
59 * @soft_iface: netdev struct of the mesh interface
61 * If the given soft interface has a bridge on top then the refcount
62 * of the according net device is increased.
64 * Return: NULL if no such bridge exists. Otherwise the net device of the
67 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
69 struct net_device *upper = soft_iface;
73 upper = netdev_master_upper_dev_get_rcu(upper);
74 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
84 * batadv_mcast_mla_softif_get - get softif multicast listeners
85 * @dev: the device to collect multicast addresses from
86 * @mcast_list: a list to put found addresses into
88 * Collects multicast addresses of multicast listeners residing
89 * on this kernel on the given soft interface, dev, in
90 * the given mcast_list. In general, multicast listeners provided by
91 * your multicast receiving applications run directly on this node.
93 * If there is a bridge interface on top of dev, collects from that one
94 * instead. Just like with IP addresses and routes, multicast listeners
95 * will(/should) register to the bridge interface instead of an
98 * Return: -ENOMEM on memory allocation error or the number of
99 * items added to the mcast_list otherwise.
101 static int batadv_mcast_mla_softif_get(struct net_device *dev,
102 struct hlist_head *mcast_list)
104 struct net_device *bridge = batadv_mcast_get_bridge(dev);
105 struct netdev_hw_addr *mc_list_entry;
106 struct batadv_hw_addr *new;
109 netif_addr_lock_bh(bridge ? bridge : dev);
110 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
111 new = kmalloc(sizeof(*new), GFP_ATOMIC);
117 ether_addr_copy(new->addr, mc_list_entry->addr);
118 hlist_add_head(&new->list, mcast_list);
121 netif_addr_unlock_bh(bridge ? bridge : dev);
130 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
131 * @mcast_addr: the multicast address to check
132 * @mcast_list: the list with multicast addresses to search in
134 * Return: true if the given address is already in the given list.
135 * Otherwise returns false.
137 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
138 struct hlist_head *mcast_list)
140 struct batadv_hw_addr *mcast_entry;
142 hlist_for_each_entry(mcast_entry, mcast_list, list)
143 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
150 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
151 * @dst: destination to write to - a multicast MAC address
152 * @src: source to read from - a multicast IP address
154 * Converts a given multicast IPv4/IPv6 address from a bridge
155 * to its matching multicast MAC address and copies it into the given
156 * destination buffer.
158 * Caller needs to make sure the destination buffer can hold
159 * at least ETH_ALEN bytes.
161 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
163 if (src->proto == htons(ETH_P_IP))
164 ip_eth_mc_map(src->u.ip4, dst);
165 #if IS_ENABLED(CONFIG_IPV6)
166 else if (src->proto == htons(ETH_P_IPV6))
167 ipv6_eth_mc_map(&src->u.ip6, dst);
174 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
175 * @dev: a bridge slave whose bridge to collect multicast addresses from
176 * @mcast_list: a list to put found addresses into
178 * Collects multicast addresses of multicast listeners residing
179 * on foreign, non-mesh devices which we gave access to our mesh via
180 * a bridge on top of the given soft interface, dev, in the given
183 * Return: -ENOMEM on memory allocation error or the number of
184 * items added to the mcast_list otherwise.
186 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
187 struct hlist_head *mcast_list)
189 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
190 struct br_ip_list *br_ip_entry, *tmp;
191 struct batadv_hw_addr *new;
192 u8 mcast_addr[ETH_ALEN];
195 /* we don't need to detect these devices/listeners, the IGMP/MLD
196 * snooping code of the Linux bridge already does that for us
198 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
202 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
203 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
204 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
207 new = kmalloc(sizeof(*new), GFP_ATOMIC);
213 ether_addr_copy(new->addr, mcast_addr);
214 hlist_add_head(&new->list, mcast_list);
218 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
219 list_del(&br_ip_entry->list);
227 * batadv_mcast_mla_list_free - free a list of multicast addresses
228 * @bat_priv: the bat priv with all the soft interface information
229 * @mcast_list: the list to free
231 * Removes and frees all items in the given mcast_list.
233 static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
234 struct hlist_head *mcast_list)
236 struct batadv_hw_addr *mcast_entry;
237 struct hlist_node *tmp;
239 lockdep_assert_held(&bat_priv->tt.commit_lock);
241 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
242 hlist_del(&mcast_entry->list);
248 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
249 * @bat_priv: the bat priv with all the soft interface information
250 * @mcast_list: a list of addresses which should _not_ be removed
252 * Retracts the announcement of any multicast listener from the
253 * translation table except the ones listed in the given mcast_list.
255 * If mcast_list is NULL then all are retracted.
257 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
258 struct hlist_head *mcast_list)
260 struct batadv_hw_addr *mcast_entry;
261 struct hlist_node *tmp;
263 lockdep_assert_held(&bat_priv->tt.commit_lock);
265 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
268 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
272 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
274 "mcast TT outdated", false);
276 hlist_del(&mcast_entry->list);
282 * batadv_mcast_mla_tt_add - add multicast listener announcements
283 * @bat_priv: the bat priv with all the soft interface information
284 * @mcast_list: a list of addresses which are going to get added
286 * Adds multicast listener announcements from the given mcast_list to the
287 * translation table if they have not been added yet.
289 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
290 struct hlist_head *mcast_list)
292 struct batadv_hw_addr *mcast_entry;
293 struct hlist_node *tmp;
295 lockdep_assert_held(&bat_priv->tt.commit_lock);
300 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
301 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
302 &bat_priv->mcast.mla_list))
305 if (!batadv_tt_local_add(bat_priv->soft_iface,
306 mcast_entry->addr, BATADV_NO_FLAGS,
307 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
310 hlist_del(&mcast_entry->list);
311 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
316 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
317 * @bat_priv: the bat priv with all the soft interface information
319 * Checks whether there is a bridge on top of our soft interface.
321 * Return: true if there is a bridge, false otherwise.
323 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
325 struct net_device *upper = bat_priv->soft_iface;
329 upper = netdev_master_upper_dev_get_rcu(upper);
330 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
337 * batadv_mcast_mla_tvlv_update - update multicast tvlv
338 * @bat_priv: the bat priv with all the soft interface information
340 * Updates the own multicast tvlv with our current multicast related settings,
341 * capabilities and inabilities.
343 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
346 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
348 struct batadv_tvlv_mcast_data mcast_data;
349 struct batadv_mcast_querier_state querier4 = {false, false};
350 struct batadv_mcast_querier_state querier6 = {false, false};
351 struct net_device *dev = bat_priv->soft_iface;
353 mcast_data.flags = BATADV_NO_FLAGS;
354 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
356 bat_priv->mcast.bridged = batadv_mcast_has_bridge(bat_priv);
357 if (!bat_priv->mcast.bridged)
360 #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
361 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
364 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
365 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
367 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
368 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
370 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
372 /* 1) If no querier exists at all, then multicast listeners on
373 * our local TT clients behind the bridge will keep silent.
374 * 2) If the selected querier is on one of our local TT clients,
375 * behind the bridge, then this querier might shadow multicast
376 * listeners on our local TT clients, behind this bridge.
378 * In both cases, we will signalize other batman nodes that
379 * we need all multicast traffic of the according protocol.
381 if (!querier4.exists || querier4.shadowing)
382 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
384 if (!querier6.exists || querier6.shadowing)
385 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
388 if (!bat_priv->mcast.enabled ||
389 mcast_data.flags != bat_priv->mcast.flags) {
390 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
391 &mcast_data, sizeof(mcast_data));
392 bat_priv->mcast.flags = mcast_data.flags;
393 bat_priv->mcast.enabled = true;
396 return !(mcast_data.flags &
397 (BATADV_MCAST_WANT_ALL_IPV4 + BATADV_MCAST_WANT_ALL_IPV6));
401 * batadv_mcast_mla_update - update the own MLAs
402 * @bat_priv: the bat priv with all the soft interface information
404 * Updates the own multicast listener announcements in the translation
405 * table as well as the own, announced multicast tvlv container.
407 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
409 struct net_device *soft_iface = bat_priv->soft_iface;
410 struct hlist_head mcast_list = HLIST_HEAD_INIT;
413 if (!batadv_mcast_mla_tvlv_update(bat_priv))
416 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
420 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
425 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
426 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
429 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
433 * batadv_mcast_is_report_ipv4 - check for IGMP reports
434 * @skb: the ethernet frame destined for the mesh
436 * This call might reallocate skb data.
438 * Checks whether the given frame is a valid IGMP report.
440 * Return: If so then true, otherwise false.
442 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
444 if (ip_mc_check_igmp(skb, NULL) < 0)
447 switch (igmp_hdr(skb)->type) {
448 case IGMP_HOST_MEMBERSHIP_REPORT:
449 case IGMPV2_HOST_MEMBERSHIP_REPORT:
450 case IGMPV3_HOST_MEMBERSHIP_REPORT:
458 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
459 * @bat_priv: the bat priv with all the soft interface information
460 * @skb: the IPv4 packet to check
461 * @is_unsnoopable: stores whether the destination is snoopable
463 * Checks whether the given IPv4 packet has the potential to be forwarded with a
464 * mode more optimal than classic flooding.
466 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
467 * allocation failure.
469 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
471 bool *is_unsnoopable)
475 /* We might fail due to out-of-memory -> drop it */
476 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
479 if (batadv_mcast_is_report_ipv4(skb))
484 /* TODO: Implement Multicast Router Discovery (RFC4286),
485 * then allow scope > link local, too
487 if (!ipv4_is_local_multicast(iphdr->daddr))
490 /* link-local multicast listeners behind a bridge are
491 * not snoopable (see RFC4541, section 2.1.2.2)
493 *is_unsnoopable = true;
498 #if IS_ENABLED(CONFIG_IPV6)
500 * batadv_mcast_is_report_ipv6 - check for MLD reports
501 * @skb: the ethernet frame destined for the mesh
503 * This call might reallocate skb data.
505 * Checks whether the given frame is a valid MLD report.
507 * Return: If so then true, otherwise false.
509 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
511 if (ipv6_mc_check_mld(skb, NULL) < 0)
514 switch (icmp6_hdr(skb)->icmp6_type) {
515 case ICMPV6_MGM_REPORT:
516 case ICMPV6_MLD2_REPORT:
524 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
525 * @bat_priv: the bat priv with all the soft interface information
526 * @skb: the IPv6 packet to check
527 * @is_unsnoopable: stores whether the destination is snoopable
529 * Checks whether the given IPv6 packet has the potential to be forwarded with a
530 * mode more optimal than classic flooding.
532 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
534 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
536 bool *is_unsnoopable)
538 struct ipv6hdr *ip6hdr;
540 /* We might fail due to out-of-memory -> drop it */
541 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
544 if (batadv_mcast_is_report_ipv6(skb))
547 ip6hdr = ipv6_hdr(skb);
549 /* TODO: Implement Multicast Router Discovery (RFC4286),
550 * then allow scope > link local, too
552 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
555 /* link-local-all-nodes multicast listeners behind a bridge are
556 * not snoopable (see RFC4541, section 3, paragraph 3)
558 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
559 *is_unsnoopable = true;
566 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
567 * @bat_priv: the bat priv with all the soft interface information
568 * @skb: the multicast frame to check
569 * @is_unsnoopable: stores whether the destination is snoopable
571 * Checks whether the given multicast ethernet frame has the potential to be
572 * forwarded with a mode more optimal than classic flooding.
574 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
576 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
578 bool *is_unsnoopable)
580 struct ethhdr *ethhdr = eth_hdr(skb);
582 if (!atomic_read(&bat_priv->multicast_mode))
585 if (atomic_read(&bat_priv->mcast.num_disabled))
588 switch (ntohs(ethhdr->h_proto)) {
590 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
592 #if IS_ENABLED(CONFIG_IPV6)
594 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
603 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
605 * @bat_priv: the bat priv with all the soft interface information
606 * @ethhdr: ethernet header of a packet
608 * Return: the number of nodes which want all IPv4 multicast traffic if the
609 * given ethhdr is from an IPv4 packet or the number of nodes which want all
610 * IPv6 traffic if it matches an IPv6 packet.
612 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
613 struct ethhdr *ethhdr)
615 switch (ntohs(ethhdr->h_proto)) {
617 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
619 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
621 /* we shouldn't be here... */
627 * batadv_mcast_forw_tt_node_get - get a multicast tt node
628 * @bat_priv: the bat priv with all the soft interface information
629 * @ethhdr: the ether header containing the multicast destination
631 * Return: an orig_node matching the multicast address provided by ethhdr
632 * via a translation table lookup. This increases the returned nodes refcount.
634 static struct batadv_orig_node *
635 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
636 struct ethhdr *ethhdr)
638 return batadv_transtable_search(bat_priv, ethhdr->h_source,
639 ethhdr->h_dest, BATADV_NO_FLAGS);
643 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
644 * @bat_priv: the bat priv with all the soft interface information
646 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
647 * increases its refcount.
649 static struct batadv_orig_node *
650 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
652 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
655 hlist_for_each_entry_rcu(tmp_orig_node,
656 &bat_priv->mcast.want_all_ipv4_list,
657 mcast_want_all_ipv4_node) {
658 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
661 orig_node = tmp_orig_node;
670 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
671 * @bat_priv: the bat priv with all the soft interface information
673 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
674 * and increases its refcount.
676 static struct batadv_orig_node *
677 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
679 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
682 hlist_for_each_entry_rcu(tmp_orig_node,
683 &bat_priv->mcast.want_all_ipv6_list,
684 mcast_want_all_ipv6_node) {
685 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
688 orig_node = tmp_orig_node;
697 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
698 * @bat_priv: the bat priv with all the soft interface information
699 * @ethhdr: an ethernet header to determine the protocol family from
701 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
702 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
703 * increases its refcount.
705 static struct batadv_orig_node *
706 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
707 struct ethhdr *ethhdr)
709 switch (ntohs(ethhdr->h_proto)) {
711 return batadv_mcast_forw_ipv4_node_get(bat_priv);
713 return batadv_mcast_forw_ipv6_node_get(bat_priv);
715 /* we shouldn't be here... */
721 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
722 * @bat_priv: the bat priv with all the soft interface information
724 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
725 * set and increases its refcount.
727 static struct batadv_orig_node *
728 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
730 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
733 hlist_for_each_entry_rcu(tmp_orig_node,
734 &bat_priv->mcast.want_all_unsnoopables_list,
735 mcast_want_all_unsnoopables_node) {
736 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
739 orig_node = tmp_orig_node;
748 * batadv_mcast_forw_mode - check on how to forward a multicast packet
749 * @bat_priv: the bat priv with all the soft interface information
750 * @skb: The multicast packet to check
751 * @orig: an originator to be set to forward the skb to
753 * Return: the forwarding mode as enum batadv_forw_mode and in case of
754 * BATADV_FORW_SINGLE set the orig to the single originator the skb
755 * should be forwarded to.
757 enum batadv_forw_mode
758 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
759 struct batadv_orig_node **orig)
761 int ret, tt_count, ip_count, unsnoop_count, total_count;
762 bool is_unsnoopable = false;
763 struct ethhdr *ethhdr;
765 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
767 return BATADV_FORW_NONE;
769 return BATADV_FORW_ALL;
771 ethhdr = eth_hdr(skb);
773 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
775 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
776 unsnoop_count = !is_unsnoopable ? 0 :
777 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
779 total_count = tt_count + ip_count + unsnoop_count;
781 switch (total_count) {
784 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
786 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
787 else if (unsnoop_count)
788 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
791 return BATADV_FORW_SINGLE;
795 return BATADV_FORW_NONE;
797 return BATADV_FORW_ALL;
802 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
803 * @bat_priv: the bat priv with all the soft interface information
804 * @orig: the orig_node which multicast state might have changed of
805 * @mcast_flags: flags indicating the new multicast state
807 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
808 * orig, has toggled then this method updates counter and list accordingly.
810 * Caller needs to hold orig->mcast_handler_lock.
812 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
813 struct batadv_orig_node *orig,
816 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
817 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
819 lockdep_assert_held(&orig->mcast_handler_lock);
821 /* switched from flag unset to set */
822 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
823 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
824 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
826 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
827 /* flag checks above + mcast_handler_lock prevents this */
828 WARN_ON(!hlist_unhashed(node));
830 hlist_add_head_rcu(node, head);
831 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
832 /* switched from flag set to unset */
833 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
834 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
835 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
837 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
838 /* flag checks above + mcast_handler_lock prevents this */
839 WARN_ON(hlist_unhashed(node));
841 hlist_del_init_rcu(node);
842 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
847 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
848 * @bat_priv: the bat priv with all the soft interface information
849 * @orig: the orig_node which multicast state might have changed of
850 * @mcast_flags: flags indicating the new multicast state
852 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
853 * toggled then this method updates counter and list accordingly.
855 * Caller needs to hold orig->mcast_handler_lock.
857 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
858 struct batadv_orig_node *orig,
861 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
862 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
864 lockdep_assert_held(&orig->mcast_handler_lock);
866 /* switched from flag unset to set */
867 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
868 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
869 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
871 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
872 /* flag checks above + mcast_handler_lock prevents this */
873 WARN_ON(!hlist_unhashed(node));
875 hlist_add_head_rcu(node, head);
876 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
877 /* switched from flag set to unset */
878 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
879 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
880 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
882 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
883 /* flag checks above + mcast_handler_lock prevents this */
884 WARN_ON(hlist_unhashed(node));
886 hlist_del_init_rcu(node);
887 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
892 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
893 * @bat_priv: the bat priv with all the soft interface information
894 * @orig: the orig_node which multicast state might have changed of
895 * @mcast_flags: flags indicating the new multicast state
897 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
898 * toggled then this method updates counter and list accordingly.
900 * Caller needs to hold orig->mcast_handler_lock.
902 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
903 struct batadv_orig_node *orig,
906 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
907 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
909 lockdep_assert_held(&orig->mcast_handler_lock);
911 /* switched from flag unset to set */
912 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
913 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
914 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
916 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
917 /* flag checks above + mcast_handler_lock prevents this */
918 WARN_ON(!hlist_unhashed(node));
920 hlist_add_head_rcu(node, head);
921 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
922 /* switched from flag set to unset */
923 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
924 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
925 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
927 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
928 /* flag checks above + mcast_handler_lock prevents this */
929 WARN_ON(hlist_unhashed(node));
931 hlist_del_init_rcu(node);
932 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
937 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
938 * @bat_priv: the bat priv with all the soft interface information
939 * @orig: the orig_node of the ogm
940 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
941 * @tvlv_value: tvlv buffer containing the multicast data
942 * @tvlv_value_len: tvlv buffer length
944 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
945 struct batadv_orig_node *orig,
950 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
951 u8 mcast_flags = BATADV_NO_FLAGS;
952 bool orig_initialized;
954 if (orig_mcast_enabled && tvlv_value &&
955 (tvlv_value_len >= sizeof(mcast_flags)))
956 mcast_flags = *(u8 *)tvlv_value;
958 spin_lock_bh(&orig->mcast_handler_lock);
959 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
960 &orig->capa_initialized);
962 /* If mcast support is turned on decrease the disabled mcast node
963 * counter only if we had increased it for this node before. If this
964 * is a completely new orig_node no need to decrease the counter.
966 if (orig_mcast_enabled &&
967 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
968 if (orig_initialized)
969 atomic_dec(&bat_priv->mcast.num_disabled);
970 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
971 /* If mcast support is being switched off or if this is an initial
972 * OGM without mcast support then increase the disabled mcast
975 } else if (!orig_mcast_enabled &&
976 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
977 !orig_initialized)) {
978 atomic_inc(&bat_priv->mcast.num_disabled);
979 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
982 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
984 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
985 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
986 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
988 orig->mcast_flags = mcast_flags;
989 spin_unlock_bh(&orig->mcast_handler_lock);
993 * batadv_mcast_init - initialize the multicast optimizations structures
994 * @bat_priv: the bat priv with all the soft interface information
996 void batadv_mcast_init(struct batadv_priv *bat_priv)
998 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
999 NULL, BATADV_TVLV_MCAST, 2,
1000 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1004 * batadv_mcast_free - free the multicast optimizations structures
1005 * @bat_priv: the bat priv with all the soft interface information
1007 void batadv_mcast_free(struct batadv_priv *bat_priv)
1009 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1010 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1012 spin_lock_bh(&bat_priv->tt.commit_lock);
1013 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1014 spin_unlock_bh(&bat_priv->tt.commit_lock);
1018 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1019 * @orig: the originator which is going to get purged
1021 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1023 struct batadv_priv *bat_priv = orig->bat_priv;
1025 spin_lock_bh(&orig->mcast_handler_lock);
1027 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1028 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1029 atomic_dec(&bat_priv->mcast.num_disabled);
1031 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1032 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1033 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1035 spin_unlock_bh(&orig->mcast_handler_lock);