blob: 0e7d78f4f1b838ba841263b6dfc43aace09295d9 [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>
Linus Lüssing4e3e8232016-05-10 18:41:27 +020044#include <linux/seq_file.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020045#include <linux/skbuff.h>
46#include <linux/slab.h>
47#include <linux/spinlock.h>
48#include <linux/stddef.h>
49#include <linux/string.h>
50#include <linux/types.h>
51#include <net/addrconf.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020052#include <net/if_inet6.h>
53#include <net/ip.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020054#include <net/ipv6.h>
55
Linus Lüssing4e3e8232016-05-10 18:41:27 +020056#include "hard-interface.h"
57#include "hash.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020058#include "packet.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010059#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020060#include "tvlv.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010061
62/**
Linus Lüssing687937a2016-05-10 18:41:25 +020063 * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
64 * @soft_iface: netdev struct of the mesh interface
65 *
66 * If the given soft interface has a bridge on top then the refcount
67 * of the according net device is increased.
68 *
69 * Return: NULL if no such bridge exists. Otherwise the net device of the
70 * bridge.
71 */
72static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
73{
74 struct net_device *upper = soft_iface;
75
76 rcu_read_lock();
77 do {
78 upper = netdev_master_upper_dev_get_rcu(upper);
79 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
80
81 if (upper)
82 dev_hold(upper);
83 rcu_read_unlock();
84
85 return upper;
86}
87
88/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010089 * batadv_mcast_mla_softif_get - get softif multicast listeners
90 * @dev: the device to collect multicast addresses from
91 * @mcast_list: a list to put found addresses into
92 *
Linus Lüssing687937a2016-05-10 18:41:25 +020093 * Collects multicast addresses of multicast listeners residing
94 * on this kernel on the given soft interface, dev, in
95 * the given mcast_list. In general, multicast listeners provided by
96 * your multicast receiving applications run directly on this node.
97 *
98 * If there is a bridge interface on top of dev, collects from that one
99 * instead. Just like with IP addresses and routes, multicast listeners
100 * will(/should) register to the bridge interface instead of an
101 * enslaved bat0.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100102 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200103 * Return: -ENOMEM on memory allocation error or the number of
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100104 * items added to the mcast_list otherwise.
105 */
106static int batadv_mcast_mla_softif_get(struct net_device *dev,
107 struct hlist_head *mcast_list)
108{
Linus Lüssing687937a2016-05-10 18:41:25 +0200109 struct net_device *bridge = batadv_mcast_get_bridge(dev);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100110 struct netdev_hw_addr *mc_list_entry;
111 struct batadv_hw_addr *new;
112 int ret = 0;
113
Linus Lüssing687937a2016-05-10 18:41:25 +0200114 netif_addr_lock_bh(bridge ? bridge : dev);
115 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100116 new = kmalloc(sizeof(*new), GFP_ATOMIC);
117 if (!new) {
118 ret = -ENOMEM;
119 break;
120 }
121
122 ether_addr_copy(new->addr, mc_list_entry->addr);
123 hlist_add_head(&new->list, mcast_list);
124 ret++;
125 }
Linus Lüssing687937a2016-05-10 18:41:25 +0200126 netif_addr_unlock_bh(bridge ? bridge : dev);
127
128 if (bridge)
129 dev_put(bridge);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100130
131 return ret;
132}
133
134/**
135 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
136 * @mcast_addr: the multicast address to check
137 * @mcast_list: the list with multicast addresses to search in
138 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200139 * Return: true if the given address is already in the given list.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100140 * Otherwise returns false.
141 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200142static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100143 struct hlist_head *mcast_list)
144{
145 struct batadv_hw_addr *mcast_entry;
146
147 hlist_for_each_entry(mcast_entry, mcast_list, list)
148 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
149 return true;
150
151 return false;
152}
153
154/**
Linus Lüssing687937a2016-05-10 18:41:25 +0200155 * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
156 * @dst: destination to write to - a multicast MAC address
157 * @src: source to read from - a multicast IP address
158 *
159 * Converts a given multicast IPv4/IPv6 address from a bridge
160 * to its matching multicast MAC address and copies it into the given
161 * destination buffer.
162 *
163 * Caller needs to make sure the destination buffer can hold
164 * at least ETH_ALEN bytes.
165 */
166static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
167{
168 if (src->proto == htons(ETH_P_IP))
169 ip_eth_mc_map(src->u.ip4, dst);
170#if IS_ENABLED(CONFIG_IPV6)
171 else if (src->proto == htons(ETH_P_IPV6))
172 ipv6_eth_mc_map(&src->u.ip6, dst);
173#endif
174 else
175 eth_zero_addr(dst);
176}
177
178/**
179 * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
180 * @dev: a bridge slave whose bridge to collect multicast addresses from
181 * @mcast_list: a list to put found addresses into
182 *
183 * Collects multicast addresses of multicast listeners residing
184 * on foreign, non-mesh devices which we gave access to our mesh via
185 * a bridge on top of the given soft interface, dev, in the given
186 * mcast_list.
187 *
188 * Return: -ENOMEM on memory allocation error or the number of
189 * items added to the mcast_list otherwise.
190 */
191static int batadv_mcast_mla_bridge_get(struct net_device *dev,
192 struct hlist_head *mcast_list)
193{
194 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
195 struct br_ip_list *br_ip_entry, *tmp;
196 struct batadv_hw_addr *new;
197 u8 mcast_addr[ETH_ALEN];
198 int ret;
199
200 /* we don't need to detect these devices/listeners, the IGMP/MLD
201 * snooping code of the Linux bridge already does that for us
202 */
203 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
204 if (ret < 0)
205 goto out;
206
207 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
208 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
209 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
210 continue;
211
212 new = kmalloc(sizeof(*new), GFP_ATOMIC);
213 if (!new) {
214 ret = -ENOMEM;
215 break;
216 }
217
218 ether_addr_copy(new->addr, mcast_addr);
219 hlist_add_head(&new->list, mcast_list);
220 }
221
222out:
223 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
224 list_del(&br_ip_entry->list);
225 kfree(br_ip_entry);
226 }
227
228 return ret;
229}
230
231/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100232 * batadv_mcast_mla_list_free - free a list of multicast addresses
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200233 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100234 * @mcast_list: the list to free
235 *
236 * Removes and frees all items in the given mcast_list.
237 */
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200238static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
239 struct hlist_head *mcast_list)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100240{
241 struct batadv_hw_addr *mcast_entry;
242 struct hlist_node *tmp;
243
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200244 lockdep_assert_held(&bat_priv->tt.commit_lock);
245
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100246 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
247 hlist_del(&mcast_entry->list);
248 kfree(mcast_entry);
249 }
250}
251
252/**
253 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
254 * @bat_priv: the bat priv with all the soft interface information
255 * @mcast_list: a list of addresses which should _not_ be removed
256 *
257 * Retracts the announcement of any multicast listener from the
258 * translation table except the ones listed in the given mcast_list.
259 *
260 * If mcast_list is NULL then all are retracted.
261 */
262static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
263 struct hlist_head *mcast_list)
264{
265 struct batadv_hw_addr *mcast_entry;
266 struct hlist_node *tmp;
267
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200268 lockdep_assert_held(&bat_priv->tt.commit_lock);
269
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100270 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
271 list) {
272 if (mcast_list &&
273 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
274 mcast_list))
275 continue;
276
277 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
278 BATADV_NO_FLAGS,
279 "mcast TT outdated", false);
280
281 hlist_del(&mcast_entry->list);
282 kfree(mcast_entry);
283 }
284}
285
286/**
287 * batadv_mcast_mla_tt_add - add multicast listener announcements
288 * @bat_priv: the bat priv with all the soft interface information
289 * @mcast_list: a list of addresses which are going to get added
290 *
291 * Adds multicast listener announcements from the given mcast_list to the
292 * translation table if they have not been added yet.
293 */
294static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
295 struct hlist_head *mcast_list)
296{
297 struct batadv_hw_addr *mcast_entry;
298 struct hlist_node *tmp;
299
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200300 lockdep_assert_held(&bat_priv->tt.commit_lock);
301
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100302 if (!mcast_list)
303 return;
304
305 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
306 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
307 &bat_priv->mcast.mla_list))
308 continue;
309
310 if (!batadv_tt_local_add(bat_priv->soft_iface,
311 mcast_entry->addr, BATADV_NO_FLAGS,
312 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
313 continue;
314
315 hlist_del(&mcast_entry->list);
316 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
317 }
318}
319
320/**
321 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
322 * @bat_priv: the bat priv with all the soft interface information
323 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200324 * Checks whether there is a bridge on top of our soft interface.
325 *
326 * Return: true if there is a bridge, false otherwise.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100327 */
328static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
329{
330 struct net_device *upper = bat_priv->soft_iface;
331
332 rcu_read_lock();
333 do {
334 upper = netdev_master_upper_dev_get_rcu(upper);
335 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
336 rcu_read_unlock();
337
338 return upper;
339}
340
341/**
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200342 * batadv_mcast_querier_log - debug output regarding the querier status on link
343 * @bat_priv: the bat priv with all the soft interface information
344 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
345 * @old_state: the previous querier state on our link
346 * @new_state: the new querier state on our link
347 *
348 * Outputs debug messages to the logging facility with log level 'mcast'
349 * regarding changes to the querier status on the link which are relevant
350 * to our multicast optimizations.
351 *
352 * Usually this is about whether a querier appeared or vanished in
353 * our mesh or whether the querier is in the suboptimal position of being
354 * behind our local bridge segment: Snooping switches will directly
355 * forward listener reports to the querier, therefore batman-adv and
356 * the bridge will potentially not see these listeners - the querier is
357 * potentially shadowing listeners from us then.
358 *
359 * This is only interesting for nodes with a bridge on top of their
360 * soft interface.
361 */
362static void
363batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
364 struct batadv_mcast_querier_state *old_state,
365 struct batadv_mcast_querier_state *new_state)
366{
367 if (!old_state->exists && new_state->exists)
368 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
369 str_proto);
370 else if (old_state->exists && !new_state->exists)
371 batadv_info(bat_priv->soft_iface,
372 "%s Querier disappeared - multicast optimizations disabled\n",
373 str_proto);
374 else if (!bat_priv->mcast.bridged && !new_state->exists)
375 batadv_info(bat_priv->soft_iface,
376 "No %s Querier present - multicast optimizations disabled\n",
377 str_proto);
378
379 if (new_state->exists) {
380 if ((!old_state->shadowing && new_state->shadowing) ||
381 (!old_state->exists && new_state->shadowing))
382 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
383 "%s Querier is behind our bridged segment: Might shadow listeners\n",
384 str_proto);
385 else if (old_state->shadowing && !new_state->shadowing)
386 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
387 "%s Querier is not behind our bridged segment\n",
388 str_proto);
389 }
390}
391
392/**
393 * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
394 * @bat_priv: the bat priv with all the soft interface information
395 * @bridged: a flag about whether the soft interface is currently bridged or not
396 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
397 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
398 *
399 * If no bridges are ever used on this node, then this function does nothing.
400 *
401 * Otherwise this function outputs debug information to the 'mcast' log level
402 * which might be relevant to our multicast optimizations.
403 *
404 * More precisely, it outputs information when a bridge interface is added or
405 * removed from a soft interface. And when a bridge is present, it further
406 * outputs information about the querier state which is relevant for the
407 * multicast flags this node is going to set.
408 */
409static void
410batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
411 struct batadv_mcast_querier_state *querier_ipv4,
412 struct batadv_mcast_querier_state *querier_ipv6)
413{
414 if (!bat_priv->mcast.bridged && bridged)
415 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
416 "Bridge added: Setting Unsnoopables(U)-flag\n");
417 else if (bat_priv->mcast.bridged && !bridged)
418 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
419 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
420
421 if (bridged) {
422 batadv_mcast_querier_log(bat_priv, "IGMP",
423 &bat_priv->mcast.querier_ipv4,
424 querier_ipv4);
425 batadv_mcast_querier_log(bat_priv, "MLD",
426 &bat_priv->mcast.querier_ipv6,
427 querier_ipv6);
428 }
429}
430
431/**
432 * batadv_mcast_flags_logs - output debug information about mcast flag changes
433 * @bat_priv: the bat priv with all the soft interface information
434 * @flags: flags indicating the new multicast state
435 *
436 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
437 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
438 */
439static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
440{
441 u8 old_flags = bat_priv->mcast.flags;
442 char str_old_flags[] = "[...]";
443
444 sprintf(str_old_flags, "[%c%c%c]",
445 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
446 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
447 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
448
449 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
450 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
451 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
452 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
453 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
454 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
455}
456
457/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100458 * batadv_mcast_mla_tvlv_update - update multicast tvlv
459 * @bat_priv: the bat priv with all the soft interface information
460 *
461 * Updates the own multicast tvlv with our current multicast related settings,
462 * capabilities and inabilities.
463 *
Linus Lüssing687937a2016-05-10 18:41:25 +0200464 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
465 * otherwise.
Linus Lüssing60432d72014-02-15 17:47:51 +0100466 */
467static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
468{
469 struct batadv_tvlv_mcast_data mcast_data;
Linus Lüssing687937a2016-05-10 18:41:25 +0200470 struct batadv_mcast_querier_state querier4 = {false, false};
471 struct batadv_mcast_querier_state querier6 = {false, false};
472 struct net_device *dev = bat_priv->soft_iface;
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200473 bool bridged;
Linus Lüssing60432d72014-02-15 17:47:51 +0100474
475 mcast_data.flags = BATADV_NO_FLAGS;
476 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
477
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200478 bridged = batadv_mcast_has_bridge(bat_priv);
479 if (!bridged)
Linus Lüssing687937a2016-05-10 18:41:25 +0200480 goto update;
481
482#if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
483 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
484#endif
485
486 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
487 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
488
489 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
490 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
491
492 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
493
494 /* 1) If no querier exists at all, then multicast listeners on
495 * our local TT clients behind the bridge will keep silent.
496 * 2) If the selected querier is on one of our local TT clients,
497 * behind the bridge, then this querier might shadow multicast
498 * listeners on our local TT clients, behind this bridge.
499 *
500 * In both cases, we will signalize other batman nodes that
501 * we need all multicast traffic of the according protocol.
Linus Lüssing60432d72014-02-15 17:47:51 +0100502 */
Linus Lüssing687937a2016-05-10 18:41:25 +0200503 if (!querier4.exists || querier4.shadowing)
504 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
Linus Lüssing60432d72014-02-15 17:47:51 +0100505
Linus Lüssing687937a2016-05-10 18:41:25 +0200506 if (!querier6.exists || querier6.shadowing)
507 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
Linus Lüssing60432d72014-02-15 17:47:51 +0100508
Linus Lüssing687937a2016-05-10 18:41:25 +0200509update:
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200510 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
511
512 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
513 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
514
515 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
516 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
517
518 bat_priv->mcast.bridged = bridged;
519
Linus Lüssing60432d72014-02-15 17:47:51 +0100520 if (!bat_priv->mcast.enabled ||
521 mcast_data.flags != bat_priv->mcast.flags) {
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200522 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200523 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +0100524 &mcast_data, sizeof(mcast_data));
525 bat_priv->mcast.flags = mcast_data.flags;
526 bat_priv->mcast.enabled = true;
527 }
528
Linus Lüssing687937a2016-05-10 18:41:25 +0200529 return !(mcast_data.flags &
530 (BATADV_MCAST_WANT_ALL_IPV4 + BATADV_MCAST_WANT_ALL_IPV6));
Linus Lüssing60432d72014-02-15 17:47:51 +0100531}
532
533/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100534 * batadv_mcast_mla_update - update the own MLAs
535 * @bat_priv: the bat priv with all the soft interface information
536 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100537 * Updates the own multicast listener announcements in the translation
538 * table as well as the own, announced multicast tvlv container.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100539 */
540void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
541{
542 struct net_device *soft_iface = bat_priv->soft_iface;
543 struct hlist_head mcast_list = HLIST_HEAD_INIT;
544 int ret;
545
Linus Lüssing60432d72014-02-15 17:47:51 +0100546 if (!batadv_mcast_mla_tvlv_update(bat_priv))
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100547 goto update;
548
549 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
550 if (ret < 0)
551 goto out;
552
Linus Lüssing687937a2016-05-10 18:41:25 +0200553 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
554 if (ret < 0)
555 goto out;
556
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100557update:
558 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
559 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
560
561out:
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200562 batadv_mcast_mla_list_free(bat_priv, &mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100563}
564
565/**
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200566 * batadv_mcast_is_report_ipv4 - check for IGMP reports
567 * @skb: the ethernet frame destined for the mesh
568 *
569 * This call might reallocate skb data.
570 *
571 * Checks whether the given frame is a valid IGMP report.
572 *
573 * Return: If so then true, otherwise false.
574 */
575static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
576{
577 if (ip_mc_check_igmp(skb, NULL) < 0)
578 return false;
579
580 switch (igmp_hdr(skb)->type) {
581 case IGMP_HOST_MEMBERSHIP_REPORT:
582 case IGMPV2_HOST_MEMBERSHIP_REPORT:
583 case IGMPV3_HOST_MEMBERSHIP_REPORT:
584 return true;
585 }
586
587 return false;
588}
589
590/**
Linus Lüssingab498862014-02-15 17:47:53 +0100591 * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
592 * @bat_priv: the bat priv with all the soft interface information
593 * @skb: the IPv4 packet to check
594 * @is_unsnoopable: stores whether the destination is snoopable
595 *
596 * Checks whether the given IPv4 packet has the potential to be forwarded with a
597 * mode more optimal than classic flooding.
598 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200599 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
600 * allocation failure.
Linus Lüssingab498862014-02-15 17:47:53 +0100601 */
602static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
603 struct sk_buff *skb,
604 bool *is_unsnoopable)
605{
606 struct iphdr *iphdr;
607
608 /* We might fail due to out-of-memory -> drop it */
609 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
610 return -ENOMEM;
611
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200612 if (batadv_mcast_is_report_ipv4(skb))
613 return -EINVAL;
614
Linus Lüssingab498862014-02-15 17:47:53 +0100615 iphdr = ip_hdr(skb);
616
617 /* TODO: Implement Multicast Router Discovery (RFC4286),
618 * then allow scope > link local, too
619 */
620 if (!ipv4_is_local_multicast(iphdr->daddr))
621 return -EINVAL;
622
623 /* link-local multicast listeners behind a bridge are
624 * not snoopable (see RFC4541, section 2.1.2.2)
625 */
626 *is_unsnoopable = true;
627
628 return 0;
629}
630
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200631#if IS_ENABLED(CONFIG_IPV6)
632/**
633 * batadv_mcast_is_report_ipv6 - check for MLD reports
634 * @skb: the ethernet frame destined for the mesh
635 *
636 * This call might reallocate skb data.
637 *
638 * Checks whether the given frame is a valid MLD report.
639 *
640 * Return: If so then true, otherwise false.
641 */
642static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
643{
644 if (ipv6_mc_check_mld(skb, NULL) < 0)
645 return false;
646
647 switch (icmp6_hdr(skb)->icmp6_type) {
648 case ICMPV6_MGM_REPORT:
649 case ICMPV6_MLD2_REPORT:
650 return true;
651 }
652
653 return false;
654}
655
Linus Lüssingab498862014-02-15 17:47:53 +0100656/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100657 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
658 * @bat_priv: the bat priv with all the soft interface information
659 * @skb: the IPv6 packet to check
Linus Lüssingab498862014-02-15 17:47:53 +0100660 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100661 *
662 * Checks whether the given IPv6 packet has the potential to be forwarded with a
663 * mode more optimal than classic flooding.
664 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200665 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100666 */
667static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100668 struct sk_buff *skb,
669 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100670{
671 struct ipv6hdr *ip6hdr;
672
673 /* We might fail due to out-of-memory -> drop it */
674 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
675 return -ENOMEM;
676
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200677 if (batadv_mcast_is_report_ipv6(skb))
678 return -EINVAL;
679
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100680 ip6hdr = ipv6_hdr(skb);
681
682 /* TODO: Implement Multicast Router Discovery (RFC4286),
683 * then allow scope > link local, too
684 */
685 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
686 return -EINVAL;
687
688 /* link-local-all-nodes multicast listeners behind a bridge are
689 * not snoopable (see RFC4541, section 3, paragraph 3)
690 */
691 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
Linus Lüssingab498862014-02-15 17:47:53 +0100692 *is_unsnoopable = true;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100693
694 return 0;
695}
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200696#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100697
698/**
699 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
700 * @bat_priv: the bat priv with all the soft interface information
701 * @skb: the multicast frame to check
Linus Lüssingab498862014-02-15 17:47:53 +0100702 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100703 *
704 * Checks whether the given multicast ethernet frame has the potential to be
705 * forwarded with a mode more optimal than classic flooding.
706 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200707 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100708 */
709static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100710 struct sk_buff *skb,
711 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100712{
713 struct ethhdr *ethhdr = eth_hdr(skb);
714
715 if (!atomic_read(&bat_priv->multicast_mode))
716 return -EINVAL;
717
718 if (atomic_read(&bat_priv->mcast.num_disabled))
719 return -EINVAL;
720
721 switch (ntohs(ethhdr->h_proto)) {
Linus Lüssingab498862014-02-15 17:47:53 +0100722 case ETH_P_IP:
723 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
724 is_unsnoopable);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200725#if IS_ENABLED(CONFIG_IPV6)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100726 case ETH_P_IPV6:
Linus Lüssingab498862014-02-15 17:47:53 +0100727 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
728 is_unsnoopable);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200729#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100730 default:
731 return -EINVAL;
732 }
733}
734
735/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100736 * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
737 * interest
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100738 * @bat_priv: the bat priv with all the soft interface information
739 * @ethhdr: ethernet header of a packet
740 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200741 * Return: the number of nodes which want all IPv4 multicast traffic if the
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100742 * given ethhdr is from an IPv4 packet or the number of nodes which want all
743 * IPv6 traffic if it matches an IPv6 packet.
744 */
745static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
746 struct ethhdr *ethhdr)
747{
748 switch (ntohs(ethhdr->h_proto)) {
749 case ETH_P_IP:
750 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
751 case ETH_P_IPV6:
752 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
753 default:
754 /* we shouldn't be here... */
755 return 0;
756 }
757}
758
759/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100760 * batadv_mcast_forw_tt_node_get - get a multicast tt node
761 * @bat_priv: the bat priv with all the soft interface information
762 * @ethhdr: the ether header containing the multicast destination
763 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200764 * Return: an orig_node matching the multicast address provided by ethhdr
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100765 * via a translation table lookup. This increases the returned nodes refcount.
766 */
767static struct batadv_orig_node *
768batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
769 struct ethhdr *ethhdr)
770{
771 return batadv_transtable_search(bat_priv, ethhdr->h_source,
772 ethhdr->h_dest, BATADV_NO_FLAGS);
773}
774
775/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100776 * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100777 * @bat_priv: the bat priv with all the soft interface information
778 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200779 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100780 * increases its refcount.
781 */
782static struct batadv_orig_node *
783batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
784{
785 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
786
787 rcu_read_lock();
788 hlist_for_each_entry_rcu(tmp_orig_node,
789 &bat_priv->mcast.want_all_ipv4_list,
790 mcast_want_all_ipv4_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100791 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100792 continue;
793
794 orig_node = tmp_orig_node;
795 break;
796 }
797 rcu_read_unlock();
798
799 return orig_node;
800}
801
802/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100803 * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100804 * @bat_priv: the bat priv with all the soft interface information
805 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200806 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100807 * and increases its refcount.
808 */
809static struct batadv_orig_node *
810batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
811{
812 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
813
814 rcu_read_lock();
815 hlist_for_each_entry_rcu(tmp_orig_node,
816 &bat_priv->mcast.want_all_ipv6_list,
817 mcast_want_all_ipv6_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100818 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100819 continue;
820
821 orig_node = tmp_orig_node;
822 break;
823 }
824 rcu_read_unlock();
825
826 return orig_node;
827}
828
829/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100830 * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100831 * @bat_priv: the bat priv with all the soft interface information
832 * @ethhdr: an ethernet header to determine the protocol family from
833 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200834 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100835 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
836 * increases its refcount.
837 */
838static struct batadv_orig_node *
839batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
840 struct ethhdr *ethhdr)
841{
842 switch (ntohs(ethhdr->h_proto)) {
843 case ETH_P_IP:
844 return batadv_mcast_forw_ipv4_node_get(bat_priv);
845 case ETH_P_IPV6:
846 return batadv_mcast_forw_ipv6_node_get(bat_priv);
847 default:
848 /* we shouldn't be here... */
849 return NULL;
850 }
851}
852
853/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100854 * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
Linus Lüssingab498862014-02-15 17:47:53 +0100855 * @bat_priv: the bat priv with all the soft interface information
856 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200857 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
Linus Lüssingab498862014-02-15 17:47:53 +0100858 * set and increases its refcount.
859 */
860static struct batadv_orig_node *
861batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
862{
863 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
864
865 rcu_read_lock();
866 hlist_for_each_entry_rcu(tmp_orig_node,
867 &bat_priv->mcast.want_all_unsnoopables_list,
868 mcast_want_all_unsnoopables_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100869 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssingab498862014-02-15 17:47:53 +0100870 continue;
871
872 orig_node = tmp_orig_node;
873 break;
874 }
875 rcu_read_unlock();
876
877 return orig_node;
878}
879
880/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100881 * batadv_mcast_forw_mode - check on how to forward a multicast packet
882 * @bat_priv: the bat priv with all the soft interface information
883 * @skb: The multicast packet to check
884 * @orig: an originator to be set to forward the skb to
885 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200886 * Return: the forwarding mode as enum batadv_forw_mode and in case of
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100887 * BATADV_FORW_SINGLE set the orig to the single originator the skb
888 * should be forwarded to.
889 */
890enum batadv_forw_mode
891batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
892 struct batadv_orig_node **orig)
893{
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100894 int ret, tt_count, ip_count, unsnoop_count, total_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100895 bool is_unsnoopable = false;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100896 struct ethhdr *ethhdr;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100897
Linus Lüssingab498862014-02-15 17:47:53 +0100898 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100899 if (ret == -ENOMEM)
900 return BATADV_FORW_NONE;
901 else if (ret < 0)
902 return BATADV_FORW_ALL;
903
904 ethhdr = eth_hdr(skb);
905
906 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
907 BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100908 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100909 unsnoop_count = !is_unsnoopable ? 0 :
910 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100911
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100912 total_count = tt_count + ip_count + unsnoop_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100913
914 switch (total_count) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100915 case 1:
Linus Lüssingab498862014-02-15 17:47:53 +0100916 if (tt_count)
917 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100918 else if (ip_count)
919 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +0100920 else if (unsnoop_count)
921 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
922
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100923 if (*orig)
924 return BATADV_FORW_SINGLE;
925
926 /* fall through */
927 case 0:
928 return BATADV_FORW_NONE;
929 default:
930 return BATADV_FORW_ALL;
931 }
932}
933
934/**
Linus Lüssingab498862014-02-15 17:47:53 +0100935 * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
936 * @bat_priv: the bat priv with all the soft interface information
937 * @orig: the orig_node which multicast state might have changed of
938 * @mcast_flags: flags indicating the new multicast state
939 *
940 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
941 * orig, has toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200942 *
943 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssingab498862014-02-15 17:47:53 +0100944 */
945static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
946 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200947 u8 mcast_flags)
Linus Lüssingab498862014-02-15 17:47:53 +0100948{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200949 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
950 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
951
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200952 lockdep_assert_held(&orig->mcast_handler_lock);
953
Linus Lüssingab498862014-02-15 17:47:53 +0100954 /* switched from flag unset to set */
955 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
956 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
957 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
958
959 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200960 /* flag checks above + mcast_handler_lock prevents this */
961 WARN_ON(!hlist_unhashed(node));
962
963 hlist_add_head_rcu(node, head);
Linus Lüssingab498862014-02-15 17:47:53 +0100964 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
965 /* switched from flag set to unset */
966 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
967 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
968 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
969
970 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200971 /* flag checks above + mcast_handler_lock prevents this */
972 WARN_ON(hlist_unhashed(node));
973
974 hlist_del_init_rcu(node);
Linus Lüssingab498862014-02-15 17:47:53 +0100975 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
976 }
977}
978
979/**
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100980 * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
981 * @bat_priv: the bat priv with all the soft interface information
982 * @orig: the orig_node which multicast state might have changed of
983 * @mcast_flags: flags indicating the new multicast state
984 *
985 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
986 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200987 *
988 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100989 */
990static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
991 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200992 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100993{
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200994 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
995 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
996
Sven Eckelmann5274cd62015-06-21 14:45:15 +0200997 lockdep_assert_held(&orig->mcast_handler_lock);
998
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100999 /* switched from flag unset to set */
1000 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1001 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1002 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1003
1004 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001005 /* flag checks above + mcast_handler_lock prevents this */
1006 WARN_ON(!hlist_unhashed(node));
1007
1008 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001009 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1010 /* switched from flag set to unset */
1011 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1012 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1013 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1014
1015 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001016 /* flag checks above + mcast_handler_lock prevents this */
1017 WARN_ON(hlist_unhashed(node));
1018
1019 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001020 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1021 }
1022}
1023
1024/**
1025 * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
1026 * @bat_priv: the bat priv with all the soft interface information
1027 * @orig: the orig_node which multicast state might have changed of
1028 * @mcast_flags: flags indicating the new multicast state
1029 *
1030 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1031 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001032 *
1033 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001034 */
1035static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1036 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001037 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001038{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001039 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1040 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1041
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001042 lockdep_assert_held(&orig->mcast_handler_lock);
1043
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001044 /* switched from flag unset to set */
1045 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1046 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1047 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1048
1049 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001050 /* flag checks above + mcast_handler_lock prevents this */
1051 WARN_ON(!hlist_unhashed(node));
1052
1053 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001054 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1055 /* switched from flag set to unset */
1056 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1057 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1058 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1059
1060 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001061 /* flag checks above + mcast_handler_lock prevents this */
1062 WARN_ON(hlist_unhashed(node));
1063
1064 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001065 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1066 }
1067}
1068
1069/**
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001070 * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
Linus Lüssing60432d72014-02-15 17:47:51 +01001071 * @bat_priv: the bat priv with all the soft interface information
1072 * @orig: the orig_node of the ogm
1073 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1074 * @tvlv_value: tvlv buffer containing the multicast data
1075 * @tvlv_value_len: tvlv buffer length
1076 */
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001077static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1078 struct batadv_orig_node *orig,
1079 u8 flags,
1080 void *tvlv_value,
1081 u16 tvlv_value_len)
Linus Lüssing60432d72014-02-15 17:47:51 +01001082{
1083 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001084 u8 mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing60432d72014-02-15 17:47:51 +01001085 bool orig_initialized;
1086
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001087 if (orig_mcast_enabled && tvlv_value &&
1088 (tvlv_value_len >= sizeof(mcast_flags)))
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001089 mcast_flags = *(u8 *)tvlv_value;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001090
1091 spin_lock_bh(&orig->mcast_handler_lock);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001092 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1093 &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01001094
1095 /* If mcast support is turned on decrease the disabled mcast node
1096 * counter only if we had increased it for this node before. If this
1097 * is a completely new orig_node no need to decrease the counter.
1098 */
1099 if (orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02001100 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing60432d72014-02-15 17:47:51 +01001101 if (orig_initialized)
1102 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001103 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssinge8829f02014-10-30 05:40:46 +01001104 /* If mcast support is being switched off or if this is an initial
1105 * OGM without mcast support then increase the disabled mcast
1106 * node counter.
Linus Lüssing60432d72014-02-15 17:47:51 +01001107 */
1108 } else if (!orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02001109 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
Linus Lüssinge8829f02014-10-30 05:40:46 +01001110 !orig_initialized)) {
Linus Lüssing60432d72014-02-15 17:47:51 +01001111 atomic_inc(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001112 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +01001113 }
1114
Linus Lüssing9c936e32015-06-16 17:10:25 +02001115 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01001116
Linus Lüssingab498862014-02-15 17:47:53 +01001117 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001118 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1119 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
Linus Lüssingab498862014-02-15 17:47:53 +01001120
Linus Lüssing60432d72014-02-15 17:47:51 +01001121 orig->mcast_flags = mcast_flags;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001122 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001123}
1124
1125/**
1126 * batadv_mcast_init - initialize the multicast optimizations structures
1127 * @bat_priv: the bat priv with all the soft interface information
1128 */
1129void batadv_mcast_init(struct batadv_priv *bat_priv)
1130{
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001131 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1132 NULL, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +01001133 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
1134}
1135
1136/**
Linus Lüssing4e3e8232016-05-10 18:41:27 +02001137 * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
1138 * @bat_priv: the bat priv with all the soft interface information
1139 * @seq: debugfs table seq_file struct
1140 *
1141 * Prints our own multicast flags including a more specific reason why
1142 * they are set, that is prints the bridge and querier state too, to
1143 * the debugfs table specified via @seq.
1144 */
1145static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1146 struct seq_file *seq)
1147{
1148 u8 flags = bat_priv->mcast.flags;
1149 char querier4, querier6, shadowing4, shadowing6;
1150 bool bridged = bat_priv->mcast.bridged;
1151
1152 if (bridged) {
1153 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1154 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1155 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1156 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1157 } else {
1158 querier4 = '?';
1159 querier6 = '?';
1160 shadowing4 = '?';
1161 shadowing6 = '?';
1162 }
1163
1164 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1165 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1166 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1167 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1168 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1169 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1170 querier4, querier6);
1171 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1172 shadowing4, shadowing6);
1173 seq_puts(seq, "-------------------------------------------\n");
1174 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1175}
1176
1177/**
1178 * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
1179 * @seq: seq file to print on
1180 * @offset: not used
1181 *
1182 * This prints a table of (primary) originators and their according
1183 * multicast flags, including (in the header) our own.
1184 *
1185 * Return: always 0
1186 */
1187int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1188{
1189 struct net_device *net_dev = (struct net_device *)seq->private;
1190 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1191 struct batadv_hard_iface *primary_if;
1192 struct batadv_hashtable *hash = bat_priv->orig_hash;
1193 struct batadv_orig_node *orig_node;
1194 struct hlist_head *head;
1195 u8 flags;
1196 u32 i;
1197
1198 primary_if = batadv_seq_print_text_primary_if_get(seq);
1199 if (!primary_if)
1200 return 0;
1201
1202 batadv_mcast_flags_print_header(bat_priv, seq);
1203
1204 for (i = 0; i < hash->size; i++) {
1205 head = &hash->table[i];
1206
1207 rcu_read_lock();
1208 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1209 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1210 &orig_node->capa_initialized))
1211 continue;
1212
1213 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1214 &orig_node->capabilities)) {
1215 seq_printf(seq, "%pM -\n", orig_node->orig);
1216 continue;
1217 }
1218
1219 flags = orig_node->mcast_flags;
1220
1221 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1222 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1223 ? 'U' : '.',
1224 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1225 ? '4' : '.',
1226 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1227 ? '6' : '.');
1228 }
1229 rcu_read_unlock();
1230 }
1231
1232 batadv_hardif_put(primary_if);
1233
1234 return 0;
1235}
1236
1237/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001238 * batadv_mcast_free - free the multicast optimizations structures
1239 * @bat_priv: the bat priv with all the soft interface information
1240 */
1241void batadv_mcast_free(struct batadv_priv *bat_priv)
1242{
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001243 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1244 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
Linus Lüssing60432d72014-02-15 17:47:51 +01001245
Simon Wunderlichaf63cf52015-11-30 17:34:01 +01001246 spin_lock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001247 batadv_mcast_mla_tt_retract(bat_priv, NULL);
Simon Wunderlichaf63cf52015-11-30 17:34:01 +01001248 spin_unlock_bh(&bat_priv->tt.commit_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001249}
Linus Lüssing60432d72014-02-15 17:47:51 +01001250
1251/**
1252 * batadv_mcast_purge_orig - reset originator global mcast state modifications
1253 * @orig: the originator which is going to get purged
1254 */
1255void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1256{
1257 struct batadv_priv *bat_priv = orig->bat_priv;
1258
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001259 spin_lock_bh(&orig->mcast_handler_lock);
1260
Linus Lüssing9c936e32015-06-16 17:10:25 +02001261 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1262 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
Linus Lüssing60432d72014-02-15 17:47:51 +01001263 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssingab498862014-02-15 17:47:53 +01001264
1265 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001266 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1267 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001268
1269 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001270}