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