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