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