xref: /openbmc/linux/net/batman-adv/multicast.c (revision f97769fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2014-2020  B.A.T.M.A.N. contributors:
3  *
4  * Linus Lüssing
5  */
6 
7 #include "multicast.h"
8 #include "main.h"
9 
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/seq_file.h>
37 #include <linux/skbuff.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40 #include <linux/stddef.h>
41 #include <linux/string.h>
42 #include <linux/types.h>
43 #include <linux/workqueue.h>
44 #include <net/addrconf.h>
45 #include <net/genetlink.h>
46 #include <net/if_inet6.h>
47 #include <net/ip.h>
48 #include <net/ipv6.h>
49 #include <net/netlink.h>
50 #include <net/sock.h>
51 #include <uapi/linux/batadv_packet.h>
52 #include <uapi/linux/batman_adv.h>
53 
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"
62 
63 static void batadv_mcast_mla_update(struct work_struct *work);
64 
65 /**
66  * batadv_mcast_start_timer() - schedule the multicast periodic worker
67  * @bat_priv: the bat priv with all the soft interface information
68  */
69 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
70 {
71 	queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
72 			   msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
73 }
74 
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
78  *
79  * If the given soft interface has a bridge on top then the refcount
80  * of the according net device is increased.
81  *
82  * Return: NULL if no such bridge exists. Otherwise the net device of the
83  * bridge.
84  */
85 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
86 {
87 	struct net_device *upper = soft_iface;
88 
89 	rcu_read_lock();
90 	do {
91 		upper = netdev_master_upper_dev_get_rcu(upper);
92 	} while (upper && !(upper->priv_flags & IFF_EBRIDGE));
93 
94 	if (upper)
95 		dev_hold(upper);
96 	rcu_read_unlock();
97 
98 	return upper;
99 }
100 
101 /**
102  * batadv_mcast_mla_rtr_flags_softif_get_ipv4() - get mcast router flags from
103  *  node for IPv4
104  * @dev: the interface to check
105  *
106  * Checks the presence of an IPv4 multicast router on this node.
107  *
108  * Caller needs to hold rcu read lock.
109  *
110  * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
111  */
112 static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv4(struct net_device *dev)
113 {
114 	struct in_device *in_dev = __in_dev_get_rcu(dev);
115 
116 	if (in_dev && IN_DEV_MFORWARD(in_dev))
117 		return BATADV_NO_FLAGS;
118 	else
119 		return BATADV_MCAST_WANT_NO_RTR4;
120 }
121 
122 /**
123  * batadv_mcast_mla_rtr_flags_softif_get_ipv6() - get mcast router flags from
124  *  node for IPv6
125  * @dev: the interface to check
126  *
127  * Checks the presence of an IPv6 multicast router on this node.
128  *
129  * Caller needs to hold rcu read lock.
130  *
131  * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
132  */
133 #if IS_ENABLED(CONFIG_IPV6_MROUTE)
134 static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
135 {
136 	struct inet6_dev *in6_dev = __in6_dev_get(dev);
137 
138 	if (in6_dev && in6_dev->cnf.mc_forwarding)
139 		return BATADV_NO_FLAGS;
140 	else
141 		return BATADV_MCAST_WANT_NO_RTR6;
142 }
143 #else
144 static inline u8
145 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
146 {
147 	return BATADV_MCAST_WANT_NO_RTR6;
148 }
149 #endif
150 
151 /**
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
156  *
157  * Checks the presence of IPv4 and IPv6 multicast routers on this
158  * node.
159  *
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
165  */
166 static u8 batadv_mcast_mla_rtr_flags_softif_get(struct batadv_priv *bat_priv,
167 						struct net_device *bridge)
168 {
169 	struct net_device *dev = bridge ? bridge : bat_priv->soft_iface;
170 	u8 flags = BATADV_NO_FLAGS;
171 
172 	rcu_read_lock();
173 
174 	flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv4(dev);
175 	flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv6(dev);
176 
177 	rcu_read_unlock();
178 
179 	return flags;
180 }
181 
182 /**
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
187  *
188  * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
189  *
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
195  */
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)
199 {
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;
205 
206 	if (!bridge)
207 		return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
208 
209 	/* TODO: ask the bridge if a multicast router is present (the bridge
210 	 * is capable of performing proper RFC4286 multicast multicast router
211 	 * discovery) instead of searching for a ff02::2 listener here
212 	 */
213 	ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
214 	if (ret < 0)
215 		return BATADV_NO_FLAGS;
216 
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
221 		 */
222 		if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
223 		    ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.u.ip6))
224 			flags &= ~BATADV_MCAST_WANT_NO_RTR6;
225 
226 		list_del(&br_ip_entry->list);
227 		kfree(br_ip_entry);
228 	}
229 
230 	return flags;
231 }
232 #else
233 static inline u8
234 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
235 				      struct net_device *bridge)
236 {
237 	if (bridge)
238 		return BATADV_NO_FLAGS;
239 	else
240 		return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
241 }
242 #endif
243 
244 /**
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
249  *
250  * Checks the presence of IPv4 and IPv6 multicast routers on this
251  * node or behind its bridge.
252  *
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
258  */
259 static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
260 					 struct net_device *bridge)
261 {
262 	u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
263 
264 	flags &= batadv_mcast_mla_rtr_flags_softif_get(bat_priv, bridge);
265 	flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
266 
267 	return flags;
268 }
269 
270 /**
271  * batadv_mcast_mla_flags_get() - get the new multicast flags
272  * @bat_priv: the bat priv with all the soft interface information
273  *
274  * Return: A set of flags for the current/next TVLV, querier and
275  * bridge state.
276  */
277 static struct batadv_mcast_mla_flags
278 batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
279 {
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;
284 
285 	bridge = batadv_mcast_get_bridge(dev);
286 
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);
291 
292 	if (!bridge)
293 		return mla_flags;
294 
295 	dev_put(bridge);
296 
297 	mla_flags.bridged = 1;
298 	qr4 = &mla_flags.querier_ipv4;
299 	qr6 = &mla_flags.querier_ipv6;
300 
301 	if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
302 		pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
303 
304 	qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
305 	qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
306 
307 	qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
308 	qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
309 
310 	mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
311 
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.
317 	 *
318 	 * In both cases, we will signalize other batman nodes that
319 	 * we need all multicast traffic of the according protocol.
320 	 */
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;
324 	}
325 
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;
329 	}
330 
331 	return mla_flags;
332 }
333 
334 /**
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
338  *
339  * Return: true if the given address is already in the given list.
340  * Otherwise returns false.
341  */
342 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
343 					  struct hlist_head *mcast_list)
344 {
345 	struct batadv_hw_addr *mcast_entry;
346 
347 	hlist_for_each_entry(mcast_entry, mcast_list, list)
348 		if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
349 			return true;
350 
351 	return false;
352 }
353 
354 /**
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
359  *
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.
364  *
365  * Return: -ENOMEM on memory allocation error or the number of
366  * items added to the mcast_list otherwise.
367  */
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)
372 {
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;
378 
379 	if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
380 		return 0;
381 
382 	rcu_read_lock();
383 
384 	in_dev = __in_dev_get_rcu(dev);
385 	if (!in_dev) {
386 		rcu_read_unlock();
387 		return 0;
388 	}
389 
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;
395 
396 		if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
397 		    !ipv4_is_local_multicast(pmc->multiaddr))
398 			continue;
399 
400 		ip_eth_mc_map(pmc->multiaddr, mcast_addr);
401 
402 		if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
403 			continue;
404 
405 		new = kmalloc(sizeof(*new), GFP_ATOMIC);
406 		if (!new) {
407 			ret = -ENOMEM;
408 			break;
409 		}
410 
411 		ether_addr_copy(new->addr, mcast_addr);
412 		hlist_add_head(&new->list, mcast_list);
413 		ret++;
414 	}
415 	rcu_read_unlock();
416 
417 	return ret;
418 }
419 
420 /**
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
425  *
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.
430  *
431  * Return: -ENOMEM on memory allocation error or the number of
432  * items added to the mcast_list otherwise.
433  */
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)
439 {
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;
445 
446 	if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
447 		return 0;
448 
449 	rcu_read_lock();
450 
451 	in6_dev = __in6_dev_get(dev);
452 	if (!in6_dev) {
453 		rcu_read_unlock();
454 		return 0;
455 	}
456 
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;
462 
463 		if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
464 		    ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
465 			continue;
466 
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;
471 
472 		ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
473 
474 		if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
475 			continue;
476 
477 		new = kmalloc(sizeof(*new), GFP_ATOMIC);
478 		if (!new) {
479 			ret = -ENOMEM;
480 			break;
481 		}
482 
483 		ether_addr_copy(new->addr, mcast_addr);
484 		hlist_add_head(&new->list, mcast_list);
485 		ret++;
486 	}
487 	read_unlock_bh(&in6_dev->lock);
488 	rcu_read_unlock();
489 
490 	return ret;
491 }
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)
497 {
498 	return 0;
499 }
500 #endif
501 
502 /**
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
507  *
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.
512  *
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.
517  *
518  * Return: -ENOMEM on memory allocation error or the number of
519  * items added to the mcast_list otherwise.
520  */
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)
525 {
526 	struct net_device *bridge = batadv_mcast_get_bridge(dev);
527 	int ret4, ret6 = 0;
528 
529 	if (bridge)
530 		dev = bridge;
531 
532 	ret4 = batadv_mcast_mla_softif_get_ipv4(dev, mcast_list, flags);
533 	if (ret4 < 0)
534 		goto out;
535 
536 	ret6 = batadv_mcast_mla_softif_get_ipv6(dev, mcast_list, flags);
537 	if (ret6 < 0) {
538 		ret4 = 0;
539 		goto out;
540 	}
541 
542 out:
543 	if (bridge)
544 		dev_put(bridge);
545 
546 	return ret4 + ret6;
547 }
548 
549 /**
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
553  *
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.
557  *
558  * Caller needs to make sure the destination buffer can hold
559  * at least ETH_ALEN bytes.
560  */
561 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
562 {
563 	if (src->proto == htons(ETH_P_IP))
564 		ip_eth_mc_map(src->u.ip4, dst);
565 #if IS_ENABLED(CONFIG_IPV6)
566 	else if (src->proto == htons(ETH_P_IPV6))
567 		ipv6_eth_mc_map(&src->u.ip6, dst);
568 #endif
569 	else
570 		eth_zero_addr(dst);
571 }
572 
573 /**
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
578  *
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.
583  *
584  * Return: -ENOMEM on memory allocation error or the number of
585  * items added to the mcast_list otherwise.
586  */
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)
590 {
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;
597 
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
600 	 */
601 	ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
602 	if (ret < 0)
603 		goto out;
604 
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;
609 
610 			if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
611 			    ipv4_is_local_multicast(br_ip_entry->addr.u.ip4))
612 				continue;
613 
614 			if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
615 			    !ipv4_is_local_multicast(br_ip_entry->addr.u.ip4))
616 				continue;
617 		}
618 
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;
623 
624 			if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
625 			    ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.u.ip6))
626 				continue;
627 
628 			if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
629 			    IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.u.ip6) >
630 			    IPV6_ADDR_SCOPE_LINKLOCAL)
631 				continue;
632 		}
633 #endif
634 
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;
638 
639 		new = kmalloc(sizeof(*new), GFP_ATOMIC);
640 		if (!new) {
641 			ret = -ENOMEM;
642 			break;
643 		}
644 
645 		ether_addr_copy(new->addr, mcast_addr);
646 		hlist_add_head(&new->list, mcast_list);
647 	}
648 
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);
653 	}
654 
655 	return ret;
656 }
657 
658 /**
659  * batadv_mcast_mla_list_free() - free a list of multicast addresses
660  * @mcast_list: the list to free
661  *
662  * Removes and frees all items in the given mcast_list.
663  */
664 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
665 {
666 	struct batadv_hw_addr *mcast_entry;
667 	struct hlist_node *tmp;
668 
669 	hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
670 		hlist_del(&mcast_entry->list);
671 		kfree(mcast_entry);
672 	}
673 }
674 
675 /**
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
679  *
680  * Retracts the announcement of any multicast listener from the
681  * translation table except the ones listed in the given mcast_list.
682  *
683  * If mcast_list is NULL then all are retracted.
684  */
685 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
686 					struct hlist_head *mcast_list)
687 {
688 	struct batadv_hw_addr *mcast_entry;
689 	struct hlist_node *tmp;
690 
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;
697 
698 		batadv_tt_local_remove(bat_priv, mcast_entry->addr,
699 				       BATADV_NO_FLAGS,
700 				       "mcast TT outdated", false);
701 
702 		hlist_del(&mcast_entry->list);
703 		kfree(mcast_entry);
704 	}
705 }
706 
707 /**
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
711  *
712  * Adds multicast listener announcements from the given mcast_list to the
713  * translation table if they have not been added yet.
714  */
715 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
716 				    struct hlist_head *mcast_list)
717 {
718 	struct batadv_hw_addr *mcast_entry;
719 	struct hlist_node *tmp;
720 
721 	if (!mcast_list)
722 		return;
723 
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;
728 
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;
733 
734 		hlist_del(&mcast_entry->list);
735 		hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
736 	}
737 }
738 
739 /**
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
746  *
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.
750  *
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.
757  *
758  * This is only interesting for nodes with a bridge on top of their
759  * soft interface.
760  */
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)
765 {
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);
777 
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);
788 	}
789 }
790 
791 /**
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
796  *
797  * If no bridges are ever used on this node, then this function does nothing.
798  *
799  * Otherwise this function outputs debug information to the 'mcast' log level
800  * which might be relevant to our multicast optimizations.
801  *
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.
806  */
807 static void
808 batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
809 			struct batadv_mcast_mla_flags *new_flags)
810 {
811 	struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
812 
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");
819 
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);
827 	}
828 }
829 
830 /**
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
834  *
835  * Whenever the multicast TVLV flags this node announces change, this function
836  * should be used to notify userspace about the change.
837  */
838 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
839 {
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[] = "[.... . ]";
843 
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" : ". ");
850 
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" : ". ");
859 }
860 
861 /**
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
865  *
866  * Updates the own multicast tvlv with our current multicast related settings,
867  * capabilities and inabilities.
868  */
869 static void
870 batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
871 			      struct batadv_mcast_mla_flags *flags)
872 {
873 	struct batadv_tvlv_mcast_data mcast_data;
874 
875 	if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
876 		return;
877 
878 	batadv_mcast_bridge_log(bat_priv, flags);
879 	batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
880 
881 	mcast_data.flags = flags->tvlv_flags;
882 	memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
883 
884 	batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
885 				       &mcast_data, sizeof(mcast_data));
886 
887 	bat_priv->mcast.mla_flags = *flags;
888 }
889 
890 /**
891  * __batadv_mcast_mla_update() - update the own MLAs
892  * @bat_priv: the bat priv with all the soft interface information
893  *
894  * Updates the own multicast listener announcements in the translation
895  * table as well as the own, announced multicast tvlv container.
896  *
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.
901  */
902 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
903 {
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;
908 
909 	flags = batadv_mcast_mla_flags_get(bat_priv);
910 
911 	ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list, &flags);
912 	if (ret < 0)
913 		goto out;
914 
915 	ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list, &flags);
916 	if (ret < 0)
917 		goto out;
918 
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);
924 
925 out:
926 	batadv_mcast_mla_list_free(&mcast_list);
927 }
928 
929 /**
930  * batadv_mcast_mla_update() - update the own MLAs
931  * @work: kernel work struct
932  *
933  * Updates the own multicast listener announcements in the translation
934  * table as well as the own, announced multicast tvlv container.
935  *
936  * In the end, reschedules the work timer.
937  */
938 static void batadv_mcast_mla_update(struct work_struct *work)
939 {
940 	struct delayed_work *delayed_work;
941 	struct batadv_priv_mcast *priv_mcast;
942 	struct batadv_priv *bat_priv;
943 
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);
947 
948 	__batadv_mcast_mla_update(bat_priv);
949 	batadv_mcast_start_timer(bat_priv);
950 }
951 
952 /**
953  * batadv_mcast_is_report_ipv4() - check for IGMP reports
954  * @skb: the ethernet frame destined for the mesh
955  *
956  * This call might reallocate skb data.
957  *
958  * Checks whether the given frame is a valid IGMP report.
959  *
960  * Return: If so then true, otherwise false.
961  */
962 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
963 {
964 	if (ip_mc_check_igmp(skb) < 0)
965 		return false;
966 
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;
972 	}
973 
974 	return false;
975 }
976 
977 /**
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
984  *
985  * Checks whether the given IPv4 packet has the potential to be forwarded with a
986  * mode more optimal than classic flooding.
987  *
988  * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
989  * allocation failure.
990  */
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)
995 {
996 	struct iphdr *iphdr;
997 
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;
1001 
1002 	if (batadv_mcast_is_report_ipv4(skb))
1003 		return -EINVAL;
1004 
1005 	iphdr = ip_hdr(skb);
1006 
1007 	/* link-local multicast listeners behind a bridge are
1008 	 * not snoopable (see RFC4541, section 2.1.2.2)
1009 	 */
1010 	if (ipv4_is_local_multicast(iphdr->daddr))
1011 		*is_unsnoopable = true;
1012 	else
1013 		*is_routable = ETH_P_IP;
1014 
1015 	return 0;
1016 }
1017 
1018 /**
1019  * batadv_mcast_is_report_ipv6() - check for MLD reports
1020  * @skb: the ethernet frame destined for the mesh
1021  *
1022  * This call might reallocate skb data.
1023  *
1024  * Checks whether the given frame is a valid MLD report.
1025  *
1026  * Return: If so then true, otherwise false.
1027  */
1028 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
1029 {
1030 	if (ipv6_mc_check_mld(skb) < 0)
1031 		return false;
1032 
1033 	switch (icmp6_hdr(skb)->icmp6_type) {
1034 	case ICMPV6_MGM_REPORT:
1035 	case ICMPV6_MLD2_REPORT:
1036 		return true;
1037 	}
1038 
1039 	return false;
1040 }
1041 
1042 /**
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
1049  *
1050  * Checks whether the given IPv6 packet has the potential to be forwarded with a
1051  * mode more optimal than classic flooding.
1052  *
1053  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1054  */
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)
1059 {
1060 	struct ipv6hdr *ip6hdr;
1061 
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;
1065 
1066 	if (batadv_mcast_is_report_ipv6(skb))
1067 		return -EINVAL;
1068 
1069 	ip6hdr = ipv6_hdr(skb);
1070 
1071 	if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1072 		return -EINVAL;
1073 
1074 	/* link-local-all-nodes multicast listeners behind a bridge are
1075 	 * not snoopable (see RFC4541, section 3, paragraph 3)
1076 	 */
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;
1081 
1082 	return 0;
1083 }
1084 
1085 /**
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
1091  *
1092  * Checks whether the given multicast ethernet frame has the potential to be
1093  * forwarded with a mode more optimal than classic flooding.
1094  *
1095  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1096  */
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)
1101 {
1102 	struct ethhdr *ethhdr = eth_hdr(skb);
1103 
1104 	if (!atomic_read(&bat_priv->multicast_mode))
1105 		return -EINVAL;
1106 
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;
1115 
1116 		return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
1117 							 is_unsnoopable,
1118 							 is_routable);
1119 	default:
1120 		return -EINVAL;
1121 	}
1122 }
1123 
1124 /**
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
1129  *
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.
1133  */
1134 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
1135 					       struct ethhdr *ethhdr)
1136 {
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;
1145 	}
1146 }
1147 
1148 /**
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
1152  *
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.
1156  */
1157 
1158 static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
1159 				       int protocol)
1160 {
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;
1168 	}
1169 }
1170 
1171 /**
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
1175  *
1176  * Return: an orig_node matching the multicast address provided by ethhdr
1177  * via a translation table lookup. This increases the returned nodes refcount.
1178  */
1179 static struct batadv_orig_node *
1180 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
1181 			      struct ethhdr *ethhdr)
1182 {
1183 	return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
1184 					BATADV_NO_FLAGS);
1185 }
1186 
1187 /**
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
1190  *
1191  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
1192  * increases its refcount.
1193  */
1194 static struct batadv_orig_node *
1195 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
1196 {
1197 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1198 
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;
1205 
1206 		orig_node = tmp_orig_node;
1207 		break;
1208 	}
1209 	rcu_read_unlock();
1210 
1211 	return orig_node;
1212 }
1213 
1214 /**
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
1217  *
1218  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
1219  * and increases its refcount.
1220  */
1221 static struct batadv_orig_node *
1222 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
1223 {
1224 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1225 
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;
1232 
1233 		orig_node = tmp_orig_node;
1234 		break;
1235 	}
1236 	rcu_read_unlock();
1237 
1238 	return orig_node;
1239 }
1240 
1241 /**
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
1245  *
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.
1249  */
1250 static struct batadv_orig_node *
1251 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
1252 			      struct ethhdr *ethhdr)
1253 {
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;
1262 	}
1263 }
1264 
1265 /**
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
1268  *
1269  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
1270  * set and increases its refcount.
1271  */
1272 static struct batadv_orig_node *
1273 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
1274 {
1275 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1276 
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;
1283 
1284 		orig_node = tmp_orig_node;
1285 		break;
1286 	}
1287 	rcu_read_unlock();
1288 
1289 	return orig_node;
1290 }
1291 
1292 /**
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
1295  *
1296  * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR4 flag unset and
1297  * increases its refcount.
1298  */
1299 static struct batadv_orig_node *
1300 batadv_mcast_forw_rtr4_node_get(struct batadv_priv *bat_priv)
1301 {
1302 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1303 
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;
1310 
1311 		orig_node = tmp_orig_node;
1312 		break;
1313 	}
1314 	rcu_read_unlock();
1315 
1316 	return orig_node;
1317 }
1318 
1319 /**
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
1322  *
1323  * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR6 flag unset
1324  * and increases its refcount.
1325  */
1326 static struct batadv_orig_node *
1327 batadv_mcast_forw_rtr6_node_get(struct batadv_priv *bat_priv)
1328 {
1329 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1330 
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;
1337 
1338 		orig_node = tmp_orig_node;
1339 		break;
1340 	}
1341 	rcu_read_unlock();
1342 
1343 	return orig_node;
1344 }
1345 
1346 /**
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
1350  *
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.
1354  */
1355 static struct batadv_orig_node *
1356 batadv_mcast_forw_rtr_node_get(struct batadv_priv *bat_priv,
1357 			       struct ethhdr *ethhdr)
1358 {
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;
1367 	}
1368 }
1369 
1370 /**
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
1375  *
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.
1379  */
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)
1383 {
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;
1390 
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;
1397 
1398 	ethhdr = eth_hdr(skb);
1399 
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);
1406 
1407 	total_count = tt_count + ip_count + unsnoop_count + rtr_count;
1408 
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);
1420 
1421 		if (*orig)
1422 			return BATADV_FORW_SINGLE;
1423 
1424 		fallthrough;
1425 	case 0:
1426 		return BATADV_FORW_NONE;
1427 	default:
1428 		mcast_fanout = atomic_read(&bat_priv->multicast_fanout);
1429 
1430 		if (!unsnoop_count && total_count <= mcast_fanout)
1431 			return BATADV_FORW_SOME;
1432 	}
1433 
1434 	return BATADV_FORW_ALL;
1435 }
1436 
1437 /**
1438  * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1439  * @bat_priv: the bat priv with all the soft interface information
1440  * @skb: the multicast packet to transmit
1441  * @vid: the vlan identifier
1442  *
1443  * Sends copies of a frame with multicast destination to any multicast
1444  * listener registered in the translation table. A transmission is performed
1445  * via a batman-adv unicast packet for each such destination node.
1446  *
1447  * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1448  * otherwise.
1449  */
1450 static int
1451 batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
1452 		     unsigned short vid)
1453 {
1454 	int ret = NET_XMIT_SUCCESS;
1455 	struct sk_buff *newskb;
1456 
1457 	struct batadv_tt_orig_list_entry *orig_entry;
1458 
1459 	struct batadv_tt_global_entry *tt_global;
1460 	const u8 *addr = eth_hdr(skb)->h_dest;
1461 
1462 	tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
1463 	if (!tt_global)
1464 		goto out;
1465 
1466 	rcu_read_lock();
1467 	hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
1468 		newskb = skb_copy(skb, GFP_ATOMIC);
1469 		if (!newskb) {
1470 			ret = NET_XMIT_DROP;
1471 			break;
1472 		}
1473 
1474 		batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1475 					orig_entry->orig_node, vid);
1476 	}
1477 	rcu_read_unlock();
1478 
1479 	batadv_tt_global_entry_put(tt_global);
1480 
1481 out:
1482 	return ret;
1483 }
1484 
1485 /**
1486  * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1487  * @bat_priv: the bat priv with all the soft interface information
1488  * @skb: the multicast packet to transmit
1489  * @vid: the vlan identifier
1490  *
1491  * Sends copies of a frame with multicast destination to any node with a
1492  * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1493  * batman-adv unicast packet for each such destination node.
1494  *
1495  * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1496  * otherwise.
1497  */
1498 static int
1499 batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
1500 				struct sk_buff *skb, unsigned short vid)
1501 {
1502 	struct batadv_orig_node *orig_node;
1503 	int ret = NET_XMIT_SUCCESS;
1504 	struct sk_buff *newskb;
1505 
1506 	rcu_read_lock();
1507 	hlist_for_each_entry_rcu(orig_node,
1508 				 &bat_priv->mcast.want_all_ipv4_list,
1509 				 mcast_want_all_ipv4_node) {
1510 		newskb = skb_copy(skb, GFP_ATOMIC);
1511 		if (!newskb) {
1512 			ret = NET_XMIT_DROP;
1513 			break;
1514 		}
1515 
1516 		batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1517 					orig_node, vid);
1518 	}
1519 	rcu_read_unlock();
1520 	return ret;
1521 }
1522 
1523 /**
1524  * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1525  * @bat_priv: the bat priv with all the soft interface information
1526  * @skb: The multicast packet to transmit
1527  * @vid: the vlan identifier
1528  *
1529  * Sends copies of a frame with multicast destination to any node with a
1530  * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1531  * batman-adv unicast packet for each such destination node.
1532  *
1533  * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1534  * otherwise.
1535  */
1536 static int
1537 batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
1538 				struct sk_buff *skb, unsigned short vid)
1539 {
1540 	struct batadv_orig_node *orig_node;
1541 	int ret = NET_XMIT_SUCCESS;
1542 	struct sk_buff *newskb;
1543 
1544 	rcu_read_lock();
1545 	hlist_for_each_entry_rcu(orig_node,
1546 				 &bat_priv->mcast.want_all_ipv6_list,
1547 				 mcast_want_all_ipv6_node) {
1548 		newskb = skb_copy(skb, GFP_ATOMIC);
1549 		if (!newskb) {
1550 			ret = NET_XMIT_DROP;
1551 			break;
1552 		}
1553 
1554 		batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1555 					orig_node, vid);
1556 	}
1557 	rcu_read_unlock();
1558 	return ret;
1559 }
1560 
1561 /**
1562  * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1563  * @bat_priv: the bat priv with all the soft interface information
1564  * @skb: the multicast packet to transmit
1565  * @vid: the vlan identifier
1566  *
1567  * Sends copies of a frame with multicast destination to any node with a
1568  * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1569  * transmission is performed via a batman-adv unicast packet for each such
1570  * destination node.
1571  *
1572  * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1573  * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1574  */
1575 static int
1576 batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
1577 			   struct sk_buff *skb, unsigned short vid)
1578 {
1579 	switch (ntohs(eth_hdr(skb)->h_proto)) {
1580 	case ETH_P_IP:
1581 		return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
1582 	case ETH_P_IPV6:
1583 		return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
1584 	default:
1585 		/* we shouldn't be here... */
1586 		return NET_XMIT_DROP;
1587 	}
1588 }
1589 
1590 /**
1591  * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
1592  * @bat_priv: the bat priv with all the soft interface information
1593  * @skb: the multicast packet to transmit
1594  * @vid: the vlan identifier
1595  *
1596  * Sends copies of a frame with multicast destination to any node with a
1597  * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
1598  * batman-adv unicast packet for each such destination node.
1599  *
1600  * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1601  * otherwise.
1602  */
1603 static int
1604 batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
1605 				struct sk_buff *skb, unsigned short vid)
1606 {
1607 	struct batadv_orig_node *orig_node;
1608 	int ret = NET_XMIT_SUCCESS;
1609 	struct sk_buff *newskb;
1610 
1611 	rcu_read_lock();
1612 	hlist_for_each_entry_rcu(orig_node,
1613 				 &bat_priv->mcast.want_all_rtr4_list,
1614 				 mcast_want_all_rtr4_node) {
1615 		newskb = skb_copy(skb, GFP_ATOMIC);
1616 		if (!newskb) {
1617 			ret = NET_XMIT_DROP;
1618 			break;
1619 		}
1620 
1621 		batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1622 					orig_node, vid);
1623 	}
1624 	rcu_read_unlock();
1625 	return ret;
1626 }
1627 
1628 /**
1629  * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
1630  * @bat_priv: the bat priv with all the soft interface information
1631  * @skb: The multicast packet to transmit
1632  * @vid: the vlan identifier
1633  *
1634  * Sends copies of a frame with multicast destination to any node with a
1635  * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
1636  * batman-adv unicast packet for each such destination node.
1637  *
1638  * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1639  * otherwise.
1640  */
1641 static int
1642 batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
1643 				struct sk_buff *skb, unsigned short vid)
1644 {
1645 	struct batadv_orig_node *orig_node;
1646 	int ret = NET_XMIT_SUCCESS;
1647 	struct sk_buff *newskb;
1648 
1649 	rcu_read_lock();
1650 	hlist_for_each_entry_rcu(orig_node,
1651 				 &bat_priv->mcast.want_all_rtr6_list,
1652 				 mcast_want_all_rtr6_node) {
1653 		newskb = skb_copy(skb, GFP_ATOMIC);
1654 		if (!newskb) {
1655 			ret = NET_XMIT_DROP;
1656 			break;
1657 		}
1658 
1659 		batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1660 					orig_node, vid);
1661 	}
1662 	rcu_read_unlock();
1663 	return ret;
1664 }
1665 
1666 /**
1667  * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
1668  * @bat_priv: the bat priv with all the soft interface information
1669  * @skb: the multicast packet to transmit
1670  * @vid: the vlan identifier
1671  *
1672  * Sends copies of a frame with multicast destination to any node with a
1673  * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
1674  * transmission is performed via a batman-adv unicast packet for each such
1675  * destination node.
1676  *
1677  * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1678  * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1679  */
1680 static int
1681 batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
1682 			   struct sk_buff *skb, unsigned short vid)
1683 {
1684 	switch (ntohs(eth_hdr(skb)->h_proto)) {
1685 	case ETH_P_IP:
1686 		return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
1687 	case ETH_P_IPV6:
1688 		return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
1689 	default:
1690 		/* we shouldn't be here... */
1691 		return NET_XMIT_DROP;
1692 	}
1693 }
1694 
1695 /**
1696  * batadv_mcast_forw_send() - send packet to any detected multicast recipient
1697  * @bat_priv: the bat priv with all the soft interface information
1698  * @skb: the multicast packet to transmit
1699  * @vid: the vlan identifier
1700  *
1701  * Sends copies of a frame with multicast destination to any node that signaled
1702  * interest in it, that is either via the translation table or the according
1703  * want-all flags. A transmission is performed via a batman-adv unicast packet
1704  * for each such destination node.
1705  *
1706  * The given skb is consumed/freed.
1707  *
1708  * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1709  * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1710  */
1711 int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
1712 			   unsigned short vid)
1713 {
1714 	int ret;
1715 
1716 	ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
1717 	if (ret != NET_XMIT_SUCCESS) {
1718 		kfree_skb(skb);
1719 		return ret;
1720 	}
1721 
1722 	ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
1723 	if (ret != NET_XMIT_SUCCESS) {
1724 		kfree_skb(skb);
1725 		return ret;
1726 	}
1727 
1728 	ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
1729 	if (ret != NET_XMIT_SUCCESS) {
1730 		kfree_skb(skb);
1731 		return ret;
1732 	}
1733 
1734 	consume_skb(skb);
1735 	return ret;
1736 }
1737 
1738 /**
1739  * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1740  * @bat_priv: the bat priv with all the soft interface information
1741  * @orig: the orig_node which multicast state might have changed of
1742  * @mcast_flags: flags indicating the new multicast state
1743  *
1744  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1745  * orig, has toggled then this method updates the counter and the list
1746  * accordingly.
1747  *
1748  * Caller needs to hold orig->mcast_handler_lock.
1749  */
1750 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1751 					     struct batadv_orig_node *orig,
1752 					     u8 mcast_flags)
1753 {
1754 	struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1755 	struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1756 
1757 	lockdep_assert_held(&orig->mcast_handler_lock);
1758 
1759 	/* switched from flag unset to set */
1760 	if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1761 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1762 		atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1763 
1764 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1765 		/* flag checks above + mcast_handler_lock prevents this */
1766 		WARN_ON(!hlist_unhashed(node));
1767 
1768 		hlist_add_head_rcu(node, head);
1769 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1770 	/* switched from flag set to unset */
1771 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1772 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1773 		atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1774 
1775 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1776 		/* flag checks above + mcast_handler_lock prevents this */
1777 		WARN_ON(hlist_unhashed(node));
1778 
1779 		hlist_del_init_rcu(node);
1780 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1781 	}
1782 }
1783 
1784 /**
1785  * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1786  * @bat_priv: the bat priv with all the soft interface information
1787  * @orig: the orig_node which multicast state might have changed of
1788  * @mcast_flags: flags indicating the new multicast state
1789  *
1790  * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1791  * toggled then this method updates the counter and the list accordingly.
1792  *
1793  * Caller needs to hold orig->mcast_handler_lock.
1794  */
1795 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1796 					  struct batadv_orig_node *orig,
1797 					  u8 mcast_flags)
1798 {
1799 	struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1800 	struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1801 
1802 	lockdep_assert_held(&orig->mcast_handler_lock);
1803 
1804 	/* switched from flag unset to set */
1805 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1806 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1807 		atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1808 
1809 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1810 		/* flag checks above + mcast_handler_lock prevents this */
1811 		WARN_ON(!hlist_unhashed(node));
1812 
1813 		hlist_add_head_rcu(node, head);
1814 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1815 	/* switched from flag set to unset */
1816 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1817 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1818 		atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1819 
1820 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1821 		/* flag checks above + mcast_handler_lock prevents this */
1822 		WARN_ON(hlist_unhashed(node));
1823 
1824 		hlist_del_init_rcu(node);
1825 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1826 	}
1827 }
1828 
1829 /**
1830  * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1831  * @bat_priv: the bat priv with all the soft interface information
1832  * @orig: the orig_node which multicast state might have changed of
1833  * @mcast_flags: flags indicating the new multicast state
1834  *
1835  * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1836  * toggled then this method updates the counter and the list accordingly.
1837  *
1838  * Caller needs to hold orig->mcast_handler_lock.
1839  */
1840 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1841 					  struct batadv_orig_node *orig,
1842 					  u8 mcast_flags)
1843 {
1844 	struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1845 	struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1846 
1847 	lockdep_assert_held(&orig->mcast_handler_lock);
1848 
1849 	/* switched from flag unset to set */
1850 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1851 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1852 		atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1853 
1854 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1855 		/* flag checks above + mcast_handler_lock prevents this */
1856 		WARN_ON(!hlist_unhashed(node));
1857 
1858 		hlist_add_head_rcu(node, head);
1859 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1860 	/* switched from flag set to unset */
1861 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1862 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1863 		atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1864 
1865 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1866 		/* flag checks above + mcast_handler_lock prevents this */
1867 		WARN_ON(hlist_unhashed(node));
1868 
1869 		hlist_del_init_rcu(node);
1870 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1871 	}
1872 }
1873 
1874 /**
1875  * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
1876  * @bat_priv: the bat priv with all the soft interface information
1877  * @orig: the orig_node which multicast state might have changed of
1878  * @mcast_flags: flags indicating the new multicast state
1879  *
1880  * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
1881  * toggled then this method updates the counter and the list accordingly.
1882  *
1883  * Caller needs to hold orig->mcast_handler_lock.
1884  */
1885 static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
1886 					  struct batadv_orig_node *orig,
1887 					  u8 mcast_flags)
1888 {
1889 	struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
1890 	struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
1891 
1892 	lockdep_assert_held(&orig->mcast_handler_lock);
1893 
1894 	/* switched from flag set to unset */
1895 	if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
1896 	    orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
1897 		atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
1898 
1899 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1900 		/* flag checks above + mcast_handler_lock prevents this */
1901 		WARN_ON(!hlist_unhashed(node));
1902 
1903 		hlist_add_head_rcu(node, head);
1904 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1905 	/* switched from flag unset to set */
1906 	} else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
1907 		   !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
1908 		atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
1909 
1910 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1911 		/* flag checks above + mcast_handler_lock prevents this */
1912 		WARN_ON(hlist_unhashed(node));
1913 
1914 		hlist_del_init_rcu(node);
1915 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1916 	}
1917 }
1918 
1919 /**
1920  * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
1921  * @bat_priv: the bat priv with all the soft interface information
1922  * @orig: the orig_node which multicast state might have changed of
1923  * @mcast_flags: flags indicating the new multicast state
1924  *
1925  * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
1926  * toggled then this method updates the counter and the list accordingly.
1927  *
1928  * Caller needs to hold orig->mcast_handler_lock.
1929  */
1930 static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
1931 					  struct batadv_orig_node *orig,
1932 					  u8 mcast_flags)
1933 {
1934 	struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
1935 	struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
1936 
1937 	lockdep_assert_held(&orig->mcast_handler_lock);
1938 
1939 	/* switched from flag set to unset */
1940 	if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
1941 	    orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
1942 		atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
1943 
1944 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1945 		/* flag checks above + mcast_handler_lock prevents this */
1946 		WARN_ON(!hlist_unhashed(node));
1947 
1948 		hlist_add_head_rcu(node, head);
1949 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1950 	/* switched from flag unset to set */
1951 	} else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
1952 		   !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
1953 		atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
1954 
1955 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1956 		/* flag checks above + mcast_handler_lock prevents this */
1957 		WARN_ON(hlist_unhashed(node));
1958 
1959 		hlist_del_init_rcu(node);
1960 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1961 	}
1962 }
1963 
1964 /**
1965  * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
1966  * @enabled: whether the originator has multicast TVLV support enabled
1967  * @tvlv_value: tvlv buffer containing the multicast flags
1968  * @tvlv_value_len: tvlv buffer length
1969  *
1970  * Return: multicast flags for the given tvlv buffer
1971  */
1972 static u8
1973 batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
1974 {
1975 	u8 mcast_flags = BATADV_NO_FLAGS;
1976 
1977 	if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
1978 		mcast_flags = *(u8 *)tvlv_value;
1979 
1980 	if (!enabled) {
1981 		mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1982 		mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1983 	}
1984 
1985 	/* remove redundant flags to avoid sending duplicate packets later */
1986 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
1987 		mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
1988 
1989 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
1990 		mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
1991 
1992 	return mcast_flags;
1993 }
1994 
1995 /**
1996  * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1997  * @bat_priv: the bat priv with all the soft interface information
1998  * @orig: the orig_node of the ogm
1999  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2000  * @tvlv_value: tvlv buffer containing the multicast data
2001  * @tvlv_value_len: tvlv buffer length
2002  */
2003 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
2004 					  struct batadv_orig_node *orig,
2005 					  u8 flags,
2006 					  void *tvlv_value,
2007 					  u16 tvlv_value_len)
2008 {
2009 	bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2010 	u8 mcast_flags;
2011 
2012 	mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
2013 						  tvlv_value, tvlv_value_len);
2014 
2015 	spin_lock_bh(&orig->mcast_handler_lock);
2016 
2017 	if (orig_mcast_enabled &&
2018 	    !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2019 		set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2020 	} else if (!orig_mcast_enabled &&
2021 		   test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2022 		clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2023 	}
2024 
2025 	set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
2026 
2027 	batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
2028 	batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
2029 	batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
2030 	batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
2031 	batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
2032 
2033 	orig->mcast_flags = mcast_flags;
2034 	spin_unlock_bh(&orig->mcast_handler_lock);
2035 }
2036 
2037 /**
2038  * batadv_mcast_init() - initialize the multicast optimizations structures
2039  * @bat_priv: the bat priv with all the soft interface information
2040  */
2041 void batadv_mcast_init(struct batadv_priv *bat_priv)
2042 {
2043 	batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
2044 				     NULL, BATADV_TVLV_MCAST, 2,
2045 				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2046 
2047 	INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
2048 	batadv_mcast_start_timer(bat_priv);
2049 }
2050 
2051 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2052 /**
2053  * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
2054  * @bat_priv: the bat priv with all the soft interface information
2055  * @seq: debugfs table seq_file struct
2056  *
2057  * Prints our own multicast flags including a more specific reason why
2058  * they are set, that is prints the bridge and querier state too, to
2059  * the debugfs table specified via @seq.
2060  */
2061 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
2062 					    struct seq_file *seq)
2063 {
2064 	struct batadv_mcast_mla_flags *mla_flags = &bat_priv->mcast.mla_flags;
2065 	char querier4, querier6, shadowing4, shadowing6;
2066 	bool bridged = mla_flags->bridged;
2067 	u8 flags = mla_flags->tvlv_flags;
2068 
2069 	if (bridged) {
2070 		querier4 = mla_flags->querier_ipv4.exists ? '.' : '4';
2071 		querier6 = mla_flags->querier_ipv6.exists ? '.' : '6';
2072 		shadowing4 = mla_flags->querier_ipv4.shadowing ? '4' : '.';
2073 		shadowing6 = mla_flags->querier_ipv6.shadowing ? '6' : '.';
2074 	} else {
2075 		querier4 = '?';
2076 		querier6 = '?';
2077 		shadowing4 = '?';
2078 		shadowing6 = '?';
2079 	}
2080 
2081 	seq_printf(seq, "Multicast flags (own flags: [%c%c%c%s%s])\n",
2082 		   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
2083 		   (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
2084 		   (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
2085 		   !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
2086 		   !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
2087 	seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
2088 	seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
2089 		   querier4, querier6);
2090 	seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
2091 		   shadowing4, shadowing6);
2092 	seq_puts(seq, "-------------------------------------------\n");
2093 	seq_printf(seq, "       %-10s %s\n", "Originator", "Flags");
2094 }
2095 
2096 /**
2097  * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
2098  * @seq: seq file to print on
2099  * @offset: not used
2100  *
2101  * This prints a table of (primary) originators and their according
2102  * multicast flags, including (in the header) our own.
2103  *
2104  * Return: always 0
2105  */
2106 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
2107 {
2108 	struct net_device *net_dev = (struct net_device *)seq->private;
2109 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
2110 	struct batadv_hard_iface *primary_if;
2111 	struct batadv_hashtable *hash = bat_priv->orig_hash;
2112 	struct batadv_orig_node *orig_node;
2113 	struct hlist_head *head;
2114 	u8 flags;
2115 	u32 i;
2116 
2117 	primary_if = batadv_seq_print_text_primary_if_get(seq);
2118 	if (!primary_if)
2119 		return 0;
2120 
2121 	batadv_mcast_flags_print_header(bat_priv, seq);
2122 
2123 	for (i = 0; i < hash->size; i++) {
2124 		head = &hash->table[i];
2125 
2126 		rcu_read_lock();
2127 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2128 			if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2129 				      &orig_node->capa_initialized))
2130 				continue;
2131 
2132 			if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2133 				      &orig_node->capabilities)) {
2134 				seq_printf(seq, "%pM -\n", orig_node->orig);
2135 				continue;
2136 			}
2137 
2138 			flags = orig_node->mcast_flags;
2139 
2140 			seq_printf(seq, "%pM [%c%c%c%s%s]\n", orig_node->orig,
2141 				   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
2142 				   ? 'U' : '.',
2143 				   (flags & BATADV_MCAST_WANT_ALL_IPV4)
2144 				   ? '4' : '.',
2145 				   (flags & BATADV_MCAST_WANT_ALL_IPV6)
2146 				   ? '6' : '.',
2147 				   !(flags & BATADV_MCAST_WANT_NO_RTR4)
2148 				   ? "R4" : ". ",
2149 				   !(flags & BATADV_MCAST_WANT_NO_RTR6)
2150 				   ? "R6" : ". ");
2151 		}
2152 		rcu_read_unlock();
2153 	}
2154 
2155 	batadv_hardif_put(primary_if);
2156 
2157 	return 0;
2158 }
2159 #endif
2160 
2161 /**
2162  * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
2163  * @msg: buffer for the message
2164  * @bat_priv: the bat priv with all the soft interface information
2165  *
2166  * Return: 0 or error code.
2167  */
2168 int batadv_mcast_mesh_info_put(struct sk_buff *msg,
2169 			       struct batadv_priv *bat_priv)
2170 {
2171 	u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
2172 	u32 flags_priv = BATADV_NO_FLAGS;
2173 
2174 	if (bat_priv->mcast.mla_flags.bridged) {
2175 		flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
2176 
2177 		if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
2178 			flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
2179 		if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
2180 			flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
2181 		if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
2182 			flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
2183 		if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
2184 			flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
2185 	}
2186 
2187 	if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
2188 	    nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
2189 		return -EMSGSIZE;
2190 
2191 	return 0;
2192 }
2193 
2194 /**
2195  * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
2196  *  to a netlink socket
2197  * @msg: buffer for the message
2198  * @portid: netlink port
2199  * @cb: Control block containing additional options
2200  * @orig_node: originator to dump the multicast flags of
2201  *
2202  * Return: 0 or error code.
2203  */
2204 static int
2205 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
2206 			      struct netlink_callback *cb,
2207 			      struct batadv_orig_node *orig_node)
2208 {
2209 	void *hdr;
2210 
2211 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2212 			  &batadv_netlink_family, NLM_F_MULTI,
2213 			  BATADV_CMD_GET_MCAST_FLAGS);
2214 	if (!hdr)
2215 		return -ENOBUFS;
2216 
2217 	genl_dump_check_consistent(cb, hdr);
2218 
2219 	if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2220 		    orig_node->orig)) {
2221 		genlmsg_cancel(msg, hdr);
2222 		return -EMSGSIZE;
2223 	}
2224 
2225 	if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2226 		     &orig_node->capabilities)) {
2227 		if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
2228 				orig_node->mcast_flags)) {
2229 			genlmsg_cancel(msg, hdr);
2230 			return -EMSGSIZE;
2231 		}
2232 	}
2233 
2234 	genlmsg_end(msg, hdr);
2235 	return 0;
2236 }
2237 
2238 /**
2239  * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
2240  *  table to a netlink socket
2241  * @msg: buffer for the message
2242  * @portid: netlink port
2243  * @cb: Control block containing additional options
2244  * @hash: hash to dump
2245  * @bucket: bucket index to dump
2246  * @idx_skip: How many entries to skip
2247  *
2248  * Return: 0 or error code.
2249  */
2250 static int
2251 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
2252 			       struct netlink_callback *cb,
2253 			       struct batadv_hashtable *hash,
2254 			       unsigned int bucket, long *idx_skip)
2255 {
2256 	struct batadv_orig_node *orig_node;
2257 	long idx = 0;
2258 
2259 	spin_lock_bh(&hash->list_locks[bucket]);
2260 	cb->seq = atomic_read(&hash->generation) << 1 | 1;
2261 
2262 	hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
2263 		if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2264 			      &orig_node->capa_initialized))
2265 			continue;
2266 
2267 		if (idx < *idx_skip)
2268 			goto skip;
2269 
2270 		if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
2271 			spin_unlock_bh(&hash->list_locks[bucket]);
2272 			*idx_skip = idx;
2273 
2274 			return -EMSGSIZE;
2275 		}
2276 
2277 skip:
2278 		idx++;
2279 	}
2280 	spin_unlock_bh(&hash->list_locks[bucket]);
2281 
2282 	return 0;
2283 }
2284 
2285 /**
2286  * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2287  * @msg: buffer for the message
2288  * @portid: netlink port
2289  * @cb: Control block containing additional options
2290  * @bat_priv: the bat priv with all the soft interface information
2291  * @bucket: current bucket to dump
2292  * @idx: index in current bucket to the next entry to dump
2293  *
2294  * Return: 0 or error code.
2295  */
2296 static int
2297 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
2298 			  struct netlink_callback *cb,
2299 			  struct batadv_priv *bat_priv, long *bucket, long *idx)
2300 {
2301 	struct batadv_hashtable *hash = bat_priv->orig_hash;
2302 	long bucket_tmp = *bucket;
2303 	long idx_tmp = *idx;
2304 
2305 	while (bucket_tmp < hash->size) {
2306 		if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
2307 						   bucket_tmp, &idx_tmp))
2308 			break;
2309 
2310 		bucket_tmp++;
2311 		idx_tmp = 0;
2312 	}
2313 
2314 	*bucket = bucket_tmp;
2315 	*idx = idx_tmp;
2316 
2317 	return msg->len;
2318 }
2319 
2320 /**
2321  * batadv_mcast_netlink_get_primary() - get primary interface from netlink
2322  *  callback
2323  * @cb: netlink callback structure
2324  * @primary_if: the primary interface pointer to return the result in
2325  *
2326  * Return: 0 or error code.
2327  */
2328 static int
2329 batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
2330 				 struct batadv_hard_iface **primary_if)
2331 {
2332 	struct batadv_hard_iface *hard_iface = NULL;
2333 	struct net *net = sock_net(cb->skb->sk);
2334 	struct net_device *soft_iface;
2335 	struct batadv_priv *bat_priv;
2336 	int ifindex;
2337 	int ret = 0;
2338 
2339 	ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2340 	if (!ifindex)
2341 		return -EINVAL;
2342 
2343 	soft_iface = dev_get_by_index(net, ifindex);
2344 	if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2345 		ret = -ENODEV;
2346 		goto out;
2347 	}
2348 
2349 	bat_priv = netdev_priv(soft_iface);
2350 
2351 	hard_iface = batadv_primary_if_get_selected(bat_priv);
2352 	if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
2353 		ret = -ENOENT;
2354 		goto out;
2355 	}
2356 
2357 out:
2358 	if (soft_iface)
2359 		dev_put(soft_iface);
2360 
2361 	if (!ret && primary_if)
2362 		*primary_if = hard_iface;
2363 	else if (hard_iface)
2364 		batadv_hardif_put(hard_iface);
2365 
2366 	return ret;
2367 }
2368 
2369 /**
2370  * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2371  * @msg: buffer for the message
2372  * @cb: callback structure containing arguments
2373  *
2374  * Return: message length.
2375  */
2376 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
2377 {
2378 	struct batadv_hard_iface *primary_if = NULL;
2379 	int portid = NETLINK_CB(cb->skb).portid;
2380 	struct batadv_priv *bat_priv;
2381 	long *bucket = &cb->args[0];
2382 	long *idx = &cb->args[1];
2383 	int ret;
2384 
2385 	ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
2386 	if (ret)
2387 		return ret;
2388 
2389 	bat_priv = netdev_priv(primary_if->soft_iface);
2390 	ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
2391 
2392 	batadv_hardif_put(primary_if);
2393 	return ret;
2394 }
2395 
2396 /**
2397  * batadv_mcast_free() - free the multicast optimizations structures
2398  * @bat_priv: the bat priv with all the soft interface information
2399  */
2400 void batadv_mcast_free(struct batadv_priv *bat_priv)
2401 {
2402 	cancel_delayed_work_sync(&bat_priv->mcast.work);
2403 
2404 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2405 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2406 
2407 	/* safely calling outside of worker, as worker was canceled above */
2408 	batadv_mcast_mla_tt_retract(bat_priv, NULL);
2409 }
2410 
2411 /**
2412  * batadv_mcast_purge_orig() - reset originator global mcast state modifications
2413  * @orig: the originator which is going to get purged
2414  */
2415 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
2416 {
2417 	struct batadv_priv *bat_priv = orig->bat_priv;
2418 
2419 	spin_lock_bh(&orig->mcast_handler_lock);
2420 
2421 	batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
2422 	batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
2423 	batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
2424 	batadv_mcast_want_rtr4_update(bat_priv, orig,
2425 				      BATADV_MCAST_WANT_NO_RTR4);
2426 	batadv_mcast_want_rtr6_update(bat_priv, orig,
2427 				      BATADV_MCAST_WANT_NO_RTR6);
2428 
2429 	spin_unlock_bh(&orig->mcast_handler_lock);
2430 }
2431