blob: bdc4a1fba1c6e3dcd580487b26f8a317fa645b82 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann68e039f2020-01-01 00:00:01 +01002/* Copyright (C) 2014-2020 B.A.T.M.A.N. contributors:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003 *
4 * Linus Lüssing
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01005 */
6
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01007#include "multicast.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
10#include <linux/atomic.h>
Linus Lüssing9c936e32015-06-16 17:10:25 +020011#include <linux/bitops.h>
Linus Lüssing8a4023c2015-06-16 17:10:26 +020012#include <linux/bug.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020013#include <linux/byteorder/generic.h>
14#include <linux/errno.h>
15#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010016#include <linux/gfp.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020017#include <linux/icmpv6.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020018#include <linux/if_bridge.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include <linux/if_ether.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020020#include <linux/igmp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/in.h>
Linus Lüssingbd2a9792016-05-10 18:41:24 +020022#include <linux/in6.h>
Linus Lüssing5c506802019-05-26 18:35:49 +020023#include <linux/inetdevice.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020024#include <linux/ip.h>
25#include <linux/ipv6.h>
Linus Lüssingcbebd362016-08-06 22:23:16 +020026#include <linux/jiffies.h>
Linus Lüssing72f7b2d2016-05-10 18:41:26 +020027#include <linux/kernel.h>
Sven Eckelmann7c124392016-01-16 10:29:56 +010028#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/list.h>
Sven Eckelmann2c72d652015-06-21 14:45:14 +020030#include <linux/lockdep.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include <linux/netdevice.h>
Linus Lüssing53dd9a62018-03-13 11:41:13 +010032#include <linux/netlink.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020033#include <linux/printk.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020034#include <linux/rculist.h>
35#include <linux/rcupdate.h>
Linus Lüssing4e3e8232016-05-10 18:41:27 +020036#include <linux/seq_file.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/skbuff.h>
38#include <linux/slab.h>
39#include <linux/spinlock.h>
40#include <linux/stddef.h>
41#include <linux/string.h>
42#include <linux/types.h>
Linus Lüssingcbebd362016-08-06 22:23:16 +020043#include <linux/workqueue.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include <net/addrconf.h>
Linus Lüssing53dd9a62018-03-13 11:41:13 +010045#include <net/genetlink.h>
Linus Lüssing687937a2016-05-10 18:41:25 +020046#include <net/if_inet6.h>
47#include <net/ip.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048#include <net/ipv6.h>
Linus Lüssing53dd9a62018-03-13 11:41:13 +010049#include <net/netlink.h>
50#include <net/sock.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010051#include <uapi/linux/batadv_packet.h>
Linus Lüssing53dd9a62018-03-13 11:41:13 +010052#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053
Linus Lüssing4e3e8232016-05-10 18:41:27 +020054#include "hard-interface.h"
55#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020056#include "log.h"
Linus Lüssing53dd9a62018-03-13 11:41:13 +010057#include "netlink.h"
Linus Lüssing32e72742019-03-23 05:47:41 +010058#include "send.h"
Linus Lüssing53dd9a62018-03-13 11:41:13 +010059#include "soft-interface.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010060#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020061#include "tvlv.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010062
Linus Lüssingcbebd362016-08-06 22:23:16 +020063static void batadv_mcast_mla_update(struct work_struct *work);
64
65/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010066 * batadv_mcast_start_timer() - schedule the multicast periodic worker
Linus Lüssingcbebd362016-08-06 22:23:16 +020067 * @bat_priv: the bat priv with all the soft interface information
68 */
69static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
70{
71 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
72 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
73}
74
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010075/**
Linus Lüssing61caf3d2019-06-11 22:58:40 +020076 * batadv_mcast_get_bridge() - get the bridge on top of the softif if it exists
77 * @soft_iface: netdev struct of the mesh interface
Linus Lüssing6bc45442019-05-07 06:08:26 +020078 *
Linus Lüssing61caf3d2019-06-11 22:58:40 +020079 * If the given soft interface has a bridge on top then the refcount
80 * of the according net device is increased.
Linus Lüssing6bc45442019-05-07 06:08:26 +020081 *
Linus Lüssing61caf3d2019-06-11 22:58:40 +020082 * Return: NULL if no such bridge exists. Otherwise the net device of the
83 * bridge.
Linus Lüssing6bc45442019-05-07 06:08:26 +020084 */
Linus Lüssing61caf3d2019-06-11 22:58:40 +020085static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
Linus Lüssing6bc45442019-05-07 06:08:26 +020086{
Linus Lüssing61caf3d2019-06-11 22:58:40 +020087 struct net_device *upper = soft_iface;
Linus Lüssing6bc45442019-05-07 06:08:26 +020088
89 rcu_read_lock();
90 do {
91 upper = netdev_master_upper_dev_get_rcu(upper);
92 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
Linus Lüssing61caf3d2019-06-11 22:58:40 +020093
94 if (upper)
95 dev_hold(upper);
Linus Lüssing6bc45442019-05-07 06:08:26 +020096 rcu_read_unlock();
97
98 return upper;
99}
100
101/**
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200102 * batadv_mcast_mla_rtr_flags_softif_get_ipv4() - get mcast router flags from
103 * node for IPv4
104 * @dev: the interface to check
105 *
106 * Checks the presence of an IPv4 multicast router on this node.
107 *
108 * Caller needs to hold rcu read lock.
109 *
110 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
111 */
112static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv4(struct net_device *dev)
113{
114 struct in_device *in_dev = __in_dev_get_rcu(dev);
115
116 if (in_dev && IN_DEV_MFORWARD(in_dev))
117 return BATADV_NO_FLAGS;
118 else
119 return BATADV_MCAST_WANT_NO_RTR4;
120}
121
122/**
123 * batadv_mcast_mla_rtr_flags_softif_get_ipv6() - get mcast router flags from
124 * node for IPv6
125 * @dev: the interface to check
126 *
127 * Checks the presence of an IPv6 multicast router on this node.
128 *
129 * Caller needs to hold rcu read lock.
130 *
131 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
132 */
133#if IS_ENABLED(CONFIG_IPV6_MROUTE)
134static u8 batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
135{
136 struct inet6_dev *in6_dev = __in6_dev_get(dev);
137
138 if (in6_dev && in6_dev->cnf.mc_forwarding)
139 return BATADV_NO_FLAGS;
140 else
141 return BATADV_MCAST_WANT_NO_RTR6;
142}
143#else
144static inline u8
145batadv_mcast_mla_rtr_flags_softif_get_ipv6(struct net_device *dev)
146{
147 return BATADV_MCAST_WANT_NO_RTR6;
148}
149#endif
150
151/**
152 * batadv_mcast_mla_rtr_flags_softif_get() - get mcast router flags from node
153 * @bat_priv: the bat priv with all the soft interface information
154 * @bridge: bridge interface on top of the soft_iface if present,
155 * otherwise pass NULL
156 *
157 * Checks the presence of IPv4 and IPv6 multicast routers on this
158 * node.
159 *
160 * Return:
161 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
162 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
163 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
164 * The former two OR'd: no multicast router is present
165 */
166static u8 batadv_mcast_mla_rtr_flags_softif_get(struct batadv_priv *bat_priv,
167 struct net_device *bridge)
168{
169 struct net_device *dev = bridge ? bridge : bat_priv->soft_iface;
170 u8 flags = BATADV_NO_FLAGS;
171
172 rcu_read_lock();
173
174 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv4(dev);
175 flags |= batadv_mcast_mla_rtr_flags_softif_get_ipv6(dev);
176
177 rcu_read_unlock();
178
179 return flags;
180}
181
182/**
183 * batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge
184 * @bat_priv: the bat priv with all the soft interface information
185 * @bridge: bridge interface on top of the soft_iface if present,
186 * otherwise pass NULL
187 *
188 * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
189 *
190 * Return:
191 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
192 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
193 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
194 * The former two OR'd: no multicast router is present
195 */
196#if IS_ENABLED(CONFIG_IPV6)
197static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
198 struct net_device *bridge)
199{
200 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
201 struct net_device *dev = bat_priv->soft_iface;
202 struct br_ip_list *br_ip_entry, *tmp;
203 u8 flags = BATADV_MCAST_WANT_NO_RTR6;
204 int ret;
205
206 if (!bridge)
207 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
208
209 /* TODO: ask the bridge if a multicast router is present (the bridge
210 * is capable of performing proper RFC4286 multicast multicast router
211 * discovery) instead of searching for a ff02::2 listener here
212 */
213 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
214 if (ret < 0)
215 return BATADV_NO_FLAGS;
216
217 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
218 /* the bridge snooping does not maintain IPv4 link-local
219 * addresses - therefore we won't find any IPv4 multicast router
220 * address here, only IPv6 ones
221 */
222 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6) &&
223 ipv6_addr_is_ll_all_routers(&br_ip_entry->addr.u.ip6))
224 flags &= ~BATADV_MCAST_WANT_NO_RTR6;
225
226 list_del(&br_ip_entry->list);
227 kfree(br_ip_entry);
228 }
229
230 return flags;
231}
232#else
233static inline u8
234batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
235 struct net_device *bridge)
236{
237 if (bridge)
238 return BATADV_NO_FLAGS;
239 else
240 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
241}
242#endif
243
244/**
245 * batadv_mcast_mla_rtr_flags_get() - get multicast router flags
246 * @bat_priv: the bat priv with all the soft interface information
247 * @bridge: bridge interface on top of the soft_iface if present,
248 * otherwise pass NULL
249 *
250 * Checks the presence of IPv4 and IPv6 multicast routers on this
251 * node or behind its bridge.
252 *
253 * Return:
254 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
255 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
256 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
257 * The former two OR'd: no multicast router is present
258 */
259static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
260 struct net_device *bridge)
261{
262 u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
263
264 flags &= batadv_mcast_mla_rtr_flags_softif_get(bat_priv, bridge);
265 flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
266
267 return flags;
268}
269
270/**
Linus Lüssing6bc45442019-05-07 06:08:26 +0200271 * batadv_mcast_mla_flags_get() - get the new multicast flags
272 * @bat_priv: the bat priv with all the soft interface information
273 *
274 * Return: A set of flags for the current/next TVLV, querier and
275 * bridge state.
276 */
277static struct batadv_mcast_mla_flags
278batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
279{
280 struct net_device *dev = bat_priv->soft_iface;
281 struct batadv_mcast_querier_state *qr4, *qr6;
282 struct batadv_mcast_mla_flags mla_flags;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200283 struct net_device *bridge;
284
285 bridge = batadv_mcast_get_bridge(dev);
Linus Lüssing6bc45442019-05-07 06:08:26 +0200286
287 memset(&mla_flags, 0, sizeof(mla_flags));
288 mla_flags.enabled = 1;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200289 mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv,
290 bridge);
Linus Lüssing6bc45442019-05-07 06:08:26 +0200291
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200292 if (!bridge)
Linus Lüssing6bc45442019-05-07 06:08:26 +0200293 return mla_flags;
294
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200295 dev_put(bridge);
296
Linus Lüssing6bc45442019-05-07 06:08:26 +0200297 mla_flags.bridged = 1;
298 qr4 = &mla_flags.querier_ipv4;
299 qr6 = &mla_flags.querier_ipv6;
300
301 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
302 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
303
304 qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
305 qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
306
307 qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
308 qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
309
310 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
311
312 /* 1) If no querier exists at all, then multicast listeners on
313 * our local TT clients behind the bridge will keep silent.
314 * 2) If the selected querier is on one of our local TT clients,
315 * behind the bridge, then this querier might shadow multicast
316 * listeners on our local TT clients, behind this bridge.
317 *
318 * In both cases, we will signalize other batman nodes that
319 * we need all multicast traffic of the according protocol.
320 */
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200321 if (!qr4->exists || qr4->shadowing) {
Linus Lüssing6bc45442019-05-07 06:08:26 +0200322 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200323 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4;
324 }
Linus Lüssing6bc45442019-05-07 06:08:26 +0200325
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200326 if (!qr6->exists || qr6->shadowing) {
Linus Lüssing6bc45442019-05-07 06:08:26 +0200327 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200328 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6;
329 }
Linus Lüssing6bc45442019-05-07 06:08:26 +0200330
331 return mla_flags;
332}
333
334/**
Linus Lüssing5c506802019-05-26 18:35:49 +0200335 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
336 * @mcast_addr: the multicast address to check
337 * @mcast_list: the list with multicast addresses to search in
Linus Lüssing6b253602018-03-04 21:02:18 +0100338 *
Linus Lüssing5c506802019-05-26 18:35:49 +0200339 * Return: true if the given address is already in the given list.
340 * Otherwise returns false.
Linus Lüssing6b253602018-03-04 21:02:18 +0100341 */
Linus Lüssing5c506802019-05-26 18:35:49 +0200342static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
343 struct hlist_head *mcast_list)
Linus Lüssing6b253602018-03-04 21:02:18 +0100344{
Linus Lüssing5c506802019-05-26 18:35:49 +0200345 struct batadv_hw_addr *mcast_entry;
Linus Lüssing6b253602018-03-04 21:02:18 +0100346
Linus Lüssing5c506802019-05-26 18:35:49 +0200347 hlist_for_each_entry(mcast_entry, mcast_list, list)
348 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
349 return true;
350
351 return false;
Linus Lüssing6b253602018-03-04 21:02:18 +0100352}
353
354/**
Linus Lüssing5c506802019-05-26 18:35:49 +0200355 * batadv_mcast_mla_softif_get_ipv4() - get softif IPv4 multicast listeners
356 * @dev: the device to collect multicast addresses from
357 * @mcast_list: a list to put found addresses into
358 * @flags: flags indicating the new multicast state
Linus Lüssing6b253602018-03-04 21:02:18 +0100359 *
Linus Lüssing5c506802019-05-26 18:35:49 +0200360 * Collects multicast addresses of IPv4 multicast listeners residing
361 * on this kernel on the given soft interface, dev, in
362 * the given mcast_list. In general, multicast listeners provided by
363 * your multicast receiving applications run directly on this node.
364 *
365 * Return: -ENOMEM on memory allocation error or the number of
366 * items added to the mcast_list otherwise.
Linus Lüssing6b253602018-03-04 21:02:18 +0100367 */
Linus Lüssing5c506802019-05-26 18:35:49 +0200368static int
369batadv_mcast_mla_softif_get_ipv4(struct net_device *dev,
370 struct hlist_head *mcast_list,
371 struct batadv_mcast_mla_flags *flags)
Linus Lüssing6b253602018-03-04 21:02:18 +0100372{
Linus Lüssing5c506802019-05-26 18:35:49 +0200373 struct batadv_hw_addr *new;
374 struct in_device *in_dev;
375 u8 mcast_addr[ETH_ALEN];
376 struct ip_mc_list *pmc;
377 int ret = 0;
Linus Lüssing6b253602018-03-04 21:02:18 +0100378
Linus Lüssing5c506802019-05-26 18:35:49 +0200379 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
380 return 0;
381
382 rcu_read_lock();
383
384 in_dev = __in_dev_get_rcu(dev);
385 if (!in_dev) {
386 rcu_read_unlock();
387 return 0;
388 }
389
390 for (pmc = rcu_dereference(in_dev->mc_list); pmc;
391 pmc = rcu_dereference(pmc->next_rcu)) {
Linus Lüssing390dcd482019-05-26 18:35:50 +0200392 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
393 ipv4_is_local_multicast(pmc->multiaddr))
394 continue;
395
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200396 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
397 !ipv4_is_local_multicast(pmc->multiaddr))
398 continue;
399
Linus Lüssing5c506802019-05-26 18:35:49 +0200400 ip_eth_mc_map(pmc->multiaddr, mcast_addr);
401
402 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
403 continue;
404
405 new = kmalloc(sizeof(*new), GFP_ATOMIC);
406 if (!new) {
407 ret = -ENOMEM;
408 break;
409 }
410
411 ether_addr_copy(new->addr, mcast_addr);
412 hlist_add_head(&new->list, mcast_list);
413 ret++;
414 }
415 rcu_read_unlock();
416
417 return ret;
Linus Lüssing6b253602018-03-04 21:02:18 +0100418}
419
420/**
Linus Lüssing5c506802019-05-26 18:35:49 +0200421 * batadv_mcast_mla_softif_get_ipv6() - get softif IPv6 multicast listeners
422 * @dev: the device to collect multicast addresses from
423 * @mcast_list: a list to put found addresses into
424 * @flags: flags indicating the new multicast state
425 *
426 * Collects multicast addresses of IPv6 multicast listeners residing
427 * on this kernel on the given soft interface, dev, in
428 * the given mcast_list. In general, multicast listeners provided by
429 * your multicast receiving applications run directly on this node.
430 *
431 * Return: -ENOMEM on memory allocation error or the number of
432 * items added to the mcast_list otherwise.
433 */
434#if IS_ENABLED(CONFIG_IPV6)
435static int
436batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
437 struct hlist_head *mcast_list,
438 struct batadv_mcast_mla_flags *flags)
439{
440 struct batadv_hw_addr *new;
441 struct inet6_dev *in6_dev;
442 u8 mcast_addr[ETH_ALEN];
443 struct ifmcaddr6 *pmc6;
444 int ret = 0;
445
446 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
447 return 0;
448
449 rcu_read_lock();
450
451 in6_dev = __in6_dev_get(dev);
452 if (!in6_dev) {
453 rcu_read_unlock();
454 return 0;
455 }
456
457 read_lock_bh(&in6_dev->lock);
458 for (pmc6 = in6_dev->mc_list; pmc6; pmc6 = pmc6->next) {
459 if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) <
460 IPV6_ADDR_SCOPE_LINKLOCAL)
461 continue;
462
Linus Lüssing390dcd482019-05-26 18:35:50 +0200463 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
464 ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
465 continue;
466
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200467 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
468 IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) >
469 IPV6_ADDR_SCOPE_LINKLOCAL)
470 continue;
471
Linus Lüssing5c506802019-05-26 18:35:49 +0200472 ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
473
474 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
475 continue;
476
477 new = kmalloc(sizeof(*new), GFP_ATOMIC);
478 if (!new) {
479 ret = -ENOMEM;
480 break;
481 }
482
483 ether_addr_copy(new->addr, mcast_addr);
484 hlist_add_head(&new->list, mcast_list);
485 ret++;
486 }
487 read_unlock_bh(&in6_dev->lock);
488 rcu_read_unlock();
489
490 return ret;
491}
492#else
493static inline int
494batadv_mcast_mla_softif_get_ipv6(struct net_device *dev,
495 struct hlist_head *mcast_list,
496 struct batadv_mcast_mla_flags *flags)
497{
498 return 0;
499}
500#endif
501
502/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100503 * batadv_mcast_mla_softif_get() - get softif multicast listeners
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100504 * @dev: the device to collect multicast addresses from
505 * @mcast_list: a list to put found addresses into
Linus Lüssing6bc45442019-05-07 06:08:26 +0200506 * @flags: flags indicating the new multicast state
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100507 *
Linus Lüssing687937a2016-05-10 18:41:25 +0200508 * Collects multicast addresses of multicast listeners residing
509 * on this kernel on the given soft interface, dev, in
510 * the given mcast_list. In general, multicast listeners provided by
511 * your multicast receiving applications run directly on this node.
512 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200513 * If there is a bridge interface on top of dev, collect from that one
Linus Lüssing687937a2016-05-10 18:41:25 +0200514 * instead. Just like with IP addresses and routes, multicast listeners
515 * will(/should) register to the bridge interface instead of an
516 * enslaved bat0.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100517 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200518 * Return: -ENOMEM on memory allocation error or the number of
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100519 * items added to the mcast_list otherwise.
520 */
Linus Lüssing6bc45442019-05-07 06:08:26 +0200521static int
522batadv_mcast_mla_softif_get(struct net_device *dev,
523 struct hlist_head *mcast_list,
524 struct batadv_mcast_mla_flags *flags)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100525{
Linus Lüssing687937a2016-05-10 18:41:25 +0200526 struct net_device *bridge = batadv_mcast_get_bridge(dev);
Linus Lüssing5c506802019-05-26 18:35:49 +0200527 int ret4, ret6 = 0;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100528
Linus Lüssing5c506802019-05-26 18:35:49 +0200529 if (bridge)
530 dev = bridge;
Linus Lüssing6b253602018-03-04 21:02:18 +0100531
Linus Lüssing5c506802019-05-26 18:35:49 +0200532 ret4 = batadv_mcast_mla_softif_get_ipv4(dev, mcast_list, flags);
533 if (ret4 < 0)
534 goto out;
Linus Lüssing6b253602018-03-04 21:02:18 +0100535
Linus Lüssing5c506802019-05-26 18:35:49 +0200536 ret6 = batadv_mcast_mla_softif_get_ipv6(dev, mcast_list, flags);
537 if (ret6 < 0) {
538 ret4 = 0;
539 goto out;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100540 }
Linus Lüssing687937a2016-05-10 18:41:25 +0200541
Linus Lüssing5c506802019-05-26 18:35:49 +0200542out:
Linus Lüssing687937a2016-05-10 18:41:25 +0200543 if (bridge)
544 dev_put(bridge);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100545
Linus Lüssing5c506802019-05-26 18:35:49 +0200546 return ret4 + ret6;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100547}
548
549/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100550 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
Linus Lüssing687937a2016-05-10 18:41:25 +0200551 * @dst: destination to write to - a multicast MAC address
552 * @src: source to read from - a multicast IP address
553 *
554 * Converts a given multicast IPv4/IPv6 address from a bridge
555 * to its matching multicast MAC address and copies it into the given
556 * destination buffer.
557 *
558 * Caller needs to make sure the destination buffer can hold
559 * at least ETH_ALEN bytes.
560 */
561static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
562{
563 if (src->proto == htons(ETH_P_IP))
564 ip_eth_mc_map(src->u.ip4, dst);
565#if IS_ENABLED(CONFIG_IPV6)
566 else if (src->proto == htons(ETH_P_IPV6))
567 ipv6_eth_mc_map(&src->u.ip6, dst);
568#endif
569 else
570 eth_zero_addr(dst);
571}
572
573/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100574 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
Linus Lüssing687937a2016-05-10 18:41:25 +0200575 * @dev: a bridge slave whose bridge to collect multicast addresses from
576 * @mcast_list: a list to put found addresses into
Linus Lüssing6bc45442019-05-07 06:08:26 +0200577 * @flags: flags indicating the new multicast state
Linus Lüssing687937a2016-05-10 18:41:25 +0200578 *
579 * Collects multicast addresses of multicast listeners residing
580 * on foreign, non-mesh devices which we gave access to our mesh via
581 * a bridge on top of the given soft interface, dev, in the given
582 * mcast_list.
583 *
584 * Return: -ENOMEM on memory allocation error or the number of
585 * items added to the mcast_list otherwise.
586 */
Linus Lüssing6bc45442019-05-07 06:08:26 +0200587static int batadv_mcast_mla_bridge_get(struct net_device *dev,
588 struct hlist_head *mcast_list,
589 struct batadv_mcast_mla_flags *flags)
Linus Lüssing687937a2016-05-10 18:41:25 +0200590{
591 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
592 struct br_ip_list *br_ip_entry, *tmp;
Linus Lüssing390dcd482019-05-26 18:35:50 +0200593 u8 tvlv_flags = flags->tvlv_flags;
Linus Lüssing687937a2016-05-10 18:41:25 +0200594 struct batadv_hw_addr *new;
595 u8 mcast_addr[ETH_ALEN];
596 int ret;
597
598 /* we don't need to detect these devices/listeners, the IGMP/MLD
599 * snooping code of the Linux bridge already does that for us
600 */
601 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
602 if (ret < 0)
603 goto out;
604
605 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
Linus Lüssing390dcd482019-05-26 18:35:50 +0200606 if (br_ip_entry->addr.proto == htons(ETH_P_IP)) {
607 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
608 continue;
Linus Lüssing6b253602018-03-04 21:02:18 +0100609
Linus Lüssing390dcd482019-05-26 18:35:50 +0200610 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
611 ipv4_is_local_multicast(br_ip_entry->addr.u.ip4))
612 continue;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200613
614 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
615 !ipv4_is_local_multicast(br_ip_entry->addr.u.ip4))
616 continue;
Linus Lüssing390dcd482019-05-26 18:35:50 +0200617 }
618
619#if IS_ENABLED(CONFIG_IPV6)
620 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) {
621 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
622 continue;
623
624 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
625 ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.u.ip6))
626 continue;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200627
628 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
629 IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.u.ip6) >
630 IPV6_ADDR_SCOPE_LINKLOCAL)
631 continue;
Linus Lüssing390dcd482019-05-26 18:35:50 +0200632 }
633#endif
Linus Lüssing6b253602018-03-04 21:02:18 +0100634
Linus Lüssing687937a2016-05-10 18:41:25 +0200635 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
636 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
637 continue;
638
639 new = kmalloc(sizeof(*new), GFP_ATOMIC);
640 if (!new) {
641 ret = -ENOMEM;
642 break;
643 }
644
645 ether_addr_copy(new->addr, mcast_addr);
646 hlist_add_head(&new->list, mcast_list);
647 }
648
649out:
650 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
651 list_del(&br_ip_entry->list);
652 kfree(br_ip_entry);
653 }
654
655 return ret;
656}
657
658/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100659 * batadv_mcast_mla_list_free() - free a list of multicast addresses
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100660 * @mcast_list: the list to free
661 *
662 * Removes and frees all items in the given mcast_list.
663 */
Linus Lüssingb7769762016-08-06 22:23:15 +0200664static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100665{
666 struct batadv_hw_addr *mcast_entry;
667 struct hlist_node *tmp;
668
669 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
670 hlist_del(&mcast_entry->list);
671 kfree(mcast_entry);
672 }
673}
674
675/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100676 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100677 * @bat_priv: the bat priv with all the soft interface information
678 * @mcast_list: a list of addresses which should _not_ be removed
679 *
680 * Retracts the announcement of any multicast listener from the
681 * translation table except the ones listed in the given mcast_list.
682 *
683 * If mcast_list is NULL then all are retracted.
684 */
685static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
686 struct hlist_head *mcast_list)
687{
688 struct batadv_hw_addr *mcast_entry;
689 struct hlist_node *tmp;
690
691 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
692 list) {
693 if (mcast_list &&
694 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
695 mcast_list))
696 continue;
697
698 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
699 BATADV_NO_FLAGS,
700 "mcast TT outdated", false);
701
702 hlist_del(&mcast_entry->list);
703 kfree(mcast_entry);
704 }
705}
706
707/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100708 * batadv_mcast_mla_tt_add() - add multicast listener announcements
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100709 * @bat_priv: the bat priv with all the soft interface information
710 * @mcast_list: a list of addresses which are going to get added
711 *
712 * Adds multicast listener announcements from the given mcast_list to the
713 * translation table if they have not been added yet.
714 */
715static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
716 struct hlist_head *mcast_list)
717{
718 struct batadv_hw_addr *mcast_entry;
719 struct hlist_node *tmp;
720
721 if (!mcast_list)
722 return;
723
724 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
725 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
726 &bat_priv->mcast.mla_list))
727 continue;
728
729 if (!batadv_tt_local_add(bat_priv->soft_iface,
730 mcast_entry->addr, BATADV_NO_FLAGS,
731 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
732 continue;
733
734 hlist_del(&mcast_entry->list);
735 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
736 }
737}
738
739/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100740 * batadv_mcast_querier_log() - debug output regarding the querier status on
741 * link
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200742 * @bat_priv: the bat priv with all the soft interface information
743 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
744 * @old_state: the previous querier state on our link
745 * @new_state: the new querier state on our link
746 *
747 * Outputs debug messages to the logging facility with log level 'mcast'
748 * regarding changes to the querier status on the link which are relevant
749 * to our multicast optimizations.
750 *
751 * Usually this is about whether a querier appeared or vanished in
752 * our mesh or whether the querier is in the suboptimal position of being
753 * behind our local bridge segment: Snooping switches will directly
754 * forward listener reports to the querier, therefore batman-adv and
755 * the bridge will potentially not see these listeners - the querier is
756 * potentially shadowing listeners from us then.
757 *
758 * This is only interesting for nodes with a bridge on top of their
759 * soft interface.
760 */
761static void
762batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
763 struct batadv_mcast_querier_state *old_state,
764 struct batadv_mcast_querier_state *new_state)
765{
766 if (!old_state->exists && new_state->exists)
767 batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
768 str_proto);
769 else if (old_state->exists && !new_state->exists)
770 batadv_info(bat_priv->soft_iface,
771 "%s Querier disappeared - multicast optimizations disabled\n",
772 str_proto);
Linus Lüssing6bc45442019-05-07 06:08:26 +0200773 else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists)
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200774 batadv_info(bat_priv->soft_iface,
775 "No %s Querier present - multicast optimizations disabled\n",
776 str_proto);
777
778 if (new_state->exists) {
779 if ((!old_state->shadowing && new_state->shadowing) ||
780 (!old_state->exists && new_state->shadowing))
781 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
782 "%s Querier is behind our bridged segment: Might shadow listeners\n",
783 str_proto);
784 else if (old_state->shadowing && !new_state->shadowing)
785 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
786 "%s Querier is not behind our bridged segment\n",
787 str_proto);
788 }
789}
790
791/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100792 * batadv_mcast_bridge_log() - debug output for topology changes in bridged
793 * setups
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200794 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing6bc45442019-05-07 06:08:26 +0200795 * @new_flags: flags indicating the new multicast state
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200796 *
797 * If no bridges are ever used on this node, then this function does nothing.
798 *
799 * Otherwise this function outputs debug information to the 'mcast' log level
800 * which might be relevant to our multicast optimizations.
801 *
802 * More precisely, it outputs information when a bridge interface is added or
803 * removed from a soft interface. And when a bridge is present, it further
804 * outputs information about the querier state which is relevant for the
805 * multicast flags this node is going to set.
806 */
807static void
Linus Lüssing6bc45442019-05-07 06:08:26 +0200808batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
809 struct batadv_mcast_mla_flags *new_flags)
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200810{
Linus Lüssing6bc45442019-05-07 06:08:26 +0200811 struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
812
813 if (!old_flags->bridged && new_flags->bridged)
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200814 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
815 "Bridge added: Setting Unsnoopables(U)-flag\n");
Linus Lüssing6bc45442019-05-07 06:08:26 +0200816 else if (old_flags->bridged && !new_flags->bridged)
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200817 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
818 "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
819
Linus Lüssing6bc45442019-05-07 06:08:26 +0200820 if (new_flags->bridged) {
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200821 batadv_mcast_querier_log(bat_priv, "IGMP",
Linus Lüssing6bc45442019-05-07 06:08:26 +0200822 &old_flags->querier_ipv4,
823 &new_flags->querier_ipv4);
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200824 batadv_mcast_querier_log(bat_priv, "MLD",
Linus Lüssing6bc45442019-05-07 06:08:26 +0200825 &old_flags->querier_ipv6,
826 &new_flags->querier_ipv6);
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200827 }
828}
829
830/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100831 * batadv_mcast_flags_logs() - output debug information about mcast flag changes
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200832 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing6bc45442019-05-07 06:08:26 +0200833 * @flags: TVLV flags indicating the new multicast state
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200834 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200835 * Whenever the multicast TVLV flags this node announces change, this function
836 * should be used to notify userspace about the change.
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200837 */
838static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
839{
Linus Lüssing6bc45442019-05-07 06:08:26 +0200840 bool old_enabled = bat_priv->mcast.mla_flags.enabled;
841 u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags;
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200842 char str_old_flags[] = "[.... . ]";
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200843
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200844 sprintf(str_old_flags, "[%c%c%c%s%s]",
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200845 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
846 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200847 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
848 !(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
849 !(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200850
851 batadv_dbg(BATADV_DBG_MCAST, bat_priv,
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200852 "Changing multicast flags from '%s' to '[%c%c%c%s%s]'\n",
Linus Lüssing6bc45442019-05-07 06:08:26 +0200853 old_enabled ? str_old_flags : "<undefined>",
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200854 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
855 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200856 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
857 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
858 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
Linus Lüssing72f7b2d2016-05-10 18:41:26 +0200859}
860
861/**
Linus Lüssing6bc45442019-05-07 06:08:26 +0200862 * batadv_mcast_mla_flags_update() - update multicast flags
Linus Lüssing60432d72014-02-15 17:47:51 +0100863 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing6bc45442019-05-07 06:08:26 +0200864 * @flags: flags indicating the new multicast state
Linus Lüssing60432d72014-02-15 17:47:51 +0100865 *
866 * Updates the own multicast tvlv with our current multicast related settings,
867 * capabilities and inabilities.
Linus Lüssing60432d72014-02-15 17:47:51 +0100868 */
Linus Lüssing6bc45442019-05-07 06:08:26 +0200869static void
870batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
871 struct batadv_mcast_mla_flags *flags)
Linus Lüssing60432d72014-02-15 17:47:51 +0100872{
873 struct batadv_tvlv_mcast_data mcast_data;
874
Linus Lüssing6bc45442019-05-07 06:08:26 +0200875 if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
876 return;
877
878 batadv_mcast_bridge_log(bat_priv, flags);
879 batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
880
881 mcast_data.flags = flags->tvlv_flags;
Linus Lüssing60432d72014-02-15 17:47:51 +0100882 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
883
Linus Lüssing6bc45442019-05-07 06:08:26 +0200884 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
885 &mcast_data, sizeof(mcast_data));
Linus Lüssing687937a2016-05-10 18:41:25 +0200886
Linus Lüssing6bc45442019-05-07 06:08:26 +0200887 bat_priv->mcast.mla_flags = *flags;
Linus Lüssing60432d72014-02-15 17:47:51 +0100888}
889
890/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100891 * __batadv_mcast_mla_update() - update the own MLAs
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100892 * @bat_priv: the bat priv with all the soft interface information
893 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100894 * Updates the own multicast listener announcements in the translation
895 * table as well as the own, announced multicast tvlv container.
Linus Lüssingcbebd362016-08-06 22:23:16 +0200896 *
897 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
898 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
899 * ensured by the non-parallel execution of the worker this function
900 * belongs to.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100901 */
Linus Lüssingcbebd362016-08-06 22:23:16 +0200902static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100903{
904 struct net_device *soft_iface = bat_priv->soft_iface;
905 struct hlist_head mcast_list = HLIST_HEAD_INIT;
Linus Lüssing6bc45442019-05-07 06:08:26 +0200906 struct batadv_mcast_mla_flags flags;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100907 int ret;
908
Linus Lüssing6bc45442019-05-07 06:08:26 +0200909 flags = batadv_mcast_mla_flags_get(bat_priv);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100910
Linus Lüssing6bc45442019-05-07 06:08:26 +0200911 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list, &flags);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100912 if (ret < 0)
913 goto out;
914
Linus Lüssing6bc45442019-05-07 06:08:26 +0200915 ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list, &flags);
Linus Lüssing687937a2016-05-10 18:41:25 +0200916 if (ret < 0)
917 goto out;
918
Linus Lüssing6bc45442019-05-07 06:08:26 +0200919 spin_lock(&bat_priv->mcast.mla_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100920 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
921 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
Linus Lüssing6bc45442019-05-07 06:08:26 +0200922 batadv_mcast_mla_flags_update(bat_priv, &flags);
923 spin_unlock(&bat_priv->mcast.mla_lock);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100924
925out:
Linus Lüssingb7769762016-08-06 22:23:15 +0200926 batadv_mcast_mla_list_free(&mcast_list);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100927}
928
929/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100930 * batadv_mcast_mla_update() - update the own MLAs
Linus Lüssingcbebd362016-08-06 22:23:16 +0200931 * @work: kernel work struct
932 *
933 * Updates the own multicast listener announcements in the translation
934 * table as well as the own, announced multicast tvlv container.
935 *
936 * In the end, reschedules the work timer.
937 */
938static void batadv_mcast_mla_update(struct work_struct *work)
939{
940 struct delayed_work *delayed_work;
941 struct batadv_priv_mcast *priv_mcast;
942 struct batadv_priv *bat_priv;
943
944 delayed_work = to_delayed_work(work);
945 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
946 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
947
948 __batadv_mcast_mla_update(bat_priv);
949 batadv_mcast_start_timer(bat_priv);
950}
951
952/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100953 * batadv_mcast_is_report_ipv4() - check for IGMP reports
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200954 * @skb: the ethernet frame destined for the mesh
955 *
956 * This call might reallocate skb data.
957 *
958 * Checks whether the given frame is a valid IGMP report.
959 *
960 * Return: If so then true, otherwise false.
961 */
962static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
963{
Linus Lüssingba5ea6142019-01-21 07:26:25 +0100964 if (ip_mc_check_igmp(skb) < 0)
Linus Lüssingbd2a9792016-05-10 18:41:24 +0200965 return false;
966
967 switch (igmp_hdr(skb)->type) {
968 case IGMP_HOST_MEMBERSHIP_REPORT:
969 case IGMPV2_HOST_MEMBERSHIP_REPORT:
970 case IGMPV3_HOST_MEMBERSHIP_REPORT:
971 return true;
972 }
973
974 return false;
975}
976
977/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100978 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
979 * potential
Linus Lüssingab498862014-02-15 17:47:53 +0100980 * @bat_priv: the bat priv with all the soft interface information
981 * @skb: the IPv4 packet to check
982 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing11d458c2019-06-11 22:58:41 +0200983 * @is_routable: stores whether the destination is routable
Linus Lüssingab498862014-02-15 17:47:53 +0100984 *
985 * Checks whether the given IPv4 packet has the potential to be forwarded with a
986 * mode more optimal than classic flooding.
987 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200988 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
989 * allocation failure.
Linus Lüssingab498862014-02-15 17:47:53 +0100990 */
991static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
992 struct sk_buff *skb,
Linus Lüssing11d458c2019-06-11 22:58:41 +0200993 bool *is_unsnoopable,
994 int *is_routable)
Linus Lüssingab498862014-02-15 17:47:53 +0100995{
996 struct iphdr *iphdr;
997
998 /* We might fail due to out-of-memory -> drop it */
999 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
1000 return -ENOMEM;
1001
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001002 if (batadv_mcast_is_report_ipv4(skb))
1003 return -EINVAL;
1004
Linus Lüssingab498862014-02-15 17:47:53 +01001005 iphdr = ip_hdr(skb);
1006
Linus Lüssingab498862014-02-15 17:47:53 +01001007 /* link-local multicast listeners behind a bridge are
1008 * not snoopable (see RFC4541, section 2.1.2.2)
1009 */
Linus Lüssing11d458c2019-06-11 22:58:41 +02001010 if (ipv4_is_local_multicast(iphdr->daddr))
1011 *is_unsnoopable = true;
1012 else
1013 *is_routable = ETH_P_IP;
Linus Lüssingab498862014-02-15 17:47:53 +01001014
1015 return 0;
1016}
1017
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001018/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001019 * batadv_mcast_is_report_ipv6() - check for MLD reports
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001020 * @skb: the ethernet frame destined for the mesh
1021 *
1022 * This call might reallocate skb data.
1023 *
1024 * Checks whether the given frame is a valid MLD report.
1025 *
1026 * Return: If so then true, otherwise false.
1027 */
1028static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
1029{
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001030 if (ipv6_mc_check_mld(skb) < 0)
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001031 return false;
1032
1033 switch (icmp6_hdr(skb)->icmp6_type) {
1034 case ICMPV6_MGM_REPORT:
1035 case ICMPV6_MLD2_REPORT:
1036 return true;
1037 }
1038
1039 return false;
1040}
1041
Linus Lüssingab498862014-02-15 17:47:53 +01001042/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001043 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
1044 * potential
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001045 * @bat_priv: the bat priv with all the soft interface information
1046 * @skb: the IPv6 packet to check
Linus Lüssingab498862014-02-15 17:47:53 +01001047 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing11d458c2019-06-11 22:58:41 +02001048 * @is_routable: stores whether the destination is routable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001049 *
1050 * Checks whether the given IPv6 packet has the potential to be forwarded with a
1051 * mode more optimal than classic flooding.
1052 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001053 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001054 */
1055static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +01001056 struct sk_buff *skb,
Linus Lüssing11d458c2019-06-11 22:58:41 +02001057 bool *is_unsnoopable,
1058 int *is_routable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001059{
1060 struct ipv6hdr *ip6hdr;
1061
1062 /* We might fail due to out-of-memory -> drop it */
1063 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
1064 return -ENOMEM;
1065
Linus Lüssingbd2a9792016-05-10 18:41:24 +02001066 if (batadv_mcast_is_report_ipv6(skb))
1067 return -EINVAL;
1068
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001069 ip6hdr = ipv6_hdr(skb);
1070
Linus Lüssing11d458c2019-06-11 22:58:41 +02001071 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001072 return -EINVAL;
1073
1074 /* link-local-all-nodes multicast listeners behind a bridge are
1075 * not snoopable (see RFC4541, section 3, paragraph 3)
1076 */
1077 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
Linus Lüssingab498862014-02-15 17:47:53 +01001078 *is_unsnoopable = true;
Linus Lüssing11d458c2019-06-11 22:58:41 +02001079 else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL)
1080 *is_routable = ETH_P_IPV6;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001081
1082 return 0;
1083}
1084
1085/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001086 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001087 * @bat_priv: the bat priv with all the soft interface information
1088 * @skb: the multicast frame to check
Linus Lüssingab498862014-02-15 17:47:53 +01001089 * @is_unsnoopable: stores whether the destination is snoopable
Linus Lüssing11d458c2019-06-11 22:58:41 +02001090 * @is_routable: stores whether the destination is routable
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001091 *
1092 * Checks whether the given multicast ethernet frame has the potential to be
1093 * forwarded with a mode more optimal than classic flooding.
1094 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001095 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001096 */
1097static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
Linus Lüssingab498862014-02-15 17:47:53 +01001098 struct sk_buff *skb,
Linus Lüssing11d458c2019-06-11 22:58:41 +02001099 bool *is_unsnoopable,
1100 int *is_routable)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001101{
1102 struct ethhdr *ethhdr = eth_hdr(skb);
1103
1104 if (!atomic_read(&bat_priv->multicast_mode))
1105 return -EINVAL;
1106
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001107 switch (ntohs(ethhdr->h_proto)) {
Linus Lüssingab498862014-02-15 17:47:53 +01001108 case ETH_P_IP:
1109 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
Linus Lüssing11d458c2019-06-11 22:58:41 +02001110 is_unsnoopable,
1111 is_routable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001112 case ETH_P_IPV6:
Sven Eckelmannc1bacea2017-02-22 17:16:39 +01001113 if (!IS_ENABLED(CONFIG_IPV6))
1114 return -EINVAL;
1115
Linus Lüssingab498862014-02-15 17:47:53 +01001116 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
Linus Lüssing11d458c2019-06-11 22:58:41 +02001117 is_unsnoopable,
1118 is_routable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001119 default:
1120 return -EINVAL;
1121 }
1122}
1123
1124/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001125 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
Antonio Quartulli6d030de2016-03-11 16:36:19 +01001126 * interest
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001127 * @bat_priv: the bat priv with all the soft interface information
1128 * @ethhdr: ethernet header of a packet
1129 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001130 * Return: the number of nodes which want all IPv4 multicast traffic if the
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001131 * given ethhdr is from an IPv4 packet or the number of nodes which want all
1132 * IPv6 traffic if it matches an IPv6 packet.
1133 */
1134static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
1135 struct ethhdr *ethhdr)
1136{
1137 switch (ntohs(ethhdr->h_proto)) {
1138 case ETH_P_IP:
1139 return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
1140 case ETH_P_IPV6:
1141 return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
1142 default:
1143 /* we shouldn't be here... */
1144 return 0;
1145 }
1146}
1147
1148/**
Linus Lüssing11d458c2019-06-11 22:58:41 +02001149 * batadv_mcast_forw_rtr_count() - count nodes with a multicast router
1150 * @bat_priv: the bat priv with all the soft interface information
1151 * @protocol: the ethernet protocol type to count multicast routers for
1152 *
1153 * Return: the number of nodes which want all routable IPv4 multicast traffic
1154 * if the protocol is ETH_P_IP or the number of nodes which want all routable
1155 * IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0.
1156 */
1157
1158static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
1159 int protocol)
1160{
1161 switch (protocol) {
1162 case ETH_P_IP:
1163 return atomic_read(&bat_priv->mcast.num_want_all_rtr4);
1164 case ETH_P_IPV6:
1165 return atomic_read(&bat_priv->mcast.num_want_all_rtr6);
1166 default:
1167 return 0;
1168 }
1169}
1170
1171/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001172 * batadv_mcast_forw_tt_node_get() - get a multicast tt node
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001173 * @bat_priv: the bat priv with all the soft interface information
1174 * @ethhdr: the ether header containing the multicast destination
1175 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001176 * Return: an orig_node matching the multicast address provided by ethhdr
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001177 * via a translation table lookup. This increases the returned nodes refcount.
1178 */
1179static struct batadv_orig_node *
1180batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
1181 struct ethhdr *ethhdr)
1182{
Linus Lüssingf8fb3412018-03-20 03:13:27 +01001183 return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
1184 BATADV_NO_FLAGS);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001185}
1186
1187/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001188 * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001189 * @bat_priv: the bat priv with all the soft interface information
1190 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001191 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001192 * increases its refcount.
1193 */
1194static struct batadv_orig_node *
1195batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
1196{
1197 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1198
1199 rcu_read_lock();
1200 hlist_for_each_entry_rcu(tmp_orig_node,
1201 &bat_priv->mcast.want_all_ipv4_list,
1202 mcast_want_all_ipv4_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +01001203 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001204 continue;
1205
1206 orig_node = tmp_orig_node;
1207 break;
1208 }
1209 rcu_read_unlock();
1210
1211 return orig_node;
1212}
1213
1214/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001215 * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001216 * @bat_priv: the bat priv with all the soft interface information
1217 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001218 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001219 * and increases its refcount.
1220 */
1221static struct batadv_orig_node *
1222batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
1223{
1224 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1225
1226 rcu_read_lock();
1227 hlist_for_each_entry_rcu(tmp_orig_node,
1228 &bat_priv->mcast.want_all_ipv6_list,
1229 mcast_want_all_ipv6_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +01001230 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001231 continue;
1232
1233 orig_node = tmp_orig_node;
1234 break;
1235 }
1236 rcu_read_unlock();
1237
1238 return orig_node;
1239}
1240
1241/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001242 * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001243 * @bat_priv: the bat priv with all the soft interface information
1244 * @ethhdr: an ethernet header to determine the protocol family from
1245 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001246 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001247 * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, sets and
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001248 * increases its refcount.
1249 */
1250static struct batadv_orig_node *
1251batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
1252 struct ethhdr *ethhdr)
1253{
1254 switch (ntohs(ethhdr->h_proto)) {
1255 case ETH_P_IP:
1256 return batadv_mcast_forw_ipv4_node_get(bat_priv);
1257 case ETH_P_IPV6:
1258 return batadv_mcast_forw_ipv6_node_get(bat_priv);
1259 default:
1260 /* we shouldn't be here... */
1261 return NULL;
1262 }
1263}
1264
1265/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001266 * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
Linus Lüssingab498862014-02-15 17:47:53 +01001267 * @bat_priv: the bat priv with all the soft interface information
1268 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001269 * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
Linus Lüssingab498862014-02-15 17:47:53 +01001270 * set and increases its refcount.
1271 */
1272static struct batadv_orig_node *
1273batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
1274{
1275 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1276
1277 rcu_read_lock();
1278 hlist_for_each_entry_rcu(tmp_orig_node,
1279 &bat_priv->mcast.want_all_unsnoopables_list,
1280 mcast_want_all_unsnoopables_node) {
Sven Eckelmann7c124392016-01-16 10:29:56 +01001281 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
Linus Lüssingab498862014-02-15 17:47:53 +01001282 continue;
1283
1284 orig_node = tmp_orig_node;
1285 break;
1286 }
1287 rcu_read_unlock();
1288
1289 return orig_node;
1290}
1291
1292/**
Linus Lüssing11d458c2019-06-11 22:58:41 +02001293 * batadv_mcast_forw_rtr4_node_get() - get a node with an ipv4 mcast router flag
1294 * @bat_priv: the bat priv with all the soft interface information
1295 *
1296 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR4 flag unset and
1297 * increases its refcount.
1298 */
1299static struct batadv_orig_node *
1300batadv_mcast_forw_rtr4_node_get(struct batadv_priv *bat_priv)
1301{
1302 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1303
1304 rcu_read_lock();
1305 hlist_for_each_entry_rcu(tmp_orig_node,
1306 &bat_priv->mcast.want_all_rtr4_list,
1307 mcast_want_all_rtr4_node) {
1308 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1309 continue;
1310
1311 orig_node = tmp_orig_node;
1312 break;
1313 }
1314 rcu_read_unlock();
1315
1316 return orig_node;
1317}
1318
1319/**
1320 * batadv_mcast_forw_rtr6_node_get() - get a node with an ipv6 mcast router flag
1321 * @bat_priv: the bat priv with all the soft interface information
1322 *
1323 * Return: an orig_node which has the BATADV_MCAST_WANT_NO_RTR6 flag unset
1324 * and increases its refcount.
1325 */
1326static struct batadv_orig_node *
1327batadv_mcast_forw_rtr6_node_get(struct batadv_priv *bat_priv)
1328{
1329 struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
1330
1331 rcu_read_lock();
1332 hlist_for_each_entry_rcu(tmp_orig_node,
1333 &bat_priv->mcast.want_all_rtr6_list,
1334 mcast_want_all_rtr6_node) {
1335 if (!kref_get_unless_zero(&tmp_orig_node->refcount))
1336 continue;
1337
1338 orig_node = tmp_orig_node;
1339 break;
1340 }
1341 rcu_read_unlock();
1342
1343 return orig_node;
1344}
1345
1346/**
1347 * batadv_mcast_forw_rtr_node_get() - get a node with an ipv4/ipv6 router flag
1348 * @bat_priv: the bat priv with all the soft interface information
1349 * @ethhdr: an ethernet header to determine the protocol family from
1350 *
1351 * Return: an orig_node which has no BATADV_MCAST_WANT_NO_RTR4 or
1352 * BATADV_MCAST_WANT_NO_RTR6 flag, depending on the provided ethhdr, set and
1353 * increases its refcount.
1354 */
1355static struct batadv_orig_node *
1356batadv_mcast_forw_rtr_node_get(struct batadv_priv *bat_priv,
1357 struct ethhdr *ethhdr)
1358{
1359 switch (ntohs(ethhdr->h_proto)) {
1360 case ETH_P_IP:
1361 return batadv_mcast_forw_rtr4_node_get(bat_priv);
1362 case ETH_P_IPV6:
1363 return batadv_mcast_forw_rtr6_node_get(bat_priv);
1364 default:
1365 /* we shouldn't be here... */
1366 return NULL;
1367 }
1368}
1369
1370/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001371 * batadv_mcast_forw_mode() - check on how to forward a multicast packet
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001372 * @bat_priv: the bat priv with all the soft interface information
1373 * @skb: The multicast packet to check
1374 * @orig: an originator to be set to forward the skb to
1375 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001376 * Return: the forwarding mode as enum batadv_forw_mode and in case of
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001377 * BATADV_FORW_SINGLE set the orig to the single originator the skb
1378 * should be forwarded to.
1379 */
1380enum batadv_forw_mode
1381batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
1382 struct batadv_orig_node **orig)
1383{
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001384 int ret, tt_count, ip_count, unsnoop_count, total_count;
Linus Lüssingab498862014-02-15 17:47:53 +01001385 bool is_unsnoopable = false;
Linus Lüssing32e72742019-03-23 05:47:41 +01001386 unsigned int mcast_fanout;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001387 struct ethhdr *ethhdr;
Linus Lüssing11d458c2019-06-11 22:58:41 +02001388 int is_routable = 0;
1389 int rtr_count = 0;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001390
Linus Lüssing11d458c2019-06-11 22:58:41 +02001391 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable,
1392 &is_routable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001393 if (ret == -ENOMEM)
1394 return BATADV_FORW_NONE;
1395 else if (ret < 0)
1396 return BATADV_FORW_ALL;
1397
1398 ethhdr = eth_hdr(skb);
1399
1400 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
1401 BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001402 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +01001403 unsnoop_count = !is_unsnoopable ? 0 :
1404 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
Linus Lüssing11d458c2019-06-11 22:58:41 +02001405 rtr_count = batadv_mcast_forw_rtr_count(bat_priv, is_routable);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001406
Linus Lüssing11d458c2019-06-11 22:58:41 +02001407 total_count = tt_count + ip_count + unsnoop_count + rtr_count;
Linus Lüssingab498862014-02-15 17:47:53 +01001408
1409 switch (total_count) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001410 case 1:
Linus Lüssingab498862014-02-15 17:47:53 +01001411 if (tt_count)
1412 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001413 else if (ip_count)
1414 *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +01001415 else if (unsnoop_count)
1416 *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
Linus Lüssing11d458c2019-06-11 22:58:41 +02001417 else if (rtr_count)
1418 *orig = batadv_mcast_forw_rtr_node_get(bat_priv,
1419 ethhdr);
Linus Lüssingab498862014-02-15 17:47:53 +01001420
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001421 if (*orig)
1422 return BATADV_FORW_SINGLE;
1423
Sven Eckelmanna7757d32019-10-31 17:34:37 +01001424 fallthrough;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001425 case 0:
1426 return BATADV_FORW_NONE;
1427 default:
Linus Lüssing32e72742019-03-23 05:47:41 +01001428 mcast_fanout = atomic_read(&bat_priv->multicast_fanout);
1429
1430 if (!unsnoop_count && total_count <= mcast_fanout)
1431 return BATADV_FORW_SOME;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001432 }
Linus Lüssing32e72742019-03-23 05:47:41 +01001433
1434 return BATADV_FORW_ALL;
1435}
1436
1437/**
1438 * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
1439 * @bat_priv: the bat priv with all the soft interface information
1440 * @skb: the multicast packet to transmit
1441 * @vid: the vlan identifier
1442 *
1443 * Sends copies of a frame with multicast destination to any multicast
1444 * listener registered in the translation table. A transmission is performed
1445 * via a batman-adv unicast packet for each such destination node.
1446 *
1447 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1448 * otherwise.
1449 */
1450static int
1451batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
1452 unsigned short vid)
1453{
1454 int ret = NET_XMIT_SUCCESS;
1455 struct sk_buff *newskb;
1456
1457 struct batadv_tt_orig_list_entry *orig_entry;
1458
1459 struct batadv_tt_global_entry *tt_global;
1460 const u8 *addr = eth_hdr(skb)->h_dest;
1461
1462 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
1463 if (!tt_global)
1464 goto out;
1465
1466 rcu_read_lock();
1467 hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
1468 newskb = skb_copy(skb, GFP_ATOMIC);
1469 if (!newskb) {
1470 ret = NET_XMIT_DROP;
1471 break;
1472 }
1473
1474 batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1475 orig_entry->orig_node, vid);
1476 }
1477 rcu_read_unlock();
1478
1479 batadv_tt_global_entry_put(tt_global);
1480
1481out:
1482 return ret;
1483}
1484
1485/**
1486 * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
1487 * @bat_priv: the bat priv with all the soft interface information
1488 * @skb: the multicast packet to transmit
1489 * @vid: the vlan identifier
1490 *
1491 * Sends copies of a frame with multicast destination to any node with a
1492 * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
1493 * batman-adv unicast packet for each such destination node.
1494 *
1495 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1496 * otherwise.
1497 */
1498static int
1499batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
1500 struct sk_buff *skb, unsigned short vid)
1501{
1502 struct batadv_orig_node *orig_node;
1503 int ret = NET_XMIT_SUCCESS;
1504 struct sk_buff *newskb;
1505
1506 rcu_read_lock();
1507 hlist_for_each_entry_rcu(orig_node,
1508 &bat_priv->mcast.want_all_ipv4_list,
1509 mcast_want_all_ipv4_node) {
1510 newskb = skb_copy(skb, GFP_ATOMIC);
1511 if (!newskb) {
1512 ret = NET_XMIT_DROP;
1513 break;
1514 }
1515
1516 batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1517 orig_node, vid);
1518 }
1519 rcu_read_unlock();
1520 return ret;
1521}
1522
1523/**
1524 * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
1525 * @bat_priv: the bat priv with all the soft interface information
1526 * @skb: The multicast packet to transmit
1527 * @vid: the vlan identifier
1528 *
1529 * Sends copies of a frame with multicast destination to any node with a
1530 * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
1531 * batman-adv unicast packet for each such destination node.
1532 *
1533 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1534 * otherwise.
1535 */
1536static int
1537batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
1538 struct sk_buff *skb, unsigned short vid)
1539{
1540 struct batadv_orig_node *orig_node;
1541 int ret = NET_XMIT_SUCCESS;
1542 struct sk_buff *newskb;
1543
1544 rcu_read_lock();
1545 hlist_for_each_entry_rcu(orig_node,
1546 &bat_priv->mcast.want_all_ipv6_list,
1547 mcast_want_all_ipv6_node) {
1548 newskb = skb_copy(skb, GFP_ATOMIC);
1549 if (!newskb) {
1550 ret = NET_XMIT_DROP;
1551 break;
1552 }
1553
1554 batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1555 orig_node, vid);
1556 }
1557 rcu_read_unlock();
1558 return ret;
1559}
1560
1561/**
1562 * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
1563 * @bat_priv: the bat priv with all the soft interface information
1564 * @skb: the multicast packet to transmit
1565 * @vid: the vlan identifier
1566 *
1567 * Sends copies of a frame with multicast destination to any node with a
1568 * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
1569 * transmission is performed via a batman-adv unicast packet for each such
1570 * destination node.
1571 *
1572 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1573 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1574 */
1575static int
1576batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
1577 struct sk_buff *skb, unsigned short vid)
1578{
1579 switch (ntohs(eth_hdr(skb)->h_proto)) {
1580 case ETH_P_IP:
1581 return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
1582 case ETH_P_IPV6:
1583 return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
1584 default:
1585 /* we shouldn't be here... */
1586 return NET_XMIT_DROP;
1587 }
1588}
1589
1590/**
Linus Lüssing11d458c2019-06-11 22:58:41 +02001591 * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
1592 * @bat_priv: the bat priv with all the soft interface information
1593 * @skb: the multicast packet to transmit
1594 * @vid: the vlan identifier
1595 *
1596 * Sends copies of a frame with multicast destination to any node with a
1597 * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
1598 * batman-adv unicast packet for each such destination node.
1599 *
1600 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1601 * otherwise.
1602 */
1603static int
1604batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
1605 struct sk_buff *skb, unsigned short vid)
1606{
1607 struct batadv_orig_node *orig_node;
1608 int ret = NET_XMIT_SUCCESS;
1609 struct sk_buff *newskb;
1610
1611 rcu_read_lock();
1612 hlist_for_each_entry_rcu(orig_node,
1613 &bat_priv->mcast.want_all_rtr4_list,
1614 mcast_want_all_rtr4_node) {
1615 newskb = skb_copy(skb, GFP_ATOMIC);
1616 if (!newskb) {
1617 ret = NET_XMIT_DROP;
1618 break;
1619 }
1620
1621 batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1622 orig_node, vid);
1623 }
1624 rcu_read_unlock();
1625 return ret;
1626}
1627
1628/**
1629 * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
1630 * @bat_priv: the bat priv with all the soft interface information
1631 * @skb: The multicast packet to transmit
1632 * @vid: the vlan identifier
1633 *
1634 * Sends copies of a frame with multicast destination to any node with a
1635 * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
1636 * batman-adv unicast packet for each such destination node.
1637 *
1638 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
1639 * otherwise.
1640 */
1641static int
1642batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
1643 struct sk_buff *skb, unsigned short vid)
1644{
1645 struct batadv_orig_node *orig_node;
1646 int ret = NET_XMIT_SUCCESS;
1647 struct sk_buff *newskb;
1648
1649 rcu_read_lock();
1650 hlist_for_each_entry_rcu(orig_node,
1651 &bat_priv->mcast.want_all_rtr6_list,
1652 mcast_want_all_rtr6_node) {
1653 newskb = skb_copy(skb, GFP_ATOMIC);
1654 if (!newskb) {
1655 ret = NET_XMIT_DROP;
1656 break;
1657 }
1658
1659 batadv_send_skb_unicast(bat_priv, newskb, BATADV_UNICAST, 0,
1660 orig_node, vid);
1661 }
1662 rcu_read_unlock();
1663 return ret;
1664}
1665
1666/**
1667 * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
1668 * @bat_priv: the bat priv with all the soft interface information
1669 * @skb: the multicast packet to transmit
1670 * @vid: the vlan identifier
1671 *
1672 * Sends copies of a frame with multicast destination to any node with a
1673 * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
1674 * transmission is performed via a batman-adv unicast packet for each such
1675 * destination node.
1676 *
1677 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1678 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1679 */
1680static int
1681batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
1682 struct sk_buff *skb, unsigned short vid)
1683{
1684 switch (ntohs(eth_hdr(skb)->h_proto)) {
1685 case ETH_P_IP:
1686 return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
1687 case ETH_P_IPV6:
1688 return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
1689 default:
1690 /* we shouldn't be here... */
1691 return NET_XMIT_DROP;
1692 }
1693}
1694
1695/**
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001696 * batadv_mcast_forw_send() - send packet to any detected multicast recipient
Linus Lüssing32e72742019-03-23 05:47:41 +01001697 * @bat_priv: the bat priv with all the soft interface information
1698 * @skb: the multicast packet to transmit
1699 * @vid: the vlan identifier
1700 *
1701 * Sends copies of a frame with multicast destination to any node that signaled
1702 * interest in it, that is either via the translation table or the according
1703 * want-all flags. A transmission is performed via a batman-adv unicast packet
1704 * for each such destination node.
1705 *
1706 * The given skb is consumed/freed.
1707 *
1708 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
1709 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
1710 */
1711int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
1712 unsigned short vid)
1713{
1714 int ret;
1715
1716 ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
1717 if (ret != NET_XMIT_SUCCESS) {
1718 kfree_skb(skb);
1719 return ret;
1720 }
1721
1722 ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
1723 if (ret != NET_XMIT_SUCCESS) {
1724 kfree_skb(skb);
1725 return ret;
1726 }
1727
Linus Lüssing11d458c2019-06-11 22:58:41 +02001728 ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
1729 if (ret != NET_XMIT_SUCCESS) {
1730 kfree_skb(skb);
1731 return ret;
1732 }
1733
Linus Lüssing32e72742019-03-23 05:47:41 +01001734 consume_skb(skb);
1735 return ret;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001736}
1737
1738/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001739 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
Linus Lüssingab498862014-02-15 17:47:53 +01001740 * @bat_priv: the bat priv with all the soft interface information
1741 * @orig: the orig_node which multicast state might have changed of
1742 * @mcast_flags: flags indicating the new multicast state
1743 *
1744 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001745 * orig, has toggled then this method updates the counter and the list
1746 * accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001747 *
1748 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssingab498862014-02-15 17:47:53 +01001749 */
1750static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
1751 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001752 u8 mcast_flags)
Linus Lüssingab498862014-02-15 17:47:53 +01001753{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001754 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
1755 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
1756
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001757 lockdep_assert_held(&orig->mcast_handler_lock);
1758
Linus Lüssingab498862014-02-15 17:47:53 +01001759 /* switched from flag unset to set */
1760 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
1761 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
1762 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
1763
1764 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001765 /* flag checks above + mcast_handler_lock prevents this */
1766 WARN_ON(!hlist_unhashed(node));
1767
1768 hlist_add_head_rcu(node, head);
Linus Lüssingab498862014-02-15 17:47:53 +01001769 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1770 /* switched from flag set to unset */
1771 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
1772 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
1773 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
1774
1775 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001776 /* flag checks above + mcast_handler_lock prevents this */
1777 WARN_ON(hlist_unhashed(node));
1778
1779 hlist_del_init_rcu(node);
Linus Lüssingab498862014-02-15 17:47:53 +01001780 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1781 }
1782}
1783
1784/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001785 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001786 * @bat_priv: the bat priv with all the soft interface information
1787 * @orig: the orig_node which multicast state might have changed of
1788 * @mcast_flags: flags indicating the new multicast state
1789 *
1790 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001791 * toggled then this method updates the counter and the list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001792 *
1793 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001794 */
1795static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
1796 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001797 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001798{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001799 struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
1800 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
1801
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001802 lockdep_assert_held(&orig->mcast_handler_lock);
1803
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001804 /* switched from flag unset to set */
1805 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
1806 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
1807 atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
1808
1809 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001810 /* flag checks above + mcast_handler_lock prevents this */
1811 WARN_ON(!hlist_unhashed(node));
1812
1813 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001814 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1815 /* switched from flag set to unset */
1816 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
1817 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
1818 atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
1819
1820 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001821 /* flag checks above + mcast_handler_lock prevents this */
1822 WARN_ON(hlist_unhashed(node));
1823
1824 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001825 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1826 }
1827}
1828
1829/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001830 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001831 * @bat_priv: the bat priv with all the soft interface information
1832 * @orig: the orig_node which multicast state might have changed of
1833 * @mcast_flags: flags indicating the new multicast state
1834 *
1835 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001836 * toggled then this method updates the counter and the list accordingly.
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001837 *
1838 * Caller needs to hold orig->mcast_handler_lock.
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001839 */
1840static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
1841 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001842 u8 mcast_flags)
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001843{
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001844 struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
1845 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
1846
Sven Eckelmann5274cd62015-06-21 14:45:15 +02001847 lockdep_assert_held(&orig->mcast_handler_lock);
1848
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001849 /* switched from flag unset to set */
1850 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
1851 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
1852 atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
1853
1854 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001855 /* flag checks above + mcast_handler_lock prevents this */
1856 WARN_ON(!hlist_unhashed(node));
1857
1858 hlist_add_head_rcu(node, head);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001859 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1860 /* switched from flag set to unset */
1861 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
1862 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
1863 atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
1864
1865 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001866 /* flag checks above + mcast_handler_lock prevents this */
1867 WARN_ON(hlist_unhashed(node));
1868
1869 hlist_del_init_rcu(node);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01001870 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1871 }
1872}
1873
1874/**
Linus Lüssing61caf3d2019-06-11 22:58:40 +02001875 * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
1876 * @bat_priv: the bat priv with all the soft interface information
1877 * @orig: the orig_node which multicast state might have changed of
1878 * @mcast_flags: flags indicating the new multicast state
1879 *
1880 * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001881 * toggled then this method updates the counter and the list accordingly.
Linus Lüssing61caf3d2019-06-11 22:58:40 +02001882 *
1883 * Caller needs to hold orig->mcast_handler_lock.
1884 */
1885static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
1886 struct batadv_orig_node *orig,
1887 u8 mcast_flags)
1888{
1889 struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
1890 struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
1891
1892 lockdep_assert_held(&orig->mcast_handler_lock);
1893
1894 /* switched from flag set to unset */
1895 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
1896 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
1897 atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
1898
1899 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1900 /* flag checks above + mcast_handler_lock prevents this */
1901 WARN_ON(!hlist_unhashed(node));
1902
1903 hlist_add_head_rcu(node, head);
1904 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1905 /* switched from flag unset to set */
1906 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
1907 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
1908 atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
1909
1910 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1911 /* flag checks above + mcast_handler_lock prevents this */
1912 WARN_ON(hlist_unhashed(node));
1913
1914 hlist_del_init_rcu(node);
1915 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1916 }
1917}
1918
1919/**
1920 * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
1921 * @bat_priv: the bat priv with all the soft interface information
1922 * @orig: the orig_node which multicast state might have changed of
1923 * @mcast_flags: flags indicating the new multicast state
1924 *
1925 * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001926 * toggled then this method updates the counter and the list accordingly.
Linus Lüssing61caf3d2019-06-11 22:58:40 +02001927 *
1928 * Caller needs to hold orig->mcast_handler_lock.
1929 */
1930static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
1931 struct batadv_orig_node *orig,
1932 u8 mcast_flags)
1933{
1934 struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
1935 struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
1936
1937 lockdep_assert_held(&orig->mcast_handler_lock);
1938
1939 /* switched from flag set to unset */
1940 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
1941 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
1942 atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
1943
1944 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1945 /* flag checks above + mcast_handler_lock prevents this */
1946 WARN_ON(!hlist_unhashed(node));
1947
1948 hlist_add_head_rcu(node, head);
1949 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1950 /* switched from flag unset to set */
1951 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
1952 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
1953 atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
1954
1955 spin_lock_bh(&bat_priv->mcast.want_lists_lock);
1956 /* flag checks above + mcast_handler_lock prevents this */
1957 WARN_ON(hlist_unhashed(node));
1958
1959 hlist_del_init_rcu(node);
1960 spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
1961 }
1962}
1963
1964/**
1965 * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
1966 * @enabled: whether the originator has multicast TVLV support enabled
1967 * @tvlv_value: tvlv buffer containing the multicast flags
1968 * @tvlv_value_len: tvlv buffer length
1969 *
1970 * Return: multicast flags for the given tvlv buffer
1971 */
1972static u8
1973batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
1974{
1975 u8 mcast_flags = BATADV_NO_FLAGS;
1976
1977 if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
1978 mcast_flags = *(u8 *)tvlv_value;
1979
1980 if (!enabled) {
1981 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
1982 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
1983 }
1984
1985 /* remove redundant flags to avoid sending duplicate packets later */
1986 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
1987 mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
1988
1989 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
1990 mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
1991
1992 return mcast_flags;
1993}
1994
1995/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001996 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
Linus Lüssing60432d72014-02-15 17:47:51 +01001997 * @bat_priv: the bat priv with all the soft interface information
1998 * @orig: the orig_node of the ogm
1999 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2000 * @tvlv_value: tvlv buffer containing the multicast data
2001 * @tvlv_value_len: tvlv buffer length
2002 */
Linus Lüssingbd2a9792016-05-10 18:41:24 +02002003static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
2004 struct batadv_orig_node *orig,
2005 u8 flags,
2006 void *tvlv_value,
2007 u16 tvlv_value_len)
Linus Lüssing60432d72014-02-15 17:47:51 +01002008{
2009 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002010 u8 mcast_flags;
Linus Lüssing60432d72014-02-15 17:47:51 +01002011
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002012 mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
2013 tvlv_value, tvlv_value_len);
Linus Lüssing60432d72014-02-15 17:47:51 +01002014
Linus Lüssingf26e4e92018-03-25 00:32:04 +01002015 spin_lock_bh(&orig->mcast_handler_lock);
2016
Linus Lüssing60432d72014-02-15 17:47:51 +01002017 if (orig_mcast_enabled &&
Linus Lüssing9c936e32015-06-16 17:10:25 +02002018 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing9c936e32015-06-16 17:10:25 +02002019 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +01002020 } else if (!orig_mcast_enabled &&
Linus Lüssingf26e4e92018-03-25 00:32:04 +01002021 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
Linus Lüssing9c936e32015-06-16 17:10:25 +02002022 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
Linus Lüssing60432d72014-02-15 17:47:51 +01002023 }
2024
Linus Lüssing9c936e32015-06-16 17:10:25 +02002025 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
Linus Lüssing60432d72014-02-15 17:47:51 +01002026
Linus Lüssingab498862014-02-15 17:47:53 +01002027 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01002028 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
2029 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002030 batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
2031 batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
Linus Lüssingab498862014-02-15 17:47:53 +01002032
Linus Lüssing60432d72014-02-15 17:47:51 +01002033 orig->mcast_flags = mcast_flags;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02002034 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01002035}
2036
2037/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002038 * batadv_mcast_init() - initialize the multicast optimizations structures
Linus Lüssing60432d72014-02-15 17:47:51 +01002039 * @bat_priv: the bat priv with all the soft interface information
2040 */
2041void batadv_mcast_init(struct batadv_priv *bat_priv)
2042{
Linus Lüssingbd2a9792016-05-10 18:41:24 +02002043 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
2044 NULL, BATADV_TVLV_MCAST, 2,
Linus Lüssing60432d72014-02-15 17:47:51 +01002045 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
Linus Lüssingcbebd362016-08-06 22:23:16 +02002046
2047 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
2048 batadv_mcast_start_timer(bat_priv);
Linus Lüssing60432d72014-02-15 17:47:51 +01002049}
2050
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002051#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Linus Lüssing60432d72014-02-15 17:47:51 +01002052/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002053 * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002054 * @bat_priv: the bat priv with all the soft interface information
2055 * @seq: debugfs table seq_file struct
2056 *
2057 * Prints our own multicast flags including a more specific reason why
2058 * they are set, that is prints the bridge and querier state too, to
2059 * the debugfs table specified via @seq.
2060 */
2061static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
2062 struct seq_file *seq)
2063{
Linus Lüssing6bc45442019-05-07 06:08:26 +02002064 struct batadv_mcast_mla_flags *mla_flags = &bat_priv->mcast.mla_flags;
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002065 char querier4, querier6, shadowing4, shadowing6;
Linus Lüssing6bc45442019-05-07 06:08:26 +02002066 bool bridged = mla_flags->bridged;
2067 u8 flags = mla_flags->tvlv_flags;
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002068
2069 if (bridged) {
Linus Lüssing6bc45442019-05-07 06:08:26 +02002070 querier4 = mla_flags->querier_ipv4.exists ? '.' : '4';
2071 querier6 = mla_flags->querier_ipv6.exists ? '.' : '6';
2072 shadowing4 = mla_flags->querier_ipv4.shadowing ? '4' : '.';
2073 shadowing6 = mla_flags->querier_ipv6.shadowing ? '6' : '.';
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002074 } else {
2075 querier4 = '?';
2076 querier6 = '?';
2077 shadowing4 = '?';
2078 shadowing6 = '?';
2079 }
2080
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002081 seq_printf(seq, "Multicast flags (own flags: [%c%c%c%s%s])\n",
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002082 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
2083 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002084 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
2085 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
2086 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ");
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002087 seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
2088 seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
2089 querier4, querier6);
2090 seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
2091 shadowing4, shadowing6);
2092 seq_puts(seq, "-------------------------------------------\n");
2093 seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
2094}
2095
2096/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002097 * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002098 * @seq: seq file to print on
2099 * @offset: not used
2100 *
2101 * This prints a table of (primary) originators and their according
2102 * multicast flags, including (in the header) our own.
2103 *
2104 * Return: always 0
2105 */
2106int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
2107{
2108 struct net_device *net_dev = (struct net_device *)seq->private;
2109 struct batadv_priv *bat_priv = netdev_priv(net_dev);
2110 struct batadv_hard_iface *primary_if;
2111 struct batadv_hashtable *hash = bat_priv->orig_hash;
2112 struct batadv_orig_node *orig_node;
2113 struct hlist_head *head;
2114 u8 flags;
2115 u32 i;
2116
2117 primary_if = batadv_seq_print_text_primary_if_get(seq);
2118 if (!primary_if)
2119 return 0;
2120
2121 batadv_mcast_flags_print_header(bat_priv, seq);
2122
2123 for (i = 0; i < hash->size; i++) {
2124 head = &hash->table[i];
2125
2126 rcu_read_lock();
2127 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2128 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2129 &orig_node->capa_initialized))
2130 continue;
2131
2132 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2133 &orig_node->capabilities)) {
2134 seq_printf(seq, "%pM -\n", orig_node->orig);
2135 continue;
2136 }
2137
2138 flags = orig_node->mcast_flags;
2139
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002140 seq_printf(seq, "%pM [%c%c%c%s%s]\n", orig_node->orig,
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002141 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
2142 ? 'U' : '.',
2143 (flags & BATADV_MCAST_WANT_ALL_IPV4)
2144 ? '4' : '.',
2145 (flags & BATADV_MCAST_WANT_ALL_IPV6)
Linus Lüssing61caf3d2019-06-11 22:58:40 +02002146 ? '6' : '.',
2147 !(flags & BATADV_MCAST_WANT_NO_RTR4)
2148 ? "R4" : ". ",
2149 !(flags & BATADV_MCAST_WANT_NO_RTR6)
2150 ? "R6" : ". ");
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002151 }
2152 rcu_read_unlock();
2153 }
2154
2155 batadv_hardif_put(primary_if);
2156
2157 return 0;
2158}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02002159#endif
Linus Lüssing4e3e8232016-05-10 18:41:27 +02002160
2161/**
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002162 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
2163 * @msg: buffer for the message
2164 * @bat_priv: the bat priv with all the soft interface information
2165 *
2166 * Return: 0 or error code.
2167 */
2168int batadv_mcast_mesh_info_put(struct sk_buff *msg,
2169 struct batadv_priv *bat_priv)
2170{
Linus Lüssing6bc45442019-05-07 06:08:26 +02002171 u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002172 u32 flags_priv = BATADV_NO_FLAGS;
2173
Linus Lüssing6bc45442019-05-07 06:08:26 +02002174 if (bat_priv->mcast.mla_flags.bridged) {
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002175 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
2176
Linus Lüssing6bc45442019-05-07 06:08:26 +02002177 if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002178 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
Linus Lüssing6bc45442019-05-07 06:08:26 +02002179 if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002180 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
Linus Lüssing6bc45442019-05-07 06:08:26 +02002181 if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002182 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
Linus Lüssing6bc45442019-05-07 06:08:26 +02002183 if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002184 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
2185 }
2186
2187 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
2188 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
2189 return -EMSGSIZE;
2190
2191 return 0;
2192}
2193
2194/**
2195 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
2196 * to a netlink socket
2197 * @msg: buffer for the message
2198 * @portid: netlink port
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002199 * @cb: Control block containing additional options
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002200 * @orig_node: originator to dump the multicast flags of
2201 *
2202 * Return: 0 or error code.
2203 */
2204static int
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002205batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
2206 struct netlink_callback *cb,
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002207 struct batadv_orig_node *orig_node)
2208{
2209 void *hdr;
2210
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002211 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2212 &batadv_netlink_family, NLM_F_MULTI,
2213 BATADV_CMD_GET_MCAST_FLAGS);
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002214 if (!hdr)
2215 return -ENOBUFS;
2216
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002217 genl_dump_check_consistent(cb, hdr);
2218
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002219 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2220 orig_node->orig)) {
2221 genlmsg_cancel(msg, hdr);
2222 return -EMSGSIZE;
2223 }
2224
2225 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2226 &orig_node->capabilities)) {
2227 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
2228 orig_node->mcast_flags)) {
2229 genlmsg_cancel(msg, hdr);
2230 return -EMSGSIZE;
2231 }
2232 }
2233
2234 genlmsg_end(msg, hdr);
2235 return 0;
2236}
2237
2238/**
2239 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
2240 * table to a netlink socket
2241 * @msg: buffer for the message
2242 * @portid: netlink port
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002243 * @cb: Control block containing additional options
2244 * @hash: hash to dump
2245 * @bucket: bucket index to dump
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002246 * @idx_skip: How many entries to skip
2247 *
2248 * Return: 0 or error code.
2249 */
2250static int
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002251batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
2252 struct netlink_callback *cb,
2253 struct batadv_hashtable *hash,
2254 unsigned int bucket, long *idx_skip)
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002255{
2256 struct batadv_orig_node *orig_node;
2257 long idx = 0;
2258
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002259 spin_lock_bh(&hash->list_locks[bucket]);
2260 cb->seq = atomic_read(&hash->generation) << 1 | 1;
2261
2262 hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002263 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
2264 &orig_node->capa_initialized))
2265 continue;
2266
2267 if (idx < *idx_skip)
2268 goto skip;
2269
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002270 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
2271 spin_unlock_bh(&hash->list_locks[bucket]);
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002272 *idx_skip = idx;
2273
2274 return -EMSGSIZE;
2275 }
2276
2277skip:
2278 idx++;
2279 }
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002280 spin_unlock_bh(&hash->list_locks[bucket]);
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002281
2282 return 0;
2283}
2284
2285/**
2286 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2287 * @msg: buffer for the message
2288 * @portid: netlink port
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002289 * @cb: Control block containing additional options
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002290 * @bat_priv: the bat priv with all the soft interface information
2291 * @bucket: current bucket to dump
2292 * @idx: index in current bucket to the next entry to dump
2293 *
2294 * Return: 0 or error code.
2295 */
2296static int
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002297__batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
2298 struct netlink_callback *cb,
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002299 struct batadv_priv *bat_priv, long *bucket, long *idx)
2300{
2301 struct batadv_hashtable *hash = bat_priv->orig_hash;
2302 long bucket_tmp = *bucket;
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002303 long idx_tmp = *idx;
2304
2305 while (bucket_tmp < hash->size) {
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002306 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
Sven Eckelmannfa3a03d2019-07-07 22:15:13 +02002307 bucket_tmp, &idx_tmp))
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002308 break;
2309
2310 bucket_tmp++;
2311 idx_tmp = 0;
2312 }
2313
2314 *bucket = bucket_tmp;
2315 *idx = idx_tmp;
2316
2317 return msg->len;
2318}
2319
2320/**
2321 * batadv_mcast_netlink_get_primary() - get primary interface from netlink
2322 * callback
2323 * @cb: netlink callback structure
2324 * @primary_if: the primary interface pointer to return the result in
2325 *
2326 * Return: 0 or error code.
2327 */
2328static int
2329batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
2330 struct batadv_hard_iface **primary_if)
2331{
2332 struct batadv_hard_iface *hard_iface = NULL;
2333 struct net *net = sock_net(cb->skb->sk);
2334 struct net_device *soft_iface;
2335 struct batadv_priv *bat_priv;
2336 int ifindex;
2337 int ret = 0;
2338
2339 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2340 if (!ifindex)
2341 return -EINVAL;
2342
2343 soft_iface = dev_get_by_index(net, ifindex);
2344 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2345 ret = -ENODEV;
2346 goto out;
2347 }
2348
2349 bat_priv = netdev_priv(soft_iface);
2350
2351 hard_iface = batadv_primary_if_get_selected(bat_priv);
2352 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
2353 ret = -ENOENT;
2354 goto out;
2355 }
2356
2357out:
2358 if (soft_iface)
2359 dev_put(soft_iface);
2360
2361 if (!ret && primary_if)
2362 *primary_if = hard_iface;
Colin Ian King65cc02a2018-03-23 22:53:50 +00002363 else if (hard_iface)
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002364 batadv_hardif_put(hard_iface);
2365
2366 return ret;
2367}
2368
2369/**
2370 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
2371 * @msg: buffer for the message
2372 * @cb: callback structure containing arguments
2373 *
2374 * Return: message length.
2375 */
2376int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
2377{
2378 struct batadv_hard_iface *primary_if = NULL;
2379 int portid = NETLINK_CB(cb->skb).portid;
2380 struct batadv_priv *bat_priv;
2381 long *bucket = &cb->args[0];
2382 long *idx = &cb->args[1];
2383 int ret;
2384
2385 ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
2386 if (ret)
2387 return ret;
2388
2389 bat_priv = netdev_priv(primary_if->soft_iface);
Sven Eckelmannd2d489b2018-10-30 22:01:30 +01002390 ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
Linus Lüssing53dd9a62018-03-13 11:41:13 +01002391
2392 batadv_hardif_put(primary_if);
2393 return ret;
2394}
2395
2396/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002397 * batadv_mcast_free() - free the multicast optimizations structures
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01002398 * @bat_priv: the bat priv with all the soft interface information
2399 */
2400void batadv_mcast_free(struct batadv_priv *bat_priv)
2401{
Linus Lüssingcbebd362016-08-06 22:23:16 +02002402 cancel_delayed_work_sync(&bat_priv->mcast.work);
2403
Linus Lüssingbd2a9792016-05-10 18:41:24 +02002404 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
2405 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
Linus Lüssing60432d72014-02-15 17:47:51 +01002406
Linus Lüssingcbebd362016-08-06 22:23:16 +02002407 /* safely calling outside of worker, as worker was canceled above */
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01002408 batadv_mcast_mla_tt_retract(bat_priv, NULL);
2409}
Linus Lüssing60432d72014-02-15 17:47:51 +01002410
2411/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01002412 * batadv_mcast_purge_orig() - reset originator global mcast state modifications
Linus Lüssing60432d72014-02-15 17:47:51 +01002413 * @orig: the originator which is going to get purged
2414 */
2415void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
2416{
2417 struct batadv_priv *bat_priv = orig->bat_priv;
2418
Linus Lüssing8a4023c2015-06-16 17:10:26 +02002419 spin_lock_bh(&orig->mcast_handler_lock);
2420
Linus Lüssingab498862014-02-15 17:47:53 +01002421 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
Linus Lüssing4c8755d2014-02-15 17:47:54 +01002422 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
2423 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
Sven Eckelmannf7af86c2019-07-07 23:04:57 +02002424 batadv_mcast_want_rtr4_update(bat_priv, orig,
2425 BATADV_MCAST_WANT_NO_RTR4);
2426 batadv_mcast_want_rtr6_update(bat_priv, orig,
2427 BATADV_MCAST_WANT_NO_RTR6);
Linus Lüssing8a4023c2015-06-16 17:10:26 +02002428
2429 spin_unlock_bh(&orig->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01002430}