blob: 155565e0fecce4237eb0ed4304fd99923215c9d1 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2014-2016 B.A.T.M.A.N. contributors:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01002 *
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
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010018#include "multicast.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/atomic.h>
Linus Lüssing9c936e32015-06-16 17:10:25 +020022#include <linux/bitops.h>
Linus Lüssing8a4023c2015-06-16 17:10:26 +020023#include <linux/bug.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020024#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>
Sven Eckelmann2c72d652015-06-21 14:45:14 +020034#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#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 "packet.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010048#include "translation-table.h"
49
50/**
51 * batadv_mcast_mla_softif_get - get softif multicast listeners
52 * @dev: the device to collect multicast addresses from
53 * @mcast_list: a list to put found addresses into
54 *
55 * Collect multicast addresses of the local multicast listeners
56 * on the given soft interface, dev, in the given mcast_list.
57 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020058 * Return: -ENOMEM on memory allocation error or the number of
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010059 * items added to the mcast_list otherwise.
60 */
61static int batadv_mcast_mla_softif_get(struct net_device *dev,
62 struct hlist_head *mcast_list)
63{
64 struct netdev_hw_addr *mc_list_entry;
65 struct batadv_hw_addr *new;
66 int ret = 0;
67
68 netif_addr_lock_bh(dev);
69 netdev_for_each_mc_addr(mc_list_entry, dev) {
70 new = kmalloc(sizeof(*new), GFP_ATOMIC);
71 if (!new) {
72 ret = -ENOMEM;
73 break;
74 }
75
76 ether_addr_copy(new->addr, mc_list_entry->addr);
77 hlist_add_head(&new->list, mcast_list);
78 ret++;
79 }
80 netif_addr_unlock_bh(dev);
81
82 return ret;
83}
84
85/**
86 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
87 * @mcast_addr: the multicast address to check
88 * @mcast_list: the list with multicast addresses to search in
89 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020090 * Return: true if the given address is already in the given list.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010091 * Otherwise returns false.
92 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020093static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010094 struct hlist_head *mcast_list)
95{
96 struct batadv_hw_addr *mcast_entry;
97
98 hlist_for_each_entry(mcast_entry, mcast_list, list)
99 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
100 return true;
101
102 return false;
103}
104
105/**
106 * batadv_mcast_mla_list_free - free a list of multicast addresses
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200107 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100108 * @mcast_list: the list to free
109 *
110 * Removes and frees all items in the given mcast_list.
111 */
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200112static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
113 struct hlist_head *mcast_list)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100114{
115 struct batadv_hw_addr *mcast_entry;
116 struct hlist_node *tmp;
117
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200118 lockdep_assert_held(&bat_priv->tt.commit_lock);
119
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100120 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
121 hlist_del(&mcast_entry->list);
122 kfree(mcast_entry);
123 }
124}
125
126/**
127 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
128 * @bat_priv: the bat priv with all the soft interface information
129 * @mcast_list: a list of addresses which should _not_ be removed
130 *
131 * Retracts the announcement of any multicast listener from the
132 * translation table except the ones listed in the given mcast_list.
133 *
134 * If mcast_list is NULL then all are retracted.
135 */
136static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
137 struct hlist_head *mcast_list)
138{
139 struct batadv_hw_addr *mcast_entry;
140 struct hlist_node *tmp;
141
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200142 lockdep_assert_held(&bat_priv->tt.commit_lock);
143
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100144 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
145 list) {
146 if (mcast_list &&
147 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
148 mcast_list))
149 continue;
150
151 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
152 BATADV_NO_FLAGS,
153 "mcast TT outdated", false);
154
155 hlist_del(&mcast_entry->list);
156 kfree(mcast_entry);
157 }
158}
159
160/**
161 * batadv_mcast_mla_tt_add - add multicast listener announcements
162 * @bat_priv: the bat priv with all the soft interface information
163 * @mcast_list: a list of addresses which are going to get added
164 *
165 * Adds multicast listener announcements from the given mcast_list to the
166 * translation table if they have not been added yet.
167 */
168static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
169 struct hlist_head *mcast_list)
170{
171 struct batadv_hw_addr *mcast_entry;
172 struct hlist_node *tmp;
173
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200174 lockdep_assert_held(&bat_priv->tt.commit_lock);
175
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100176 if (!mcast_list)
177 return;
178
179 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
180 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
181 &bat_priv->mcast.mla_list))
182 continue;
183
184 if (!batadv_tt_local_add(bat_priv->soft_iface,
185 mcast_entry->addr, BATADV_NO_FLAGS,
186 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
187 continue;
188
189 hlist_del(&mcast_entry->list);
190 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
191 }
192}
193
194/**
195 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
196 * @bat_priv: the bat priv with all the soft interface information
197 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200198 * Checks whether there is a bridge on top of our soft interface.
199 *
200 * Return: true if there is a bridge, false otherwise.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100201 */
202static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
203{
204 struct net_device *upper = bat_priv->soft_iface;
205
206 rcu_read_lock();
207 do {
208 upper = netdev_master_upper_dev_get_rcu(upper);
209 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
210 rcu_read_unlock();
211
212 return upper;
213}
214
215/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100216 * batadv_mcast_mla_tvlv_update - update multicast tvlv
217 * @bat_priv: the bat priv with all the soft interface information
218 *
219 * Updates the own multicast tvlv with our current multicast related settings,
220 * capabilities and inabilities.
221 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200222 * Return: true if the tvlv container is registered afterwards. Otherwise
Linus Lüssing60432d72014-02-15 17:47:51 +0100223 * returns false.
224 */
225static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
226{
227 struct batadv_tvlv_mcast_data mcast_data;
228
229 mcast_data.flags = BATADV_NO_FLAGS;
230 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
231
232 /* Avoid attaching MLAs, if there is a bridge on top of our soft
233 * interface, we don't support that yet (TODO)
234 */
235 if (batadv_mcast_has_bridge(bat_priv)) {
236 if (bat_priv->mcast.enabled) {
237 batadv_tvlv_container_unregister(bat_priv,
238 BATADV_TVLV_MCAST, 1);
239 bat_priv->mcast.enabled = false;
240 }
241
242 return false;
243 }
244
245 if (!bat_priv->mcast.enabled ||
246 mcast_data.flags != bat_priv->mcast.flags) {
247 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
248 &mcast_data, sizeof(mcast_data));
249 bat_priv->mcast.flags = mcast_data.flags;
250 bat_priv->mcast.enabled = true;
251 }
252
253 return true;
254}
255
256/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100257 * batadv_mcast_mla_update - update the own MLAs
258 * @bat_priv: the bat priv with all the soft interface information
259 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100260 * Updates the own multicast listener announcements in the translation
261 * table as well as the own, announced multicast tvlv container.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100262 */
263void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
264{
265 struct net_device *soft_iface = bat_priv->soft_iface;
266 struct hlist_head mcast_list = HLIST_HEAD_INIT;
267 int ret;
268
Linus Lüssing60432d72014-02-15 17:47:51 +0100269 if (!batadv_mcast_mla_tvlv_update(bat_priv))
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100270 goto update;
271
272 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
273 if (ret < 0)
274 goto out;
275
276update:
277 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
278 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
279
280out:
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200281 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100282}
283
284/**
Linus Lüssingab498862014-02-15 17:47:53 +0100285 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
286 * @bat_priv: the bat priv with all the soft interface information
287 * @skb: the IPv4 packet to check
288 * @is_unsnoopable: stores whether the destination is snoopable
289 *
290 * Checks whether the given IPv4 packet has the potential to be forwarded with a
291 * mode more optimal than classic flooding.
292 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200293 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
294 * allocation failure.
Linus Lüssingab498862014-02-15 17:47:53 +0100295 */
296static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
297 struct sk_buff *skb,
298 bool *is_unsnoopable)
299{
300 struct iphdr *iphdr;
301
302 /* We might fail due to out-of-memory -> drop it */
303 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
304 return -ENOMEM;
305
306 iphdr = ip_hdr(skb);
307
308 /* TODO: Implement Multicast Router Discovery (RFC4286),
309 * then allow scope > link local, too
310 */
311 if (!ipv4_is_local_multicast(iphdr->daddr))
312 return -EINVAL;
313
314 /* link-local multicast listeners behind a bridge are
315 * not snoopable (see RFC4541, section 2.1.2.2)
316 */
317 *is_unsnoopable = true;
318
319 return 0;
320}
321
322/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100323 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
324 * @bat_priv: the bat priv with all the soft interface information
325 * @skb: the IPv6 packet to check
Linus Lüssingab498862014-02-15 17:47:53 +0100326 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100327 *
328 * Checks whether the given IPv6 packet has the potential to be forwarded with a
329 * mode more optimal than classic flooding.
330 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200331 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100332 */
333static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100334 struct sk_buff *skb,
335 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100336{
337 struct ipv6hdr *ip6hdr;
338
339 /* We might fail due to out-of-memory -> drop it */
340 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
341 return -ENOMEM;
342
343 ip6hdr = ipv6_hdr(skb);
344
345 /* TODO: Implement Multicast Router Discovery (RFC4286),
346 * then allow scope > link local, too
347 */
348 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
349 return -EINVAL;
350
351 /* link-local-all-nodes multicast listeners behind a bridge are
352 * not snoopable (see RFC4541, section 3, paragraph 3)
353 */
354 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
Linus Lüssingab498862014-02-15 17:47:53 +0100355 *is_unsnoopable = true;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100356
357 return 0;
358}
359
360/**
361 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
362 * @bat_priv: the bat priv with all the soft interface information
363 * @skb: the multicast frame to check
Linus Lüssingab498862014-02-15 17:47:53 +0100364 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100365 *
366 * Checks whether the given multicast ethernet frame has the potential to be
367 * forwarded with a mode more optimal than classic flooding.
368 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200369 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100370 */
371static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100372 struct sk_buff *skb,
373 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100374{
375 struct ethhdr *ethhdr = eth_hdr(skb);
376
377 if (!atomic_read(&bat_priv->multicast_mode))
378 return -EINVAL;
379
380 if (atomic_read(&bat_priv->mcast.num_disabled))
381 return -EINVAL;
382
383 switch (ntohs(ethhdr->h_proto)) {
Linus Lüssingab498862014-02-15 17:47:53 +0100384 case ETH_P_IP:
385 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
386 is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100387 case ETH_P_IPV6:
Linus Lüssingab498862014-02-15 17:47:53 +0100388 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
389 is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100390 default:
391 return -EINVAL;
392 }
393}
394
395/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100396 * batadv_mcast_want_all_ip_count - count nodes with unspecific mcast interest
397 * @bat_priv: the bat priv with all the soft interface information
398 * @ethhdr: ethernet header of a packet
399 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200400 * Return: the number of nodes which want all IPv4 multicast traffic if the
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100401 * given ethhdr is from an IPv4 packet or the number of nodes which want all
402 * IPv6 traffic if it matches an IPv6 packet.
403 */
404static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
405 struct ethhdr *ethhdr)
406{
407 switch (ntohs(ethhdr->h_proto)) {
408 case ETH_P_IP:
409 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
410 case ETH_P_IPV6:
411 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
412 default:
413 /* we shouldn't be here... */
414 return 0;
415 }
416}
417
418/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100419 * batadv_mcast_forw_tt_node_get - get a multicast tt node
420 * @bat_priv: the bat priv with all the soft interface information
421 * @ethhdr: the ether header containing the multicast destination
422 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200423 * Return: an orig_node matching the multicast address provided by ethhdr
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100424 * via a translation table lookup. This increases the returned nodes refcount.
425 */
426static struct batadv_orig_node *
427batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
428 struct ethhdr *ethhdr)
429{
430 return batadv_transtable_search(bat_priv, ethhdr->h_source,
431 ethhdr->h_dest, BATADV_NO_FLAGS);
432}
433
434/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100435 * batadv_mcast_want_forw_ipv4_node_get - get a node with an ipv4 flag
436 * @bat_priv: the bat priv with all the soft interface information
437 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200438 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100439 * increases its refcount.
440 */
441static struct batadv_orig_node *
442batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
443{
444 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
445
446 rcu_read_lock();
447 hlist_for_each_entry_rcu(tmp_orig_node,
448 &bat_priv->mcast.want_all_ipv4_list,
449 mcast_want_all_ipv4_node) {
Marek Lindneraf0a1712014-04-24 03:41:26 +0800450 if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100451 continue;
452
453 orig_node = tmp_orig_node;
454 break;
455 }
456 rcu_read_unlock();
457
458 return orig_node;
459}
460
461/**
462 * batadv_mcast_want_forw_ipv6_node_get - get a node with an ipv6 flag
463 * @bat_priv: the bat priv with all the soft interface information
464 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200465 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100466 * and increases its refcount.
467 */
468static struct batadv_orig_node *
469batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
470{
471 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
472
473 rcu_read_lock();
474 hlist_for_each_entry_rcu(tmp_orig_node,
475 &bat_priv->mcast.want_all_ipv6_list,
476 mcast_want_all_ipv6_node) {
Marek Lindneraf0a1712014-04-24 03:41:26 +0800477 if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100478 continue;
479
480 orig_node = tmp_orig_node;
481 break;
482 }
483 rcu_read_unlock();
484
485 return orig_node;
486}
487
488/**
489 * batadv_mcast_want_forw_ip_node_get - get a node with an ipv4/ipv6 flag
490 * @bat_priv: the bat priv with all the soft interface information
491 * @ethhdr: an ethernet header to determine the protocol family from
492 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200493 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100494 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
495 * increases its refcount.
496 */
497static struct batadv_orig_node *
498batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
499 struct ethhdr *ethhdr)
500{
501 switch (ntohs(ethhdr->h_proto)) {
502 case ETH_P_IP:
503 return batadv_mcast_forw_ipv4_node_get(bat_priv);
504 case ETH_P_IPV6:
505 return batadv_mcast_forw_ipv6_node_get(bat_priv);
506 default:
507 /* we shouldn't be here... */
508 return NULL;
509 }
510}
511
512/**
Linus Lüssingab498862014-02-15 17:47:53 +0100513 * batadv_mcast_want_forw_unsnoop_node_get - get a node with an unsnoopable flag
514 * @bat_priv: the bat priv with all the soft interface information
515 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200516 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
Linus Lüssingab498862014-02-15 17:47:53 +0100517 * set and increases its refcount.
518 */
519static struct batadv_orig_node *
520batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
521{
522 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
523
524 rcu_read_lock();
525 hlist_for_each_entry_rcu(tmp_orig_node,
526 &bat_priv->mcast.want_all_unsnoopables_list,
527 mcast_want_all_unsnoopables_node) {
Marek Lindneraf0a1712014-04-24 03:41:26 +0800528 if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
Linus Lüssingab498862014-02-15 17:47:53 +0100529 continue;
530
531 orig_node = tmp_orig_node;
532 break;
533 }
534 rcu_read_unlock();
535
536 return orig_node;
537}
538
539/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100540 * batadv_mcast_forw_mode - check on how to forward a multicast packet
541 * @bat_priv: the bat priv with all the soft interface information
542 * @skb: The multicast packet to check
543 * @orig: an originator to be set to forward the skb to
544 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200545 * Return: the forwarding mode as enum batadv_forw_mode and in case of
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100546 * BATADV_FORW_SINGLE set the orig to the single originator the skb
547 * should be forwarded to.
548 */
549enum batadv_forw_mode
550batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
551 struct batadv_orig_node **orig)
552{
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100553 int ret, tt_count, ip_count, unsnoop_count, total_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100554 bool is_unsnoopable = false;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100555 struct ethhdr *ethhdr;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100556
Linus Lüssingab498862014-02-15 17:47:53 +0100557 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100558 if (ret == -ENOMEM)
559 return BATADV_FORW_NONE;
560 else if (ret < 0)
561 return BATADV_FORW_ALL;
562
563 ethhdr = eth_hdr(skb);
564
565 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
566 BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100567 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100568 unsnoop_count = !is_unsnoopable ? 0 :
569 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100570
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100571 total_count = tt_count + ip_count + unsnoop_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100572
573 switch (total_count) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100574 case 1:
Linus Lüssingab498862014-02-15 17:47:53 +0100575 if (tt_count)
576 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100577 else if (ip_count)
578 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100579 else if (unsnoop_count)
580 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
581
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100582 if (*orig)
583 return BATADV_FORW_SINGLE;
584
585 /* fall through */
586 case 0:
587 return BATADV_FORW_NONE;
588 default:
589 return BATADV_FORW_ALL;
590 }
591}
592
593/**
Linus Lüssingab498862014-02-15 17:47:53 +0100594 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
595 * @bat_priv: the bat priv with all the soft interface information
596 * @orig: the orig_node which multicast state might have changed of
597 * @mcast_flags: flags indicating the new multicast state
598 *
599 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
600 * orig, has toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200601 *
602 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssingab498862014-02-15 17:47:53 +0100603 */
604static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
605 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200606 u8 mcast_flags)
Linus Lüssingab498862014-02-15 17:47:53 +0100607{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200608 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
609 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
610
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200611 lockdep_assert_held(&orig->mcast_handler_lock);
612
Linus Lüssingab498862014-02-15 17:47:53 +0100613 /* switched from flag unset to set */
614 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
615 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
616 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
617
618 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200619 /* flag checks above + mcast_handler_lock prevents this */
620 WARN_ON(!hlist_unhashed(node));
621
622 hlist_add_head_rcu(node, head);
Linus Lüssingab498862014-02-15 17:47:53 +0100623 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
624 /* switched from flag set to unset */
625 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
626 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
627 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
628
629 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200630 /* flag checks above + mcast_handler_lock prevents this */
631 WARN_ON(hlist_unhashed(node));
632
633 hlist_del_init_rcu(node);
Linus Lüssingab498862014-02-15 17:47:53 +0100634 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
635 }
636}
637
638/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100639 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
640 * @bat_priv: the bat priv with all the soft interface information
641 * @orig: the orig_node which multicast state might have changed of
642 * @mcast_flags: flags indicating the new multicast state
643 *
644 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
645 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200646 *
647 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100648 */
649static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
650 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200651 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100652{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200653 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
654 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
655
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200656 lockdep_assert_held(&orig->mcast_handler_lock);
657
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100658 /* switched from flag unset to set */
659 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
660 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
661 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
662
663 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200664 /* flag checks above + mcast_handler_lock prevents this */
665 WARN_ON(!hlist_unhashed(node));
666
667 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100668 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
669 /* switched from flag set to unset */
670 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
671 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
672 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
673
674 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200675 /* flag checks above + mcast_handler_lock prevents this */
676 WARN_ON(hlist_unhashed(node));
677
678 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100679 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
680 }
681}
682
683/**
684 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
685 * @bat_priv: the bat priv with all the soft interface information
686 * @orig: the orig_node which multicast state might have changed of
687 * @mcast_flags: flags indicating the new multicast state
688 *
689 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
690 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200691 *
692 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100693 */
694static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
695 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200696 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100697{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200698 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
699 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
700
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200701 lockdep_assert_held(&orig->mcast_handler_lock);
702
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100703 /* switched from flag unset to set */
704 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
705 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
706 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
707
708 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200709 /* flag checks above + mcast_handler_lock prevents this */
710 WARN_ON(!hlist_unhashed(node));
711
712 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100713 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
714 /* switched from flag set to unset */
715 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
716 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
717 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
718
719 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200720 /* flag checks above + mcast_handler_lock prevents this */
721 WARN_ON(hlist_unhashed(node));
722
723 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100724 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
725 }
726}
727
728/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100729 * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
730 * @bat_priv: the bat priv with all the soft interface information
731 * @orig: the orig_node of the ogm
732 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
733 * @tvlv_value: tvlv buffer containing the multicast data
734 * @tvlv_value_len: tvlv buffer length
735 */
736static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
737 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200738 u8 flags,
Linus Lüssing60432d72014-02-15 17:47:51 +0100739 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200740 u16 tvlv_value_len)
Linus Lüssing60432d72014-02-15 17:47:51 +0100741{
742 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200743 u8 mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing60432d72014-02-15 17:47:51 +0100744 bool orig_initialized;
745
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200746 if (orig_mcast_enabled && tvlv_value &&
747 (tvlv_value_len >= sizeof(mcast_flags)))
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200748 mcast_flags = *(u8 *)tvlv_value;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200749
750 spin_lock_bh(&orig->mcast_handler_lock);
Linus Lüssing9c936e32015-06-16 17:10:25 +0200751 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
752 &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +0100753
754 /* If mcast support is turned on decrease the disabled mcast node
755 * counter only if we had increased it for this node before. If this
756 * is a completely new orig_node no need to decrease the counter.
757 */
758 if (orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +0200759 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing60432d72014-02-15 17:47:51 +0100760 if (orig_initialized)
761 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +0200762 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssinge8829f02014-10-30 05:40:46 +0100763 /* If mcast support is being switched off or if this is an initial
764 * OGM without mcast support then increase the disabled mcast
765 * node counter.
Linus Lüssing60432d72014-02-15 17:47:51 +0100766 */
767 } else if (!orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +0200768 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
Linus Lüssinge8829f02014-10-30 05:40:46 +0100769 !orig_initialized)) {
Linus Lüssing60432d72014-02-15 17:47:51 +0100770 atomic_inc(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +0200771 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +0100772 }
773
Linus Lüssing9c936e32015-06-16 17:10:25 +0200774 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +0100775
Linus Lüssingab498862014-02-15 17:47:53 +0100776 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100777 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
778 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
Linus Lüssingab498862014-02-15 17:47:53 +0100779
Linus Lüssing60432d72014-02-15 17:47:51 +0100780 orig->mcast_flags = mcast_flags;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200781 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100782}
783
784/**
785 * batadv_mcast_init - initialize the multicast optimizations structures
786 * @bat_priv: the bat priv with all the soft interface information
787 */
788void batadv_mcast_init(struct batadv_priv *bat_priv)
789{
790 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
791 NULL, BATADV_TVLV_MCAST, 1,
792 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
793}
794
795/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100796 * batadv_mcast_free - free the multicast optimizations structures
797 * @bat_priv: the bat priv with all the soft interface information
798 */
799void batadv_mcast_free(struct batadv_priv *bat_priv)
800{
Linus Lüssing60432d72014-02-15 17:47:51 +0100801 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
802 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
803
Simon Wunderlichaf63cf52015-11-30 17:34:01 +0100804 spin_lock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100805 batadv_mcast_mla_tt_retract(bat_priv, NULL);
Simon Wunderlichaf63cf52015-11-30 17:34:01 +0100806 spin_unlock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100807}
Linus Lüssing60432d72014-02-15 17:47:51 +0100808
809/**
810 * batadv_mcast_purge_orig - reset originator global mcast state modifications
811 * @orig: the originator which is going to get purged
812 */
813void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
814{
815 struct batadv_priv *bat_priv = orig->bat_priv;
816
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200817 spin_lock_bh(&orig->mcast_handler_lock);
818
Linus Lüssing9c936e32015-06-16 17:10:25 +0200819 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
820 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
Linus Lüssing60432d72014-02-15 17:47:51 +0100821 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssingab498862014-02-15 17:47:53 +0100822
823 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100824 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
825 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200826
827 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100828}