GNU Linux-libre 4.4.294-gnu1
[releases.git] / net / batman-adv / multicast.c
1 /* Copyright (C) 2014-2015 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/if_ether.h>
29 #include <linux/in6.h>
30 #include <linux/in.h>
31 #include <linux/ip.h>
32 #include <linux/ipv6.h>
33 #include <linux/list.h>
34 #include <linux/lockdep.h>
35 #include <linux/netdevice.h>
36 #include <linux/rculist.h>
37 #include <linux/rcupdate.h>
38 #include <linux/skbuff.h>
39 #include <linux/slab.h>
40 #include <linux/spinlock.h>
41 #include <linux/stddef.h>
42 #include <linux/string.h>
43 #include <linux/types.h>
44 #include <net/addrconf.h>
45 #include <net/ipv6.h>
46
47 #include "bridge_loop_avoidance.h"
48 #include "packet.h"
49 #include "send.h"
50 #include "translation-table.h"
51
52 /**
53  * batadv_mcast_mla_softif_get - get softif multicast listeners
54  * @dev: the device to collect multicast addresses from
55  * @mcast_list: a list to put found addresses into
56  *
57  * Collect multicast addresses of the local multicast listeners
58  * on the given soft interface, dev, in the given mcast_list.
59  *
60  * Returns -ENOMEM on memory allocation error or the number of
61  * items added to the mcast_list otherwise.
62  */
63 static int batadv_mcast_mla_softif_get(struct net_device *dev,
64                                        struct hlist_head *mcast_list)
65 {
66         struct netdev_hw_addr *mc_list_entry;
67         struct batadv_hw_addr *new;
68         int ret = 0;
69
70         netif_addr_lock_bh(dev);
71         netdev_for_each_mc_addr(mc_list_entry, dev) {
72                 new = kmalloc(sizeof(*new), GFP_ATOMIC);
73                 if (!new) {
74                         ret = -ENOMEM;
75                         break;
76                 }
77
78                 ether_addr_copy(new->addr, mc_list_entry->addr);
79                 hlist_add_head(&new->list, mcast_list);
80                 ret++;
81         }
82         netif_addr_unlock_bh(dev);
83
84         return ret;
85 }
86
87 /**
88  * batadv_mcast_mla_is_duplicate - check whether an address is in a list
89  * @mcast_addr: the multicast address to check
90  * @mcast_list: the list with multicast addresses to search in
91  *
92  * Returns true if the given address is already in the given list.
93  * Otherwise returns false.
94  */
95 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
96                                           struct hlist_head *mcast_list)
97 {
98         struct batadv_hw_addr *mcast_entry;
99
100         hlist_for_each_entry(mcast_entry, mcast_list, list)
101                 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
102                         return true;
103
104         return false;
105 }
106
107 /**
108  * batadv_mcast_mla_list_free - free a list of multicast addresses
109  * @bat_priv: the bat priv with all the soft interface information
110  * @mcast_list: the list to free
111  *
112  * Removes and frees all items in the given mcast_list.
113  */
114 static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
115                                        struct hlist_head *mcast_list)
116 {
117         struct batadv_hw_addr *mcast_entry;
118         struct hlist_node *tmp;
119
120         lockdep_assert_held(&bat_priv->tt.commit_lock);
121
122         hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
123                 hlist_del(&mcast_entry->list);
124                 kfree(mcast_entry);
125         }
126 }
127
128 /**
129  * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
130  * @bat_priv: the bat priv with all the soft interface information
131  * @mcast_list: a list of addresses which should _not_ be removed
132  *
133  * Retracts the announcement of any multicast listener from the
134  * translation table except the ones listed in the given mcast_list.
135  *
136  * If mcast_list is NULL then all are retracted.
137  */
138 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
139                                         struct hlist_head *mcast_list)
140 {
141         struct batadv_hw_addr *mcast_entry;
142         struct hlist_node *tmp;
143
144         lockdep_assert_held(&bat_priv->tt.commit_lock);
145
146         hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
147                                   list) {
148                 if (mcast_list &&
149                     batadv_mcast_mla_is_duplicate(mcast_entry->addr,
150                                                   mcast_list))
151                         continue;
152
153                 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
154                                        BATADV_NO_FLAGS,
155                                        "mcast TT outdated", false);
156
157                 hlist_del(&mcast_entry->list);
158                 kfree(mcast_entry);
159         }
160 }
161
162 /**
163  * batadv_mcast_mla_tt_add - add multicast listener announcements
164  * @bat_priv: the bat priv with all the soft interface information
165  * @mcast_list: a list of addresses which are going to get added
166  *
167  * Adds multicast listener announcements from the given mcast_list to the
168  * translation table if they have not been added yet.
169  */
170 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
171                                     struct hlist_head *mcast_list)
172 {
173         struct batadv_hw_addr *mcast_entry;
174         struct hlist_node *tmp;
175
176         lockdep_assert_held(&bat_priv->tt.commit_lock);
177
178         if (!mcast_list)
179                 return;
180
181         hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
182                 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
183                                                   &bat_priv->mcast.mla_list))
184                         continue;
185
186                 if (!batadv_tt_local_add(bat_priv->soft_iface,
187                                          mcast_entry->addr, BATADV_NO_FLAGS,
188                                          BATADV_NULL_IFINDEX, BATADV_NO_MARK))
189                         continue;
190
191                 hlist_del(&mcast_entry->list);
192                 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
193         }
194 }
195
196 /**
197  * batadv_mcast_has_bridge - check whether the soft-iface is bridged
198  * @bat_priv: the bat priv with all the soft interface information
199  *
200  * Checks whether there is a bridge on top of our soft interface. Returns
201  * true if so, false otherwise.
202  */
203 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
204 {
205         struct net_device *upper = bat_priv->soft_iface;
206
207         rcu_read_lock();
208         do {
209                 upper = netdev_master_upper_dev_get_rcu(upper);
210         } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
211         rcu_read_unlock();
212
213         return upper;
214 }
215
216 /**
217  * batadv_mcast_mla_tvlv_update - update multicast tvlv
218  * @bat_priv: the bat priv with all the soft interface information
219  *
220  * Updates the own multicast tvlv with our current multicast related settings,
221  * capabilities and inabilities.
222  *
223  * Returns true if the tvlv container is registered afterwards. Otherwise
224  * returns false.
225  */
226 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
227 {
228         struct batadv_tvlv_mcast_data mcast_data;
229
230         mcast_data.flags = BATADV_NO_FLAGS;
231         memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
232
233         /* Avoid attaching MLAs, if there is a bridge on top of our soft
234          * interface, we don't support that yet (TODO)
235          */
236         if (batadv_mcast_has_bridge(bat_priv)) {
237                 if (bat_priv->mcast.enabled) {
238                         batadv_tvlv_container_unregister(bat_priv,
239                                                          BATADV_TVLV_MCAST, 1);
240                         bat_priv->mcast.enabled = false;
241                 }
242
243                 return false;
244         }
245
246         if (!bat_priv->mcast.enabled ||
247             mcast_data.flags != bat_priv->mcast.flags) {
248                 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
249                                                &mcast_data, sizeof(mcast_data));
250                 bat_priv->mcast.flags = mcast_data.flags;
251                 bat_priv->mcast.enabled = true;
252         }
253
254         return true;
255 }
256
257 /**
258  * batadv_mcast_mla_update - update the own MLAs
259  * @bat_priv: the bat priv with all the soft interface information
260  *
261  * Updates the own multicast listener announcements in the translation
262  * table as well as the own, announced multicast tvlv container.
263  */
264 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
265 {
266         struct net_device *soft_iface = bat_priv->soft_iface;
267         struct hlist_head mcast_list = HLIST_HEAD_INIT;
268         int ret;
269
270         if (!batadv_mcast_mla_tvlv_update(bat_priv))
271                 goto update;
272
273         ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
274         if (ret < 0)
275                 goto out;
276
277 update:
278         batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
279         batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
280
281 out:
282         batadv_mcast_mla_list_free(bat_priv, &mcast_list);
283 }
284
285 /**
286  * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
287  * @bat_priv: the bat priv with all the soft interface information
288  * @skb: the IPv4 packet to check
289  * @is_unsnoopable: stores whether the destination is snoopable
290  *
291  * Checks whether the given IPv4 packet has the potential to be forwarded with a
292  * mode more optimal than classic flooding.
293  *
294  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM in case of
295  * memory allocation failure.
296  */
297 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
298                                              struct sk_buff *skb,
299                                              bool *is_unsnoopable)
300 {
301         struct iphdr *iphdr;
302
303         /* We might fail due to out-of-memory -> drop it */
304         if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
305                 return -ENOMEM;
306
307         iphdr = ip_hdr(skb);
308
309         /* TODO: Implement Multicast Router Discovery (RFC4286),
310          * then allow scope > link local, too
311          */
312         if (!ipv4_is_local_multicast(iphdr->daddr))
313                 return -EINVAL;
314
315         /* link-local multicast listeners behind a bridge are
316          * not snoopable (see RFC4541, section 2.1.2.2)
317          */
318         *is_unsnoopable = true;
319
320         return 0;
321 }
322
323 /**
324  * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
325  * @bat_priv: the bat priv with all the soft interface information
326  * @skb: the IPv6 packet to check
327  * @is_unsnoopable: stores whether the destination is snoopable
328  *
329  * Checks whether the given IPv6 packet has the potential to be forwarded with a
330  * mode more optimal than classic flooding.
331  *
332  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
333  * of memory.
334  */
335 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
336                                              struct sk_buff *skb,
337                                              bool *is_unsnoopable)
338 {
339         struct ipv6hdr *ip6hdr;
340
341         /* We might fail due to out-of-memory -> drop it */
342         if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
343                 return -ENOMEM;
344
345         ip6hdr = ipv6_hdr(skb);
346
347         /* TODO: Implement Multicast Router Discovery (RFC4286),
348          * then allow scope > link local, too
349          */
350         if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
351                 return -EINVAL;
352
353         /* link-local-all-nodes multicast listeners behind a bridge are
354          * not snoopable (see RFC4541, section 3, paragraph 3)
355          */
356         if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
357                 *is_unsnoopable = true;
358
359         return 0;
360 }
361
362 /**
363  * batadv_mcast_forw_mode_check - check for optimized forwarding potential
364  * @bat_priv: the bat priv with all the soft interface information
365  * @skb: the multicast frame to check
366  * @is_unsnoopable: stores whether the destination is snoopable
367  *
368  * Checks whether the given multicast ethernet frame has the potential to be
369  * forwarded with a mode more optimal than classic flooding.
370  *
371  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
372  * of memory.
373  */
374 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
375                                         struct sk_buff *skb,
376                                         bool *is_unsnoopable)
377 {
378         struct ethhdr *ethhdr = eth_hdr(skb);
379
380         if (!atomic_read(&bat_priv->multicast_mode))
381                 return -EINVAL;
382
383         if (atomic_read(&bat_priv->mcast.num_disabled))
384                 return -EINVAL;
385
386         switch (ntohs(ethhdr->h_proto)) {
387         case ETH_P_IP:
388                 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
389                                                          is_unsnoopable);
390         case ETH_P_IPV6:
391                 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
392                                                          is_unsnoopable);
393         default:
394                 return -EINVAL;
395         }
396 }
397
398 /**
399  * batadv_mcast_want_all_ip_count - count nodes with unspecific mcast interest
400  * @bat_priv: the bat priv with all the soft interface information
401  * @ethhdr: ethernet header of a packet
402  *
403  * Returns the number of nodes which want all IPv4 multicast traffic if the
404  * given ethhdr is from an IPv4 packet or the number of nodes which want all
405  * IPv6 traffic if it matches an IPv6 packet.
406  */
407 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
408                                                struct ethhdr *ethhdr)
409 {
410         switch (ntohs(ethhdr->h_proto)) {
411         case ETH_P_IP:
412                 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
413         case ETH_P_IPV6:
414                 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
415         default:
416                 /* we shouldn't be here... */
417                 return 0;
418         }
419 }
420
421 /**
422  * batadv_mcast_forw_tt_node_get - get a multicast tt node
423  * @bat_priv: the bat priv with all the soft interface information
424  * @ethhdr: the ether header containing the multicast destination
425  *
426  * Returns an orig_node matching the multicast address provided by ethhdr
427  * via a translation table lookup. This increases the returned nodes refcount.
428  */
429 static struct batadv_orig_node *
430 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
431                               struct ethhdr *ethhdr)
432 {
433         return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
434                                         BATADV_NO_FLAGS);
435 }
436
437 /**
438  * batadv_mcast_want_forw_ipv4_node_get - get a node with an ipv4 flag
439  * @bat_priv: the bat priv with all the soft interface information
440  *
441  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
442  * increases its refcount.
443  */
444 static struct batadv_orig_node *
445 batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
446 {
447         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
448
449         rcu_read_lock();
450         hlist_for_each_entry_rcu(tmp_orig_node,
451                                  &bat_priv->mcast.want_all_ipv4_list,
452                                  mcast_want_all_ipv4_node) {
453                 if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
454                         continue;
455
456                 orig_node = tmp_orig_node;
457                 break;
458         }
459         rcu_read_unlock();
460
461         return orig_node;
462 }
463
464 /**
465  * batadv_mcast_want_forw_ipv6_node_get - get a node with an ipv6 flag
466  * @bat_priv: the bat priv with all the soft interface information
467  *
468  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
469  * and increases its refcount.
470  */
471 static struct batadv_orig_node *
472 batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
473 {
474         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
475
476         rcu_read_lock();
477         hlist_for_each_entry_rcu(tmp_orig_node,
478                                  &bat_priv->mcast.want_all_ipv6_list,
479                                  mcast_want_all_ipv6_node) {
480                 if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
481                         continue;
482
483                 orig_node = tmp_orig_node;
484                 break;
485         }
486         rcu_read_unlock();
487
488         return orig_node;
489 }
490
491 /**
492  * batadv_mcast_want_forw_ip_node_get - get a node with an ipv4/ipv6 flag
493  * @bat_priv: the bat priv with all the soft interface information
494  * @ethhdr: an ethernet header to determine the protocol family from
495  *
496  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
497  * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
498  * increases its refcount.
499  */
500 static struct batadv_orig_node *
501 batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
502                               struct ethhdr *ethhdr)
503 {
504         switch (ntohs(ethhdr->h_proto)) {
505         case ETH_P_IP:
506                 return batadv_mcast_forw_ipv4_node_get(bat_priv);
507         case ETH_P_IPV6:
508                 return batadv_mcast_forw_ipv6_node_get(bat_priv);
509         default:
510                 /* we shouldn't be here... */
511                 return NULL;
512         }
513 }
514
515 /**
516  * batadv_mcast_want_forw_unsnoop_node_get - get a node with an unsnoopable flag
517  * @bat_priv: the bat priv with all the soft interface information
518  *
519  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
520  * set and increases its refcount.
521  */
522 static struct batadv_orig_node *
523 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
524 {
525         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
526
527         rcu_read_lock();
528         hlist_for_each_entry_rcu(tmp_orig_node,
529                                  &bat_priv->mcast.want_all_unsnoopables_list,
530                                  mcast_want_all_unsnoopables_node) {
531                 if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
532                         continue;
533
534                 orig_node = tmp_orig_node;
535                 break;
536         }
537         rcu_read_unlock();
538
539         return orig_node;
540 }
541
542 /**
543  * batadv_mcast_forw_mode - check on how to forward a multicast packet
544  * @bat_priv: the bat priv with all the soft interface information
545  * @skb: The multicast packet to check
546  * @orig: an originator to be set to forward the skb to
547  *
548  * Returns the forwarding mode as enum batadv_forw_mode and in case of
549  * BATADV_FORW_SINGLE set the orig to the single originator the skb
550  * should be forwarded to.
551  */
552 enum batadv_forw_mode
553 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
554                        struct batadv_orig_node **orig)
555 {
556         int ret, tt_count, ip_count, unsnoop_count, total_count;
557         bool is_unsnoopable = false;
558         struct ethhdr *ethhdr;
559
560         ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
561         if (ret == -ENOMEM)
562                 return BATADV_FORW_NONE;
563         else if (ret < 0)
564                 return BATADV_FORW_ALL;
565
566         ethhdr = eth_hdr(skb);
567
568         tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
569                                                BATADV_NO_FLAGS);
570         ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
571         unsnoop_count = !is_unsnoopable ? 0 :
572                         atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
573
574         total_count = tt_count + ip_count + unsnoop_count;
575
576         switch (total_count) {
577         case 1:
578                 if (tt_count)
579                         *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
580                 else if (ip_count)
581                         *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
582                 else if (unsnoop_count)
583                         *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
584
585                 if (*orig)
586                         return BATADV_FORW_SINGLE;
587
588                 /* fall through */
589         case 0:
590                 return BATADV_FORW_NONE;
591         default:
592                 return BATADV_FORW_ALL;
593         }
594 }
595
596 /**
597  * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
598  * @bat_priv: the bat priv with all the soft interface information
599  * @orig: the orig_node which multicast state might have changed of
600  * @mcast_flags: flags indicating the new multicast state
601  *
602  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
603  * orig, has toggled then this method updates counter and list accordingly.
604  *
605  * Caller needs to hold orig->mcast_handler_lock.
606  */
607 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
608                                              struct batadv_orig_node *orig,
609                                              u8 mcast_flags)
610 {
611         struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
612         struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
613
614         lockdep_assert_held(&orig->mcast_handler_lock);
615
616         /* switched from flag unset to set */
617         if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
618             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
619                 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
620
621                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
622                 /* flag checks above + mcast_handler_lock prevents this */
623                 WARN_ON(!hlist_unhashed(node));
624
625                 hlist_add_head_rcu(node, head);
626                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
627         /* switched from flag set to unset */
628         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
629                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
630                 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
631
632                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
633                 /* flag checks above + mcast_handler_lock prevents this */
634                 WARN_ON(hlist_unhashed(node));
635
636                 hlist_del_init_rcu(node);
637                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
638         }
639 }
640
641 /**
642  * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
643  * @bat_priv: the bat priv with all the soft interface information
644  * @orig: the orig_node which multicast state might have changed of
645  * @mcast_flags: flags indicating the new multicast state
646  *
647  * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
648  * toggled then this method updates counter and list accordingly.
649  *
650  * Caller needs to hold orig->mcast_handler_lock.
651  */
652 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
653                                           struct batadv_orig_node *orig,
654                                           u8 mcast_flags)
655 {
656         struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
657         struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
658
659         lockdep_assert_held(&orig->mcast_handler_lock);
660
661         /* switched from flag unset to set */
662         if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
663             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
664                 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
665
666                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
667                 /* flag checks above + mcast_handler_lock prevents this */
668                 WARN_ON(!hlist_unhashed(node));
669
670                 hlist_add_head_rcu(node, head);
671                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
672         /* switched from flag set to unset */
673         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
674                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
675                 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
676
677                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
678                 /* flag checks above + mcast_handler_lock prevents this */
679                 WARN_ON(hlist_unhashed(node));
680
681                 hlist_del_init_rcu(node);
682                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
683         }
684 }
685
686 /**
687  * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
688  * @bat_priv: the bat priv with all the soft interface information
689  * @orig: the orig_node which multicast state might have changed of
690  * @mcast_flags: flags indicating the new multicast state
691  *
692  * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
693  * toggled then this method updates counter and list accordingly.
694  *
695  * Caller needs to hold orig->mcast_handler_lock.
696  */
697 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
698                                           struct batadv_orig_node *orig,
699                                           u8 mcast_flags)
700 {
701         struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
702         struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
703
704         lockdep_assert_held(&orig->mcast_handler_lock);
705
706         /* switched from flag unset to set */
707         if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
708             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
709                 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
710
711                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
712                 /* flag checks above + mcast_handler_lock prevents this */
713                 WARN_ON(!hlist_unhashed(node));
714
715                 hlist_add_head_rcu(node, head);
716                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
717         /* switched from flag set to unset */
718         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
719                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
720                 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
721
722                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
723                 /* flag checks above + mcast_handler_lock prevents this */
724                 WARN_ON(hlist_unhashed(node));
725
726                 hlist_del_init_rcu(node);
727                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
728         }
729 }
730
731 /**
732  * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
733  * @bat_priv: the bat priv with all the soft interface information
734  * @orig: the orig_node of the ogm
735  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
736  * @tvlv_value: tvlv buffer containing the multicast data
737  * @tvlv_value_len: tvlv buffer length
738  */
739 static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
740                                              struct batadv_orig_node *orig,
741                                              u8 flags,
742                                              void *tvlv_value,
743                                              u16 tvlv_value_len)
744 {
745         bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
746         u8 mcast_flags = BATADV_NO_FLAGS;
747         bool orig_initialized;
748
749         if (orig_mcast_enabled && tvlv_value &&
750             (tvlv_value_len >= sizeof(mcast_flags)))
751                 mcast_flags = *(u8 *)tvlv_value;
752
753         spin_lock_bh(&orig->mcast_handler_lock);
754         orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
755                                     &orig->capa_initialized);
756
757         /* If mcast support is turned on decrease the disabled mcast node
758          * counter only if we had increased it for this node before. If this
759          * is a completely new orig_node no need to decrease the counter.
760          */
761         if (orig_mcast_enabled &&
762             !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
763                 if (orig_initialized)
764                         atomic_dec(&bat_priv->mcast.num_disabled);
765                 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
766         /* If mcast support is being switched off or if this is an initial
767          * OGM without mcast support then increase the disabled mcast
768          * node counter.
769          */
770         } else if (!orig_mcast_enabled &&
771                    (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
772                     !orig_initialized)) {
773                 atomic_inc(&bat_priv->mcast.num_disabled);
774                 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
775         }
776
777         set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
778
779         batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
780         batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
781         batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
782
783         orig->mcast_flags = mcast_flags;
784         spin_unlock_bh(&orig->mcast_handler_lock);
785 }
786
787 /**
788  * batadv_mcast_init - initialize the multicast optimizations structures
789  * @bat_priv: the bat priv with all the soft interface information
790  */
791 void batadv_mcast_init(struct batadv_priv *bat_priv)
792 {
793         batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
794                                      NULL, BATADV_TVLV_MCAST, 1,
795                                      BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
796 }
797
798 /**
799  * batadv_mcast_free - free the multicast optimizations structures
800  * @bat_priv: the bat priv with all the soft interface information
801  */
802 void batadv_mcast_free(struct batadv_priv *bat_priv)
803 {
804         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
805         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
806
807         batadv_mcast_mla_tt_retract(bat_priv, NULL);
808 }
809
810 /**
811  * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
812  * @bat_priv: the bat priv with all the soft interface information
813  * @skb: the multicast packet to send
814  * @vid: the vlan identifier
815  * @orig_node: the originator to send the packet to
816  *
817  * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
818  */
819 int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
820                                 struct sk_buff *skb,
821                                 unsigned short vid,
822                                 struct batadv_orig_node *orig_node)
823 {
824         /* Avoid sending multicast-in-unicast packets to other BLA
825          * gateways - they already got the frame from the LAN side
826          * we share with them.
827          * TODO: Refactor to take BLA into account earlier, to avoid
828          * reducing the mcast_fanout count.
829          */
830         if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
831                 dev_kfree_skb(skb);
832                 return NET_XMIT_SUCCESS;
833         }
834
835         return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
836                                        orig_node, vid);
837 }
838
839 /**
840  * batadv_mcast_purge_orig - reset originator global mcast state modifications
841  * @orig: the originator which is going to get purged
842  */
843 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
844 {
845         struct batadv_priv *bat_priv = orig->bat_priv;
846
847         spin_lock_bh(&orig->mcast_handler_lock);
848
849         if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
850             test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
851                 atomic_dec(&bat_priv->mcast.num_disabled);
852
853         batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
854         batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
855         batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
856
857         spin_unlock_bh(&orig->mcast_handler_lock);
858 }