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