blob: 2d1a896fb66bf32877878b99fc63ae0d5376cc45 [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>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020028#include <linux/icmpv6.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020029#include <linux/if_bridge.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020030#include <linux/if_ether.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020031#include <linux/igmp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/in.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020033#include <linux/in6.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include <linux/ip.h>
35#include <linux/ipv6.h>
Linus Lüssing72f7b2d2016-05-10 18:41:26 +020036#include <linux/kernel.h>
Sven Eckelmann7c124392016-01-16 10:29:56 +010037#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include <linux/list.h>
Sven Eckelmann2c72d652015-06-21 14:45:14 +020039#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020040#include <linux/netdevice.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020041#include <linux/printk.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include <linux/rculist.h>
43#include <linux/rcupdate.h>
44#include <linux/skbuff.h>
45#include <linux/slab.h>
46#include <linux/spinlock.h>
47#include <linux/stddef.h>
48#include <linux/string.h>
49#include <linux/types.h>
50#include <net/addrconf.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020051#include <net/if_inet6.h>
52#include <net/ip.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include <net/ipv6.h>
54
55#include "packet.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010056#include "translation-table.h"
57
58/**
Linus Lüssing687937a2016-05-10 18:41:25 +020059 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
60 * @soft_iface: netdev struct of the mesh interface
61 *
62 * If the given soft interface has a bridge on top then the refcount
63 * of the according net device is increased.
64 *
65 * Return: NULL if no such bridge exists. Otherwise the net device of the
66 * bridge.
67 */
68static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
69{
70 struct net_device *upper = soft_iface;
71
72 rcu_read_lock();
73 do {
74 upper = netdev_master_upper_dev_get_rcu(upper);
75 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
76
77 if (upper)
78 dev_hold(upper);
79 rcu_read_unlock();
80
81 return upper;
82}
83
84/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010085 * batadv_mcast_mla_softif_get - get softif multicast listeners
86 * @dev: the device to collect multicast addresses from
87 * @mcast_list: a list to put found addresses into
88 *
Linus Lüssing687937a2016-05-10 18:41:25 +020089 * Collects multicast addresses of multicast listeners residing
90 * on this kernel on the given soft interface, dev, in
91 * the given mcast_list. In general, multicast listeners provided by
92 * your multicast receiving applications run directly on this node.
93 *
94 * If there is a bridge interface on top of dev, collects from that one
95 * instead. Just like with IP addresses and routes, multicast listeners
96 * will(/should) register to the bridge interface instead of an
97 * enslaved bat0.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010098 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020099 * Return: -ENOMEM on memory allocation error or the number of
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100100 * items added to the mcast_list otherwise.
101 */
102static int batadv_mcast_mla_softif_get(struct net_device *dev,
103 struct hlist_head *mcast_list)
104{
Linus Lüssing687937a2016-05-10 18:41:25 +0200105 struct net_device *bridge = batadv_mcast_get_bridge(dev);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100106 struct netdev_hw_addr *mc_list_entry;
107 struct batadv_hw_addr *new;
108 int ret = 0;
109
Linus Lüssing687937a2016-05-10 18:41:25 +0200110 netif_addr_lock_bh(bridge ? bridge : dev);
111 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100112 new = kmalloc(sizeof(*new), GFP_ATOMIC);
113 if (!new) {
114 ret = -ENOMEM;
115 break;
116 }
117
118 ether_addr_copy(new->addr, mc_list_entry->addr);
119 hlist_add_head(&new->list, mcast_list);
120 ret++;
121 }
Linus Lüssing687937a2016-05-10 18:41:25 +0200122 netif_addr_unlock_bh(bridge ? bridge : dev);
123
124 if (bridge)
125 dev_put(bridge);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100126
127 return ret;
128}
129
130/**
131 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
132 * @mcast_addr: the multicast address to check
133 * @mcast_list: the list with multicast addresses to search in
134 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200135 * Return: true if the given address is already in the given list.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100136 * Otherwise returns false.
137 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200138static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100139 struct hlist_head *mcast_list)
140{
141 struct batadv_hw_addr *mcast_entry;
142
143 hlist_for_each_entry(mcast_entry, mcast_list, list)
144 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
145 return true;
146
147 return false;
148}
149
150/**
Linus Lüssing687937a2016-05-10 18:41:25 +0200151 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
152 * @dst: destination to write to - a multicast MAC address
153 * @src: source to read from - a multicast IP address
154 *
155 * Converts a given multicast IPv4/IPv6 address from a bridge
156 * to its matching multicast MAC address and copies it into the given
157 * destination buffer.
158 *
159 * Caller needs to make sure the destination buffer can hold
160 * at least ETH_ALEN bytes.
161 */
162static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
163{
164 if (src->proto == htons(ETH_P_IP))
165 ip_eth_mc_map(src->u.ip4, dst);
166#if IS_ENABLED(CONFIG_IPV6)
167 else if (src->proto == htons(ETH_P_IPV6))
168 ipv6_eth_mc_map(&src->u.ip6, dst);
169#endif
170 else
171 eth_zero_addr(dst);
172}
173
174/**
175 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
176 * @dev: a bridge slave whose bridge to collect multicast addresses from
177 * @mcast_list: a list to put found addresses into
178 *
179 * Collects multicast addresses of multicast listeners residing
180 * on foreign, non-mesh devices which we gave access to our mesh via
181 * a bridge on top of the given soft interface, dev, in the given
182 * mcast_list.
183 *
184 * Return: -ENOMEM on memory allocation error or the number of
185 * items added to the mcast_list otherwise.
186 */
187static int batadv_mcast_mla_bridge_get(struct net_device *dev,
188 struct hlist_head *mcast_list)
189{
190 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
191 struct br_ip_list *br_ip_entry, *tmp;
192 struct batadv_hw_addr *new;
193 u8 mcast_addr[ETH_ALEN];
194 int ret;
195
196 /* we don't need to detect these devices/listeners, the IGMP/MLD
197 * snooping code of the Linux bridge already does that for us
198 */
199 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
200 if (ret < 0)
201 goto out;
202
203 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
204 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
205 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
206 continue;
207
208 new = kmalloc(sizeof(*new), GFP_ATOMIC);
209 if (!new) {
210 ret = -ENOMEM;
211 break;
212 }
213
214 ether_addr_copy(new->addr, mcast_addr);
215 hlist_add_head(&new->list, mcast_list);
216 }
217
218out:
219 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
220 list_del(&br_ip_entry->list);
221 kfree(br_ip_entry);
222 }
223
224 return ret;
225}
226
227/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100228 * batadv_mcast_mla_list_free - free a list of multicast addresses
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200229 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100230 * @mcast_list: the list to free
231 *
232 * Removes and frees all items in the given mcast_list.
233 */
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200234static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
235 struct hlist_head *mcast_list)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100236{
237 struct batadv_hw_addr *mcast_entry;
238 struct hlist_node *tmp;
239
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200240 lockdep_assert_held(&bat_priv->tt.commit_lock);
241
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100242 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
243 hlist_del(&mcast_entry->list);
244 kfree(mcast_entry);
245 }
246}
247
248/**
249 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
250 * @bat_priv: the bat priv with all the soft interface information
251 * @mcast_list: a list of addresses which should _not_ be removed
252 *
253 * Retracts the announcement of any multicast listener from the
254 * translation table except the ones listed in the given mcast_list.
255 *
256 * If mcast_list is NULL then all are retracted.
257 */
258static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
259 struct hlist_head *mcast_list)
260{
261 struct batadv_hw_addr *mcast_entry;
262 struct hlist_node *tmp;
263
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200264 lockdep_assert_held(&bat_priv->tt.commit_lock);
265
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100266 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
267 list) {
268 if (mcast_list &&
269 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
270 mcast_list))
271 continue;
272
273 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
274 BATADV_NO_FLAGS,
275 "mcast TT outdated", false);
276
277 hlist_del(&mcast_entry->list);
278 kfree(mcast_entry);
279 }
280}
281
282/**
283 * batadv_mcast_mla_tt_add - add multicast listener announcements
284 * @bat_priv: the bat priv with all the soft interface information
285 * @mcast_list: a list of addresses which are going to get added
286 *
287 * Adds multicast listener announcements from the given mcast_list to the
288 * translation table if they have not been added yet.
289 */
290static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
291 struct hlist_head *mcast_list)
292{
293 struct batadv_hw_addr *mcast_entry;
294 struct hlist_node *tmp;
295
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200296 lockdep_assert_held(&bat_priv->tt.commit_lock);
297
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100298 if (!mcast_list)
299 return;
300
301 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
302 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
303 &bat_priv->mcast.mla_list))
304 continue;
305
306 if (!batadv_tt_local_add(bat_priv->soft_iface,
307 mcast_entry->addr, BATADV_NO_FLAGS,
308 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
309 continue;
310
311 hlist_del(&mcast_entry->list);
312 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
313 }
314}
315
316/**
317 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
318 * @bat_priv: the bat priv with all the soft interface information
319 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200320 * Checks whether there is a bridge on top of our soft interface.
321 *
322 * Return: true if there is a bridge, false otherwise.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100323 */
324static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
325{
326 struct net_device *upper = bat_priv->soft_iface;
327
328 rcu_read_lock();
329 do {
330 upper = netdev_master_upper_dev_get_rcu(upper);
331 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
332 rcu_read_unlock();
333
334 return upper;
335}
336
337/**
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200338 * batadv_mcast_querier_log - debug output regarding the querier status on link
339 * @bat_priv: the bat priv with all the soft interface information
340 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
341 * @old_state: the previous querier state on our link
342 * @new_state: the new querier state on our link
343 *
344 * Outputs debug messages to the logging facility with log level 'mcast'
345 * regarding changes to the querier status on the link which are relevant
346 * to our multicast optimizations.
347 *
348 * Usually this is about whether a querier appeared or vanished in
349 * our mesh or whether the querier is in the suboptimal position of being
350 * behind our local bridge segment: Snooping switches will directly
351 * forward listener reports to the querier, therefore batman-adv and
352 * the bridge will potentially not see these listeners - the querier is
353 * potentially shadowing listeners from us then.
354 *
355 * This is only interesting for nodes with a bridge on top of their
356 * soft interface.
357 */
358static void
359batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
360 struct batadv_mcast_querier_state *old_state,
361 struct batadv_mcast_querier_state *new_state)
362{
363 if (!old_state->exists && new_state->exists)
364 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
365 str_proto);
366 else if (old_state->exists && !new_state->exists)
367 batadv_info(bat_priv->soft_iface,
368 "%s Querier disappeared - multicast optimizations disabled\n",
369 str_proto);
370 else if (!bat_priv->mcast.bridged && !new_state->exists)
371 batadv_info(bat_priv->soft_iface,
372 "No %s Querier present - multicast optimizations disabled\n",
373 str_proto);
374
375 if (new_state->exists) {
376 if ((!old_state->shadowing && new_state->shadowing) ||
377 (!old_state->exists && new_state->shadowing))
378 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
379 "%s Querier is behind our bridged segment: Might shadow listeners\n",
380 str_proto);
381 else if (old_state->shadowing && !new_state->shadowing)
382 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
383 "%s Querier is not behind our bridged segment\n",
384 str_proto);
385 }
386}
387
388/**
389 * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
390 * @bat_priv: the bat priv with all the soft interface information
391 * @bridged: a flag about whether the soft interface is currently bridged or not
392 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
393 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
394 *
395 * If no bridges are ever used on this node, then this function does nothing.
396 *
397 * Otherwise this function outputs debug information to the 'mcast' log level
398 * which might be relevant to our multicast optimizations.
399 *
400 * More precisely, it outputs information when a bridge interface is added or
401 * removed from a soft interface. And when a bridge is present, it further
402 * outputs information about the querier state which is relevant for the
403 * multicast flags this node is going to set.
404 */
405static void
406batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
407 struct batadv_mcast_querier_state *querier_ipv4,
408 struct batadv_mcast_querier_state *querier_ipv6)
409{
410 if (!bat_priv->mcast.bridged && bridged)
411 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
412 "Bridge added: Setting Unsnoopables(U)-flag\n");
413 else if (bat_priv->mcast.bridged && !bridged)
414 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
415 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
416
417 if (bridged) {
418 batadv_mcast_querier_log(bat_priv, "IGMP",
419 &bat_priv->mcast.querier_ipv4,
420 querier_ipv4);
421 batadv_mcast_querier_log(bat_priv, "MLD",
422 &bat_priv->mcast.querier_ipv6,
423 querier_ipv6);
424 }
425}
426
427/**
428 * batadv_mcast_flags_logs - output debug information about mcast flag changes
429 * @bat_priv: the bat priv with all the soft interface information
430 * @flags: flags indicating the new multicast state
431 *
432 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
433 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
434 */
435static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
436{
437 u8 old_flags = bat_priv->mcast.flags;
438 char str_old_flags[] = "[...]";
439
440 sprintf(str_old_flags, "[%c%c%c]",
441 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
442 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
443 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
444
445 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
446 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
447 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
448 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
449 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
450 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
451}
452
453/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100454 * batadv_mcast_mla_tvlv_update - update multicast tvlv
455 * @bat_priv: the bat priv with all the soft interface information
456 *
457 * Updates the own multicast tvlv with our current multicast related settings,
458 * capabilities and inabilities.
459 *
Linus Lüssing687937a2016-05-10 18:41:25 +0200460 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
461 * otherwise.
Linus Lüssing60432d72014-02-15 17:47:51 +0100462 */
463static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
464{
465 struct batadv_tvlv_mcast_data mcast_data;
Linus Lüssing687937a2016-05-10 18:41:25 +0200466 struct batadv_mcast_querier_state querier4 = {false, false};
467 struct batadv_mcast_querier_state querier6 = {false, false};
468 struct net_device *dev = bat_priv->soft_iface;
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200469 bool bridged;
Linus Lüssing60432d72014-02-15 17:47:51 +0100470
471 mcast_data.flags = BATADV_NO_FLAGS;
472 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
473
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200474 bridged = batadv_mcast_has_bridge(bat_priv);
475 if (!bridged)
Linus Lüssing687937a2016-05-10 18:41:25 +0200476 goto update;
477
478#if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
479 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
480#endif
481
482 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
483 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
484
485 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
486 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
487
488 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
489
490 /* 1) If no querier exists at all, then multicast listeners on
491 * our local TT clients behind the bridge will keep silent.
492 * 2) If the selected querier is on one of our local TT clients,
493 * behind the bridge, then this querier might shadow multicast
494 * listeners on our local TT clients, behind this bridge.
495 *
496 * In both cases, we will signalize other batman nodes that
497 * we need all multicast traffic of the according protocol.
Linus Lüssing60432d72014-02-15 17:47:51 +0100498 */
Linus Lüssing687937a2016-05-10 18:41:25 +0200499 if (!querier4.exists || querier4.shadowing)
500 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
Linus Lüssing60432d72014-02-15 17:47:51 +0100501
Linus Lüssing687937a2016-05-10 18:41:25 +0200502 if (!querier6.exists || querier6.shadowing)
503 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
Linus Lüssing60432d72014-02-15 17:47:51 +0100504
Linus Lüssing687937a2016-05-10 18:41:25 +0200505update:
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200506 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
507
508 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
509 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
510
511 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
512 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
513
514 bat_priv->mcast.bridged = bridged;
515
Linus Lüssing60432d72014-02-15 17:47:51 +0100516 if (!bat_priv->mcast.enabled ||
517 mcast_data.flags != bat_priv->mcast.flags) {
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200518 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200519 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +0100520 &mcast_data, sizeof(mcast_data));
521 bat_priv->mcast.flags = mcast_data.flags;
522 bat_priv->mcast.enabled = true;
523 }
524
Linus Lüssing687937a2016-05-10 18:41:25 +0200525 return !(mcast_data.flags &
526 (BATADV_MCAST_WANT_ALL_IPV4 + BATADV_MCAST_WANT_ALL_IPV6));
Linus Lüssing60432d72014-02-15 17:47:51 +0100527}
528
529/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100530 * batadv_mcast_mla_update - update the own MLAs
531 * @bat_priv: the bat priv with all the soft interface information
532 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100533 * Updates the own multicast listener announcements in the translation
534 * table as well as the own, announced multicast tvlv container.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100535 */
536void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
537{
538 struct net_device *soft_iface = bat_priv->soft_iface;
539 struct hlist_head mcast_list = HLIST_HEAD_INIT;
540 int ret;
541
Linus Lüssing60432d72014-02-15 17:47:51 +0100542 if (!batadv_mcast_mla_tvlv_update(bat_priv))
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100543 goto update;
544
545 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
546 if (ret < 0)
547 goto out;
548
Linus Lüssing687937a2016-05-10 18:41:25 +0200549 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
550 if (ret < 0)
551 goto out;
552
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100553update:
554 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
555 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
556
557out:
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200558 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100559}
560
561/**
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200562 * batadv_mcast_is_report_ipv4 - check for IGMP reports
563 * @skb: the ethernet frame destined for the mesh
564 *
565 * This call might reallocate skb data.
566 *
567 * Checks whether the given frame is a valid IGMP report.
568 *
569 * Return: If so then true, otherwise false.
570 */
571static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
572{
573 if (ip_mc_check_igmp(skb, NULL) < 0)
574 return false;
575
576 switch (igmp_hdr(skb)->type) {
577 case IGMP_HOST_MEMBERSHIP_REPORT:
578 case IGMPV2_HOST_MEMBERSHIP_REPORT:
579 case IGMPV3_HOST_MEMBERSHIP_REPORT:
580 return true;
581 }
582
583 return false;
584}
585
586/**
Linus Lüssingab498862014-02-15 17:47:53 +0100587 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
588 * @bat_priv: the bat priv with all the soft interface information
589 * @skb: the IPv4 packet to check
590 * @is_unsnoopable: stores whether the destination is snoopable
591 *
592 * Checks whether the given IPv4 packet has the potential to be forwarded with a
593 * mode more optimal than classic flooding.
594 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200595 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
596 * allocation failure.
Linus Lüssingab498862014-02-15 17:47:53 +0100597 */
598static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
599 struct sk_buff *skb,
600 bool *is_unsnoopable)
601{
602 struct iphdr *iphdr;
603
604 /* We might fail due to out-of-memory -> drop it */
605 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
606 return -ENOMEM;
607
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200608 if (batadv_mcast_is_report_ipv4(skb))
609 return -EINVAL;
610
Linus Lüssingab498862014-02-15 17:47:53 +0100611 iphdr = ip_hdr(skb);
612
613 /* TODO: Implement Multicast Router Discovery (RFC4286),
614 * then allow scope > link local, too
615 */
616 if (!ipv4_is_local_multicast(iphdr->daddr))
617 return -EINVAL;
618
619 /* link-local multicast listeners behind a bridge are
620 * not snoopable (see RFC4541, section 2.1.2.2)
621 */
622 *is_unsnoopable = true;
623
624 return 0;
625}
626
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200627#if IS_ENABLED(CONFIG_IPV6)
628/**
629 * batadv_mcast_is_report_ipv6 - check for MLD reports
630 * @skb: the ethernet frame destined for the mesh
631 *
632 * This call might reallocate skb data.
633 *
634 * Checks whether the given frame is a valid MLD report.
635 *
636 * Return: If so then true, otherwise false.
637 */
638static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
639{
640 if (ipv6_mc_check_mld(skb, NULL) < 0)
641 return false;
642
643 switch (icmp6_hdr(skb)->icmp6_type) {
644 case ICMPV6_MGM_REPORT:
645 case ICMPV6_MLD2_REPORT:
646 return true;
647 }
648
649 return false;
650}
651
Linus Lüssingab498862014-02-15 17:47:53 +0100652/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100653 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
654 * @bat_priv: the bat priv with all the soft interface information
655 * @skb: the IPv6 packet to check
Linus Lüssingab498862014-02-15 17:47:53 +0100656 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100657 *
658 * Checks whether the given IPv6 packet has the potential to be forwarded with a
659 * mode more optimal than classic flooding.
660 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200661 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100662 */
663static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100664 struct sk_buff *skb,
665 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100666{
667 struct ipv6hdr *ip6hdr;
668
669 /* We might fail due to out-of-memory -> drop it */
670 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
671 return -ENOMEM;
672
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200673 if (batadv_mcast_is_report_ipv6(skb))
674 return -EINVAL;
675
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100676 ip6hdr = ipv6_hdr(skb);
677
678 /* TODO: Implement Multicast Router Discovery (RFC4286),
679 * then allow scope > link local, too
680 */
681 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
682 return -EINVAL;
683
684 /* link-local-all-nodes multicast listeners behind a bridge are
685 * not snoopable (see RFC4541, section 3, paragraph 3)
686 */
687 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
Linus Lüssingab498862014-02-15 17:47:53 +0100688 *is_unsnoopable = true;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100689
690 return 0;
691}
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200692#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100693
694/**
695 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
696 * @bat_priv: the bat priv with all the soft interface information
697 * @skb: the multicast frame to check
Linus Lüssingab498862014-02-15 17:47:53 +0100698 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100699 *
700 * Checks whether the given multicast ethernet frame has the potential to be
701 * forwarded with a mode more optimal than classic flooding.
702 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200703 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100704 */
705static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100706 struct sk_buff *skb,
707 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100708{
709 struct ethhdr *ethhdr = eth_hdr(skb);
710
711 if (!atomic_read(&bat_priv->multicast_mode))
712 return -EINVAL;
713
714 if (atomic_read(&bat_priv->mcast.num_disabled))
715 return -EINVAL;
716
717 switch (ntohs(ethhdr->h_proto)) {
Linus Lüssingab498862014-02-15 17:47:53 +0100718 case ETH_P_IP:
719 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
720 is_unsnoopable);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200721#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100722 case ETH_P_IPV6:
Linus Lüssingab498862014-02-15 17:47:53 +0100723 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
724 is_unsnoopable);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200725#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100726 default:
727 return -EINVAL;
728 }
729}
730
731/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100732 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
733 * interest
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100734 * @bat_priv: the bat priv with all the soft interface information
735 * @ethhdr: ethernet header of a packet
736 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200737 * Return: the number of nodes which want all IPv4 multicast traffic if the
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100738 * given ethhdr is from an IPv4 packet or the number of nodes which want all
739 * IPv6 traffic if it matches an IPv6 packet.
740 */
741static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
742 struct ethhdr *ethhdr)
743{
744 switch (ntohs(ethhdr->h_proto)) {
745 case ETH_P_IP:
746 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
747 case ETH_P_IPV6:
748 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
749 default:
750 /* we shouldn't be here... */
751 return 0;
752 }
753}
754
755/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100756 * batadv_mcast_forw_tt_node_get - get a multicast tt node
757 * @bat_priv: the bat priv with all the soft interface information
758 * @ethhdr: the ether header containing the multicast destination
759 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200760 * Return: an orig_node matching the multicast address provided by ethhdr
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100761 * via a translation table lookup. This increases the returned nodes refcount.
762 */
763static struct batadv_orig_node *
764batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
765 struct ethhdr *ethhdr)
766{
767 return batadv_transtable_search(bat_priv, ethhdr->h_source,
768 ethhdr->h_dest, BATADV_NO_FLAGS);
769}
770
771/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100772 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100773 * @bat_priv: the bat priv with all the soft interface information
774 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200775 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100776 * increases its refcount.
777 */
778static struct batadv_orig_node *
779batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
780{
781 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
782
783 rcu_read_lock();
784 hlist_for_each_entry_rcu(tmp_orig_node,
785 &bat_priv->mcast.want_all_ipv4_list,
786 mcast_want_all_ipv4_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100787 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100788 continue;
789
790 orig_node = tmp_orig_node;
791 break;
792 }
793 rcu_read_unlock();
794
795 return orig_node;
796}
797
798/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100799 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100800 * @bat_priv: the bat priv with all the soft interface information
801 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200802 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100803 * and increases its refcount.
804 */
805static struct batadv_orig_node *
806batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
807{
808 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
809
810 rcu_read_lock();
811 hlist_for_each_entry_rcu(tmp_orig_node,
812 &bat_priv->mcast.want_all_ipv6_list,
813 mcast_want_all_ipv6_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100814 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100815 continue;
816
817 orig_node = tmp_orig_node;
818 break;
819 }
820 rcu_read_unlock();
821
822 return orig_node;
823}
824
825/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100826 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100827 * @bat_priv: the bat priv with all the soft interface information
828 * @ethhdr: an ethernet header to determine the protocol family from
829 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200830 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100831 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
832 * increases its refcount.
833 */
834static struct batadv_orig_node *
835batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
836 struct ethhdr *ethhdr)
837{
838 switch (ntohs(ethhdr->h_proto)) {
839 case ETH_P_IP:
840 return batadv_mcast_forw_ipv4_node_get(bat_priv);
841 case ETH_P_IPV6:
842 return batadv_mcast_forw_ipv6_node_get(bat_priv);
843 default:
844 /* we shouldn't be here... */
845 return NULL;
846 }
847}
848
849/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100850 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
Linus Lüssingab498862014-02-15 17:47:53 +0100851 * @bat_priv: the bat priv with all the soft interface information
852 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200853 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
Linus Lüssingab498862014-02-15 17:47:53 +0100854 * set and increases its refcount.
855 */
856static struct batadv_orig_node *
857batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
858{
859 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
860
861 rcu_read_lock();
862 hlist_for_each_entry_rcu(tmp_orig_node,
863 &bat_priv->mcast.want_all_unsnoopables_list,
864 mcast_want_all_unsnoopables_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100865 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssingab498862014-02-15 17:47:53 +0100866 continue;
867
868 orig_node = tmp_orig_node;
869 break;
870 }
871 rcu_read_unlock();
872
873 return orig_node;
874}
875
876/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100877 * batadv_mcast_forw_mode - check on how to forward a multicast packet
878 * @bat_priv: the bat priv with all the soft interface information
879 * @skb: The multicast packet to check
880 * @orig: an originator to be set to forward the skb to
881 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200882 * Return: the forwarding mode as enum batadv_forw_mode and in case of
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100883 * BATADV_FORW_SINGLE set the orig to the single originator the skb
884 * should be forwarded to.
885 */
886enum batadv_forw_mode
887batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
888 struct batadv_orig_node **orig)
889{
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100890 int ret, tt_count, ip_count, unsnoop_count, total_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100891 bool is_unsnoopable = false;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100892 struct ethhdr *ethhdr;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100893
Linus Lüssingab498862014-02-15 17:47:53 +0100894 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100895 if (ret == -ENOMEM)
896 return BATADV_FORW_NONE;
897 else if (ret < 0)
898 return BATADV_FORW_ALL;
899
900 ethhdr = eth_hdr(skb);
901
902 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
903 BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100904 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100905 unsnoop_count = !is_unsnoopable ? 0 :
906 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100907
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100908 total_count = tt_count + ip_count + unsnoop_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100909
910 switch (total_count) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100911 case 1:
Linus Lüssingab498862014-02-15 17:47:53 +0100912 if (tt_count)
913 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100914 else if (ip_count)
915 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100916 else if (unsnoop_count)
917 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
918
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100919 if (*orig)
920 return BATADV_FORW_SINGLE;
921
922 /* fall through */
923 case 0:
924 return BATADV_FORW_NONE;
925 default:
926 return BATADV_FORW_ALL;
927 }
928}
929
930/**
Linus Lüssingab498862014-02-15 17:47:53 +0100931 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
932 * @bat_priv: the bat priv with all the soft interface information
933 * @orig: the orig_node which multicast state might have changed of
934 * @mcast_flags: flags indicating the new multicast state
935 *
936 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
937 * orig, has toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200938 *
939 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssingab498862014-02-15 17:47:53 +0100940 */
941static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
942 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200943 u8 mcast_flags)
Linus Lüssingab498862014-02-15 17:47:53 +0100944{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200945 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
946 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
947
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200948 lockdep_assert_held(&orig->mcast_handler_lock);
949
Linus Lüssingab498862014-02-15 17:47:53 +0100950 /* switched from flag unset to set */
951 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
952 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
953 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
954
955 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200956 /* flag checks above + mcast_handler_lock prevents this */
957 WARN_ON(!hlist_unhashed(node));
958
959 hlist_add_head_rcu(node, head);
Linus Lüssingab498862014-02-15 17:47:53 +0100960 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
961 /* switched from flag set to unset */
962 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
963 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
964 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
965
966 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200967 /* flag checks above + mcast_handler_lock prevents this */
968 WARN_ON(hlist_unhashed(node));
969
970 hlist_del_init_rcu(node);
Linus Lüssingab498862014-02-15 17:47:53 +0100971 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
972 }
973}
974
975/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100976 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
977 * @bat_priv: the bat priv with all the soft interface information
978 * @orig: the orig_node which multicast state might have changed of
979 * @mcast_flags: flags indicating the new multicast state
980 *
981 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
982 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200983 *
984 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100985 */
986static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
987 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200988 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100989{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200990 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
991 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
992
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200993 lockdep_assert_held(&orig->mcast_handler_lock);
994
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100995 /* switched from flag unset to set */
996 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
997 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
998 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
999
1000 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001001 /* flag checks above + mcast_handler_lock prevents this */
1002 WARN_ON(!hlist_unhashed(node));
1003
1004 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001005 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1006 /* switched from flag set to unset */
1007 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1008 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1009 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1010
1011 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001012 /* flag checks above + mcast_handler_lock prevents this */
1013 WARN_ON(hlist_unhashed(node));
1014
1015 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001016 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1017 }
1018}
1019
1020/**
1021 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1022 * @bat_priv: the bat priv with all the soft interface information
1023 * @orig: the orig_node which multicast state might have changed of
1024 * @mcast_flags: flags indicating the new multicast state
1025 *
1026 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1027 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001028 *
1029 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001030 */
1031static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1032 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001033 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001034{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001035 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1036 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1037
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001038 lockdep_assert_held(&orig->mcast_handler_lock);
1039
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001040 /* switched from flag unset to set */
1041 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1042 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1043 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1044
1045 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001046 /* flag checks above + mcast_handler_lock prevents this */
1047 WARN_ON(!hlist_unhashed(node));
1048
1049 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001050 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1051 /* switched from flag set to unset */
1052 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1053 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1054 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1055
1056 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001057 /* flag checks above + mcast_handler_lock prevents this */
1058 WARN_ON(hlist_unhashed(node));
1059
1060 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001061 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1062 }
1063}
1064
1065/**
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001066 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
Linus Lüssing60432d72014-02-15 17:47:51 +01001067 * @bat_priv: the bat priv with all the soft interface information
1068 * @orig: the orig_node of the ogm
1069 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1070 * @tvlv_value: tvlv buffer containing the multicast data
1071 * @tvlv_value_len: tvlv buffer length
1072 */
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001073static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1074 struct batadv_orig_node *orig,
1075 u8 flags,
1076 void *tvlv_value,
1077 u16 tvlv_value_len)
Linus Lüssing60432d72014-02-15 17:47:51 +01001078{
1079 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001080 u8 mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing60432d72014-02-15 17:47:51 +01001081 bool orig_initialized;
1082
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001083 if (orig_mcast_enabled && tvlv_value &&
1084 (tvlv_value_len >= sizeof(mcast_flags)))
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001085 mcast_flags = *(u8 *)tvlv_value;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001086
1087 spin_lock_bh(&orig->mcast_handler_lock);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001088 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1089 &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01001090
1091 /* If mcast support is turned on decrease the disabled mcast node
1092 * counter only if we had increased it for this node before. If this
1093 * is a completely new orig_node no need to decrease the counter.
1094 */
1095 if (orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02001096 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing60432d72014-02-15 17:47:51 +01001097 if (orig_initialized)
1098 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001099 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssinge8829f02014-10-30 05:40:46 +01001100 /* If mcast support is being switched off or if this is an initial
1101 * OGM without mcast support then increase the disabled mcast
1102 * node counter.
Linus Lüssing60432d72014-02-15 17:47:51 +01001103 */
1104 } else if (!orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02001105 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
Linus Lüssinge8829f02014-10-30 05:40:46 +01001106 !orig_initialized)) {
Linus Lüssing60432d72014-02-15 17:47:51 +01001107 atomic_inc(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001108 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +01001109 }
1110
Linus Lüssing9c936e32015-06-16 17:10:25 +02001111 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01001112
Linus Lüssingab498862014-02-15 17:47:53 +01001113 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001114 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1115 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
Linus Lüssingab498862014-02-15 17:47:53 +01001116
Linus Lüssing60432d72014-02-15 17:47:51 +01001117 orig->mcast_flags = mcast_flags;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001118 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001119}
1120
1121/**
1122 * batadv_mcast_init - initialize the multicast optimizations structures
1123 * @bat_priv: the bat priv with all the soft interface information
1124 */
1125void batadv_mcast_init(struct batadv_priv *bat_priv)
1126{
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001127 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1128 NULL, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +01001129 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1130}
1131
1132/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001133 * batadv_mcast_free - free the multicast optimizations structures
1134 * @bat_priv: the bat priv with all the soft interface information
1135 */
1136void batadv_mcast_free(struct batadv_priv *bat_priv)
1137{
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001138 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1139 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
Linus Lüssing60432d72014-02-15 17:47:51 +01001140
Simon Wunderlichaf63cf52015-11-30 17:34:01 +01001141 spin_lock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001142 batadv_mcast_mla_tt_retract(bat_priv, NULL);
Simon Wunderlichaf63cf52015-11-30 17:34:01 +01001143 spin_unlock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001144}
Linus Lüssing60432d72014-02-15 17:47:51 +01001145
1146/**
1147 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1148 * @orig: the originator which is going to get purged
1149 */
1150void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1151{
1152 struct batadv_priv *bat_priv = orig->bat_priv;
1153
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001154 spin_lock_bh(&orig->mcast_handler_lock);
1155
Linus Lüssing9c936e32015-06-16 17:10:25 +02001156 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1157 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
Linus Lüssing60432d72014-02-15 17:47:51 +01001158 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssingab498862014-02-15 17:47:53 +01001159
1160 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001161 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1162 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001163
1164 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001165}