Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / net / batman-adv / multicast.c
blob854e5ff28a3fe5d2d57862142be45d56a403c448
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2014-2020 B.A.T.M.A.N. contributors:
4 * Linus Lüssing
5 */
7 #include "multicast.h"
8 #include "main.h"
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/errno.h>
15 #include <linux/etherdevice.h>
16 #include <linux/gfp.h>
17 #include <linux/icmpv6.h>
18 #include <linux/if_bridge.h>
19 #include <linux/if_ether.h>
20 #include <linux/igmp.h>
21 #include <linux/in.h>
22 #include <linux/in6.h>
23 #include <linux/inetdevice.h>
24 #include <linux/ip.h>
25 #include <linux/ipv6.h>
26 #include <linux/jiffies.h>
27 #include <linux/kernel.h>
28 #include <linux/kref.h>
29 #include <linux/list.h>
30 #include <linux/lockdep.h>
31 #include <linux/netdevice.h>
32 #include <linux/netlink.h>
33 #include <linux/printk.h>
34 #include <linux/rculist.h>
35 #include <linux/rcupdate.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/stddef.h>
40 #include <linux/string.h>
41 #include <linux/types.h>
42 #include <linux/workqueue.h>
43 #include <net/addrconf.h>
44 #include <net/genetlink.h>
45 #include <net/if_inet6.h>
46 #include <net/ip.h>
47 #include <net/ipv6.h>
48 #include <net/netlink.h>
49 #include <net/sock.h>
50 #include <uapi/linux/batadv_packet.h>
51 #include <uapi/linux/batman_adv.h>
53 #include "bridge_loop_avoidance.h"
54 #include "hard-interface.h"
55 #include "hash.h"
56 #include "log.h"
57 #include "netlink.h"
58 #include "send.h"
59 #include "soft-interface.h"
60 #include "translation-table.h"
61 #include "tvlv.h"
63 static void batadv_mcast_mla_update(struct work_struct *work);
65 /**
66 * batadv_mcast_start_timer() - schedule the multicast periodic worker
67 * @bat_priv: the bat priv with all the soft interface information
69 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
71 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
72 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
75 /**
76 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
77 * @soft_iface: netdev struct of the mesh interface
79 * If the given soft interface has a bridge on top then the refcount
80 * of the according net device is increased.
82 * Return: NULL if no such bridge exists. Otherwise the net device of the
83 * bridge.
85 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
87 struct net_device *upper = soft_iface;
89 rcu_read_lock();
90 do {
91 upper = netdev_master_upper_dev_get_rcu(upper);
92 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
94 if (upper)
95 dev_hold(upper);
96 rcu_read_unlock();
98 return upper;
102 * batadv_mcast_mla_rtr_flags_softif_get_ipv4() - get mcast router flags from
103 * node for IPv4
104 * @dev: the interface to check
106 * Checks the presence of an IPv4 multicast router on this node.
108 * Caller needs to hold rcu read lock.
110 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
112 static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv4(struct net_device *dev)
114 struct in_device *in_dev = __in_dev_get_rcu(dev);
116 if (in_dev && IN_DEV_MFORWARD(in_dev))
117 return BATADV_NO_FLAGS;
118 else
119 return BATADV_MCAST_WANT_NO_RTR4;
123 * batadv_mcast_mla_rtr_flags_softif_get_ipv6() - get mcast router flags from
124 * node for IPv6
125 * @dev: the interface to check
127 * Checks the presence of an IPv6 multicast router on this node.
129 * Caller needs to hold rcu read lock.
131 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
133 #if IS_ENABLED(CONFIG_IPV6_MROUTE)
134 static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
136 struct inet6_dev *in6_dev = __in6_dev_get(dev);
138 if (in6_dev && in6_dev->cnf.mc_forwarding)
139 return BATADV_NO_FLAGS;
140 else
141 return BATADV_MCAST_WANT_NO_RTR6;
143 #else
144 static inline u8
145 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
147 return BATADV_MCAST_WANT_NO_RTR6;
149 #endif
152 * batadv_mcast_mla_rtr_flags_softif_get() - get mcast router flags from node
153 * @bat_priv: the bat priv with all the soft interface information
154 * @bridge: bridge interface on top of the soft_iface if present,
155 * otherwise pass NULL
157 * Checks the presence of IPv4 and IPv6 multicast routers on this
158 * node.
160 * Return:
161 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
162 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
163 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
164 * The former two OR'd: no multicast router is present
166 static u8 batadv_mcast_mla_rtr_flags_softif_get(struct batadv_priv *bat_priv,
167 struct net_device *bridge)
169 struct net_device *dev = bridge ? bridge : bat_priv->soft_iface;
170 u8 flags = BATADV_NO_FLAGS;
172 rcu_read_lock();
174 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv4(dev);
175 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv6(dev);
177 rcu_read_unlock();
179 return flags;
183 * batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge
184 * @bat_priv: the bat priv with all the soft interface information
185 * @bridge: bridge interface on top of the soft_iface if present,
186 * otherwise pass NULL
188 * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
190 * Return:
191 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
192 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
193 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
194 * The former two OR'd: no multicast router is present
196 #if IS_ENABLED(CONFIG_IPV6)
197 static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
198 struct net_device *bridge)
200 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
201 struct net_device *dev = bat_priv->soft_iface;
202 struct br_ip_list *br_ip_entry, *tmp;
203 u8 flags = BATADV_MCAST_WANT_NO_RTR6;
204 int ret;
206 if (!bridge)
207 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
209 /* TODO: ask the bridge if a multicast router is present (the bridge
210 * is capable of performing proper RFC4286 multicast router
211 * discovery) instead of searching for a ff02::2 listener here
213 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
214 if (ret < 0)
215 return BATADV_NO_FLAGS;
217 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
218 /* the bridge snooping does not maintain IPv4 link-local
219 * addresses - therefore we won't find any IPv4 multicast router
220 * address here, only IPv6 ones
222 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
223 ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.dst.ip6))
224 flags &= ~BATADV_MCAST_WANT_NO_RTR6;
226 list_del(&br_ip_entry->list);
227 kfree(br_ip_entry);
230 return flags;
232 #else
233 static inline u8
234 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
235 struct net_device *bridge)
237 if (bridge)
238 return BATADV_NO_FLAGS;
239 else
240 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
242 #endif
245 * batadv_mcast_mla_rtr_flags_get() - get multicast router flags
246 * @bat_priv: the bat priv with all the soft interface information
247 * @bridge: bridge interface on top of the soft_iface if present,
248 * otherwise pass NULL
250 * Checks the presence of IPv4 and IPv6 multicast routers on this
251 * node or behind its bridge.
253 * Return:
254 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
255 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
256 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
257 * The former two OR'd: no multicast router is present
259 static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
260 struct net_device *bridge)
262 u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
264 flags &= batadv_mcast_mla_rtr_flags_softif_get(bat_priv, bridge);
265 flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
267 return flags;
271 * batadv_mcast_mla_flags_get() - get the new multicast flags
272 * @bat_priv: the bat priv with all the soft interface information
274 * Return: A set of flags for the current/next TVLV, querier and
275 * bridge state.
277 static struct batadv_mcast_mla_flags
278 batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
280 struct net_device *dev = bat_priv->soft_iface;
281 struct batadv_mcast_querier_state *qr4, *qr6;
282 struct batadv_mcast_mla_flags mla_flags;
283 struct net_device *bridge;
285 bridge = batadv_mcast_get_bridge(dev);
287 memset(&mla_flags, 0, sizeof(mla_flags));
288 mla_flags.enabled = 1;
289 mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv,
290 bridge);
292 if (!bridge)
293 return mla_flags;
295 dev_put(bridge);
297 mla_flags.bridged = 1;
298 qr4 = &mla_flags.querier_ipv4;
299 qr6 = &mla_flags.querier_ipv6;
301 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
302 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
304 qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
305 qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
307 qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
308 qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
310 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
312 /* 1) If no querier exists at all, then multicast listeners on
313 * our local TT clients behind the bridge will keep silent.
314 * 2) If the selected querier is on one of our local TT clients,
315 * behind the bridge, then this querier might shadow multicast
316 * listeners on our local TT clients, behind this bridge.
318 * In both cases, we will signalize other batman nodes that
319 * we need all multicast traffic of the according protocol.
321 if (!qr4->exists || qr4->shadowing) {
322 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4;
323 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4;
326 if (!qr6->exists || qr6->shadowing) {
327 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6;
328 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6;
331 return mla_flags;
335 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
336 * @mcast_addr: the multicast address to check
337 * @mcast_list: the list with multicast addresses to search in
339 * Return: true if the given address is already in the given list.
340 * Otherwise returns false.
342 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
343 struct hlist_head *mcast_list)
345 struct batadv_hw_addr *mcast_entry;
347 hlist_for_each_entry(mcast_entry, mcast_list, list)
348 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
349 return true;
351 return false;
355 * batadv_mcast_mla_softif_get_ipv4() - get softif IPv4 multicast listeners
356 * @dev: the device to collect multicast addresses from
357 * @mcast_list: a list to put found addresses into
358 * @flags: flags indicating the new multicast state
360 * Collects multicast addresses of IPv4 multicast listeners residing
361 * on this kernel on the given soft interface, dev, in
362 * the given mcast_list. In general, multicast listeners provided by
363 * your multicast receiving applications run directly on this node.
365 * Return: -ENOMEM on memory allocation error or the number of
366 * items added to the mcast_list otherwise.
368 static int
369 batadv_mcast_mla_softif_get_ipv4(struct net_device *dev,
370 struct hlist_head *mcast_list,
371 struct batadv_mcast_mla_flags *flags)
373 struct batadv_hw_addr *new;
374 struct in_device *in_dev;
375 u8 mcast_addr[ETH_ALEN];
376 struct ip_mc_list *pmc;
377 int ret = 0;
379 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
380 return 0;
382 rcu_read_lock();
384 in_dev = __in_dev_get_rcu(dev);
385 if (!in_dev) {
386 rcu_read_unlock();
387 return 0;
390 for (pmc = rcu_dereference(in_dev->mc_list); pmc;
391 pmc = rcu_dereference(pmc->next_rcu)) {
392 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
393 ipv4_is_local_multicast(pmc->multiaddr))
394 continue;
396 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
397 !ipv4_is_local_multicast(pmc->multiaddr))
398 continue;
400 ip_eth_mc_map(pmc->multiaddr, mcast_addr);
402 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
403 continue;
405 new = kmalloc(sizeof(*new), GFP_ATOMIC);
406 if (!new) {
407 ret = -ENOMEM;
408 break;
411 ether_addr_copy(new->addr, mcast_addr);
412 hlist_add_head(&new->list, mcast_list);
413 ret++;
415 rcu_read_unlock();
417 return ret;
421 * batadv_mcast_mla_softif_get_ipv6() - get softif IPv6 multicast listeners
422 * @dev: the device to collect multicast addresses from
423 * @mcast_list: a list to put found addresses into
424 * @flags: flags indicating the new multicast state
426 * Collects multicast addresses of IPv6 multicast listeners residing
427 * on this kernel on the given soft interface, dev, in
428 * the given mcast_list. In general, multicast listeners provided by
429 * your multicast receiving applications run directly on this node.
431 * Return: -ENOMEM on memory allocation error or the number of
432 * items added to the mcast_list otherwise.
434 #if IS_ENABLED(CONFIG_IPV6)
435 static int
436 batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
437 struct hlist_head *mcast_list,
438 struct batadv_mcast_mla_flags *flags)
440 struct batadv_hw_addr *new;
441 struct inet6_dev *in6_dev;
442 u8 mcast_addr[ETH_ALEN];
443 struct ifmcaddr6 *pmc6;
444 int ret = 0;
446 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
447 return 0;
449 rcu_read_lock();
451 in6_dev = __in6_dev_get(dev);
452 if (!in6_dev) {
453 rcu_read_unlock();
454 return 0;
457 read_lock_bh(&in6_dev->lock);
458 for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) {
459 if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) <
460 IPV6_ADDR_SCOPE_LINKLOCAL)
461 continue;
463 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
464 ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
465 continue;
467 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
468 IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) >
469 IPV6_ADDR_SCOPE_LINKLOCAL)
470 continue;
472 ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
474 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
475 continue;
477 new = kmalloc(sizeof(*new), GFP_ATOMIC);
478 if (!new) {
479 ret = -ENOMEM;
480 break;
483 ether_addr_copy(new->addr, mcast_addr);
484 hlist_add_head(&new->list, mcast_list);
485 ret++;
487 read_unlock_bh(&in6_dev->lock);
488 rcu_read_unlock();
490 return ret;
492 #else
493 static inline int
494 batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
495 struct hlist_head *mcast_list,
496 struct batadv_mcast_mla_flags *flags)
498 return 0;
500 #endif
503 * batadv_mcast_mla_softif_get() - get softif multicast listeners
504 * @dev: the device to collect multicast addresses from
505 * @mcast_list: a list to put found addresses into
506 * @flags: flags indicating the new multicast state
508 * Collects multicast addresses of multicast listeners residing
509 * on this kernel on the given soft interface, dev, in
510 * the given mcast_list. In general, multicast listeners provided by
511 * your multicast receiving applications run directly on this node.
513 * If there is a bridge interface on top of dev, collect from that one
514 * instead. Just like with IP addresses and routes, multicast listeners
515 * will(/should) register to the bridge interface instead of an
516 * enslaved bat0.
518 * Return: -ENOMEM on memory allocation error or the number of
519 * items added to the mcast_list otherwise.
521 static int
522 batadv_mcast_mla_softif_get(struct net_device *dev,
523 struct hlist_head *mcast_list,
524 struct batadv_mcast_mla_flags *flags)
526 struct net_device *bridge = batadv_mcast_get_bridge(dev);
527 int ret4, ret6 = 0;
529 if (bridge)
530 dev = bridge;
532 ret4 = batadv_mcast_mla_softif_get_ipv4(dev, mcast_list, flags);
533 if (ret4 < 0)
534 goto out;
536 ret6 = batadv_mcast_mla_softif_get_ipv6(dev, mcast_list, flags);
537 if (ret6 < 0) {
538 ret4 = 0;
539 goto out;
542 out:
543 if (bridge)
544 dev_put(bridge);
546 return ret4 + ret6;
550 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
551 * @dst: destination to write to - a multicast MAC address
552 * @src: source to read from - a multicast IP address
554 * Converts a given multicast IPv4/IPv6 address from a bridge
555 * to its matching multicast MAC address and copies it into the given
556 * destination buffer.
558 * Caller needs to make sure the destination buffer can hold
559 * at least ETH_ALEN bytes.
561 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
563 if (src->proto == htons(ETH_P_IP))
564 ip_eth_mc_map(src->dst.ip4, dst);
565 #if IS_ENABLED(CONFIG_IPV6)
566 else if (src->proto == htons(ETH_P_IPV6))
567 ipv6_eth_mc_map(&src->dst.ip6, dst);
568 #endif
569 else
570 eth_zero_addr(dst);
574 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
575 * @dev: a bridge slave whose bridge to collect multicast addresses from
576 * @mcast_list: a list to put found addresses into
577 * @flags: flags indicating the new multicast state
579 * Collects multicast addresses of multicast listeners residing
580 * on foreign, non-mesh devices which we gave access to our mesh via
581 * a bridge on top of the given soft interface, dev, in the given
582 * mcast_list.
584 * Return: -ENOMEM on memory allocation error or the number of
585 * items added to the mcast_list otherwise.
587 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
588 struct hlist_head *mcast_list,
589 struct batadv_mcast_mla_flags *flags)
591 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
592 struct br_ip_list *br_ip_entry, *tmp;
593 u8 tvlv_flags = flags->tvlv_flags;
594 struct batadv_hw_addr *new;
595 u8 mcast_addr[ETH_ALEN];
596 int ret;
598 /* we don't need to detect these devices/listeners, the IGMP/MLD
599 * snooping code of the Linux bridge already does that for us
601 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
602 if (ret < 0)
603 goto out;
605 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
606 if (br_ip_entry->addr.proto == htons(ETH_P_IP)) {
607 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
608 continue;
610 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
611 ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
612 continue;
614 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
615 !ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
616 continue;
619 #if IS_ENABLED(CONFIG_IPV6)
620 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) {
621 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
622 continue;
624 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
625 ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.dst.ip6))
626 continue;
628 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
629 IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.dst.ip6) >
630 IPV6_ADDR_SCOPE_LINKLOCAL)
631 continue;
633 #endif
635 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
636 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
637 continue;
639 new = kmalloc(sizeof(*new), GFP_ATOMIC);
640 if (!new) {
641 ret = -ENOMEM;
642 break;
645 ether_addr_copy(new->addr, mcast_addr);
646 hlist_add_head(&new->list, mcast_list);
649 out:
650 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
651 list_del(&br_ip_entry->list);
652 kfree(br_ip_entry);
655 return ret;
659 * batadv_mcast_mla_list_free() - free a list of multicast addresses
660 * @mcast_list: the list to free
662 * Removes and frees all items in the given mcast_list.
664 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
666 struct batadv_hw_addr *mcast_entry;
667 struct hlist_node *tmp;
669 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
670 hlist_del(&mcast_entry->list);
671 kfree(mcast_entry);
676 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
677 * @bat_priv: the bat priv with all the soft interface information
678 * @mcast_list: a list of addresses which should _not_ be removed
680 * Retracts the announcement of any multicast listener from the
681 * translation table except the ones listed in the given mcast_list.
683 * If mcast_list is NULL then all are retracted.
685 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
686 struct hlist_head *mcast_list)
688 struct batadv_hw_addr *mcast_entry;
689 struct hlist_node *tmp;
691 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
692 list) {
693 if (mcast_list &&
694 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
695 mcast_list))
696 continue;
698 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
699 BATADV_NO_FLAGS,
700 "mcast TT outdated", false);
702 hlist_del(&mcast_entry->list);
703 kfree(mcast_entry);
708 * batadv_mcast_mla_tt_add() - add multicast listener announcements
709 * @bat_priv: the bat priv with all the soft interface information
710 * @mcast_list: a list of addresses which are going to get added
712 * Adds multicast listener announcements from the given mcast_list to the
713 * translation table if they have not been added yet.
715 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
716 struct hlist_head *mcast_list)
718 struct batadv_hw_addr *mcast_entry;
719 struct hlist_node *tmp;
721 if (!mcast_list)
722 return;
724 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
725 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
726 &bat_priv->mcast.mla_list))
727 continue;
729 if (!batadv_tt_local_add(bat_priv->soft_iface,
730 mcast_entry->addr, BATADV_NO_FLAGS,
731 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
732 continue;
734 hlist_del(&mcast_entry->list);
735 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
740 * batadv_mcast_querier_log() - debug output regarding the querier status on
741 * link
742 * @bat_priv: the bat priv with all the soft interface information
743 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
744 * @old_state: the previous querier state on our link
745 * @new_state: the new querier state on our link
747 * Outputs debug messages to the logging facility with log level 'mcast'
748 * regarding changes to the querier status on the link which are relevant
749 * to our multicast optimizations.
751 * Usually this is about whether a querier appeared or vanished in
752 * our mesh or whether the querier is in the suboptimal position of being
753 * behind our local bridge segment: Snooping switches will directly
754 * forward listener reports to the querier, therefore batman-adv and
755 * the bridge will potentially not see these listeners - the querier is
756 * potentially shadowing listeners from us then.
758 * This is only interesting for nodes with a bridge on top of their
759 * soft interface.
761 static void
762 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
763 struct batadv_mcast_querier_state *old_state,
764 struct batadv_mcast_querier_state *new_state)
766 if (!old_state->exists && new_state->exists)
767 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
768 str_proto);
769 else if (old_state->exists && !new_state->exists)
770 batadv_info(bat_priv->soft_iface,
771 "%s Querier disappeared - multicast optimizations disabled\n",
772 str_proto);
773 else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists)
774 batadv_info(bat_priv->soft_iface,
775 "No %s Querier present - multicast optimizations disabled\n",
776 str_proto);
778 if (new_state->exists) {
779 if ((!old_state->shadowing && new_state->shadowing) ||
780 (!old_state->exists && new_state->shadowing))
781 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
782 "%s Querier is behind our bridged segment: Might shadow listeners\n",
783 str_proto);
784 else if (old_state->shadowing && !new_state->shadowing)
785 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
786 "%s Querier is not behind our bridged segment\n",
787 str_proto);
792 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
793 * setups
794 * @bat_priv: the bat priv with all the soft interface information
795 * @new_flags: flags indicating the new multicast state
797 * If no bridges are ever used on this node, then this function does nothing.
799 * Otherwise this function outputs debug information to the 'mcast' log level
800 * which might be relevant to our multicast optimizations.
802 * More precisely, it outputs information when a bridge interface is added or
803 * removed from a soft interface. And when a bridge is present, it further
804 * outputs information about the querier state which is relevant for the
805 * multicast flags this node is going to set.
807 static void
808 batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
809 struct batadv_mcast_mla_flags *new_flags)
811 struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
813 if (!old_flags->bridged && new_flags->bridged)
814 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
815 "Bridge added: Setting Unsnoopables(U)-flag\n");
816 else if (old_flags->bridged && !new_flags->bridged)
817 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
818 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
820 if (new_flags->bridged) {
821 batadv_mcast_querier_log(bat_priv, "IGMP",
822 &old_flags->querier_ipv4,
823 &new_flags->querier_ipv4);
824 batadv_mcast_querier_log(bat_priv, "MLD",
825 &old_flags->querier_ipv6,
826 &new_flags->querier_ipv6);
831 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
832 * @bat_priv: the bat priv with all the soft interface information
833 * @flags: TVLV flags indicating the new multicast state
835 * Whenever the multicast TVLV flags this node announces change, this function
836 * should be used to notify userspace about the change.
838 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
840 bool old_enabled = bat_priv->mcast.mla_flags.enabled;
841 u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags;
842 char str_old_flags[] = "[.... . ]";
844 sprintf(str_old_flags, "[%c%c%c%s%s]",
845 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
846 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
847 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
848 !(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
849 !(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
851 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
852 "Changing multicast flags from '%s' to '[%c%c%c%s%s]'\n",
853 old_enabled ? str_old_flags : "<undefined>",
854 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
855 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
856 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
857 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
858 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
862 * batadv_mcast_mla_flags_update() - update multicast flags
863 * @bat_priv: the bat priv with all the soft interface information
864 * @flags: flags indicating the new multicast state
866 * Updates the own multicast tvlv with our current multicast related settings,
867 * capabilities and inabilities.
869 static void
870 batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
871 struct batadv_mcast_mla_flags *flags)
873 struct batadv_tvlv_mcast_data mcast_data;
875 if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
876 return;
878 batadv_mcast_bridge_log(bat_priv, flags);
879 batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
881 mcast_data.flags = flags->tvlv_flags;
882 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
884 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
885 &mcast_data, sizeof(mcast_data));
887 bat_priv->mcast.mla_flags = *flags;
891 * __batadv_mcast_mla_update() - update the own MLAs
892 * @bat_priv: the bat priv with all the soft interface information
894 * Updates the own multicast listener announcements in the translation
895 * table as well as the own, announced multicast tvlv container.
897 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
898 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
899 * ensured by the non-parallel execution of the worker this function
900 * belongs to.
902 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
904 struct net_device *soft_iface = bat_priv->soft_iface;
905 struct hlist_head mcast_list = HLIST_HEAD_INIT;
906 struct batadv_mcast_mla_flags flags;
907 int ret;
909 flags = batadv_mcast_mla_flags_get(bat_priv);
911 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list, &flags);
912 if (ret < 0)
913 goto out;
915 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list, &flags);
916 if (ret < 0)
917 goto out;
919 spin_lock(&bat_priv->mcast.mla_lock);
920 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
921 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
922 batadv_mcast_mla_flags_update(bat_priv, &flags);
923 spin_unlock(&bat_priv->mcast.mla_lock);
925 out:
926 batadv_mcast_mla_list_free(&mcast_list);
930 * batadv_mcast_mla_update() - update the own MLAs
931 * @work: kernel work struct
933 * Updates the own multicast listener announcements in the translation
934 * table as well as the own, announced multicast tvlv container.
936 * In the end, reschedules the work timer.
938 static void batadv_mcast_mla_update(struct work_struct *work)
940 struct delayed_work *delayed_work;
941 struct batadv_priv_mcast *priv_mcast;
942 struct batadv_priv *bat_priv;
944 delayed_work = to_delayed_work(work);
945 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
946 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
948 __batadv_mcast_mla_update(bat_priv);
949 batadv_mcast_start_timer(bat_priv);
953 * batadv_mcast_is_report_ipv4() - check for IGMP reports
954 * @skb: the ethernet frame destined for the mesh
956 * This call might reallocate skb data.
958 * Checks whether the given frame is a valid IGMP report.
960 * Return: If so then true, otherwise false.
962 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
964 if (ip_mc_check_igmp(skb) < 0)
965 return false;
967 switch (igmp_hdr(skb)->type) {
968 case IGMP_HOST_MEMBERSHIP_REPORT:
969 case IGMPV2_HOST_MEMBERSHIP_REPORT:
970 case IGMPV3_HOST_MEMBERSHIP_REPORT:
971 return true;
974 return false;
978 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
979 * potential
980 * @bat_priv: the bat priv with all the soft interface information
981 * @skb: the IPv4 packet to check
982 * @is_unsnoopable: stores whether the destination is snoopable
983 * @is_routable: stores whether the destination is routable
985 * Checks whether the given IPv4 packet has the potential to be forwarded with a
986 * mode more optimal than classic flooding.
988 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
989 * allocation failure.
991 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
992 struct sk_buff *skb,
993 bool *is_unsnoopable,
994 int *is_routable)
996 struct iphdr *iphdr;
998 /* We might fail due to out-of-memory -> drop it */
999 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
1000 return -ENOMEM;
1002 if (batadv_mcast_is_report_ipv4(skb))
1003 return -EINVAL;
1005 iphdr = ip_hdr(skb);
1007 /* link-local multicast listeners behind a bridge are
1008 * not snoopable (see RFC4541, section 2.1.2.2)
1010 if (ipv4_is_local_multicast(iphdr->daddr))
1011 *is_unsnoopable = true;
1012 else
1013 *is_routable = ETH_P_IP;
1015 return 0;
1019 * batadv_mcast_is_report_ipv6() - check for MLD reports
1020 * @skb: the ethernet frame destined for the mesh
1022 * This call might reallocate skb data.
1024 * Checks whether the given frame is a valid MLD report.
1026 * Return: If so then true, otherwise false.
1028 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
1030 if (ipv6_mc_check_mld(skb) < 0)
1031 return false;
1033 switch (icmp6_hdr(skb)->icmp6_type) {
1034 case ICMPV6_MGM_REPORT:
1035 case ICMPV6_MLD2_REPORT:
1036 return true;
1039 return false;
1043 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
1044 * potential
1045 * @bat_priv: the bat priv with all the soft interface information
1046 * @skb: the IPv6 packet to check
1047 * @is_unsnoopable: stores whether the destination is snoopable
1048 * @is_routable: stores whether the destination is routable
1050 * Checks whether the given IPv6 packet has the potential to be forwarded with a
1051 * mode more optimal than classic flooding.
1053 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1055 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
1056 struct sk_buff *skb,
1057 bool *is_unsnoopable,
1058 int *is_routable)
1060 struct ipv6hdr *ip6hdr;
1062 /* We might fail due to out-of-memory -> drop it */
1063 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
1064 return -ENOMEM;
1066 if (batadv_mcast_is_report_ipv6(skb))
1067 return -EINVAL;
1069 ip6hdr = ipv6_hdr(skb);
1071 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1072 return -EINVAL;
1074 /* link-local-all-nodes multicast listeners behind a bridge are
1075 * not snoopable (see RFC4541, section 3, paragraph 3)
1077 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
1078 *is_unsnoopable = true;
1079 else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL)
1080 *is_routable = ETH_P_IPV6;
1082 return 0;
1086 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
1087 * @bat_priv: the bat priv with all the soft interface information
1088 * @skb: the multicast frame to check
1089 * @is_unsnoopable: stores whether the destination is snoopable
1090 * @is_routable: stores whether the destination is routable
1092 * Checks whether the given multicast ethernet frame has the potential to be
1093 * forwarded with a mode more optimal than classic flooding.
1095 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1097 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
1098 struct sk_buff *skb,
1099 bool *is_unsnoopable,
1100 int *is_routable)
1102 struct ethhdr *ethhdr = eth_hdr(skb);
1104 if (!atomic_read(&bat_priv->multicast_mode))
1105 return -EINVAL;
1107 switch (ntohs(ethhdr->h_proto)) {
1108 case ETH_P_IP:
1109 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
1110 is_unsnoopable,
1111 is_routable);
1112 case ETH_P_IPV6:
1113 if (!IS_ENABLED(CONFIG_IPV6))
1114 return -EINVAL;
1116 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
1117 is_unsnoopable,
1118 is_routable);
1119 default:
1120 return -EINVAL;
1125 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
1126 * interest
1127 * @bat_priv: the bat priv with all the soft interface information
1128 * @ethhdr: ethernet header of a packet
1130 * Return: the number of nodes which want all IPv4 multicast traffic if the
1131 * given ethhdr is from an IPv4 packet or the number of nodes which want all
1132 * IPv6 traffic if it matches an IPv6 packet.
1134 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
1135 struct ethhdr *ethhdr)
1137 switch (ntohs(ethhdr->h_proto)) {
1138 case ETH_P_IP:
1139 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
1140 case ETH_P_IPV6:
1141 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
1142 default:
1143 /* we shouldn't be here... */
1144 return 0;
1149 * batadv_mcast_forw_rtr_count() - count nodes with a multicast router
1150 * @bat_priv: the bat priv with all the soft interface information
1151 * @protocol: the ethernet protocol type to count multicast routers for
1153 * Return: the number of nodes which want all routable IPv4 multicast traffic
1154 * if the protocol is ETH_P_IP or the number of nodes which want all routable
1155 * IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0.
1158 static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
1159 int protocol)
1161 switch (protocol) {
1162 case ETH_P_IP:
1163 return atomic_read(&bat_priv->mcast.num_want_all_rtr4);
1164 case ETH_P_IPV6:
1165 return atomic_read(&bat_priv->mcast.num_want_all_rtr6);
1166 default:
1167 return 0;
1172 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
1173 * @bat_priv: the bat priv with all the soft interface information
1174 * @ethhdr: the ether header containing the multicast destination
1176 * Return: an orig_node matching the multicast address provided by ethhdr
1177 * via a translation table lookup. This increases the returned nodes refcount.
1179 static struct batadv_orig_node *
1180 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
1181 struct ethhdr *ethhdr)
1183 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
1184 BATADV_NO_FLAGS);
1188 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
1189 * @bat_priv: the bat priv with all the soft interface information
1191 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
1192 * increases its refcount.
1194 static struct batadv_orig_node *
1195 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
1197 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1199 rcu_read_lock();
1200 hlist_for_each_entry_rcu(tmp_orig_node,
1201 &bat_priv->mcast.want_all_ipv4_list,
1202 mcast_want_all_ipv4_node) {
1203 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1204 continue;
1206 orig_node = tmp_orig_node;
1207 break;
1209 rcu_read_unlock();
1211 return orig_node;
1215 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
1216 * @bat_priv: the bat priv with all the soft interface information
1218 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
1219 * and increases its refcount.
1221 static struct batadv_orig_node *
1222 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
1224 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1226 rcu_read_lock();
1227 hlist_for_each_entry_rcu(tmp_orig_node,
1228 &bat_priv->mcast.want_all_ipv6_list,
1229 mcast_want_all_ipv6_node) {
1230 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1231 continue;
1233 orig_node = tmp_orig_node;
1234 break;
1236 rcu_read_unlock();
1238 return orig_node;
1242 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
1243 * @bat_priv: the bat priv with all the soft interface information
1244 * @ethhdr: an ethernet header to determine the protocol family from
1246 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
1247 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, sets and
1248 * increases its refcount.
1250 static struct batadv_orig_node *
1251 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
1252 struct ethhdr *ethhdr)
1254 switch (ntohs(ethhdr->h_proto)) {
1255 case ETH_P_IP:
1256 return batadv_mcast_forw_ipv4_node_get(bat_priv);
1257 case ETH_P_IPV6:
1258 return batadv_mcast_forw_ipv6_node_get(bat_priv);
1259 default:
1260 /* we shouldn't be here... */
1261 return NULL;
1266 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
1267 * @bat_priv: the bat priv with all the soft interface information
1269 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
1270 * set and increases its refcount.
1272 static struct batadv_orig_node *
1273 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
1275 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1277 rcu_read_lock();
1278 hlist_for_each_entry_rcu(tmp_orig_node,
1279 &bat_priv->mcast.want_all_unsnoopables_list,
1280 mcast_want_all_unsnoopables_node) {
1281 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1282 continue;
1284 orig_node = tmp_orig_node;
1285 break;
1287 rcu_read_unlock();
1289 return orig_node;
1293 * batadv_mcast_forw_rtr4_node_get() - get a node with an ipv4 mcast router flag
1294 * @bat_priv: the bat priv with all the soft interface information
1296 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR4 flag unset and
1297 * increases its refcount.
1299 static struct batadv_orig_node *
1300 batadv_mcast_forw_rtr4_node_get(struct batadv_priv *bat_priv)
1302 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1304 rcu_read_lock();
1305 hlist_for_each_entry_rcu(tmp_orig_node,
1306 &bat_priv->mcast.want_all_rtr4_list,
1307 mcast_want_all_rtr4_node) {
1308 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1309 continue;
1311 orig_node = tmp_orig_node;
1312 break;
1314 rcu_read_unlock();
1316 return orig_node;
1320 * batadv_mcast_forw_rtr6_node_get() - get a node with an ipv6 mcast router flag
1321 * @bat_priv: the bat priv with all the soft interface information
1323 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR6 flag unset
1324 * and increases its refcount.
1326 static struct batadv_orig_node *
1327 batadv_mcast_forw_rtr6_node_get(struct batadv_priv *bat_priv)
1329 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1331 rcu_read_lock();
1332 hlist_for_each_entry_rcu(tmp_orig_node,
1333 &bat_priv->mcast.want_all_rtr6_list,
1334 mcast_want_all_rtr6_node) {
1335 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1336 continue;
1338 orig_node = tmp_orig_node;
1339 break;
1341 rcu_read_unlock();
1343 return orig_node;
1347 * batadv_mcast_forw_rtr_node_get() - get a node with an ipv4/ipv6 router flag
1348 * @bat_priv: the bat priv with all the soft interface information
1349 * @ethhdr: an ethernet header to determine the protocol family from
1351 * Return: an orig_node which has no BATADV_MCAST_WANT_NO_RTR4 or
1352 * BATADV_MCAST_WANT_NO_RTR6 flag, depending on the provided ethhdr, set and
1353 * increases its refcount.
1355 static struct batadv_orig_node *
1356 batadv_mcast_forw_rtr_node_get(struct batadv_priv *bat_priv,
1357 struct ethhdr *ethhdr)
1359 switch (ntohs(ethhdr->h_proto)) {
1360 case ETH_P_IP:
1361 return batadv_mcast_forw_rtr4_node_get(bat_priv);
1362 case ETH_P_IPV6:
1363 return batadv_mcast_forw_rtr6_node_get(bat_priv);
1364 default:
1365 /* we shouldn't be here... */
1366 return NULL;
1371 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
1372 * @bat_priv: the bat priv with all the soft interface information
1373 * @skb: The multicast packet to check
1374 * @orig: an originator to be set to forward the skb to
1376 * Return: the forwarding mode as enum batadv_forw_mode and in case of
1377 * BATADV_FORW_SINGLE set the orig to the single originator the skb
1378 * should be forwarded to.
1380 enum batadv_forw_mode
1381 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
1382 struct batadv_orig_node **orig)
1384 int ret, tt_count, ip_count, unsnoop_count, total_count;
1385 bool is_unsnoopable = false;
1386 unsigned int mcast_fanout;
1387 struct ethhdr *ethhdr;
1388 int is_routable = 0;
1389 int rtr_count = 0;
1391 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable,
1392 &is_routable);
1393 if (ret == -ENOMEM)
1394 return BATADV_FORW_NONE;
1395 else if (ret < 0)
1396 return BATADV_FORW_ALL;
1398 ethhdr = eth_hdr(skb);
1400 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1401 BATADV_NO_FLAGS);
1402 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1403 unsnoop_count = !is_unsnoopable ? 0 :
1404 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
1405 rtr_count = batadv_mcast_forw_rtr_count(bat_priv, is_routable);
1407 total_count = tt_count + ip_count + unsnoop_count + rtr_count;
1409 switch (total_count) {
1410 case 1:
1411 if (tt_count)
1412 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1413 else if (ip_count)
1414 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1415 else if (unsnoop_count)
1416 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1417 else if (rtr_count)
1418 *orig = batadv_mcast_forw_rtr_node_get(bat_priv,
1419 ethhdr);
1421 if (*orig)
1422 return BATADV_FORW_SINGLE;
1424 fallthrough;
1425 case 0:
1426 return BATADV_FORW_NONE;
1427 default:
1428 mcast_fanout = atomic_read(&bat_priv->multicast_fanout);
1430 if (!unsnoop_count && total_count <= mcast_fanout)
1431 return BATADV_FORW_SOME;
1434 return BATADV_FORW_ALL;
1438 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1439 * @bat_priv: the bat priv with all the soft interface information
1440 * @skb: the multicast packet to send
1441 * @vid: the vlan identifier
1442 * @orig_node: the originator to send the packet to
1444 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1446 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1447 struct sk_buff *skb,
1448 unsigned short vid,
1449 struct batadv_orig_node *orig_node)
1451 /* Avoid sending multicast-in-unicast packets to other BLA
1452 * gateways - they already got the frame from the LAN side
1453 * we share with them.
1454 * TODO: Refactor to take BLA into account earlier, to avoid
1455 * reducing the mcast_fanout count.
1457 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1458 dev_kfree_skb(skb);
1459 return NET_XMIT_SUCCESS;
1462 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1463 orig_node, vid);
1467 * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1468 * @bat_priv: the bat priv with all the soft interface information
1469 * @skb: the multicast packet to transmit
1470 * @vid: the vlan identifier
1472 * Sends copies of a frame with multicast destination to any multicast
1473 * listener registered in the translation table. A transmission is performed
1474 * via a batman-adv unicast packet for each such destination node.
1476 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1477 * otherwise.
1479 static int
1480 batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
1481 unsigned short vid)
1483 int ret = NET_XMIT_SUCCESS;
1484 struct sk_buff *newskb;
1486 struct batadv_tt_orig_list_entry *orig_entry;
1488 struct batadv_tt_global_entry *tt_global;
1489 const u8 *addr = eth_hdr(skb)->h_dest;
1491 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
1492 if (!tt_global)
1493 goto out;
1495 rcu_read_lock();
1496 hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
1497 newskb = skb_copy(skb, GFP_ATOMIC);
1498 if (!newskb) {
1499 ret = NET_XMIT_DROP;
1500 break;
1503 batadv_mcast_forw_send_orig(bat_priv, newskb, vid,
1504 orig_entry->orig_node);
1506 rcu_read_unlock();
1508 batadv_tt_global_entry_put(tt_global);
1510 out:
1511 return ret;
1515 * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1516 * @bat_priv: the bat priv with all the soft interface information
1517 * @skb: the multicast packet to transmit
1518 * @vid: the vlan identifier
1520 * Sends copies of a frame with multicast destination to any node with a
1521 * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1522 * batman-adv unicast packet for each such destination node.
1524 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1525 * otherwise.
1527 static int
1528 batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
1529 struct sk_buff *skb, unsigned short vid)
1531 struct batadv_orig_node *orig_node;
1532 int ret = NET_XMIT_SUCCESS;
1533 struct sk_buff *newskb;
1535 rcu_read_lock();
1536 hlist_for_each_entry_rcu(orig_node,
1537 &bat_priv->mcast.want_all_ipv4_list,
1538 mcast_want_all_ipv4_node) {
1539 newskb = skb_copy(skb, GFP_ATOMIC);
1540 if (!newskb) {
1541 ret = NET_XMIT_DROP;
1542 break;
1545 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1547 rcu_read_unlock();
1548 return ret;
1552 * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1553 * @bat_priv: the bat priv with all the soft interface information
1554 * @skb: The multicast packet to transmit
1555 * @vid: the vlan identifier
1557 * Sends copies of a frame with multicast destination to any node with a
1558 * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1559 * batman-adv unicast packet for each such destination node.
1561 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1562 * otherwise.
1564 static int
1565 batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
1566 struct sk_buff *skb, unsigned short vid)
1568 struct batadv_orig_node *orig_node;
1569 int ret = NET_XMIT_SUCCESS;
1570 struct sk_buff *newskb;
1572 rcu_read_lock();
1573 hlist_for_each_entry_rcu(orig_node,
1574 &bat_priv->mcast.want_all_ipv6_list,
1575 mcast_want_all_ipv6_node) {
1576 newskb = skb_copy(skb, GFP_ATOMIC);
1577 if (!newskb) {
1578 ret = NET_XMIT_DROP;
1579 break;
1582 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1584 rcu_read_unlock();
1585 return ret;
1589 * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1590 * @bat_priv: the bat priv with all the soft interface information
1591 * @skb: the multicast packet to transmit
1592 * @vid: the vlan identifier
1594 * Sends copies of a frame with multicast destination to any node with a
1595 * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1596 * transmission is performed via a batman-adv unicast packet for each such
1597 * destination node.
1599 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1600 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1602 static int
1603 batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
1604 struct sk_buff *skb, unsigned short vid)
1606 switch (ntohs(eth_hdr(skb)->h_proto)) {
1607 case ETH_P_IP:
1608 return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
1609 case ETH_P_IPV6:
1610 return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
1611 default:
1612 /* we shouldn't be here... */
1613 return NET_XMIT_DROP;
1618 * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
1619 * @bat_priv: the bat priv with all the soft interface information
1620 * @skb: the multicast packet to transmit
1621 * @vid: the vlan identifier
1623 * Sends copies of a frame with multicast destination to any node with a
1624 * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
1625 * batman-adv unicast packet for each such destination node.
1627 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1628 * otherwise.
1630 static int
1631 batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
1632 struct sk_buff *skb, unsigned short vid)
1634 struct batadv_orig_node *orig_node;
1635 int ret = NET_XMIT_SUCCESS;
1636 struct sk_buff *newskb;
1638 rcu_read_lock();
1639 hlist_for_each_entry_rcu(orig_node,
1640 &bat_priv->mcast.want_all_rtr4_list,
1641 mcast_want_all_rtr4_node) {
1642 newskb = skb_copy(skb, GFP_ATOMIC);
1643 if (!newskb) {
1644 ret = NET_XMIT_DROP;
1645 break;
1648 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1650 rcu_read_unlock();
1651 return ret;
1655 * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
1656 * @bat_priv: the bat priv with all the soft interface information
1657 * @skb: The multicast packet to transmit
1658 * @vid: the vlan identifier
1660 * Sends copies of a frame with multicast destination to any node with a
1661 * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
1662 * batman-adv unicast packet for each such destination node.
1664 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1665 * otherwise.
1667 static int
1668 batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
1669 struct sk_buff *skb, unsigned short vid)
1671 struct batadv_orig_node *orig_node;
1672 int ret = NET_XMIT_SUCCESS;
1673 struct sk_buff *newskb;
1675 rcu_read_lock();
1676 hlist_for_each_entry_rcu(orig_node,
1677 &bat_priv->mcast.want_all_rtr6_list,
1678 mcast_want_all_rtr6_node) {
1679 newskb = skb_copy(skb, GFP_ATOMIC);
1680 if (!newskb) {
1681 ret = NET_XMIT_DROP;
1682 break;
1685 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1687 rcu_read_unlock();
1688 return ret;
1692 * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
1693 * @bat_priv: the bat priv with all the soft interface information
1694 * @skb: the multicast packet to transmit
1695 * @vid: the vlan identifier
1697 * Sends copies of a frame with multicast destination to any node with a
1698 * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
1699 * transmission is performed via a batman-adv unicast packet for each such
1700 * destination node.
1702 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1703 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1705 static int
1706 batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
1707 struct sk_buff *skb, unsigned short vid)
1709 switch (ntohs(eth_hdr(skb)->h_proto)) {
1710 case ETH_P_IP:
1711 return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
1712 case ETH_P_IPV6:
1713 return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
1714 default:
1715 /* we shouldn't be here... */
1716 return NET_XMIT_DROP;
1721 * batadv_mcast_forw_send() - send packet to any detected multicast recipient
1722 * @bat_priv: the bat priv with all the soft interface information
1723 * @skb: the multicast packet to transmit
1724 * @vid: the vlan identifier
1726 * Sends copies of a frame with multicast destination to any node that signaled
1727 * interest in it, that is either via the translation table or the according
1728 * want-all flags. A transmission is performed via a batman-adv unicast packet
1729 * for each such destination node.
1731 * The given skb is consumed/freed.
1733 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1734 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1736 int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
1737 unsigned short vid)
1739 int ret;
1741 ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
1742 if (ret != NET_XMIT_SUCCESS) {
1743 kfree_skb(skb);
1744 return ret;
1747 ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
1748 if (ret != NET_XMIT_SUCCESS) {
1749 kfree_skb(skb);
1750 return ret;
1753 ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
1754 if (ret != NET_XMIT_SUCCESS) {
1755 kfree_skb(skb);
1756 return ret;
1759 consume_skb(skb);
1760 return ret;
1764 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1765 * @bat_priv: the bat priv with all the soft interface information
1766 * @orig: the orig_node which multicast state might have changed of
1767 * @mcast_flags: flags indicating the new multicast state
1769 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1770 * orig, has toggled then this method updates the counter and the list
1771 * accordingly.
1773 * Caller needs to hold orig->mcast_handler_lock.
1775 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1776 struct batadv_orig_node *orig,
1777 u8 mcast_flags)
1779 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1780 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1782 lockdep_assert_held(&orig->mcast_handler_lock);
1784 /* switched from flag unset to set */
1785 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1786 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1787 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1789 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1790 /* flag checks above + mcast_handler_lock prevents this */
1791 WARN_ON(!hlist_unhashed(node));
1793 hlist_add_head_rcu(node, head);
1794 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1795 /* switched from flag set to unset */
1796 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1797 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1798 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1800 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1801 /* flag checks above + mcast_handler_lock prevents this */
1802 WARN_ON(hlist_unhashed(node));
1804 hlist_del_init_rcu(node);
1805 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1810 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1811 * @bat_priv: the bat priv with all the soft interface information
1812 * @orig: the orig_node which multicast state might have changed of
1813 * @mcast_flags: flags indicating the new multicast state
1815 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1816 * toggled then this method updates the counter and the list accordingly.
1818 * Caller needs to hold orig->mcast_handler_lock.
1820 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1821 struct batadv_orig_node *orig,
1822 u8 mcast_flags)
1824 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1825 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1827 lockdep_assert_held(&orig->mcast_handler_lock);
1829 /* switched from flag unset to set */
1830 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1831 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1832 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1834 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1835 /* flag checks above + mcast_handler_lock prevents this */
1836 WARN_ON(!hlist_unhashed(node));
1838 hlist_add_head_rcu(node, head);
1839 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1840 /* switched from flag set to unset */
1841 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1842 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1843 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1845 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1846 /* flag checks above + mcast_handler_lock prevents this */
1847 WARN_ON(hlist_unhashed(node));
1849 hlist_del_init_rcu(node);
1850 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1855 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1856 * @bat_priv: the bat priv with all the soft interface information
1857 * @orig: the orig_node which multicast state might have changed of
1858 * @mcast_flags: flags indicating the new multicast state
1860 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1861 * toggled then this method updates the counter and the list accordingly.
1863 * Caller needs to hold orig->mcast_handler_lock.
1865 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1866 struct batadv_orig_node *orig,
1867 u8 mcast_flags)
1869 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1870 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1872 lockdep_assert_held(&orig->mcast_handler_lock);
1874 /* switched from flag unset to set */
1875 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1876 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1877 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1879 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1880 /* flag checks above + mcast_handler_lock prevents this */
1881 WARN_ON(!hlist_unhashed(node));
1883 hlist_add_head_rcu(node, head);
1884 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1885 /* switched from flag set to unset */
1886 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1887 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1888 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1890 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1891 /* flag checks above + mcast_handler_lock prevents this */
1892 WARN_ON(hlist_unhashed(node));
1894 hlist_del_init_rcu(node);
1895 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1900 * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
1901 * @bat_priv: the bat priv with all the soft interface information
1902 * @orig: the orig_node which multicast state might have changed of
1903 * @mcast_flags: flags indicating the new multicast state
1905 * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
1906 * toggled then this method updates the counter and the list accordingly.
1908 * Caller needs to hold orig->mcast_handler_lock.
1910 static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
1911 struct batadv_orig_node *orig,
1912 u8 mcast_flags)
1914 struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
1915 struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
1917 lockdep_assert_held(&orig->mcast_handler_lock);
1919 /* switched from flag set to unset */
1920 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
1921 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
1922 atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
1924 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1925 /* flag checks above + mcast_handler_lock prevents this */
1926 WARN_ON(!hlist_unhashed(node));
1928 hlist_add_head_rcu(node, head);
1929 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1930 /* switched from flag unset to set */
1931 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
1932 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
1933 atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
1935 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1936 /* flag checks above + mcast_handler_lock prevents this */
1937 WARN_ON(hlist_unhashed(node));
1939 hlist_del_init_rcu(node);
1940 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1945 * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
1946 * @bat_priv: the bat priv with all the soft interface information
1947 * @orig: the orig_node which multicast state might have changed of
1948 * @mcast_flags: flags indicating the new multicast state
1950 * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
1951 * toggled then this method updates the counter and the list accordingly.
1953 * Caller needs to hold orig->mcast_handler_lock.
1955 static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
1956 struct batadv_orig_node *orig,
1957 u8 mcast_flags)
1959 struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
1960 struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
1962 lockdep_assert_held(&orig->mcast_handler_lock);
1964 /* switched from flag set to unset */
1965 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
1966 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
1967 atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
1969 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1970 /* flag checks above + mcast_handler_lock prevents this */
1971 WARN_ON(!hlist_unhashed(node));
1973 hlist_add_head_rcu(node, head);
1974 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1975 /* switched from flag unset to set */
1976 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
1977 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
1978 atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
1980 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1981 /* flag checks above + mcast_handler_lock prevents this */
1982 WARN_ON(hlist_unhashed(node));
1984 hlist_del_init_rcu(node);
1985 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1990 * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
1991 * @enabled: whether the originator has multicast TVLV support enabled
1992 * @tvlv_value: tvlv buffer containing the multicast flags
1993 * @tvlv_value_len: tvlv buffer length
1995 * Return: multicast flags for the given tvlv buffer
1997 static u8
1998 batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
2000 u8 mcast_flags = BATADV_NO_FLAGS;
2002 if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
2003 mcast_flags = *(u8 *)tvlv_value;
2005 if (!enabled) {
2006 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
2007 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
2010 /* remove redundant flags to avoid sending duplicate packets later */
2011 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
2012 mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
2014 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
2015 mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
2017 return mcast_flags;
2021 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
2022 * @bat_priv: the bat priv with all the soft interface information
2023 * @orig: the orig_node of the ogm
2024 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2025 * @tvlv_value: tvlv buffer containing the multicast data
2026 * @tvlv_value_len: tvlv buffer length
2028 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
2029 struct batadv_orig_node *orig,
2030 u8 flags,
2031 void *tvlv_value,
2032 u16 tvlv_value_len)
2034 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2035 u8 mcast_flags;
2037 mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
2038 tvlv_value, tvlv_value_len);
2040 spin_lock_bh(&orig->mcast_handler_lock);
2042 if (orig_mcast_enabled &&
2043 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2044 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2045 } else if (!orig_mcast_enabled &&
2046 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2047 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2050 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
2052 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
2053 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
2054 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
2055 batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
2056 batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
2058 orig->mcast_flags = mcast_flags;
2059 spin_unlock_bh(&orig->mcast_handler_lock);
2063 * batadv_mcast_init() - initialize the multicast optimizations structures
2064 * @bat_priv: the bat priv with all the soft interface information
2066 void batadv_mcast_init(struct batadv_priv *bat_priv)
2068 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
2069 NULL, BATADV_TVLV_MCAST, 2,
2070 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2072 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
2073 batadv_mcast_start_timer(bat_priv);
2077 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
2078 * @msg: buffer for the message
2079 * @bat_priv: the bat priv with all the soft interface information
2081 * Return: 0 or error code.
2083 int batadv_mcast_mesh_info_put(struct sk_buff *msg,
2084 struct batadv_priv *bat_priv)
2086 u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
2087 u32 flags_priv = BATADV_NO_FLAGS;
2089 if (bat_priv->mcast.mla_flags.bridged) {
2090 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
2092 if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
2093 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
2094 if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
2095 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
2096 if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
2097 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
2098 if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
2099 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
2102 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
2103 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
2104 return -EMSGSIZE;
2106 return 0;
2110 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
2111 * to a netlink socket
2112 * @msg: buffer for the message
2113 * @portid: netlink port
2114 * @cb: Control block containing additional options
2115 * @orig_node: originator to dump the multicast flags of
2117 * Return: 0 or error code.
2119 static int
2120 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
2121 struct netlink_callback *cb,
2122 struct batadv_orig_node *orig_node)
2124 void *hdr;
2126 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2127 &batadv_netlink_family, NLM_F_MULTI,
2128 BATADV_CMD_GET_MCAST_FLAGS);
2129 if (!hdr)
2130 return -ENOBUFS;
2132 genl_dump_check_consistent(cb, hdr);
2134 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2135 orig_node->orig)) {
2136 genlmsg_cancel(msg, hdr);
2137 return -EMSGSIZE;
2140 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2141 &orig_node->capabilities)) {
2142 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
2143 orig_node->mcast_flags)) {
2144 genlmsg_cancel(msg, hdr);
2145 return -EMSGSIZE;
2149 genlmsg_end(msg, hdr);
2150 return 0;
2154 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
2155 * table to a netlink socket
2156 * @msg: buffer for the message
2157 * @portid: netlink port
2158 * @cb: Control block containing additional options
2159 * @hash: hash to dump
2160 * @bucket: bucket index to dump
2161 * @idx_skip: How many entries to skip
2163 * Return: 0 or error code.
2165 static int
2166 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
2167 struct netlink_callback *cb,
2168 struct batadv_hashtable *hash,
2169 unsigned int bucket, long *idx_skip)
2171 struct batadv_orig_node *orig_node;
2172 long idx = 0;
2174 spin_lock_bh(&hash->list_locks[bucket]);
2175 cb->seq = atomic_read(&hash->generation) << 1 | 1;
2177 hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
2178 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2179 &orig_node->capa_initialized))
2180 continue;
2182 if (idx < *idx_skip)
2183 goto skip;
2185 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
2186 spin_unlock_bh(&hash->list_locks[bucket]);
2187 *idx_skip = idx;
2189 return -EMSGSIZE;
2192 skip:
2193 idx++;
2195 spin_unlock_bh(&hash->list_locks[bucket]);
2197 return 0;
2201 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2202 * @msg: buffer for the message
2203 * @portid: netlink port
2204 * @cb: Control block containing additional options
2205 * @bat_priv: the bat priv with all the soft interface information
2206 * @bucket: current bucket to dump
2207 * @idx: index in current bucket to the next entry to dump
2209 * Return: 0 or error code.
2211 static int
2212 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
2213 struct netlink_callback *cb,
2214 struct batadv_priv *bat_priv, long *bucket, long *idx)
2216 struct batadv_hashtable *hash = bat_priv->orig_hash;
2217 long bucket_tmp = *bucket;
2218 long idx_tmp = *idx;
2220 while (bucket_tmp < hash->size) {
2221 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
2222 bucket_tmp, &idx_tmp))
2223 break;
2225 bucket_tmp++;
2226 idx_tmp = 0;
2229 *bucket = bucket_tmp;
2230 *idx = idx_tmp;
2232 return msg->len;
2236 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
2237 * callback
2238 * @cb: netlink callback structure
2239 * @primary_if: the primary interface pointer to return the result in
2241 * Return: 0 or error code.
2243 static int
2244 batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
2245 struct batadv_hard_iface **primary_if)
2247 struct batadv_hard_iface *hard_iface = NULL;
2248 struct net *net = sock_net(cb->skb->sk);
2249 struct net_device *soft_iface;
2250 struct batadv_priv *bat_priv;
2251 int ifindex;
2252 int ret = 0;
2254 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2255 if (!ifindex)
2256 return -EINVAL;
2258 soft_iface = dev_get_by_index(net, ifindex);
2259 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2260 ret = -ENODEV;
2261 goto out;
2264 bat_priv = netdev_priv(soft_iface);
2266 hard_iface = batadv_primary_if_get_selected(bat_priv);
2267 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
2268 ret = -ENOENT;
2269 goto out;
2272 out:
2273 if (soft_iface)
2274 dev_put(soft_iface);
2276 if (!ret && primary_if)
2277 *primary_if = hard_iface;
2278 else if (hard_iface)
2279 batadv_hardif_put(hard_iface);
2281 return ret;
2285 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2286 * @msg: buffer for the message
2287 * @cb: callback structure containing arguments
2289 * Return: message length.
2291 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
2293 struct batadv_hard_iface *primary_if = NULL;
2294 int portid = NETLINK_CB(cb->skb).portid;
2295 struct batadv_priv *bat_priv;
2296 long *bucket = &cb->args[0];
2297 long *idx = &cb->args[1];
2298 int ret;
2300 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
2301 if (ret)
2302 return ret;
2304 bat_priv = netdev_priv(primary_if->soft_iface);
2305 ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
2307 batadv_hardif_put(primary_if);
2308 return ret;
2312 * batadv_mcast_free() - free the multicast optimizations structures
2313 * @bat_priv: the bat priv with all the soft interface information
2315 void batadv_mcast_free(struct batadv_priv *bat_priv)
2317 cancel_delayed_work_sync(&bat_priv->mcast.work);
2319 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2320 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2322 /* safely calling outside of worker, as worker was canceled above */
2323 batadv_mcast_mla_tt_retract(bat_priv, NULL);
2327 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
2328 * @orig: the originator which is going to get purged
2330 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
2332 struct batadv_priv *bat_priv = orig->bat_priv;
2334 spin_lock_bh(&orig->mcast_handler_lock);
2336 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
2337 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
2338 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
2339 batadv_mcast_want_rtr4_update(bat_priv, orig,
2340 BATADV_MCAST_WANT_NO_RTR4);
2341 batadv_mcast_want_rtr6_update(bat_priv, orig,
2342 BATADV_MCAST_WANT_NO_RTR6);
2344 spin_unlock_bh(&orig->mcast_handler_lock);