xref: /linux/net/batman-adv/multicast.c (revision 80d443e8876602be2c130f79c4de81e12e2a700d)
1 /* Copyright (C) 2014-2016  B.A.T.M.A.N. contributors:
2  *
3  * Linus Lüssing
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "multicast.h"
19 #include "main.h"
20 
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
27 #include <linux/fs.h>
28 #include <linux/icmpv6.h>
29 #include <linux/if_bridge.h>
30 #include <linux/if_ether.h>
31 #include <linux/igmp.h>
32 #include <linux/in.h>
33 #include <linux/in6.h>
34 #include <linux/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/jiffies.h>
37 #include <linux/kernel.h>
38 #include <linux/kref.h>
39 #include <linux/list.h>
40 #include <linux/lockdep.h>
41 #include <linux/netdevice.h>
42 #include <linux/printk.h>
43 #include <linux/rculist.h>
44 #include <linux/rcupdate.h>
45 #include <linux/seq_file.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/stddef.h>
50 #include <linux/string.h>
51 #include <linux/types.h>
52 #include <linux/workqueue.h>
53 #include <net/addrconf.h>
54 #include <net/if_inet6.h>
55 #include <net/ip.h>
56 #include <net/ipv6.h>
57 
58 #include "hard-interface.h"
59 #include "hash.h"
60 #include "log.h"
61 #include "packet.h"
62 #include "translation-table.h"
63 #include "tvlv.h"
64 
65 static void batadv_mcast_mla_update(struct work_struct *work);
66 
67 /**
68  * batadv_mcast_start_timer - schedule the multicast periodic worker
69  * @bat_priv: the bat priv with all the soft interface information
70  */
71 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
72 {
73 	queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
74 			   msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
75 }
76 
77 /**
78  * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
79  * @soft_iface: netdev struct of the mesh interface
80  *
81  * If the given soft interface has a bridge on top then the refcount
82  * of the according net device is increased.
83  *
84  * Return: NULL if no such bridge exists. Otherwise the net device of the
85  * bridge.
86  */
87 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
88 {
89 	struct net_device *upper = soft_iface;
90 
91 	rcu_read_lock();
92 	do {
93 		upper = netdev_master_upper_dev_get_rcu(upper);
94 	} while (upper && !(upper->priv_flags & IFF_EBRIDGE));
95 
96 	if (upper)
97 		dev_hold(upper);
98 	rcu_read_unlock();
99 
100 	return upper;
101 }
102 
103 /**
104  * batadv_mcast_mla_softif_get - get softif multicast listeners
105  * @dev: the device to collect multicast addresses from
106  * @mcast_list: a list to put found addresses into
107  *
108  * Collects multicast addresses of multicast listeners residing
109  * on this kernel on the given soft interface, dev, in
110  * the given mcast_list. In general, multicast listeners provided by
111  * your multicast receiving applications run directly on this node.
112  *
113  * If there is a bridge interface on top of dev, collects from that one
114  * instead. Just like with IP addresses and routes, multicast listeners
115  * will(/should) register to the bridge interface instead of an
116  * enslaved bat0.
117  *
118  * Return: -ENOMEM on memory allocation error or the number of
119  * items added to the mcast_list otherwise.
120  */
121 static int batadv_mcast_mla_softif_get(struct net_device *dev,
122 				       struct hlist_head *mcast_list)
123 {
124 	struct net_device *bridge = batadv_mcast_get_bridge(dev);
125 	struct netdev_hw_addr *mc_list_entry;
126 	struct batadv_hw_addr *new;
127 	int ret = 0;
128 
129 	netif_addr_lock_bh(bridge ? bridge : dev);
130 	netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
131 		new = kmalloc(sizeof(*new), GFP_ATOMIC);
132 		if (!new) {
133 			ret = -ENOMEM;
134 			break;
135 		}
136 
137 		ether_addr_copy(new->addr, mc_list_entry->addr);
138 		hlist_add_head(&new->list, mcast_list);
139 		ret++;
140 	}
141 	netif_addr_unlock_bh(bridge ? bridge : dev);
142 
143 	if (bridge)
144 		dev_put(bridge);
145 
146 	return ret;
147 }
148 
149 /**
150  * batadv_mcast_mla_is_duplicate - check whether an address is in a list
151  * @mcast_addr: the multicast address to check
152  * @mcast_list: the list with multicast addresses to search in
153  *
154  * Return: true if the given address is already in the given list.
155  * Otherwise returns false.
156  */
157 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
158 					  struct hlist_head *mcast_list)
159 {
160 	struct batadv_hw_addr *mcast_entry;
161 
162 	hlist_for_each_entry(mcast_entry, mcast_list, list)
163 		if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
164 			return true;
165 
166 	return false;
167 }
168 
169 /**
170  * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
171  * @dst: destination to write to - a multicast MAC address
172  * @src: source to read from - a multicast IP address
173  *
174  * Converts a given multicast IPv4/IPv6 address from a bridge
175  * to its matching multicast MAC address and copies it into the given
176  * destination buffer.
177  *
178  * Caller needs to make sure the destination buffer can hold
179  * at least ETH_ALEN bytes.
180  */
181 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
182 {
183 	if (src->proto == htons(ETH_P_IP))
184 		ip_eth_mc_map(src->u.ip4, dst);
185 #if IS_ENABLED(CONFIG_IPV6)
186 	else if (src->proto == htons(ETH_P_IPV6))
187 		ipv6_eth_mc_map(&src->u.ip6, dst);
188 #endif
189 	else
190 		eth_zero_addr(dst);
191 }
192 
193 /**
194  * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
195  * @dev: a bridge slave whose bridge to collect multicast addresses from
196  * @mcast_list: a list to put found addresses into
197  *
198  * Collects multicast addresses of multicast listeners residing
199  * on foreign, non-mesh devices which we gave access to our mesh via
200  * a bridge on top of the given soft interface, dev, in the given
201  * mcast_list.
202  *
203  * Return: -ENOMEM on memory allocation error or the number of
204  * items added to the mcast_list otherwise.
205  */
206 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
207 				       struct hlist_head *mcast_list)
208 {
209 	struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
210 	struct br_ip_list *br_ip_entry, *tmp;
211 	struct batadv_hw_addr *new;
212 	u8 mcast_addr[ETH_ALEN];
213 	int ret;
214 
215 	/* we don't need to detect these devices/listeners, the IGMP/MLD
216 	 * snooping code of the Linux bridge already does that for us
217 	 */
218 	ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
219 	if (ret < 0)
220 		goto out;
221 
222 	list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
223 		batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
224 		if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
225 			continue;
226 
227 		new = kmalloc(sizeof(*new), GFP_ATOMIC);
228 		if (!new) {
229 			ret = -ENOMEM;
230 			break;
231 		}
232 
233 		ether_addr_copy(new->addr, mcast_addr);
234 		hlist_add_head(&new->list, mcast_list);
235 	}
236 
237 out:
238 	list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
239 		list_del(&br_ip_entry->list);
240 		kfree(br_ip_entry);
241 	}
242 
243 	return ret;
244 }
245 
246 /**
247  * batadv_mcast_mla_list_free - free a list of multicast addresses
248  * @mcast_list: the list to free
249  *
250  * Removes and frees all items in the given mcast_list.
251  */
252 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
253 {
254 	struct batadv_hw_addr *mcast_entry;
255 	struct hlist_node *tmp;
256 
257 	hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
258 		hlist_del(&mcast_entry->list);
259 		kfree(mcast_entry);
260 	}
261 }
262 
263 /**
264  * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
265  * @bat_priv: the bat priv with all the soft interface information
266  * @mcast_list: a list of addresses which should _not_ be removed
267  *
268  * Retracts the announcement of any multicast listener from the
269  * translation table except the ones listed in the given mcast_list.
270  *
271  * If mcast_list is NULL then all are retracted.
272  *
273  * Do not call outside of the mcast worker! (or cancel mcast worker first)
274  */
275 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
276 					struct hlist_head *mcast_list)
277 {
278 	struct batadv_hw_addr *mcast_entry;
279 	struct hlist_node *tmp;
280 
281 	WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
282 
283 	hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
284 				  list) {
285 		if (mcast_list &&
286 		    batadv_mcast_mla_is_duplicate(mcast_entry->addr,
287 						  mcast_list))
288 			continue;
289 
290 		batadv_tt_local_remove(bat_priv, mcast_entry->addr,
291 				       BATADV_NO_FLAGS,
292 				       "mcast TT outdated", false);
293 
294 		hlist_del(&mcast_entry->list);
295 		kfree(mcast_entry);
296 	}
297 }
298 
299 /**
300  * batadv_mcast_mla_tt_add - add multicast listener announcements
301  * @bat_priv: the bat priv with all the soft interface information
302  * @mcast_list: a list of addresses which are going to get added
303  *
304  * Adds multicast listener announcements from the given mcast_list to the
305  * translation table if they have not been added yet.
306  *
307  * Do not call outside of the mcast worker! (or cancel mcast worker first)
308  */
309 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
310 				    struct hlist_head *mcast_list)
311 {
312 	struct batadv_hw_addr *mcast_entry;
313 	struct hlist_node *tmp;
314 
315 	WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
316 
317 	if (!mcast_list)
318 		return;
319 
320 	hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
321 		if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
322 						  &bat_priv->mcast.mla_list))
323 			continue;
324 
325 		if (!batadv_tt_local_add(bat_priv->soft_iface,
326 					 mcast_entry->addr, BATADV_NO_FLAGS,
327 					 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
328 			continue;
329 
330 		hlist_del(&mcast_entry->list);
331 		hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
332 	}
333 }
334 
335 /**
336  * batadv_mcast_has_bridge - check whether the soft-iface is bridged
337  * @bat_priv: the bat priv with all the soft interface information
338  *
339  * Checks whether there is a bridge on top of our soft interface.
340  *
341  * Return: true if there is a bridge, false otherwise.
342  */
343 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
344 {
345 	struct net_device *upper = bat_priv->soft_iface;
346 
347 	rcu_read_lock();
348 	do {
349 		upper = netdev_master_upper_dev_get_rcu(upper);
350 	} while (upper && !(upper->priv_flags & IFF_EBRIDGE));
351 	rcu_read_unlock();
352 
353 	return upper;
354 }
355 
356 /**
357  * batadv_mcast_querier_log - debug output regarding the querier status on link
358  * @bat_priv: the bat priv with all the soft interface information
359  * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
360  * @old_state: the previous querier state on our link
361  * @new_state: the new querier state on our link
362  *
363  * Outputs debug messages to the logging facility with log level 'mcast'
364  * regarding changes to the querier status on the link which are relevant
365  * to our multicast optimizations.
366  *
367  * Usually this is about whether a querier appeared or vanished in
368  * our mesh or whether the querier is in the suboptimal position of being
369  * behind our local bridge segment: Snooping switches will directly
370  * forward listener reports to the querier, therefore batman-adv and
371  * the bridge will potentially not see these listeners - the querier is
372  * potentially shadowing listeners from us then.
373  *
374  * This is only interesting for nodes with a bridge on top of their
375  * soft interface.
376  */
377 static void
378 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
379 			 struct batadv_mcast_querier_state *old_state,
380 			 struct batadv_mcast_querier_state *new_state)
381 {
382 	if (!old_state->exists && new_state->exists)
383 		batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
384 			    str_proto);
385 	else if (old_state->exists && !new_state->exists)
386 		batadv_info(bat_priv->soft_iface,
387 			    "%s Querier disappeared - multicast optimizations disabled\n",
388 			    str_proto);
389 	else if (!bat_priv->mcast.bridged && !new_state->exists)
390 		batadv_info(bat_priv->soft_iface,
391 			    "No %s Querier present - multicast optimizations disabled\n",
392 			    str_proto);
393 
394 	if (new_state->exists) {
395 		if ((!old_state->shadowing && new_state->shadowing) ||
396 		    (!old_state->exists && new_state->shadowing))
397 			batadv_dbg(BATADV_DBG_MCAST, bat_priv,
398 				   "%s Querier is behind our bridged segment: Might shadow listeners\n",
399 				   str_proto);
400 		else if (old_state->shadowing && !new_state->shadowing)
401 			batadv_dbg(BATADV_DBG_MCAST, bat_priv,
402 				   "%s Querier is not behind our bridged segment\n",
403 				   str_proto);
404 	}
405 }
406 
407 /**
408  * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
409  * @bat_priv: the bat priv with all the soft interface information
410  * @bridged: a flag about whether the soft interface is currently bridged or not
411  * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
412  * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
413  *
414  * If no bridges are ever used on this node, then this function does nothing.
415  *
416  * Otherwise this function outputs debug information to the 'mcast' log level
417  * which might be relevant to our multicast optimizations.
418  *
419  * More precisely, it outputs information when a bridge interface is added or
420  * removed from a soft interface. And when a bridge is present, it further
421  * outputs information about the querier state which is relevant for the
422  * multicast flags this node is going to set.
423  */
424 static void
425 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
426 			struct batadv_mcast_querier_state *querier_ipv4,
427 			struct batadv_mcast_querier_state *querier_ipv6)
428 {
429 	if (!bat_priv->mcast.bridged && bridged)
430 		batadv_dbg(BATADV_DBG_MCAST, bat_priv,
431 			   "Bridge added: Setting Unsnoopables(U)-flag\n");
432 	else if (bat_priv->mcast.bridged && !bridged)
433 		batadv_dbg(BATADV_DBG_MCAST, bat_priv,
434 			   "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
435 
436 	if (bridged) {
437 		batadv_mcast_querier_log(bat_priv, "IGMP",
438 					 &bat_priv->mcast.querier_ipv4,
439 					 querier_ipv4);
440 		batadv_mcast_querier_log(bat_priv, "MLD",
441 					 &bat_priv->mcast.querier_ipv6,
442 					 querier_ipv6);
443 	}
444 }
445 
446 /**
447  * batadv_mcast_flags_logs - output debug information about mcast flag changes
448  * @bat_priv: the bat priv with all the soft interface information
449  * @flags: flags indicating the new multicast state
450  *
451  * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
452  * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
453  */
454 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
455 {
456 	u8 old_flags = bat_priv->mcast.flags;
457 	char str_old_flags[] = "[...]";
458 
459 	sprintf(str_old_flags, "[%c%c%c]",
460 		(old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
461 		(old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
462 		(old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
463 
464 	batadv_dbg(BATADV_DBG_MCAST, bat_priv,
465 		   "Changing multicast flags from '%s' to '[%c%c%c]'\n",
466 		   bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
467 		   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
468 		   (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
469 		   (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
470 }
471 
472 /**
473  * batadv_mcast_mla_tvlv_update - update multicast tvlv
474  * @bat_priv: the bat priv with all the soft interface information
475  *
476  * Updates the own multicast tvlv with our current multicast related settings,
477  * capabilities and inabilities.
478  *
479  * Return: false if we want all IPv4 && IPv6 multicast traffic and true
480  * otherwise.
481  */
482 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
483 {
484 	struct batadv_tvlv_mcast_data mcast_data;
485 	struct batadv_mcast_querier_state querier4 = {false, false};
486 	struct batadv_mcast_querier_state querier6 = {false, false};
487 	struct net_device *dev = bat_priv->soft_iface;
488 	bool bridged;
489 
490 	mcast_data.flags = BATADV_NO_FLAGS;
491 	memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
492 
493 	bridged = batadv_mcast_has_bridge(bat_priv);
494 	if (!bridged)
495 		goto update;
496 
497 #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
498 	pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
499 #endif
500 
501 	querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
502 	querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
503 
504 	querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
505 	querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
506 
507 	mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
508 
509 	/* 1) If no querier exists at all, then multicast listeners on
510 	 *    our local TT clients behind the bridge will keep silent.
511 	 * 2) If the selected querier is on one of our local TT clients,
512 	 *    behind the bridge, then this querier might shadow multicast
513 	 *    listeners on our local TT clients, behind this bridge.
514 	 *
515 	 * In both cases, we will signalize other batman nodes that
516 	 * we need all multicast traffic of the according protocol.
517 	 */
518 	if (!querier4.exists || querier4.shadowing)
519 		mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
520 
521 	if (!querier6.exists || querier6.shadowing)
522 		mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
523 
524 update:
525 	batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
526 
527 	bat_priv->mcast.querier_ipv4.exists = querier4.exists;
528 	bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
529 
530 	bat_priv->mcast.querier_ipv6.exists = querier6.exists;
531 	bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
532 
533 	bat_priv->mcast.bridged = bridged;
534 
535 	if (!bat_priv->mcast.enabled ||
536 	    mcast_data.flags != bat_priv->mcast.flags) {
537 		batadv_mcast_flags_log(bat_priv, mcast_data.flags);
538 		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
539 					       &mcast_data, sizeof(mcast_data));
540 		bat_priv->mcast.flags = mcast_data.flags;
541 		bat_priv->mcast.enabled = true;
542 	}
543 
544 	return !(mcast_data.flags &
545 		 (BATADV_MCAST_WANT_ALL_IPV4 | BATADV_MCAST_WANT_ALL_IPV6));
546 }
547 
548 /**
549  * __batadv_mcast_mla_update - update the own MLAs
550  * @bat_priv: the bat priv with all the soft interface information
551  *
552  * Updates the own multicast listener announcements in the translation
553  * table as well as the own, announced multicast tvlv container.
554  *
555  * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
556  * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
557  * ensured by the non-parallel execution of the worker this function
558  * belongs to.
559  */
560 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
561 {
562 	struct net_device *soft_iface = bat_priv->soft_iface;
563 	struct hlist_head mcast_list = HLIST_HEAD_INIT;
564 	int ret;
565 
566 	if (!batadv_mcast_mla_tvlv_update(bat_priv))
567 		goto update;
568 
569 	ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
570 	if (ret < 0)
571 		goto out;
572 
573 	ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
574 	if (ret < 0)
575 		goto out;
576 
577 update:
578 	batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
579 	batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
580 
581 out:
582 	batadv_mcast_mla_list_free(&mcast_list);
583 }
584 
585 /**
586  * batadv_mcast_mla_update - update the own MLAs
587  * @work: kernel work struct
588  *
589  * Updates the own multicast listener announcements in the translation
590  * table as well as the own, announced multicast tvlv container.
591  *
592  * In the end, reschedules the work timer.
593  */
594 static void batadv_mcast_mla_update(struct work_struct *work)
595 {
596 	struct delayed_work *delayed_work;
597 	struct batadv_priv_mcast *priv_mcast;
598 	struct batadv_priv *bat_priv;
599 
600 	delayed_work = to_delayed_work(work);
601 	priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
602 	bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
603 
604 	__batadv_mcast_mla_update(bat_priv);
605 	batadv_mcast_start_timer(bat_priv);
606 }
607 
608 /**
609  * batadv_mcast_is_report_ipv4 - check for IGMP reports
610  * @skb: the ethernet frame destined for the mesh
611  *
612  * This call might reallocate skb data.
613  *
614  * Checks whether the given frame is a valid IGMP report.
615  *
616  * Return: If so then true, otherwise false.
617  */
618 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
619 {
620 	if (ip_mc_check_igmp(skb, NULL) < 0)
621 		return false;
622 
623 	switch (igmp_hdr(skb)->type) {
624 	case IGMP_HOST_MEMBERSHIP_REPORT:
625 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
626 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
627 		return true;
628 	}
629 
630 	return false;
631 }
632 
633 /**
634  * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
635  * @bat_priv: the bat priv with all the soft interface information
636  * @skb: the IPv4 packet to check
637  * @is_unsnoopable: stores whether the destination is snoopable
638  *
639  * Checks whether the given IPv4 packet has the potential to be forwarded with a
640  * mode more optimal than classic flooding.
641  *
642  * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
643  * allocation failure.
644  */
645 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
646 					     struct sk_buff *skb,
647 					     bool *is_unsnoopable)
648 {
649 	struct iphdr *iphdr;
650 
651 	/* We might fail due to out-of-memory -> drop it */
652 	if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
653 		return -ENOMEM;
654 
655 	if (batadv_mcast_is_report_ipv4(skb))
656 		return -EINVAL;
657 
658 	iphdr = ip_hdr(skb);
659 
660 	/* TODO: Implement Multicast Router Discovery (RFC4286),
661 	 * then allow scope > link local, too
662 	 */
663 	if (!ipv4_is_local_multicast(iphdr->daddr))
664 		return -EINVAL;
665 
666 	/* link-local multicast listeners behind a bridge are
667 	 * not snoopable (see RFC4541, section 2.1.2.2)
668 	 */
669 	*is_unsnoopable = true;
670 
671 	return 0;
672 }
673 
674 #if IS_ENABLED(CONFIG_IPV6)
675 /**
676  * batadv_mcast_is_report_ipv6 - check for MLD reports
677  * @skb: the ethernet frame destined for the mesh
678  *
679  * This call might reallocate skb data.
680  *
681  * Checks whether the given frame is a valid MLD report.
682  *
683  * Return: If so then true, otherwise false.
684  */
685 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
686 {
687 	if (ipv6_mc_check_mld(skb, NULL) < 0)
688 		return false;
689 
690 	switch (icmp6_hdr(skb)->icmp6_type) {
691 	case ICMPV6_MGM_REPORT:
692 	case ICMPV6_MLD2_REPORT:
693 		return true;
694 	}
695 
696 	return false;
697 }
698 
699 /**
700  * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
701  * @bat_priv: the bat priv with all the soft interface information
702  * @skb: the IPv6 packet to check
703  * @is_unsnoopable: stores whether the destination is snoopable
704  *
705  * Checks whether the given IPv6 packet has the potential to be forwarded with a
706  * mode more optimal than classic flooding.
707  *
708  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
709  */
710 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
711 					     struct sk_buff *skb,
712 					     bool *is_unsnoopable)
713 {
714 	struct ipv6hdr *ip6hdr;
715 
716 	/* We might fail due to out-of-memory -> drop it */
717 	if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
718 		return -ENOMEM;
719 
720 	if (batadv_mcast_is_report_ipv6(skb))
721 		return -EINVAL;
722 
723 	ip6hdr = ipv6_hdr(skb);
724 
725 	/* TODO: Implement Multicast Router Discovery (RFC4286),
726 	 * then allow scope > link local, too
727 	 */
728 	if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
729 		return -EINVAL;
730 
731 	/* link-local-all-nodes multicast listeners behind a bridge are
732 	 * not snoopable (see RFC4541, section 3, paragraph 3)
733 	 */
734 	if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
735 		*is_unsnoopable = true;
736 
737 	return 0;
738 }
739 #endif
740 
741 /**
742  * batadv_mcast_forw_mode_check - check for optimized forwarding potential
743  * @bat_priv: the bat priv with all the soft interface information
744  * @skb: the multicast frame to check
745  * @is_unsnoopable: stores whether the destination is snoopable
746  *
747  * Checks whether the given multicast ethernet frame has the potential to be
748  * forwarded with a mode more optimal than classic flooding.
749  *
750  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
751  */
752 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
753 					struct sk_buff *skb,
754 					bool *is_unsnoopable)
755 {
756 	struct ethhdr *ethhdr = eth_hdr(skb);
757 
758 	if (!atomic_read(&bat_priv->multicast_mode))
759 		return -EINVAL;
760 
761 	if (atomic_read(&bat_priv->mcast.num_disabled))
762 		return -EINVAL;
763 
764 	switch (ntohs(ethhdr->h_proto)) {
765 	case ETH_P_IP:
766 		return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
767 							 is_unsnoopable);
768 #if IS_ENABLED(CONFIG_IPV6)
769 	case ETH_P_IPV6:
770 		return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
771 							 is_unsnoopable);
772 #endif
773 	default:
774 		return -EINVAL;
775 	}
776 }
777 
778 /**
779  * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
780  *  interest
781  * @bat_priv: the bat priv with all the soft interface information
782  * @ethhdr: ethernet header of a packet
783  *
784  * Return: the number of nodes which want all IPv4 multicast traffic if the
785  * given ethhdr is from an IPv4 packet or the number of nodes which want all
786  * IPv6 traffic if it matches an IPv6 packet.
787  */
788 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
789 					       struct ethhdr *ethhdr)
790 {
791 	switch (ntohs(ethhdr->h_proto)) {
792 	case ETH_P_IP:
793 		return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
794 	case ETH_P_IPV6:
795 		return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
796 	default:
797 		/* we shouldn't be here... */
798 		return 0;
799 	}
800 }
801 
802 /**
803  * batadv_mcast_forw_tt_node_get - get a multicast tt node
804  * @bat_priv: the bat priv with all the soft interface information
805  * @ethhdr: the ether header containing the multicast destination
806  *
807  * Return: an orig_node matching the multicast address provided by ethhdr
808  * via a translation table lookup. This increases the returned nodes refcount.
809  */
810 static struct batadv_orig_node *
811 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
812 			      struct ethhdr *ethhdr)
813 {
814 	return batadv_transtable_search(bat_priv, ethhdr->h_source,
815 					ethhdr->h_dest, BATADV_NO_FLAGS);
816 }
817 
818 /**
819  * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
820  * @bat_priv: the bat priv with all the soft interface information
821  *
822  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
823  * increases its refcount.
824  */
825 static struct batadv_orig_node *
826 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
827 {
828 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
829 
830 	rcu_read_lock();
831 	hlist_for_each_entry_rcu(tmp_orig_node,
832 				 &bat_priv->mcast.want_all_ipv4_list,
833 				 mcast_want_all_ipv4_node) {
834 		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
835 			continue;
836 
837 		orig_node = tmp_orig_node;
838 		break;
839 	}
840 	rcu_read_unlock();
841 
842 	return orig_node;
843 }
844 
845 /**
846  * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
847  * @bat_priv: the bat priv with all the soft interface information
848  *
849  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
850  * and increases its refcount.
851  */
852 static struct batadv_orig_node *
853 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
854 {
855 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
856 
857 	rcu_read_lock();
858 	hlist_for_each_entry_rcu(tmp_orig_node,
859 				 &bat_priv->mcast.want_all_ipv6_list,
860 				 mcast_want_all_ipv6_node) {
861 		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
862 			continue;
863 
864 		orig_node = tmp_orig_node;
865 		break;
866 	}
867 	rcu_read_unlock();
868 
869 	return orig_node;
870 }
871 
872 /**
873  * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
874  * @bat_priv: the bat priv with all the soft interface information
875  * @ethhdr: an ethernet header to determine the protocol family from
876  *
877  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
878  * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
879  * increases its refcount.
880  */
881 static struct batadv_orig_node *
882 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
883 			      struct ethhdr *ethhdr)
884 {
885 	switch (ntohs(ethhdr->h_proto)) {
886 	case ETH_P_IP:
887 		return batadv_mcast_forw_ipv4_node_get(bat_priv);
888 	case ETH_P_IPV6:
889 		return batadv_mcast_forw_ipv6_node_get(bat_priv);
890 	default:
891 		/* we shouldn't be here... */
892 		return NULL;
893 	}
894 }
895 
896 /**
897  * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
898  * @bat_priv: the bat priv with all the soft interface information
899  *
900  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
901  * set and increases its refcount.
902  */
903 static struct batadv_orig_node *
904 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
905 {
906 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
907 
908 	rcu_read_lock();
909 	hlist_for_each_entry_rcu(tmp_orig_node,
910 				 &bat_priv->mcast.want_all_unsnoopables_list,
911 				 mcast_want_all_unsnoopables_node) {
912 		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
913 			continue;
914 
915 		orig_node = tmp_orig_node;
916 		break;
917 	}
918 	rcu_read_unlock();
919 
920 	return orig_node;
921 }
922 
923 /**
924  * batadv_mcast_forw_mode - check on how to forward a multicast packet
925  * @bat_priv: the bat priv with all the soft interface information
926  * @skb: The multicast packet to check
927  * @orig: an originator to be set to forward the skb to
928  *
929  * Return: the forwarding mode as enum batadv_forw_mode and in case of
930  * BATADV_FORW_SINGLE set the orig to the single originator the skb
931  * should be forwarded to.
932  */
933 enum batadv_forw_mode
934 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
935 		       struct batadv_orig_node **orig)
936 {
937 	int ret, tt_count, ip_count, unsnoop_count, total_count;
938 	bool is_unsnoopable = false;
939 	struct ethhdr *ethhdr;
940 
941 	ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
942 	if (ret == -ENOMEM)
943 		return BATADV_FORW_NONE;
944 	else if (ret < 0)
945 		return BATADV_FORW_ALL;
946 
947 	ethhdr = eth_hdr(skb);
948 
949 	tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
950 					       BATADV_NO_FLAGS);
951 	ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
952 	unsnoop_count = !is_unsnoopable ? 0 :
953 			atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
954 
955 	total_count = tt_count + ip_count + unsnoop_count;
956 
957 	switch (total_count) {
958 	case 1:
959 		if (tt_count)
960 			*orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
961 		else if (ip_count)
962 			*orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
963 		else if (unsnoop_count)
964 			*orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
965 
966 		if (*orig)
967 			return BATADV_FORW_SINGLE;
968 
969 		/* fall through */
970 	case 0:
971 		return BATADV_FORW_NONE;
972 	default:
973 		return BATADV_FORW_ALL;
974 	}
975 }
976 
977 /**
978  * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
979  * @bat_priv: the bat priv with all the soft interface information
980  * @orig: the orig_node which multicast state might have changed of
981  * @mcast_flags: flags indicating the new multicast state
982  *
983  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
984  * orig, has toggled then this method updates counter and list accordingly.
985  *
986  * Caller needs to hold orig->mcast_handler_lock.
987  */
988 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
989 					     struct batadv_orig_node *orig,
990 					     u8 mcast_flags)
991 {
992 	struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
993 	struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
994 
995 	lockdep_assert_held(&orig->mcast_handler_lock);
996 
997 	/* switched from flag unset to set */
998 	if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
999 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1000 		atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1001 
1002 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1003 		/* flag checks above + mcast_handler_lock prevents this */
1004 		WARN_ON(!hlist_unhashed(node));
1005 
1006 		hlist_add_head_rcu(node, head);
1007 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1008 	/* switched from flag set to unset */
1009 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1010 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1011 		atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1012 
1013 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1014 		/* flag checks above + mcast_handler_lock prevents this */
1015 		WARN_ON(hlist_unhashed(node));
1016 
1017 		hlist_del_init_rcu(node);
1018 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1019 	}
1020 }
1021 
1022 /**
1023  * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
1024  * @bat_priv: the bat priv with all the soft interface information
1025  * @orig: the orig_node which multicast state might have changed of
1026  * @mcast_flags: flags indicating the new multicast state
1027  *
1028  * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1029  * toggled then this method updates counter and list accordingly.
1030  *
1031  * Caller needs to hold orig->mcast_handler_lock.
1032  */
1033 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1034 					  struct batadv_orig_node *orig,
1035 					  u8 mcast_flags)
1036 {
1037 	struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1038 	struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1039 
1040 	lockdep_assert_held(&orig->mcast_handler_lock);
1041 
1042 	/* switched from flag unset to set */
1043 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1044 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1045 		atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1046 
1047 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1048 		/* flag checks above + mcast_handler_lock prevents this */
1049 		WARN_ON(!hlist_unhashed(node));
1050 
1051 		hlist_add_head_rcu(node, head);
1052 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1053 	/* switched from flag set to unset */
1054 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1055 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1056 		atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1057 
1058 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1059 		/* flag checks above + mcast_handler_lock prevents this */
1060 		WARN_ON(hlist_unhashed(node));
1061 
1062 		hlist_del_init_rcu(node);
1063 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1064 	}
1065 }
1066 
1067 /**
1068  * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1069  * @bat_priv: the bat priv with all the soft interface information
1070  * @orig: the orig_node which multicast state might have changed of
1071  * @mcast_flags: flags indicating the new multicast state
1072  *
1073  * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1074  * toggled then this method updates counter and list accordingly.
1075  *
1076  * Caller needs to hold orig->mcast_handler_lock.
1077  */
1078 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1079 					  struct batadv_orig_node *orig,
1080 					  u8 mcast_flags)
1081 {
1082 	struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1083 	struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1084 
1085 	lockdep_assert_held(&orig->mcast_handler_lock);
1086 
1087 	/* switched from flag unset to set */
1088 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1089 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1090 		atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1091 
1092 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1093 		/* flag checks above + mcast_handler_lock prevents this */
1094 		WARN_ON(!hlist_unhashed(node));
1095 
1096 		hlist_add_head_rcu(node, head);
1097 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1098 	/* switched from flag set to unset */
1099 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1100 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1101 		atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1102 
1103 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1104 		/* flag checks above + mcast_handler_lock prevents this */
1105 		WARN_ON(hlist_unhashed(node));
1106 
1107 		hlist_del_init_rcu(node);
1108 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1109 	}
1110 }
1111 
1112 /**
1113  * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
1114  * @bat_priv: the bat priv with all the soft interface information
1115  * @orig: the orig_node of the ogm
1116  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1117  * @tvlv_value: tvlv buffer containing the multicast data
1118  * @tvlv_value_len: tvlv buffer length
1119  */
1120 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1121 					  struct batadv_orig_node *orig,
1122 					  u8 flags,
1123 					  void *tvlv_value,
1124 					  u16 tvlv_value_len)
1125 {
1126 	bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1127 	u8 mcast_flags = BATADV_NO_FLAGS;
1128 	bool orig_initialized;
1129 
1130 	if (orig_mcast_enabled && tvlv_value &&
1131 	    (tvlv_value_len >= sizeof(mcast_flags)))
1132 		mcast_flags = *(u8 *)tvlv_value;
1133 
1134 	spin_lock_bh(&orig->mcast_handler_lock);
1135 	orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1136 				    &orig->capa_initialized);
1137 
1138 	/* If mcast support is turned on decrease the disabled mcast node
1139 	 * counter only if we had increased it for this node before. If this
1140 	 * is a completely new orig_node no need to decrease the counter.
1141 	 */
1142 	if (orig_mcast_enabled &&
1143 	    !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1144 		if (orig_initialized)
1145 			atomic_dec(&bat_priv->mcast.num_disabled);
1146 		set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1147 	/* If mcast support is being switched off or if this is an initial
1148 	 * OGM without mcast support then increase the disabled mcast
1149 	 * node counter.
1150 	 */
1151 	} else if (!orig_mcast_enabled &&
1152 		   (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1153 		    !orig_initialized)) {
1154 		atomic_inc(&bat_priv->mcast.num_disabled);
1155 		clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1156 	}
1157 
1158 	set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1159 
1160 	batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1161 	batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1162 	batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1163 
1164 	orig->mcast_flags = mcast_flags;
1165 	spin_unlock_bh(&orig->mcast_handler_lock);
1166 }
1167 
1168 /**
1169  * batadv_mcast_init - initialize the multicast optimizations structures
1170  * @bat_priv: the bat priv with all the soft interface information
1171  */
1172 void batadv_mcast_init(struct batadv_priv *bat_priv)
1173 {
1174 	batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1175 				     NULL, BATADV_TVLV_MCAST, 2,
1176 				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1177 
1178 	INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1179 	batadv_mcast_start_timer(bat_priv);
1180 }
1181 
1182 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1183 /**
1184  * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1185  * @bat_priv: the bat priv with all the soft interface information
1186  * @seq: debugfs table seq_file struct
1187  *
1188  * Prints our own multicast flags including a more specific reason why
1189  * they are set, that is prints the bridge and querier state too, to
1190  * the debugfs table specified via @seq.
1191  */
1192 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1193 					    struct seq_file *seq)
1194 {
1195 	u8 flags = bat_priv->mcast.flags;
1196 	char querier4, querier6, shadowing4, shadowing6;
1197 	bool bridged = bat_priv->mcast.bridged;
1198 
1199 	if (bridged) {
1200 		querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1201 		querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1202 		shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1203 		shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1204 	} else {
1205 		querier4 = '?';
1206 		querier6 = '?';
1207 		shadowing4 = '?';
1208 		shadowing6 = '?';
1209 	}
1210 
1211 	seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1212 		   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1213 		   (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1214 		   (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1215 	seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1216 	seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1217 		   querier4, querier6);
1218 	seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1219 		   shadowing4, shadowing6);
1220 	seq_puts(seq, "-------------------------------------------\n");
1221 	seq_printf(seq, "       %-10s %s\n", "Originator", "Flags");
1222 }
1223 
1224 /**
1225  * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1226  * @seq: seq file to print on
1227  * @offset: not used
1228  *
1229  * This prints a table of (primary) originators and their according
1230  * multicast flags, including (in the header) our own.
1231  *
1232  * Return: always 0
1233  */
1234 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1235 {
1236 	struct net_device *net_dev = (struct net_device *)seq->private;
1237 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1238 	struct batadv_hard_iface *primary_if;
1239 	struct batadv_hashtable *hash = bat_priv->orig_hash;
1240 	struct batadv_orig_node *orig_node;
1241 	struct hlist_head *head;
1242 	u8 flags;
1243 	u32 i;
1244 
1245 	primary_if = batadv_seq_print_text_primary_if_get(seq);
1246 	if (!primary_if)
1247 		return 0;
1248 
1249 	batadv_mcast_flags_print_header(bat_priv, seq);
1250 
1251 	for (i = 0; i < hash->size; i++) {
1252 		head = &hash->table[i];
1253 
1254 		rcu_read_lock();
1255 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1256 			if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1257 				      &orig_node->capa_initialized))
1258 				continue;
1259 
1260 			if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1261 				      &orig_node->capabilities)) {
1262 				seq_printf(seq, "%pM -\n", orig_node->orig);
1263 				continue;
1264 			}
1265 
1266 			flags = orig_node->mcast_flags;
1267 
1268 			seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1269 				   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1270 				   ? 'U' : '.',
1271 				   (flags & BATADV_MCAST_WANT_ALL_IPV4)
1272 				   ? '4' : '.',
1273 				   (flags & BATADV_MCAST_WANT_ALL_IPV6)
1274 				   ? '6' : '.');
1275 		}
1276 		rcu_read_unlock();
1277 	}
1278 
1279 	batadv_hardif_put(primary_if);
1280 
1281 	return 0;
1282 }
1283 #endif
1284 
1285 /**
1286  * batadv_mcast_free - free the multicast optimizations structures
1287  * @bat_priv: the bat priv with all the soft interface information
1288  */
1289 void batadv_mcast_free(struct batadv_priv *bat_priv)
1290 {
1291 	cancel_delayed_work_sync(&bat_priv->mcast.work);
1292 
1293 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1294 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1295 
1296 	/* safely calling outside of worker, as worker was canceled above */
1297 	batadv_mcast_mla_tt_retract(bat_priv, NULL);
1298 }
1299 
1300 /**
1301  * batadv_mcast_purge_orig - reset originator global mcast state modifications
1302  * @orig: the originator which is going to get purged
1303  */
1304 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1305 {
1306 	struct batadv_priv *bat_priv = orig->bat_priv;
1307 
1308 	spin_lock_bh(&orig->mcast_handler_lock);
1309 
1310 	if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1311 	    test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1312 		atomic_dec(&bat_priv->mcast.num_disabled);
1313 
1314 	batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1315 	batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1316 	batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1317 
1318 	spin_unlock_bh(&orig->mcast_handler_lock);
1319 }
1320