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