blob: 8caa2c72efa33511108a2b2e5f3e867bd6ebe59f [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>
Sven Eckelmann7c124392016-01-16 10:29:56 +010033#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include <linux/list.h>
Sven Eckelmann2c72d652015-06-21 14:45:14 +020035#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020036#include <linux/netdevice.h>
37#include <linux/rculist.h>
38#include <linux/rcupdate.h>
39#include <linux/skbuff.h>
40#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/stddef.h>
43#include <linux/string.h>
44#include <linux/types.h>
45#include <net/addrconf.h>
46#include <net/ipv6.h>
47
48#include "packet.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010049#include "translation-table.h"
50
51/**
52 * batadv_mcast_mla_softif_get - get softif multicast listeners
53 * @dev: the device to collect multicast addresses from
54 * @mcast_list: a list to put found addresses into
55 *
56 * Collect multicast addresses of the local multicast listeners
57 * on the given soft interface, dev, in the given mcast_list.
58 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020059 * Return: -ENOMEM on memory allocation error or the number of
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010060 * items added to the mcast_list otherwise.
61 */
62static int batadv_mcast_mla_softif_get(struct net_device *dev,
63 struct hlist_head *mcast_list)
64{
65 struct netdev_hw_addr *mc_list_entry;
66 struct batadv_hw_addr *new;
67 int ret = 0;
68
69 netif_addr_lock_bh(dev);
70 netdev_for_each_mc_addr(mc_list_entry, dev) {
71 new = kmalloc(sizeof(*new), GFP_ATOMIC);
72 if (!new) {
73 ret = -ENOMEM;
74 break;
75 }
76
77 ether_addr_copy(new->addr, mc_list_entry->addr);
78 hlist_add_head(&new->list, mcast_list);
79 ret++;
80 }
81 netif_addr_unlock_bh(dev);
82
83 return ret;
84}
85
86/**
87 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
88 * @mcast_addr: the multicast address to check
89 * @mcast_list: the list with multicast addresses to search in
90 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020091 * Return: true if the given address is already in the given list.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010092 * Otherwise returns false.
93 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020094static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010095 struct hlist_head *mcast_list)
96{
97 struct batadv_hw_addr *mcast_entry;
98
99 hlist_for_each_entry(mcast_entry, mcast_list, list)
100 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
101 return true;
102
103 return false;
104}
105
106/**
107 * batadv_mcast_mla_list_free - free a list of multicast addresses
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200108 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100109 * @mcast_list: the list to free
110 *
111 * Removes and frees all items in the given mcast_list.
112 */
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200113static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
114 struct hlist_head *mcast_list)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100115{
116 struct batadv_hw_addr *mcast_entry;
117 struct hlist_node *tmp;
118
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200119 lockdep_assert_held(&bat_priv->tt.commit_lock);
120
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100121 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
122 hlist_del(&mcast_entry->list);
123 kfree(mcast_entry);
124 }
125}
126
127/**
128 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
129 * @bat_priv: the bat priv with all the soft interface information
130 * @mcast_list: a list of addresses which should _not_ be removed
131 *
132 * Retracts the announcement of any multicast listener from the
133 * translation table except the ones listed in the given mcast_list.
134 *
135 * If mcast_list is NULL then all are retracted.
136 */
137static void batadv_mcast_mla_tt_retract(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
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200143 lockdep_assert_held(&bat_priv->tt.commit_lock);
144
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100145 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
146 list) {
147 if (mcast_list &&
148 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
149 mcast_list))
150 continue;
151
152 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
153 BATADV_NO_FLAGS,
154 "mcast TT outdated", false);
155
156 hlist_del(&mcast_entry->list);
157 kfree(mcast_entry);
158 }
159}
160
161/**
162 * batadv_mcast_mla_tt_add - add multicast listener announcements
163 * @bat_priv: the bat priv with all the soft interface information
164 * @mcast_list: a list of addresses which are going to get added
165 *
166 * Adds multicast listener announcements from the given mcast_list to the
167 * translation table if they have not been added yet.
168 */
169static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
170 struct hlist_head *mcast_list)
171{
172 struct batadv_hw_addr *mcast_entry;
173 struct hlist_node *tmp;
174
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200175 lockdep_assert_held(&bat_priv->tt.commit_lock);
176
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100177 if (!mcast_list)
178 return;
179
180 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
181 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
182 &bat_priv->mcast.mla_list))
183 continue;
184
185 if (!batadv_tt_local_add(bat_priv->soft_iface,
186 mcast_entry->addr, BATADV_NO_FLAGS,
187 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
188 continue;
189
190 hlist_del(&mcast_entry->list);
191 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
192 }
193}
194
195/**
196 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
197 * @bat_priv: the bat priv with all the soft interface information
198 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200199 * Checks whether there is a bridge on top of our soft interface.
200 *
201 * Return: true if there is a bridge, false otherwise.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100202 */
203static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
204{
205 struct net_device *upper = bat_priv->soft_iface;
206
207 rcu_read_lock();
208 do {
209 upper = netdev_master_upper_dev_get_rcu(upper);
210 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
211 rcu_read_unlock();
212
213 return upper;
214}
215
216/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100217 * batadv_mcast_mla_tvlv_update - update multicast tvlv
218 * @bat_priv: the bat priv with all the soft interface information
219 *
220 * Updates the own multicast tvlv with our current multicast related settings,
221 * capabilities and inabilities.
222 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200223 * Return: true if the tvlv container is registered afterwards. Otherwise
Linus Lüssing60432d72014-02-15 17:47:51 +0100224 * returns false.
225 */
226static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
227{
228 struct batadv_tvlv_mcast_data mcast_data;
229
230 mcast_data.flags = BATADV_NO_FLAGS;
231 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
232
233 /* Avoid attaching MLAs, if there is a bridge on top of our soft
234 * interface, we don't support that yet (TODO)
235 */
236 if (batadv_mcast_has_bridge(bat_priv)) {
237 if (bat_priv->mcast.enabled) {
238 batadv_tvlv_container_unregister(bat_priv,
239 BATADV_TVLV_MCAST, 1);
240 bat_priv->mcast.enabled = false;
241 }
242
243 return false;
244 }
245
246 if (!bat_priv->mcast.enabled ||
247 mcast_data.flags != bat_priv->mcast.flags) {
248 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
249 &mcast_data, sizeof(mcast_data));
250 bat_priv->mcast.flags = mcast_data.flags;
251 bat_priv->mcast.enabled = true;
252 }
253
254 return true;
255}
256
257/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100258 * batadv_mcast_mla_update - update the own MLAs
259 * @bat_priv: the bat priv with all the soft interface information
260 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100261 * Updates the own multicast listener announcements in the translation
262 * table as well as the own, announced multicast tvlv container.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100263 */
264void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
265{
266 struct net_device *soft_iface = bat_priv->soft_iface;
267 struct hlist_head mcast_list = HLIST_HEAD_INIT;
268 int ret;
269
Linus Lüssing60432d72014-02-15 17:47:51 +0100270 if (!batadv_mcast_mla_tvlv_update(bat_priv))
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100271 goto update;
272
273 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
274 if (ret < 0)
275 goto out;
276
277update:
278 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
279 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
280
281out:
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200282 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100283}
284
285/**
Linus Lüssingab498862014-02-15 17:47:53 +0100286 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
287 * @bat_priv: the bat priv with all the soft interface information
288 * @skb: the IPv4 packet to check
289 * @is_unsnoopable: stores whether the destination is snoopable
290 *
291 * Checks whether the given IPv4 packet has the potential to be forwarded with a
292 * mode more optimal than classic flooding.
293 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200294 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
295 * allocation failure.
Linus Lüssingab498862014-02-15 17:47:53 +0100296 */
297static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
298 struct sk_buff *skb,
299 bool *is_unsnoopable)
300{
301 struct iphdr *iphdr;
302
303 /* We might fail due to out-of-memory -> drop it */
304 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
305 return -ENOMEM;
306
307 iphdr = ip_hdr(skb);
308
309 /* TODO: Implement Multicast Router Discovery (RFC4286),
310 * then allow scope > link local, too
311 */
312 if (!ipv4_is_local_multicast(iphdr->daddr))
313 return -EINVAL;
314
315 /* link-local multicast listeners behind a bridge are
316 * not snoopable (see RFC4541, section 2.1.2.2)
317 */
318 *is_unsnoopable = true;
319
320 return 0;
321}
322
323/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100324 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
325 * @bat_priv: the bat priv with all the soft interface information
326 * @skb: the IPv6 packet to check
Linus Lüssingab498862014-02-15 17:47:53 +0100327 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100328 *
329 * Checks whether the given IPv6 packet has the potential to be forwarded with a
330 * mode more optimal than classic flooding.
331 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200332 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100333 */
334static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100335 struct sk_buff *skb,
336 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100337{
338 struct ipv6hdr *ip6hdr;
339
340 /* We might fail due to out-of-memory -> drop it */
341 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
342 return -ENOMEM;
343
344 ip6hdr = ipv6_hdr(skb);
345
346 /* TODO: Implement Multicast Router Discovery (RFC4286),
347 * then allow scope > link local, too
348 */
349 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
350 return -EINVAL;
351
352 /* link-local-all-nodes multicast listeners behind a bridge are
353 * not snoopable (see RFC4541, section 3, paragraph 3)
354 */
355 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
Linus Lüssingab498862014-02-15 17:47:53 +0100356 *is_unsnoopable = true;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100357
358 return 0;
359}
360
361/**
362 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
363 * @bat_priv: the bat priv with all the soft interface information
364 * @skb: the multicast frame to check
Linus Lüssingab498862014-02-15 17:47:53 +0100365 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100366 *
367 * Checks whether the given multicast ethernet frame has the potential to be
368 * forwarded with a mode more optimal than classic flooding.
369 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200370 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100371 */
372static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100373 struct sk_buff *skb,
374 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100375{
376 struct ethhdr *ethhdr = eth_hdr(skb);
377
378 if (!atomic_read(&bat_priv->multicast_mode))
379 return -EINVAL;
380
381 if (atomic_read(&bat_priv->mcast.num_disabled))
382 return -EINVAL;
383
384 switch (ntohs(ethhdr->h_proto)) {
Linus Lüssingab498862014-02-15 17:47:53 +0100385 case ETH_P_IP:
386 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
387 is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100388 case ETH_P_IPV6:
Linus Lüssingab498862014-02-15 17:47:53 +0100389 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
390 is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100391 default:
392 return -EINVAL;
393 }
394}
395
396/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100397 * batadv_mcast_want_all_ip_count - count nodes with unspecific mcast interest
398 * @bat_priv: the bat priv with all the soft interface information
399 * @ethhdr: ethernet header of a packet
400 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200401 * Return: the number of nodes which want all IPv4 multicast traffic if the
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100402 * given ethhdr is from an IPv4 packet or the number of nodes which want all
403 * IPv6 traffic if it matches an IPv6 packet.
404 */
405static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
406 struct ethhdr *ethhdr)
407{
408 switch (ntohs(ethhdr->h_proto)) {
409 case ETH_P_IP:
410 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
411 case ETH_P_IPV6:
412 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
413 default:
414 /* we shouldn't be here... */
415 return 0;
416 }
417}
418
419/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100420 * batadv_mcast_forw_tt_node_get - get a multicast tt node
421 * @bat_priv: the bat priv with all the soft interface information
422 * @ethhdr: the ether header containing the multicast destination
423 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200424 * Return: an orig_node matching the multicast address provided by ethhdr
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100425 * via a translation table lookup. This increases the returned nodes refcount.
426 */
427static struct batadv_orig_node *
428batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
429 struct ethhdr *ethhdr)
430{
431 return batadv_transtable_search(bat_priv, ethhdr->h_source,
432 ethhdr->h_dest, BATADV_NO_FLAGS);
433}
434
435/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100436 * batadv_mcast_want_forw_ipv4_node_get - get a node with an ipv4 flag
437 * @bat_priv: the bat priv with all the soft interface information
438 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200439 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100440 * increases its refcount.
441 */
442static struct batadv_orig_node *
443batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
444{
445 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
446
447 rcu_read_lock();
448 hlist_for_each_entry_rcu(tmp_orig_node,
449 &bat_priv->mcast.want_all_ipv4_list,
450 mcast_want_all_ipv4_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100451 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100452 continue;
453
454 orig_node = tmp_orig_node;
455 break;
456 }
457 rcu_read_unlock();
458
459 return orig_node;
460}
461
462/**
463 * batadv_mcast_want_forw_ipv6_node_get - get a node with an ipv6 flag
464 * @bat_priv: the bat priv with all the soft interface information
465 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200466 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100467 * and increases its refcount.
468 */
469static struct batadv_orig_node *
470batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
471{
472 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
473
474 rcu_read_lock();
475 hlist_for_each_entry_rcu(tmp_orig_node,
476 &bat_priv->mcast.want_all_ipv6_list,
477 mcast_want_all_ipv6_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100478 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100479 continue;
480
481 orig_node = tmp_orig_node;
482 break;
483 }
484 rcu_read_unlock();
485
486 return orig_node;
487}
488
489/**
490 * batadv_mcast_want_forw_ip_node_get - get a node with an ipv4/ipv6 flag
491 * @bat_priv: the bat priv with all the soft interface information
492 * @ethhdr: an ethernet header to determine the protocol family from
493 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200494 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100495 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
496 * increases its refcount.
497 */
498static struct batadv_orig_node *
499batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
500 struct ethhdr *ethhdr)
501{
502 switch (ntohs(ethhdr->h_proto)) {
503 case ETH_P_IP:
504 return batadv_mcast_forw_ipv4_node_get(bat_priv);
505 case ETH_P_IPV6:
506 return batadv_mcast_forw_ipv6_node_get(bat_priv);
507 default:
508 /* we shouldn't be here... */
509 return NULL;
510 }
511}
512
513/**
Linus Lüssingab498862014-02-15 17:47:53 +0100514 * batadv_mcast_want_forw_unsnoop_node_get - get a node with an unsnoopable flag
515 * @bat_priv: the bat priv with all the soft interface information
516 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200517 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
Linus Lüssingab498862014-02-15 17:47:53 +0100518 * set and increases its refcount.
519 */
520static struct batadv_orig_node *
521batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
522{
523 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
524
525 rcu_read_lock();
526 hlist_for_each_entry_rcu(tmp_orig_node,
527 &bat_priv->mcast.want_all_unsnoopables_list,
528 mcast_want_all_unsnoopables_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100529 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssingab498862014-02-15 17:47:53 +0100530 continue;
531
532 orig_node = tmp_orig_node;
533 break;
534 }
535 rcu_read_unlock();
536
537 return orig_node;
538}
539
540/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100541 * batadv_mcast_forw_mode - check on how to forward a multicast packet
542 * @bat_priv: the bat priv with all the soft interface information
543 * @skb: The multicast packet to check
544 * @orig: an originator to be set to forward the skb to
545 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200546 * Return: the forwarding mode as enum batadv_forw_mode and in case of
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100547 * BATADV_FORW_SINGLE set the orig to the single originator the skb
548 * should be forwarded to.
549 */
550enum batadv_forw_mode
551batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
552 struct batadv_orig_node **orig)
553{
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100554 int ret, tt_count, ip_count, unsnoop_count, total_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100555 bool is_unsnoopable = false;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100556 struct ethhdr *ethhdr;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100557
Linus Lüssingab498862014-02-15 17:47:53 +0100558 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100559 if (ret == -ENOMEM)
560 return BATADV_FORW_NONE;
561 else if (ret < 0)
562 return BATADV_FORW_ALL;
563
564 ethhdr = eth_hdr(skb);
565
566 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
567 BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100568 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100569 unsnoop_count = !is_unsnoopable ? 0 :
570 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100571
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100572 total_count = tt_count + ip_count + unsnoop_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100573
574 switch (total_count) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100575 case 1:
Linus Lüssingab498862014-02-15 17:47:53 +0100576 if (tt_count)
577 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100578 else if (ip_count)
579 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100580 else if (unsnoop_count)
581 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
582
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100583 if (*orig)
584 return BATADV_FORW_SINGLE;
585
586 /* fall through */
587 case 0:
588 return BATADV_FORW_NONE;
589 default:
590 return BATADV_FORW_ALL;
591 }
592}
593
594/**
Linus Lüssingab498862014-02-15 17:47:53 +0100595 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
596 * @bat_priv: the bat priv with all the soft interface information
597 * @orig: the orig_node which multicast state might have changed of
598 * @mcast_flags: flags indicating the new multicast state
599 *
600 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
601 * orig, has toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200602 *
603 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssingab498862014-02-15 17:47:53 +0100604 */
605static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
606 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200607 u8 mcast_flags)
Linus Lüssingab498862014-02-15 17:47:53 +0100608{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200609 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
610 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
611
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200612 lockdep_assert_held(&orig->mcast_handler_lock);
613
Linus Lüssingab498862014-02-15 17:47:53 +0100614 /* switched from flag unset to set */
615 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
616 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
617 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
618
619 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200620 /* flag checks above + mcast_handler_lock prevents this */
621 WARN_ON(!hlist_unhashed(node));
622
623 hlist_add_head_rcu(node, head);
Linus Lüssingab498862014-02-15 17:47:53 +0100624 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
625 /* switched from flag set to unset */
626 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
627 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
628 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
629
630 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200631 /* flag checks above + mcast_handler_lock prevents this */
632 WARN_ON(hlist_unhashed(node));
633
634 hlist_del_init_rcu(node);
Linus Lüssingab498862014-02-15 17:47:53 +0100635 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
636 }
637}
638
639/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100640 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
641 * @bat_priv: the bat priv with all the soft interface information
642 * @orig: the orig_node which multicast state might have changed of
643 * @mcast_flags: flags indicating the new multicast state
644 *
645 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
646 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200647 *
648 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100649 */
650static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
651 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200652 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100653{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200654 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
655 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
656
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200657 lockdep_assert_held(&orig->mcast_handler_lock);
658
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100659 /* switched from flag unset to set */
660 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
661 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
662 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
663
664 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200665 /* flag checks above + mcast_handler_lock prevents this */
666 WARN_ON(!hlist_unhashed(node));
667
668 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100669 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
670 /* switched from flag set to unset */
671 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
672 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
673 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
674
675 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200676 /* flag checks above + mcast_handler_lock prevents this */
677 WARN_ON(hlist_unhashed(node));
678
679 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100680 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
681 }
682}
683
684/**
685 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
686 * @bat_priv: the bat priv with all the soft interface information
687 * @orig: the orig_node which multicast state might have changed of
688 * @mcast_flags: flags indicating the new multicast state
689 *
690 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
691 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200692 *
693 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100694 */
695static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
696 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200697 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100698{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200699 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
700 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
701
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200702 lockdep_assert_held(&orig->mcast_handler_lock);
703
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100704 /* switched from flag unset to set */
705 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
706 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
707 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
708
709 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200710 /* flag checks above + mcast_handler_lock prevents this */
711 WARN_ON(!hlist_unhashed(node));
712
713 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100714 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
715 /* switched from flag set to unset */
716 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
717 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
718 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
719
720 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200721 /* flag checks above + mcast_handler_lock prevents this */
722 WARN_ON(hlist_unhashed(node));
723
724 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100725 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
726 }
727}
728
729/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100730 * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
731 * @bat_priv: the bat priv with all the soft interface information
732 * @orig: the orig_node of the ogm
733 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
734 * @tvlv_value: tvlv buffer containing the multicast data
735 * @tvlv_value_len: tvlv buffer length
736 */
737static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
738 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200739 u8 flags,
Linus Lüssing60432d72014-02-15 17:47:51 +0100740 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200741 u16 tvlv_value_len)
Linus Lüssing60432d72014-02-15 17:47:51 +0100742{
743 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200744 u8 mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing60432d72014-02-15 17:47:51 +0100745 bool orig_initialized;
746
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200747 if (orig_mcast_enabled && tvlv_value &&
748 (tvlv_value_len >= sizeof(mcast_flags)))
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200749 mcast_flags = *(u8 *)tvlv_value;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200750
751 spin_lock_bh(&orig->mcast_handler_lock);
Linus Lüssing9c936e32015-06-16 17:10:25 +0200752 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
753 &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +0100754
755 /* If mcast support is turned on decrease the disabled mcast node
756 * counter only if we had increased it for this node before. If this
757 * is a completely new orig_node no need to decrease the counter.
758 */
759 if (orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +0200760 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing60432d72014-02-15 17:47:51 +0100761 if (orig_initialized)
762 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +0200763 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssinge8829f02014-10-30 05:40:46 +0100764 /* If mcast support is being switched off or if this is an initial
765 * OGM without mcast support then increase the disabled mcast
766 * node counter.
Linus Lüssing60432d72014-02-15 17:47:51 +0100767 */
768 } else if (!orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +0200769 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
Linus Lüssinge8829f02014-10-30 05:40:46 +0100770 !orig_initialized)) {
Linus Lüssing60432d72014-02-15 17:47:51 +0100771 atomic_inc(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +0200772 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +0100773 }
774
Linus Lüssing9c936e32015-06-16 17:10:25 +0200775 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +0100776
Linus Lüssingab498862014-02-15 17:47:53 +0100777 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100778 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
779 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
Linus Lüssingab498862014-02-15 17:47:53 +0100780
Linus Lüssing60432d72014-02-15 17:47:51 +0100781 orig->mcast_flags = mcast_flags;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200782 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100783}
784
785/**
786 * batadv_mcast_init - initialize the multicast optimizations structures
787 * @bat_priv: the bat priv with all the soft interface information
788 */
789void batadv_mcast_init(struct batadv_priv *bat_priv)
790{
791 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
792 NULL, BATADV_TVLV_MCAST, 1,
793 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
794}
795
796/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100797 * batadv_mcast_free - free the multicast optimizations structures
798 * @bat_priv: the bat priv with all the soft interface information
799 */
800void batadv_mcast_free(struct batadv_priv *bat_priv)
801{
Linus Lüssing60432d72014-02-15 17:47:51 +0100802 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
803 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
804
Simon Wunderlichaf63cf52015-11-30 17:34:01 +0100805 spin_lock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100806 batadv_mcast_mla_tt_retract(bat_priv, NULL);
Simon Wunderlichaf63cf52015-11-30 17:34:01 +0100807 spin_unlock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100808}
Linus Lüssing60432d72014-02-15 17:47:51 +0100809
810/**
811 * batadv_mcast_purge_orig - reset originator global mcast state modifications
812 * @orig: the originator which is going to get purged
813 */
814void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
815{
816 struct batadv_priv *bat_priv = orig->bat_priv;
817
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200818 spin_lock_bh(&orig->mcast_handler_lock);
819
Linus Lüssing9c936e32015-06-16 17:10:25 +0200820 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
821 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
Linus Lüssing60432d72014-02-15 17:47:51 +0100822 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssingab498862014-02-15 17:47:53 +0100823
824 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100825 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
826 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200827
828 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100829}