batman-adv: Add missing kernel-doc to packet.h
[linux-2.6-block.git] / net / batman-adv / multicast.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
ac79cbb9 2/* Copyright (C) 2014-2017 B.A.T.M.A.N. contributors:
c5caf4ef
LL
3 *
4 * Linus Lüssing
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
c5caf4ef 19#include "multicast.h"
1e2c2a4f
SE
20#include "main.h"
21
22#include <linux/atomic.h>
9c936e3f 23#include <linux/bitops.h>
8a4023c5 24#include <linux/bug.h>
1e2c2a4f
SE
25#include <linux/byteorder/generic.h>
26#include <linux/errno.h>
27#include <linux/etherdevice.h>
b92b94ac 28#include <linux/gfp.h>
bd2a979e 29#include <linux/icmpv6.h>
687937ab 30#include <linux/if_bridge.h>
1e2c2a4f 31#include <linux/if_ether.h>
bd2a979e 32#include <linux/igmp.h>
1e2c2a4f 33#include <linux/in.h>
bd2a979e 34#include <linux/in6.h>
1e2c2a4f
SE
35#include <linux/ip.h>
36#include <linux/ipv6.h>
cbebd363 37#include <linux/jiffies.h>
72f7b2de 38#include <linux/kernel.h>
7c124391 39#include <linux/kref.h>
1e2c2a4f 40#include <linux/list.h>
2c72d655 41#include <linux/lockdep.h>
1e2c2a4f 42#include <linux/netdevice.h>
687937ab 43#include <linux/printk.h>
1e2c2a4f
SE
44#include <linux/rculist.h>
45#include <linux/rcupdate.h>
4e3e823b 46#include <linux/seq_file.h>
1e2c2a4f
SE
47#include <linux/skbuff.h>
48#include <linux/slab.h>
49#include <linux/spinlock.h>
50#include <linux/stddef.h>
51#include <linux/string.h>
52#include <linux/types.h>
cbebd363 53#include <linux/workqueue.h>
1e2c2a4f 54#include <net/addrconf.h>
687937ab
LL
55#include <net/if_inet6.h>
56#include <net/ip.h>
1e2c2a4f
SE
57#include <net/ipv6.h>
58
4e3e823b
LL
59#include "hard-interface.h"
60#include "hash.h"
ba412080 61#include "log.h"
1e2c2a4f 62#include "packet.h"
c5caf4ef 63#include "translation-table.h"
1f8dce49 64#include "tvlv.h"
c5caf4ef 65
cbebd363
LL
66static void batadv_mcast_mla_update(struct work_struct *work);
67
68/**
69 * batadv_mcast_start_timer - schedule the multicast periodic worker
70 * @bat_priv: the bat priv with all the soft interface information
71 */
72static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
73{
74 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
75 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
76}
77
687937ab
LL
78/**
79 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
80 * @soft_iface: netdev struct of the mesh interface
81 *
82 * If the given soft interface has a bridge on top then the refcount
83 * of the according net device is increased.
84 *
85 * Return: NULL if no such bridge exists. Otherwise the net device of the
86 * bridge.
87 */
88static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
89{
90 struct net_device *upper = soft_iface;
91
92 rcu_read_lock();
93 do {
94 upper = netdev_master_upper_dev_get_rcu(upper);
95 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
96
97 if (upper)
98 dev_hold(upper);
99 rcu_read_unlock();
100
101 return upper;
102}
103
c5caf4ef
LL
104/**
105 * batadv_mcast_mla_softif_get - get softif multicast listeners
106 * @dev: the device to collect multicast addresses from
107 * @mcast_list: a list to put found addresses into
108 *
687937ab
LL
109 * Collects multicast addresses of multicast listeners residing
110 * on this kernel on the given soft interface, dev, in
111 * the given mcast_list. In general, multicast listeners provided by
112 * your multicast receiving applications run directly on this node.
113 *
114 * If there is a bridge interface on top of dev, collects from that one
115 * instead. Just like with IP addresses and routes, multicast listeners
116 * will(/should) register to the bridge interface instead of an
117 * enslaved bat0.
c5caf4ef 118 *
62fe710f 119 * Return: -ENOMEM on memory allocation error or the number of
c5caf4ef
LL
120 * items added to the mcast_list otherwise.
121 */
122static int batadv_mcast_mla_softif_get(struct net_device *dev,
123 struct hlist_head *mcast_list)
124{
687937ab 125 struct net_device *bridge = batadv_mcast_get_bridge(dev);
c5caf4ef
LL
126 struct netdev_hw_addr *mc_list_entry;
127 struct batadv_hw_addr *new;
128 int ret = 0;
129
687937ab
LL
130 netif_addr_lock_bh(bridge ? bridge : dev);
131 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
c5caf4ef
LL
132 new = kmalloc(sizeof(*new), GFP_ATOMIC);
133 if (!new) {
134 ret = -ENOMEM;
135 break;
136 }
137
138 ether_addr_copy(new->addr, mc_list_entry->addr);
139 hlist_add_head(&new->list, mcast_list);
140 ret++;
141 }
687937ab
LL
142 netif_addr_unlock_bh(bridge ? bridge : dev);
143
144 if (bridge)
145 dev_put(bridge);
c5caf4ef
LL
146
147 return ret;
148}
149
150/**
151 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
152 * @mcast_addr: the multicast address to check
153 * @mcast_list: the list with multicast addresses to search in
154 *
62fe710f 155 * Return: true if the given address is already in the given list.
c5caf4ef
LL
156 * Otherwise returns false.
157 */
6b5e971a 158static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
c5caf4ef
LL
159 struct hlist_head *mcast_list)
160{
161 struct batadv_hw_addr *mcast_entry;
162
163 hlist_for_each_entry(mcast_entry, mcast_list, list)
164 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
165 return true;
166
167 return false;
168}
169
687937ab
LL
170/**
171 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
172 * @dst: destination to write to - a multicast MAC address
173 * @src: source to read from - a multicast IP address
174 *
175 * Converts a given multicast IPv4/IPv6 address from a bridge
176 * to its matching multicast MAC address and copies it into the given
177 * destination buffer.
178 *
179 * Caller needs to make sure the destination buffer can hold
180 * at least ETH_ALEN bytes.
181 */
182static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
183{
184 if (src->proto == htons(ETH_P_IP))
185 ip_eth_mc_map(src->u.ip4, dst);
186#if IS_ENABLED(CONFIG_IPV6)
187 else if (src->proto == htons(ETH_P_IPV6))
188 ipv6_eth_mc_map(&src->u.ip6, dst);
189#endif
190 else
191 eth_zero_addr(dst);
192}
193
194/**
195 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
196 * @dev: a bridge slave whose bridge to collect multicast addresses from
197 * @mcast_list: a list to put found addresses into
198 *
199 * Collects multicast addresses of multicast listeners residing
200 * on foreign, non-mesh devices which we gave access to our mesh via
201 * a bridge on top of the given soft interface, dev, in the given
202 * mcast_list.
203 *
204 * Return: -ENOMEM on memory allocation error or the number of
205 * items added to the mcast_list otherwise.
206 */
207static int batadv_mcast_mla_bridge_get(struct net_device *dev,
208 struct hlist_head *mcast_list)
209{
210 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
211 struct br_ip_list *br_ip_entry, *tmp;
212 struct batadv_hw_addr *new;
213 u8 mcast_addr[ETH_ALEN];
214 int ret;
215
216 /* we don't need to detect these devices/listeners, the IGMP/MLD
217 * snooping code of the Linux bridge already does that for us
218 */
219 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
220 if (ret < 0)
221 goto out;
222
223 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
224 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
225 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
226 continue;
227
228 new = kmalloc(sizeof(*new), GFP_ATOMIC);
229 if (!new) {
230 ret = -ENOMEM;
231 break;
232 }
233
234 ether_addr_copy(new->addr, mcast_addr);
235 hlist_add_head(&new->list, mcast_list);
236 }
237
238out:
239 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
240 list_del(&br_ip_entry->list);
241 kfree(br_ip_entry);
242 }
243
244 return ret;
245}
246
c5caf4ef
LL
247/**
248 * batadv_mcast_mla_list_free - free a list of multicast addresses
249 * @mcast_list: the list to free
250 *
251 * Removes and frees all items in the given mcast_list.
252 */
b7769763 253static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
c5caf4ef
LL
254{
255 struct batadv_hw_addr *mcast_entry;
256 struct hlist_node *tmp;
257
258 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
259 hlist_del(&mcast_entry->list);
260 kfree(mcast_entry);
261 }
262}
263
264/**
265 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
266 * @bat_priv: the bat priv with all the soft interface information
267 * @mcast_list: a list of addresses which should _not_ be removed
268 *
269 * Retracts the announcement of any multicast listener from the
270 * translation table except the ones listed in the given mcast_list.
271 *
272 * If mcast_list is NULL then all are retracted.
cbebd363
LL
273 *
274 * Do not call outside of the mcast worker! (or cancel mcast worker first)
c5caf4ef
LL
275 */
276static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
277 struct hlist_head *mcast_list)
278{
279 struct batadv_hw_addr *mcast_entry;
280 struct hlist_node *tmp;
281
cbebd363 282 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
2c72d655 283
c5caf4ef
LL
284 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
285 list) {
286 if (mcast_list &&
287 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
288 mcast_list))
289 continue;
290
291 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
292 BATADV_NO_FLAGS,
293 "mcast TT outdated", false);
294
295 hlist_del(&mcast_entry->list);
296 kfree(mcast_entry);
297 }
298}
299
300/**
301 * batadv_mcast_mla_tt_add - add multicast listener announcements
302 * @bat_priv: the bat priv with all the soft interface information
303 * @mcast_list: a list of addresses which are going to get added
304 *
305 * Adds multicast listener announcements from the given mcast_list to the
306 * translation table if they have not been added yet.
cbebd363
LL
307 *
308 * Do not call outside of the mcast worker! (or cancel mcast worker first)
c5caf4ef
LL
309 */
310static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
311 struct hlist_head *mcast_list)
312{
313 struct batadv_hw_addr *mcast_entry;
314 struct hlist_node *tmp;
315
cbebd363 316 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
2c72d655 317
c5caf4ef
LL
318 if (!mcast_list)
319 return;
320
321 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
322 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
323 &bat_priv->mcast.mla_list))
324 continue;
325
326 if (!batadv_tt_local_add(bat_priv->soft_iface,
327 mcast_entry->addr, BATADV_NO_FLAGS,
328 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
329 continue;
330
331 hlist_del(&mcast_entry->list);
332 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
333 }
334}
335
336/**
337 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
338 * @bat_priv: the bat priv with all the soft interface information
339 *
62fe710f
SE
340 * Checks whether there is a bridge on top of our soft interface.
341 *
342 * Return: true if there is a bridge, false otherwise.
c5caf4ef
LL
343 */
344static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
345{
346 struct net_device *upper = bat_priv->soft_iface;
347
348 rcu_read_lock();
349 do {
350 upper = netdev_master_upper_dev_get_rcu(upper);
351 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
352 rcu_read_unlock();
353
354 return upper;
355}
356
72f7b2de
LL
357/**
358 * batadv_mcast_querier_log - debug output regarding the querier status on link
359 * @bat_priv: the bat priv with all the soft interface information
360 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
361 * @old_state: the previous querier state on our link
362 * @new_state: the new querier state on our link
363 *
364 * Outputs debug messages to the logging facility with log level 'mcast'
365 * regarding changes to the querier status on the link which are relevant
366 * to our multicast optimizations.
367 *
368 * Usually this is about whether a querier appeared or vanished in
369 * our mesh or whether the querier is in the suboptimal position of being
370 * behind our local bridge segment: Snooping switches will directly
371 * forward listener reports to the querier, therefore batman-adv and
372 * the bridge will potentially not see these listeners - the querier is
373 * potentially shadowing listeners from us then.
374 *
375 * This is only interesting for nodes with a bridge on top of their
376 * soft interface.
377 */
378static void
379batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
380 struct batadv_mcast_querier_state *old_state,
381 struct batadv_mcast_querier_state *new_state)
382{
383 if (!old_state->exists && new_state->exists)
384 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
385 str_proto);
386 else if (old_state->exists && !new_state->exists)
387 batadv_info(bat_priv->soft_iface,
388 "%s Querier disappeared - multicast optimizations disabled\n",
389 str_proto);
390 else if (!bat_priv->mcast.bridged && !new_state->exists)
391 batadv_info(bat_priv->soft_iface,
392 "No %s Querier present - multicast optimizations disabled\n",
393 str_proto);
394
395 if (new_state->exists) {
396 if ((!old_state->shadowing && new_state->shadowing) ||
397 (!old_state->exists && new_state->shadowing))
398 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
399 "%s Querier is behind our bridged segment: Might shadow listeners\n",
400 str_proto);
401 else if (old_state->shadowing && !new_state->shadowing)
402 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
403 "%s Querier is not behind our bridged segment\n",
404 str_proto);
405 }
406}
407
408/**
409 * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
410 * @bat_priv: the bat priv with all the soft interface information
411 * @bridged: a flag about whether the soft interface is currently bridged or not
412 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
413 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
414 *
415 * If no bridges are ever used on this node, then this function does nothing.
416 *
417 * Otherwise this function outputs debug information to the 'mcast' log level
418 * which might be relevant to our multicast optimizations.
419 *
420 * More precisely, it outputs information when a bridge interface is added or
421 * removed from a soft interface. And when a bridge is present, it further
422 * outputs information about the querier state which is relevant for the
423 * multicast flags this node is going to set.
424 */
425static void
426batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
427 struct batadv_mcast_querier_state *querier_ipv4,
428 struct batadv_mcast_querier_state *querier_ipv6)
429{
430 if (!bat_priv->mcast.bridged && bridged)
431 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
432 "Bridge added: Setting Unsnoopables(U)-flag\n");
433 else if (bat_priv->mcast.bridged && !bridged)
434 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
435 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
436
437 if (bridged) {
438 batadv_mcast_querier_log(bat_priv, "IGMP",
439 &bat_priv->mcast.querier_ipv4,
440 querier_ipv4);
441 batadv_mcast_querier_log(bat_priv, "MLD",
442 &bat_priv->mcast.querier_ipv6,
443 querier_ipv6);
444 }
445}
446
447/**
448 * batadv_mcast_flags_logs - output debug information about mcast flag changes
449 * @bat_priv: the bat priv with all the soft interface information
450 * @flags: flags indicating the new multicast state
451 *
452 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
453 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
454 */
455static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
456{
457 u8 old_flags = bat_priv->mcast.flags;
458 char str_old_flags[] = "[...]";
459
460 sprintf(str_old_flags, "[%c%c%c]",
461 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
462 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
463 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
464
465 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
466 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
467 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
468 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
469 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
470 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
471}
472
60432d75
LL
473/**
474 * batadv_mcast_mla_tvlv_update - update multicast tvlv
475 * @bat_priv: the bat priv with all the soft interface information
476 *
477 * Updates the own multicast tvlv with our current multicast related settings,
478 * capabilities and inabilities.
479 *
687937ab
LL
480 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
481 * otherwise.
60432d75
LL
482 */
483static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
484{
485 struct batadv_tvlv_mcast_data mcast_data;
687937ab
LL
486 struct batadv_mcast_querier_state querier4 = {false, false};
487 struct batadv_mcast_querier_state querier6 = {false, false};
488 struct net_device *dev = bat_priv->soft_iface;
72f7b2de 489 bool bridged;
60432d75
LL
490
491 mcast_data.flags = BATADV_NO_FLAGS;
492 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
493
72f7b2de
LL
494 bridged = batadv_mcast_has_bridge(bat_priv);
495 if (!bridged)
687937ab
LL
496 goto update;
497
c1bacea0
SE
498 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
499 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
687937ab
LL
500
501 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
502 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
503
504 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
505 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
506
507 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
508
509 /* 1) If no querier exists at all, then multicast listeners on
510 * our local TT clients behind the bridge will keep silent.
511 * 2) If the selected querier is on one of our local TT clients,
512 * behind the bridge, then this querier might shadow multicast
513 * listeners on our local TT clients, behind this bridge.
514 *
515 * In both cases, we will signalize other batman nodes that
516 * we need all multicast traffic of the according protocol.
60432d75 517 */
687937ab
LL
518 if (!querier4.exists || querier4.shadowing)
519 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
60432d75 520
687937ab
LL
521 if (!querier6.exists || querier6.shadowing)
522 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
60432d75 523
687937ab 524update:
72f7b2de
LL
525 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
526
527 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
528 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
529
530 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
531 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
532
533 bat_priv->mcast.bridged = bridged;
534
60432d75
LL
535 if (!bat_priv->mcast.enabled ||
536 mcast_data.flags != bat_priv->mcast.flags) {
72f7b2de 537 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
bd2a979e 538 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
60432d75
LL
539 &mcast_data, sizeof(mcast_data));
540 bat_priv->mcast.flags = mcast_data.flags;
541 bat_priv->mcast.enabled = true;
542 }
543
687937ab 544 return !(mcast_data.flags &
4d7de48c 545 (BATADV_MCAST_WANT_ALL_IPV4 | BATADV_MCAST_WANT_ALL_IPV6));
60432d75
LL
546}
547
c5caf4ef 548/**
cbebd363 549 * __batadv_mcast_mla_update - update the own MLAs
c5caf4ef
LL
550 * @bat_priv: the bat priv with all the soft interface information
551 *
60432d75
LL
552 * Updates the own multicast listener announcements in the translation
553 * table as well as the own, announced multicast tvlv container.
cbebd363
LL
554 *
555 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
556 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
557 * ensured by the non-parallel execution of the worker this function
558 * belongs to.
c5caf4ef 559 */
cbebd363 560static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
c5caf4ef
LL
561{
562 struct net_device *soft_iface = bat_priv->soft_iface;
563 struct hlist_head mcast_list = HLIST_HEAD_INIT;
564 int ret;
565
60432d75 566 if (!batadv_mcast_mla_tvlv_update(bat_priv))
c5caf4ef
LL
567 goto update;
568
569 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
570 if (ret < 0)
571 goto out;
572
687937ab
LL
573 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
574 if (ret < 0)
575 goto out;
576
c5caf4ef
LL
577update:
578 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
579 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
580
581out:
b7769763 582 batadv_mcast_mla_list_free(&mcast_list);
c5caf4ef
LL
583}
584
cbebd363
LL
585/**
586 * batadv_mcast_mla_update - update the own MLAs
587 * @work: kernel work struct
588 *
589 * Updates the own multicast listener announcements in the translation
590 * table as well as the own, announced multicast tvlv container.
591 *
592 * In the end, reschedules the work timer.
593 */
594static void batadv_mcast_mla_update(struct work_struct *work)
595{
596 struct delayed_work *delayed_work;
597 struct batadv_priv_mcast *priv_mcast;
598 struct batadv_priv *bat_priv;
599
600 delayed_work = to_delayed_work(work);
601 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
602 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
603
604 __batadv_mcast_mla_update(bat_priv);
605 batadv_mcast_start_timer(bat_priv);
606}
607
bd2a979e
LL
608/**
609 * batadv_mcast_is_report_ipv4 - check for IGMP reports
610 * @skb: the ethernet frame destined for the mesh
611 *
612 * This call might reallocate skb data.
613 *
614 * Checks whether the given frame is a valid IGMP report.
615 *
616 * Return: If so then true, otherwise false.
617 */
618static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
619{
620 if (ip_mc_check_igmp(skb, NULL) < 0)
621 return false;
622
623 switch (igmp_hdr(skb)->type) {
624 case IGMP_HOST_MEMBERSHIP_REPORT:
625 case IGMPV2_HOST_MEMBERSHIP_REPORT:
626 case IGMPV3_HOST_MEMBERSHIP_REPORT:
627 return true;
628 }
629
630 return false;
631}
632
ab49886e
LL
633/**
634 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
635 * @bat_priv: the bat priv with all the soft interface information
636 * @skb: the IPv4 packet to check
637 * @is_unsnoopable: stores whether the destination is snoopable
638 *
639 * Checks whether the given IPv4 packet has the potential to be forwarded with a
640 * mode more optimal than classic flooding.
641 *
62fe710f
SE
642 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
643 * allocation failure.
ab49886e
LL
644 */
645static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
646 struct sk_buff *skb,
647 bool *is_unsnoopable)
648{
649 struct iphdr *iphdr;
650
651 /* We might fail due to out-of-memory -> drop it */
652 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
653 return -ENOMEM;
654
bd2a979e
LL
655 if (batadv_mcast_is_report_ipv4(skb))
656 return -EINVAL;
657
ab49886e
LL
658 iphdr = ip_hdr(skb);
659
660 /* TODO: Implement Multicast Router Discovery (RFC4286),
661 * then allow scope > link local, too
662 */
663 if (!ipv4_is_local_multicast(iphdr->daddr))
664 return -EINVAL;
665
666 /* link-local multicast listeners behind a bridge are
667 * not snoopable (see RFC4541, section 2.1.2.2)
668 */
669 *is_unsnoopable = true;
670
671 return 0;
672}
673
bd2a979e
LL
674/**
675 * batadv_mcast_is_report_ipv6 - check for MLD reports
676 * @skb: the ethernet frame destined for the mesh
677 *
678 * This call might reallocate skb data.
679 *
680 * Checks whether the given frame is a valid MLD report.
681 *
682 * Return: If so then true, otherwise false.
683 */
684static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
685{
686 if (ipv6_mc_check_mld(skb, NULL) < 0)
687 return false;
688
689 switch (icmp6_hdr(skb)->icmp6_type) {
690 case ICMPV6_MGM_REPORT:
691 case ICMPV6_MLD2_REPORT:
692 return true;
693 }
694
695 return false;
696}
697
1d8ab8d3
LL
698/**
699 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
700 * @bat_priv: the bat priv with all the soft interface information
701 * @skb: the IPv6 packet to check
ab49886e 702 * @is_unsnoopable: stores whether the destination is snoopable
1d8ab8d3
LL
703 *
704 * Checks whether the given IPv6 packet has the potential to be forwarded with a
705 * mode more optimal than classic flooding.
706 *
62fe710f 707 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1d8ab8d3
LL
708 */
709static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
ab49886e
LL
710 struct sk_buff *skb,
711 bool *is_unsnoopable)
1d8ab8d3
LL
712{
713 struct ipv6hdr *ip6hdr;
714
715 /* We might fail due to out-of-memory -> drop it */
716 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
717 return -ENOMEM;
718
bd2a979e
LL
719 if (batadv_mcast_is_report_ipv6(skb))
720 return -EINVAL;
721
1d8ab8d3
LL
722 ip6hdr = ipv6_hdr(skb);
723
724 /* TODO: Implement Multicast Router Discovery (RFC4286),
725 * then allow scope > link local, too
726 */
727 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
728 return -EINVAL;
729
730 /* link-local-all-nodes multicast listeners behind a bridge are
731 * not snoopable (see RFC4541, section 3, paragraph 3)
732 */
733 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
ab49886e 734 *is_unsnoopable = true;
1d8ab8d3
LL
735
736 return 0;
737}
738
739/**
740 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
741 * @bat_priv: the bat priv with all the soft interface information
742 * @skb: the multicast frame to check
ab49886e 743 * @is_unsnoopable: stores whether the destination is snoopable
1d8ab8d3
LL
744 *
745 * Checks whether the given multicast ethernet frame has the potential to be
746 * forwarded with a mode more optimal than classic flooding.
747 *
62fe710f 748 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1d8ab8d3
LL
749 */
750static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
ab49886e
LL
751 struct sk_buff *skb,
752 bool *is_unsnoopable)
1d8ab8d3
LL
753{
754 struct ethhdr *ethhdr = eth_hdr(skb);
755
756 if (!atomic_read(&bat_priv->multicast_mode))
757 return -EINVAL;
758
759 if (atomic_read(&bat_priv->mcast.num_disabled))
760 return -EINVAL;
761
762 switch (ntohs(ethhdr->h_proto)) {
ab49886e
LL
763 case ETH_P_IP:
764 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
765 is_unsnoopable);
1d8ab8d3 766 case ETH_P_IPV6:
c1bacea0
SE
767 if (!IS_ENABLED(CONFIG_IPV6))
768 return -EINVAL;
769
ab49886e
LL
770 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
771 is_unsnoopable);
1d8ab8d3
LL
772 default:
773 return -EINVAL;
774 }
775}
776
4c8755d6 777/**
6d030de8
AQ
778 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
779 * interest
4c8755d6
LL
780 * @bat_priv: the bat priv with all the soft interface information
781 * @ethhdr: ethernet header of a packet
782 *
62fe710f 783 * Return: the number of nodes which want all IPv4 multicast traffic if the
4c8755d6
LL
784 * given ethhdr is from an IPv4 packet or the number of nodes which want all
785 * IPv6 traffic if it matches an IPv6 packet.
786 */
787static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
788 struct ethhdr *ethhdr)
789{
790 switch (ntohs(ethhdr->h_proto)) {
791 case ETH_P_IP:
792 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
793 case ETH_P_IPV6:
794 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
795 default:
796 /* we shouldn't be here... */
797 return 0;
798 }
799}
800
1d8ab8d3
LL
801/**
802 * batadv_mcast_forw_tt_node_get - get a multicast tt node
803 * @bat_priv: the bat priv with all the soft interface information
804 * @ethhdr: the ether header containing the multicast destination
805 *
62fe710f 806 * Return: an orig_node matching the multicast address provided by ethhdr
1d8ab8d3
LL
807 * via a translation table lookup. This increases the returned nodes refcount.
808 */
809static struct batadv_orig_node *
810batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
811 struct ethhdr *ethhdr)
812{
813 return batadv_transtable_search(bat_priv, ethhdr->h_source,
814 ethhdr->h_dest, BATADV_NO_FLAGS);
815}
816
4c8755d6 817/**
6d030de8 818 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
4c8755d6
LL
819 * @bat_priv: the bat priv with all the soft interface information
820 *
62fe710f 821 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
4c8755d6
LL
822 * increases its refcount.
823 */
824static struct batadv_orig_node *
825batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
826{
827 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
828
829 rcu_read_lock();
830 hlist_for_each_entry_rcu(tmp_orig_node,
831 &bat_priv->mcast.want_all_ipv4_list,
832 mcast_want_all_ipv4_node) {
7c124391 833 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
4c8755d6
LL
834 continue;
835
836 orig_node = tmp_orig_node;
837 break;
838 }
839 rcu_read_unlock();
840
841 return orig_node;
842}
843
844/**
6d030de8 845 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
4c8755d6
LL
846 * @bat_priv: the bat priv with all the soft interface information
847 *
62fe710f 848 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
4c8755d6
LL
849 * and increases its refcount.
850 */
851static struct batadv_orig_node *
852batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
853{
854 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
855
856 rcu_read_lock();
857 hlist_for_each_entry_rcu(tmp_orig_node,
858 &bat_priv->mcast.want_all_ipv6_list,
859 mcast_want_all_ipv6_node) {
7c124391 860 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
4c8755d6
LL
861 continue;
862
863 orig_node = tmp_orig_node;
864 break;
865 }
866 rcu_read_unlock();
867
868 return orig_node;
869}
870
871/**
6d030de8 872 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
4c8755d6
LL
873 * @bat_priv: the bat priv with all the soft interface information
874 * @ethhdr: an ethernet header to determine the protocol family from
875 *
62fe710f 876 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
4c8755d6
LL
877 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
878 * increases its refcount.
879 */
880static struct batadv_orig_node *
881batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
882 struct ethhdr *ethhdr)
883{
884 switch (ntohs(ethhdr->h_proto)) {
885 case ETH_P_IP:
886 return batadv_mcast_forw_ipv4_node_get(bat_priv);
887 case ETH_P_IPV6:
888 return batadv_mcast_forw_ipv6_node_get(bat_priv);
889 default:
890 /* we shouldn't be here... */
891 return NULL;
892 }
893}
894
ab49886e 895/**
6d030de8 896 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
ab49886e
LL
897 * @bat_priv: the bat priv with all the soft interface information
898 *
62fe710f 899 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
ab49886e
LL
900 * set and increases its refcount.
901 */
902static struct batadv_orig_node *
903batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
904{
905 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
906
907 rcu_read_lock();
908 hlist_for_each_entry_rcu(tmp_orig_node,
909 &bat_priv->mcast.want_all_unsnoopables_list,
910 mcast_want_all_unsnoopables_node) {
7c124391 911 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
ab49886e
LL
912 continue;
913
914 orig_node = tmp_orig_node;
915 break;
916 }
917 rcu_read_unlock();
918
919 return orig_node;
920}
921
1d8ab8d3
LL
922/**
923 * batadv_mcast_forw_mode - check on how to forward a multicast packet
924 * @bat_priv: the bat priv with all the soft interface information
925 * @skb: The multicast packet to check
926 * @orig: an originator to be set to forward the skb to
927 *
62fe710f 928 * Return: the forwarding mode as enum batadv_forw_mode and in case of
1d8ab8d3
LL
929 * BATADV_FORW_SINGLE set the orig to the single originator the skb
930 * should be forwarded to.
931 */
932enum batadv_forw_mode
933batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
934 struct batadv_orig_node **orig)
935{
4c8755d6 936 int ret, tt_count, ip_count, unsnoop_count, total_count;
ab49886e 937 bool is_unsnoopable = false;
1d8ab8d3 938 struct ethhdr *ethhdr;
1d8ab8d3 939
ab49886e 940 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
1d8ab8d3
LL
941 if (ret == -ENOMEM)
942 return BATADV_FORW_NONE;
943 else if (ret < 0)
944 return BATADV_FORW_ALL;
945
946 ethhdr = eth_hdr(skb);
947
948 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
949 BATADV_NO_FLAGS);
4c8755d6 950 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
ab49886e
LL
951 unsnoop_count = !is_unsnoopable ? 0 :
952 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
953
4c8755d6 954 total_count = tt_count + ip_count + unsnoop_count;
1d8ab8d3 955
ab49886e 956 switch (total_count) {
1d8ab8d3 957 case 1:
ab49886e
LL
958 if (tt_count)
959 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
4c8755d6
LL
960 else if (ip_count)
961 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
ab49886e
LL
962 else if (unsnoop_count)
963 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
964
1d8ab8d3
LL
965 if (*orig)
966 return BATADV_FORW_SINGLE;
967
968 /* fall through */
969 case 0:
970 return BATADV_FORW_NONE;
971 default:
972 return BATADV_FORW_ALL;
973 }
974}
975
ab49886e
LL
976/**
977 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
978 * @bat_priv: the bat priv with all the soft interface information
979 * @orig: the orig_node which multicast state might have changed of
980 * @mcast_flags: flags indicating the new multicast state
981 *
982 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
983 * orig, has toggled then this method updates counter and list accordingly.
8a4023c5
LL
984 *
985 * Caller needs to hold orig->mcast_handler_lock.
ab49886e
LL
986 */
987static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
988 struct batadv_orig_node *orig,
6b5e971a 989 u8 mcast_flags)
ab49886e 990{
8a4023c5
LL
991 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
992 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
993
5274cd68
SE
994 lockdep_assert_held(&orig->mcast_handler_lock);
995
ab49886e
LL
996 /* switched from flag unset to set */
997 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
998 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
999 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1000
1001 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
8a4023c5
LL
1002 /* flag checks above + mcast_handler_lock prevents this */
1003 WARN_ON(!hlist_unhashed(node));
1004
1005 hlist_add_head_rcu(node, head);
ab49886e
LL
1006 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1007 /* switched from flag set to unset */
1008 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1009 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1010 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1011
1012 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
8a4023c5
LL
1013 /* flag checks above + mcast_handler_lock prevents this */
1014 WARN_ON(hlist_unhashed(node));
1015
1016 hlist_del_init_rcu(node);
ab49886e
LL
1017 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1018 }
1019}
1020
4c8755d6
LL
1021/**
1022 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
1023 * @bat_priv: the bat priv with all the soft interface information
1024 * @orig: the orig_node which multicast state might have changed of
1025 * @mcast_flags: flags indicating the new multicast state
1026 *
1027 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1028 * toggled then this method updates counter and list accordingly.
8a4023c5
LL
1029 *
1030 * Caller needs to hold orig->mcast_handler_lock.
4c8755d6
LL
1031 */
1032static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1033 struct batadv_orig_node *orig,
6b5e971a 1034 u8 mcast_flags)
4c8755d6 1035{
8a4023c5
LL
1036 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1037 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1038
5274cd68
SE
1039 lockdep_assert_held(&orig->mcast_handler_lock);
1040
4c8755d6
LL
1041 /* switched from flag unset to set */
1042 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1043 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1044 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1045
1046 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
8a4023c5
LL
1047 /* flag checks above + mcast_handler_lock prevents this */
1048 WARN_ON(!hlist_unhashed(node));
1049
1050 hlist_add_head_rcu(node, head);
4c8755d6
LL
1051 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1052 /* switched from flag set to unset */
1053 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1054 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1055 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1056
1057 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
8a4023c5
LL
1058 /* flag checks above + mcast_handler_lock prevents this */
1059 WARN_ON(hlist_unhashed(node));
1060
1061 hlist_del_init_rcu(node);
4c8755d6
LL
1062 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1063 }
1064}
1065
1066/**
1067 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1068 * @bat_priv: the bat priv with all the soft interface information
1069 * @orig: the orig_node which multicast state might have changed of
1070 * @mcast_flags: flags indicating the new multicast state
1071 *
1072 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1073 * toggled then this method updates counter and list accordingly.
8a4023c5
LL
1074 *
1075 * Caller needs to hold orig->mcast_handler_lock.
4c8755d6
LL
1076 */
1077static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1078 struct batadv_orig_node *orig,
6b5e971a 1079 u8 mcast_flags)
4c8755d6 1080{
8a4023c5
LL
1081 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1082 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1083
5274cd68
SE
1084 lockdep_assert_held(&orig->mcast_handler_lock);
1085
4c8755d6
LL
1086 /* switched from flag unset to set */
1087 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1088 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1089 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1090
1091 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
8a4023c5
LL
1092 /* flag checks above + mcast_handler_lock prevents this */
1093 WARN_ON(!hlist_unhashed(node));
1094
1095 hlist_add_head_rcu(node, head);
4c8755d6
LL
1096 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1097 /* switched from flag set to unset */
1098 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1099 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1100 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1101
1102 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
8a4023c5
LL
1103 /* flag checks above + mcast_handler_lock prevents this */
1104 WARN_ON(hlist_unhashed(node));
1105
1106 hlist_del_init_rcu(node);
4c8755d6
LL
1107 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1108 }
1109}
1110
60432d75 1111/**
bd2a979e 1112 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
60432d75
LL
1113 * @bat_priv: the bat priv with all the soft interface information
1114 * @orig: the orig_node of the ogm
1115 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1116 * @tvlv_value: tvlv buffer containing the multicast data
1117 * @tvlv_value_len: tvlv buffer length
1118 */
bd2a979e
LL
1119static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1120 struct batadv_orig_node *orig,
1121 u8 flags,
1122 void *tvlv_value,
1123 u16 tvlv_value_len)
60432d75
LL
1124{
1125 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
6b5e971a 1126 u8 mcast_flags = BATADV_NO_FLAGS;
60432d75
LL
1127 bool orig_initialized;
1128
8a4023c5 1129 if (orig_mcast_enabled && tvlv_value &&
825ffe1f 1130 tvlv_value_len >= sizeof(mcast_flags))
6b5e971a 1131 mcast_flags = *(u8 *)tvlv_value;
8a4023c5
LL
1132
1133 spin_lock_bh(&orig->mcast_handler_lock);
9c936e3f
LL
1134 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1135 &orig->capa_initialized);
60432d75
LL
1136
1137 /* If mcast support is turned on decrease the disabled mcast node
1138 * counter only if we had increased it for this node before. If this
1139 * is a completely new orig_node no need to decrease the counter.
1140 */
1141 if (orig_mcast_enabled &&
9c936e3f 1142 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
60432d75
LL
1143 if (orig_initialized)
1144 atomic_dec(&bat_priv->mcast.num_disabled);
9c936e3f 1145 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
e8829f00
LL
1146 /* If mcast support is being switched off or if this is an initial
1147 * OGM without mcast support then increase the disabled mcast
1148 * node counter.
60432d75
LL
1149 */
1150 } else if (!orig_mcast_enabled &&
9c936e3f 1151 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
e8829f00 1152 !orig_initialized)) {
60432d75 1153 atomic_inc(&bat_priv->mcast.num_disabled);
9c936e3f 1154 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
60432d75
LL
1155 }
1156
9c936e3f 1157 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
60432d75 1158
ab49886e 1159 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
4c8755d6
LL
1160 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1161 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
ab49886e 1162
60432d75 1163 orig->mcast_flags = mcast_flags;
8a4023c5 1164 spin_unlock_bh(&orig->mcast_handler_lock);
60432d75
LL
1165}
1166
1167/**
1168 * batadv_mcast_init - initialize the multicast optimizations structures
1169 * @bat_priv: the bat priv with all the soft interface information
1170 */
1171void batadv_mcast_init(struct batadv_priv *bat_priv)
1172{
bd2a979e
LL
1173 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1174 NULL, BATADV_TVLV_MCAST, 2,
60432d75 1175 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
cbebd363
LL
1176
1177 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1178 batadv_mcast_start_timer(bat_priv);
60432d75
LL
1179}
1180
dc1cbd14 1181#ifdef CONFIG_BATMAN_ADV_DEBUGFS
4e3e823b
LL
1182/**
1183 * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1184 * @bat_priv: the bat priv with all the soft interface information
1185 * @seq: debugfs table seq_file struct
1186 *
1187 * Prints our own multicast flags including a more specific reason why
1188 * they are set, that is prints the bridge and querier state too, to
1189 * the debugfs table specified via @seq.
1190 */
1191static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1192 struct seq_file *seq)
1193{
1194 u8 flags = bat_priv->mcast.flags;
1195 char querier4, querier6, shadowing4, shadowing6;
1196 bool bridged = bat_priv->mcast.bridged;
1197
1198 if (bridged) {
1199 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1200 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1201 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1202 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1203 } else {
1204 querier4 = '?';
1205 querier6 = '?';
1206 shadowing4 = '?';
1207 shadowing6 = '?';
1208 }
1209
1210 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1211 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1212 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1213 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1214 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1215 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1216 querier4, querier6);
1217 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1218 shadowing4, shadowing6);
1219 seq_puts(seq, "-------------------------------------------\n");
1220 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1221}
1222
1223/**
1224 * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1225 * @seq: seq file to print on
1226 * @offset: not used
1227 *
1228 * This prints a table of (primary) originators and their according
1229 * multicast flags, including (in the header) our own.
1230 *
1231 * Return: always 0
1232 */
1233int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1234{
1235 struct net_device *net_dev = (struct net_device *)seq->private;
1236 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1237 struct batadv_hard_iface *primary_if;
1238 struct batadv_hashtable *hash = bat_priv->orig_hash;
1239 struct batadv_orig_node *orig_node;
1240 struct hlist_head *head;
1241 u8 flags;
1242 u32 i;
1243
1244 primary_if = batadv_seq_print_text_primary_if_get(seq);
1245 if (!primary_if)
1246 return 0;
1247
1248 batadv_mcast_flags_print_header(bat_priv, seq);
1249
1250 for (i = 0; i < hash->size; i++) {
1251 head = &hash->table[i];
1252
1253 rcu_read_lock();
1254 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1255 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1256 &orig_node->capa_initialized))
1257 continue;
1258
1259 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1260 &orig_node->capabilities)) {
1261 seq_printf(seq, "%pM -\n", orig_node->orig);
1262 continue;
1263 }
1264
1265 flags = orig_node->mcast_flags;
1266
1267 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1268 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1269 ? 'U' : '.',
1270 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1271 ? '4' : '.',
1272 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1273 ? '6' : '.');
1274 }
1275 rcu_read_unlock();
1276 }
1277
1278 batadv_hardif_put(primary_if);
1279
1280 return 0;
1281}
dc1cbd14 1282#endif
4e3e823b 1283
c5caf4ef
LL
1284/**
1285 * batadv_mcast_free - free the multicast optimizations structures
1286 * @bat_priv: the bat priv with all the soft interface information
1287 */
1288void batadv_mcast_free(struct batadv_priv *bat_priv)
1289{
cbebd363
LL
1290 cancel_delayed_work_sync(&bat_priv->mcast.work);
1291
bd2a979e
LL
1292 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1293 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
60432d75 1294
cbebd363 1295 /* safely calling outside of worker, as worker was canceled above */
c5caf4ef
LL
1296 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1297}
60432d75
LL
1298
1299/**
1300 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1301 * @orig: the originator which is going to get purged
1302 */
1303void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1304{
1305 struct batadv_priv *bat_priv = orig->bat_priv;
1306
8a4023c5
LL
1307 spin_lock_bh(&orig->mcast_handler_lock);
1308
9c936e3f
LL
1309 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1310 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
60432d75 1311 atomic_dec(&bat_priv->mcast.num_disabled);
ab49886e
LL
1312
1313 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
4c8755d6
LL
1314 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1315 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
8a4023c5
LL
1316
1317 spin_unlock_bh(&orig->mcast_handler_lock);
60432d75 1318}