• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2014-2017  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  */
batadv_mcast_start_timer(struct batadv_priv * bat_priv)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  */
batadv_mcast_get_bridge(struct net_device * soft_iface)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  */
batadv_mcast_mla_softif_get(struct net_device * dev,struct hlist_head * mcast_list)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  */
batadv_mcast_mla_is_duplicate(u8 * mcast_addr,struct hlist_head * mcast_list)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  */
batadv_mcast_mla_br_addr_cpy(char * dst,const struct br_ip * src)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  */
batadv_mcast_mla_bridge_get(struct net_device * dev,struct hlist_head * mcast_list)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  */
batadv_mcast_mla_list_free(struct hlist_head * mcast_list)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  */
batadv_mcast_mla_tt_retract(struct batadv_priv * bat_priv,struct hlist_head * mcast_list)273 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
274 					struct hlist_head *mcast_list)
275 {
276 	struct batadv_hw_addr *mcast_entry;
277 	struct hlist_node *tmp;
278 
279 	hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
280 				  list) {
281 		if (mcast_list &&
282 		    batadv_mcast_mla_is_duplicate(mcast_entry->addr,
283 						  mcast_list))
284 			continue;
285 
286 		batadv_tt_local_remove(bat_priv, mcast_entry->addr,
287 				       BATADV_NO_FLAGS,
288 				       "mcast TT outdated", false);
289 
290 		hlist_del(&mcast_entry->list);
291 		kfree(mcast_entry);
292 	}
293 }
294 
295 /**
296  * batadv_mcast_mla_tt_add - add multicast listener announcements
297  * @bat_priv: the bat priv with all the soft interface information
298  * @mcast_list: a list of addresses which are going to get added
299  *
300  * Adds multicast listener announcements from the given mcast_list to the
301  * translation table if they have not been added yet.
302  */
batadv_mcast_mla_tt_add(struct batadv_priv * bat_priv,struct hlist_head * mcast_list)303 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
304 				    struct hlist_head *mcast_list)
305 {
306 	struct batadv_hw_addr *mcast_entry;
307 	struct hlist_node *tmp;
308 
309 	if (!mcast_list)
310 		return;
311 
312 	hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
313 		if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
314 						  &bat_priv->mcast.mla_list))
315 			continue;
316 
317 		if (!batadv_tt_local_add(bat_priv->soft_iface,
318 					 mcast_entry->addr, BATADV_NO_FLAGS,
319 					 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
320 			continue;
321 
322 		hlist_del(&mcast_entry->list);
323 		hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
324 	}
325 }
326 
327 /**
328  * batadv_mcast_has_bridge - check whether the soft-iface is bridged
329  * @bat_priv: the bat priv with all the soft interface information
330  *
331  * Checks whether there is a bridge on top of our soft interface.
332  *
333  * Return: true if there is a bridge, false otherwise.
334  */
batadv_mcast_has_bridge(struct batadv_priv * bat_priv)335 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
336 {
337 	struct net_device *upper = bat_priv->soft_iface;
338 
339 	rcu_read_lock();
340 	do {
341 		upper = netdev_master_upper_dev_get_rcu(upper);
342 	} while (upper && !(upper->priv_flags & IFF_EBRIDGE));
343 	rcu_read_unlock();
344 
345 	return upper;
346 }
347 
348 /**
349  * batadv_mcast_querier_log - debug output regarding the querier status on link
350  * @bat_priv: the bat priv with all the soft interface information
351  * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
352  * @old_state: the previous querier state on our link
353  * @new_state: the new querier state on our link
354  *
355  * Outputs debug messages to the logging facility with log level 'mcast'
356  * regarding changes to the querier status on the link which are relevant
357  * to our multicast optimizations.
358  *
359  * Usually this is about whether a querier appeared or vanished in
360  * our mesh or whether the querier is in the suboptimal position of being
361  * behind our local bridge segment: Snooping switches will directly
362  * forward listener reports to the querier, therefore batman-adv and
363  * the bridge will potentially not see these listeners - the querier is
364  * potentially shadowing listeners from us then.
365  *
366  * This is only interesting for nodes with a bridge on top of their
367  * soft interface.
368  */
369 static void
batadv_mcast_querier_log(struct batadv_priv * bat_priv,char * str_proto,struct batadv_mcast_querier_state * old_state,struct batadv_mcast_querier_state * new_state)370 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
371 			 struct batadv_mcast_querier_state *old_state,
372 			 struct batadv_mcast_querier_state *new_state)
373 {
374 	if (!old_state->exists && new_state->exists)
375 		batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
376 			    str_proto);
377 	else if (old_state->exists && !new_state->exists)
378 		batadv_info(bat_priv->soft_iface,
379 			    "%s Querier disappeared - multicast optimizations disabled\n",
380 			    str_proto);
381 	else if (!bat_priv->mcast.bridged && !new_state->exists)
382 		batadv_info(bat_priv->soft_iface,
383 			    "No %s Querier present - multicast optimizations disabled\n",
384 			    str_proto);
385 
386 	if (new_state->exists) {
387 		if ((!old_state->shadowing && new_state->shadowing) ||
388 		    (!old_state->exists && new_state->shadowing))
389 			batadv_dbg(BATADV_DBG_MCAST, bat_priv,
390 				   "%s Querier is behind our bridged segment: Might shadow listeners\n",
391 				   str_proto);
392 		else if (old_state->shadowing && !new_state->shadowing)
393 			batadv_dbg(BATADV_DBG_MCAST, bat_priv,
394 				   "%s Querier is not behind our bridged segment\n",
395 				   str_proto);
396 	}
397 }
398 
399 /**
400  * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
401  * @bat_priv: the bat priv with all the soft interface information
402  * @bridged: a flag about whether the soft interface is currently bridged or not
403  * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
404  * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
405  *
406  * If no bridges are ever used on this node, then this function does nothing.
407  *
408  * Otherwise this function outputs debug information to the 'mcast' log level
409  * which might be relevant to our multicast optimizations.
410  *
411  * More precisely, it outputs information when a bridge interface is added or
412  * removed from a soft interface. And when a bridge is present, it further
413  * outputs information about the querier state which is relevant for the
414  * multicast flags this node is going to set.
415  */
416 static void
batadv_mcast_bridge_log(struct batadv_priv * bat_priv,bool bridged,struct batadv_mcast_querier_state * querier_ipv4,struct batadv_mcast_querier_state * querier_ipv6)417 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
418 			struct batadv_mcast_querier_state *querier_ipv4,
419 			struct batadv_mcast_querier_state *querier_ipv6)
420 {
421 	if (!bat_priv->mcast.bridged && bridged)
422 		batadv_dbg(BATADV_DBG_MCAST, bat_priv,
423 			   "Bridge added: Setting Unsnoopables(U)-flag\n");
424 	else if (bat_priv->mcast.bridged && !bridged)
425 		batadv_dbg(BATADV_DBG_MCAST, bat_priv,
426 			   "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
427 
428 	if (bridged) {
429 		batadv_mcast_querier_log(bat_priv, "IGMP",
430 					 &bat_priv->mcast.querier_ipv4,
431 					 querier_ipv4);
432 		batadv_mcast_querier_log(bat_priv, "MLD",
433 					 &bat_priv->mcast.querier_ipv6,
434 					 querier_ipv6);
435 	}
436 }
437 
438 /**
439  * batadv_mcast_flags_logs - output debug information about mcast flag changes
440  * @bat_priv: the bat priv with all the soft interface information
441  * @flags: flags indicating the new multicast state
442  *
443  * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
444  * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
445  */
batadv_mcast_flags_log(struct batadv_priv * bat_priv,u8 flags)446 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
447 {
448 	u8 old_flags = bat_priv->mcast.flags;
449 	char str_old_flags[] = "[...]";
450 
451 	sprintf(str_old_flags, "[%c%c%c]",
452 		(old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
453 		(old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
454 		(old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
455 
456 	batadv_dbg(BATADV_DBG_MCAST, bat_priv,
457 		   "Changing multicast flags from '%s' to '[%c%c%c]'\n",
458 		   bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
459 		   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
460 		   (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
461 		   (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
462 }
463 
464 /**
465  * batadv_mcast_mla_tvlv_update - update multicast tvlv
466  * @bat_priv: the bat priv with all the soft interface information
467  *
468  * Updates the own multicast tvlv with our current multicast related settings,
469  * capabilities and inabilities.
470  *
471  * Return: false if we want all IPv4 && IPv6 multicast traffic and true
472  * otherwise.
473  */
batadv_mcast_mla_tvlv_update(struct batadv_priv * bat_priv)474 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
475 {
476 	struct batadv_tvlv_mcast_data mcast_data;
477 	struct batadv_mcast_querier_state querier4 = {false, false};
478 	struct batadv_mcast_querier_state querier6 = {false, false};
479 	struct net_device *dev = bat_priv->soft_iface;
480 	bool bridged;
481 
482 	mcast_data.flags = BATADV_NO_FLAGS;
483 	memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
484 
485 	bridged = batadv_mcast_has_bridge(bat_priv);
486 	if (!bridged)
487 		goto update;
488 
489 	if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
490 		pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
491 
492 	querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
493 	querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
494 
495 	querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
496 	querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
497 
498 	mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
499 
500 	/* 1) If no querier exists at all, then multicast listeners on
501 	 *    our local TT clients behind the bridge will keep silent.
502 	 * 2) If the selected querier is on one of our local TT clients,
503 	 *    behind the bridge, then this querier might shadow multicast
504 	 *    listeners on our local TT clients, behind this bridge.
505 	 *
506 	 * In both cases, we will signalize other batman nodes that
507 	 * we need all multicast traffic of the according protocol.
508 	 */
509 	if (!querier4.exists || querier4.shadowing)
510 		mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
511 
512 	if (!querier6.exists || querier6.shadowing)
513 		mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
514 
515 update:
516 	batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
517 
518 	bat_priv->mcast.querier_ipv4.exists = querier4.exists;
519 	bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
520 
521 	bat_priv->mcast.querier_ipv6.exists = querier6.exists;
522 	bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
523 
524 	bat_priv->mcast.bridged = bridged;
525 
526 	if (!bat_priv->mcast.enabled ||
527 	    mcast_data.flags != bat_priv->mcast.flags) {
528 		batadv_mcast_flags_log(bat_priv, mcast_data.flags);
529 		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
530 					       &mcast_data, sizeof(mcast_data));
531 		bat_priv->mcast.flags = mcast_data.flags;
532 		bat_priv->mcast.enabled = true;
533 	}
534 
535 	return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
536 		 mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
537 }
538 
539 /**
540  * __batadv_mcast_mla_update - update the own MLAs
541  * @bat_priv: the bat priv with all the soft interface information
542  *
543  * Updates the own multicast listener announcements in the translation
544  * table as well as the own, announced multicast tvlv container.
545  *
546  * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
547  * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
548  * ensured by the non-parallel execution of the worker this function
549  * belongs to.
550  */
__batadv_mcast_mla_update(struct batadv_priv * bat_priv)551 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
552 {
553 	struct net_device *soft_iface = bat_priv->soft_iface;
554 	struct hlist_head mcast_list = HLIST_HEAD_INIT;
555 	int ret;
556 
557 	if (!batadv_mcast_mla_tvlv_update(bat_priv))
558 		goto update;
559 
560 	ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
561 	if (ret < 0)
562 		goto out;
563 
564 	ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
565 	if (ret < 0)
566 		goto out;
567 
568 update:
569 	batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
570 	batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
571 
572 out:
573 	batadv_mcast_mla_list_free(&mcast_list);
574 }
575 
576 /**
577  * batadv_mcast_mla_update - update the own MLAs
578  * @work: kernel work struct
579  *
580  * Updates the own multicast listener announcements in the translation
581  * table as well as the own, announced multicast tvlv container.
582  *
583  * In the end, reschedules the work timer.
584  */
batadv_mcast_mla_update(struct work_struct * work)585 static void batadv_mcast_mla_update(struct work_struct *work)
586 {
587 	struct delayed_work *delayed_work;
588 	struct batadv_priv_mcast *priv_mcast;
589 	struct batadv_priv *bat_priv;
590 
591 	delayed_work = to_delayed_work(work);
592 	priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
593 	bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
594 
595 	spin_lock(&bat_priv->mcast.mla_lock);
596 	__batadv_mcast_mla_update(bat_priv);
597 	spin_unlock(&bat_priv->mcast.mla_lock);
598 
599 	batadv_mcast_start_timer(bat_priv);
600 }
601 
602 /**
603  * batadv_mcast_is_report_ipv4 - check for IGMP reports
604  * @skb: the ethernet frame destined for the mesh
605  *
606  * This call might reallocate skb data.
607  *
608  * Checks whether the given frame is a valid IGMP report.
609  *
610  * Return: If so then true, otherwise false.
611  */
batadv_mcast_is_report_ipv4(struct sk_buff * skb)612 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
613 {
614 	if (ip_mc_check_igmp(skb, NULL) < 0)
615 		return false;
616 
617 	switch (igmp_hdr(skb)->type) {
618 	case IGMP_HOST_MEMBERSHIP_REPORT:
619 	case IGMPV2_HOST_MEMBERSHIP_REPORT:
620 	case IGMPV3_HOST_MEMBERSHIP_REPORT:
621 		return true;
622 	}
623 
624 	return false;
625 }
626 
627 /**
628  * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
629  * @bat_priv: the bat priv with all the soft interface information
630  * @skb: the IPv4 packet to check
631  * @is_unsnoopable: stores whether the destination is snoopable
632  *
633  * Checks whether the given IPv4 packet has the potential to be forwarded with a
634  * mode more optimal than classic flooding.
635  *
636  * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
637  * allocation failure.
638  */
batadv_mcast_forw_mode_check_ipv4(struct batadv_priv * bat_priv,struct sk_buff * skb,bool * is_unsnoopable)639 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
640 					     struct sk_buff *skb,
641 					     bool *is_unsnoopable)
642 {
643 	struct iphdr *iphdr;
644 
645 	/* We might fail due to out-of-memory -> drop it */
646 	if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
647 		return -ENOMEM;
648 
649 	if (batadv_mcast_is_report_ipv4(skb))
650 		return -EINVAL;
651 
652 	iphdr = ip_hdr(skb);
653 
654 	/* TODO: Implement Multicast Router Discovery (RFC4286),
655 	 * then allow scope > link local, too
656 	 */
657 	if (!ipv4_is_local_multicast(iphdr->daddr))
658 		return -EINVAL;
659 
660 	/* link-local multicast listeners behind a bridge are
661 	 * not snoopable (see RFC4541, section 2.1.2.2)
662 	 */
663 	*is_unsnoopable = true;
664 
665 	return 0;
666 }
667 
668 /**
669  * batadv_mcast_is_report_ipv6 - check for MLD reports
670  * @skb: the ethernet frame destined for the mesh
671  *
672  * This call might reallocate skb data.
673  *
674  * Checks whether the given frame is a valid MLD report.
675  *
676  * Return: If so then true, otherwise false.
677  */
batadv_mcast_is_report_ipv6(struct sk_buff * skb)678 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
679 {
680 	if (ipv6_mc_check_mld(skb, NULL) < 0)
681 		return false;
682 
683 	switch (icmp6_hdr(skb)->icmp6_type) {
684 	case ICMPV6_MGM_REPORT:
685 	case ICMPV6_MLD2_REPORT:
686 		return true;
687 	}
688 
689 	return false;
690 }
691 
692 /**
693  * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
694  * @bat_priv: the bat priv with all the soft interface information
695  * @skb: the IPv6 packet to check
696  * @is_unsnoopable: stores whether the destination is snoopable
697  *
698  * Checks whether the given IPv6 packet has the potential to be forwarded with a
699  * mode more optimal than classic flooding.
700  *
701  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
702  */
batadv_mcast_forw_mode_check_ipv6(struct batadv_priv * bat_priv,struct sk_buff * skb,bool * is_unsnoopable)703 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
704 					     struct sk_buff *skb,
705 					     bool *is_unsnoopable)
706 {
707 	struct ipv6hdr *ip6hdr;
708 
709 	/* We might fail due to out-of-memory -> drop it */
710 	if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
711 		return -ENOMEM;
712 
713 	if (batadv_mcast_is_report_ipv6(skb))
714 		return -EINVAL;
715 
716 	ip6hdr = ipv6_hdr(skb);
717 
718 	/* TODO: Implement Multicast Router Discovery (RFC4286),
719 	 * then allow scope > link local, too
720 	 */
721 	if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
722 		return -EINVAL;
723 
724 	/* link-local-all-nodes multicast listeners behind a bridge are
725 	 * not snoopable (see RFC4541, section 3, paragraph 3)
726 	 */
727 	if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
728 		*is_unsnoopable = true;
729 
730 	return 0;
731 }
732 
733 /**
734  * batadv_mcast_forw_mode_check - check for optimized forwarding potential
735  * @bat_priv: the bat priv with all the soft interface information
736  * @skb: the multicast frame to check
737  * @is_unsnoopable: stores whether the destination is snoopable
738  *
739  * Checks whether the given multicast ethernet frame has the potential to be
740  * forwarded with a mode more optimal than classic flooding.
741  *
742  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
743  */
batadv_mcast_forw_mode_check(struct batadv_priv * bat_priv,struct sk_buff * skb,bool * is_unsnoopable)744 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
745 					struct sk_buff *skb,
746 					bool *is_unsnoopable)
747 {
748 	struct ethhdr *ethhdr = eth_hdr(skb);
749 
750 	if (!atomic_read(&bat_priv->multicast_mode))
751 		return -EINVAL;
752 
753 	if (atomic_read(&bat_priv->mcast.num_disabled))
754 		return -EINVAL;
755 
756 	switch (ntohs(ethhdr->h_proto)) {
757 	case ETH_P_IP:
758 		return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
759 							 is_unsnoopable);
760 	case ETH_P_IPV6:
761 		if (!IS_ENABLED(CONFIG_IPV6))
762 			return -EINVAL;
763 
764 		return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
765 							 is_unsnoopable);
766 	default:
767 		return -EINVAL;
768 	}
769 }
770 
771 /**
772  * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
773  *  interest
774  * @bat_priv: the bat priv with all the soft interface information
775  * @ethhdr: ethernet header of a packet
776  *
777  * Return: the number of nodes which want all IPv4 multicast traffic if the
778  * given ethhdr is from an IPv4 packet or the number of nodes which want all
779  * IPv6 traffic if it matches an IPv6 packet.
780  */
batadv_mcast_forw_want_all_ip_count(struct batadv_priv * bat_priv,struct ethhdr * ethhdr)781 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
782 					       struct ethhdr *ethhdr)
783 {
784 	switch (ntohs(ethhdr->h_proto)) {
785 	case ETH_P_IP:
786 		return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
787 	case ETH_P_IPV6:
788 		return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
789 	default:
790 		/* we shouldn't be here... */
791 		return 0;
792 	}
793 }
794 
795 /**
796  * batadv_mcast_forw_tt_node_get - get a multicast tt node
797  * @bat_priv: the bat priv with all the soft interface information
798  * @ethhdr: the ether header containing the multicast destination
799  *
800  * Return: an orig_node matching the multicast address provided by ethhdr
801  * via a translation table lookup. This increases the returned nodes refcount.
802  */
803 static struct batadv_orig_node *
batadv_mcast_forw_tt_node_get(struct batadv_priv * bat_priv,struct ethhdr * ethhdr)804 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
805 			      struct ethhdr *ethhdr)
806 {
807 	return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
808 					BATADV_NO_FLAGS);
809 }
810 
811 /**
812  * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
813  * @bat_priv: the bat priv with all the soft interface information
814  *
815  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
816  * increases its refcount.
817  */
818 static struct batadv_orig_node *
batadv_mcast_forw_ipv4_node_get(struct batadv_priv * bat_priv)819 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
820 {
821 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
822 
823 	rcu_read_lock();
824 	hlist_for_each_entry_rcu(tmp_orig_node,
825 				 &bat_priv->mcast.want_all_ipv4_list,
826 				 mcast_want_all_ipv4_node) {
827 		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
828 			continue;
829 
830 		orig_node = tmp_orig_node;
831 		break;
832 	}
833 	rcu_read_unlock();
834 
835 	return orig_node;
836 }
837 
838 /**
839  * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
840  * @bat_priv: the bat priv with all the soft interface information
841  *
842  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
843  * and increases its refcount.
844  */
845 static struct batadv_orig_node *
batadv_mcast_forw_ipv6_node_get(struct batadv_priv * bat_priv)846 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
847 {
848 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
849 
850 	rcu_read_lock();
851 	hlist_for_each_entry_rcu(tmp_orig_node,
852 				 &bat_priv->mcast.want_all_ipv6_list,
853 				 mcast_want_all_ipv6_node) {
854 		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
855 			continue;
856 
857 		orig_node = tmp_orig_node;
858 		break;
859 	}
860 	rcu_read_unlock();
861 
862 	return orig_node;
863 }
864 
865 /**
866  * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
867  * @bat_priv: the bat priv with all the soft interface information
868  * @ethhdr: an ethernet header to determine the protocol family from
869  *
870  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
871  * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
872  * increases its refcount.
873  */
874 static struct batadv_orig_node *
batadv_mcast_forw_ip_node_get(struct batadv_priv * bat_priv,struct ethhdr * ethhdr)875 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
876 			      struct ethhdr *ethhdr)
877 {
878 	switch (ntohs(ethhdr->h_proto)) {
879 	case ETH_P_IP:
880 		return batadv_mcast_forw_ipv4_node_get(bat_priv);
881 	case ETH_P_IPV6:
882 		return batadv_mcast_forw_ipv6_node_get(bat_priv);
883 	default:
884 		/* we shouldn't be here... */
885 		return NULL;
886 	}
887 }
888 
889 /**
890  * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
891  * @bat_priv: the bat priv with all the soft interface information
892  *
893  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
894  * set and increases its refcount.
895  */
896 static struct batadv_orig_node *
batadv_mcast_forw_unsnoop_node_get(struct batadv_priv * bat_priv)897 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
898 {
899 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
900 
901 	rcu_read_lock();
902 	hlist_for_each_entry_rcu(tmp_orig_node,
903 				 &bat_priv->mcast.want_all_unsnoopables_list,
904 				 mcast_want_all_unsnoopables_node) {
905 		if (!kref_get_unless_zero(&tmp_orig_node->refcount))
906 			continue;
907 
908 		orig_node = tmp_orig_node;
909 		break;
910 	}
911 	rcu_read_unlock();
912 
913 	return orig_node;
914 }
915 
916 /**
917  * batadv_mcast_forw_mode - check on how to forward a multicast packet
918  * @bat_priv: the bat priv with all the soft interface information
919  * @skb: The multicast packet to check
920  * @orig: an originator to be set to forward the skb to
921  *
922  * Return: the forwarding mode as enum batadv_forw_mode and in case of
923  * BATADV_FORW_SINGLE set the orig to the single originator the skb
924  * should be forwarded to.
925  */
926 enum batadv_forw_mode
batadv_mcast_forw_mode(struct batadv_priv * bat_priv,struct sk_buff * skb,struct batadv_orig_node ** orig)927 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
928 		       struct batadv_orig_node **orig)
929 {
930 	int ret, tt_count, ip_count, unsnoop_count, total_count;
931 	bool is_unsnoopable = false;
932 	struct ethhdr *ethhdr;
933 
934 	ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
935 	if (ret == -ENOMEM)
936 		return BATADV_FORW_NONE;
937 	else if (ret < 0)
938 		return BATADV_FORW_ALL;
939 
940 	ethhdr = eth_hdr(skb);
941 
942 	tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
943 					       BATADV_NO_FLAGS);
944 	ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
945 	unsnoop_count = !is_unsnoopable ? 0 :
946 			atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
947 
948 	total_count = tt_count + ip_count + unsnoop_count;
949 
950 	switch (total_count) {
951 	case 1:
952 		if (tt_count)
953 			*orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
954 		else if (ip_count)
955 			*orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
956 		else if (unsnoop_count)
957 			*orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
958 
959 		if (*orig)
960 			return BATADV_FORW_SINGLE;
961 
962 		/* fall through */
963 	case 0:
964 		return BATADV_FORW_NONE;
965 	default:
966 		return BATADV_FORW_ALL;
967 	}
968 }
969 
970 /**
971  * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
972  * @bat_priv: the bat priv with all the soft interface information
973  * @orig: the orig_node which multicast state might have changed of
974  * @mcast_flags: flags indicating the new multicast state
975  *
976  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
977  * orig, has toggled then this method updates counter and list accordingly.
978  *
979  * Caller needs to hold orig->mcast_handler_lock.
980  */
batadv_mcast_want_unsnoop_update(struct batadv_priv * bat_priv,struct batadv_orig_node * orig,u8 mcast_flags)981 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
982 					     struct batadv_orig_node *orig,
983 					     u8 mcast_flags)
984 {
985 	struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
986 	struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
987 
988 	lockdep_assert_held(&orig->mcast_handler_lock);
989 
990 	/* switched from flag unset to set */
991 	if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
992 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
993 		atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
994 
995 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
996 		/* flag checks above + mcast_handler_lock prevents this */
997 		WARN_ON(!hlist_unhashed(node));
998 
999 		hlist_add_head_rcu(node, head);
1000 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1001 	/* switched from flag set to unset */
1002 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1003 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1004 		atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1005 
1006 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1007 		/* flag checks above + mcast_handler_lock prevents this */
1008 		WARN_ON(hlist_unhashed(node));
1009 
1010 		hlist_del_init_rcu(node);
1011 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1012 	}
1013 }
1014 
1015 /**
1016  * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
1017  * @bat_priv: the bat priv with all the soft interface information
1018  * @orig: the orig_node which multicast state might have changed of
1019  * @mcast_flags: flags indicating the new multicast state
1020  *
1021  * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1022  * toggled then this method updates counter and list accordingly.
1023  *
1024  * Caller needs to hold orig->mcast_handler_lock.
1025  */
batadv_mcast_want_ipv4_update(struct batadv_priv * bat_priv,struct batadv_orig_node * orig,u8 mcast_flags)1026 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1027 					  struct batadv_orig_node *orig,
1028 					  u8 mcast_flags)
1029 {
1030 	struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1031 	struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1032 
1033 	lockdep_assert_held(&orig->mcast_handler_lock);
1034 
1035 	/* switched from flag unset to set */
1036 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1037 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1038 		atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1039 
1040 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1041 		/* flag checks above + mcast_handler_lock prevents this */
1042 		WARN_ON(!hlist_unhashed(node));
1043 
1044 		hlist_add_head_rcu(node, head);
1045 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1046 	/* switched from flag set to unset */
1047 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1048 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1049 		atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1050 
1051 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1052 		/* flag checks above + mcast_handler_lock prevents this */
1053 		WARN_ON(hlist_unhashed(node));
1054 
1055 		hlist_del_init_rcu(node);
1056 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1057 	}
1058 }
1059 
1060 /**
1061  * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1062  * @bat_priv: the bat priv with all the soft interface information
1063  * @orig: the orig_node which multicast state might have changed of
1064  * @mcast_flags: flags indicating the new multicast state
1065  *
1066  * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1067  * toggled then this method updates counter and list accordingly.
1068  *
1069  * Caller needs to hold orig->mcast_handler_lock.
1070  */
batadv_mcast_want_ipv6_update(struct batadv_priv * bat_priv,struct batadv_orig_node * orig,u8 mcast_flags)1071 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1072 					  struct batadv_orig_node *orig,
1073 					  u8 mcast_flags)
1074 {
1075 	struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1076 	struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1077 
1078 	lockdep_assert_held(&orig->mcast_handler_lock);
1079 
1080 	/* switched from flag unset to set */
1081 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1082 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1083 		atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1084 
1085 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1086 		/* flag checks above + mcast_handler_lock prevents this */
1087 		WARN_ON(!hlist_unhashed(node));
1088 
1089 		hlist_add_head_rcu(node, head);
1090 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1091 	/* switched from flag set to unset */
1092 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1093 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1094 		atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1095 
1096 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1097 		/* flag checks above + mcast_handler_lock prevents this */
1098 		WARN_ON(hlist_unhashed(node));
1099 
1100 		hlist_del_init_rcu(node);
1101 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1102 	}
1103 }
1104 
1105 /**
1106  * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
1107  * @bat_priv: the bat priv with all the soft interface information
1108  * @orig: the orig_node of the ogm
1109  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1110  * @tvlv_value: tvlv buffer containing the multicast data
1111  * @tvlv_value_len: tvlv buffer length
1112  */
batadv_mcast_tvlv_ogm_handler(struct batadv_priv * bat_priv,struct batadv_orig_node * orig,u8 flags,void * tvlv_value,u16 tvlv_value_len)1113 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1114 					  struct batadv_orig_node *orig,
1115 					  u8 flags,
1116 					  void *tvlv_value,
1117 					  u16 tvlv_value_len)
1118 {
1119 	bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1120 	u8 mcast_flags = BATADV_NO_FLAGS;
1121 	bool orig_initialized;
1122 
1123 	if (orig_mcast_enabled && tvlv_value &&
1124 	    (tvlv_value_len >= sizeof(mcast_flags)))
1125 		mcast_flags = *(u8 *)tvlv_value;
1126 
1127 	spin_lock_bh(&orig->mcast_handler_lock);
1128 	orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1129 				    &orig->capa_initialized);
1130 
1131 	/* If mcast support is turned on decrease the disabled mcast node
1132 	 * counter only if we had increased it for this node before. If this
1133 	 * is a completely new orig_node no need to decrease the counter.
1134 	 */
1135 	if (orig_mcast_enabled &&
1136 	    !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1137 		if (orig_initialized)
1138 			atomic_dec(&bat_priv->mcast.num_disabled);
1139 		set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1140 	/* If mcast support is being switched off or if this is an initial
1141 	 * OGM without mcast support then increase the disabled mcast
1142 	 * node counter.
1143 	 */
1144 	} else if (!orig_mcast_enabled &&
1145 		   (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1146 		    !orig_initialized)) {
1147 		atomic_inc(&bat_priv->mcast.num_disabled);
1148 		clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1149 	}
1150 
1151 	set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1152 
1153 	batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1154 	batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1155 	batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1156 
1157 	orig->mcast_flags = mcast_flags;
1158 	spin_unlock_bh(&orig->mcast_handler_lock);
1159 }
1160 
1161 /**
1162  * batadv_mcast_init - initialize the multicast optimizations structures
1163  * @bat_priv: the bat priv with all the soft interface information
1164  */
batadv_mcast_init(struct batadv_priv * bat_priv)1165 void batadv_mcast_init(struct batadv_priv *bat_priv)
1166 {
1167 	batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1168 				     NULL, BATADV_TVLV_MCAST, 2,
1169 				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1170 
1171 	INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1172 	batadv_mcast_start_timer(bat_priv);
1173 }
1174 
1175 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1176 /**
1177  * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1178  * @bat_priv: the bat priv with all the soft interface information
1179  * @seq: debugfs table seq_file struct
1180  *
1181  * Prints our own multicast flags including a more specific reason why
1182  * they are set, that is prints the bridge and querier state too, to
1183  * the debugfs table specified via @seq.
1184  */
batadv_mcast_flags_print_header(struct batadv_priv * bat_priv,struct seq_file * seq)1185 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1186 					    struct seq_file *seq)
1187 {
1188 	u8 flags = bat_priv->mcast.flags;
1189 	char querier4, querier6, shadowing4, shadowing6;
1190 	bool bridged = bat_priv->mcast.bridged;
1191 
1192 	if (bridged) {
1193 		querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1194 		querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1195 		shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1196 		shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1197 	} else {
1198 		querier4 = '?';
1199 		querier6 = '?';
1200 		shadowing4 = '?';
1201 		shadowing6 = '?';
1202 	}
1203 
1204 	seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1205 		   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1206 		   (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1207 		   (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1208 	seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1209 	seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1210 		   querier4, querier6);
1211 	seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1212 		   shadowing4, shadowing6);
1213 	seq_puts(seq, "-------------------------------------------\n");
1214 	seq_printf(seq, "       %-10s %s\n", "Originator", "Flags");
1215 }
1216 
1217 /**
1218  * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1219  * @seq: seq file to print on
1220  * @offset: not used
1221  *
1222  * This prints a table of (primary) originators and their according
1223  * multicast flags, including (in the header) our own.
1224  *
1225  * Return: always 0
1226  */
batadv_mcast_flags_seq_print_text(struct seq_file * seq,void * offset)1227 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1228 {
1229 	struct net_device *net_dev = (struct net_device *)seq->private;
1230 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1231 	struct batadv_hard_iface *primary_if;
1232 	struct batadv_hashtable *hash = bat_priv->orig_hash;
1233 	struct batadv_orig_node *orig_node;
1234 	struct hlist_head *head;
1235 	u8 flags;
1236 	u32 i;
1237 
1238 	primary_if = batadv_seq_print_text_primary_if_get(seq);
1239 	if (!primary_if)
1240 		return 0;
1241 
1242 	batadv_mcast_flags_print_header(bat_priv, seq);
1243 
1244 	for (i = 0; i < hash->size; i++) {
1245 		head = &hash->table[i];
1246 
1247 		rcu_read_lock();
1248 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1249 			if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1250 				      &orig_node->capa_initialized))
1251 				continue;
1252 
1253 			if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1254 				      &orig_node->capabilities)) {
1255 				seq_printf(seq, "%pM -\n", orig_node->orig);
1256 				continue;
1257 			}
1258 
1259 			flags = orig_node->mcast_flags;
1260 
1261 			seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1262 				   (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1263 				   ? 'U' : '.',
1264 				   (flags & BATADV_MCAST_WANT_ALL_IPV4)
1265 				   ? '4' : '.',
1266 				   (flags & BATADV_MCAST_WANT_ALL_IPV6)
1267 				   ? '6' : '.');
1268 		}
1269 		rcu_read_unlock();
1270 	}
1271 
1272 	batadv_hardif_put(primary_if);
1273 
1274 	return 0;
1275 }
1276 #endif
1277 
1278 /**
1279  * batadv_mcast_free - free the multicast optimizations structures
1280  * @bat_priv: the bat priv with all the soft interface information
1281  */
batadv_mcast_free(struct batadv_priv * bat_priv)1282 void batadv_mcast_free(struct batadv_priv *bat_priv)
1283 {
1284 	cancel_delayed_work_sync(&bat_priv->mcast.work);
1285 
1286 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1287 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1288 
1289 	/* safely calling outside of worker, as worker was canceled above */
1290 	batadv_mcast_mla_tt_retract(bat_priv, NULL);
1291 }
1292 
1293 /**
1294  * batadv_mcast_purge_orig - reset originator global mcast state modifications
1295  * @orig: the originator which is going to get purged
1296  */
batadv_mcast_purge_orig(struct batadv_orig_node * orig)1297 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1298 {
1299 	struct batadv_priv *bat_priv = orig->bat_priv;
1300 
1301 	spin_lock_bh(&orig->mcast_handler_lock);
1302 
1303 	if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1304 	    test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1305 		atomic_dec(&bat_priv->mcast.num_disabled);
1306 
1307 	batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1308 	batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1309 	batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1310 
1311 	spin_unlock_bh(&orig->mcast_handler_lock);
1312 }
1313