xref: /openbmc/linux/net/batman-adv/multicast.c (revision c0e297dc)
1 /* Copyright (C) 2014-2015 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/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/etherdevice.h>
25 #include <linux/fs.h>
26 #include <linux/if_ether.h>
27 #include <linux/in6.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/ipv6.h>
31 #include <linux/list.h>
32 #include <linux/netdevice.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/skbuff.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/stddef.h>
39 #include <linux/string.h>
40 #include <linux/types.h>
41 #include <net/addrconf.h>
42 #include <net/ipv6.h>
43 
44 #include "packet.h"
45 #include "translation-table.h"
46 
47 /**
48  * batadv_mcast_mla_softif_get - get softif multicast listeners
49  * @dev: the device to collect multicast addresses from
50  * @mcast_list: a list to put found addresses into
51  *
52  * Collect multicast addresses of the local multicast listeners
53  * on the given soft interface, dev, in the given mcast_list.
54  *
55  * Returns -ENOMEM on memory allocation error or the number of
56  * items added to the mcast_list otherwise.
57  */
58 static int batadv_mcast_mla_softif_get(struct net_device *dev,
59 				       struct hlist_head *mcast_list)
60 {
61 	struct netdev_hw_addr *mc_list_entry;
62 	struct batadv_hw_addr *new;
63 	int ret = 0;
64 
65 	netif_addr_lock_bh(dev);
66 	netdev_for_each_mc_addr(mc_list_entry, dev) {
67 		new = kmalloc(sizeof(*new), GFP_ATOMIC);
68 		if (!new) {
69 			ret = -ENOMEM;
70 			break;
71 		}
72 
73 		ether_addr_copy(new->addr, mc_list_entry->addr);
74 		hlist_add_head(&new->list, mcast_list);
75 		ret++;
76 	}
77 	netif_addr_unlock_bh(dev);
78 
79 	return ret;
80 }
81 
82 /**
83  * batadv_mcast_mla_is_duplicate - check whether an address is in a list
84  * @mcast_addr: the multicast address to check
85  * @mcast_list: the list with multicast addresses to search in
86  *
87  * Returns true if the given address is already in the given list.
88  * Otherwise returns false.
89  */
90 static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
91 					  struct hlist_head *mcast_list)
92 {
93 	struct batadv_hw_addr *mcast_entry;
94 
95 	hlist_for_each_entry(mcast_entry, mcast_list, list)
96 		if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
97 			return true;
98 
99 	return false;
100 }
101 
102 /**
103  * batadv_mcast_mla_list_free - free a list of multicast addresses
104  * @mcast_list: the list to free
105  *
106  * Removes and frees all items in the given mcast_list.
107  */
108 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
109 {
110 	struct batadv_hw_addr *mcast_entry;
111 	struct hlist_node *tmp;
112 
113 	hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
114 		hlist_del(&mcast_entry->list);
115 		kfree(mcast_entry);
116 	}
117 }
118 
119 /**
120  * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
121  * @bat_priv: the bat priv with all the soft interface information
122  * @mcast_list: a list of addresses which should _not_ be removed
123  *
124  * Retracts the announcement of any multicast listener from the
125  * translation table except the ones listed in the given mcast_list.
126  *
127  * If mcast_list is NULL then all are retracted.
128  */
129 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
130 					struct hlist_head *mcast_list)
131 {
132 	struct batadv_hw_addr *mcast_entry;
133 	struct hlist_node *tmp;
134 
135 	hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
136 				  list) {
137 		if (mcast_list &&
138 		    batadv_mcast_mla_is_duplicate(mcast_entry->addr,
139 						  mcast_list))
140 			continue;
141 
142 		batadv_tt_local_remove(bat_priv, mcast_entry->addr,
143 				       BATADV_NO_FLAGS,
144 				       "mcast TT outdated", false);
145 
146 		hlist_del(&mcast_entry->list);
147 		kfree(mcast_entry);
148 	}
149 }
150 
151 /**
152  * batadv_mcast_mla_tt_add - add multicast listener announcements
153  * @bat_priv: the bat priv with all the soft interface information
154  * @mcast_list: a list of addresses which are going to get added
155  *
156  * Adds multicast listener announcements from the given mcast_list to the
157  * translation table if they have not been added yet.
158  */
159 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
160 				    struct hlist_head *mcast_list)
161 {
162 	struct batadv_hw_addr *mcast_entry;
163 	struct hlist_node *tmp;
164 
165 	if (!mcast_list)
166 		return;
167 
168 	hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
169 		if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
170 						  &bat_priv->mcast.mla_list))
171 			continue;
172 
173 		if (!batadv_tt_local_add(bat_priv->soft_iface,
174 					 mcast_entry->addr, BATADV_NO_FLAGS,
175 					 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
176 			continue;
177 
178 		hlist_del(&mcast_entry->list);
179 		hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
180 	}
181 }
182 
183 /**
184  * batadv_mcast_has_bridge - check whether the soft-iface is bridged
185  * @bat_priv: the bat priv with all the soft interface information
186  *
187  * Checks whether there is a bridge on top of our soft interface. Returns
188  * true if so, false otherwise.
189  */
190 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
191 {
192 	struct net_device *upper = bat_priv->soft_iface;
193 
194 	rcu_read_lock();
195 	do {
196 		upper = netdev_master_upper_dev_get_rcu(upper);
197 	} while (upper && !(upper->priv_flags & IFF_EBRIDGE));
198 	rcu_read_unlock();
199 
200 	return upper;
201 }
202 
203 /**
204  * batadv_mcast_mla_tvlv_update - update multicast tvlv
205  * @bat_priv: the bat priv with all the soft interface information
206  *
207  * Updates the own multicast tvlv with our current multicast related settings,
208  * capabilities and inabilities.
209  *
210  * Returns true if the tvlv container is registered afterwards. Otherwise
211  * returns false.
212  */
213 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
214 {
215 	struct batadv_tvlv_mcast_data mcast_data;
216 
217 	mcast_data.flags = BATADV_NO_FLAGS;
218 	memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
219 
220 	/* Avoid attaching MLAs, if there is a bridge on top of our soft
221 	 * interface, we don't support that yet (TODO)
222 	 */
223 	if (batadv_mcast_has_bridge(bat_priv)) {
224 		if (bat_priv->mcast.enabled) {
225 			batadv_tvlv_container_unregister(bat_priv,
226 							 BATADV_TVLV_MCAST, 1);
227 			bat_priv->mcast.enabled = false;
228 		}
229 
230 		return false;
231 	}
232 
233 	if (!bat_priv->mcast.enabled ||
234 	    mcast_data.flags != bat_priv->mcast.flags) {
235 		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
236 					       &mcast_data, sizeof(mcast_data));
237 		bat_priv->mcast.flags = mcast_data.flags;
238 		bat_priv->mcast.enabled = true;
239 	}
240 
241 	return true;
242 }
243 
244 /**
245  * batadv_mcast_mla_update - update the own MLAs
246  * @bat_priv: the bat priv with all the soft interface information
247  *
248  * Updates the own multicast listener announcements in the translation
249  * table as well as the own, announced multicast tvlv container.
250  */
251 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
252 {
253 	struct net_device *soft_iface = bat_priv->soft_iface;
254 	struct hlist_head mcast_list = HLIST_HEAD_INIT;
255 	int ret;
256 
257 	if (!batadv_mcast_mla_tvlv_update(bat_priv))
258 		goto update;
259 
260 	ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
261 	if (ret < 0)
262 		goto out;
263 
264 update:
265 	batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
266 	batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
267 
268 out:
269 	batadv_mcast_mla_list_free(&mcast_list);
270 }
271 
272 /**
273  * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
274  * @bat_priv: the bat priv with all the soft interface information
275  * @skb: the IPv4 packet to check
276  * @is_unsnoopable: stores whether the destination is snoopable
277  *
278  * Checks whether the given IPv4 packet has the potential to be forwarded with a
279  * mode more optimal than classic flooding.
280  *
281  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM in case of
282  * memory allocation failure.
283  */
284 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
285 					     struct sk_buff *skb,
286 					     bool *is_unsnoopable)
287 {
288 	struct iphdr *iphdr;
289 
290 	/* We might fail due to out-of-memory -> drop it */
291 	if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
292 		return -ENOMEM;
293 
294 	iphdr = ip_hdr(skb);
295 
296 	/* TODO: Implement Multicast Router Discovery (RFC4286),
297 	 * then allow scope > link local, too
298 	 */
299 	if (!ipv4_is_local_multicast(iphdr->daddr))
300 		return -EINVAL;
301 
302 	/* link-local multicast listeners behind a bridge are
303 	 * not snoopable (see RFC4541, section 2.1.2.2)
304 	 */
305 	*is_unsnoopable = true;
306 
307 	return 0;
308 }
309 
310 /**
311  * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
312  * @bat_priv: the bat priv with all the soft interface information
313  * @skb: the IPv6 packet to check
314  * @is_unsnoopable: stores whether the destination is snoopable
315  *
316  * Checks whether the given IPv6 packet has the potential to be forwarded with a
317  * mode more optimal than classic flooding.
318  *
319  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
320  * of memory.
321  */
322 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
323 					     struct sk_buff *skb,
324 					     bool *is_unsnoopable)
325 {
326 	struct ipv6hdr *ip6hdr;
327 
328 	/* We might fail due to out-of-memory -> drop it */
329 	if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
330 		return -ENOMEM;
331 
332 	ip6hdr = ipv6_hdr(skb);
333 
334 	/* TODO: Implement Multicast Router Discovery (RFC4286),
335 	 * then allow scope > link local, too
336 	 */
337 	if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
338 		return -EINVAL;
339 
340 	/* link-local-all-nodes multicast listeners behind a bridge are
341 	 * not snoopable (see RFC4541, section 3, paragraph 3)
342 	 */
343 	if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
344 		*is_unsnoopable = true;
345 
346 	return 0;
347 }
348 
349 /**
350  * batadv_mcast_forw_mode_check - check for optimized forwarding potential
351  * @bat_priv: the bat priv with all the soft interface information
352  * @skb: the multicast frame to check
353  * @is_unsnoopable: stores whether the destination is snoopable
354  *
355  * Checks whether the given multicast ethernet frame has the potential to be
356  * forwarded with a mode more optimal than classic flooding.
357  *
358  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
359  * of memory.
360  */
361 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
362 					struct sk_buff *skb,
363 					bool *is_unsnoopable)
364 {
365 	struct ethhdr *ethhdr = eth_hdr(skb);
366 
367 	if (!atomic_read(&bat_priv->multicast_mode))
368 		return -EINVAL;
369 
370 	if (atomic_read(&bat_priv->mcast.num_disabled))
371 		return -EINVAL;
372 
373 	switch (ntohs(ethhdr->h_proto)) {
374 	case ETH_P_IP:
375 		return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
376 							 is_unsnoopable);
377 	case ETH_P_IPV6:
378 		return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
379 							 is_unsnoopable);
380 	default:
381 		return -EINVAL;
382 	}
383 }
384 
385 /**
386  * batadv_mcast_want_all_ip_count - count nodes with unspecific mcast interest
387  * @bat_priv: the bat priv with all the soft interface information
388  * @ethhdr: ethernet header of a packet
389  *
390  * Returns the number of nodes which want all IPv4 multicast traffic if the
391  * given ethhdr is from an IPv4 packet or the number of nodes which want all
392  * IPv6 traffic if it matches an IPv6 packet.
393  */
394 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
395 					       struct ethhdr *ethhdr)
396 {
397 	switch (ntohs(ethhdr->h_proto)) {
398 	case ETH_P_IP:
399 		return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
400 	case ETH_P_IPV6:
401 		return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
402 	default:
403 		/* we shouldn't be here... */
404 		return 0;
405 	}
406 }
407 
408 /**
409  * batadv_mcast_forw_tt_node_get - get a multicast tt node
410  * @bat_priv: the bat priv with all the soft interface information
411  * @ethhdr: the ether header containing the multicast destination
412  *
413  * Returns an orig_node matching the multicast address provided by ethhdr
414  * via a translation table lookup. This increases the returned nodes refcount.
415  */
416 static struct batadv_orig_node *
417 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
418 			      struct ethhdr *ethhdr)
419 {
420 	return batadv_transtable_search(bat_priv, ethhdr->h_source,
421 					ethhdr->h_dest, BATADV_NO_FLAGS);
422 }
423 
424 /**
425  * batadv_mcast_want_forw_ipv4_node_get - get a node with an ipv4 flag
426  * @bat_priv: the bat priv with all the soft interface information
427  *
428  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
429  * increases its refcount.
430  */
431 static struct batadv_orig_node *
432 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
433 {
434 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
435 
436 	rcu_read_lock();
437 	hlist_for_each_entry_rcu(tmp_orig_node,
438 				 &bat_priv->mcast.want_all_ipv4_list,
439 				 mcast_want_all_ipv4_node) {
440 		if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
441 			continue;
442 
443 		orig_node = tmp_orig_node;
444 		break;
445 	}
446 	rcu_read_unlock();
447 
448 	return orig_node;
449 }
450 
451 /**
452  * batadv_mcast_want_forw_ipv6_node_get - get a node with an ipv6 flag
453  * @bat_priv: the bat priv with all the soft interface information
454  *
455  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
456  * and increases its refcount.
457  */
458 static struct batadv_orig_node *
459 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
460 {
461 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
462 
463 	rcu_read_lock();
464 	hlist_for_each_entry_rcu(tmp_orig_node,
465 				 &bat_priv->mcast.want_all_ipv6_list,
466 				 mcast_want_all_ipv6_node) {
467 		if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
468 			continue;
469 
470 		orig_node = tmp_orig_node;
471 		break;
472 	}
473 	rcu_read_unlock();
474 
475 	return orig_node;
476 }
477 
478 /**
479  * batadv_mcast_want_forw_ip_node_get - get a node with an ipv4/ipv6 flag
480  * @bat_priv: the bat priv with all the soft interface information
481  * @ethhdr: an ethernet header to determine the protocol family from
482  *
483  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
484  * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
485  * increases its refcount.
486  */
487 static struct batadv_orig_node *
488 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
489 			      struct ethhdr *ethhdr)
490 {
491 	switch (ntohs(ethhdr->h_proto)) {
492 	case ETH_P_IP:
493 		return batadv_mcast_forw_ipv4_node_get(bat_priv);
494 	case ETH_P_IPV6:
495 		return batadv_mcast_forw_ipv6_node_get(bat_priv);
496 	default:
497 		/* we shouldn't be here... */
498 		return NULL;
499 	}
500 }
501 
502 /**
503  * batadv_mcast_want_forw_unsnoop_node_get - get a node with an unsnoopable flag
504  * @bat_priv: the bat priv with all the soft interface information
505  *
506  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
507  * set and increases its refcount.
508  */
509 static struct batadv_orig_node *
510 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
511 {
512 	struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
513 
514 	rcu_read_lock();
515 	hlist_for_each_entry_rcu(tmp_orig_node,
516 				 &bat_priv->mcast.want_all_unsnoopables_list,
517 				 mcast_want_all_unsnoopables_node) {
518 		if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
519 			continue;
520 
521 		orig_node = tmp_orig_node;
522 		break;
523 	}
524 	rcu_read_unlock();
525 
526 	return orig_node;
527 }
528 
529 /**
530  * batadv_mcast_forw_mode - check on how to forward a multicast packet
531  * @bat_priv: the bat priv with all the soft interface information
532  * @skb: The multicast packet to check
533  * @orig: an originator to be set to forward the skb to
534  *
535  * Returns the forwarding mode as enum batadv_forw_mode and in case of
536  * BATADV_FORW_SINGLE set the orig to the single originator the skb
537  * should be forwarded to.
538  */
539 enum batadv_forw_mode
540 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
541 		       struct batadv_orig_node **orig)
542 {
543 	int ret, tt_count, ip_count, unsnoop_count, total_count;
544 	bool is_unsnoopable = false;
545 	struct ethhdr *ethhdr;
546 
547 	ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
548 	if (ret == -ENOMEM)
549 		return BATADV_FORW_NONE;
550 	else if (ret < 0)
551 		return BATADV_FORW_ALL;
552 
553 	ethhdr = eth_hdr(skb);
554 
555 	tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
556 					       BATADV_NO_FLAGS);
557 	ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
558 	unsnoop_count = !is_unsnoopable ? 0 :
559 			atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
560 
561 	total_count = tt_count + ip_count + unsnoop_count;
562 
563 	switch (total_count) {
564 	case 1:
565 		if (tt_count)
566 			*orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
567 		else if (ip_count)
568 			*orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
569 		else if (unsnoop_count)
570 			*orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
571 
572 		if (*orig)
573 			return BATADV_FORW_SINGLE;
574 
575 		/* fall through */
576 	case 0:
577 		return BATADV_FORW_NONE;
578 	default:
579 		return BATADV_FORW_ALL;
580 	}
581 }
582 
583 /**
584  * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
585  * @bat_priv: the bat priv with all the soft interface information
586  * @orig: the orig_node which multicast state might have changed of
587  * @mcast_flags: flags indicating the new multicast state
588  *
589  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
590  * orig, has toggled then this method updates counter and list accordingly.
591  */
592 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
593 					     struct batadv_orig_node *orig,
594 					     uint8_t mcast_flags)
595 {
596 	/* switched from flag unset to set */
597 	if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
598 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
599 		atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
600 
601 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
602 		hlist_add_head_rcu(&orig->mcast_want_all_unsnoopables_node,
603 				   &bat_priv->mcast.want_all_unsnoopables_list);
604 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
605 	/* switched from flag set to unset */
606 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
607 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
608 		atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
609 
610 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
611 		hlist_del_rcu(&orig->mcast_want_all_unsnoopables_node);
612 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
613 	}
614 }
615 
616 /**
617  * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
618  * @bat_priv: the bat priv with all the soft interface information
619  * @orig: the orig_node which multicast state might have changed of
620  * @mcast_flags: flags indicating the new multicast state
621  *
622  * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
623  * toggled then this method updates counter and list accordingly.
624  */
625 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
626 					  struct batadv_orig_node *orig,
627 					  uint8_t mcast_flags)
628 {
629 	/* switched from flag unset to set */
630 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
631 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
632 		atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
633 
634 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
635 		hlist_add_head_rcu(&orig->mcast_want_all_ipv4_node,
636 				   &bat_priv->mcast.want_all_ipv4_list);
637 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
638 	/* switched from flag set to unset */
639 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
640 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
641 		atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
642 
643 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
644 		hlist_del_rcu(&orig->mcast_want_all_ipv4_node);
645 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
646 	}
647 }
648 
649 /**
650  * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
651  * @bat_priv: the bat priv with all the soft interface information
652  * @orig: the orig_node which multicast state might have changed of
653  * @mcast_flags: flags indicating the new multicast state
654  *
655  * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
656  * toggled then this method updates counter and list accordingly.
657  */
658 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
659 					  struct batadv_orig_node *orig,
660 					  uint8_t mcast_flags)
661 {
662 	/* switched from flag unset to set */
663 	if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
664 	    !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
665 		atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
666 
667 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
668 		hlist_add_head_rcu(&orig->mcast_want_all_ipv6_node,
669 				   &bat_priv->mcast.want_all_ipv6_list);
670 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
671 	/* switched from flag set to unset */
672 	} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
673 		   orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
674 		atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
675 
676 		spin_lock_bh(&bat_priv->mcast.want_lists_lock);
677 		hlist_del_rcu(&orig->mcast_want_all_ipv6_node);
678 		spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
679 	}
680 }
681 
682 /**
683  * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
684  * @bat_priv: the bat priv with all the soft interface information
685  * @orig: the orig_node of the ogm
686  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
687  * @tvlv_value: tvlv buffer containing the multicast data
688  * @tvlv_value_len: tvlv buffer length
689  */
690 static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
691 					     struct batadv_orig_node *orig,
692 					     uint8_t flags,
693 					     void *tvlv_value,
694 					     uint16_t tvlv_value_len)
695 {
696 	bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
697 	uint8_t mcast_flags = BATADV_NO_FLAGS;
698 	bool orig_initialized;
699 
700 	orig_initialized = orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST;
701 
702 	/* If mcast support is turned on decrease the disabled mcast node
703 	 * counter only if we had increased it for this node before. If this
704 	 * is a completely new orig_node no need to decrease the counter.
705 	 */
706 	if (orig_mcast_enabled &&
707 	    !(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST)) {
708 		if (orig_initialized)
709 			atomic_dec(&bat_priv->mcast.num_disabled);
710 		orig->capabilities |= BATADV_ORIG_CAPA_HAS_MCAST;
711 	/* If mcast support is being switched off or if this is an initial
712 	 * OGM without mcast support then increase the disabled mcast
713 	 * node counter.
714 	 */
715 	} else if (!orig_mcast_enabled &&
716 		   (orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST ||
717 		    !orig_initialized)) {
718 		atomic_inc(&bat_priv->mcast.num_disabled);
719 		orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_MCAST;
720 	}
721 
722 	orig->capa_initialized |= BATADV_ORIG_CAPA_HAS_MCAST;
723 
724 	if (orig_mcast_enabled && tvlv_value &&
725 	    (tvlv_value_len >= sizeof(mcast_flags)))
726 		mcast_flags = *(uint8_t *)tvlv_value;
727 
728 	batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
729 	batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
730 	batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
731 
732 	orig->mcast_flags = mcast_flags;
733 }
734 
735 /**
736  * batadv_mcast_init - initialize the multicast optimizations structures
737  * @bat_priv: the bat priv with all the soft interface information
738  */
739 void batadv_mcast_init(struct batadv_priv *bat_priv)
740 {
741 	batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
742 				     NULL, BATADV_TVLV_MCAST, 1,
743 				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
744 }
745 
746 /**
747  * batadv_mcast_free - free the multicast optimizations structures
748  * @bat_priv: the bat priv with all the soft interface information
749  */
750 void batadv_mcast_free(struct batadv_priv *bat_priv)
751 {
752 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
753 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
754 
755 	batadv_mcast_mla_tt_retract(bat_priv, NULL);
756 }
757 
758 /**
759  * batadv_mcast_purge_orig - reset originator global mcast state modifications
760  * @orig: the originator which is going to get purged
761  */
762 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
763 {
764 	struct batadv_priv *bat_priv = orig->bat_priv;
765 
766 	if (!(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST) &&
767 	    orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST)
768 		atomic_dec(&bat_priv->mcast.num_disabled);
769 
770 	batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
771 	batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
772 	batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
773 }
774