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