]> asedeno.scripts.mit.edu Git - linux.git/blob - net/batman-adv/multicast.c
4d6e89e04aa0e8aeae20de7cb10ef8100f9b78b6
[linux.git] / net / batman-adv / multicast.c
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/ip.h>
24 #include <linux/ipv6.h>
25 #include <linux/jiffies.h>
26 #include <linux/kernel.h>
27 #include <linux/kref.h>
28 #include <linux/list.h>
29 #include <linux/lockdep.h>
30 #include <linux/netdevice.h>
31 #include <linux/netlink.h>
32 #include <linux/printk.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/seq_file.h>
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>
42 #include <linux/workqueue.h>
43 #include <net/addrconf.h>
44 #include <net/genetlink.h>
45 #include <net/if_inet6.h>
46 #include <net/ip.h>
47 #include <net/ipv6.h>
48 #include <net/netlink.h>
49 #include <net/sock.h>
50 #include <uapi/linux/batadv_packet.h>
51 #include <uapi/linux/batman_adv.h>
52
53 #include "hard-interface.h"
54 #include "hash.h"
55 #include "log.h"
56 #include "netlink.h"
57 #include "soft-interface.h"
58 #include "translation-table.h"
59 #include "tvlv.h"
60
61 static void batadv_mcast_mla_update(struct work_struct *work);
62
63 /**
64  * batadv_mcast_start_timer() - schedule the multicast periodic worker
65  * @bat_priv: the bat priv with all the soft interface information
66  */
67 static 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
73 /**
74  * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
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  */
83 static 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
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  */
106 static 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  */
120 static 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
127 /**
128  * batadv_mcast_mla_softif_get() - get softif multicast listeners
129  * @bat_priv: the bat priv with all the soft interface information
130  * @dev: the device to collect multicast addresses from
131  * @mcast_list: a list to put found addresses into
132  *
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.
142  *
143  * Return: -ENOMEM on memory allocation error or the number of
144  * items added to the mcast_list otherwise.
145  */
146 static int batadv_mcast_mla_softif_get(struct batadv_priv *bat_priv,
147                                        struct net_device *dev,
148                                        struct hlist_head *mcast_list)
149 {
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;
152         struct net_device *bridge = batadv_mcast_get_bridge(dev);
153         struct netdev_hw_addr *mc_list_entry;
154         struct batadv_hw_addr *new;
155         int ret = 0;
156
157         netif_addr_lock_bh(bridge ? bridge : dev);
158         netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
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
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         }
175         netif_addr_unlock_bh(bridge ? bridge : dev);
176
177         if (bridge)
178                 dev_put(bridge);
179
180         return ret;
181 }
182
183 /**
184  * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
185  * @mcast_addr: the multicast address to check
186  * @mcast_list: the list with multicast addresses to search in
187  *
188  * Return: true if the given address is already in the given list.
189  * Otherwise returns false.
190  */
191 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
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
203 /**
204  * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
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  */
215 static 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 /**
228  * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
229  * @bat_priv: the bat priv with all the soft interface information
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  */
241 static int batadv_mcast_mla_bridge_get(struct batadv_priv *bat_priv,
242                                        struct net_device *dev,
243                                        struct hlist_head *mcast_list)
244 {
245         struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
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;
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) {
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
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
281 out:
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
290 /**
291  * batadv_mcast_mla_list_free() - free a list of multicast addresses
292  * @mcast_list: the list to free
293  *
294  * Removes and frees all items in the given mcast_list.
295  */
296 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
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 /**
308  * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
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.
316  *
317  * Do not call outside of the mcast worker! (or cancel mcast worker first)
318  */
319 static 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
325         WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
326
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 /**
344  * batadv_mcast_mla_tt_add() - add multicast listener announcements
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.
350  *
351  * Do not call outside of the mcast worker! (or cancel mcast worker first)
352  */
353 static 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
359         WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
360
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 /**
380  * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
381  * @bat_priv: the bat priv with all the soft interface information
382  *
383  * Checks whether there is a bridge on top of our soft interface.
384  *
385  * Return: true if there is a bridge, false otherwise.
386  */
387 static 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
400 /**
401  * batadv_mcast_querier_log() - debug output regarding the querier status on
402  *  link
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  */
422 static void
423 batadv_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 /**
453  * batadv_mcast_bridge_log() - debug output for topology changes in bridged
454  *  setups
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  */
470 static void
471 batadv_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 /**
493  * batadv_mcast_flags_logs() - output debug information about mcast flag changes
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  */
500 static 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
518 /**
519  * batadv_mcast_mla_tvlv_update() - update multicast tvlv
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  *
525  * Return: false if we want all IPv4 && IPv6 multicast traffic and true
526  * otherwise.
527  */
528 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
529 {
530         struct batadv_tvlv_mcast_data mcast_data;
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;
534         bool bridged;
535
536         mcast_data.flags = BATADV_NO_FLAGS;
537         memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
538
539         bridged = batadv_mcast_has_bridge(bat_priv);
540         if (!bridged)
541                 goto update;
542
543         if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
544                 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
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.
562          */
563         if (!querier4.exists || querier4.shadowing)
564                 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
565
566         if (!querier6.exists || querier6.shadowing)
567                 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
568
569 update:
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
580         if (!bat_priv->mcast.enabled ||
581             mcast_data.flags != bat_priv->mcast.flags) {
582                 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
583                 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
584                                                &mcast_data, sizeof(mcast_data));
585                 bat_priv->mcast.flags = mcast_data.flags;
586                 bat_priv->mcast.enabled = true;
587         }
588
589         return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
590                  mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
591 }
592
593 /**
594  * __batadv_mcast_mla_update() - update the own MLAs
595  * @bat_priv: the bat priv with all the soft interface information
596  *
597  * Updates the own multicast listener announcements in the translation
598  * table as well as the own, announced multicast tvlv container.
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.
604  */
605 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
606 {
607         struct net_device *soft_iface = bat_priv->soft_iface;
608         struct hlist_head mcast_list = HLIST_HEAD_INIT;
609         int ret;
610
611         if (!batadv_mcast_mla_tvlv_update(bat_priv))
612                 goto update;
613
614         ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
615         if (ret < 0)
616                 goto out;
617
618         ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
619         if (ret < 0)
620                 goto out;
621
622 update:
623         batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
624         batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
625
626 out:
627         batadv_mcast_mla_list_free(&mcast_list);
628 }
629
630 /**
631  * batadv_mcast_mla_update() - update the own MLAs
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  */
639 static 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
653 /**
654  * batadv_mcast_is_report_ipv4() - check for IGMP reports
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  */
663 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
664 {
665         if (ip_mc_check_igmp(skb) < 0)
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
678 /**
679  * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
680  *  potential
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  *
688  * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
689  * allocation failure.
690  */
691 static 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
701         if (batadv_mcast_is_report_ipv4(skb))
702                 return -EINVAL;
703
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
720 /**
721  * batadv_mcast_is_report_ipv6() - check for MLD reports
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  */
730 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
731 {
732         if (ipv6_mc_check_mld(skb) < 0)
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
744 /**
745  * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
746  *  potential
747  * @bat_priv: the bat priv with all the soft interface information
748  * @skb: the IPv6 packet to check
749  * @is_unsnoopable: stores whether the destination is snoopable
750  *
751  * Checks whether the given IPv6 packet has the potential to be forwarded with a
752  * mode more optimal than classic flooding.
753  *
754  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
755  */
756 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
757                                              struct sk_buff *skb,
758                                              bool *is_unsnoopable)
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
766         if (batadv_mcast_is_report_ipv6(skb))
767                 return -EINVAL;
768
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))
781                 *is_unsnoopable = true;
782
783         return 0;
784 }
785
786 /**
787  * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
788  * @bat_priv: the bat priv with all the soft interface information
789  * @skb: the multicast frame to check
790  * @is_unsnoopable: stores whether the destination is snoopable
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  *
795  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
796  */
797 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
798                                         struct sk_buff *skb,
799                                         bool *is_unsnoopable)
800 {
801         struct ethhdr *ethhdr = eth_hdr(skb);
802
803         if (!atomic_read(&bat_priv->multicast_mode))
804                 return -EINVAL;
805
806         switch (ntohs(ethhdr->h_proto)) {
807         case ETH_P_IP:
808                 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
809                                                          is_unsnoopable);
810         case ETH_P_IPV6:
811                 if (!IS_ENABLED(CONFIG_IPV6))
812                         return -EINVAL;
813
814                 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
815                                                          is_unsnoopable);
816         default:
817                 return -EINVAL;
818         }
819 }
820
821 /**
822  * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
823  *  interest
824  * @bat_priv: the bat priv with all the soft interface information
825  * @ethhdr: ethernet header of a packet
826  *
827  * Return: the number of nodes which want all IPv4 multicast traffic if the
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  */
831 static 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
845 /**
846  * batadv_mcast_forw_tt_node_get() - get a multicast tt node
847  * @bat_priv: the bat priv with all the soft interface information
848  * @ethhdr: the ether header containing the multicast destination
849  *
850  * Return: an orig_node matching the multicast address provided by ethhdr
851  * via a translation table lookup. This increases the returned nodes refcount.
852  */
853 static struct batadv_orig_node *
854 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
855                               struct ethhdr *ethhdr)
856 {
857         return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
858                                         BATADV_NO_FLAGS);
859 }
860
861 /**
862  * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
863  * @bat_priv: the bat priv with all the soft interface information
864  *
865  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
866  * increases its refcount.
867  */
868 static struct batadv_orig_node *
869 batadv_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) {
877                 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
878                         continue;
879
880                 orig_node = tmp_orig_node;
881                 break;
882         }
883         rcu_read_unlock();
884
885         return orig_node;
886 }
887
888 /**
889  * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
890  * @bat_priv: the bat priv with all the soft interface information
891  *
892  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
893  * and increases its refcount.
894  */
895 static struct batadv_orig_node *
896 batadv_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) {
904                 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
905                         continue;
906
907                 orig_node = tmp_orig_node;
908                 break;
909         }
910         rcu_read_unlock();
911
912         return orig_node;
913 }
914
915 /**
916  * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
917  * @bat_priv: the bat priv with all the soft interface information
918  * @ethhdr: an ethernet header to determine the protocol family from
919  *
920  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
921  * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
922  * increases its refcount.
923  */
924 static struct batadv_orig_node *
925 batadv_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
939 /**
940  * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
941  * @bat_priv: the bat priv with all the soft interface information
942  *
943  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
944  * set and increases its refcount.
945  */
946 static struct batadv_orig_node *
947 batadv_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) {
955                 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
956                         continue;
957
958                 orig_node = tmp_orig_node;
959                 break;
960         }
961         rcu_read_unlock();
962
963         return orig_node;
964 }
965
966 /**
967  * batadv_mcast_forw_mode() - check on how to forward a multicast packet
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  *
972  * Return: the forwarding mode as enum batadv_forw_mode and in case of
973  * BATADV_FORW_SINGLE set the orig to the single originator the skb
974  * should be forwarded to.
975  */
976 enum batadv_forw_mode
977 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
978                        struct batadv_orig_node **orig)
979 {
980         int ret, tt_count, ip_count, unsnoop_count, total_count;
981         bool is_unsnoopable = false;
982         struct ethhdr *ethhdr;
983
984         ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
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);
994         ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
995         unsnoop_count = !is_unsnoopable ? 0 :
996                         atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
997
998         total_count = tt_count + ip_count + unsnoop_count;
999
1000         switch (total_count) {
1001         case 1:
1002                 if (tt_count)
1003                         *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
1004                 else if (ip_count)
1005                         *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
1006                 else if (unsnoop_count)
1007                         *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1008
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
1020 /**
1021  * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
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.
1028  *
1029  * Caller needs to hold orig->mcast_handler_lock.
1030  */
1031 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1032                                              struct batadv_orig_node *orig,
1033                                              u8 mcast_flags)
1034 {
1035         struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1036         struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1037
1038         lockdep_assert_held(&orig->mcast_handler_lock);
1039
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);
1046                 /* flag checks above + mcast_handler_lock prevents this */
1047                 WARN_ON(!hlist_unhashed(node));
1048
1049                 hlist_add_head_rcu(node, head);
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);
1057                 /* flag checks above + mcast_handler_lock prevents this */
1058                 WARN_ON(hlist_unhashed(node));
1059
1060                 hlist_del_init_rcu(node);
1061                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1062         }
1063 }
1064
1065 /**
1066  * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
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.
1073  *
1074  * Caller needs to hold orig->mcast_handler_lock.
1075  */
1076 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1077                                           struct batadv_orig_node *orig,
1078                                           u8 mcast_flags)
1079 {
1080         struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1081         struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1082
1083         lockdep_assert_held(&orig->mcast_handler_lock);
1084
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);
1091                 /* flag checks above + mcast_handler_lock prevents this */
1092                 WARN_ON(!hlist_unhashed(node));
1093
1094                 hlist_add_head_rcu(node, head);
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);
1102                 /* flag checks above + mcast_handler_lock prevents this */
1103                 WARN_ON(hlist_unhashed(node));
1104
1105                 hlist_del_init_rcu(node);
1106                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1107         }
1108 }
1109
1110 /**
1111  * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
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.
1118  *
1119  * Caller needs to hold orig->mcast_handler_lock.
1120  */
1121 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1122                                           struct batadv_orig_node *orig,
1123                                           u8 mcast_flags)
1124 {
1125         struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1126         struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1127
1128         lockdep_assert_held(&orig->mcast_handler_lock);
1129
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);
1136                 /* flag checks above + mcast_handler_lock prevents this */
1137                 WARN_ON(!hlist_unhashed(node));
1138
1139                 hlist_add_head_rcu(node, head);
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);
1147                 /* flag checks above + mcast_handler_lock prevents this */
1148                 WARN_ON(hlist_unhashed(node));
1149
1150                 hlist_del_init_rcu(node);
1151                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1152         }
1153 }
1154
1155 /**
1156  * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
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  */
1163 static 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)
1168 {
1169         bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1170         u8 mcast_flags = BATADV_NO_FLAGS;
1171
1172         if (orig_mcast_enabled && tvlv_value &&
1173             tvlv_value_len >= sizeof(mcast_flags))
1174                 mcast_flags = *(u8 *)tvlv_value;
1175
1176         if (!orig_mcast_enabled) {
1177                 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1178                 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1179         }
1180
1181         spin_lock_bh(&orig->mcast_handler_lock);
1182
1183         if (orig_mcast_enabled &&
1184             !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1185                 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1186         } else if (!orig_mcast_enabled &&
1187                    test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1188                 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1189         }
1190
1191         set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1192
1193         batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1194         batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1195         batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1196
1197         orig->mcast_flags = mcast_flags;
1198         spin_unlock_bh(&orig->mcast_handler_lock);
1199 }
1200
1201 /**
1202  * batadv_mcast_init() - initialize the multicast optimizations structures
1203  * @bat_priv: the bat priv with all the soft interface information
1204  */
1205 void batadv_mcast_init(struct batadv_priv *bat_priv)
1206 {
1207         batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1208                                      NULL, BATADV_TVLV_MCAST, 2,
1209                                      BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1210
1211         INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1212         batadv_mcast_start_timer(bat_priv);
1213 }
1214
1215 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1216 /**
1217  * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
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  */
1225 static 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 /**
1258  * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
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  */
1267 int 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 }
1316 #endif
1317
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  */
1325 int 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
1356  * @cb: Control block containing additional options
1357  * @orig_node: originator to dump the multicast flags of
1358  *
1359  * Return: 0 or error code.
1360  */
1361 static int
1362 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
1363                               struct netlink_callback *cb,
1364                               struct batadv_orig_node *orig_node)
1365 {
1366         void *hdr;
1367
1368         hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1369                           &batadv_netlink_family, NLM_F_MULTI,
1370                           BATADV_CMD_GET_MCAST_FLAGS);
1371         if (!hdr)
1372                 return -ENOBUFS;
1373
1374         genl_dump_check_consistent(cb, hdr);
1375
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
1400  * @cb: Control block containing additional options
1401  * @hash: hash to dump
1402  * @bucket: bucket index to dump
1403  * @idx_skip: How many entries to skip
1404  *
1405  * Return: 0 or error code.
1406  */
1407 static int
1408 batadv_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)
1412 {
1413         struct batadv_orig_node *orig_node;
1414         long idx = 0;
1415
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) {
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
1427                 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
1428                         spin_unlock_bh(&hash->list_locks[bucket]);
1429                         *idx_skip = idx;
1430
1431                         return -EMSGSIZE;
1432                 }
1433
1434 skip:
1435                 idx++;
1436         }
1437         spin_unlock_bh(&hash->list_locks[bucket]);
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
1446  * @cb: Control block containing additional options
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  */
1453 static int
1454 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
1455                           struct netlink_callback *cb,
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;
1460         long idx_tmp = *idx;
1461
1462         while (bucket_tmp < hash->size) {
1463                 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
1464                                                    *bucket, &idx_tmp))
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  */
1485 static int
1486 batadv_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
1514 out:
1515         if (soft_iface)
1516                 dev_put(soft_iface);
1517
1518         if (!ret && primary_if)
1519                 *primary_if = hard_iface;
1520         else if (hard_iface)
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  */
1533 int 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);
1547         ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
1548
1549         batadv_hardif_put(primary_if);
1550         return ret;
1551 }
1552
1553 /**
1554  * batadv_mcast_free() - free the multicast optimizations structures
1555  * @bat_priv: the bat priv with all the soft interface information
1556  */
1557 void batadv_mcast_free(struct batadv_priv *bat_priv)
1558 {
1559         cancel_delayed_work_sync(&bat_priv->mcast.work);
1560
1561         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1562         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1563
1564         /* safely calling outside of worker, as worker was canceled above */
1565         batadv_mcast_mla_tt_retract(bat_priv, NULL);
1566 }
1567
1568 /**
1569  * batadv_mcast_purge_orig() - reset originator global mcast state modifications
1570  * @orig: the originator which is going to get purged
1571  */
1572 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1573 {
1574         struct batadv_priv *bat_priv = orig->bat_priv;
1575
1576         spin_lock_bh(&orig->mcast_handler_lock);
1577
1578         batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1579         batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1580         batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1581
1582         spin_unlock_bh(&orig->mcast_handler_lock);
1583 }