blob: f5bf931252c4b434e528fc88cfceac9e52913aa3 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
David Brazdil0f672f62019-12-10 10:32:29 +00002/* Copyright (C) 2014-2019 B.A.T.M.A.N. contributors:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003 *
4 * Linus Lüssing
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6
7#include "multicast.h"
8#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/bitops.h>
12#include <linux/bug.h>
13#include <linux/byteorder/generic.h>
14#include <linux/errno.h>
15#include <linux/etherdevice.h>
16#include <linux/gfp.h>
17#include <linux/icmpv6.h>
18#include <linux/if_bridge.h>
19#include <linux/if_ether.h>
20#include <linux/igmp.h>
21#include <linux/in.h>
22#include <linux/in6.h>
David Brazdil0f672f62019-12-10 10:32:29 +000023#include <linux/inetdevice.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include <linux/ip.h>
25#include <linux/ipv6.h>
26#include <linux/jiffies.h>
27#include <linux/kernel.h>
28#include <linux/kref.h>
29#include <linux/list.h>
30#include <linux/lockdep.h>
31#include <linux/netdevice.h>
32#include <linux/netlink.h>
33#include <linux/printk.h>
34#include <linux/rculist.h>
35#include <linux/rcupdate.h>
36#include <linux/seq_file.h>
37#include <linux/skbuff.h>
38#include <linux/slab.h>
39#include <linux/spinlock.h>
40#include <linux/stddef.h>
41#include <linux/string.h>
42#include <linux/types.h>
43#include <linux/workqueue.h>
44#include <net/addrconf.h>
45#include <net/genetlink.h>
46#include <net/if_inet6.h>
47#include <net/ip.h>
48#include <net/ipv6.h>
49#include <net/netlink.h>
50#include <net/sock.h>
51#include <uapi/linux/batadv_packet.h>
52#include <uapi/linux/batman_adv.h>
53
Olivier Deprez0e641232021-09-23 10:07:05 +020054#include "bridge_loop_avoidance.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055#include "hard-interface.h"
56#include "hash.h"
57#include "log.h"
58#include "netlink.h"
David Brazdil0f672f62019-12-10 10:32:29 +000059#include "send.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060#include "soft-interface.h"
61#include "translation-table.h"
62#include "tvlv.h"
63
64static void batadv_mcast_mla_update(struct work_struct *work);
65
66/**
67 * batadv_mcast_start_timer() - schedule the multicast periodic worker
68 * @bat_priv: the bat priv with all the soft interface information
69 */
70static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
71{
72 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
73 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
74}
75
76/**
77 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
78 * @soft_iface: netdev struct of the mesh interface
79 *
80 * If the given soft interface has a bridge on top then the refcount
81 * of the according net device is increased.
82 *
83 * Return: NULL if no such bridge exists. Otherwise the net device of the
84 * bridge.
85 */
86static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
87{
88 struct net_device *upper = soft_iface;
89
90 rcu_read_lock();
91 do {
92 upper = netdev_master_upper_dev_get_rcu(upper);
93 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
94
95 if (upper)
96 dev_hold(upper);
97 rcu_read_unlock();
98
99 return upper;
100}
101
102/**
David Brazdil0f672f62019-12-10 10:32:29 +0000103 * batadv_mcast_mla_rtr_flags_softif_get_ipv4() - get mcast router flags from
104 * node for IPv4
105 * @dev: the interface to check
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106 *
David Brazdil0f672f62019-12-10 10:32:29 +0000107 * Checks the presence of an IPv4 multicast router on this node.
108 *
109 * Caller needs to hold rcu read lock.
110 *
111 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112 */
David Brazdil0f672f62019-12-10 10:32:29 +0000113static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv4(struct net_device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114{
David Brazdil0f672f62019-12-10 10:32:29 +0000115 struct in_device *in_dev = __in_dev_get_rcu(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116
David Brazdil0f672f62019-12-10 10:32:29 +0000117 if (in_dev && IN_DEV_MFORWARD(in_dev))
118 return BATADV_NO_FLAGS;
119 else
120 return BATADV_MCAST_WANT_NO_RTR4;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121}
122
123/**
David Brazdil0f672f62019-12-10 10:32:29 +0000124 * batadv_mcast_mla_rtr_flags_softif_get_ipv6() - get mcast router flags from
125 * node for IPv6
126 * @dev: the interface to check
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127 *
David Brazdil0f672f62019-12-10 10:32:29 +0000128 * Checks the presence of an IPv6 multicast router on this node.
129 *
130 * Caller needs to hold rcu read lock.
131 *
132 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133 */
David Brazdil0f672f62019-12-10 10:32:29 +0000134#if IS_ENABLED(CONFIG_IPV6_MROUTE)
135static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136{
David Brazdil0f672f62019-12-10 10:32:29 +0000137 struct inet6_dev *in6_dev = __in6_dev_get(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138
David Brazdil0f672f62019-12-10 10:32:29 +0000139 if (in6_dev && in6_dev->cnf.mc_forwarding)
140 return BATADV_NO_FLAGS;
141 else
142 return BATADV_MCAST_WANT_NO_RTR6;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143}
David Brazdil0f672f62019-12-10 10:32:29 +0000144#else
145static inline u8
146batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
147{
148 return BATADV_MCAST_WANT_NO_RTR6;
149}
150#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151
152/**
David Brazdil0f672f62019-12-10 10:32:29 +0000153 * batadv_mcast_mla_rtr_flags_softif_get() - get mcast router flags from node
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000154 * @bat_priv: the bat priv with all the soft interface information
David Brazdil0f672f62019-12-10 10:32:29 +0000155 * @bridge: bridge interface on top of the soft_iface if present,
156 * otherwise pass NULL
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000157 *
David Brazdil0f672f62019-12-10 10:32:29 +0000158 * Checks the presence of IPv4 and IPv6 multicast routers on this
159 * node.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160 *
David Brazdil0f672f62019-12-10 10:32:29 +0000161 * Return:
162 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
163 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
164 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
165 * The former two OR'd: no multicast router is present
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166 */
David Brazdil0f672f62019-12-10 10:32:29 +0000167static u8 batadv_mcast_mla_rtr_flags_softif_get(struct batadv_priv *bat_priv,
168 struct net_device *bridge)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169{
David Brazdil0f672f62019-12-10 10:32:29 +0000170 struct net_device *dev = bridge ? bridge : bat_priv->soft_iface;
171 u8 flags = BATADV_NO_FLAGS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172
David Brazdil0f672f62019-12-10 10:32:29 +0000173 rcu_read_lock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174
David Brazdil0f672f62019-12-10 10:32:29 +0000175 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv4(dev);
176 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv6(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000177
David Brazdil0f672f62019-12-10 10:32:29 +0000178 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179
David Brazdil0f672f62019-12-10 10:32:29 +0000180 return flags;
181}
182
183/**
184 * batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge
185 * @bat_priv: the bat priv with all the soft interface information
186 * @bridge: bridge interface on top of the soft_iface if present,
187 * otherwise pass NULL
188 *
189 * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
190 *
191 * Return:
192 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
193 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
194 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
195 * The former two OR'd: no multicast router is present
196 */
197#if IS_ENABLED(CONFIG_IPV6)
198static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
199 struct net_device *bridge)
200{
201 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
202 struct net_device *dev = bat_priv->soft_iface;
203 struct br_ip_list *br_ip_entry, *tmp;
204 u8 flags = BATADV_MCAST_WANT_NO_RTR6;
205 int ret;
206
207 if (!bridge)
208 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
209
210 /* TODO: ask the bridge if a multicast router is present (the bridge
211 * is capable of performing proper RFC4286 multicast multicast router
212 * discovery) instead of searching for a ff02::2 listener here
213 */
214 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
215 if (ret < 0)
216 return BATADV_NO_FLAGS;
217
218 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
219 /* the bridge snooping does not maintain IPv4 link-local
220 * addresses - therefore we won't find any IPv4 multicast router
221 * address here, only IPv6 ones
222 */
223 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
224 ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.u.ip6))
225 flags &= ~BATADV_MCAST_WANT_NO_RTR6;
226
227 list_del(&br_ip_entry->list);
228 kfree(br_ip_entry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000230
David Brazdil0f672f62019-12-10 10:32:29 +0000231 return flags;
232}
233#else
234static inline u8
235batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
236 struct net_device *bridge)
237{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238 if (bridge)
David Brazdil0f672f62019-12-10 10:32:29 +0000239 return BATADV_NO_FLAGS;
240 else
241 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
242}
243#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244
David Brazdil0f672f62019-12-10 10:32:29 +0000245/**
246 * batadv_mcast_mla_rtr_flags_get() - get multicast router flags
247 * @bat_priv: the bat priv with all the soft interface information
248 * @bridge: bridge interface on top of the soft_iface if present,
249 * otherwise pass NULL
250 *
251 * Checks the presence of IPv4 and IPv6 multicast routers on this
252 * node or behind its bridge.
253 *
254 * Return:
255 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
256 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
257 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
258 * The former two OR'd: no multicast router is present
259 */
260static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
261 struct net_device *bridge)
262{
263 u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
264
265 flags &= batadv_mcast_mla_rtr_flags_softif_get(bat_priv, bridge);
266 flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
267
268 return flags;
269}
270
271/**
272 * batadv_mcast_mla_flags_get() - get the new multicast flags
273 * @bat_priv: the bat priv with all the soft interface information
274 *
275 * Return: A set of flags for the current/next TVLV, querier and
276 * bridge state.
277 */
278static struct batadv_mcast_mla_flags
279batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
280{
281 struct net_device *dev = bat_priv->soft_iface;
282 struct batadv_mcast_querier_state *qr4, *qr6;
283 struct batadv_mcast_mla_flags mla_flags;
284 struct net_device *bridge;
285
286 bridge = batadv_mcast_get_bridge(dev);
287
288 memset(&mla_flags, 0, sizeof(mla_flags));
289 mla_flags.enabled = 1;
290 mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv,
291 bridge);
292
293 if (!bridge)
294 return mla_flags;
295
296 dev_put(bridge);
297
298 mla_flags.bridged = 1;
299 qr4 = &mla_flags.querier_ipv4;
300 qr6 = &mla_flags.querier_ipv6;
301
302 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
303 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
304
305 qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
306 qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
307
308 qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
309 qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
310
311 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
312
313 /* 1) If no querier exists at all, then multicast listeners on
314 * our local TT clients behind the bridge will keep silent.
315 * 2) If the selected querier is on one of our local TT clients,
316 * behind the bridge, then this querier might shadow multicast
317 * listeners on our local TT clients, behind this bridge.
318 *
319 * In both cases, we will signalize other batman nodes that
320 * we need all multicast traffic of the according protocol.
321 */
322 if (!qr4->exists || qr4->shadowing) {
323 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4;
324 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4;
325 }
326
327 if (!qr6->exists || qr6->shadowing) {
328 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6;
329 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6;
330 }
331
332 return mla_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333}
334
335/**
336 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
337 * @mcast_addr: the multicast address to check
338 * @mcast_list: the list with multicast addresses to search in
339 *
340 * Return: true if the given address is already in the given list.
341 * Otherwise returns false.
342 */
343static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
344 struct hlist_head *mcast_list)
345{
346 struct batadv_hw_addr *mcast_entry;
347
348 hlist_for_each_entry(mcast_entry, mcast_list, list)
349 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
350 return true;
351
352 return false;
353}
354
355/**
David Brazdil0f672f62019-12-10 10:32:29 +0000356 * batadv_mcast_mla_softif_get_ipv4() - get softif IPv4 multicast listeners
357 * @dev: the device to collect multicast addresses from
358 * @mcast_list: a list to put found addresses into
359 * @flags: flags indicating the new multicast state
360 *
361 * Collects multicast addresses of IPv4 multicast listeners residing
362 * on this kernel on the given soft interface, dev, in
363 * the given mcast_list. In general, multicast listeners provided by
364 * your multicast receiving applications run directly on this node.
365 *
366 * Return: -ENOMEM on memory allocation error or the number of
367 * items added to the mcast_list otherwise.
368 */
369static int
370batadv_mcast_mla_softif_get_ipv4(struct net_device *dev,
371 struct hlist_head *mcast_list,
372 struct batadv_mcast_mla_flags *flags)
373{
374 struct batadv_hw_addr *new;
375 struct in_device *in_dev;
376 u8 mcast_addr[ETH_ALEN];
377 struct ip_mc_list *pmc;
378 int ret = 0;
379
380 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
381 return 0;
382
383 rcu_read_lock();
384
385 in_dev = __in_dev_get_rcu(dev);
386 if (!in_dev) {
387 rcu_read_unlock();
388 return 0;
389 }
390
391 for (pmc = rcu_dereference(in_dev->mc_list); pmc;
392 pmc = rcu_dereference(pmc->next_rcu)) {
393 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
394 ipv4_is_local_multicast(pmc->multiaddr))
395 continue;
396
397 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
398 !ipv4_is_local_multicast(pmc->multiaddr))
399 continue;
400
401 ip_eth_mc_map(pmc->multiaddr, mcast_addr);
402
403 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
404 continue;
405
406 new = kmalloc(sizeof(*new), GFP_ATOMIC);
407 if (!new) {
408 ret = -ENOMEM;
409 break;
410 }
411
412 ether_addr_copy(new->addr, mcast_addr);
413 hlist_add_head(&new->list, mcast_list);
414 ret++;
415 }
416 rcu_read_unlock();
417
418 return ret;
419}
420
421/**
422 * batadv_mcast_mla_softif_get_ipv6() - get softif IPv6 multicast listeners
423 * @dev: the device to collect multicast addresses from
424 * @mcast_list: a list to put found addresses into
425 * @flags: flags indicating the new multicast state
426 *
427 * Collects multicast addresses of IPv6 multicast listeners residing
428 * on this kernel on the given soft interface, dev, in
429 * the given mcast_list. In general, multicast listeners provided by
430 * your multicast receiving applications run directly on this node.
431 *
432 * Return: -ENOMEM on memory allocation error or the number of
433 * items added to the mcast_list otherwise.
434 */
435#if IS_ENABLED(CONFIG_IPV6)
436static int
437batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
438 struct hlist_head *mcast_list,
439 struct batadv_mcast_mla_flags *flags)
440{
441 struct batadv_hw_addr *new;
442 struct inet6_dev *in6_dev;
443 u8 mcast_addr[ETH_ALEN];
444 struct ifmcaddr6 *pmc6;
445 int ret = 0;
446
447 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
448 return 0;
449
450 rcu_read_lock();
451
452 in6_dev = __in6_dev_get(dev);
453 if (!in6_dev) {
454 rcu_read_unlock();
455 return 0;
456 }
457
458 read_lock_bh(&in6_dev->lock);
459 for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) {
460 if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) <
461 IPV6_ADDR_SCOPE_LINKLOCAL)
462 continue;
463
464 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
465 ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
466 continue;
467
468 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
469 IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) >
470 IPV6_ADDR_SCOPE_LINKLOCAL)
471 continue;
472
473 ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
474
475 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
476 continue;
477
478 new = kmalloc(sizeof(*new), GFP_ATOMIC);
479 if (!new) {
480 ret = -ENOMEM;
481 break;
482 }
483
484 ether_addr_copy(new->addr, mcast_addr);
485 hlist_add_head(&new->list, mcast_list);
486 ret++;
487 }
488 read_unlock_bh(&in6_dev->lock);
489 rcu_read_unlock();
490
491 return ret;
492}
493#else
494static inline int
495batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
496 struct hlist_head *mcast_list,
497 struct batadv_mcast_mla_flags *flags)
498{
499 return 0;
500}
501#endif
502
503/**
504 * batadv_mcast_mla_softif_get() - get softif multicast listeners
505 * @dev: the device to collect multicast addresses from
506 * @mcast_list: a list to put found addresses into
507 * @flags: flags indicating the new multicast state
508 *
509 * Collects multicast addresses of multicast listeners residing
510 * on this kernel on the given soft interface, dev, in
511 * the given mcast_list. In general, multicast listeners provided by
512 * your multicast receiving applications run directly on this node.
513 *
514 * If there is a bridge interface on top of dev, collects from that one
515 * instead. Just like with IP addresses and routes, multicast listeners
516 * will(/should) register to the bridge interface instead of an
517 * enslaved bat0.
518 *
519 * Return: -ENOMEM on memory allocation error or the number of
520 * items added to the mcast_list otherwise.
521 */
522static int
523batadv_mcast_mla_softif_get(struct net_device *dev,
524 struct hlist_head *mcast_list,
525 struct batadv_mcast_mla_flags *flags)
526{
527 struct net_device *bridge = batadv_mcast_get_bridge(dev);
528 int ret4, ret6 = 0;
529
530 if (bridge)
531 dev = bridge;
532
533 ret4 = batadv_mcast_mla_softif_get_ipv4(dev, mcast_list, flags);
534 if (ret4 < 0)
535 goto out;
536
537 ret6 = batadv_mcast_mla_softif_get_ipv6(dev, mcast_list, flags);
538 if (ret6 < 0) {
539 ret4 = 0;
540 goto out;
541 }
542
543out:
544 if (bridge)
545 dev_put(bridge);
546
547 return ret4 + ret6;
548}
549
550/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
552 * @dst: destination to write to - a multicast MAC address
553 * @src: source to read from - a multicast IP address
554 *
555 * Converts a given multicast IPv4/IPv6 address from a bridge
556 * to its matching multicast MAC address and copies it into the given
557 * destination buffer.
558 *
559 * Caller needs to make sure the destination buffer can hold
560 * at least ETH_ALEN bytes.
561 */
562static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
563{
564 if (src->proto == htons(ETH_P_IP))
565 ip_eth_mc_map(src->u.ip4, dst);
566#if IS_ENABLED(CONFIG_IPV6)
567 else if (src->proto == htons(ETH_P_IPV6))
568 ipv6_eth_mc_map(&src->u.ip6, dst);
569#endif
570 else
571 eth_zero_addr(dst);
572}
573
574/**
575 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000576 * @dev: a bridge slave whose bridge to collect multicast addresses from
577 * @mcast_list: a list to put found addresses into
David Brazdil0f672f62019-12-10 10:32:29 +0000578 * @flags: flags indicating the new multicast state
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 *
580 * Collects multicast addresses of multicast listeners residing
581 * on foreign, non-mesh devices which we gave access to our mesh via
582 * a bridge on top of the given soft interface, dev, in the given
583 * mcast_list.
584 *
585 * Return: -ENOMEM on memory allocation error or the number of
586 * items added to the mcast_list otherwise.
587 */
David Brazdil0f672f62019-12-10 10:32:29 +0000588static int batadv_mcast_mla_bridge_get(struct net_device *dev,
589 struct hlist_head *mcast_list,
590 struct batadv_mcast_mla_flags *flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591{
592 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000593 struct br_ip_list *br_ip_entry, *tmp;
David Brazdil0f672f62019-12-10 10:32:29 +0000594 u8 tvlv_flags = flags->tvlv_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000595 struct batadv_hw_addr *new;
596 u8 mcast_addr[ETH_ALEN];
597 int ret;
598
599 /* we don't need to detect these devices/listeners, the IGMP/MLD
600 * snooping code of the Linux bridge already does that for us
601 */
602 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
603 if (ret < 0)
604 goto out;
605
606 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
David Brazdil0f672f62019-12-10 10:32:29 +0000607 if (br_ip_entry->addr.proto == htons(ETH_P_IP)) {
608 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
609 continue;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610
David Brazdil0f672f62019-12-10 10:32:29 +0000611 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
612 ipv4_is_local_multicast(br_ip_entry->addr.u.ip4))
613 continue;
614
615 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
616 !ipv4_is_local_multicast(br_ip_entry->addr.u.ip4))
617 continue;
618 }
619
620#if IS_ENABLED(CONFIG_IPV6)
621 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) {
622 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
623 continue;
624
625 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
626 ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.u.ip6))
627 continue;
628
629 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
630 IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.u.ip6) >
631 IPV6_ADDR_SCOPE_LINKLOCAL)
632 continue;
633 }
634#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635
636 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
637 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
638 continue;
639
640 new = kmalloc(sizeof(*new), GFP_ATOMIC);
641 if (!new) {
642 ret = -ENOMEM;
643 break;
644 }
645
646 ether_addr_copy(new->addr, mcast_addr);
647 hlist_add_head(&new->list, mcast_list);
648 }
649
650out:
651 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
652 list_del(&br_ip_entry->list);
653 kfree(br_ip_entry);
654 }
655
656 return ret;
657}
658
659/**
660 * batadv_mcast_mla_list_free() - free a list of multicast addresses
661 * @mcast_list: the list to free
662 *
663 * Removes and frees all items in the given mcast_list.
664 */
665static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
666{
667 struct batadv_hw_addr *mcast_entry;
668 struct hlist_node *tmp;
669
670 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
671 hlist_del(&mcast_entry->list);
672 kfree(mcast_entry);
673 }
674}
675
676/**
677 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
678 * @bat_priv: the bat priv with all the soft interface information
679 * @mcast_list: a list of addresses which should _not_ be removed
680 *
681 * Retracts the announcement of any multicast listener from the
682 * translation table except the ones listed in the given mcast_list.
683 *
684 * If mcast_list is NULL then all are retracted.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685 */
686static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
687 struct hlist_head *mcast_list)
688{
689 struct batadv_hw_addr *mcast_entry;
690 struct hlist_node *tmp;
691
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000692 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
693 list) {
694 if (mcast_list &&
695 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
696 mcast_list))
697 continue;
698
699 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
700 BATADV_NO_FLAGS,
701 "mcast TT outdated", false);
702
703 hlist_del(&mcast_entry->list);
704 kfree(mcast_entry);
705 }
706}
707
708/**
709 * batadv_mcast_mla_tt_add() - add multicast listener announcements
710 * @bat_priv: the bat priv with all the soft interface information
711 * @mcast_list: a list of addresses which are going to get added
712 *
713 * Adds multicast listener announcements from the given mcast_list to the
714 * translation table if they have not been added yet.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000715 */
716static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
717 struct hlist_head *mcast_list)
718{
719 struct batadv_hw_addr *mcast_entry;
720 struct hlist_node *tmp;
721
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722 if (!mcast_list)
723 return;
724
725 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
726 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
727 &bat_priv->mcast.mla_list))
728 continue;
729
730 if (!batadv_tt_local_add(bat_priv->soft_iface,
731 mcast_entry->addr, BATADV_NO_FLAGS,
732 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
733 continue;
734
735 hlist_del(&mcast_entry->list);
736 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
737 }
738}
739
740/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741 * batadv_mcast_querier_log() - debug output regarding the querier status on
742 * link
743 * @bat_priv: the bat priv with all the soft interface information
744 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
745 * @old_state: the previous querier state on our link
746 * @new_state: the new querier state on our link
747 *
748 * Outputs debug messages to the logging facility with log level 'mcast'
749 * regarding changes to the querier status on the link which are relevant
750 * to our multicast optimizations.
751 *
752 * Usually this is about whether a querier appeared or vanished in
753 * our mesh or whether the querier is in the suboptimal position of being
754 * behind our local bridge segment: Snooping switches will directly
755 * forward listener reports to the querier, therefore batman-adv and
756 * the bridge will potentially not see these listeners - the querier is
757 * potentially shadowing listeners from us then.
758 *
759 * This is only interesting for nodes with a bridge on top of their
760 * soft interface.
761 */
762static void
763batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
764 struct batadv_mcast_querier_state *old_state,
765 struct batadv_mcast_querier_state *new_state)
766{
767 if (!old_state->exists && new_state->exists)
768 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
769 str_proto);
770 else if (old_state->exists && !new_state->exists)
771 batadv_info(bat_priv->soft_iface,
772 "%s Querier disappeared - multicast optimizations disabled\n",
773 str_proto);
David Brazdil0f672f62019-12-10 10:32:29 +0000774 else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000775 batadv_info(bat_priv->soft_iface,
776 "No %s Querier present - multicast optimizations disabled\n",
777 str_proto);
778
779 if (new_state->exists) {
780 if ((!old_state->shadowing && new_state->shadowing) ||
781 (!old_state->exists && new_state->shadowing))
782 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
783 "%s Querier is behind our bridged segment: Might shadow listeners\n",
784 str_proto);
785 else if (old_state->shadowing && !new_state->shadowing)
786 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
787 "%s Querier is not behind our bridged segment\n",
788 str_proto);
789 }
790}
791
792/**
793 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
794 * setups
795 * @bat_priv: the bat priv with all the soft interface information
David Brazdil0f672f62019-12-10 10:32:29 +0000796 * @new_flags: flags indicating the new multicast state
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000797 *
798 * If no bridges are ever used on this node, then this function does nothing.
799 *
800 * Otherwise this function outputs debug information to the 'mcast' log level
801 * which might be relevant to our multicast optimizations.
802 *
803 * More precisely, it outputs information when a bridge interface is added or
804 * removed from a soft interface. And when a bridge is present, it further
805 * outputs information about the querier state which is relevant for the
806 * multicast flags this node is going to set.
807 */
808static void
David Brazdil0f672f62019-12-10 10:32:29 +0000809batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
810 struct batadv_mcast_mla_flags *new_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000811{
David Brazdil0f672f62019-12-10 10:32:29 +0000812 struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
813
814 if (!old_flags->bridged && new_flags->bridged)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000815 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
816 "Bridge added: Setting Unsnoopables(U)-flag\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000817 else if (old_flags->bridged && !new_flags->bridged)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000818 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
819 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
820
David Brazdil0f672f62019-12-10 10:32:29 +0000821 if (new_flags->bridged) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000822 batadv_mcast_querier_log(bat_priv, "IGMP",
David Brazdil0f672f62019-12-10 10:32:29 +0000823 &old_flags->querier_ipv4,
824 &new_flags->querier_ipv4);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000825 batadv_mcast_querier_log(bat_priv, "MLD",
David Brazdil0f672f62019-12-10 10:32:29 +0000826 &old_flags->querier_ipv6,
827 &new_flags->querier_ipv6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828 }
829}
830
831/**
832 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
833 * @bat_priv: the bat priv with all the soft interface information
David Brazdil0f672f62019-12-10 10:32:29 +0000834 * @flags: TVLV flags indicating the new multicast state
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000835 *
David Brazdil0f672f62019-12-10 10:32:29 +0000836 * Whenever the multicast TVLV flags this nodes announces change this notifies
837 * userspace via the 'mcast' log level.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000838 */
839static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
840{
David Brazdil0f672f62019-12-10 10:32:29 +0000841 bool old_enabled = bat_priv->mcast.mla_flags.enabled;
842 u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags;
843 char str_old_flags[] = "[.... . ]";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844
David Brazdil0f672f62019-12-10 10:32:29 +0000845 sprintf(str_old_flags, "[%c%c%c%s%s]",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000846 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
847 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
David Brazdil0f672f62019-12-10 10:32:29 +0000848 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
849 !(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
850 !(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000851
852 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
David Brazdil0f672f62019-12-10 10:32:29 +0000853 "Changing multicast flags from '%s' to '[%c%c%c%s%s]'\n",
854 old_enabled ? str_old_flags : "<undefined>",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000855 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
856 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
David Brazdil0f672f62019-12-10 10:32:29 +0000857 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
858 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
859 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000860}
861
862/**
David Brazdil0f672f62019-12-10 10:32:29 +0000863 * batadv_mcast_mla_flags_update() - update multicast flags
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000864 * @bat_priv: the bat priv with all the soft interface information
David Brazdil0f672f62019-12-10 10:32:29 +0000865 * @flags: flags indicating the new multicast state
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000866 *
867 * Updates the own multicast tvlv with our current multicast related settings,
868 * capabilities and inabilities.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000869 */
David Brazdil0f672f62019-12-10 10:32:29 +0000870static void
871batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
872 struct batadv_mcast_mla_flags *flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000873{
874 struct batadv_tvlv_mcast_data mcast_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000875
David Brazdil0f672f62019-12-10 10:32:29 +0000876 if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
877 return;
878
879 batadv_mcast_bridge_log(bat_priv, flags);
880 batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
881
882 mcast_data.flags = flags->tvlv_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000883 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
884
David Brazdil0f672f62019-12-10 10:32:29 +0000885 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
886 &mcast_data, sizeof(mcast_data));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887
David Brazdil0f672f62019-12-10 10:32:29 +0000888 bat_priv->mcast.mla_flags = *flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000889}
890
891/**
892 * __batadv_mcast_mla_update() - update the own MLAs
893 * @bat_priv: the bat priv with all the soft interface information
894 *
895 * Updates the own multicast listener announcements in the translation
896 * table as well as the own, announced multicast tvlv container.
897 *
898 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
899 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
900 * ensured by the non-parallel execution of the worker this function
901 * belongs to.
902 */
903static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
904{
905 struct net_device *soft_iface = bat_priv->soft_iface;
906 struct hlist_head mcast_list = HLIST_HEAD_INIT;
David Brazdil0f672f62019-12-10 10:32:29 +0000907 struct batadv_mcast_mla_flags flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000908 int ret;
909
David Brazdil0f672f62019-12-10 10:32:29 +0000910 flags = batadv_mcast_mla_flags_get(bat_priv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911
David Brazdil0f672f62019-12-10 10:32:29 +0000912 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list, &flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 if (ret < 0)
914 goto out;
915
David Brazdil0f672f62019-12-10 10:32:29 +0000916 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list, &flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000917 if (ret < 0)
918 goto out;
919
David Brazdil0f672f62019-12-10 10:32:29 +0000920 spin_lock(&bat_priv->mcast.mla_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
922 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
David Brazdil0f672f62019-12-10 10:32:29 +0000923 batadv_mcast_mla_flags_update(bat_priv, &flags);
924 spin_unlock(&bat_priv->mcast.mla_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000925
926out:
927 batadv_mcast_mla_list_free(&mcast_list);
928}
929
930/**
931 * batadv_mcast_mla_update() - update the own MLAs
932 * @work: kernel work struct
933 *
934 * Updates the own multicast listener announcements in the translation
935 * table as well as the own, announced multicast tvlv container.
936 *
937 * In the end, reschedules the work timer.
938 */
939static void batadv_mcast_mla_update(struct work_struct *work)
940{
941 struct delayed_work *delayed_work;
942 struct batadv_priv_mcast *priv_mcast;
943 struct batadv_priv *bat_priv;
944
945 delayed_work = to_delayed_work(work);
946 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
947 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
948
949 __batadv_mcast_mla_update(bat_priv);
950 batadv_mcast_start_timer(bat_priv);
951}
952
953/**
954 * batadv_mcast_is_report_ipv4() - check for IGMP reports
955 * @skb: the ethernet frame destined for the mesh
956 *
957 * This call might reallocate skb data.
958 *
959 * Checks whether the given frame is a valid IGMP report.
960 *
961 * Return: If so then true, otherwise false.
962 */
963static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
964{
David Brazdil0f672f62019-12-10 10:32:29 +0000965 if (ip_mc_check_igmp(skb) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000966 return false;
967
968 switch (igmp_hdr(skb)->type) {
969 case IGMP_HOST_MEMBERSHIP_REPORT:
970 case IGMPV2_HOST_MEMBERSHIP_REPORT:
971 case IGMPV3_HOST_MEMBERSHIP_REPORT:
972 return true;
973 }
974
975 return false;
976}
977
978/**
979 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
980 * potential
981 * @bat_priv: the bat priv with all the soft interface information
982 * @skb: the IPv4 packet to check
983 * @is_unsnoopable: stores whether the destination is snoopable
David Brazdil0f672f62019-12-10 10:32:29 +0000984 * @is_routable: stores whether the destination is routable
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000985 *
986 * Checks whether the given IPv4 packet has the potential to be forwarded with a
987 * mode more optimal than classic flooding.
988 *
989 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
990 * allocation failure.
991 */
992static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
993 struct sk_buff *skb,
David Brazdil0f672f62019-12-10 10:32:29 +0000994 bool *is_unsnoopable,
995 int *is_routable)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000996{
997 struct iphdr *iphdr;
998
999 /* We might fail due to out-of-memory -> drop it */
1000 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
1001 return -ENOMEM;
1002
1003 if (batadv_mcast_is_report_ipv4(skb))
1004 return -EINVAL;
1005
1006 iphdr = ip_hdr(skb);
1007
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001008 /* link-local multicast listeners behind a bridge are
1009 * not snoopable (see RFC4541, section 2.1.2.2)
1010 */
David Brazdil0f672f62019-12-10 10:32:29 +00001011 if (ipv4_is_local_multicast(iphdr->daddr))
1012 *is_unsnoopable = true;
1013 else
1014 *is_routable = ETH_P_IP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001015
1016 return 0;
1017}
1018
1019/**
1020 * batadv_mcast_is_report_ipv6() - check for MLD reports
1021 * @skb: the ethernet frame destined for the mesh
1022 *
1023 * This call might reallocate skb data.
1024 *
1025 * Checks whether the given frame is a valid MLD report.
1026 *
1027 * Return: If so then true, otherwise false.
1028 */
1029static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
1030{
David Brazdil0f672f62019-12-10 10:32:29 +00001031 if (ipv6_mc_check_mld(skb) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032 return false;
1033
1034 switch (icmp6_hdr(skb)->icmp6_type) {
1035 case ICMPV6_MGM_REPORT:
1036 case ICMPV6_MLD2_REPORT:
1037 return true;
1038 }
1039
1040 return false;
1041}
1042
1043/**
1044 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
1045 * potential
1046 * @bat_priv: the bat priv with all the soft interface information
1047 * @skb: the IPv6 packet to check
1048 * @is_unsnoopable: stores whether the destination is snoopable
David Brazdil0f672f62019-12-10 10:32:29 +00001049 * @is_routable: stores whether the destination is routable
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050 *
1051 * Checks whether the given IPv6 packet has the potential to be forwarded with a
1052 * mode more optimal than classic flooding.
1053 *
1054 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1055 */
1056static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
1057 struct sk_buff *skb,
David Brazdil0f672f62019-12-10 10:32:29 +00001058 bool *is_unsnoopable,
1059 int *is_routable)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001060{
1061 struct ipv6hdr *ip6hdr;
1062
1063 /* We might fail due to out-of-memory -> drop it */
1064 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
1065 return -ENOMEM;
1066
1067 if (batadv_mcast_is_report_ipv6(skb))
1068 return -EINVAL;
1069
1070 ip6hdr = ipv6_hdr(skb);
1071
David Brazdil0f672f62019-12-10 10:32:29 +00001072 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073 return -EINVAL;
1074
1075 /* link-local-all-nodes multicast listeners behind a bridge are
1076 * not snoopable (see RFC4541, section 3, paragraph 3)
1077 */
1078 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
1079 *is_unsnoopable = true;
David Brazdil0f672f62019-12-10 10:32:29 +00001080 else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL)
1081 *is_routable = ETH_P_IPV6;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001082
1083 return 0;
1084}
1085
1086/**
1087 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
1088 * @bat_priv: the bat priv with all the soft interface information
1089 * @skb: the multicast frame to check
1090 * @is_unsnoopable: stores whether the destination is snoopable
David Brazdil0f672f62019-12-10 10:32:29 +00001091 * @is_routable: stores whether the destination is routable
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001092 *
1093 * Checks whether the given multicast ethernet frame has the potential to be
1094 * forwarded with a mode more optimal than classic flooding.
1095 *
1096 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1097 */
1098static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
1099 struct sk_buff *skb,
David Brazdil0f672f62019-12-10 10:32:29 +00001100 bool *is_unsnoopable,
1101 int *is_routable)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102{
1103 struct ethhdr *ethhdr = eth_hdr(skb);
1104
1105 if (!atomic_read(&bat_priv->multicast_mode))
1106 return -EINVAL;
1107
1108 switch (ntohs(ethhdr->h_proto)) {
1109 case ETH_P_IP:
1110 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
David Brazdil0f672f62019-12-10 10:32:29 +00001111 is_unsnoopable,
1112 is_routable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001113 case ETH_P_IPV6:
1114 if (!IS_ENABLED(CONFIG_IPV6))
1115 return -EINVAL;
1116
1117 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
David Brazdil0f672f62019-12-10 10:32:29 +00001118 is_unsnoopable,
1119 is_routable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001120 default:
1121 return -EINVAL;
1122 }
1123}
1124
1125/**
1126 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
1127 * interest
1128 * @bat_priv: the bat priv with all the soft interface information
1129 * @ethhdr: ethernet header of a packet
1130 *
1131 * Return: the number of nodes which want all IPv4 multicast traffic if the
1132 * given ethhdr is from an IPv4 packet or the number of nodes which want all
1133 * IPv6 traffic if it matches an IPv6 packet.
1134 */
1135static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
1136 struct ethhdr *ethhdr)
1137{
1138 switch (ntohs(ethhdr->h_proto)) {
1139 case ETH_P_IP:
1140 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
1141 case ETH_P_IPV6:
1142 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
1143 default:
1144 /* we shouldn't be here... */
1145 return 0;
1146 }
1147}
1148
1149/**
David Brazdil0f672f62019-12-10 10:32:29 +00001150 * batadv_mcast_forw_rtr_count() - count nodes with a multicast router
1151 * @bat_priv: the bat priv with all the soft interface information
1152 * @protocol: the ethernet protocol type to count multicast routers for
1153 *
1154 * Return: the number of nodes which want all routable IPv4 multicast traffic
1155 * if the protocol is ETH_P_IP or the number of nodes which want all routable
1156 * IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0.
1157 */
1158
1159static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
1160 int protocol)
1161{
1162 switch (protocol) {
1163 case ETH_P_IP:
1164 return atomic_read(&bat_priv->mcast.num_want_all_rtr4);
1165 case ETH_P_IPV6:
1166 return atomic_read(&bat_priv->mcast.num_want_all_rtr6);
1167 default:
1168 return 0;
1169 }
1170}
1171
1172/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001173 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
1174 * @bat_priv: the bat priv with all the soft interface information
1175 * @ethhdr: the ether header containing the multicast destination
1176 *
1177 * Return: an orig_node matching the multicast address provided by ethhdr
1178 * via a translation table lookup. This increases the returned nodes refcount.
1179 */
1180static struct batadv_orig_node *
1181batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
1182 struct ethhdr *ethhdr)
1183{
1184 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
1185 BATADV_NO_FLAGS);
1186}
1187
1188/**
1189 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
1190 * @bat_priv: the bat priv with all the soft interface information
1191 *
1192 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
1193 * increases its refcount.
1194 */
1195static struct batadv_orig_node *
1196batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
1197{
1198 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1199
1200 rcu_read_lock();
1201 hlist_for_each_entry_rcu(tmp_orig_node,
1202 &bat_priv->mcast.want_all_ipv4_list,
1203 mcast_want_all_ipv4_node) {
1204 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1205 continue;
1206
1207 orig_node = tmp_orig_node;
1208 break;
1209 }
1210 rcu_read_unlock();
1211
1212 return orig_node;
1213}
1214
1215/**
1216 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
1217 * @bat_priv: the bat priv with all the soft interface information
1218 *
1219 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
1220 * and increases its refcount.
1221 */
1222static struct batadv_orig_node *
1223batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
1224{
1225 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1226
1227 rcu_read_lock();
1228 hlist_for_each_entry_rcu(tmp_orig_node,
1229 &bat_priv->mcast.want_all_ipv6_list,
1230 mcast_want_all_ipv6_node) {
1231 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1232 continue;
1233
1234 orig_node = tmp_orig_node;
1235 break;
1236 }
1237 rcu_read_unlock();
1238
1239 return orig_node;
1240}
1241
1242/**
1243 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
1244 * @bat_priv: the bat priv with all the soft interface information
1245 * @ethhdr: an ethernet header to determine the protocol family from
1246 *
1247 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
1248 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
1249 * increases its refcount.
1250 */
1251static struct batadv_orig_node *
1252batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
1253 struct ethhdr *ethhdr)
1254{
1255 switch (ntohs(ethhdr->h_proto)) {
1256 case ETH_P_IP:
1257 return batadv_mcast_forw_ipv4_node_get(bat_priv);
1258 case ETH_P_IPV6:
1259 return batadv_mcast_forw_ipv6_node_get(bat_priv);
1260 default:
1261 /* we shouldn't be here... */
1262 return NULL;
1263 }
1264}
1265
1266/**
1267 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
1268 * @bat_priv: the bat priv with all the soft interface information
1269 *
1270 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
1271 * set and increases its refcount.
1272 */
1273static struct batadv_orig_node *
1274batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
1275{
1276 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1277
1278 rcu_read_lock();
1279 hlist_for_each_entry_rcu(tmp_orig_node,
1280 &bat_priv->mcast.want_all_unsnoopables_list,
1281 mcast_want_all_unsnoopables_node) {
1282 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1283 continue;
1284
1285 orig_node = tmp_orig_node;
1286 break;
1287 }
1288 rcu_read_unlock();
1289
1290 return orig_node;
1291}
1292
1293/**
David Brazdil0f672f62019-12-10 10:32:29 +00001294 * batadv_mcast_forw_rtr4_node_get() - get a node with an ipv4 mcast router flag
1295 * @bat_priv: the bat priv with all the soft interface information
1296 *
1297 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR4 flag unset and
1298 * increases its refcount.
1299 */
1300static struct batadv_orig_node *
1301batadv_mcast_forw_rtr4_node_get(struct batadv_priv *bat_priv)
1302{
1303 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1304
1305 rcu_read_lock();
1306 hlist_for_each_entry_rcu(tmp_orig_node,
1307 &bat_priv->mcast.want_all_rtr4_list,
1308 mcast_want_all_rtr4_node) {
1309 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1310 continue;
1311
1312 orig_node = tmp_orig_node;
1313 break;
1314 }
1315 rcu_read_unlock();
1316
1317 return orig_node;
1318}
1319
1320/**
1321 * batadv_mcast_forw_rtr6_node_get() - get a node with an ipv6 mcast router flag
1322 * @bat_priv: the bat priv with all the soft interface information
1323 *
1324 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR6 flag unset
1325 * and increases its refcount.
1326 */
1327static struct batadv_orig_node *
1328batadv_mcast_forw_rtr6_node_get(struct batadv_priv *bat_priv)
1329{
1330 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1331
1332 rcu_read_lock();
1333 hlist_for_each_entry_rcu(tmp_orig_node,
1334 &bat_priv->mcast.want_all_rtr6_list,
1335 mcast_want_all_rtr6_node) {
1336 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1337 continue;
1338
1339 orig_node = tmp_orig_node;
1340 break;
1341 }
1342 rcu_read_unlock();
1343
1344 return orig_node;
1345}
1346
1347/**
1348 * batadv_mcast_forw_rtr_node_get() - get a node with an ipv4/ipv6 router flag
1349 * @bat_priv: the bat priv with all the soft interface information
1350 * @ethhdr: an ethernet header to determine the protocol family from
1351 *
1352 * Return: an orig_node which has no BATADV_MCAST_WANT_NO_RTR4 or
1353 * BATADV_MCAST_WANT_NO_RTR6 flag, depending on the provided ethhdr, set and
1354 * increases its refcount.
1355 */
1356static struct batadv_orig_node *
1357batadv_mcast_forw_rtr_node_get(struct batadv_priv *bat_priv,
1358 struct ethhdr *ethhdr)
1359{
1360 switch (ntohs(ethhdr->h_proto)) {
1361 case ETH_P_IP:
1362 return batadv_mcast_forw_rtr4_node_get(bat_priv);
1363 case ETH_P_IPV6:
1364 return batadv_mcast_forw_rtr6_node_get(bat_priv);
1365 default:
1366 /* we shouldn't be here... */
1367 return NULL;
1368 }
1369}
1370
1371/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001372 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
1373 * @bat_priv: the bat priv with all the soft interface information
1374 * @skb: The multicast packet to check
1375 * @orig: an originator to be set to forward the skb to
1376 *
1377 * Return: the forwarding mode as enum batadv_forw_mode and in case of
1378 * BATADV_FORW_SINGLE set the orig to the single originator the skb
1379 * should be forwarded to.
1380 */
1381enum batadv_forw_mode
1382batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
1383 struct batadv_orig_node **orig)
1384{
1385 int ret, tt_count, ip_count, unsnoop_count, total_count;
1386 bool is_unsnoopable = false;
David Brazdil0f672f62019-12-10 10:32:29 +00001387 unsigned int mcast_fanout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001388 struct ethhdr *ethhdr;
David Brazdil0f672f62019-12-10 10:32:29 +00001389 int is_routable = 0;
1390 int rtr_count = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001391
David Brazdil0f672f62019-12-10 10:32:29 +00001392 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable,
1393 &is_routable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001394 if (ret == -ENOMEM)
1395 return BATADV_FORW_NONE;
1396 else if (ret < 0)
1397 return BATADV_FORW_ALL;
1398
1399 ethhdr = eth_hdr(skb);
1400
1401 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1402 BATADV_NO_FLAGS);
1403 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1404 unsnoop_count = !is_unsnoopable ? 0 :
1405 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
David Brazdil0f672f62019-12-10 10:32:29 +00001406 rtr_count = batadv_mcast_forw_rtr_count(bat_priv, is_routable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001407
David Brazdil0f672f62019-12-10 10:32:29 +00001408 total_count = tt_count + ip_count + unsnoop_count + rtr_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001409
1410 switch (total_count) {
1411 case 1:
1412 if (tt_count)
1413 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1414 else if (ip_count)
1415 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1416 else if (unsnoop_count)
1417 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
David Brazdil0f672f62019-12-10 10:32:29 +00001418 else if (rtr_count)
1419 *orig = batadv_mcast_forw_rtr_node_get(bat_priv,
1420 ethhdr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421
1422 if (*orig)
1423 return BATADV_FORW_SINGLE;
1424
1425 /* fall through */
1426 case 0:
1427 return BATADV_FORW_NONE;
1428 default:
David Brazdil0f672f62019-12-10 10:32:29 +00001429 mcast_fanout = atomic_read(&bat_priv->multicast_fanout);
1430
1431 if (!unsnoop_count && total_count <= mcast_fanout)
1432 return BATADV_FORW_SOME;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433 }
David Brazdil0f672f62019-12-10 10:32:29 +00001434
1435 return BATADV_FORW_ALL;
1436}
1437
1438/**
Olivier Deprez0e641232021-09-23 10:07:05 +02001439 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1440 * @bat_priv: the bat priv with all the soft interface information
1441 * @skb: the multicast packet to send
1442 * @vid: the vlan identifier
1443 * @orig_node: the originator to send the packet to
1444 *
1445 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1446 */
1447int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1448 struct sk_buff *skb,
1449 unsigned short vid,
1450 struct batadv_orig_node *orig_node)
1451{
1452 /* Avoid sending multicast-in-unicast packets to other BLA
1453 * gateways - they already got the frame from the LAN side
1454 * we share with them.
1455 * TODO: Refactor to take BLA into account earlier, to avoid
1456 * reducing the mcast_fanout count.
1457 */
1458 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1459 dev_kfree_skb(skb);
1460 return NET_XMIT_SUCCESS;
1461 }
1462
1463 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1464 orig_node, vid);
1465}
1466
1467/**
David Brazdil0f672f62019-12-10 10:32:29 +00001468 * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1469 * @bat_priv: the bat priv with all the soft interface information
1470 * @skb: the multicast packet to transmit
1471 * @vid: the vlan identifier
1472 *
1473 * Sends copies of a frame with multicast destination to any multicast
1474 * listener registered in the translation table. A transmission is performed
1475 * via a batman-adv unicast packet for each such destination node.
1476 *
1477 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1478 * otherwise.
1479 */
1480static int
1481batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
1482 unsigned short vid)
1483{
1484 int ret = NET_XMIT_SUCCESS;
1485 struct sk_buff *newskb;
1486
1487 struct batadv_tt_orig_list_entry *orig_entry;
1488
1489 struct batadv_tt_global_entry *tt_global;
1490 const u8 *addr = eth_hdr(skb)->h_dest;
1491
1492 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
1493 if (!tt_global)
1494 goto out;
1495
1496 rcu_read_lock();
1497 hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
1498 newskb = skb_copy(skb, GFP_ATOMIC);
1499 if (!newskb) {
1500 ret = NET_XMIT_DROP;
1501 break;
1502 }
1503
Olivier Deprez0e641232021-09-23 10:07:05 +02001504 batadv_mcast_forw_send_orig(bat_priv, newskb, vid,
1505 orig_entry->orig_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001506 }
1507 rcu_read_unlock();
1508
1509 batadv_tt_global_entry_put(tt_global);
1510
1511out:
1512 return ret;
1513}
1514
1515/**
1516 * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1517 * @bat_priv: the bat priv with all the soft interface information
1518 * @skb: the multicast packet to transmit
1519 * @vid: the vlan identifier
1520 *
1521 * Sends copies of a frame with multicast destination to any node with a
1522 * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1523 * batman-adv unicast packet for each such destination node.
1524 *
1525 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1526 * otherwise.
1527 */
1528static int
1529batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
1530 struct sk_buff *skb, unsigned short vid)
1531{
1532 struct batadv_orig_node *orig_node;
1533 int ret = NET_XMIT_SUCCESS;
1534 struct sk_buff *newskb;
1535
1536 rcu_read_lock();
1537 hlist_for_each_entry_rcu(orig_node,
1538 &bat_priv->mcast.want_all_ipv4_list,
1539 mcast_want_all_ipv4_node) {
1540 newskb = skb_copy(skb, GFP_ATOMIC);
1541 if (!newskb) {
1542 ret = NET_XMIT_DROP;
1543 break;
1544 }
1545
Olivier Deprez0e641232021-09-23 10:07:05 +02001546 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001547 }
1548 rcu_read_unlock();
1549 return ret;
1550}
1551
1552/**
1553 * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1554 * @bat_priv: the bat priv with all the soft interface information
1555 * @skb: The multicast packet to transmit
1556 * @vid: the vlan identifier
1557 *
1558 * Sends copies of a frame with multicast destination to any node with a
1559 * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1560 * batman-adv unicast packet for each such destination node.
1561 *
1562 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1563 * otherwise.
1564 */
1565static int
1566batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
1567 struct sk_buff *skb, unsigned short vid)
1568{
1569 struct batadv_orig_node *orig_node;
1570 int ret = NET_XMIT_SUCCESS;
1571 struct sk_buff *newskb;
1572
1573 rcu_read_lock();
1574 hlist_for_each_entry_rcu(orig_node,
1575 &bat_priv->mcast.want_all_ipv6_list,
1576 mcast_want_all_ipv6_node) {
1577 newskb = skb_copy(skb, GFP_ATOMIC);
1578 if (!newskb) {
1579 ret = NET_XMIT_DROP;
1580 break;
1581 }
1582
Olivier Deprez0e641232021-09-23 10:07:05 +02001583 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001584 }
1585 rcu_read_unlock();
1586 return ret;
1587}
1588
1589/**
1590 * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1591 * @bat_priv: the bat priv with all the soft interface information
1592 * @skb: the multicast packet to transmit
1593 * @vid: the vlan identifier
1594 *
1595 * Sends copies of a frame with multicast destination to any node with a
1596 * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1597 * transmission is performed via a batman-adv unicast packet for each such
1598 * destination node.
1599 *
1600 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1601 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1602 */
1603static int
1604batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
1605 struct sk_buff *skb, unsigned short vid)
1606{
1607 switch (ntohs(eth_hdr(skb)->h_proto)) {
1608 case ETH_P_IP:
1609 return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
1610 case ETH_P_IPV6:
1611 return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
1612 default:
1613 /* we shouldn't be here... */
1614 return NET_XMIT_DROP;
1615 }
1616}
1617
1618/**
1619 * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
1620 * @bat_priv: the bat priv with all the soft interface information
1621 * @skb: the multicast packet to transmit
1622 * @vid: the vlan identifier
1623 *
1624 * Sends copies of a frame with multicast destination to any node with a
1625 * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
1626 * batman-adv unicast packet for each such destination node.
1627 *
1628 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1629 * otherwise.
1630 */
1631static int
1632batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
1633 struct sk_buff *skb, unsigned short vid)
1634{
1635 struct batadv_orig_node *orig_node;
1636 int ret = NET_XMIT_SUCCESS;
1637 struct sk_buff *newskb;
1638
1639 rcu_read_lock();
1640 hlist_for_each_entry_rcu(orig_node,
1641 &bat_priv->mcast.want_all_rtr4_list,
1642 mcast_want_all_rtr4_node) {
1643 newskb = skb_copy(skb, GFP_ATOMIC);
1644 if (!newskb) {
1645 ret = NET_XMIT_DROP;
1646 break;
1647 }
1648
Olivier Deprez0e641232021-09-23 10:07:05 +02001649 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001650 }
1651 rcu_read_unlock();
1652 return ret;
1653}
1654
1655/**
1656 * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
1657 * @bat_priv: the bat priv with all the soft interface information
1658 * @skb: The multicast packet to transmit
1659 * @vid: the vlan identifier
1660 *
1661 * Sends copies of a frame with multicast destination to any node with a
1662 * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
1663 * batman-adv unicast packet for each such destination node.
1664 *
1665 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1666 * otherwise.
1667 */
1668static int
1669batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
1670 struct sk_buff *skb, unsigned short vid)
1671{
1672 struct batadv_orig_node *orig_node;
1673 int ret = NET_XMIT_SUCCESS;
1674 struct sk_buff *newskb;
1675
1676 rcu_read_lock();
1677 hlist_for_each_entry_rcu(orig_node,
1678 &bat_priv->mcast.want_all_rtr6_list,
1679 mcast_want_all_rtr6_node) {
1680 newskb = skb_copy(skb, GFP_ATOMIC);
1681 if (!newskb) {
1682 ret = NET_XMIT_DROP;
1683 break;
1684 }
1685
Olivier Deprez0e641232021-09-23 10:07:05 +02001686 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001687 }
1688 rcu_read_unlock();
1689 return ret;
1690}
1691
1692/**
1693 * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
1694 * @bat_priv: the bat priv with all the soft interface information
1695 * @skb: the multicast packet to transmit
1696 * @vid: the vlan identifier
1697 *
1698 * Sends copies of a frame with multicast destination to any node with a
1699 * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
1700 * transmission is performed via a batman-adv unicast packet for each such
1701 * destination node.
1702 *
1703 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1704 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1705 */
1706static int
1707batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
1708 struct sk_buff *skb, unsigned short vid)
1709{
1710 switch (ntohs(eth_hdr(skb)->h_proto)) {
1711 case ETH_P_IP:
1712 return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
1713 case ETH_P_IPV6:
1714 return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
1715 default:
1716 /* we shouldn't be here... */
1717 return NET_XMIT_DROP;
1718 }
1719}
1720
1721/**
1722 * batadv_mcast_forw_send() - send packet to any detected multicast recpient
1723 * @bat_priv: the bat priv with all the soft interface information
1724 * @skb: the multicast packet to transmit
1725 * @vid: the vlan identifier
1726 *
1727 * Sends copies of a frame with multicast destination to any node that signaled
1728 * interest in it, that is either via the translation table or the according
1729 * want-all flags. A transmission is performed via a batman-adv unicast packet
1730 * for each such destination node.
1731 *
1732 * The given skb is consumed/freed.
1733 *
1734 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1735 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1736 */
1737int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
1738 unsigned short vid)
1739{
1740 int ret;
1741
1742 ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
1743 if (ret != NET_XMIT_SUCCESS) {
1744 kfree_skb(skb);
1745 return ret;
1746 }
1747
1748 ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
1749 if (ret != NET_XMIT_SUCCESS) {
1750 kfree_skb(skb);
1751 return ret;
1752 }
1753
1754 ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
1755 if (ret != NET_XMIT_SUCCESS) {
1756 kfree_skb(skb);
1757 return ret;
1758 }
1759
1760 consume_skb(skb);
1761 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001762}
1763
1764/**
1765 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1766 * @bat_priv: the bat priv with all the soft interface information
1767 * @orig: the orig_node which multicast state might have changed of
1768 * @mcast_flags: flags indicating the new multicast state
1769 *
1770 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1771 * orig, has toggled then this method updates counter and list accordingly.
1772 *
1773 * Caller needs to hold orig->mcast_handler_lock.
1774 */
1775static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1776 struct batadv_orig_node *orig,
1777 u8 mcast_flags)
1778{
1779 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1780 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1781
1782 lockdep_assert_held(&orig->mcast_handler_lock);
1783
1784 /* switched from flag unset to set */
1785 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1786 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1787 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1788
1789 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1790 /* flag checks above + mcast_handler_lock prevents this */
1791 WARN_ON(!hlist_unhashed(node));
1792
1793 hlist_add_head_rcu(node, head);
1794 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1795 /* switched from flag set to unset */
1796 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1797 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1798 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1799
1800 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1801 /* flag checks above + mcast_handler_lock prevents this */
1802 WARN_ON(hlist_unhashed(node));
1803
1804 hlist_del_init_rcu(node);
1805 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1806 }
1807}
1808
1809/**
1810 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1811 * @bat_priv: the bat priv with all the soft interface information
1812 * @orig: the orig_node which multicast state might have changed of
1813 * @mcast_flags: flags indicating the new multicast state
1814 *
1815 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1816 * toggled then this method updates counter and list accordingly.
1817 *
1818 * Caller needs to hold orig->mcast_handler_lock.
1819 */
1820static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1821 struct batadv_orig_node *orig,
1822 u8 mcast_flags)
1823{
1824 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1825 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1826
1827 lockdep_assert_held(&orig->mcast_handler_lock);
1828
1829 /* switched from flag unset to set */
1830 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1831 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1832 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1833
1834 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1835 /* flag checks above + mcast_handler_lock prevents this */
1836 WARN_ON(!hlist_unhashed(node));
1837
1838 hlist_add_head_rcu(node, head);
1839 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1840 /* switched from flag set to unset */
1841 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1842 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1843 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1844
1845 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1846 /* flag checks above + mcast_handler_lock prevents this */
1847 WARN_ON(hlist_unhashed(node));
1848
1849 hlist_del_init_rcu(node);
1850 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1851 }
1852}
1853
1854/**
1855 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1856 * @bat_priv: the bat priv with all the soft interface information
1857 * @orig: the orig_node which multicast state might have changed of
1858 * @mcast_flags: flags indicating the new multicast state
1859 *
1860 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1861 * toggled then this method updates counter and list accordingly.
1862 *
1863 * Caller needs to hold orig->mcast_handler_lock.
1864 */
1865static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1866 struct batadv_orig_node *orig,
1867 u8 mcast_flags)
1868{
1869 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1870 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1871
1872 lockdep_assert_held(&orig->mcast_handler_lock);
1873
1874 /* switched from flag unset to set */
1875 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1876 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1877 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1878
1879 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1880 /* flag checks above + mcast_handler_lock prevents this */
1881 WARN_ON(!hlist_unhashed(node));
1882
1883 hlist_add_head_rcu(node, head);
1884 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1885 /* switched from flag set to unset */
1886 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1887 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1888 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1889
1890 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1891 /* flag checks above + mcast_handler_lock prevents this */
1892 WARN_ON(hlist_unhashed(node));
1893
1894 hlist_del_init_rcu(node);
1895 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1896 }
1897}
1898
1899/**
David Brazdil0f672f62019-12-10 10:32:29 +00001900 * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
1901 * @bat_priv: the bat priv with all the soft interface information
1902 * @orig: the orig_node which multicast state might have changed of
1903 * @mcast_flags: flags indicating the new multicast state
1904 *
1905 * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
1906 * toggled then this method updates counter and list accordingly.
1907 *
1908 * Caller needs to hold orig->mcast_handler_lock.
1909 */
1910static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
1911 struct batadv_orig_node *orig,
1912 u8 mcast_flags)
1913{
1914 struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
1915 struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
1916
1917 lockdep_assert_held(&orig->mcast_handler_lock);
1918
1919 /* switched from flag set to unset */
1920 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
1921 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
1922 atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
1923
1924 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1925 /* flag checks above + mcast_handler_lock prevents this */
1926 WARN_ON(!hlist_unhashed(node));
1927
1928 hlist_add_head_rcu(node, head);
1929 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1930 /* switched from flag unset to set */
1931 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
1932 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
1933 atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
1934
1935 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1936 /* flag checks above + mcast_handler_lock prevents this */
1937 WARN_ON(hlist_unhashed(node));
1938
1939 hlist_del_init_rcu(node);
1940 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1941 }
1942}
1943
1944/**
1945 * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
1946 * @bat_priv: the bat priv with all the soft interface information
1947 * @orig: the orig_node which multicast state might have changed of
1948 * @mcast_flags: flags indicating the new multicast state
1949 *
1950 * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
1951 * toggled then this method updates counter and list accordingly.
1952 *
1953 * Caller needs to hold orig->mcast_handler_lock.
1954 */
1955static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
1956 struct batadv_orig_node *orig,
1957 u8 mcast_flags)
1958{
1959 struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
1960 struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
1961
1962 lockdep_assert_held(&orig->mcast_handler_lock);
1963
1964 /* switched from flag set to unset */
1965 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
1966 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
1967 atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
1968
1969 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1970 /* flag checks above + mcast_handler_lock prevents this */
1971 WARN_ON(!hlist_unhashed(node));
1972
1973 hlist_add_head_rcu(node, head);
1974 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1975 /* switched from flag unset to set */
1976 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
1977 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
1978 atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
1979
1980 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1981 /* flag checks above + mcast_handler_lock prevents this */
1982 WARN_ON(hlist_unhashed(node));
1983
1984 hlist_del_init_rcu(node);
1985 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1986 }
1987}
1988
1989/**
1990 * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
1991 * @enabled: whether the originator has multicast TVLV support enabled
1992 * @tvlv_value: tvlv buffer containing the multicast flags
1993 * @tvlv_value_len: tvlv buffer length
1994 *
1995 * Return: multicast flags for the given tvlv buffer
1996 */
1997static u8
1998batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
1999{
2000 u8 mcast_flags = BATADV_NO_FLAGS;
2001
2002 if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
2003 mcast_flags = *(u8 *)tvlv_value;
2004
2005 if (!enabled) {
2006 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
2007 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
2008 }
2009
2010 /* remove redundant flags to avoid sending duplicate packets later */
2011 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
2012 mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
2013
2014 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
2015 mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
2016
2017 return mcast_flags;
2018}
2019
2020/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002021 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
2022 * @bat_priv: the bat priv with all the soft interface information
2023 * @orig: the orig_node of the ogm
2024 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2025 * @tvlv_value: tvlv buffer containing the multicast data
2026 * @tvlv_value_len: tvlv buffer length
2027 */
2028static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
2029 struct batadv_orig_node *orig,
2030 u8 flags,
2031 void *tvlv_value,
2032 u16 tvlv_value_len)
2033{
2034 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
David Brazdil0f672f62019-12-10 10:32:29 +00002035 u8 mcast_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002036
David Brazdil0f672f62019-12-10 10:32:29 +00002037 mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
2038 tvlv_value, tvlv_value_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002039
2040 spin_lock_bh(&orig->mcast_handler_lock);
2041
2042 if (orig_mcast_enabled &&
2043 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2044 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2045 } else if (!orig_mcast_enabled &&
2046 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
2047 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
2048 }
2049
2050 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
2051
2052 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
2053 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
2054 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
David Brazdil0f672f62019-12-10 10:32:29 +00002055 batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
2056 batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002057
2058 orig->mcast_flags = mcast_flags;
2059 spin_unlock_bh(&orig->mcast_handler_lock);
2060}
2061
2062/**
2063 * batadv_mcast_init() - initialize the multicast optimizations structures
2064 * @bat_priv: the bat priv with all the soft interface information
2065 */
2066void batadv_mcast_init(struct batadv_priv *bat_priv)
2067{
2068 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
2069 NULL, BATADV_TVLV_MCAST, 2,
2070 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
2071
2072 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
2073 batadv_mcast_start_timer(bat_priv);
2074}
2075
2076#ifdef CONFIG_BATMAN_ADV_DEBUGFS
2077/**
2078 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
2079 * @bat_priv: the bat priv with all the soft interface information
2080 * @seq: debugfs table seq_file struct
2081 *
2082 * Prints our own multicast flags including a more specific reason why
2083 * they are set, that is prints the bridge and querier state too, to
2084 * the debugfs table specified via @seq.
2085 */
2086static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
2087 struct seq_file *seq)
2088{
David Brazdil0f672f62019-12-10 10:32:29 +00002089 struct batadv_mcast_mla_flags *mla_flags = &bat_priv->mcast.mla_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002090 char querier4, querier6, shadowing4, shadowing6;
David Brazdil0f672f62019-12-10 10:32:29 +00002091 bool bridged = mla_flags->bridged;
2092 u8 flags = mla_flags->tvlv_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002093
2094 if (bridged) {
David Brazdil0f672f62019-12-10 10:32:29 +00002095 querier4 = mla_flags->querier_ipv4.exists ? '.' : '4';
2096 querier6 = mla_flags->querier_ipv6.exists ? '.' : '6';
2097 shadowing4 = mla_flags->querier_ipv4.shadowing ? '4' : '.';
2098 shadowing6 = mla_flags->querier_ipv6.shadowing ? '6' : '.';
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002099 } else {
2100 querier4 = '?';
2101 querier6 = '?';
2102 shadowing4 = '?';
2103 shadowing6 = '?';
2104 }
2105
David Brazdil0f672f62019-12-10 10:32:29 +00002106 seq_printf(seq, "Multicast flags (own flags: [%c%c%c%s%s])\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002107 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
2108 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
David Brazdil0f672f62019-12-10 10:32:29 +00002109 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
2110 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
2111 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002112 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
2113 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
2114 querier4, querier6);
2115 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
2116 shadowing4, shadowing6);
2117 seq_puts(seq, "-------------------------------------------\n");
2118 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
2119}
2120
2121/**
2122 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
2123 * @seq: seq file to print on
2124 * @offset: not used
2125 *
2126 * This prints a table of (primary) originators and their according
2127 * multicast flags, including (in the header) our own.
2128 *
2129 * Return: always 0
2130 */
2131int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
2132{
2133 struct net_device *net_dev = (struct net_device *)seq->private;
2134 struct batadv_priv *bat_priv = netdev_priv(net_dev);
2135 struct batadv_hard_iface *primary_if;
2136 struct batadv_hashtable *hash = bat_priv->orig_hash;
2137 struct batadv_orig_node *orig_node;
2138 struct hlist_head *head;
2139 u8 flags;
2140 u32 i;
2141
2142 primary_if = batadv_seq_print_text_primary_if_get(seq);
2143 if (!primary_if)
2144 return 0;
2145
2146 batadv_mcast_flags_print_header(bat_priv, seq);
2147
2148 for (i = 0; i < hash->size; i++) {
2149 head = &hash->table[i];
2150
2151 rcu_read_lock();
2152 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2153 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2154 &orig_node->capa_initialized))
2155 continue;
2156
2157 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2158 &orig_node->capabilities)) {
2159 seq_printf(seq, "%pM -\n", orig_node->orig);
2160 continue;
2161 }
2162
2163 flags = orig_node->mcast_flags;
2164
David Brazdil0f672f62019-12-10 10:32:29 +00002165 seq_printf(seq, "%pM [%c%c%c%s%s]\n", orig_node->orig,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002166 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
2167 ? 'U' : '.',
2168 (flags & BATADV_MCAST_WANT_ALL_IPV4)
2169 ? '4' : '.',
2170 (flags & BATADV_MCAST_WANT_ALL_IPV6)
David Brazdil0f672f62019-12-10 10:32:29 +00002171 ? '6' : '.',
2172 !(flags & BATADV_MCAST_WANT_NO_RTR4)
2173 ? "R4" : ". ",
2174 !(flags & BATADV_MCAST_WANT_NO_RTR6)
2175 ? "R6" : ". ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176 }
2177 rcu_read_unlock();
2178 }
2179
2180 batadv_hardif_put(primary_if);
2181
2182 return 0;
2183}
2184#endif
2185
2186/**
2187 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
2188 * @msg: buffer for the message
2189 * @bat_priv: the bat priv with all the soft interface information
2190 *
2191 * Return: 0 or error code.
2192 */
2193int batadv_mcast_mesh_info_put(struct sk_buff *msg,
2194 struct batadv_priv *bat_priv)
2195{
David Brazdil0f672f62019-12-10 10:32:29 +00002196 u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002197 u32 flags_priv = BATADV_NO_FLAGS;
2198
David Brazdil0f672f62019-12-10 10:32:29 +00002199 if (bat_priv->mcast.mla_flags.bridged) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002200 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
2201
David Brazdil0f672f62019-12-10 10:32:29 +00002202 if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002203 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
David Brazdil0f672f62019-12-10 10:32:29 +00002204 if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002205 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
David Brazdil0f672f62019-12-10 10:32:29 +00002206 if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002207 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
David Brazdil0f672f62019-12-10 10:32:29 +00002208 if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002209 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
2210 }
2211
2212 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
2213 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
2214 return -EMSGSIZE;
2215
2216 return 0;
2217}
2218
2219/**
2220 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
2221 * to a netlink socket
2222 * @msg: buffer for the message
2223 * @portid: netlink port
David Brazdil0f672f62019-12-10 10:32:29 +00002224 * @cb: Control block containing additional options
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002225 * @orig_node: originator to dump the multicast flags of
2226 *
2227 * Return: 0 or error code.
2228 */
2229static int
David Brazdil0f672f62019-12-10 10:32:29 +00002230batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
2231 struct netlink_callback *cb,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002232 struct batadv_orig_node *orig_node)
2233{
2234 void *hdr;
2235
David Brazdil0f672f62019-12-10 10:32:29 +00002236 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2237 &batadv_netlink_family, NLM_F_MULTI,
2238 BATADV_CMD_GET_MCAST_FLAGS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002239 if (!hdr)
2240 return -ENOBUFS;
2241
David Brazdil0f672f62019-12-10 10:32:29 +00002242 genl_dump_check_consistent(cb, hdr);
2243
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002244 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2245 orig_node->orig)) {
2246 genlmsg_cancel(msg, hdr);
2247 return -EMSGSIZE;
2248 }
2249
2250 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2251 &orig_node->capabilities)) {
2252 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
2253 orig_node->mcast_flags)) {
2254 genlmsg_cancel(msg, hdr);
2255 return -EMSGSIZE;
2256 }
2257 }
2258
2259 genlmsg_end(msg, hdr);
2260 return 0;
2261}
2262
2263/**
2264 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
2265 * table to a netlink socket
2266 * @msg: buffer for the message
2267 * @portid: netlink port
David Brazdil0f672f62019-12-10 10:32:29 +00002268 * @cb: Control block containing additional options
2269 * @hash: hash to dump
2270 * @bucket: bucket index to dump
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002271 * @idx_skip: How many entries to skip
2272 *
2273 * Return: 0 or error code.
2274 */
2275static int
David Brazdil0f672f62019-12-10 10:32:29 +00002276batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
2277 struct netlink_callback *cb,
2278 struct batadv_hashtable *hash,
2279 unsigned int bucket, long *idx_skip)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002280{
2281 struct batadv_orig_node *orig_node;
2282 long idx = 0;
2283
David Brazdil0f672f62019-12-10 10:32:29 +00002284 spin_lock_bh(&hash->list_locks[bucket]);
2285 cb->seq = atomic_read(&hash->generation) << 1 | 1;
2286
2287 hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002288 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2289 &orig_node->capa_initialized))
2290 continue;
2291
2292 if (idx < *idx_skip)
2293 goto skip;
2294
David Brazdil0f672f62019-12-10 10:32:29 +00002295 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
2296 spin_unlock_bh(&hash->list_locks[bucket]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002297 *idx_skip = idx;
2298
2299 return -EMSGSIZE;
2300 }
2301
2302skip:
2303 idx++;
2304 }
David Brazdil0f672f62019-12-10 10:32:29 +00002305 spin_unlock_bh(&hash->list_locks[bucket]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002306
2307 return 0;
2308}
2309
2310/**
2311 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2312 * @msg: buffer for the message
2313 * @portid: netlink port
David Brazdil0f672f62019-12-10 10:32:29 +00002314 * @cb: Control block containing additional options
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002315 * @bat_priv: the bat priv with all the soft interface information
2316 * @bucket: current bucket to dump
2317 * @idx: index in current bucket to the next entry to dump
2318 *
2319 * Return: 0 or error code.
2320 */
2321static int
David Brazdil0f672f62019-12-10 10:32:29 +00002322__batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
2323 struct netlink_callback *cb,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002324 struct batadv_priv *bat_priv, long *bucket, long *idx)
2325{
2326 struct batadv_hashtable *hash = bat_priv->orig_hash;
2327 long bucket_tmp = *bucket;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002328 long idx_tmp = *idx;
2329
2330 while (bucket_tmp < hash->size) {
David Brazdil0f672f62019-12-10 10:32:29 +00002331 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
2332 bucket_tmp, &idx_tmp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002333 break;
2334
2335 bucket_tmp++;
2336 idx_tmp = 0;
2337 }
2338
2339 *bucket = bucket_tmp;
2340 *idx = idx_tmp;
2341
2342 return msg->len;
2343}
2344
2345/**
2346 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
2347 * callback
2348 * @cb: netlink callback structure
2349 * @primary_if: the primary interface pointer to return the result in
2350 *
2351 * Return: 0 or error code.
2352 */
2353static int
2354batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
2355 struct batadv_hard_iface **primary_if)
2356{
2357 struct batadv_hard_iface *hard_iface = NULL;
2358 struct net *net = sock_net(cb->skb->sk);
2359 struct net_device *soft_iface;
2360 struct batadv_priv *bat_priv;
2361 int ifindex;
2362 int ret = 0;
2363
2364 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2365 if (!ifindex)
2366 return -EINVAL;
2367
2368 soft_iface = dev_get_by_index(net, ifindex);
2369 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2370 ret = -ENODEV;
2371 goto out;
2372 }
2373
2374 bat_priv = netdev_priv(soft_iface);
2375
2376 hard_iface = batadv_primary_if_get_selected(bat_priv);
2377 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
2378 ret = -ENOENT;
2379 goto out;
2380 }
2381
2382out:
2383 if (soft_iface)
2384 dev_put(soft_iface);
2385
2386 if (!ret && primary_if)
2387 *primary_if = hard_iface;
2388 else if (hard_iface)
2389 batadv_hardif_put(hard_iface);
2390
2391 return ret;
2392}
2393
2394/**
2395 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2396 * @msg: buffer for the message
2397 * @cb: callback structure containing arguments
2398 *
2399 * Return: message length.
2400 */
2401int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
2402{
2403 struct batadv_hard_iface *primary_if = NULL;
2404 int portid = NETLINK_CB(cb->skb).portid;
2405 struct batadv_priv *bat_priv;
2406 long *bucket = &cb->args[0];
2407 long *idx = &cb->args[1];
2408 int ret;
2409
2410 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
2411 if (ret)
2412 return ret;
2413
2414 bat_priv = netdev_priv(primary_if->soft_iface);
David Brazdil0f672f62019-12-10 10:32:29 +00002415 ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002416
2417 batadv_hardif_put(primary_if);
2418 return ret;
2419}
2420
2421/**
2422 * batadv_mcast_free() - free the multicast optimizations structures
2423 * @bat_priv: the bat priv with all the soft interface information
2424 */
2425void batadv_mcast_free(struct batadv_priv *bat_priv)
2426{
2427 cancel_delayed_work_sync(&bat_priv->mcast.work);
2428
2429 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2430 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2431
2432 /* safely calling outside of worker, as worker was canceled above */
2433 batadv_mcast_mla_tt_retract(bat_priv, NULL);
2434}
2435
2436/**
2437 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
2438 * @orig: the originator which is going to get purged
2439 */
2440void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
2441{
2442 struct batadv_priv *bat_priv = orig->bat_priv;
2443
2444 spin_lock_bh(&orig->mcast_handler_lock);
2445
2446 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
2447 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
2448 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
David Brazdil0f672f62019-12-10 10:32:29 +00002449 batadv_mcast_want_rtr4_update(bat_priv, orig,
2450 BATADV_MCAST_WANT_NO_RTR4);
2451 batadv_mcast_want_rtr6_update(bat_priv, orig,
2452 BATADV_MCAST_WANT_NO_RTR6);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002453
2454 spin_unlock_bh(&orig->mcast_handler_lock);
2455}