Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/net/batman-adv/multicast.c
26278 views
1
// SPDX-License-Identifier: GPL-2.0
2
/* Copyright (C) B.A.T.M.A.N. contributors:
3
*
4
* Linus Lüssing
5
*/
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/container_of.h>
15
#include <linux/err.h>
16
#include <linux/errno.h>
17
#include <linux/etherdevice.h>
18
#include <linux/gfp.h>
19
#include <linux/icmpv6.h>
20
#include <linux/if_bridge.h>
21
#include <linux/if_ether.h>
22
#include <linux/igmp.h>
23
#include <linux/in.h>
24
#include <linux/in6.h>
25
#include <linux/inetdevice.h>
26
#include <linux/ip.h>
27
#include <linux/ipv6.h>
28
#include <linux/jiffies.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/skbuff.h>
37
#include <linux/slab.h>
38
#include <linux/spinlock.h>
39
#include <linux/sprintf.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 <uapi/linux/batadv_packet.h>
51
#include <uapi/linux/batman_adv.h>
52
53
#include "bridge_loop_avoidance.h"
54
#include "hard-interface.h"
55
#include "hash.h"
56
#include "log.h"
57
#include "netlink.h"
58
#include "send.h"
59
#include "translation-table.h"
60
#include "tvlv.h"
61
62
static void batadv_mcast_mla_update(struct work_struct *work);
63
64
/**
65
* batadv_mcast_start_timer() - schedule the multicast periodic worker
66
* @bat_priv: the bat priv with all the mesh interface information
67
*/
68
static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
69
{
70
queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
71
msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
72
}
73
74
/**
75
* batadv_mcast_get_bridge() - get the bridge on top of the meshif if it exists
76
* @mesh_iface: netdev struct of the mesh interface
77
*
78
* If the given mesh interface has a bridge on top then the refcount
79
* of the according net device is increased.
80
*
81
* Return: NULL if no such bridge exists. Otherwise the net device of the
82
* bridge.
83
*/
84
static struct net_device *batadv_mcast_get_bridge(struct net_device *mesh_iface)
85
{
86
struct net_device *upper = mesh_iface;
87
88
rcu_read_lock();
89
do {
90
upper = netdev_master_upper_dev_get_rcu(upper);
91
} while (upper && !netif_is_bridge_master(upper));
92
93
dev_hold(upper);
94
rcu_read_unlock();
95
96
return upper;
97
}
98
99
/**
100
* batadv_mcast_mla_rtr_flags_meshif_get_ipv4() - get mcast router flags from
101
* node for IPv4
102
* @dev: the interface to check
103
*
104
* Checks the presence of an IPv4 multicast router on this node.
105
*
106
* Caller needs to hold rcu read lock.
107
*
108
* Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
109
*/
110
static u8 batadv_mcast_mla_rtr_flags_meshif_get_ipv4(struct net_device *dev)
111
{
112
struct in_device *in_dev = __in_dev_get_rcu(dev);
113
114
if (in_dev && IN_DEV_MFORWARD(in_dev))
115
return BATADV_NO_FLAGS;
116
else
117
return BATADV_MCAST_WANT_NO_RTR4;
118
}
119
120
/**
121
* batadv_mcast_mla_rtr_flags_meshif_get_ipv6() - get mcast router flags from
122
* node for IPv6
123
* @dev: the interface to check
124
*
125
* Checks the presence of an IPv6 multicast router on this node.
126
*
127
* Caller needs to hold rcu read lock.
128
*
129
* Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
130
*/
131
#if IS_ENABLED(CONFIG_IPV6_MROUTE)
132
static u8 batadv_mcast_mla_rtr_flags_meshif_get_ipv6(struct net_device *dev)
133
{
134
struct inet6_dev *in6_dev = __in6_dev_get(dev);
135
136
if (in6_dev && atomic_read(&in6_dev->cnf.mc_forwarding))
137
return BATADV_NO_FLAGS;
138
else
139
return BATADV_MCAST_WANT_NO_RTR6;
140
}
141
#else
142
static inline u8
143
batadv_mcast_mla_rtr_flags_meshif_get_ipv6(struct net_device *dev)
144
{
145
return BATADV_MCAST_WANT_NO_RTR6;
146
}
147
#endif
148
149
/**
150
* batadv_mcast_mla_rtr_flags_meshif_get() - get mcast router flags from node
151
* @bat_priv: the bat priv with all the mesh interface information
152
* @bridge: bridge interface on top of the mesh_iface if present,
153
* otherwise pass NULL
154
*
155
* Checks the presence of IPv4 and IPv6 multicast routers on this
156
* node.
157
*
158
* Return:
159
* BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
160
* BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
161
* BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
162
* The former two OR'd: no multicast router is present
163
*/
164
static u8 batadv_mcast_mla_rtr_flags_meshif_get(struct batadv_priv *bat_priv,
165
struct net_device *bridge)
166
{
167
struct net_device *dev = bridge ? bridge : bat_priv->mesh_iface;
168
u8 flags = BATADV_NO_FLAGS;
169
170
rcu_read_lock();
171
172
flags |= batadv_mcast_mla_rtr_flags_meshif_get_ipv4(dev);
173
flags |= batadv_mcast_mla_rtr_flags_meshif_get_ipv6(dev);
174
175
rcu_read_unlock();
176
177
return flags;
178
}
179
180
/**
181
* batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge
182
* @bat_priv: the bat priv with all the mesh interface information
183
* @bridge: bridge interface on top of the mesh_iface if present,
184
* otherwise pass NULL
185
*
186
* Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
187
*
188
* Return:
189
* BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
190
* BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
191
* BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
192
* The former two OR'd: no multicast router is present
193
*/
194
static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
195
struct net_device *bridge)
196
{
197
struct net_device *dev = bat_priv->mesh_iface;
198
u8 flags = BATADV_NO_FLAGS;
199
200
if (!bridge)
201
return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
202
203
if (!br_multicast_has_router_adjacent(dev, ETH_P_IP))
204
flags |= BATADV_MCAST_WANT_NO_RTR4;
205
if (!br_multicast_has_router_adjacent(dev, ETH_P_IPV6))
206
flags |= BATADV_MCAST_WANT_NO_RTR6;
207
208
return flags;
209
}
210
211
/**
212
* batadv_mcast_mla_rtr_flags_get() - get multicast router flags
213
* @bat_priv: the bat priv with all the mesh interface information
214
* @bridge: bridge interface on top of the mesh_iface if present,
215
* otherwise pass NULL
216
*
217
* Checks the presence of IPv4 and IPv6 multicast routers on this
218
* node or behind its bridge.
219
*
220
* Return:
221
* BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
222
* BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
223
* BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
224
* The former two OR'd: no multicast router is present
225
*/
226
static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
227
struct net_device *bridge)
228
{
229
u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
230
231
flags &= batadv_mcast_mla_rtr_flags_meshif_get(bat_priv, bridge);
232
flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
233
234
return flags;
235
}
236
237
/**
238
* batadv_mcast_mla_forw_flags_get() - get multicast forwarding flags
239
* @bat_priv: the bat priv with all the mesh interface information
240
*
241
* Checks if all active hard interfaces have an MTU larger or equal to 1280
242
* bytes (IPv6 minimum MTU).
243
*
244
* Return: BATADV_MCAST_HAVE_MC_PTYPE_CAPA if yes, BATADV_NO_FLAGS otherwise.
245
*/
246
static u8 batadv_mcast_mla_forw_flags_get(struct batadv_priv *bat_priv)
247
{
248
const struct batadv_hard_iface *hard_iface;
249
struct list_head *iter;
250
251
rcu_read_lock();
252
netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
253
if (hard_iface->if_status != BATADV_IF_ACTIVE)
254
continue;
255
256
if (hard_iface->net_dev->mtu < IPV6_MIN_MTU) {
257
rcu_read_unlock();
258
return BATADV_NO_FLAGS;
259
}
260
}
261
rcu_read_unlock();
262
263
return BATADV_MCAST_HAVE_MC_PTYPE_CAPA;
264
}
265
266
/**
267
* batadv_mcast_mla_flags_get() - get the new multicast flags
268
* @bat_priv: the bat priv with all the mesh interface information
269
*
270
* Return: A set of flags for the current/next TVLV, querier and
271
* bridge state.
272
*/
273
static struct batadv_mcast_mla_flags
274
batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
275
{
276
struct net_device *dev = bat_priv->mesh_iface;
277
struct batadv_mcast_querier_state *qr4, *qr6;
278
struct batadv_mcast_mla_flags mla_flags;
279
struct net_device *bridge;
280
281
bridge = batadv_mcast_get_bridge(dev);
282
283
memset(&mla_flags, 0, sizeof(mla_flags));
284
mla_flags.enabled = 1;
285
mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv,
286
bridge);
287
mla_flags.tvlv_flags |= batadv_mcast_mla_forw_flags_get(bat_priv);
288
289
if (!bridge)
290
return mla_flags;
291
292
dev_put(bridge);
293
294
mla_flags.bridged = 1;
295
qr4 = &mla_flags.querier_ipv4;
296
qr6 = &mla_flags.querier_ipv6;
297
298
if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
299
pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
300
301
qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
302
qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
303
304
qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
305
qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
306
307
mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
308
309
/* 1) If no querier exists at all, then multicast listeners on
310
* our local TT clients behind the bridge will keep silent.
311
* 2) If the selected querier is on one of our local TT clients,
312
* behind the bridge, then this querier might shadow multicast
313
* listeners on our local TT clients, behind this bridge.
314
*
315
* In both cases, we will signalize other batman nodes that
316
* we need all multicast traffic of the according protocol.
317
*/
318
if (!qr4->exists || qr4->shadowing) {
319
mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4;
320
mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4;
321
}
322
323
if (!qr6->exists || qr6->shadowing) {
324
mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6;
325
mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6;
326
}
327
328
return mla_flags;
329
}
330
331
/**
332
* batadv_mcast_mla_is_duplicate() - check whether an address is in a list
333
* @mcast_addr: the multicast address to check
334
* @mcast_list: the list with multicast addresses to search in
335
*
336
* Return: true if the given address is already in the given list.
337
* Otherwise returns false.
338
*/
339
static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
340
struct hlist_head *mcast_list)
341
{
342
struct batadv_hw_addr *mcast_entry;
343
344
hlist_for_each_entry(mcast_entry, mcast_list, list)
345
if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
346
return true;
347
348
return false;
349
}
350
351
/**
352
* batadv_mcast_mla_meshif_get_ipv4() - get meshif IPv4 multicast listeners
353
* @dev: the device to collect multicast addresses from
354
* @mcast_list: a list to put found addresses into
355
* @flags: flags indicating the new multicast state
356
*
357
* Collects multicast addresses of IPv4 multicast listeners residing
358
* on this kernel on the given mesh interface, dev, in
359
* the given mcast_list. In general, multicast listeners provided by
360
* your multicast receiving applications run directly on this node.
361
*
362
* Return: -ENOMEM on memory allocation error or the number of
363
* items added to the mcast_list otherwise.
364
*/
365
static int
366
batadv_mcast_mla_meshif_get_ipv4(struct net_device *dev,
367
struct hlist_head *mcast_list,
368
struct batadv_mcast_mla_flags *flags)
369
{
370
struct batadv_hw_addr *new;
371
struct in_device *in_dev;
372
u8 mcast_addr[ETH_ALEN];
373
struct ip_mc_list *pmc;
374
int ret = 0;
375
376
if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
377
return 0;
378
379
rcu_read_lock();
380
381
in_dev = __in_dev_get_rcu(dev);
382
if (!in_dev) {
383
rcu_read_unlock();
384
return 0;
385
}
386
387
for (pmc = rcu_dereference(in_dev->mc_list); pmc;
388
pmc = rcu_dereference(pmc->next_rcu)) {
389
if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
390
ipv4_is_local_multicast(pmc->multiaddr))
391
continue;
392
393
if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
394
!ipv4_is_local_multicast(pmc->multiaddr))
395
continue;
396
397
ip_eth_mc_map(pmc->multiaddr, mcast_addr);
398
399
if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
400
continue;
401
402
new = kmalloc(sizeof(*new), GFP_ATOMIC);
403
if (!new) {
404
ret = -ENOMEM;
405
break;
406
}
407
408
ether_addr_copy(new->addr, mcast_addr);
409
hlist_add_head(&new->list, mcast_list);
410
ret++;
411
}
412
rcu_read_unlock();
413
414
return ret;
415
}
416
417
/**
418
* batadv_mcast_mla_meshif_get_ipv6() - get meshif IPv6 multicast listeners
419
* @dev: the device to collect multicast addresses from
420
* @mcast_list: a list to put found addresses into
421
* @flags: flags indicating the new multicast state
422
*
423
* Collects multicast addresses of IPv6 multicast listeners residing
424
* on this kernel on the given mesh interface, dev, in
425
* the given mcast_list. In general, multicast listeners provided by
426
* your multicast receiving applications run directly on this node.
427
*
428
* Return: -ENOMEM on memory allocation error or the number of
429
* items added to the mcast_list otherwise.
430
*/
431
#if IS_ENABLED(CONFIG_IPV6)
432
static int
433
batadv_mcast_mla_meshif_get_ipv6(struct net_device *dev,
434
struct hlist_head *mcast_list,
435
struct batadv_mcast_mla_flags *flags)
436
{
437
struct batadv_hw_addr *new;
438
struct inet6_dev *in6_dev;
439
u8 mcast_addr[ETH_ALEN];
440
struct ifmcaddr6 *pmc6;
441
int ret = 0;
442
443
if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
444
return 0;
445
446
rcu_read_lock();
447
448
in6_dev = __in6_dev_get(dev);
449
if (!in6_dev) {
450
rcu_read_unlock();
451
return 0;
452
}
453
454
for (pmc6 = rcu_dereference(in6_dev->mc_list);
455
pmc6;
456
pmc6 = rcu_dereference(pmc6->next)) {
457
if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) <
458
IPV6_ADDR_SCOPE_LINKLOCAL)
459
continue;
460
461
if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
462
ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
463
continue;
464
465
if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
466
IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) >
467
IPV6_ADDR_SCOPE_LINKLOCAL)
468
continue;
469
470
ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
471
472
if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
473
continue;
474
475
new = kmalloc(sizeof(*new), GFP_ATOMIC);
476
if (!new) {
477
ret = -ENOMEM;
478
break;
479
}
480
481
ether_addr_copy(new->addr, mcast_addr);
482
hlist_add_head(&new->list, mcast_list);
483
ret++;
484
}
485
rcu_read_unlock();
486
487
return ret;
488
}
489
#else
490
static inline int
491
batadv_mcast_mla_meshif_get_ipv6(struct net_device *dev,
492
struct hlist_head *mcast_list,
493
struct batadv_mcast_mla_flags *flags)
494
{
495
return 0;
496
}
497
#endif
498
499
/**
500
* batadv_mcast_mla_meshif_get() - get meshif multicast listeners
501
* @dev: the device to collect multicast addresses from
502
* @mcast_list: a list to put found addresses into
503
* @flags: flags indicating the new multicast state
504
*
505
* Collects multicast addresses of multicast listeners residing
506
* on this kernel on the given mesh interface, dev, in
507
* the given mcast_list. In general, multicast listeners provided by
508
* your multicast receiving applications run directly on this node.
509
*
510
* If there is a bridge interface on top of dev, collect from that one
511
* instead. Just like with IP addresses and routes, multicast listeners
512
* will(/should) register to the bridge interface instead of an
513
* enslaved bat0.
514
*
515
* Return: -ENOMEM on memory allocation error or the number of
516
* items added to the mcast_list otherwise.
517
*/
518
static int
519
batadv_mcast_mla_meshif_get(struct net_device *dev,
520
struct hlist_head *mcast_list,
521
struct batadv_mcast_mla_flags *flags)
522
{
523
struct net_device *bridge = batadv_mcast_get_bridge(dev);
524
int ret4, ret6 = 0;
525
526
if (bridge)
527
dev = bridge;
528
529
ret4 = batadv_mcast_mla_meshif_get_ipv4(dev, mcast_list, flags);
530
if (ret4 < 0)
531
goto out;
532
533
ret6 = batadv_mcast_mla_meshif_get_ipv6(dev, mcast_list, flags);
534
if (ret6 < 0) {
535
ret4 = 0;
536
goto out;
537
}
538
539
out:
540
dev_put(bridge);
541
542
return ret4 + ret6;
543
}
544
545
/**
546
* batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
547
* @dst: destination to write to - a multicast MAC address
548
* @src: source to read from - a multicast IP address
549
*
550
* Converts a given multicast IPv4/IPv6 address from a bridge
551
* to its matching multicast MAC address and copies it into the given
552
* destination buffer.
553
*
554
* Caller needs to make sure the destination buffer can hold
555
* at least ETH_ALEN bytes.
556
*/
557
static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
558
{
559
if (src->proto == htons(ETH_P_IP))
560
ip_eth_mc_map(src->dst.ip4, dst);
561
#if IS_ENABLED(CONFIG_IPV6)
562
else if (src->proto == htons(ETH_P_IPV6))
563
ipv6_eth_mc_map(&src->dst.ip6, dst);
564
#endif
565
else
566
eth_zero_addr(dst);
567
}
568
569
/**
570
* batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
571
* @dev: a bridge slave whose bridge to collect multicast addresses from
572
* @mcast_list: a list to put found addresses into
573
* @flags: flags indicating the new multicast state
574
*
575
* Collects multicast addresses of multicast listeners residing
576
* on foreign, non-mesh devices which we gave access to our mesh via
577
* a bridge on top of the given mesh interface, dev, in the given
578
* mcast_list.
579
*
580
* Return: -ENOMEM on memory allocation error or the number of
581
* items added to the mcast_list otherwise.
582
*/
583
static int batadv_mcast_mla_bridge_get(struct net_device *dev,
584
struct hlist_head *mcast_list,
585
struct batadv_mcast_mla_flags *flags)
586
{
587
struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
588
struct br_ip_list *br_ip_entry, *tmp;
589
u8 tvlv_flags = flags->tvlv_flags;
590
struct batadv_hw_addr *new;
591
u8 mcast_addr[ETH_ALEN];
592
int ret;
593
594
/* we don't need to detect these devices/listeners, the IGMP/MLD
595
* snooping code of the Linux bridge already does that for us
596
*/
597
ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
598
if (ret < 0)
599
goto out;
600
601
list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
602
if (br_ip_entry->addr.proto == htons(ETH_P_IP)) {
603
if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
604
continue;
605
606
if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
607
ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
608
continue;
609
610
if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
611
!ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
612
continue;
613
}
614
615
#if IS_ENABLED(CONFIG_IPV6)
616
if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) {
617
if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
618
continue;
619
620
if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
621
ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.dst.ip6))
622
continue;
623
624
if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
625
IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.dst.ip6) >
626
IPV6_ADDR_SCOPE_LINKLOCAL)
627
continue;
628
}
629
#endif
630
631
batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
632
if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
633
continue;
634
635
new = kmalloc(sizeof(*new), GFP_ATOMIC);
636
if (!new) {
637
ret = -ENOMEM;
638
break;
639
}
640
641
ether_addr_copy(new->addr, mcast_addr);
642
hlist_add_head(&new->list, mcast_list);
643
}
644
645
out:
646
list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
647
list_del(&br_ip_entry->list);
648
kfree(br_ip_entry);
649
}
650
651
return ret;
652
}
653
654
/**
655
* batadv_mcast_mla_list_free() - free a list of multicast addresses
656
* @mcast_list: the list to free
657
*
658
* Removes and frees all items in the given mcast_list.
659
*/
660
static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
661
{
662
struct batadv_hw_addr *mcast_entry;
663
struct hlist_node *tmp;
664
665
hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
666
hlist_del(&mcast_entry->list);
667
kfree(mcast_entry);
668
}
669
}
670
671
/**
672
* batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
673
* @bat_priv: the bat priv with all the mesh interface information
674
* @mcast_list: a list of addresses which should _not_ be removed
675
*
676
* Retracts the announcement of any multicast listener from the
677
* translation table except the ones listed in the given mcast_list.
678
*
679
* If mcast_list is NULL then all are retracted.
680
*/
681
static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
682
struct hlist_head *mcast_list)
683
{
684
struct batadv_hw_addr *mcast_entry;
685
struct hlist_node *tmp;
686
687
hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
688
list) {
689
if (mcast_list &&
690
batadv_mcast_mla_is_duplicate(mcast_entry->addr,
691
mcast_list))
692
continue;
693
694
batadv_tt_local_remove(bat_priv, mcast_entry->addr,
695
BATADV_NO_FLAGS,
696
"mcast TT outdated", false);
697
698
hlist_del(&mcast_entry->list);
699
kfree(mcast_entry);
700
}
701
}
702
703
/**
704
* batadv_mcast_mla_tt_add() - add multicast listener announcements
705
* @bat_priv: the bat priv with all the mesh interface information
706
* @mcast_list: a list of addresses which are going to get added
707
*
708
* Adds multicast listener announcements from the given mcast_list to the
709
* translation table if they have not been added yet.
710
*/
711
static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
712
struct hlist_head *mcast_list)
713
{
714
struct batadv_hw_addr *mcast_entry;
715
struct hlist_node *tmp;
716
717
if (!mcast_list)
718
return;
719
720
hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
721
if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
722
&bat_priv->mcast.mla_list))
723
continue;
724
725
if (!batadv_tt_local_add(bat_priv->mesh_iface,
726
mcast_entry->addr, BATADV_NO_FLAGS,
727
BATADV_NULL_IFINDEX, BATADV_NO_MARK))
728
continue;
729
730
hlist_del(&mcast_entry->list);
731
hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
732
}
733
}
734
735
/**
736
* batadv_mcast_querier_log() - debug output regarding the querier status on
737
* link
738
* @bat_priv: the bat priv with all the mesh interface information
739
* @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
740
* @old_state: the previous querier state on our link
741
* @new_state: the new querier state on our link
742
*
743
* Outputs debug messages to the logging facility with log level 'mcast'
744
* regarding changes to the querier status on the link which are relevant
745
* to our multicast optimizations.
746
*
747
* Usually this is about whether a querier appeared or vanished in
748
* our mesh or whether the querier is in the suboptimal position of being
749
* behind our local bridge segment: Snooping switches will directly
750
* forward listener reports to the querier, therefore batman-adv and
751
* the bridge will potentially not see these listeners - the querier is
752
* potentially shadowing listeners from us then.
753
*
754
* This is only interesting for nodes with a bridge on top of their
755
* mesh interface.
756
*/
757
static void
758
batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
759
struct batadv_mcast_querier_state *old_state,
760
struct batadv_mcast_querier_state *new_state)
761
{
762
if (!old_state->exists && new_state->exists)
763
batadv_info(bat_priv->mesh_iface, "%s Querier appeared\n",
764
str_proto);
765
else if (old_state->exists && !new_state->exists)
766
batadv_info(bat_priv->mesh_iface,
767
"%s Querier disappeared - multicast optimizations disabled\n",
768
str_proto);
769
else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists)
770
batadv_info(bat_priv->mesh_iface,
771
"No %s Querier present - multicast optimizations disabled\n",
772
str_proto);
773
774
if (new_state->exists) {
775
if ((!old_state->shadowing && new_state->shadowing) ||
776
(!old_state->exists && new_state->shadowing))
777
batadv_dbg(BATADV_DBG_MCAST, bat_priv,
778
"%s Querier is behind our bridged segment: Might shadow listeners\n",
779
str_proto);
780
else if (old_state->shadowing && !new_state->shadowing)
781
batadv_dbg(BATADV_DBG_MCAST, bat_priv,
782
"%s Querier is not behind our bridged segment\n",
783
str_proto);
784
}
785
}
786
787
/**
788
* batadv_mcast_bridge_log() - debug output for topology changes in bridged
789
* setups
790
* @bat_priv: the bat priv with all the mesh interface information
791
* @new_flags: flags indicating the new multicast state
792
*
793
* If no bridges are ever used on this node, then this function does nothing.
794
*
795
* Otherwise this function outputs debug information to the 'mcast' log level
796
* which might be relevant to our multicast optimizations.
797
*
798
* More precisely, it outputs information when a bridge interface is added or
799
* removed from a mesh interface. And when a bridge is present, it further
800
* outputs information about the querier state which is relevant for the
801
* multicast flags this node is going to set.
802
*/
803
static void
804
batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
805
struct batadv_mcast_mla_flags *new_flags)
806
{
807
struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
808
809
if (!old_flags->bridged && new_flags->bridged)
810
batadv_dbg(BATADV_DBG_MCAST, bat_priv,
811
"Bridge added: Setting Unsnoopables(U)-flag\n");
812
else if (old_flags->bridged && !new_flags->bridged)
813
batadv_dbg(BATADV_DBG_MCAST, bat_priv,
814
"Bridge removed: Unsetting Unsnoopables(U)-flag\n");
815
816
if (new_flags->bridged) {
817
batadv_mcast_querier_log(bat_priv, "IGMP",
818
&old_flags->querier_ipv4,
819
&new_flags->querier_ipv4);
820
batadv_mcast_querier_log(bat_priv, "MLD",
821
&old_flags->querier_ipv6,
822
&new_flags->querier_ipv6);
823
}
824
}
825
826
/**
827
* batadv_mcast_flags_log() - output debug information about mcast flag changes
828
* @bat_priv: the bat priv with all the mesh interface information
829
* @flags: TVLV flags indicating the new multicast state
830
*
831
* Whenever the multicast TVLV flags this node announces change, this function
832
* should be used to notify userspace about the change.
833
*/
834
static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
835
{
836
bool old_enabled = bat_priv->mcast.mla_flags.enabled;
837
u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags;
838
char str_old_flags[] = "[.... . .]";
839
840
sprintf(str_old_flags, "[%c%c%c%s%s%c]",
841
(old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
842
(old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
843
(old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
844
!(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
845
!(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ",
846
!(old_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) ? 'P' : '.');
847
848
batadv_dbg(BATADV_DBG_MCAST, bat_priv,
849
"Changing multicast flags from '%s' to '[%c%c%c%s%s%c]'\n",
850
old_enabled ? str_old_flags : "<undefined>",
851
(flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
852
(flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
853
(flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
854
!(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
855
!(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ",
856
!(flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) ? 'P' : '.');
857
}
858
859
/**
860
* batadv_mcast_mla_flags_update() - update multicast flags
861
* @bat_priv: the bat priv with all the mesh interface information
862
* @flags: flags indicating the new multicast state
863
*
864
* Updates the own multicast tvlv with our current multicast related settings,
865
* capabilities and inabilities.
866
*/
867
static void
868
batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
869
struct batadv_mcast_mla_flags *flags)
870
{
871
struct batadv_tvlv_mcast_data mcast_data;
872
873
if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
874
return;
875
876
batadv_mcast_bridge_log(bat_priv, flags);
877
batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
878
879
mcast_data.flags = flags->tvlv_flags;
880
memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
881
882
batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
883
&mcast_data, sizeof(mcast_data));
884
885
bat_priv->mcast.mla_flags = *flags;
886
}
887
888
/**
889
* __batadv_mcast_mla_update() - update the own MLAs
890
* @bat_priv: the bat priv with all the mesh interface information
891
*
892
* Updates the own multicast listener announcements in the translation
893
* table as well as the own, announced multicast tvlv container.
894
*
895
* Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
896
* in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
897
* ensured by the non-parallel execution of the worker this function
898
* belongs to.
899
*/
900
static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
901
{
902
struct net_device *mesh_iface = bat_priv->mesh_iface;
903
struct hlist_head mcast_list = HLIST_HEAD_INIT;
904
struct batadv_mcast_mla_flags flags;
905
int ret;
906
907
flags = batadv_mcast_mla_flags_get(bat_priv);
908
909
ret = batadv_mcast_mla_meshif_get(mesh_iface, &mcast_list, &flags);
910
if (ret < 0)
911
goto out;
912
913
ret = batadv_mcast_mla_bridge_get(mesh_iface, &mcast_list, &flags);
914
if (ret < 0)
915
goto out;
916
917
spin_lock(&bat_priv->mcast.mla_lock);
918
batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
919
batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
920
batadv_mcast_mla_flags_update(bat_priv, &flags);
921
spin_unlock(&bat_priv->mcast.mla_lock);
922
923
out:
924
batadv_mcast_mla_list_free(&mcast_list);
925
}
926
927
/**
928
* batadv_mcast_mla_update() - update the own MLAs
929
* @work: kernel work struct
930
*
931
* Updates the own multicast listener announcements in the translation
932
* table as well as the own, announced multicast tvlv container.
933
*
934
* In the end, reschedules the work timer.
935
*/
936
static void batadv_mcast_mla_update(struct work_struct *work)
937
{
938
struct delayed_work *delayed_work;
939
struct batadv_priv_mcast *priv_mcast;
940
struct batadv_priv *bat_priv;
941
942
delayed_work = to_delayed_work(work);
943
priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
944
bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
945
946
__batadv_mcast_mla_update(bat_priv);
947
batadv_mcast_start_timer(bat_priv);
948
}
949
950
/**
951
* batadv_mcast_is_report_ipv4() - check for IGMP reports
952
* @skb: the ethernet frame destined for the mesh
953
*
954
* This call might reallocate skb data.
955
*
956
* Checks whether the given frame is a valid IGMP report.
957
*
958
* Return: If so then true, otherwise false.
959
*/
960
static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
961
{
962
if (ip_mc_check_igmp(skb) < 0)
963
return false;
964
965
switch (igmp_hdr(skb)->type) {
966
case IGMP_HOST_MEMBERSHIP_REPORT:
967
case IGMPV2_HOST_MEMBERSHIP_REPORT:
968
case IGMPV3_HOST_MEMBERSHIP_REPORT:
969
return true;
970
}
971
972
return false;
973
}
974
975
/**
976
* batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
977
* potential
978
* @bat_priv: the bat priv with all the mesh interface information
979
* @skb: the IPv4 packet to check
980
* @is_unsnoopable: stores whether the destination is snoopable
981
* @is_routable: stores whether the destination is routable
982
*
983
* Checks whether the given IPv4 packet has the potential to be forwarded with a
984
* mode more optimal than classic flooding.
985
*
986
* Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
987
* allocation failure.
988
*/
989
static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
990
struct sk_buff *skb,
991
bool *is_unsnoopable,
992
int *is_routable)
993
{
994
struct iphdr *iphdr;
995
996
/* We might fail due to out-of-memory -> drop it */
997
if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
998
return -ENOMEM;
999
1000
if (batadv_mcast_is_report_ipv4(skb))
1001
return -EINVAL;
1002
1003
iphdr = ip_hdr(skb);
1004
1005
/* link-local multicast listeners behind a bridge are
1006
* not snoopable (see RFC4541, section 2.1.2.2)
1007
*/
1008
if (ipv4_is_local_multicast(iphdr->daddr))
1009
*is_unsnoopable = true;
1010
else
1011
*is_routable = ETH_P_IP;
1012
1013
return 0;
1014
}
1015
1016
/**
1017
* batadv_mcast_is_report_ipv6() - check for MLD reports
1018
* @skb: the ethernet frame destined for the mesh
1019
*
1020
* This call might reallocate skb data.
1021
*
1022
* Checks whether the given frame is a valid MLD report.
1023
*
1024
* Return: If so then true, otherwise false.
1025
*/
1026
static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
1027
{
1028
if (ipv6_mc_check_mld(skb) < 0)
1029
return false;
1030
1031
switch (icmp6_hdr(skb)->icmp6_type) {
1032
case ICMPV6_MGM_REPORT:
1033
case ICMPV6_MLD2_REPORT:
1034
return true;
1035
}
1036
1037
return false;
1038
}
1039
1040
/**
1041
* batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
1042
* potential
1043
* @bat_priv: the bat priv with all the mesh interface information
1044
* @skb: the IPv6 packet to check
1045
* @is_unsnoopable: stores whether the destination is snoopable
1046
* @is_routable: stores whether the destination is routable
1047
*
1048
* Checks whether the given IPv6 packet has the potential to be forwarded with a
1049
* mode more optimal than classic flooding.
1050
*
1051
* Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1052
*/
1053
static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
1054
struct sk_buff *skb,
1055
bool *is_unsnoopable,
1056
int *is_routable)
1057
{
1058
struct ipv6hdr *ip6hdr;
1059
1060
/* We might fail due to out-of-memory -> drop it */
1061
if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
1062
return -ENOMEM;
1063
1064
if (batadv_mcast_is_report_ipv6(skb))
1065
return -EINVAL;
1066
1067
ip6hdr = ipv6_hdr(skb);
1068
1069
if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1070
return -EINVAL;
1071
1072
/* link-local-all-nodes multicast listeners behind a bridge are
1073
* not snoopable (see RFC4541, section 3, paragraph 3)
1074
*/
1075
if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
1076
*is_unsnoopable = true;
1077
else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL)
1078
*is_routable = ETH_P_IPV6;
1079
1080
return 0;
1081
}
1082
1083
/**
1084
* batadv_mcast_forw_mode_check() - check for optimized forwarding potential
1085
* @bat_priv: the bat priv with all the mesh interface information
1086
* @skb: the multicast frame to check
1087
* @is_unsnoopable: stores whether the destination is snoopable
1088
* @is_routable: stores whether the destination is routable
1089
*
1090
* Checks whether the given multicast ethernet frame has the potential to be
1091
* forwarded with a mode more optimal than classic flooding.
1092
*
1093
* Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1094
*/
1095
static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
1096
struct sk_buff *skb,
1097
bool *is_unsnoopable,
1098
int *is_routable)
1099
{
1100
struct ethhdr *ethhdr = eth_hdr(skb);
1101
1102
if (!atomic_read(&bat_priv->multicast_mode))
1103
return -EINVAL;
1104
1105
switch (ntohs(ethhdr->h_proto)) {
1106
case ETH_P_IP:
1107
return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
1108
is_unsnoopable,
1109
is_routable);
1110
case ETH_P_IPV6:
1111
if (!IS_ENABLED(CONFIG_IPV6))
1112
return -EINVAL;
1113
1114
return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
1115
is_unsnoopable,
1116
is_routable);
1117
default:
1118
return -EINVAL;
1119
}
1120
}
1121
1122
/**
1123
* batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
1124
* interest
1125
* @bat_priv: the bat priv with all the mesh interface information
1126
* @ethhdr: ethernet header of a packet
1127
*
1128
* Return: the number of nodes which want all IPv4 multicast traffic if the
1129
* given ethhdr is from an IPv4 packet or the number of nodes which want all
1130
* IPv6 traffic if it matches an IPv6 packet.
1131
*/
1132
static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
1133
struct ethhdr *ethhdr)
1134
{
1135
switch (ntohs(ethhdr->h_proto)) {
1136
case ETH_P_IP:
1137
return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
1138
case ETH_P_IPV6:
1139
return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
1140
default:
1141
/* we shouldn't be here... */
1142
return 0;
1143
}
1144
}
1145
1146
/**
1147
* batadv_mcast_forw_rtr_count() - count nodes with a multicast router
1148
* @bat_priv: the bat priv with all the mesh interface information
1149
* @protocol: the ethernet protocol type to count multicast routers for
1150
*
1151
* Return: the number of nodes which want all routable IPv4 multicast traffic
1152
* if the protocol is ETH_P_IP or the number of nodes which want all routable
1153
* IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0.
1154
*/
1155
1156
static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
1157
int protocol)
1158
{
1159
switch (protocol) {
1160
case ETH_P_IP:
1161
return atomic_read(&bat_priv->mcast.num_want_all_rtr4);
1162
case ETH_P_IPV6:
1163
return atomic_read(&bat_priv->mcast.num_want_all_rtr6);
1164
default:
1165
return 0;
1166
}
1167
}
1168
1169
/**
1170
* batadv_mcast_forw_mode_by_count() - get forwarding mode by count
1171
* @bat_priv: the bat priv with all the mesh interface information
1172
* @skb: the multicast packet to check
1173
* @vid: the vlan identifier
1174
* @is_routable: stores whether the destination is routable
1175
* @count: the number of originators the multicast packet need to be sent to
1176
*
1177
* For a multicast packet with multiple destination originators, checks which
1178
* mode to use. For BATADV_FORW_MCAST it also encapsulates the packet with a
1179
* complete batman-adv multicast header.
1180
*
1181
* Return:
1182
* BATADV_FORW_MCAST: If all nodes have multicast packet routing
1183
* capabilities and an MTU >= 1280 on all hard interfaces (including us)
1184
* and the encapsulated multicast packet with all destination addresses
1185
* would still fit into an 1280 bytes batman-adv multicast packet
1186
* (excluding the outer ethernet frame) and we could successfully push
1187
* the full batman-adv multicast packet header.
1188
* BATADV_FORW_UCASTS: If the packet cannot be sent in a batman-adv
1189
* multicast packet and the amount of batman-adv unicast packets needed
1190
* is smaller or equal to the configured multicast fanout.
1191
* BATADV_FORW_BCAST: Otherwise.
1192
*/
1193
static enum batadv_forw_mode
1194
batadv_mcast_forw_mode_by_count(struct batadv_priv *bat_priv,
1195
struct sk_buff *skb, unsigned short vid,
1196
int is_routable, int count)
1197
{
1198
unsigned int mcast_hdrlen = batadv_mcast_forw_packet_hdrlen(count);
1199
u8 own_tvlv_flags = bat_priv->mcast.mla_flags.tvlv_flags;
1200
1201
if (!atomic_read(&bat_priv->mcast.num_no_mc_ptype_capa) &&
1202
own_tvlv_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA &&
1203
skb->len + mcast_hdrlen <= IPV6_MIN_MTU &&
1204
batadv_mcast_forw_push(bat_priv, skb, vid, is_routable, count))
1205
return BATADV_FORW_MCAST;
1206
1207
if (count <= atomic_read(&bat_priv->multicast_fanout))
1208
return BATADV_FORW_UCASTS;
1209
1210
return BATADV_FORW_BCAST;
1211
}
1212
1213
/**
1214
* batadv_mcast_forw_mode() - check on how to forward a multicast packet
1215
* @bat_priv: the bat priv with all the mesh interface information
1216
* @skb: the multicast packet to check
1217
* @vid: the vlan identifier
1218
* @is_routable: stores whether the destination is routable
1219
*
1220
* Return: The forwarding mode as enum batadv_forw_mode.
1221
*/
1222
enum batadv_forw_mode
1223
batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
1224
unsigned short vid, int *is_routable)
1225
{
1226
int ret, tt_count, ip_count, unsnoop_count, total_count;
1227
bool is_unsnoopable = false;
1228
struct ethhdr *ethhdr;
1229
int rtr_count = 0;
1230
1231
ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable,
1232
is_routable);
1233
if (ret == -ENOMEM)
1234
return BATADV_FORW_NONE;
1235
else if (ret < 0)
1236
return BATADV_FORW_BCAST;
1237
1238
ethhdr = eth_hdr(skb);
1239
1240
tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1241
BATADV_NO_FLAGS);
1242
ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
1243
unsnoop_count = !is_unsnoopable ? 0 :
1244
atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
1245
rtr_count = batadv_mcast_forw_rtr_count(bat_priv, *is_routable);
1246
1247
total_count = tt_count + ip_count + unsnoop_count + rtr_count;
1248
1249
if (!total_count)
1250
return BATADV_FORW_NONE;
1251
else if (unsnoop_count)
1252
return BATADV_FORW_BCAST;
1253
1254
return batadv_mcast_forw_mode_by_count(bat_priv, skb, vid, *is_routable,
1255
total_count);
1256
}
1257
1258
/**
1259
* batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1260
* @bat_priv: the bat priv with all the mesh interface information
1261
* @skb: the multicast packet to send
1262
* @vid: the vlan identifier
1263
* @orig_node: the originator to send the packet to
1264
*
1265
* Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1266
*/
1267
static int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1268
struct sk_buff *skb,
1269
unsigned short vid,
1270
struct batadv_orig_node *orig_node)
1271
{
1272
/* Avoid sending multicast-in-unicast packets to other BLA
1273
* gateways - they already got the frame from the LAN side
1274
* we share with them.
1275
* TODO: Refactor to take BLA into account earlier, to avoid
1276
* reducing the mcast_fanout count.
1277
*/
1278
if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1279
dev_kfree_skb(skb);
1280
return NET_XMIT_SUCCESS;
1281
}
1282
1283
return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1284
orig_node, vid);
1285
}
1286
1287
/**
1288
* batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1289
* @bat_priv: the bat priv with all the mesh interface information
1290
* @skb: the multicast packet to transmit
1291
* @vid: the vlan identifier
1292
*
1293
* Sends copies of a frame with multicast destination to any multicast
1294
* listener registered in the translation table. A transmission is performed
1295
* via a batman-adv unicast packet for each such destination node.
1296
*
1297
* Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1298
* otherwise.
1299
*/
1300
static int
1301
batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
1302
unsigned short vid)
1303
{
1304
int ret = NET_XMIT_SUCCESS;
1305
struct sk_buff *newskb;
1306
1307
struct batadv_tt_orig_list_entry *orig_entry;
1308
1309
struct batadv_tt_global_entry *tt_global;
1310
const u8 *addr = eth_hdr(skb)->h_dest;
1311
1312
tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
1313
if (!tt_global)
1314
goto out;
1315
1316
rcu_read_lock();
1317
hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
1318
newskb = skb_copy(skb, GFP_ATOMIC);
1319
if (!newskb) {
1320
ret = NET_XMIT_DROP;
1321
break;
1322
}
1323
1324
batadv_mcast_forw_send_orig(bat_priv, newskb, vid,
1325
orig_entry->orig_node);
1326
}
1327
rcu_read_unlock();
1328
1329
batadv_tt_global_entry_put(tt_global);
1330
1331
out:
1332
return ret;
1333
}
1334
1335
/**
1336
* batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1337
* @bat_priv: the bat priv with all the mesh interface information
1338
* @skb: the multicast packet to transmit
1339
* @vid: the vlan identifier
1340
*
1341
* Sends copies of a frame with multicast destination to any node with a
1342
* BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1343
* batman-adv unicast packet for each such destination node.
1344
*
1345
* Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1346
* otherwise.
1347
*/
1348
static int
1349
batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
1350
struct sk_buff *skb, unsigned short vid)
1351
{
1352
struct batadv_orig_node *orig_node;
1353
int ret = NET_XMIT_SUCCESS;
1354
struct sk_buff *newskb;
1355
1356
rcu_read_lock();
1357
hlist_for_each_entry_rcu(orig_node,
1358
&bat_priv->mcast.want_all_ipv4_list,
1359
mcast_want_all_ipv4_node) {
1360
newskb = skb_copy(skb, GFP_ATOMIC);
1361
if (!newskb) {
1362
ret = NET_XMIT_DROP;
1363
break;
1364
}
1365
1366
batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1367
}
1368
rcu_read_unlock();
1369
return ret;
1370
}
1371
1372
/**
1373
* batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1374
* @bat_priv: the bat priv with all the mesh interface information
1375
* @skb: The multicast packet to transmit
1376
* @vid: the vlan identifier
1377
*
1378
* Sends copies of a frame with multicast destination to any node with a
1379
* BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1380
* batman-adv unicast packet for each such destination node.
1381
*
1382
* Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1383
* otherwise.
1384
*/
1385
static int
1386
batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
1387
struct sk_buff *skb, unsigned short vid)
1388
{
1389
struct batadv_orig_node *orig_node;
1390
int ret = NET_XMIT_SUCCESS;
1391
struct sk_buff *newskb;
1392
1393
rcu_read_lock();
1394
hlist_for_each_entry_rcu(orig_node,
1395
&bat_priv->mcast.want_all_ipv6_list,
1396
mcast_want_all_ipv6_node) {
1397
newskb = skb_copy(skb, GFP_ATOMIC);
1398
if (!newskb) {
1399
ret = NET_XMIT_DROP;
1400
break;
1401
}
1402
1403
batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1404
}
1405
rcu_read_unlock();
1406
return ret;
1407
}
1408
1409
/**
1410
* batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1411
* @bat_priv: the bat priv with all the mesh interface information
1412
* @skb: the multicast packet to transmit
1413
* @vid: the vlan identifier
1414
*
1415
* Sends copies of a frame with multicast destination to any node with a
1416
* BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1417
* transmission is performed via a batman-adv unicast packet for each such
1418
* destination node.
1419
*
1420
* Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1421
* is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1422
*/
1423
static int
1424
batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
1425
struct sk_buff *skb, unsigned short vid)
1426
{
1427
switch (ntohs(eth_hdr(skb)->h_proto)) {
1428
case ETH_P_IP:
1429
return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
1430
case ETH_P_IPV6:
1431
return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
1432
default:
1433
/* we shouldn't be here... */
1434
return NET_XMIT_DROP;
1435
}
1436
}
1437
1438
/**
1439
* batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
1440
* @bat_priv: the bat priv with all the mesh interface information
1441
* @skb: the multicast packet to transmit
1442
* @vid: the vlan identifier
1443
*
1444
* Sends copies of a frame with multicast destination to any node with a
1445
* BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
1446
* batman-adv unicast packet for each such destination node.
1447
*
1448
* Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1449
* otherwise.
1450
*/
1451
static int
1452
batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
1453
struct sk_buff *skb, unsigned short vid)
1454
{
1455
struct batadv_orig_node *orig_node;
1456
int ret = NET_XMIT_SUCCESS;
1457
struct sk_buff *newskb;
1458
1459
rcu_read_lock();
1460
hlist_for_each_entry_rcu(orig_node,
1461
&bat_priv->mcast.want_all_rtr4_list,
1462
mcast_want_all_rtr4_node) {
1463
newskb = skb_copy(skb, GFP_ATOMIC);
1464
if (!newskb) {
1465
ret = NET_XMIT_DROP;
1466
break;
1467
}
1468
1469
batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1470
}
1471
rcu_read_unlock();
1472
return ret;
1473
}
1474
1475
/**
1476
* batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
1477
* @bat_priv: the bat priv with all the mesh interface information
1478
* @skb: The multicast packet to transmit
1479
* @vid: the vlan identifier
1480
*
1481
* Sends copies of a frame with multicast destination to any node with a
1482
* BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
1483
* batman-adv unicast packet for each such destination node.
1484
*
1485
* Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1486
* otherwise.
1487
*/
1488
static int
1489
batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
1490
struct sk_buff *skb, unsigned short vid)
1491
{
1492
struct batadv_orig_node *orig_node;
1493
int ret = NET_XMIT_SUCCESS;
1494
struct sk_buff *newskb;
1495
1496
rcu_read_lock();
1497
hlist_for_each_entry_rcu(orig_node,
1498
&bat_priv->mcast.want_all_rtr6_list,
1499
mcast_want_all_rtr6_node) {
1500
newskb = skb_copy(skb, GFP_ATOMIC);
1501
if (!newskb) {
1502
ret = NET_XMIT_DROP;
1503
break;
1504
}
1505
1506
batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
1507
}
1508
rcu_read_unlock();
1509
return ret;
1510
}
1511
1512
/**
1513
* batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
1514
* @bat_priv: the bat priv with all the mesh interface information
1515
* @skb: the multicast packet to transmit
1516
* @vid: the vlan identifier
1517
*
1518
* Sends copies of a frame with multicast destination to any node with a
1519
* BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
1520
* transmission is performed via a batman-adv unicast packet for each such
1521
* destination node.
1522
*
1523
* Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1524
* is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1525
*/
1526
static int
1527
batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
1528
struct sk_buff *skb, unsigned short vid)
1529
{
1530
switch (ntohs(eth_hdr(skb)->h_proto)) {
1531
case ETH_P_IP:
1532
return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
1533
case ETH_P_IPV6:
1534
return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
1535
default:
1536
/* we shouldn't be here... */
1537
return NET_XMIT_DROP;
1538
}
1539
}
1540
1541
/**
1542
* batadv_mcast_forw_send() - send packet to any detected multicast recipient
1543
* @bat_priv: the bat priv with all the mesh interface information
1544
* @skb: the multicast packet to transmit
1545
* @vid: the vlan identifier
1546
* @is_routable: stores whether the destination is routable
1547
*
1548
* Sends copies of a frame with multicast destination to any node that signaled
1549
* interest in it, that is either via the translation table or the according
1550
* want-all flags. A transmission is performed via a batman-adv unicast packet
1551
* for each such destination node.
1552
*
1553
* The given skb is consumed/freed.
1554
*
1555
* Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1556
* is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1557
*/
1558
int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
1559
unsigned short vid, int is_routable)
1560
{
1561
int ret;
1562
1563
ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
1564
if (ret != NET_XMIT_SUCCESS) {
1565
kfree_skb(skb);
1566
return ret;
1567
}
1568
1569
ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
1570
if (ret != NET_XMIT_SUCCESS) {
1571
kfree_skb(skb);
1572
return ret;
1573
}
1574
1575
if (!is_routable)
1576
goto skip_mc_router;
1577
1578
ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
1579
if (ret != NET_XMIT_SUCCESS) {
1580
kfree_skb(skb);
1581
return ret;
1582
}
1583
1584
skip_mc_router:
1585
consume_skb(skb);
1586
return ret;
1587
}
1588
1589
/**
1590
* batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
1591
* @bat_priv: the bat priv with all the mesh interface information
1592
* @orig: the orig_node which multicast state might have changed of
1593
* @mcast_flags: flags indicating the new multicast state
1594
*
1595
* If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1596
* orig, has toggled then this method updates the counter and the list
1597
* accordingly.
1598
*
1599
* Caller needs to hold orig->mcast_handler_lock.
1600
*/
1601
static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1602
struct batadv_orig_node *orig,
1603
u8 mcast_flags)
1604
{
1605
struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1606
struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1607
1608
lockdep_assert_held(&orig->mcast_handler_lock);
1609
1610
/* switched from flag unset to set */
1611
if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1612
!(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1613
atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1614
1615
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1616
/* flag checks above + mcast_handler_lock prevents this */
1617
WARN_ON(!hlist_unhashed(node));
1618
1619
hlist_add_head_rcu(node, head);
1620
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1621
/* switched from flag set to unset */
1622
} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1623
orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1624
atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1625
1626
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1627
/* flag checks above + mcast_handler_lock prevents this */
1628
WARN_ON(hlist_unhashed(node));
1629
1630
hlist_del_init_rcu(node);
1631
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1632
}
1633
}
1634
1635
/**
1636
* batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
1637
* @bat_priv: the bat priv with all the mesh interface information
1638
* @orig: the orig_node which multicast state might have changed of
1639
* @mcast_flags: flags indicating the new multicast state
1640
*
1641
* If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1642
* toggled then this method updates the counter and the list accordingly.
1643
*
1644
* Caller needs to hold orig->mcast_handler_lock.
1645
*/
1646
static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1647
struct batadv_orig_node *orig,
1648
u8 mcast_flags)
1649
{
1650
struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1651
struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1652
1653
lockdep_assert_held(&orig->mcast_handler_lock);
1654
1655
/* switched from flag unset to set */
1656
if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1657
!(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1658
atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1659
1660
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1661
/* flag checks above + mcast_handler_lock prevents this */
1662
WARN_ON(!hlist_unhashed(node));
1663
1664
hlist_add_head_rcu(node, head);
1665
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1666
/* switched from flag set to unset */
1667
} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1668
orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1669
atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1670
1671
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1672
/* flag checks above + mcast_handler_lock prevents this */
1673
WARN_ON(hlist_unhashed(node));
1674
1675
hlist_del_init_rcu(node);
1676
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1677
}
1678
}
1679
1680
/**
1681
* batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
1682
* @bat_priv: the bat priv with all the mesh interface information
1683
* @orig: the orig_node which multicast state might have changed of
1684
* @mcast_flags: flags indicating the new multicast state
1685
*
1686
* If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1687
* toggled then this method updates the counter and the list accordingly.
1688
*
1689
* Caller needs to hold orig->mcast_handler_lock.
1690
*/
1691
static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1692
struct batadv_orig_node *orig,
1693
u8 mcast_flags)
1694
{
1695
struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1696
struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1697
1698
lockdep_assert_held(&orig->mcast_handler_lock);
1699
1700
/* switched from flag unset to set */
1701
if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1702
!(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1703
atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1704
1705
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1706
/* flag checks above + mcast_handler_lock prevents this */
1707
WARN_ON(!hlist_unhashed(node));
1708
1709
hlist_add_head_rcu(node, head);
1710
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1711
/* switched from flag set to unset */
1712
} else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1713
orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1714
atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1715
1716
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1717
/* flag checks above + mcast_handler_lock prevents this */
1718
WARN_ON(hlist_unhashed(node));
1719
1720
hlist_del_init_rcu(node);
1721
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1722
}
1723
}
1724
1725
/**
1726
* batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
1727
* @bat_priv: the bat priv with all the mesh interface information
1728
* @orig: the orig_node which multicast state might have changed of
1729
* @mcast_flags: flags indicating the new multicast state
1730
*
1731
* If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
1732
* toggled then this method updates the counter and the list accordingly.
1733
*
1734
* Caller needs to hold orig->mcast_handler_lock.
1735
*/
1736
static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
1737
struct batadv_orig_node *orig,
1738
u8 mcast_flags)
1739
{
1740
struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
1741
struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
1742
1743
lockdep_assert_held(&orig->mcast_handler_lock);
1744
1745
/* switched from flag set to unset */
1746
if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
1747
orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
1748
atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
1749
1750
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1751
/* flag checks above + mcast_handler_lock prevents this */
1752
WARN_ON(!hlist_unhashed(node));
1753
1754
hlist_add_head_rcu(node, head);
1755
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1756
/* switched from flag unset to set */
1757
} else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
1758
!(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
1759
atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
1760
1761
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1762
/* flag checks above + mcast_handler_lock prevents this */
1763
WARN_ON(hlist_unhashed(node));
1764
1765
hlist_del_init_rcu(node);
1766
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1767
}
1768
}
1769
1770
/**
1771
* batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
1772
* @bat_priv: the bat priv with all the mesh interface information
1773
* @orig: the orig_node which multicast state might have changed of
1774
* @mcast_flags: flags indicating the new multicast state
1775
*
1776
* If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
1777
* toggled then this method updates the counter and the list accordingly.
1778
*
1779
* Caller needs to hold orig->mcast_handler_lock.
1780
*/
1781
static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
1782
struct batadv_orig_node *orig,
1783
u8 mcast_flags)
1784
{
1785
struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
1786
struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
1787
1788
lockdep_assert_held(&orig->mcast_handler_lock);
1789
1790
/* switched from flag set to unset */
1791
if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
1792
orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
1793
atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
1794
1795
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1796
/* flag checks above + mcast_handler_lock prevents this */
1797
WARN_ON(!hlist_unhashed(node));
1798
1799
hlist_add_head_rcu(node, head);
1800
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1801
/* switched from flag unset to set */
1802
} else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
1803
!(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
1804
atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
1805
1806
spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1807
/* flag checks above + mcast_handler_lock prevents this */
1808
WARN_ON(hlist_unhashed(node));
1809
1810
hlist_del_init_rcu(node);
1811
spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1812
}
1813
}
1814
1815
/**
1816
* batadv_mcast_have_mc_ptype_update() - update multicast packet type counter
1817
* @bat_priv: the bat priv with all the mesh interface information
1818
* @orig: the orig_node which multicast state might have changed of
1819
* @mcast_flags: flags indicating the new multicast state
1820
*
1821
* If the BATADV_MCAST_HAVE_MC_PTYPE_CAPA flag of this originator, orig, has
1822
* toggled then this method updates the counter accordingly.
1823
*/
1824
static void batadv_mcast_have_mc_ptype_update(struct batadv_priv *bat_priv,
1825
struct batadv_orig_node *orig,
1826
u8 mcast_flags)
1827
{
1828
lockdep_assert_held(&orig->mcast_handler_lock);
1829
1830
/* switched from flag set to unset */
1831
if (!(mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) &&
1832
orig->mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA)
1833
atomic_inc(&bat_priv->mcast.num_no_mc_ptype_capa);
1834
/* switched from flag unset to set */
1835
else if (mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA &&
1836
!(orig->mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA))
1837
atomic_dec(&bat_priv->mcast.num_no_mc_ptype_capa);
1838
}
1839
1840
/**
1841
* batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
1842
* @enabled: whether the originator has multicast TVLV support enabled
1843
* @tvlv_value: tvlv buffer containing the multicast flags
1844
* @tvlv_value_len: tvlv buffer length
1845
*
1846
* Return: multicast flags for the given tvlv buffer
1847
*/
1848
static u8
1849
batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
1850
{
1851
u8 mcast_flags = BATADV_NO_FLAGS;
1852
1853
if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
1854
mcast_flags = *(u8 *)tvlv_value;
1855
1856
if (!enabled) {
1857
mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1858
mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1859
}
1860
1861
/* remove redundant flags to avoid sending duplicate packets later */
1862
if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
1863
mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
1864
1865
if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
1866
mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
1867
1868
return mcast_flags;
1869
}
1870
1871
/**
1872
* batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
1873
* @bat_priv: the bat priv with all the mesh interface information
1874
* @orig: the orig_node of the ogm
1875
* @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1876
* @tvlv_value: tvlv buffer containing the multicast data
1877
* @tvlv_value_len: tvlv buffer length
1878
*/
1879
static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1880
struct batadv_orig_node *orig,
1881
u8 flags,
1882
void *tvlv_value,
1883
u16 tvlv_value_len)
1884
{
1885
bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1886
u8 mcast_flags;
1887
1888
mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
1889
tvlv_value, tvlv_value_len);
1890
1891
spin_lock_bh(&orig->mcast_handler_lock);
1892
1893
if (orig_mcast_enabled &&
1894
!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1895
set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1896
} else if (!orig_mcast_enabled &&
1897
test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1898
clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1899
}
1900
1901
set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1902
1903
batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1904
batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1905
batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1906
batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
1907
batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
1908
batadv_mcast_have_mc_ptype_update(bat_priv, orig, mcast_flags);
1909
1910
orig->mcast_flags = mcast_flags;
1911
spin_unlock_bh(&orig->mcast_handler_lock);
1912
}
1913
1914
/**
1915
* batadv_mcast_init() - initialize the multicast optimizations structures
1916
* @bat_priv: the bat priv with all the mesh interface information
1917
*/
1918
void batadv_mcast_init(struct batadv_priv *bat_priv)
1919
{
1920
batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1921
NULL, NULL, BATADV_TVLV_MCAST, 2,
1922
BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1923
batadv_tvlv_handler_register(bat_priv, NULL, NULL,
1924
batadv_mcast_forw_tracker_tvlv_handler,
1925
BATADV_TVLV_MCAST_TRACKER, 1,
1926
BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1927
1928
INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1929
batadv_mcast_start_timer(bat_priv);
1930
}
1931
1932
/**
1933
* batadv_mcast_mesh_info_put() - put multicast info into a netlink message
1934
* @msg: buffer for the message
1935
* @bat_priv: the bat priv with all the mesh interface information
1936
*
1937
* Return: 0 or error code.
1938
*/
1939
int batadv_mcast_mesh_info_put(struct sk_buff *msg,
1940
struct batadv_priv *bat_priv)
1941
{
1942
u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
1943
u32 flags_priv = BATADV_NO_FLAGS;
1944
1945
if (bat_priv->mcast.mla_flags.bridged) {
1946
flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
1947
1948
if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
1949
flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
1950
if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
1951
flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
1952
if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
1953
flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
1954
if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
1955
flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
1956
}
1957
1958
if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
1959
nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
1960
return -EMSGSIZE;
1961
1962
return 0;
1963
}
1964
1965
/**
1966
* batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
1967
* to a netlink socket
1968
* @msg: buffer for the message
1969
* @portid: netlink port
1970
* @cb: Control block containing additional options
1971
* @orig_node: originator to dump the multicast flags of
1972
*
1973
* Return: 0 or error code.
1974
*/
1975
static int
1976
batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
1977
struct netlink_callback *cb,
1978
struct batadv_orig_node *orig_node)
1979
{
1980
void *hdr;
1981
1982
hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1983
&batadv_netlink_family, NLM_F_MULTI,
1984
BATADV_CMD_GET_MCAST_FLAGS);
1985
if (!hdr)
1986
return -ENOBUFS;
1987
1988
genl_dump_check_consistent(cb, hdr);
1989
1990
if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1991
orig_node->orig)) {
1992
genlmsg_cancel(msg, hdr);
1993
return -EMSGSIZE;
1994
}
1995
1996
if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1997
&orig_node->capabilities)) {
1998
if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
1999
orig_node->mcast_flags)) {
2000
genlmsg_cancel(msg, hdr);
2001
return -EMSGSIZE;
2002
}
2003
}
2004
2005
genlmsg_end(msg, hdr);
2006
return 0;
2007
}
2008
2009
/**
2010
* batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
2011
* table to a netlink socket
2012
* @msg: buffer for the message
2013
* @portid: netlink port
2014
* @cb: Control block containing additional options
2015
* @hash: hash to dump
2016
* @bucket: bucket index to dump
2017
* @idx_skip: How many entries to skip
2018
*
2019
* Return: 0 or error code.
2020
*/
2021
static int
2022
batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
2023
struct netlink_callback *cb,
2024
struct batadv_hashtable *hash,
2025
unsigned int bucket, long *idx_skip)
2026
{
2027
struct batadv_orig_node *orig_node;
2028
long idx = 0;
2029
2030
spin_lock_bh(&hash->list_locks[bucket]);
2031
cb->seq = atomic_read(&hash->generation) << 1 | 1;
2032
2033
hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
2034
if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2035
&orig_node->capa_initialized))
2036
continue;
2037
2038
if (idx < *idx_skip)
2039
goto skip;
2040
2041
if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
2042
spin_unlock_bh(&hash->list_locks[bucket]);
2043
*idx_skip = idx;
2044
2045
return -EMSGSIZE;
2046
}
2047
2048
skip:
2049
idx++;
2050
}
2051
spin_unlock_bh(&hash->list_locks[bucket]);
2052
2053
return 0;
2054
}
2055
2056
/**
2057
* __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2058
* @msg: buffer for the message
2059
* @portid: netlink port
2060
* @cb: Control block containing additional options
2061
* @bat_priv: the bat priv with all the mesh interface information
2062
* @bucket: current bucket to dump
2063
* @idx: index in current bucket to the next entry to dump
2064
*
2065
* Return: 0 or error code.
2066
*/
2067
static int
2068
__batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
2069
struct netlink_callback *cb,
2070
struct batadv_priv *bat_priv, long *bucket, long *idx)
2071
{
2072
struct batadv_hashtable *hash = bat_priv->orig_hash;
2073
long bucket_tmp = *bucket;
2074
long idx_tmp = *idx;
2075
2076
while (bucket_tmp < hash->size) {
2077
if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
2078
bucket_tmp, &idx_tmp))
2079
break;
2080
2081
bucket_tmp++;
2082
idx_tmp = 0;
2083
}
2084
2085
*bucket = bucket_tmp;
2086
*idx = idx_tmp;
2087
2088
return msg->len;
2089
}
2090
2091
/**
2092
* batadv_mcast_netlink_get_primary() - get primary interface from netlink
2093
* callback
2094
* @cb: netlink callback structure
2095
* @primary_if: the primary interface pointer to return the result in
2096
*
2097
* Return: 0 or error code.
2098
*/
2099
static int
2100
batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
2101
struct batadv_hard_iface **primary_if)
2102
{
2103
struct batadv_hard_iface *hard_iface = NULL;
2104
struct net_device *mesh_iface;
2105
struct batadv_priv *bat_priv;
2106
int ret = 0;
2107
2108
mesh_iface = batadv_netlink_get_meshif(cb);
2109
if (IS_ERR(mesh_iface))
2110
return PTR_ERR(mesh_iface);
2111
2112
bat_priv = netdev_priv(mesh_iface);
2113
2114
hard_iface = batadv_primary_if_get_selected(bat_priv);
2115
if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
2116
ret = -ENOENT;
2117
goto out;
2118
}
2119
2120
out:
2121
dev_put(mesh_iface);
2122
2123
if (!ret && primary_if)
2124
*primary_if = hard_iface;
2125
else
2126
batadv_hardif_put(hard_iface);
2127
2128
return ret;
2129
}
2130
2131
/**
2132
* batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2133
* @msg: buffer for the message
2134
* @cb: callback structure containing arguments
2135
*
2136
* Return: message length.
2137
*/
2138
int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
2139
{
2140
struct batadv_hard_iface *primary_if = NULL;
2141
int portid = NETLINK_CB(cb->skb).portid;
2142
struct batadv_priv *bat_priv;
2143
long *bucket = &cb->args[0];
2144
long *idx = &cb->args[1];
2145
int ret;
2146
2147
ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
2148
if (ret)
2149
return ret;
2150
2151
bat_priv = netdev_priv(primary_if->mesh_iface);
2152
ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
2153
2154
batadv_hardif_put(primary_if);
2155
return ret;
2156
}
2157
2158
/**
2159
* batadv_mcast_free() - free the multicast optimizations structures
2160
* @bat_priv: the bat priv with all the mesh interface information
2161
*/
2162
void batadv_mcast_free(struct batadv_priv *bat_priv)
2163
{
2164
cancel_delayed_work_sync(&bat_priv->mcast.work);
2165
2166
batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2167
batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST_TRACKER, 1);
2168
batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2169
2170
/* safely calling outside of worker, as worker was canceled above */
2171
batadv_mcast_mla_tt_retract(bat_priv, NULL);
2172
}
2173
2174
/**
2175
* batadv_mcast_purge_orig() - reset originator global mcast state modifications
2176
* @orig: the originator which is going to get purged
2177
*/
2178
void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
2179
{
2180
struct batadv_priv *bat_priv = orig->bat_priv;
2181
2182
spin_lock_bh(&orig->mcast_handler_lock);
2183
2184
batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
2185
batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
2186
batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
2187
batadv_mcast_want_rtr4_update(bat_priv, orig,
2188
BATADV_MCAST_WANT_NO_RTR4);
2189
batadv_mcast_want_rtr6_update(bat_priv, orig,
2190
BATADV_MCAST_WANT_NO_RTR6);
2191
batadv_mcast_have_mc_ptype_update(bat_priv, orig,
2192
BATADV_MCAST_HAVE_MC_PTYPE_CAPA);
2193
2194
spin_unlock_bh(&orig->mcast_handler_lock);
2195
}
2196
2197