batman-adv: Add multicast optimization support for bridged setups
[linux-2.6-block.git] / net / batman-adv / multicast.c
CommitLineData
0046b040 1/* Copyright (C) 2014-2016 B.A.T.M.A.N. contributors:
c5caf4ef
LL
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
c5caf4ef 18#include "multicast.h"
1e2c2a4f
SE
19#include "main.h"
20
21#include <linux/atomic.h>
9c936e3f 22#include <linux/bitops.h>
8a4023c5 23#include <linux/bug.h>
1e2c2a4f
SE
24#include <linux/byteorder/generic.h>
25#include <linux/errno.h>
26#include <linux/etherdevice.h>
27#include <linux/fs.h>
bd2a979e 28#include <linux/icmpv6.h>
687937ab 29#include <linux/if_bridge.h>
1e2c2a4f 30#include <linux/if_ether.h>
bd2a979e 31#include <linux/igmp.h>
1e2c2a4f 32#include <linux/in.h>
bd2a979e 33#include <linux/in6.h>
1e2c2a4f
SE
34#include <linux/ip.h>
35#include <linux/ipv6.h>
7c124391 36#include <linux/kref.h>
1e2c2a4f 37#include <linux/list.h>
2c72d655 38#include <linux/lockdep.h>
1e2c2a4f 39#include <linux/netdevice.h>
687937ab 40#include <linux/printk.h>
1e2c2a4f
SE
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>
687937ab
LL
50#include <net/if_inet6.h>
51#include <net/ip.h>
1e2c2a4f
SE
52#include <net/ipv6.h>
53
54#include "packet.h"
c5caf4ef
LL
55#include "translation-table.h"
56
687937ab
LL
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 */
67static 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
c5caf4ef
LL
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 *
687937ab
LL
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.
c5caf4ef 97 *
62fe710f 98 * Return: -ENOMEM on memory allocation error or the number of
c5caf4ef
LL
99 * items added to the mcast_list otherwise.
100 */
101static int batadv_mcast_mla_softif_get(struct net_device *dev,
102 struct hlist_head *mcast_list)
103{
687937ab 104 struct net_device *bridge = batadv_mcast_get_bridge(dev);
c5caf4ef
LL
105 struct netdev_hw_addr *mc_list_entry;
106 struct batadv_hw_addr *new;
107 int ret = 0;
108
687937ab
LL
109 netif_addr_lock_bh(bridge ? bridge : dev);
110 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
c5caf4ef
LL
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 }
687937ab
LL
121 netif_addr_unlock_bh(bridge ? bridge : dev);
122
123 if (bridge)
124 dev_put(bridge);
c5caf4ef
LL
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 *
62fe710f 134 * Return: true if the given address is already in the given list.
c5caf4ef
LL
135 * Otherwise returns false.
136 */
6b5e971a 137static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
c5caf4ef
LL
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
687937ab
LL
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 */
161static 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 */
186static 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
217out:
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
c5caf4ef
LL
226/**
227 * batadv_mcast_mla_list_free - free a list of multicast addresses
2c72d655 228 * @bat_priv: the bat priv with all the soft interface information
c5caf4ef
LL
229 * @mcast_list: the list to free
230 *
231 * Removes and frees all items in the given mcast_list.
232 */
2c72d655
SE
233static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
234 struct hlist_head *mcast_list)
c5caf4ef
LL
235{
236 struct batadv_hw_addr *mcast_entry;
237 struct hlist_node *tmp;
238
2c72d655
SE
239 lockdep_assert_held(&bat_priv->tt.commit_lock);
240
c5caf4ef
LL
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 */
257static 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
2c72d655
SE
263 lockdep_assert_held(&bat_priv->tt.commit_lock);
264
c5caf4ef
LL
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 */
289static 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
2c72d655
SE
295 lockdep_assert_held(&bat_priv->tt.commit_lock);
296
c5caf4ef
LL
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 *
62fe710f
SE
319 * Checks whether there is a bridge on top of our soft interface.
320 *
321 * Return: true if there is a bridge, false otherwise.
c5caf4ef
LL
322 */
323static 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
60432d75
LL
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 *
687937ab
LL
343 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
344 * otherwise.
60432d75
LL
345 */
346static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
347{
348 struct batadv_tvlv_mcast_data mcast_data;
687937ab
LL
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;
60432d75
LL
352
353 mcast_data.flags = BATADV_NO_FLAGS;
354 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
355
687937ab
LL
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.
60432d75 380 */
687937ab
LL
381 if (!querier4.exists || querier4.shadowing)
382 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
60432d75 383
687937ab
LL
384 if (!querier6.exists || querier6.shadowing)
385 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
60432d75 386
687937ab 387update:
60432d75
LL
388 if (!bat_priv->mcast.enabled ||
389 mcast_data.flags != bat_priv->mcast.flags) {
bd2a979e 390 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
60432d75
LL
391 &mcast_data, sizeof(mcast_data));
392 bat_priv->mcast.flags = mcast_data.flags;
393 bat_priv->mcast.enabled = true;
394 }
395
687937ab
LL
396 return !(mcast_data.flags &
397 (BATADV_MCAST_WANT_ALL_IPV4 + BATADV_MCAST_WANT_ALL_IPV6));
60432d75
LL
398}
399
c5caf4ef
LL
400/**
401 * batadv_mcast_mla_update - update the own MLAs
402 * @bat_priv: the bat priv with all the soft interface information
403 *
60432d75
LL
404 * Updates the own multicast listener announcements in the translation
405 * table as well as the own, announced multicast tvlv container.
c5caf4ef
LL
406 */
407void 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
60432d75 413 if (!batadv_mcast_mla_tvlv_update(bat_priv))
c5caf4ef
LL
414 goto update;
415
416 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
417 if (ret < 0)
418 goto out;
419
687937ab
LL
420 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
421 if (ret < 0)
422 goto out;
423
c5caf4ef
LL
424update:
425 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
426 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
427
428out:
2c72d655 429 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
c5caf4ef
LL
430}
431
bd2a979e
LL
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 */
442static 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
ab49886e
LL
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 *
62fe710f
SE
466 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
467 * allocation failure.
ab49886e
LL
468 */
469static 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
bd2a979e
LL
479 if (batadv_mcast_is_report_ipv4(skb))
480 return -EINVAL;
481
ab49886e
LL
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
bd2a979e
LL
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 */
509static 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
1d8ab8d3
LL
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
ab49886e 527 * @is_unsnoopable: stores whether the destination is snoopable
1d8ab8d3
LL
528 *
529 * Checks whether the given IPv6 packet has the potential to be forwarded with a
530 * mode more optimal than classic flooding.
531 *
62fe710f 532 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1d8ab8d3
LL
533 */
534static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
ab49886e
LL
535 struct sk_buff *skb,
536 bool *is_unsnoopable)
1d8ab8d3
LL
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
bd2a979e
LL
544 if (batadv_mcast_is_report_ipv6(skb))
545 return -EINVAL;
546
1d8ab8d3
LL
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))
ab49886e 559 *is_unsnoopable = true;
1d8ab8d3
LL
560
561 return 0;
562}
bd2a979e 563#endif
1d8ab8d3
LL
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
ab49886e 569 * @is_unsnoopable: stores whether the destination is snoopable
1d8ab8d3
LL
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 *
62fe710f 574 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
1d8ab8d3
LL
575 */
576static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
ab49886e
LL
577 struct sk_buff *skb,
578 bool *is_unsnoopable)
1d8ab8d3
LL
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)) {
ab49886e
LL
589 case ETH_P_IP:
590 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
591 is_unsnoopable);
bd2a979e 592#if IS_ENABLED(CONFIG_IPV6)
1d8ab8d3 593 case ETH_P_IPV6:
ab49886e
LL
594 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
595 is_unsnoopable);
bd2a979e 596#endif
1d8ab8d3
LL
597 default:
598 return -EINVAL;
599 }
600}
601
4c8755d6 602/**
6d030de8
AQ
603 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
604 * interest
4c8755d6
LL
605 * @bat_priv: the bat priv with all the soft interface information
606 * @ethhdr: ethernet header of a packet
607 *
62fe710f 608 * Return: the number of nodes which want all IPv4 multicast traffic if the
4c8755d6
LL
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 */
612static 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
1d8ab8d3
LL
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 *
62fe710f 631 * Return: an orig_node matching the multicast address provided by ethhdr
1d8ab8d3
LL
632 * via a translation table lookup. This increases the returned nodes refcount.
633 */
634static struct batadv_orig_node *
635batadv_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
4c8755d6 642/**
6d030de8 643 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
4c8755d6
LL
644 * @bat_priv: the bat priv with all the soft interface information
645 *
62fe710f 646 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
4c8755d6
LL
647 * increases its refcount.
648 */
649static struct batadv_orig_node *
650batadv_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) {
7c124391 658 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
4c8755d6
LL
659 continue;
660
661 orig_node = tmp_orig_node;
662 break;
663 }
664 rcu_read_unlock();
665
666 return orig_node;
667}
668
669/**
6d030de8 670 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
4c8755d6
LL
671 * @bat_priv: the bat priv with all the soft interface information
672 *
62fe710f 673 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
4c8755d6
LL
674 * and increases its refcount.
675 */
676static struct batadv_orig_node *
677batadv_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) {
7c124391 685 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
4c8755d6
LL
686 continue;
687
688 orig_node = tmp_orig_node;
689 break;
690 }
691 rcu_read_unlock();
692
693 return orig_node;
694}
695
696/**
6d030de8 697 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
4c8755d6
LL
698 * @bat_priv: the bat priv with all the soft interface information
699 * @ethhdr: an ethernet header to determine the protocol family from
700 *
62fe710f 701 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
4c8755d6
LL
702 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
703 * increases its refcount.
704 */
705static struct batadv_orig_node *
706batadv_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
ab49886e 720/**
6d030de8 721 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
ab49886e
LL
722 * @bat_priv: the bat priv with all the soft interface information
723 *
62fe710f 724 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
ab49886e
LL
725 * set and increases its refcount.
726 */
727static struct batadv_orig_node *
728batadv_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) {
7c124391 736 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
ab49886e
LL
737 continue;
738
739 orig_node = tmp_orig_node;
740 break;
741 }
742 rcu_read_unlock();
743
744 return orig_node;
745}
746
1d8ab8d3
LL
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 *
62fe710f 753 * Return: the forwarding mode as enum batadv_forw_mode and in case of
1d8ab8d3
LL
754 * BATADV_FORW_SINGLE set the orig to the single originator the skb
755 * should be forwarded to.
756 */
757enum batadv_forw_mode
758batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
759 struct batadv_orig_node **orig)
760{
4c8755d6 761 int ret, tt_count, ip_count, unsnoop_count, total_count;
ab49886e 762 bool is_unsnoopable = false;
1d8ab8d3 763 struct ethhdr *ethhdr;
1d8ab8d3 764
ab49886e 765 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
1d8ab8d3
LL
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);
4c8755d6 775 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
ab49886e
LL
776 unsnoop_count = !is_unsnoopable ? 0 :
777 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
778
4c8755d6 779 total_count = tt_count + ip_count + unsnoop_count;
1d8ab8d3 780
ab49886e 781 switch (total_count) {
1d8ab8d3 782 case 1:
ab49886e
LL
783 if (tt_count)
784 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
4c8755d6
LL
785 else if (ip_count)
786 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
ab49886e
LL
787 else if (unsnoop_count)
788 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
789
1d8ab8d3
LL
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
ab49886e
LL
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.
8a4023c5
LL
809 *
810 * Caller needs to hold orig->mcast_handler_lock.
ab49886e
LL
811 */
812static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
813 struct batadv_orig_node *orig,
6b5e971a 814 u8 mcast_flags)
ab49886e 815{
8a4023c5
LL
816 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
817 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
818
5274cd68
SE
819 lockdep_assert_held(&orig->mcast_handler_lock);
820
ab49886e
LL
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);
8a4023c5
LL
827 /* flag checks above + mcast_handler_lock prevents this */
828 WARN_ON(!hlist_unhashed(node));
829
830 hlist_add_head_rcu(node, head);
ab49886e
LL
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);
8a4023c5
LL
838 /* flag checks above + mcast_handler_lock prevents this */
839 WARN_ON(hlist_unhashed(node));
840
841 hlist_del_init_rcu(node);
ab49886e
LL
842 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
843 }
844}
845
4c8755d6
LL
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.
8a4023c5
LL
854 *
855 * Caller needs to hold orig->mcast_handler_lock.
4c8755d6
LL
856 */
857static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
858 struct batadv_orig_node *orig,
6b5e971a 859 u8 mcast_flags)
4c8755d6 860{
8a4023c5
LL
861 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
862 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
863
5274cd68
SE
864 lockdep_assert_held(&orig->mcast_handler_lock);
865
4c8755d6
LL
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);
8a4023c5
LL
872 /* flag checks above + mcast_handler_lock prevents this */
873 WARN_ON(!hlist_unhashed(node));
874
875 hlist_add_head_rcu(node, head);
4c8755d6
LL
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);
8a4023c5
LL
883 /* flag checks above + mcast_handler_lock prevents this */
884 WARN_ON(hlist_unhashed(node));
885
886 hlist_del_init_rcu(node);
4c8755d6
LL
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.
8a4023c5
LL
899 *
900 * Caller needs to hold orig->mcast_handler_lock.
4c8755d6
LL
901 */
902static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
903 struct batadv_orig_node *orig,
6b5e971a 904 u8 mcast_flags)
4c8755d6 905{
8a4023c5
LL
906 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
907 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
908
5274cd68
SE
909 lockdep_assert_held(&orig->mcast_handler_lock);
910
4c8755d6
LL
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);
8a4023c5
LL
917 /* flag checks above + mcast_handler_lock prevents this */
918 WARN_ON(!hlist_unhashed(node));
919
920 hlist_add_head_rcu(node, head);
4c8755d6
LL
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);
8a4023c5
LL
928 /* flag checks above + mcast_handler_lock prevents this */
929 WARN_ON(hlist_unhashed(node));
930
931 hlist_del_init_rcu(node);
4c8755d6
LL
932 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
933 }
934}
935
60432d75 936/**
bd2a979e 937 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
60432d75
LL
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 */
bd2a979e
LL
944static 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)
60432d75
LL
949{
950 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
6b5e971a 951 u8 mcast_flags = BATADV_NO_FLAGS;
60432d75
LL
952 bool orig_initialized;
953
8a4023c5
LL
954 if (orig_mcast_enabled && tvlv_value &&
955 (tvlv_value_len >= sizeof(mcast_flags)))
6b5e971a 956 mcast_flags = *(u8 *)tvlv_value;
8a4023c5
LL
957
958 spin_lock_bh(&orig->mcast_handler_lock);
9c936e3f
LL
959 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
960 &orig->capa_initialized);
60432d75
LL
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 &&
9c936e3f 967 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
60432d75
LL
968 if (orig_initialized)
969 atomic_dec(&bat_priv->mcast.num_disabled);
9c936e3f 970 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
e8829f00
LL
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.
60432d75
LL
974 */
975 } else if (!orig_mcast_enabled &&
9c936e3f 976 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
e8829f00 977 !orig_initialized)) {
60432d75 978 atomic_inc(&bat_priv->mcast.num_disabled);
9c936e3f 979 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
60432d75
LL
980 }
981
9c936e3f 982 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
60432d75 983
ab49886e 984 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
4c8755d6
LL
985 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
986 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
ab49886e 987
60432d75 988 orig->mcast_flags = mcast_flags;
8a4023c5 989 spin_unlock_bh(&orig->mcast_handler_lock);
60432d75
LL
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 */
996void batadv_mcast_init(struct batadv_priv *bat_priv)
997{
bd2a979e
LL
998 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
999 NULL, BATADV_TVLV_MCAST, 2,
60432d75
LL
1000 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1001}
1002
c5caf4ef
LL
1003/**
1004 * batadv_mcast_free - free the multicast optimizations structures
1005 * @bat_priv: the bat priv with all the soft interface information
1006 */
1007void batadv_mcast_free(struct batadv_priv *bat_priv)
1008{
bd2a979e
LL
1009 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1010 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
60432d75 1011
af63cf51 1012 spin_lock_bh(&bat_priv->tt.commit_lock);
c5caf4ef 1013 batadv_mcast_mla_tt_retract(bat_priv, NULL);
af63cf51 1014 spin_unlock_bh(&bat_priv->tt.commit_lock);
c5caf4ef 1015}
60432d75
LL
1016
1017/**
1018 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1019 * @orig: the originator which is going to get purged
1020 */
1021void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1022{
1023 struct batadv_priv *bat_priv = orig->bat_priv;
1024
8a4023c5
LL
1025 spin_lock_bh(&orig->mcast_handler_lock);
1026
9c936e3f
LL
1027 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1028 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
60432d75 1029 atomic_dec(&bat_priv->mcast.num_disabled);
ab49886e
LL
1030
1031 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
4c8755d6
LL
1032 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1033 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
8a4023c5
LL
1034
1035 spin_unlock_bh(&orig->mcast_handler_lock);
60432d75 1036}