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