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