watchdog: mpc8xxx: use better error code when watchdog cannot be enabled
[linux/fpc-iii.git] / net / batman-adv / multicast.c
blob7aa480b7edd0d5fa56a7d88aa09c9db5da48068a
1 /* Copyright (C) 2014-2015 B.A.T.M.A.N. contributors:
3 * Linus Lüssing
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.
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.
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/>.
18 #include "multicast.h"
19 #include "main.h"
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>
44 #include "packet.h"
45 #include "translation-table.h"
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
52 * Collect multicast addresses of the local multicast listeners
53 * on the given soft interface, dev, in the given mcast_list.
55 * Returns -ENOMEM on memory allocation error or the number of
56 * items added to the mcast_list otherwise.
58 static int batadv_mcast_mla_softif_get(struct net_device *dev,
59 struct hlist_head *mcast_list)
61 struct netdev_hw_addr *mc_list_entry;
62 struct batadv_hw_addr *new;
63 int ret = 0;
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;
73 ether_addr_copy(new->addr, mc_list_entry->addr);
74 hlist_add_head(&new->list, mcast_list);
75 ret++;
77 netif_addr_unlock_bh(dev);
79 return ret;
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
87 * Returns true if the given address is already in the given list.
88 * Otherwise returns false.
90 static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
91 struct hlist_head *mcast_list)
93 struct batadv_hw_addr *mcast_entry;
95 hlist_for_each_entry(mcast_entry, mcast_list, list)
96 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
97 return true;
99 return false;
103 * batadv_mcast_mla_list_free - free a list of multicast addresses
104 * @mcast_list: the list to free
106 * Removes and frees all items in the given mcast_list.
108 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
110 struct batadv_hw_addr *mcast_entry;
111 struct hlist_node *tmp;
113 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
114 hlist_del(&mcast_entry->list);
115 kfree(mcast_entry);
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
124 * Retracts the announcement of any multicast listener from the
125 * translation table except the ones listed in the given mcast_list.
127 * If mcast_list is NULL then all are retracted.
129 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
130 struct hlist_head *mcast_list)
132 struct batadv_hw_addr *mcast_entry;
133 struct hlist_node *tmp;
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;
142 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
143 BATADV_NO_FLAGS,
144 "mcast TT outdated", false);
146 hlist_del(&mcast_entry->list);
147 kfree(mcast_entry);
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
156 * Adds multicast listener announcements from the given mcast_list to the
157 * translation table if they have not been added yet.
159 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
160 struct hlist_head *mcast_list)
162 struct batadv_hw_addr *mcast_entry;
163 struct hlist_node *tmp;
165 if (!mcast_list)
166 return;
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;
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;
178 hlist_del(&mcast_entry->list);
179 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
184 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
185 * @bat_priv: the bat priv with all the soft interface information
187 * Checks whether there is a bridge on top of our soft interface. Returns
188 * true if so, false otherwise.
190 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
192 struct net_device *upper = bat_priv->soft_iface;
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();
200 return upper;
204 * batadv_mcast_mla_tvlv_update - update multicast tvlv
205 * @bat_priv: the bat priv with all the soft interface information
207 * Updates the own multicast tvlv with our current multicast related settings,
208 * capabilities and inabilities.
210 * Returns true if the tvlv container is registered afterwards. Otherwise
211 * returns false.
213 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
215 struct batadv_tvlv_mcast_data mcast_data;
217 mcast_data.flags = BATADV_NO_FLAGS;
218 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
220 /* Avoid attaching MLAs, if there is a bridge on top of our soft
221 * interface, we don't support that yet (TODO)
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;
230 return false;
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;
241 return true;
245 * batadv_mcast_mla_update - update the own MLAs
246 * @bat_priv: the bat priv with all the soft interface information
248 * Updates the own multicast listener announcements in the translation
249 * table as well as the own, announced multicast tvlv container.
251 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
253 struct net_device *soft_iface = bat_priv->soft_iface;
254 struct hlist_head mcast_list = HLIST_HEAD_INIT;
255 int ret;
257 if (!batadv_mcast_mla_tvlv_update(bat_priv))
258 goto update;
260 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
261 if (ret < 0)
262 goto out;
264 update:
265 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
266 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
268 out:
269 batadv_mcast_mla_list_free(&mcast_list);
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
278 * Checks whether the given IPv4 packet has the potential to be forwarded with a
279 * mode more optimal than classic flooding.
281 * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM in case of
282 * memory allocation failure.
284 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
285 struct sk_buff *skb,
286 bool *is_unsnoopable)
288 struct iphdr *iphdr;
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;
294 iphdr = ip_hdr(skb);
296 /* TODO: Implement Multicast Router Discovery (RFC4286),
297 * then allow scope > link local, too
299 if (!ipv4_is_local_multicast(iphdr->daddr))
300 return -EINVAL;
302 /* link-local multicast listeners behind a bridge are
303 * not snoopable (see RFC4541, section 2.1.2.2)
305 *is_unsnoopable = true;
307 return 0;
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
316 * Checks whether the given IPv6 packet has the potential to be forwarded with a
317 * mode more optimal than classic flooding.
319 * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
320 * of memory.
322 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
323 struct sk_buff *skb,
324 bool *is_unsnoopable)
326 struct ipv6hdr *ip6hdr;
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;
332 ip6hdr = ipv6_hdr(skb);
334 /* TODO: Implement Multicast Router Discovery (RFC4286),
335 * then allow scope > link local, too
337 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
338 return -EINVAL;
340 /* link-local-all-nodes multicast listeners behind a bridge are
341 * not snoopable (see RFC4541, section 3, paragraph 3)
343 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
344 *is_unsnoopable = true;
346 return 0;
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
355 * Checks whether the given multicast ethernet frame has the potential to be
356 * forwarded with a mode more optimal than classic flooding.
358 * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
359 * of memory.
361 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
362 struct sk_buff *skb,
363 bool *is_unsnoopable)
365 struct ethhdr *ethhdr = eth_hdr(skb);
367 if (!atomic_read(&bat_priv->multicast_mode))
368 return -EINVAL;
370 if (atomic_read(&bat_priv->mcast.num_disabled))
371 return -EINVAL;
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;
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
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.
394 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
395 struct ethhdr *ethhdr)
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;
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
413 * Returns an orig_node matching the multicast address provided by ethhdr
414 * via a translation table lookup. This increases the returned nodes refcount.
416 static struct batadv_orig_node *
417 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
418 struct ethhdr *ethhdr)
420 return batadv_transtable_search(bat_priv, ethhdr->h_source,
421 ethhdr->h_dest, BATADV_NO_FLAGS);
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
428 * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
429 * increases its refcount.
431 static struct batadv_orig_node *
432 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
434 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
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;
443 orig_node = tmp_orig_node;
444 break;
446 rcu_read_unlock();
448 return orig_node;
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
455 * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
456 * and increases its refcount.
458 static struct batadv_orig_node *
459 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
461 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
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;
470 orig_node = tmp_orig_node;
471 break;
473 rcu_read_unlock();
475 return orig_node;
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
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.
487 static struct batadv_orig_node *
488 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
489 struct ethhdr *ethhdr)
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;
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
506 * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
507 * set and increases its refcount.
509 static struct batadv_orig_node *
510 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
512 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
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;
521 orig_node = tmp_orig_node;
522 break;
524 rcu_read_unlock();
526 return orig_node;
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
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.
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)
543 int ret, tt_count, ip_count, unsnoop_count, total_count;
544 bool is_unsnoopable = false;
545 struct ethhdr *ethhdr;
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;
553 ethhdr = eth_hdr(skb);
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);
561 total_count = tt_count + ip_count + unsnoop_count;
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);
572 if (*orig)
573 return BATADV_FORW_SINGLE;
575 /* fall through */
576 case 0:
577 return BATADV_FORW_NONE;
578 default:
579 return BATADV_FORW_ALL;
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
589 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
590 * orig, has toggled then this method updates counter and list accordingly.
592 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
593 struct batadv_orig_node *orig,
594 uint8_t mcast_flags)
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);
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);
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);
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
622 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
623 * toggled then this method updates counter and list accordingly.
625 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
626 struct batadv_orig_node *orig,
627 uint8_t mcast_flags)
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);
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);
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);
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
655 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
656 * toggled then this method updates counter and list accordingly.
658 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
659 struct batadv_orig_node *orig,
660 uint8_t mcast_flags)
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);
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);
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);
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
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)
696 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
697 uint8_t mcast_flags = BATADV_NO_FLAGS;
698 bool orig_initialized;
700 orig_initialized = orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST;
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.
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.
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;
722 orig->capa_initialized |= BATADV_ORIG_CAPA_HAS_MCAST;
724 if (orig_mcast_enabled && tvlv_value &&
725 (tvlv_value_len >= sizeof(mcast_flags)))
726 mcast_flags = *(uint8_t *)tvlv_value;
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);
732 orig->mcast_flags = mcast_flags;
736 * batadv_mcast_init - initialize the multicast optimizations structures
737 * @bat_priv: the bat priv with all the soft interface information
739 void batadv_mcast_init(struct batadv_priv *bat_priv)
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);
747 * batadv_mcast_free - free the multicast optimizations structures
748 * @bat_priv: the bat priv with all the soft interface information
750 void batadv_mcast_free(struct batadv_priv *bat_priv)
752 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
753 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
755 batadv_mcast_mla_tt_retract(bat_priv, NULL);
759 * batadv_mcast_purge_orig - reset originator global mcast state modifications
760 * @orig: the originator which is going to get purged
762 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
764 struct batadv_priv *bat_priv = orig->bat_priv;
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);
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);