blob: c7a1305ca7e746e79f07d4e90359ddddb0f243d9 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann6b1aea82018-01-01 00:00:00 +01002/* Copyright (C) 2014-2018 B.A.T.M.A.N. contributors:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003 *
4 * Linus Lüssing
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010019#include "multicast.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
21
22#include <linux/atomic.h>
Linus Lüssing9c936e32015-06-16 17:10:25 +020023#include <linux/bitops.h>
Linus Lüssing8a4023c2015-06-16 17:10:26 +020024#include <linux/bug.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020025#include <linux/byteorder/generic.h>
26#include <linux/errno.h>
27#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010028#include <linux/gfp.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020029#include <linux/icmpv6.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020030#include <linux/if_bridge.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/if_ether.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020032#include <linux/igmp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020033#include <linux/in.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020034#include <linux/in6.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/ip.h>
36#include <linux/ipv6.h>
Linus Lüssingcbebd362016-08-06 22:23:16 +020037#include <linux/jiffies.h>
Linus Lüssing72f7b2d2016-05-10 18:41:26 +020038#include <linux/kernel.h>
Sven Eckelmann7c124392016-01-16 10:29:56 +010039#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020040#include <linux/list.h>
Sven Eckelmann2c72d652015-06-21 14:45:14 +020041#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include <linux/netdevice.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020043#include <linux/printk.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include <linux/rculist.h>
45#include <linux/rcupdate.h>
Linus Lüssing4e3e8232016-05-10 18:41:27 +020046#include <linux/seq_file.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020047#include <linux/skbuff.h>
48#include <linux/slab.h>
49#include <linux/spinlock.h>
50#include <linux/stddef.h>
51#include <linux/string.h>
52#include <linux/types.h>
Linus Lüssingcbebd362016-08-06 22:23:16 +020053#include <linux/workqueue.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020054#include <net/addrconf.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020055#include <net/if_inet6.h>
56#include <net/ip.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020057#include <net/ipv6.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010058#include <uapi/linux/batadv_packet.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020059
Linus Lüssing4e3e8232016-05-10 18:41:27 +020060#include "hard-interface.h"
61#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020062#include "log.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010063#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020064#include "tvlv.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010065
Linus Lüssingcbebd362016-08-06 22:23:16 +020066static void batadv_mcast_mla_update(struct work_struct *work);
67
68/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010069 * batadv_mcast_start_timer() - schedule the multicast periodic worker
Linus Lüssingcbebd362016-08-06 22:23:16 +020070 * @bat_priv: the bat priv with all the soft interface information
71 */
72static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
73{
74 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
75 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
76}
77
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010078/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010079 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
Linus Lüssing687937a2016-05-10 18:41:25 +020080 * @soft_iface: netdev struct of the mesh interface
81 *
82 * If the given soft interface has a bridge on top then the refcount
83 * of the according net device is increased.
84 *
85 * Return: NULL if no such bridge exists. Otherwise the net device of the
86 * bridge.
87 */
88static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
89{
90 struct net_device *upper = soft_iface;
91
92 rcu_read_lock();
93 do {
94 upper = netdev_master_upper_dev_get_rcu(upper);
95 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
96
97 if (upper)
98 dev_hold(upper);
99 rcu_read_unlock();
100
101 return upper;
102}
103
104/**
Linus Lüssing6b253602018-03-04 21:02:18 +0100105 * batadv_mcast_addr_is_ipv4() - check if multicast MAC is IPv4
106 * @addr: the MAC address to check
107 *
108 * Return: True, if MAC address is one reserved for IPv4 multicast, false
109 * otherwise.
110 */
111static bool batadv_mcast_addr_is_ipv4(const u8 *addr)
112{
113 static const u8 prefix[] = {0x01, 0x00, 0x5E};
114
115 return memcmp(prefix, addr, sizeof(prefix)) == 0;
116}
117
118/**
119 * batadv_mcast_addr_is_ipv6() - check if multicast MAC is IPv6
120 * @addr: the MAC address to check
121 *
122 * Return: True, if MAC address is one reserved for IPv6 multicast, false
123 * otherwise.
124 */
125static bool batadv_mcast_addr_is_ipv6(const u8 *addr)
126{
127 static const u8 prefix[] = {0x33, 0x33};
128
129 return memcmp(prefix, addr, sizeof(prefix)) == 0;
130}
131
132/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100133 * batadv_mcast_mla_softif_get() - get softif multicast listeners
Linus Lüssing6b253602018-03-04 21:02:18 +0100134 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100135 * @dev: the device to collect multicast addresses from
136 * @mcast_list: a list to put found addresses into
137 *
Linus Lüssing687937a2016-05-10 18:41:25 +0200138 * Collects multicast addresses of multicast listeners residing
139 * on this kernel on the given soft interface, dev, in
140 * the given mcast_list. In general, multicast listeners provided by
141 * your multicast receiving applications run directly on this node.
142 *
143 * If there is a bridge interface on top of dev, collects from that one
144 * instead. Just like with IP addresses and routes, multicast listeners
145 * will(/should) register to the bridge interface instead of an
146 * enslaved bat0.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100147 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200148 * Return: -ENOMEM on memory allocation error or the number of
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100149 * items added to the mcast_list otherwise.
150 */
Linus Lüssing6b253602018-03-04 21:02:18 +0100151static int batadv_mcast_mla_softif_get(struct batadv_priv *bat_priv,
152 struct net_device *dev,
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100153 struct hlist_head *mcast_list)
154{
Linus Lüssing6b253602018-03-04 21:02:18 +0100155 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
156 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
Linus Lüssing687937a2016-05-10 18:41:25 +0200157 struct net_device *bridge = batadv_mcast_get_bridge(dev);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100158 struct netdev_hw_addr *mc_list_entry;
159 struct batadv_hw_addr *new;
160 int ret = 0;
161
Linus Lüssing687937a2016-05-10 18:41:25 +0200162 netif_addr_lock_bh(bridge ? bridge : dev);
163 netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
Linus Lüssing6b253602018-03-04 21:02:18 +0100164 if (all_ipv4 && batadv_mcast_addr_is_ipv4(mc_list_entry->addr))
165 continue;
166
167 if (all_ipv6 && batadv_mcast_addr_is_ipv6(mc_list_entry->addr))
168 continue;
169
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100170 new = kmalloc(sizeof(*new), GFP_ATOMIC);
171 if (!new) {
172 ret = -ENOMEM;
173 break;
174 }
175
176 ether_addr_copy(new->addr, mc_list_entry->addr);
177 hlist_add_head(&new->list, mcast_list);
178 ret++;
179 }
Linus Lüssing687937a2016-05-10 18:41:25 +0200180 netif_addr_unlock_bh(bridge ? bridge : dev);
181
182 if (bridge)
183 dev_put(bridge);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100184
185 return ret;
186}
187
188/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100189 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100190 * @mcast_addr: the multicast address to check
191 * @mcast_list: the list with multicast addresses to search in
192 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200193 * Return: true if the given address is already in the given list.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100194 * Otherwise returns false.
195 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200196static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100197 struct hlist_head *mcast_list)
198{
199 struct batadv_hw_addr *mcast_entry;
200
201 hlist_for_each_entry(mcast_entry, mcast_list, list)
202 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
203 return true;
204
205 return false;
206}
207
208/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100209 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
Linus Lüssing687937a2016-05-10 18:41:25 +0200210 * @dst: destination to write to - a multicast MAC address
211 * @src: source to read from - a multicast IP address
212 *
213 * Converts a given multicast IPv4/IPv6 address from a bridge
214 * to its matching multicast MAC address and copies it into the given
215 * destination buffer.
216 *
217 * Caller needs to make sure the destination buffer can hold
218 * at least ETH_ALEN bytes.
219 */
220static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
221{
222 if (src->proto == htons(ETH_P_IP))
223 ip_eth_mc_map(src->u.ip4, dst);
224#if IS_ENABLED(CONFIG_IPV6)
225 else if (src->proto == htons(ETH_P_IPV6))
226 ipv6_eth_mc_map(&src->u.ip6, dst);
227#endif
228 else
229 eth_zero_addr(dst);
230}
231
232/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100233 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
Linus Lüssing6b253602018-03-04 21:02:18 +0100234 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing687937a2016-05-10 18:41:25 +0200235 * @dev: a bridge slave whose bridge to collect multicast addresses from
236 * @mcast_list: a list to put found addresses into
237 *
238 * Collects multicast addresses of multicast listeners residing
239 * on foreign, non-mesh devices which we gave access to our mesh via
240 * a bridge on top of the given soft interface, dev, in the given
241 * mcast_list.
242 *
243 * Return: -ENOMEM on memory allocation error or the number of
244 * items added to the mcast_list otherwise.
245 */
Linus Lüssing6b253602018-03-04 21:02:18 +0100246static int batadv_mcast_mla_bridge_get(struct batadv_priv *bat_priv,
247 struct net_device *dev,
Linus Lüssing687937a2016-05-10 18:41:25 +0200248 struct hlist_head *mcast_list)
249{
250 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
Linus Lüssing6b253602018-03-04 21:02:18 +0100251 bool all_ipv4 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV4;
252 bool all_ipv6 = bat_priv->mcast.flags & BATADV_MCAST_WANT_ALL_IPV6;
Linus Lüssing687937a2016-05-10 18:41:25 +0200253 struct br_ip_list *br_ip_entry, *tmp;
254 struct batadv_hw_addr *new;
255 u8 mcast_addr[ETH_ALEN];
256 int ret;
257
258 /* we don't need to detect these devices/listeners, the IGMP/MLD
259 * snooping code of the Linux bridge already does that for us
260 */
261 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
262 if (ret < 0)
263 goto out;
264
265 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
Linus Lüssing6b253602018-03-04 21:02:18 +0100266 if (all_ipv4 && br_ip_entry->addr.proto == htons(ETH_P_IP))
267 continue;
268
269 if (all_ipv6 && br_ip_entry->addr.proto == htons(ETH_P_IPV6))
270 continue;
271
Linus Lüssing687937a2016-05-10 18:41:25 +0200272 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
273 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
274 continue;
275
276 new = kmalloc(sizeof(*new), GFP_ATOMIC);
277 if (!new) {
278 ret = -ENOMEM;
279 break;
280 }
281
282 ether_addr_copy(new->addr, mcast_addr);
283 hlist_add_head(&new->list, mcast_list);
284 }
285
286out:
287 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
288 list_del(&br_ip_entry->list);
289 kfree(br_ip_entry);
290 }
291
292 return ret;
293}
294
295/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100296 * batadv_mcast_mla_list_free() - free a list of multicast addresses
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100297 * @mcast_list: the list to free
298 *
299 * Removes and frees all items in the given mcast_list.
300 */
Linus Lüssingb7769762016-08-06 22:23:15 +0200301static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100302{
303 struct batadv_hw_addr *mcast_entry;
304 struct hlist_node *tmp;
305
306 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
307 hlist_del(&mcast_entry->list);
308 kfree(mcast_entry);
309 }
310}
311
312/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100313 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100314 * @bat_priv: the bat priv with all the soft interface information
315 * @mcast_list: a list of addresses which should _not_ be removed
316 *
317 * Retracts the announcement of any multicast listener from the
318 * translation table except the ones listed in the given mcast_list.
319 *
320 * If mcast_list is NULL then all are retracted.
Linus Lüssingcbebd362016-08-06 22:23:16 +0200321 *
322 * Do not call outside of the mcast worker! (or cancel mcast worker first)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100323 */
324static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
325 struct hlist_head *mcast_list)
326{
327 struct batadv_hw_addr *mcast_entry;
328 struct hlist_node *tmp;
329
Linus Lüssingcbebd362016-08-06 22:23:16 +0200330 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200331
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100332 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
333 list) {
334 if (mcast_list &&
335 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
336 mcast_list))
337 continue;
338
339 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
340 BATADV_NO_FLAGS,
341 "mcast TT outdated", false);
342
343 hlist_del(&mcast_entry->list);
344 kfree(mcast_entry);
345 }
346}
347
348/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100349 * batadv_mcast_mla_tt_add() - add multicast listener announcements
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100350 * @bat_priv: the bat priv with all the soft interface information
351 * @mcast_list: a list of addresses which are going to get added
352 *
353 * Adds multicast listener announcements from the given mcast_list to the
354 * translation table if they have not been added yet.
Linus Lüssingcbebd362016-08-06 22:23:16 +0200355 *
356 * Do not call outside of the mcast worker! (or cancel mcast worker first)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100357 */
358static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
359 struct hlist_head *mcast_list)
360{
361 struct batadv_hw_addr *mcast_entry;
362 struct hlist_node *tmp;
363
Linus Lüssingcbebd362016-08-06 22:23:16 +0200364 WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
Sven Eckelmann2c72d652015-06-21 14:45:14 +0200365
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100366 if (!mcast_list)
367 return;
368
369 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
370 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
371 &bat_priv->mcast.mla_list))
372 continue;
373
374 if (!batadv_tt_local_add(bat_priv->soft_iface,
375 mcast_entry->addr, BATADV_NO_FLAGS,
376 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
377 continue;
378
379 hlist_del(&mcast_entry->list);
380 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
381 }
382}
383
384/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100385 * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100386 * @bat_priv: the bat priv with all the soft interface information
387 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200388 * Checks whether there is a bridge on top of our soft interface.
389 *
390 * Return: true if there is a bridge, false otherwise.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100391 */
392static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
393{
394 struct net_device *upper = bat_priv->soft_iface;
395
396 rcu_read_lock();
397 do {
398 upper = netdev_master_upper_dev_get_rcu(upper);
399 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
400 rcu_read_unlock();
401
402 return upper;
403}
404
405/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100406 * batadv_mcast_querier_log() - debug output regarding the querier status on
407 * link
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200408 * @bat_priv: the bat priv with all the soft interface information
409 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
410 * @old_state: the previous querier state on our link
411 * @new_state: the new querier state on our link
412 *
413 * Outputs debug messages to the logging facility with log level 'mcast'
414 * regarding changes to the querier status on the link which are relevant
415 * to our multicast optimizations.
416 *
417 * Usually this is about whether a querier appeared or vanished in
418 * our mesh or whether the querier is in the suboptimal position of being
419 * behind our local bridge segment: Snooping switches will directly
420 * forward listener reports to the querier, therefore batman-adv and
421 * the bridge will potentially not see these listeners - the querier is
422 * potentially shadowing listeners from us then.
423 *
424 * This is only interesting for nodes with a bridge on top of their
425 * soft interface.
426 */
427static void
428batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
429 struct batadv_mcast_querier_state *old_state,
430 struct batadv_mcast_querier_state *new_state)
431{
432 if (!old_state->exists && new_state->exists)
433 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
434 str_proto);
435 else if (old_state->exists && !new_state->exists)
436 batadv_info(bat_priv->soft_iface,
437 "%s Querier disappeared - multicast optimizations disabled\n",
438 str_proto);
439 else if (!bat_priv->mcast.bridged && !new_state->exists)
440 batadv_info(bat_priv->soft_iface,
441 "No %s Querier present - multicast optimizations disabled\n",
442 str_proto);
443
444 if (new_state->exists) {
445 if ((!old_state->shadowing && new_state->shadowing) ||
446 (!old_state->exists && new_state->shadowing))
447 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
448 "%s Querier is behind our bridged segment: Might shadow listeners\n",
449 str_proto);
450 else if (old_state->shadowing && !new_state->shadowing)
451 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
452 "%s Querier is not behind our bridged segment\n",
453 str_proto);
454 }
455}
456
457/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100458 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
459 * setups
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200460 * @bat_priv: the bat priv with all the soft interface information
461 * @bridged: a flag about whether the soft interface is currently bridged or not
462 * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
463 * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
464 *
465 * If no bridges are ever used on this node, then this function does nothing.
466 *
467 * Otherwise this function outputs debug information to the 'mcast' log level
468 * which might be relevant to our multicast optimizations.
469 *
470 * More precisely, it outputs information when a bridge interface is added or
471 * removed from a soft interface. And when a bridge is present, it further
472 * outputs information about the querier state which is relevant for the
473 * multicast flags this node is going to set.
474 */
475static void
476batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
477 struct batadv_mcast_querier_state *querier_ipv4,
478 struct batadv_mcast_querier_state *querier_ipv6)
479{
480 if (!bat_priv->mcast.bridged && bridged)
481 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
482 "Bridge added: Setting Unsnoopables(U)-flag\n");
483 else if (bat_priv->mcast.bridged && !bridged)
484 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
485 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
486
487 if (bridged) {
488 batadv_mcast_querier_log(bat_priv, "IGMP",
489 &bat_priv->mcast.querier_ipv4,
490 querier_ipv4);
491 batadv_mcast_querier_log(bat_priv, "MLD",
492 &bat_priv->mcast.querier_ipv6,
493 querier_ipv6);
494 }
495}
496
497/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100498 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200499 * @bat_priv: the bat priv with all the soft interface information
500 * @flags: flags indicating the new multicast state
501 *
502 * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
503 * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
504 */
505static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
506{
507 u8 old_flags = bat_priv->mcast.flags;
508 char str_old_flags[] = "[...]";
509
510 sprintf(str_old_flags, "[%c%c%c]",
511 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
512 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
513 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
514
515 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
516 "Changing multicast flags from '%s' to '[%c%c%c]'\n",
517 bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
518 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
519 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
520 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
521}
522
523/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100524 * batadv_mcast_mla_tvlv_update() - update multicast tvlv
Linus Lüssing60432d72014-02-15 17:47:51 +0100525 * @bat_priv: the bat priv with all the soft interface information
526 *
527 * Updates the own multicast tvlv with our current multicast related settings,
528 * capabilities and inabilities.
529 *
Linus Lüssing687937a2016-05-10 18:41:25 +0200530 * Return: false if we want all IPv4 && IPv6 multicast traffic and true
531 * otherwise.
Linus Lüssing60432d72014-02-15 17:47:51 +0100532 */
533static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
534{
535 struct batadv_tvlv_mcast_data mcast_data;
Linus Lüssing687937a2016-05-10 18:41:25 +0200536 struct batadv_mcast_querier_state querier4 = {false, false};
537 struct batadv_mcast_querier_state querier6 = {false, false};
538 struct net_device *dev = bat_priv->soft_iface;
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200539 bool bridged;
Linus Lüssing60432d72014-02-15 17:47:51 +0100540
541 mcast_data.flags = BATADV_NO_FLAGS;
542 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
543
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200544 bridged = batadv_mcast_has_bridge(bat_priv);
545 if (!bridged)
Linus Lüssing687937a2016-05-10 18:41:25 +0200546 goto update;
547
Sven Eckelmannc1bacea2017-02-22 17:16:39 +0100548 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
549 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
Linus Lüssing687937a2016-05-10 18:41:25 +0200550
551 querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
552 querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
553
554 querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
555 querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
556
557 mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
558
559 /* 1) If no querier exists at all, then multicast listeners on
560 * our local TT clients behind the bridge will keep silent.
561 * 2) If the selected querier is on one of our local TT clients,
562 * behind the bridge, then this querier might shadow multicast
563 * listeners on our local TT clients, behind this bridge.
564 *
565 * In both cases, we will signalize other batman nodes that
566 * we need all multicast traffic of the according protocol.
Linus Lüssing60432d72014-02-15 17:47:51 +0100567 */
Linus Lüssing687937a2016-05-10 18:41:25 +0200568 if (!querier4.exists || querier4.shadowing)
569 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
Linus Lüssing60432d72014-02-15 17:47:51 +0100570
Linus Lüssing687937a2016-05-10 18:41:25 +0200571 if (!querier6.exists || querier6.shadowing)
572 mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
Linus Lüssing60432d72014-02-15 17:47:51 +0100573
Linus Lüssing687937a2016-05-10 18:41:25 +0200574update:
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200575 batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
576
577 bat_priv->mcast.querier_ipv4.exists = querier4.exists;
578 bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
579
580 bat_priv->mcast.querier_ipv6.exists = querier6.exists;
581 bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
582
583 bat_priv->mcast.bridged = bridged;
584
Linus Lüssing60432d72014-02-15 17:47:51 +0100585 if (!bat_priv->mcast.enabled ||
586 mcast_data.flags != bat_priv->mcast.flags) {
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200587 batadv_mcast_flags_log(bat_priv, mcast_data.flags);
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200588 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +0100589 &mcast_data, sizeof(mcast_data));
590 bat_priv->mcast.flags = mcast_data.flags;
591 bat_priv->mcast.enabled = true;
592 }
593
Linus Lüssing687937a2016-05-10 18:41:25 +0200594 return !(mcast_data.flags &
Linus Lüssing4d7de482016-07-11 11:16:36 +0200595 (BATADV_MCAST_WANT_ALL_IPV4 | BATADV_MCAST_WANT_ALL_IPV6));
Linus Lüssing60432d72014-02-15 17:47:51 +0100596}
597
598/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100599 * __batadv_mcast_mla_update() - update the own MLAs
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100600 * @bat_priv: the bat priv with all the soft interface information
601 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100602 * Updates the own multicast listener announcements in the translation
603 * table as well as the own, announced multicast tvlv container.
Linus Lüssingcbebd362016-08-06 22:23:16 +0200604 *
605 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
606 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
607 * ensured by the non-parallel execution of the worker this function
608 * belongs to.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100609 */
Linus Lüssingcbebd362016-08-06 22:23:16 +0200610static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100611{
612 struct net_device *soft_iface = bat_priv->soft_iface;
613 struct hlist_head mcast_list = HLIST_HEAD_INIT;
614 int ret;
615
Linus Lüssing60432d72014-02-15 17:47:51 +0100616 if (!batadv_mcast_mla_tvlv_update(bat_priv))
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100617 goto update;
618
Linus Lüssing6b253602018-03-04 21:02:18 +0100619 ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100620 if (ret < 0)
621 goto out;
622
Linus Lüssing6b253602018-03-04 21:02:18 +0100623 ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
Linus Lüssing687937a2016-05-10 18:41:25 +0200624 if (ret < 0)
625 goto out;
626
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100627update:
628 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
629 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
630
631out:
Linus Lüssingb7769762016-08-06 22:23:15 +0200632 batadv_mcast_mla_list_free(&mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100633}
634
635/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100636 * batadv_mcast_mla_update() - update the own MLAs
Linus Lüssingcbebd362016-08-06 22:23:16 +0200637 * @work: kernel work struct
638 *
639 * Updates the own multicast listener announcements in the translation
640 * table as well as the own, announced multicast tvlv container.
641 *
642 * In the end, reschedules the work timer.
643 */
644static void batadv_mcast_mla_update(struct work_struct *work)
645{
646 struct delayed_work *delayed_work;
647 struct batadv_priv_mcast *priv_mcast;
648 struct batadv_priv *bat_priv;
649
650 delayed_work = to_delayed_work(work);
651 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
652 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
653
654 __batadv_mcast_mla_update(bat_priv);
655 batadv_mcast_start_timer(bat_priv);
656}
657
658/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100659 * batadv_mcast_is_report_ipv4() - check for IGMP reports
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200660 * @skb: the ethernet frame destined for the mesh
661 *
662 * This call might reallocate skb data.
663 *
664 * Checks whether the given frame is a valid IGMP report.
665 *
666 * Return: If so then true, otherwise false.
667 */
668static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
669{
670 if (ip_mc_check_igmp(skb, NULL) < 0)
671 return false;
672
673 switch (igmp_hdr(skb)->type) {
674 case IGMP_HOST_MEMBERSHIP_REPORT:
675 case IGMPV2_HOST_MEMBERSHIP_REPORT:
676 case IGMPV3_HOST_MEMBERSHIP_REPORT:
677 return true;
678 }
679
680 return false;
681}
682
683/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100684 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
685 * potential
Linus Lüssingab498862014-02-15 17:47:53 +0100686 * @bat_priv: the bat priv with all the soft interface information
687 * @skb: the IPv4 packet to check
688 * @is_unsnoopable: stores whether the destination is snoopable
689 *
690 * Checks whether the given IPv4 packet has the potential to be forwarded with a
691 * mode more optimal than classic flooding.
692 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200693 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
694 * allocation failure.
Linus Lüssingab498862014-02-15 17:47:53 +0100695 */
696static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
697 struct sk_buff *skb,
698 bool *is_unsnoopable)
699{
700 struct iphdr *iphdr;
701
702 /* We might fail due to out-of-memory -> drop it */
703 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
704 return -ENOMEM;
705
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200706 if (batadv_mcast_is_report_ipv4(skb))
707 return -EINVAL;
708
Linus Lüssingab498862014-02-15 17:47:53 +0100709 iphdr = ip_hdr(skb);
710
711 /* TODO: Implement Multicast Router Discovery (RFC4286),
712 * then allow scope > link local, too
713 */
714 if (!ipv4_is_local_multicast(iphdr->daddr))
715 return -EINVAL;
716
717 /* link-local multicast listeners behind a bridge are
718 * not snoopable (see RFC4541, section 2.1.2.2)
719 */
720 *is_unsnoopable = true;
721
722 return 0;
723}
724
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200725/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100726 * batadv_mcast_is_report_ipv6() - check for MLD reports
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200727 * @skb: the ethernet frame destined for the mesh
728 *
729 * This call might reallocate skb data.
730 *
731 * Checks whether the given frame is a valid MLD report.
732 *
733 * Return: If so then true, otherwise false.
734 */
735static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
736{
737 if (ipv6_mc_check_mld(skb, NULL) < 0)
738 return false;
739
740 switch (icmp6_hdr(skb)->icmp6_type) {
741 case ICMPV6_MGM_REPORT:
742 case ICMPV6_MLD2_REPORT:
743 return true;
744 }
745
746 return false;
747}
748
Linus Lüssingab498862014-02-15 17:47:53 +0100749/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100750 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
751 * potential
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100752 * @bat_priv: the bat priv with all the soft interface information
753 * @skb: the IPv6 packet to check
Linus Lüssingab498862014-02-15 17:47:53 +0100754 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100755 *
756 * Checks whether the given IPv6 packet has the potential to be forwarded with a
757 * mode more optimal than classic flooding.
758 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200759 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100760 */
761static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100762 struct sk_buff *skb,
763 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100764{
765 struct ipv6hdr *ip6hdr;
766
767 /* We might fail due to out-of-memory -> drop it */
768 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
769 return -ENOMEM;
770
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200771 if (batadv_mcast_is_report_ipv6(skb))
772 return -EINVAL;
773
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100774 ip6hdr = ipv6_hdr(skb);
775
776 /* TODO: Implement Multicast Router Discovery (RFC4286),
777 * then allow scope > link local, too
778 */
779 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
780 return -EINVAL;
781
782 /* link-local-all-nodes multicast listeners behind a bridge are
783 * not snoopable (see RFC4541, section 3, paragraph 3)
784 */
785 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
Linus Lüssingab498862014-02-15 17:47:53 +0100786 *is_unsnoopable = true;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100787
788 return 0;
789}
790
791/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100792 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100793 * @bat_priv: the bat priv with all the soft interface information
794 * @skb: the multicast frame to check
Linus Lüssingab498862014-02-15 17:47:53 +0100795 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100796 *
797 * Checks whether the given multicast ethernet frame has the potential to be
798 * forwarded with a mode more optimal than classic flooding.
799 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200800 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100801 */
802static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +0100803 struct sk_buff *skb,
804 bool *is_unsnoopable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100805{
806 struct ethhdr *ethhdr = eth_hdr(skb);
807
808 if (!atomic_read(&bat_priv->multicast_mode))
809 return -EINVAL;
810
811 if (atomic_read(&bat_priv->mcast.num_disabled))
812 return -EINVAL;
813
814 switch (ntohs(ethhdr->h_proto)) {
Linus Lüssingab498862014-02-15 17:47:53 +0100815 case ETH_P_IP:
816 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
817 is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100818 case ETH_P_IPV6:
Sven Eckelmannc1bacea2017-02-22 17:16:39 +0100819 if (!IS_ENABLED(CONFIG_IPV6))
820 return -EINVAL;
821
Linus Lüssingab498862014-02-15 17:47:53 +0100822 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
823 is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100824 default:
825 return -EINVAL;
826 }
827}
828
829/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100830 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100831 * interest
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100832 * @bat_priv: the bat priv with all the soft interface information
833 * @ethhdr: ethernet header of a packet
834 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200835 * Return: the number of nodes which want all IPv4 multicast traffic if the
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100836 * given ethhdr is from an IPv4 packet or the number of nodes which want all
837 * IPv6 traffic if it matches an IPv6 packet.
838 */
839static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
840 struct ethhdr *ethhdr)
841{
842 switch (ntohs(ethhdr->h_proto)) {
843 case ETH_P_IP:
844 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
845 case ETH_P_IPV6:
846 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
847 default:
848 /* we shouldn't be here... */
849 return 0;
850 }
851}
852
853/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100854 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100855 * @bat_priv: the bat priv with all the soft interface information
856 * @ethhdr: the ether header containing the multicast destination
857 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200858 * Return: an orig_node matching the multicast address provided by ethhdr
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100859 * via a translation table lookup. This increases the returned nodes refcount.
860 */
861static struct batadv_orig_node *
862batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
863 struct ethhdr *ethhdr)
864{
865 return batadv_transtable_search(bat_priv, ethhdr->h_source,
866 ethhdr->h_dest, BATADV_NO_FLAGS);
867}
868
869/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100870 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100871 * @bat_priv: the bat priv with all the soft interface information
872 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200873 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100874 * increases its refcount.
875 */
876static struct batadv_orig_node *
877batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
878{
879 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
880
881 rcu_read_lock();
882 hlist_for_each_entry_rcu(tmp_orig_node,
883 &bat_priv->mcast.want_all_ipv4_list,
884 mcast_want_all_ipv4_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100885 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100886 continue;
887
888 orig_node = tmp_orig_node;
889 break;
890 }
891 rcu_read_unlock();
892
893 return orig_node;
894}
895
896/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100897 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100898 * @bat_priv: the bat priv with all the soft interface information
899 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200900 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100901 * and increases its refcount.
902 */
903static struct batadv_orig_node *
904batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
905{
906 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
907
908 rcu_read_lock();
909 hlist_for_each_entry_rcu(tmp_orig_node,
910 &bat_priv->mcast.want_all_ipv6_list,
911 mcast_want_all_ipv6_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100912 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100913 continue;
914
915 orig_node = tmp_orig_node;
916 break;
917 }
918 rcu_read_unlock();
919
920 return orig_node;
921}
922
923/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100924 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100925 * @bat_priv: the bat priv with all the soft interface information
926 * @ethhdr: an ethernet header to determine the protocol family from
927 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200928 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100929 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
930 * increases its refcount.
931 */
932static struct batadv_orig_node *
933batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
934 struct ethhdr *ethhdr)
935{
936 switch (ntohs(ethhdr->h_proto)) {
937 case ETH_P_IP:
938 return batadv_mcast_forw_ipv4_node_get(bat_priv);
939 case ETH_P_IPV6:
940 return batadv_mcast_forw_ipv6_node_get(bat_priv);
941 default:
942 /* we shouldn't be here... */
943 return NULL;
944 }
945}
946
947/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100948 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
Linus Lüssingab498862014-02-15 17:47:53 +0100949 * @bat_priv: the bat priv with all the soft interface information
950 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200951 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
Linus Lüssingab498862014-02-15 17:47:53 +0100952 * set and increases its refcount.
953 */
954static struct batadv_orig_node *
955batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
956{
957 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
958
959 rcu_read_lock();
960 hlist_for_each_entry_rcu(tmp_orig_node,
961 &bat_priv->mcast.want_all_unsnoopables_list,
962 mcast_want_all_unsnoopables_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +0100963 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssingab498862014-02-15 17:47:53 +0100964 continue;
965
966 orig_node = tmp_orig_node;
967 break;
968 }
969 rcu_read_unlock();
970
971 return orig_node;
972}
973
974/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100975 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100976 * @bat_priv: the bat priv with all the soft interface information
977 * @skb: The multicast packet to check
978 * @orig: an originator to be set to forward the skb to
979 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200980 * Return: the forwarding mode as enum batadv_forw_mode and in case of
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100981 * BATADV_FORW_SINGLE set the orig to the single originator the skb
982 * should be forwarded to.
983 */
984enum batadv_forw_mode
985batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
986 struct batadv_orig_node **orig)
987{
Linus Lüssing4c8755d2014-02-15 17:47:54 +0100988 int ret, tt_count, ip_count, unsnoop_count, total_count;
Linus Lüssingab498862014-02-15 17:47:53 +0100989 bool is_unsnoopable = false;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100990 struct ethhdr *ethhdr;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100991
Linus Lüssingab498862014-02-15 17:47:53 +0100992 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100993 if (ret == -ENOMEM)
994 return BATADV_FORW_NONE;
995 else if (ret < 0)
996 return BATADV_FORW_ALL;
997
998 ethhdr = eth_hdr(skb);
999
1000 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1001 BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001002 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +01001003 unsnoop_count = !is_unsnoopable ? 0 :
1004 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001005
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001006 total_count = tt_count + ip_count + unsnoop_count;
Linus Lüssingab498862014-02-15 17:47:53 +01001007
1008 switch (total_count) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001009 case 1:
Linus Lüssingab498862014-02-15 17:47:53 +01001010 if (tt_count)
1011 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001012 else if (ip_count)
1013 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +01001014 else if (unsnoop_count)
1015 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
1016
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001017 if (*orig)
1018 return BATADV_FORW_SINGLE;
1019
1020 /* fall through */
1021 case 0:
1022 return BATADV_FORW_NONE;
1023 default:
1024 return BATADV_FORW_ALL;
1025 }
1026}
1027
1028/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001029 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
Linus Lüssingab498862014-02-15 17:47:53 +01001030 * @bat_priv: the bat priv with all the soft interface information
1031 * @orig: the orig_node which multicast state might have changed of
1032 * @mcast_flags: flags indicating the new multicast state
1033 *
1034 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
1035 * orig, has toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001036 *
1037 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssingab498862014-02-15 17:47:53 +01001038 */
1039static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1040 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001041 u8 mcast_flags)
Linus Lüssingab498862014-02-15 17:47:53 +01001042{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001043 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1044 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1045
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001046 lockdep_assert_held(&orig->mcast_handler_lock);
1047
Linus Lüssingab498862014-02-15 17:47:53 +01001048 /* switched from flag unset to set */
1049 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1050 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1051 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1052
1053 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001054 /* flag checks above + mcast_handler_lock prevents this */
1055 WARN_ON(!hlist_unhashed(node));
1056
1057 hlist_add_head_rcu(node, head);
Linus Lüssingab498862014-02-15 17:47:53 +01001058 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1059 /* switched from flag set to unset */
1060 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1061 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1062 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1063
1064 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001065 /* flag checks above + mcast_handler_lock prevents this */
1066 WARN_ON(hlist_unhashed(node));
1067
1068 hlist_del_init_rcu(node);
Linus Lüssingab498862014-02-15 17:47:53 +01001069 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1070 }
1071}
1072
1073/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001074 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001075 * @bat_priv: the bat priv with all the soft interface information
1076 * @orig: the orig_node which multicast state might have changed of
1077 * @mcast_flags: flags indicating the new multicast state
1078 *
1079 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
1080 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001081 *
1082 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001083 */
1084static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1085 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001086 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001087{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001088 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1089 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1090
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001091 lockdep_assert_held(&orig->mcast_handler_lock);
1092
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001093 /* switched from flag unset to set */
1094 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1095 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1096 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1097
1098 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001099 /* flag checks above + mcast_handler_lock prevents this */
1100 WARN_ON(!hlist_unhashed(node));
1101
1102 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001103 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1104 /* switched from flag set to unset */
1105 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1106 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1107 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1108
1109 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001110 /* flag checks above + mcast_handler_lock prevents this */
1111 WARN_ON(hlist_unhashed(node));
1112
1113 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001114 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1115 }
1116}
1117
1118/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001119 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001120 * @bat_priv: the bat priv with all the soft interface information
1121 * @orig: the orig_node which multicast state might have changed of
1122 * @mcast_flags: flags indicating the new multicast state
1123 *
1124 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
1125 * toggled then this method updates counter and list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001126 *
1127 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001128 */
1129static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1130 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001131 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001132{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001133 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1134 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1135
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001136 lockdep_assert_held(&orig->mcast_handler_lock);
1137
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001138 /* switched from flag unset to set */
1139 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1140 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1141 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1142
1143 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001144 /* flag checks above + mcast_handler_lock prevents this */
1145 WARN_ON(!hlist_unhashed(node));
1146
1147 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001148 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1149 /* switched from flag set to unset */
1150 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1151 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1152 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1153
1154 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001155 /* flag checks above + mcast_handler_lock prevents this */
1156 WARN_ON(hlist_unhashed(node));
1157
1158 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001159 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1160 }
1161}
1162
1163/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001164 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
Linus Lüssing60432d72014-02-15 17:47:51 +01001165 * @bat_priv: the bat priv with all the soft interface information
1166 * @orig: the orig_node of the ogm
1167 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
1168 * @tvlv_value: tvlv buffer containing the multicast data
1169 * @tvlv_value_len: tvlv buffer length
1170 */
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001171static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
1172 struct batadv_orig_node *orig,
1173 u8 flags,
1174 void *tvlv_value,
1175 u16 tvlv_value_len)
Linus Lüssing60432d72014-02-15 17:47:51 +01001176{
1177 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001178 u8 mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing60432d72014-02-15 17:47:51 +01001179 bool orig_initialized;
1180
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001181 if (orig_mcast_enabled && tvlv_value &&
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001182 tvlv_value_len >= sizeof(mcast_flags))
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001183 mcast_flags = *(u8 *)tvlv_value;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001184
1185 spin_lock_bh(&orig->mcast_handler_lock);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001186 orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1187 &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01001188
1189 /* If mcast support is turned on decrease the disabled mcast node
1190 * counter only if we had increased it for this node before. If this
1191 * is a completely new orig_node no need to decrease the counter.
1192 */
1193 if (orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02001194 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing60432d72014-02-15 17:47:51 +01001195 if (orig_initialized)
1196 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001197 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssinge8829f02014-10-30 05:40:46 +01001198 /* If mcast support is being switched off or if this is an initial
1199 * OGM without mcast support then increase the disabled mcast
1200 * node counter.
Linus Lüssing60432d72014-02-15 17:47:51 +01001201 */
1202 } else if (!orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02001203 (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
Linus Lüssinge8829f02014-10-30 05:40:46 +01001204 !orig_initialized)) {
Linus Lüssing60432d72014-02-15 17:47:51 +01001205 atomic_inc(&bat_priv->mcast.num_disabled);
Linus Lüssing9c936e32015-06-16 17:10:25 +02001206 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +01001207 }
1208
Linus Lüssing9c936e32015-06-16 17:10:25 +02001209 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01001210
Linus Lüssingab498862014-02-15 17:47:53 +01001211 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001212 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
1213 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
Linus Lüssingab498862014-02-15 17:47:53 +01001214
Linus Lüssing60432d72014-02-15 17:47:51 +01001215 orig->mcast_flags = mcast_flags;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001216 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001217}
1218
1219/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001220 * batadv_mcast_init() - initialize the multicast optimizations structures
Linus Lüssing60432d72014-02-15 17:47:51 +01001221 * @bat_priv: the bat priv with all the soft interface information
1222 */
1223void batadv_mcast_init(struct batadv_priv *bat_priv)
1224{
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001225 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
1226 NULL, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +01001227 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Linus Lüssingcbebd362016-08-06 22:23:16 +02001228
1229 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
1230 batadv_mcast_start_timer(bat_priv);
Linus Lüssing60432d72014-02-15 17:47:51 +01001231}
1232
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001233#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Linus Lüssing60432d72014-02-15 17:47:51 +01001234/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001235 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
Linus Lüssing4e3e8232016-05-10 18:41:27 +02001236 * @bat_priv: the bat priv with all the soft interface information
1237 * @seq: debugfs table seq_file struct
1238 *
1239 * Prints our own multicast flags including a more specific reason why
1240 * they are set, that is prints the bridge and querier state too, to
1241 * the debugfs table specified via @seq.
1242 */
1243static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
1244 struct seq_file *seq)
1245{
1246 u8 flags = bat_priv->mcast.flags;
1247 char querier4, querier6, shadowing4, shadowing6;
1248 bool bridged = bat_priv->mcast.bridged;
1249
1250 if (bridged) {
1251 querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
1252 querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
1253 shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
1254 shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
1255 } else {
1256 querier4 = '?';
1257 querier6 = '?';
1258 shadowing4 = '?';
1259 shadowing6 = '?';
1260 }
1261
1262 seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
1263 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
1264 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
1265 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
1266 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
1267 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
1268 querier4, querier6);
1269 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
1270 shadowing4, shadowing6);
1271 seq_puts(seq, "-------------------------------------------\n");
1272 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
1273}
1274
1275/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001276 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
Linus Lüssing4e3e8232016-05-10 18:41:27 +02001277 * @seq: seq file to print on
1278 * @offset: not used
1279 *
1280 * This prints a table of (primary) originators and their according
1281 * multicast flags, including (in the header) our own.
1282 *
1283 * Return: always 0
1284 */
1285int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
1286{
1287 struct net_device *net_dev = (struct net_device *)seq->private;
1288 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1289 struct batadv_hard_iface *primary_if;
1290 struct batadv_hashtable *hash = bat_priv->orig_hash;
1291 struct batadv_orig_node *orig_node;
1292 struct hlist_head *head;
1293 u8 flags;
1294 u32 i;
1295
1296 primary_if = batadv_seq_print_text_primary_if_get(seq);
1297 if (!primary_if)
1298 return 0;
1299
1300 batadv_mcast_flags_print_header(bat_priv, seq);
1301
1302 for (i = 0; i < hash->size; i++) {
1303 head = &hash->table[i];
1304
1305 rcu_read_lock();
1306 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1307 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1308 &orig_node->capa_initialized))
1309 continue;
1310
1311 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
1312 &orig_node->capabilities)) {
1313 seq_printf(seq, "%pM -\n", orig_node->orig);
1314 continue;
1315 }
1316
1317 flags = orig_node->mcast_flags;
1318
1319 seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
1320 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
1321 ? 'U' : '.',
1322 (flags & BATADV_MCAST_WANT_ALL_IPV4)
1323 ? '4' : '.',
1324 (flags & BATADV_MCAST_WANT_ALL_IPV6)
1325 ? '6' : '.');
1326 }
1327 rcu_read_unlock();
1328 }
1329
1330 batadv_hardif_put(primary_if);
1331
1332 return 0;
1333}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001334#endif
Linus Lüssing4e3e8232016-05-10 18:41:27 +02001335
1336/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001337 * batadv_mcast_free() - free the multicast optimizations structures
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001338 * @bat_priv: the bat priv with all the soft interface information
1339 */
1340void batadv_mcast_free(struct batadv_priv *bat_priv)
1341{
Linus Lüssingcbebd362016-08-06 22:23:16 +02001342 cancel_delayed_work_sync(&bat_priv->mcast.work);
1343
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001344 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
1345 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
Linus Lüssing60432d72014-02-15 17:47:51 +01001346
Linus Lüssingcbebd362016-08-06 22:23:16 +02001347 /* safely calling outside of worker, as worker was canceled above */
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001348 batadv_mcast_mla_tt_retract(bat_priv, NULL);
1349}
Linus Lüssing60432d72014-02-15 17:47:51 +01001350
1351/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001352 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
Linus Lüssing60432d72014-02-15 17:47:51 +01001353 * @orig: the originator which is going to get purged
1354 */
1355void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
1356{
1357 struct batadv_priv *bat_priv = orig->bat_priv;
1358
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001359 spin_lock_bh(&orig->mcast_handler_lock);
1360
Linus Lüssing9c936e32015-06-16 17:10:25 +02001361 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
1362 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
Linus Lüssing60432d72014-02-15 17:47:51 +01001363 atomic_dec(&bat_priv->mcast.num_disabled);
Linus Lüssingab498862014-02-15 17:47:53 +01001364
1365 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001366 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
1367 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001368
1369 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001370}