a4804fa1ad11c3d925b166fb7d0b9c6730662e1f
[cascardo/linux.git] / net / batman-adv / multicast.c
1 /* Copyright (C) 2014 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 "main.h"
19 #include "multicast.h"
20 #include "originator.h"
21 #include "hard-interface.h"
22 #include "translation-table.h"
23 #include "multicast.h"
24
25 /**
26  * batadv_mcast_mla_softif_get - get softif multicast listeners
27  * @dev: the device to collect multicast addresses from
28  * @mcast_list: a list to put found addresses into
29  *
30  * Collect multicast addresses of the local multicast listeners
31  * on the given soft interface, dev, in the given mcast_list.
32  *
33  * Returns -ENOMEM on memory allocation error or the number of
34  * items added to the mcast_list otherwise.
35  */
36 static int batadv_mcast_mla_softif_get(struct net_device *dev,
37                                        struct hlist_head *mcast_list)
38 {
39         struct netdev_hw_addr *mc_list_entry;
40         struct batadv_hw_addr *new;
41         int ret = 0;
42
43         netif_addr_lock_bh(dev);
44         netdev_for_each_mc_addr(mc_list_entry, dev) {
45                 new = kmalloc(sizeof(*new), GFP_ATOMIC);
46                 if (!new) {
47                         ret = -ENOMEM;
48                         break;
49                 }
50
51                 ether_addr_copy(new->addr, mc_list_entry->addr);
52                 hlist_add_head(&new->list, mcast_list);
53                 ret++;
54         }
55         netif_addr_unlock_bh(dev);
56
57         return ret;
58 }
59
60 /**
61  * batadv_mcast_mla_is_duplicate - check whether an address is in a list
62  * @mcast_addr: the multicast address to check
63  * @mcast_list: the list with multicast addresses to search in
64  *
65  * Returns true if the given address is already in the given list.
66  * Otherwise returns false.
67  */
68 static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
69                                           struct hlist_head *mcast_list)
70 {
71         struct batadv_hw_addr *mcast_entry;
72
73         hlist_for_each_entry(mcast_entry, mcast_list, list)
74                 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
75                         return true;
76
77         return false;
78 }
79
80 /**
81  * batadv_mcast_mla_list_free - free a list of multicast addresses
82  * @mcast_list: the list to free
83  *
84  * Removes and frees all items in the given mcast_list.
85  */
86 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
87 {
88         struct batadv_hw_addr *mcast_entry;
89         struct hlist_node *tmp;
90
91         hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
92                 hlist_del(&mcast_entry->list);
93                 kfree(mcast_entry);
94         }
95 }
96
97 /**
98  * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
99  * @bat_priv: the bat priv with all the soft interface information
100  * @mcast_list: a list of addresses which should _not_ be removed
101  *
102  * Retracts the announcement of any multicast listener from the
103  * translation table except the ones listed in the given mcast_list.
104  *
105  * If mcast_list is NULL then all are retracted.
106  */
107 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
108                                         struct hlist_head *mcast_list)
109 {
110         struct batadv_hw_addr *mcast_entry;
111         struct hlist_node *tmp;
112
113         hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
114                                   list) {
115                 if (mcast_list &&
116                     batadv_mcast_mla_is_duplicate(mcast_entry->addr,
117                                                   mcast_list))
118                         continue;
119
120                 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
121                                        BATADV_NO_FLAGS,
122                                        "mcast TT outdated", false);
123
124                 hlist_del(&mcast_entry->list);
125                 kfree(mcast_entry);
126         }
127 }
128
129 /**
130  * batadv_mcast_mla_tt_add - add multicast listener announcements
131  * @bat_priv: the bat priv with all the soft interface information
132  * @mcast_list: a list of addresses which are going to get added
133  *
134  * Adds multicast listener announcements from the given mcast_list to the
135  * translation table if they have not been added yet.
136  */
137 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
138                                     struct hlist_head *mcast_list)
139 {
140         struct batadv_hw_addr *mcast_entry;
141         struct hlist_node *tmp;
142
143         if (!mcast_list)
144                 return;
145
146         hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
147                 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
148                                                   &bat_priv->mcast.mla_list))
149                         continue;
150
151                 if (!batadv_tt_local_add(bat_priv->soft_iface,
152                                          mcast_entry->addr, BATADV_NO_FLAGS,
153                                          BATADV_NULL_IFINDEX, BATADV_NO_MARK))
154                         continue;
155
156                 hlist_del(&mcast_entry->list);
157                 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
158         }
159 }
160
161 /**
162  * batadv_mcast_has_bridge - check whether the soft-iface is bridged
163  * @bat_priv: the bat priv with all the soft interface information
164  *
165  * Checks whether there is a bridge on top of our soft interface. Returns
166  * true if so, false otherwise.
167  */
168 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
169 {
170         struct net_device *upper = bat_priv->soft_iface;
171
172         rcu_read_lock();
173         do {
174                 upper = netdev_master_upper_dev_get_rcu(upper);
175         } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
176         rcu_read_unlock();
177
178         return upper;
179 }
180
181 /**
182  * batadv_mcast_mla_tvlv_update - update multicast tvlv
183  * @bat_priv: the bat priv with all the soft interface information
184  *
185  * Updates the own multicast tvlv with our current multicast related settings,
186  * capabilities and inabilities.
187  *
188  * Returns true if the tvlv container is registered afterwards. Otherwise
189  * returns false.
190  */
191 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
192 {
193         struct batadv_tvlv_mcast_data mcast_data;
194
195         mcast_data.flags = BATADV_NO_FLAGS;
196         memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
197
198         /* Avoid attaching MLAs, if there is a bridge on top of our soft
199          * interface, we don't support that yet (TODO)
200          */
201         if (batadv_mcast_has_bridge(bat_priv)) {
202                 if (bat_priv->mcast.enabled) {
203                         batadv_tvlv_container_unregister(bat_priv,
204                                                          BATADV_TVLV_MCAST, 1);
205                         bat_priv->mcast.enabled = false;
206                 }
207
208                 return false;
209         }
210
211         if (!bat_priv->mcast.enabled ||
212             mcast_data.flags != bat_priv->mcast.flags) {
213                 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
214                                                &mcast_data, sizeof(mcast_data));
215                 bat_priv->mcast.flags = mcast_data.flags;
216                 bat_priv->mcast.enabled = true;
217         }
218
219         return true;
220 }
221
222 /**
223  * batadv_mcast_mla_update - update the own MLAs
224  * @bat_priv: the bat priv with all the soft interface information
225  *
226  * Updates the own multicast listener announcements in the translation
227  * table as well as the own, announced multicast tvlv container.
228  */
229 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
230 {
231         struct net_device *soft_iface = bat_priv->soft_iface;
232         struct hlist_head mcast_list = HLIST_HEAD_INIT;
233         int ret;
234
235         if (!batadv_mcast_mla_tvlv_update(bat_priv))
236                 goto update;
237
238         ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
239         if (ret < 0)
240                 goto out;
241
242 update:
243         batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
244         batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
245
246 out:
247         batadv_mcast_mla_list_free(&mcast_list);
248 }
249
250 /**
251  * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
252  * @bat_priv: the bat priv with all the soft interface information
253  * @skb: the IPv4 packet to check
254  * @is_unsnoopable: stores whether the destination is snoopable
255  *
256  * Checks whether the given IPv4 packet has the potential to be forwarded with a
257  * mode more optimal than classic flooding.
258  *
259  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM in case of
260  * memory allocation failure.
261  */
262 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
263                                              struct sk_buff *skb,
264                                              bool *is_unsnoopable)
265 {
266         struct iphdr *iphdr;
267
268         /* We might fail due to out-of-memory -> drop it */
269         if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
270                 return -ENOMEM;
271
272         iphdr = ip_hdr(skb);
273
274         /* TODO: Implement Multicast Router Discovery (RFC4286),
275          * then allow scope > link local, too
276          */
277         if (!ipv4_is_local_multicast(iphdr->daddr))
278                 return -EINVAL;
279
280         /* link-local multicast listeners behind a bridge are
281          * not snoopable (see RFC4541, section 2.1.2.2)
282          */
283         *is_unsnoopable = true;
284
285         return 0;
286 }
287
288 /**
289  * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
290  * @bat_priv: the bat priv with all the soft interface information
291  * @skb: the IPv6 packet to check
292  * @is_unsnoopable: stores whether the destination is snoopable
293  *
294  * Checks whether the given IPv6 packet has the potential to be forwarded with a
295  * mode more optimal than classic flooding.
296  *
297  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
298  * of memory.
299  */
300 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
301                                              struct sk_buff *skb,
302                                              bool *is_unsnoopable)
303 {
304         struct ipv6hdr *ip6hdr;
305
306         /* We might fail due to out-of-memory -> drop it */
307         if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
308                 return -ENOMEM;
309
310         ip6hdr = ipv6_hdr(skb);
311
312         /* TODO: Implement Multicast Router Discovery (RFC4286),
313          * then allow scope > link local, too
314          */
315         if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
316                 return -EINVAL;
317
318         /* link-local-all-nodes multicast listeners behind a bridge are
319          * not snoopable (see RFC4541, section 3, paragraph 3)
320          */
321         if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
322                 *is_unsnoopable = true;
323
324         return 0;
325 }
326
327 /**
328  * batadv_mcast_forw_mode_check - check for optimized forwarding potential
329  * @bat_priv: the bat priv with all the soft interface information
330  * @skb: the multicast frame to check
331  * @is_unsnoopable: stores whether the destination is snoopable
332  *
333  * Checks whether the given multicast ethernet frame has the potential to be
334  * forwarded with a mode more optimal than classic flooding.
335  *
336  * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
337  * of memory.
338  */
339 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
340                                         struct sk_buff *skb,
341                                         bool *is_unsnoopable)
342 {
343         struct ethhdr *ethhdr = eth_hdr(skb);
344
345         if (!atomic_read(&bat_priv->multicast_mode))
346                 return -EINVAL;
347
348         if (atomic_read(&bat_priv->mcast.num_disabled))
349                 return -EINVAL;
350
351         switch (ntohs(ethhdr->h_proto)) {
352         case ETH_P_IP:
353                 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
354                                                          is_unsnoopable);
355         case ETH_P_IPV6:
356                 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
357                                                          is_unsnoopable);
358         default:
359                 return -EINVAL;
360         }
361 }
362
363 /**
364  * batadv_mcast_forw_tt_node_get - get a multicast tt node
365  * @bat_priv: the bat priv with all the soft interface information
366  * @ethhdr: the ether header containing the multicast destination
367  *
368  * Returns an orig_node matching the multicast address provided by ethhdr
369  * via a translation table lookup. This increases the returned nodes refcount.
370  */
371 static struct batadv_orig_node *
372 batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
373                               struct ethhdr *ethhdr)
374 {
375         return batadv_transtable_search(bat_priv, ethhdr->h_source,
376                                         ethhdr->h_dest, BATADV_NO_FLAGS);
377 }
378
379 /**
380  * batadv_mcast_want_forw_unsnoop_node_get - get a node with an unsnoopable flag
381  * @bat_priv: the bat priv with all the soft interface information
382  *
383  * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
384  * set and increases its refcount.
385  */
386 static struct batadv_orig_node *
387 batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
388 {
389         struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
390
391         rcu_read_lock();
392         hlist_for_each_entry_rcu(tmp_orig_node,
393                                  &bat_priv->mcast.want_all_unsnoopables_list,
394                                  mcast_want_all_unsnoopables_node) {
395                 if (!atomic_inc_not_zero(&orig_node->refcount))
396                         continue;
397
398                 orig_node = tmp_orig_node;
399                 break;
400         }
401         rcu_read_unlock();
402
403         return orig_node;
404 }
405
406 /**
407  * batadv_mcast_forw_mode - check on how to forward a multicast packet
408  * @bat_priv: the bat priv with all the soft interface information
409  * @skb: The multicast packet to check
410  * @orig: an originator to be set to forward the skb to
411  *
412  * Returns the forwarding mode as enum batadv_forw_mode and in case of
413  * BATADV_FORW_SINGLE set the orig to the single originator the skb
414  * should be forwarded to.
415  */
416 enum batadv_forw_mode
417 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
418                        struct batadv_orig_node **orig)
419 {
420         int ret, tt_count, unsnoop_count, total_count;
421         bool is_unsnoopable = false;
422         struct ethhdr *ethhdr;
423
424         ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
425         if (ret == -ENOMEM)
426                 return BATADV_FORW_NONE;
427         else if (ret < 0)
428                 return BATADV_FORW_ALL;
429
430         ethhdr = eth_hdr(skb);
431
432         tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
433                                                BATADV_NO_FLAGS);
434         unsnoop_count = !is_unsnoopable ? 0 :
435                         atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
436
437         total_count = tt_count + unsnoop_count;
438
439         switch (total_count) {
440         case 1:
441                 if (tt_count)
442                         *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
443                 else if (unsnoop_count)
444                         *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
445
446                 if (*orig)
447                         return BATADV_FORW_SINGLE;
448
449                 /* fall through */
450         case 0:
451                 return BATADV_FORW_NONE;
452         default:
453                 return BATADV_FORW_ALL;
454         }
455 }
456
457 /**
458  * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
459  * @bat_priv: the bat priv with all the soft interface information
460  * @orig: the orig_node which multicast state might have changed of
461  * @mcast_flags: flags indicating the new multicast state
462  *
463  * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
464  * orig, has toggled then this method updates counter and list accordingly.
465  */
466 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
467                                              struct batadv_orig_node *orig,
468                                              uint8_t mcast_flags)
469 {
470         /* switched from flag unset to set */
471         if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
472             !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
473                 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
474
475                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
476                 hlist_add_head_rcu(&orig->mcast_want_all_unsnoopables_node,
477                                    &bat_priv->mcast.want_all_unsnoopables_list);
478                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
479         /* switched from flag set to unset */
480         } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
481                    orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
482                 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
483
484                 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
485                 hlist_del_rcu(&orig->mcast_want_all_unsnoopables_node);
486                 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
487         }
488 }
489
490 /**
491  * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
492  * @bat_priv: the bat priv with all the soft interface information
493  * @orig: the orig_node of the ogm
494  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
495  * @tvlv_value: tvlv buffer containing the multicast data
496  * @tvlv_value_len: tvlv buffer length
497  */
498 static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
499                                              struct batadv_orig_node *orig,
500                                              uint8_t flags,
501                                              void *tvlv_value,
502                                              uint16_t tvlv_value_len)
503 {
504         bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
505         uint8_t mcast_flags = BATADV_NO_FLAGS;
506         bool orig_initialized;
507
508         orig_initialized = orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST;
509
510         /* If mcast support is turned on decrease the disabled mcast node
511          * counter only if we had increased it for this node before. If this
512          * is a completely new orig_node no need to decrease the counter.
513          */
514         if (orig_mcast_enabled &&
515             !(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST)) {
516                 if (orig_initialized)
517                         atomic_dec(&bat_priv->mcast.num_disabled);
518                 orig->capabilities |= BATADV_ORIG_CAPA_HAS_MCAST;
519         /* If mcast support is being switched off increase the disabled
520          * mcast node counter.
521          */
522         } else if (!orig_mcast_enabled &&
523                    orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST) {
524                 atomic_inc(&bat_priv->mcast.num_disabled);
525                 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_MCAST;
526         }
527
528         orig->capa_initialized |= BATADV_ORIG_CAPA_HAS_MCAST;
529
530         if (orig_mcast_enabled && tvlv_value &&
531             (tvlv_value_len >= sizeof(mcast_flags)))
532                 mcast_flags = *(uint8_t *)tvlv_value;
533
534         batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
535
536         orig->mcast_flags = mcast_flags;
537 }
538
539 /**
540  * batadv_mcast_init - initialize the multicast optimizations structures
541  * @bat_priv: the bat priv with all the soft interface information
542  */
543 void batadv_mcast_init(struct batadv_priv *bat_priv)
544 {
545         batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
546                                      NULL, BATADV_TVLV_MCAST, 1,
547                                      BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
548 }
549
550 /**
551  * batadv_mcast_free - free the multicast optimizations structures
552  * @bat_priv: the bat priv with all the soft interface information
553  */
554 void batadv_mcast_free(struct batadv_priv *bat_priv)
555 {
556         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
557         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
558
559         batadv_mcast_mla_tt_retract(bat_priv, NULL);
560 }
561
562 /**
563  * batadv_mcast_purge_orig - reset originator global mcast state modifications
564  * @orig: the originator which is going to get purged
565  */
566 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
567 {
568         struct batadv_priv *bat_priv = orig->bat_priv;
569
570         if (!(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST))
571                 atomic_dec(&bat_priv->mcast.num_disabled);
572
573         batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
574 }