GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / batman-adv / multicast.c
1 /* Copyright (C) 2014-2016  B.A.T.M.A.N. contributors:
2  *
3  * Linus Lüssing
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "multicast.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
27 #include <linux/fs.h>
28 #include <linux/icmpv6.h>
29 #include <linux/if_bridge.h>
30 #include <linux/if_ether.h>
31 #include <linux/igmp.h>
32 #include <linux/in.h>
33 #include <linux/in6.h>
34 #include <linux/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/kernel.h>
37 #include <linux/kref.h>
38 #include <linux/list.h>
39 #include <linux/lockdep.h>
40 #include <linux/netdevice.h>
41 #include <linux/printk.h>
42 #include <linux/rculist.h>
43 #include <linux/rcupdate.h>
44 #include <linux/seq_file.h>
45 #include <linux/skbuff.h>
46 #include <linux/slab.h>
47 #include <linux/spinlock.h>
48 #include <linux/stddef.h>
49 #include <linux/string.h>
50 #include <linux/types.h>
51 #include <net/addrconf.h>
52 #include <net/if_inet6.h>
53 #include <net/ip.h>
54 #include <net/ipv6.h>
55
56 #include "bridge_loop_avoidance.h"
57 #include "hard-interface.h"
58 #include "hash.h"
59 #include "log.h"
60 #include "packet.h"
61 #include "send.h"
62 #include "translation-table.h"
63 #include "tvlv.h"
64
65 /**
66  * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
67  * @soft_iface: netdev struct of the mesh interface
68  *
69  * If the given soft interface has a bridge on top then the refcount
70  * of the according net device is increased.
71  *
72  * Return: NULL if no such bridge exists. Otherwise the net device of the
73  * bridge.
74  */
75 static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
76 {
77         struct net_device *upper = soft_iface;
78
79         rcu_read_lock();
80         do {
81                 upper = netdev_master_upper_dev_get_rcu(upper);
82         } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
83
84         if (upper)
85                 dev_hold(upper);
86         rcu_read_unlock();
87
88         return upper;
89 }
90
91 /**
92  * batadv_mcast_mla_softif_get - get softif multicast listeners
93  * @dev: the device to collect multicast addresses from
94  * @mcast_list: a list to put found addresses into
95  *
96  * Collects multicast addresses of multicast listeners residing
97  * on this kernel on the given soft interface, dev, in
98  * the given mcast_list. In general, multicast listeners provided by
99  * your multicast receiving applications run directly on this node.
100  *
101  * If there is a bridge interface on top of dev, collects from that one
102  * instead. Just like with IP addresses and routes, multicast listeners
103  * will(/should) register to the bridge interface instead of an
104  * enslaved bat0.
105  *
106  * Return: -ENOMEM on memory allocation error or the number of
107  * items added to the mcast_list otherwise.
108  */
109 static int batadv_mcast_mla_softif_get(struct net_device *dev,
110                                        struct hlist_head *mcast_list)
111 {
112         struct net_device *bridge = batadv_mcast_get_bridge(dev);
113         struct netdev_hw_addr *mc_list_entry;
114         struct batadv_hw_addr *new;
115         int ret = 0;
116
117         netif_addr_lock_bh(bridge ? bridge : dev);
118         netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
119                 new = kmalloc(sizeof(*new), GFP_ATOMIC);
120                 if (!new) {
121                         ret = -ENOMEM;
122                         break;
123                 }
124
125                 ether_addr_copy(new->addr, mc_list_entry->addr);
126                 hlist_add_head(&new->list, mcast_list);
127                 ret++;
128         }
129         netif_addr_unlock_bh(bridge ? bridge : dev);
130
131         if (bridge)
132                 dev_put(bridge);
133
134         return ret;
135 }
136
137 /**
138  * batadv_mcast_mla_is_duplicate - check whether an address is in a list
139  * @mcast_addr: the multicast address to check
140  * @mcast_list: the list with multicast addresses to search in
141  *
142  * Return: true if the given address is already in the given list.
143  * Otherwise returns false.
144  */
145 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
146                                           struct hlist_head *mcast_list)
147 {
148         struct batadv_hw_addr *mcast_entry;
149
150         hlist_for_each_entry(mcast_entry, mcast_list, list)
151                 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
152                         return true;
153
154         return false;
155 }
156
157 /**
158  * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
159  * @dst: destination to write to - a multicast MAC address
160  * @src: source to read from - a multicast IP address
161  *
162  * Converts a given multicast IPv4/IPv6 address from a bridge
163  * to its matching multicast MAC address and copies it into the given
164  * destination buffer.
165  *
166  * Caller needs to make sure the destination buffer can hold
167  * at least ETH_ALEN bytes.
168  */
169 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
170 {
171         if (src->proto == htons(ETH_P_IP))
172                 ip_eth_mc_map(src->u.ip4, dst);
173 #if IS_ENABLED(CONFIG_IPV6)
174         else if (src->proto == htons(ETH_P_IPV6))
175                 ipv6_eth_mc_map(&src->u.ip6, dst);
176 #endif
177         else
178                 eth_zero_addr(dst);
179 }
180
181 /**
182  * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
183  * @dev: a bridge slave whose bridge to collect multicast addresses from
184  * @mcast_list: a list to put found addresses into
185  *
186  * Collects multicast addresses of multicast listeners residing
187  * on foreign, non-mesh devices which we gave access to our mesh via
188  * a bridge on top of the given soft interface, dev, in the given
189  * mcast_list.
190  *
191  * Return: -ENOMEM on memory allocation error or the number of
192  * items added to the mcast_list otherwise.
193  */
194 static int batadv_mcast_mla_bridge_get(struct net_device *dev,
195                                        struct hlist_head *mcast_list)
196 {
197         struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
198         struct br_ip_list *br_ip_entry, *tmp;
199         struct batadv_hw_addr *new;
200         u8 mcast_addr[ETH_ALEN];
201         int ret;
202
203         /* we don't need to detect these devices/listeners, the IGMP/MLD
204          * snooping code of the Linux bridge already does that for us
205          */
206         ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
207         if (ret < 0)
208                 goto out;
209
210         list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
211                 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
212                 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
213                         continue;
214
215                 new = kmalloc(sizeof(*new), GFP_ATOMIC);
216                 if (!new) {
217                         ret = -ENOMEM;
218                         break;
219                 }
220
221                 ether_addr_copy(new->addr, mcast_addr);
222                 hlist_add_head(&new->list, mcast_list);
223         }
224
225 out:
226         list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
227                 list_del(&br_ip_entry->list);
228                 kfree(br_ip_entry);
229         }
230
231         return ret;
232 }
233
234 /**
235  * batadv_mcast_mla_list_free - free a list of multicast addresses
236  * @bat_priv: the bat priv with all the soft interface information
237  * @mcast_list: the list to free
238  *
239  * Removes and frees all items in the given mcast_list.
240  */
241 static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
242                                        struct hlist_head *mcast_list)
243 {
244         struct batadv_hw_addr *mcast_entry;
245         struct hlist_node *tmp;
246
247         lockdep_assert_held(&bat_priv->tt.commit_lock);
248
249         hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
250                 hlist_del(&mcast_entry->list);
251                 kfree(mcast_entry);
252         }
253 }
254
255 /**
256  * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
257  * @bat_priv: the bat priv with all the soft interface information
258  * @mcast_list: a list of addresses which should _not_ be removed
259  *
260  * Retracts the announcement of any multicast listener from the
261  * translation table except the ones listed in the given mcast_list.
262  *
263  * If mcast_list is NULL then all are retracted.
264  */
265 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
266                                         struct hlist_head *mcast_list)
267 {
268         struct batadv_hw_addr *mcast_entry;
269         struct hlist_node *tmp;
270
271         lockdep_assert_held(&bat_priv->tt.commit_lock);
272
273         hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
274                                   list) {
275                 if (mcast_list &&
276                     batadv_mcast_mla_is_duplicate(mcast_entry->addr,
277                                                   mcast_list))
278                         continue;
279
280                 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
281                                        BATADV_NO_FLAGS,
282                                        "mcast TT outdated", false);
283
284                 hlist_del(&mcast_entry->list);
285                 kfree(mcast_entry);
286         }
287 }
288
289 /**
290  * batadv_mcast_mla_tt_add - add multicast listener announcements
291  * @bat_priv: the bat priv with all the soft interface information
292  * @mcast_list: a list of addresses which are going to get added
293  *
294  * Adds multicast listener announcements from the given mcast_list to the
295  * translation table if they have not been added yet.
296  */
297 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
298                                     struct hlist_head *mcast_list)
299 {
300         struct batadv_hw_addr *mcast_entry;
301         struct hlist_node *tmp;
302
303         lockdep_assert_held(&bat_priv->tt.commit_lock);
304
305         if (!mcast_list)
306                 return;
307
308         hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
309                 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
310                                                   &bat_priv->mcast.mla_list))
311                         continue;
312
313                 if (!batadv_tt_local_add(bat_priv->soft_iface,
314                                          mcast_entry->addr, BATADV_NO_FLAGS,
315                                          BATADV_NULL_IFINDEX, BATADV_NO_MARK))
316                         continue;
317
318                 hlist_del(&mcast_entry->list);
319                 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
320         }
321 }
322
323 /**
324  * batadv_mcast_has_bridge - check whether the soft-iface is bridged
325  * @bat_priv: the bat priv with all the soft interface information
326  *
327  * Checks whether there is a bridge on top of our soft interface.
328  *
329  * Return: true if there is a bridge, false otherwise.
330  */
331 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
332 {
333         struct net_device *upper = bat_priv->soft_iface;
334
335         rcu_read_lock();
336         do {
337                 upper = netdev_master_upper_dev_get_rcu(upper);
338         } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
339         rcu_read_unlock();
340
341         return upper;
342 }
343
344 /**
345  * batadv_mcast_querier_log - debug output regarding the querier status on link
346  * @bat_priv: the bat priv with all the soft interface information
347  * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
348  * @old_state: the previous querier state on our link
349  * @new_state: the new querier state on our link
350  *
351  * Outputs debug messages to the logging facility with log level 'mcast'
352  * regarding changes to the querier status on the link which are relevant
353  * to our multicast optimizations.
354  *
355  * Usually this is about whether a querier appeared or vanished in
356  * our mesh or whether the querier is in the suboptimal position of being
357  * behind our local bridge segment: Snooping switches will directly
358  * forward listener reports to the querier, therefore batman-adv and
359  * the bridge will potentially not see these listeners - the querier is
360  * potentially shadowing listeners from us then.
361  *
362  * This is only interesting for nodes with a bridge on top of their
363  * soft interface.
364  */
365 static void
366 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
367                          struct batadv_mcast_querier_state *old_state,
368                          struct batadv_mcast_querier_state *new_state)
369 {
370         if (!old_state->exists && new_state->exists)
371                 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
372                             str_proto);
373         else if (old_state->exists && !new_state->exists)
374                 batadv_info(bat_priv->soft_iface,
375                             "%s Querier disappeared - multicast optimizations disabled\n",
376                             str_proto);
377         else if (!bat_priv->mcast.bridged && !new_state->exists)
378                 batadv_info(bat_priv->soft_iface,
379                             "No %s Querier present - multicast optimizations disabled\n",
380                             str_proto);
381
382         if (new_state->exists) {
383                 if ((!old_state->shadowing && new_state->shadowing) ||
384                     (!old_state->exists && new_state->shadowing))
385                         batadv_dbg(BATADV_DBG_MCAST, bat_priv,
386                                    "%s Querier is behind our bridged segment: Might shadow listeners\n",
387                                    str_proto);
388                 else if (old_state->shadowing && !new_state->shadowing)
389                         batadv_dbg(BATADV_DBG_MCAST, bat_priv,
390                                    "%s Querier is not behind our bridged segment\n",
391                                    str_proto);
392         }
393 }
394
395 /**
396  * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
397  * @bat_priv: the bat priv with all the soft interface information
398  * @bridged: a flag about whether the soft interface is currently bridged or not
399  * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
400  * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
401  *
402  * If no bridges are ever used on this node, then this function does nothing.
403  *
404  * Otherwise this function outputs debug information to the 'mcast' log level
405  * which might be relevant to our multicast optimizations.
406  *
407  * More precisely, it outputs information when a bridge interface is added or
408  * removed from a soft interface. And when a bridge is present, it further
409  * outputs information about the querier state which is relevant for the
410  * multicast flags this node is going to set.
411  */
412 static void
413 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
414                         struct batadv_mcast_querier_state *querier_ipv4,
415                         struct batadv_mcast_querier_state *querier_ipv6)
416 {
417         if (!bat_priv->mcast.bridged && bridged)
418                 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
419                            "Bridge added: Setting Unsnoopables(U)-flag\n");
420         else if (bat_priv->mcast.bridged && !bridged)
421                 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
422                            "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
423
424         if (bridged) {
425                 batadv_mcast_querier_log(bat_priv, "IGMP",
426                                          &bat_priv->mcast.querier_ipv4,
427                                          querier_ipv4);
428                 batadv_mcast_querier_log(bat_priv, "MLD",
429                                          &bat_priv->mcast.querier_ipv6,
430                                          querier_ipv6);
431         }
432 }
433
434 /**
435  * batadv_mcast_flags_logs - output debug information about mcast flag changes
436  * @bat_priv: the bat priv with all the soft interface information
437  * @flags: flags indicating the new multicast state
438  *
439  * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
440  * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
441  */
442 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
443 {
444         u8 old_flags = bat_priv->mcast.flags;
445         char str_old_flags[] = "[...]";
446
447         sprintf(str_old_flags, "[%c%c%c]",
448                 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
449                 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
450                 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
451
452         batadv_dbg(BATADV_DBG_MCAST, bat_priv,
453                    "Changing multicast flags from '%s' to '[%c%c%c]'\n",
454                    bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
455                    (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
456                    (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
457                    (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
458 }
459
460 /**
461  * batadv_mcast_mla_tvlv_update - update multicast tvlv
462  * @bat_priv: the bat priv with all the soft interface information
463  *
464  * Updates the own multicast tvlv with our current multicast related settings,
465  * capabilities and inabilities.
466  *
467  * Return: false if we want all IPv4 && IPv6 multicast traffic and true
468  * otherwise.
469  */
470 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
471 {
472         struct batadv_tvlv_mcast_data mcast_data;
473         struct batadv_mcast_querier_state querier4 = {false, false};
474         struct batadv_mcast_querier_state querier6 = {false, false};
475         struct net_device *dev = bat_priv->soft_iface;
476         bool bridged;
477
478         mcast_data.flags = BATADV_NO_FLAGS;
479         memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
480
481         bridged = batadv_mcast_has_bridge(bat_priv);
482         if (!bridged)
483                 goto update;
484
485 #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
486         pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
487 #endif
488
489         querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
490         querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
491
492         querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
493         querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
494
495         mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
496
497         /* 1) If no querier exists at all, then multicast listeners on
498          *    our local TT clients behind the bridge will keep silent.
499          * 2) If the selected querier is on one of our local TT clients,
500          *    behind the bridge, then this querier might shadow multicast
501          *    listeners on our local TT clients, behind this bridge.
502          *
503          * In both cases, we will signalize other batman nodes that
504          * we need all multicast traffic of the according protocol.
505          */
506         if (!querier4.exists || querier4.shadowing)
507                 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
508
509         if (!querier6.exists || querier6.shadowing)
510                 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
511
512 update:
513         batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
514
515         bat_priv->mcast.querier_ipv4.exists = querier4.exists;
516         bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
517
518         bat_priv->mcast.querier_ipv6.exists = querier6.exists;
519         bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
520
521         bat_priv->mcast.bridged = bridged;
522
523         if (!bat_priv->mcast.enabled ||
524             mcast_data.flags != bat_priv->mcast.flags) {
525                 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
526                 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
527                                                &mcast_data, sizeof(mcast_data));
528                 bat_priv->mcast.flags = mcast_data.flags;
529                 bat_priv->mcast.enabled = true;
530         }
531
532         return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
533                  mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
534 }
535
536 /**
537  * batadv_mcast_mla_update - update the own MLAs
538  * @bat_priv: the bat priv with all the soft interface information
539  *
540  * Updates the own multicast listener announcements in the translation
541  * table as well as the own, announced multicast tvlv container.
542  */
543 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
544 {
545         struct net_device *soft_iface = bat_priv->soft_iface;
546         struct hlist_head mcast_list = HLIST_HEAD_INIT;
547         int ret;
548
549         if (!batadv_mcast_mla_tvlv_update(bat_priv))
550                 goto update;
551
552         ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
553         if (ret < 0)
554                 goto out;
555
556         ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
557         if (ret < 0)
558                 goto out;
559
560 update:
561         batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
562         batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
563
564 out:
565         batadv_mcast_mla_list_free(bat_priv, &mcast_list);
566 }
567
568 /**
569  * batadv_mcast_is_report_ipv4 - check for IGMP reports
570  * @skb: the ethernet frame destined for the mesh
571  *
572  * This call might reallocate skb data.
573  *
574  * Checks whether the given frame is a valid IGMP report.
575  *
576  * Return: If so then true, otherwise false.
577  */
578 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
579 {
580         if (ip_mc_check_igmp(skb, NULL) < 0)
581                 return false;
582
583         switch (igmp_hdr(skb)->type) {
584         case IGMP_HOST_MEMBERSHIP_REPORT:
585         case IGMPV2_HOST_MEMBERSHIP_REPORT:
586         case IGMPV3_HOST_MEMBERSHIP_REPORT:
587                 return true;
588         }
589
590         return false;
591 }
592
593 /**
594  * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
595  * @bat_priv: the bat priv with all the soft interface information
596  * @skb: the IPv4 packet to check
597  * @is_unsnoopable: stores whether the destination is snoopable
598  *
599  * Checks whether the given IPv4 packet has the potential to be forwarded with a
600  * mode more optimal than classic flooding.
601  *
602  * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
603  * allocation failure.
604  */
605 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
606                                              struct sk_buff *skb,
607                                              bool *is_unsnoopable)
608 {
609         struct iphdr *iphdr;
610
611         /* We might fail due to out-of-memory -> drop it */
612         if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
613                 return -ENOMEM;
614
615         if (batadv_mcast_is_report_ipv4(skb))
616                 return -EINVAL;
617
618         iphdr = ip_hdr(skb);
619
620         /* TODO: Implement Multicast Router Discovery (RFC4286),
621          * then allow scope > link local, too
622          */
623         if (!ipv4_is_local_multicast(iphdr->daddr))
624                 return -EINVAL;
625
626         /* link-local multicast listeners behind a bridge are
627          * not snoopable (see RFC4541, section 2.1.2.2)
628          */
629         *is_unsnoopable = true;
630
631         return 0;
632 }
633
634 #if IS_ENABLED(CONFIG_IPV6)
635 /**
636  * batadv_mcast_is_report_ipv6 - check for MLD reports
637  * @skb: the ethernet frame destined for the mesh
638  *
639  * This call might reallocate skb data.
640  *
641  * Checks whether the given frame is a valid MLD report.
642  *
643  * Return: If so then true, otherwise false.
644  */
645 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
646 {
647         if (ipv6_mc_check_mld(skb, NULL) < 0)
648                 return false;
649
650         switch (icmp6_hdr(skb)->icmp6_type) {
651         case ICMPV6_MGM_REPORT:
652         case ICMPV6_MLD2_REPORT:
653                 return true;
654         }
655
656         return false;
657 }
658
659 /**
660  * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
661  * @bat_priv: the bat priv with all the soft interface information
662  * @skb: the IPv6 packet to check
663  * @is_unsnoopable: stores whether the destination is snoopable
664  *
665  * Checks whether the given IPv6 packet has the potential to be forwarded with a
666  * mode more optimal than classic flooding.
667  *
668  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
669  */
670 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
671                                              struct sk_buff *skb,
672                                              bool *is_unsnoopable)
673 {
674         struct ipv6hdr *ip6hdr;
675
676         /* We might fail due to out-of-memory -> drop it */
677         if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
678                 return -ENOMEM;
679
680         if (batadv_mcast_is_report_ipv6(skb))
681                 return -EINVAL;
682
683         ip6hdr = ipv6_hdr(skb);
684
685         /* TODO: Implement Multicast Router Discovery (RFC4286),
686          * then allow scope > link local, too
687          */
688         if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
689                 return -EINVAL;
690
691         /* link-local-all-nodes multicast listeners behind a bridge are
692          * not snoopable (see RFC4541, section 3, paragraph 3)
693          */
694         if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
695                 *is_unsnoopable = true;
696
697         return 0;
698 }
699 #endif
700
701 /**
702  * batadv_mcast_forw_mode_check - check for optimized forwarding potential
703  * @bat_priv: the bat priv with all the soft interface information
704  * @skb: the multicast frame to check
705  * @is_unsnoopable: stores whether the destination is snoopable
706  *
707  * Checks whether the given multicast ethernet frame has the potential to be
708  * forwarded with a mode more optimal than classic flooding.
709  *
710  * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
711  */
712 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
713                                         struct sk_buff *skb,
714                                         bool *is_unsnoopable)
715 {
716         struct ethhdr *ethhdr = eth_hdr(skb);
717
718         if (!atomic_read(&bat_priv->multicast_mode))
719                 return -EINVAL;
720
721         if (atomic_read(&bat_priv->mcast.num_disabled))
722                 return -EINVAL;
723
724         switch (ntohs(ethhdr->h_proto)) {
725         case ETH_P_IP:
726                 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
727                                                          is_unsnoopable);
728 #if IS_ENABLED(CONFIG_IPV6)
729         case ETH_P_IPV6:
730                 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
731                                                          is_unsnoopable);
732 #endif
733         default:
734                 return -EINVAL;
735         }
736 }
737
738 /**
739  * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
740  *  interest
741  * @bat_priv: the bat priv with all the soft interface information
742  * @ethhdr: ethernet header of a packet
743  *
744  * Return: the number of nodes which want all IPv4 multicast traffic if the
745  * given ethhdr is from an IPv4 packet or the number of nodes which want all
746  * IPv6 traffic if it matches an IPv6 packet.
747  */
748 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
749                                                struct ethhdr *ethhdr)
750 {
751         switch (ntohs(ethhdr->h_proto)) {
752         case ETH_P_IP:
753                 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
754         case ETH_P_IPV6:
755                 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
756         default:
757                 /* we shouldn't be here... */
758                 return 0;
759         }
760 }
761
762 /**
763  * batadv_mcast_forw_tt_node_get - get a multicast tt node
764  * @bat_priv: the bat priv with all the soft interface information
765  * @ethhdr: the ether header containing the multicast destination
766  *
767  * Return: an orig_node matching the multicast address provided by ethhdr
768  * via a translation table lookup. This increases the returned nodes refcount.
769  */
770 static struct batadv_orig_node *
771 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
772                               struct ethhdr *ethhdr)
773 {
774         return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
775                                         BATADV_NO_FLAGS);
776 }
777
778 /**
779  * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
780  * @bat_priv: the bat priv with all the soft interface information
781  *
782  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
783  * increases its refcount.
784  */
785 static struct batadv_orig_node *
786 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
787 {
788         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
789
790         rcu_read_lock();
791         hlist_for_each_entry_rcu(tmp_orig_node,
792                                  &bat_priv->mcast.want_all_ipv4_list,
793                                  mcast_want_all_ipv4_node) {
794                 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
795                         continue;
796
797                 orig_node = tmp_orig_node;
798                 break;
799         }
800         rcu_read_unlock();
801
802         return orig_node;
803 }
804
805 /**
806  * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
807  * @bat_priv: the bat priv with all the soft interface information
808  *
809  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
810  * and increases its refcount.
811  */
812 static struct batadv_orig_node *
813 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
814 {
815         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
816
817         rcu_read_lock();
818         hlist_for_each_entry_rcu(tmp_orig_node,
819                                  &bat_priv->mcast.want_all_ipv6_list,
820                                  mcast_want_all_ipv6_node) {
821                 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
822                         continue;
823
824                 orig_node = tmp_orig_node;
825                 break;
826         }
827         rcu_read_unlock();
828
829         return orig_node;
830 }
831
832 /**
833  * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
834  * @bat_priv: the bat priv with all the soft interface information
835  * @ethhdr: an ethernet header to determine the protocol family from
836  *
837  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
838  * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
839  * increases its refcount.
840  */
841 static struct batadv_orig_node *
842 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
843                               struct ethhdr *ethhdr)
844 {
845         switch (ntohs(ethhdr->h_proto)) {
846         case ETH_P_IP:
847                 return batadv_mcast_forw_ipv4_node_get(bat_priv);
848         case ETH_P_IPV6:
849                 return batadv_mcast_forw_ipv6_node_get(bat_priv);
850         default:
851                 /* we shouldn't be here... */
852                 return NULL;
853         }
854 }
855
856 /**
857  * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
858  * @bat_priv: the bat priv with all the soft interface information
859  *
860  * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
861  * set and increases its refcount.
862  */
863 static struct batadv_orig_node *
864 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
865 {
866         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
867
868         rcu_read_lock();
869         hlist_for_each_entry_rcu(tmp_orig_node,
870                                  &bat_priv->mcast.want_all_unsnoopables_list,
871                                  mcast_want_all_unsnoopables_node) {
872                 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
873                         continue;
874
875                 orig_node = tmp_orig_node;
876                 break;
877         }
878         rcu_read_unlock();
879
880         return orig_node;
881 }
882
883 /**
884  * batadv_mcast_forw_mode - check on how to forward a multicast packet
885  * @bat_priv: the bat priv with all the soft interface information
886  * @skb: The multicast packet to check
887  * @orig: an originator to be set to forward the skb to
888  *
889  * Return: the forwarding mode as enum batadv_forw_mode and in case of
890  * BATADV_FORW_SINGLE set the orig to the single originator the skb
891  * should be forwarded to.
892  */
893 enum batadv_forw_mode
894 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
895                        struct batadv_orig_node **orig)
896 {
897         int ret, tt_count, ip_count, unsnoop_count, total_count;
898         bool is_unsnoopable = false;
899         struct ethhdr *ethhdr;
900
901         ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
902         if (ret == -ENOMEM)
903                 return BATADV_FORW_NONE;
904         else if (ret < 0)
905                 return BATADV_FORW_ALL;
906
907         ethhdr = eth_hdr(skb);
908
909         tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
910                                                BATADV_NO_FLAGS);
911         ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
912         unsnoop_count = !is_unsnoopable ? 0 :
913                         atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
914
915         total_count = tt_count + ip_count + unsnoop_count;
916
917         switch (total_count) {
918         case 1:
919                 if (tt_count)
920                         *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
921                 else if (ip_count)
922                         *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
923                 else if (unsnoop_count)
924                         *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
925
926                 if (*orig)
927                         return BATADV_FORW_SINGLE;
928
929                 /* fall through */
930         case 0:
931                 return BATADV_FORW_NONE;
932         default:
933                 return BATADV_FORW_ALL;
934         }
935 }
936
937 /**
938  * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
939  * @bat_priv: the bat priv with all the soft interface information
940  * @orig: the orig_node which multicast state might have changed of
941  * @mcast_flags: flags indicating the new multicast state
942  *
943  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
944  * orig, has toggled then this method updates counter and list accordingly.
945  *
946  * Caller needs to hold orig->mcast_handler_lock.
947  */
948 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
949                                              struct batadv_orig_node *orig,
950                                              u8 mcast_flags)
951 {
952         struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
953         struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
954
955         lockdep_assert_held(&orig->mcast_handler_lock);
956
957         /* switched from flag unset to set */
958         if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
959             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
960                 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
961
962                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
963                 /* flag checks above + mcast_handler_lock prevents this */
964                 WARN_ON(!hlist_unhashed(node));
965
966                 hlist_add_head_rcu(node, head);
967                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
968         /* switched from flag set to unset */
969         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
970                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
971                 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
972
973                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
974                 /* flag checks above + mcast_handler_lock prevents this */
975                 WARN_ON(hlist_unhashed(node));
976
977                 hlist_del_init_rcu(node);
978                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
979         }
980 }
981
982 /**
983  * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
984  * @bat_priv: the bat priv with all the soft interface information
985  * @orig: the orig_node which multicast state might have changed of
986  * @mcast_flags: flags indicating the new multicast state
987  *
988  * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
989  * toggled then this method updates counter and list accordingly.
990  *
991  * Caller needs to hold orig->mcast_handler_lock.
992  */
993 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
994                                           struct batadv_orig_node *orig,
995                                           u8 mcast_flags)
996 {
997         struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
998         struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
999
1000         lockdep_assert_held(&orig->mcast_handler_lock);
1001
1002         /* switched from flag unset to set */
1003         if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1004             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1005                 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1006
1007                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1008                 /* flag checks above + mcast_handler_lock prevents this */
1009                 WARN_ON(!hlist_unhashed(node));
1010
1011                 hlist_add_head_rcu(node, head);
1012                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1013         /* switched from flag set to unset */
1014         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1015                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1016                 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1017
1018                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1019                 /* flag checks above + mcast_handler_lock prevents this */
1020                 WARN_ON(hlist_unhashed(node));
1021
1022                 hlist_del_init_rcu(node);
1023                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1024         }
1025 }
1026
1027 /**
1028  * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1029  * @bat_priv: the bat priv with all the soft interface information
1030  * @orig: the orig_node which multicast state might have changed of
1031  * @mcast_flags: flags indicating the new multicast state
1032  *
1033  * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1034  * toggled then this method updates counter and list accordingly.
1035  *
1036  * Caller needs to hold orig->mcast_handler_lock.
1037  */
1038 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1039                                           struct batadv_orig_node *orig,
1040                                           u8 mcast_flags)
1041 {
1042         struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1043         struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1044
1045         lockdep_assert_held(&orig->mcast_handler_lock);
1046
1047         /* switched from flag unset to set */
1048         if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1049             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1050                 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1051
1052                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1053                 /* flag checks above + mcast_handler_lock prevents this */
1054                 WARN_ON(!hlist_unhashed(node));
1055
1056                 hlist_add_head_rcu(node, head);
1057                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1058         /* switched from flag set to unset */
1059         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1060                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1061                 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1062
1063                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1064                 /* flag checks above + mcast_handler_lock prevents this */
1065                 WARN_ON(hlist_unhashed(node));
1066
1067                 hlist_del_init_rcu(node);
1068                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1069         }
1070 }
1071
1072 /**
1073  * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
1074  * @bat_priv: the bat priv with all the soft interface information
1075  * @orig: the orig_node of the ogm
1076  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1077  * @tvlv_value: tvlv buffer containing the multicast data
1078  * @tvlv_value_len: tvlv buffer length
1079  */
1080 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1081                                           struct batadv_orig_node *orig,
1082                                           u8 flags,
1083                                           void *tvlv_value,
1084                                           u16 tvlv_value_len)
1085 {
1086         bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1087         u8 mcast_flags = BATADV_NO_FLAGS;
1088         bool orig_initialized;
1089
1090         if (orig_mcast_enabled && tvlv_value &&
1091             (tvlv_value_len >= sizeof(mcast_flags)))
1092                 mcast_flags = *(u8 *)tvlv_value;
1093
1094         spin_lock_bh(&orig->mcast_handler_lock);
1095         orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1096                                     &orig->capa_initialized);
1097
1098         /* If mcast support is turned on decrease the disabled mcast node
1099          * counter only if we had increased it for this node before. If this
1100          * is a completely new orig_node no need to decrease the counter.
1101          */
1102         if (orig_mcast_enabled &&
1103             !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
1104                 if (orig_initialized)
1105                         atomic_dec(&bat_priv->mcast.num_disabled);
1106                 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1107         /* If mcast support is being switched off or if this is an initial
1108          * OGM without mcast support then increase the disabled mcast
1109          * node counter.
1110          */
1111         } else if (!orig_mcast_enabled &&
1112                    (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
1113                     !orig_initialized)) {
1114                 atomic_inc(&bat_priv->mcast.num_disabled);
1115                 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
1116         }
1117
1118         set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
1119
1120         batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
1121         batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1122         batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
1123
1124         orig->mcast_flags = mcast_flags;
1125         spin_unlock_bh(&orig->mcast_handler_lock);
1126 }
1127
1128 /**
1129  * batadv_mcast_init - initialize the multicast optimizations structures
1130  * @bat_priv: the bat priv with all the soft interface information
1131  */
1132 void batadv_mcast_init(struct batadv_priv *bat_priv)
1133 {
1134         batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1135                                      NULL, BATADV_TVLV_MCAST, 2,
1136                                      BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1137 }
1138
1139 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1140 /**
1141  * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1142  * @bat_priv: the bat priv with all the soft interface information
1143  * @seq: debugfs table seq_file struct
1144  *
1145  * Prints our own multicast flags including a more specific reason why
1146  * they are set, that is prints the bridge and querier state too, to
1147  * the debugfs table specified via @seq.
1148  */
1149 static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1150                                             struct seq_file *seq)
1151 {
1152         u8 flags = bat_priv->mcast.flags;
1153         char querier4, querier6, shadowing4, shadowing6;
1154         bool bridged = bat_priv->mcast.bridged;
1155
1156         if (bridged) {
1157                 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1158                 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1159                 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1160                 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1161         } else {
1162                 querier4 = '?';
1163                 querier6 = '?';
1164                 shadowing4 = '?';
1165                 shadowing6 = '?';
1166         }
1167
1168         seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1169                    (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1170                    (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1171                    (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1172         seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1173         seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1174                    querier4, querier6);
1175         seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1176                    shadowing4, shadowing6);
1177         seq_puts(seq, "-------------------------------------------\n");
1178         seq_printf(seq, "       %-10s %s\n", "Originator", "Flags");
1179 }
1180
1181 /**
1182  * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1183  * @seq: seq file to print on
1184  * @offset: not used
1185  *
1186  * This prints a table of (primary) originators and their according
1187  * multicast flags, including (in the header) our own.
1188  *
1189  * Return: always 0
1190  */
1191 int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1192 {
1193         struct net_device *net_dev = (struct net_device *)seq->private;
1194         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1195         struct batadv_hard_iface *primary_if;
1196         struct batadv_hashtable *hash = bat_priv->orig_hash;
1197         struct batadv_orig_node *orig_node;
1198         struct hlist_head *head;
1199         u8 flags;
1200         u32 i;
1201
1202         primary_if = batadv_seq_print_text_primary_if_get(seq);
1203         if (!primary_if)
1204                 return 0;
1205
1206         batadv_mcast_flags_print_header(bat_priv, seq);
1207
1208         for (i = 0; i < hash->size; i++) {
1209                 head = &hash->table[i];
1210
1211                 rcu_read_lock();
1212                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1213                         if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1214                                       &orig_node->capa_initialized))
1215                                 continue;
1216
1217                         if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1218                                       &orig_node->capabilities)) {
1219                                 seq_printf(seq, "%pM -\n", orig_node->orig);
1220                                 continue;
1221                         }
1222
1223                         flags = orig_node->mcast_flags;
1224
1225                         seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1226                                    (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1227                                    ? 'U' : '.',
1228                                    (flags & BATADV_MCAST_WANT_ALL_IPV4)
1229                                    ? '4' : '.',
1230                                    (flags & BATADV_MCAST_WANT_ALL_IPV6)
1231                                    ? '6' : '.');
1232                 }
1233                 rcu_read_unlock();
1234         }
1235
1236         batadv_hardif_put(primary_if);
1237
1238         return 0;
1239 }
1240 #endif
1241
1242 /**
1243  * batadv_mcast_free - free the multicast optimizations structures
1244  * @bat_priv: the bat priv with all the soft interface information
1245  */
1246 void batadv_mcast_free(struct batadv_priv *bat_priv)
1247 {
1248         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1249         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1250
1251         spin_lock_bh(&bat_priv->tt.commit_lock);
1252         batadv_mcast_mla_tt_retract(bat_priv, NULL);
1253         spin_unlock_bh(&bat_priv->tt.commit_lock);
1254 }
1255
1256 /**
1257  * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
1258  * @bat_priv: the bat priv with all the soft interface information
1259  * @skb: the multicast packet to send
1260  * @vid: the vlan identifier
1261  * @orig_node: the originator to send the packet to
1262  *
1263  * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
1264  */
1265 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
1266                                 struct sk_buff *skb,
1267                                 unsigned short vid,
1268                                 struct batadv_orig_node *orig_node)
1269 {
1270         /* Avoid sending multicast-in-unicast packets to other BLA
1271          * gateways - they already got the frame from the LAN side
1272          * we share with them.
1273          * TODO: Refactor to take BLA into account earlier, to avoid
1274          * reducing the mcast_fanout count.
1275          */
1276         if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
1277                 dev_kfree_skb(skb);
1278                 return NET_XMIT_SUCCESS;
1279         }
1280
1281         return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
1282                                        orig_node, vid);
1283 }
1284
1285 /**
1286  * batadv_mcast_purge_orig - reset originator global mcast state modifications
1287  * @orig: the originator which is going to get purged
1288  */
1289 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1290 {
1291         struct batadv_priv *bat_priv = orig->bat_priv;
1292
1293         spin_lock_bh(&orig->mcast_handler_lock);
1294
1295         if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1296             test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
1297                 atomic_dec(&bat_priv->mcast.num_disabled);
1298
1299         batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
1300         batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1301         batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
1302
1303         spin_unlock_bh(&orig->mcast_handler_lock);
1304 }