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