Commit | Line | Data |
---|---|---|
0046b040 | 1 | /* Copyright (C) 2014-2016 B.A.T.M.A.N. contributors: |
c5caf4ef LL |
2 | * |
3 | * Linus Lüssing | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of version 2 of the GNU General Public | |
7 | * License as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
c5caf4ef | 18 | #include "multicast.h" |
1e2c2a4f SE |
19 | #include "main.h" |
20 | ||
21 | #include <linux/atomic.h> | |
9c936e3f | 22 | #include <linux/bitops.h> |
8a4023c5 | 23 | #include <linux/bug.h> |
1e2c2a4f SE |
24 | #include <linux/byteorder/generic.h> |
25 | #include <linux/errno.h> | |
26 | #include <linux/etherdevice.h> | |
27 | #include <linux/fs.h> | |
bd2a979e | 28 | #include <linux/icmpv6.h> |
687937ab | 29 | #include <linux/if_bridge.h> |
1e2c2a4f | 30 | #include <linux/if_ether.h> |
bd2a979e | 31 | #include <linux/igmp.h> |
1e2c2a4f | 32 | #include <linux/in.h> |
bd2a979e | 33 | #include <linux/in6.h> |
1e2c2a4f SE |
34 | #include <linux/ip.h> |
35 | #include <linux/ipv6.h> | |
72f7b2de | 36 | #include <linux/kernel.h> |
7c124391 | 37 | #include <linux/kref.h> |
1e2c2a4f | 38 | #include <linux/list.h> |
2c72d655 | 39 | #include <linux/lockdep.h> |
1e2c2a4f | 40 | #include <linux/netdevice.h> |
687937ab | 41 | #include <linux/printk.h> |
1e2c2a4f SE |
42 | #include <linux/rculist.h> |
43 | #include <linux/rcupdate.h> | |
4e3e823b | 44 | #include <linux/seq_file.h> |
1e2c2a4f SE |
45 | #include <linux/skbuff.h> |
46 | #include <linux/slab.h> | |
47 | #include <linux/spinlock.h> | |
48 | #include <linux/stddef.h> | |
49 | #include <linux/string.h> | |
50 | #include <linux/types.h> | |
51 | #include <net/addrconf.h> | |
687937ab LL |
52 | #include <net/if_inet6.h> |
53 | #include <net/ip.h> | |
1e2c2a4f SE |
54 | #include <net/ipv6.h> |
55 | ||
4e3e823b LL |
56 | #include "hard-interface.h" |
57 | #include "hash.h" | |
ba412080 | 58 | #include "log.h" |
1e2c2a4f | 59 | #include "packet.h" |
c5caf4ef | 60 | #include "translation-table.h" |
1f8dce49 | 61 | #include "tvlv.h" |
c5caf4ef | 62 | |
687937ab LL |
63 | /** |
64 | * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists | |
65 | * @soft_iface: netdev struct of the mesh interface | |
66 | * | |
67 | * If the given soft interface has a bridge on top then the refcount | |
68 | * of the according net device is increased. | |
69 | * | |
70 | * Return: NULL if no such bridge exists. Otherwise the net device of the | |
71 | * bridge. | |
72 | */ | |
73 | static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface) | |
74 | { | |
75 | struct net_device *upper = soft_iface; | |
76 | ||
77 | rcu_read_lock(); | |
78 | do { | |
79 | upper = netdev_master_upper_dev_get_rcu(upper); | |
80 | } while (upper && !(upper->priv_flags & IFF_EBRIDGE)); | |
81 | ||
82 | if (upper) | |
83 | dev_hold(upper); | |
84 | rcu_read_unlock(); | |
85 | ||
86 | return upper; | |
87 | } | |
88 | ||
c5caf4ef LL |
89 | /** |
90 | * batadv_mcast_mla_softif_get - get softif multicast listeners | |
91 | * @dev: the device to collect multicast addresses from | |
92 | * @mcast_list: a list to put found addresses into | |
93 | * | |
687937ab LL |
94 | * Collects multicast addresses of multicast listeners residing |
95 | * on this kernel on the given soft interface, dev, in | |
96 | * the given mcast_list. In general, multicast listeners provided by | |
97 | * your multicast receiving applications run directly on this node. | |
98 | * | |
99 | * If there is a bridge interface on top of dev, collects from that one | |
100 | * instead. Just like with IP addresses and routes, multicast listeners | |
101 | * will(/should) register to the bridge interface instead of an | |
102 | * enslaved bat0. | |
c5caf4ef | 103 | * |
62fe710f | 104 | * Return: -ENOMEM on memory allocation error or the number of |
c5caf4ef LL |
105 | * items added to the mcast_list otherwise. |
106 | */ | |
107 | static int batadv_mcast_mla_softif_get(struct net_device *dev, | |
108 | struct hlist_head *mcast_list) | |
109 | { | |
687937ab | 110 | struct net_device *bridge = batadv_mcast_get_bridge(dev); |
c5caf4ef LL |
111 | struct netdev_hw_addr *mc_list_entry; |
112 | struct batadv_hw_addr *new; | |
113 | int ret = 0; | |
114 | ||
687937ab LL |
115 | netif_addr_lock_bh(bridge ? bridge : dev); |
116 | netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) { | |
c5caf4ef LL |
117 | new = kmalloc(sizeof(*new), GFP_ATOMIC); |
118 | if (!new) { | |
119 | ret = -ENOMEM; | |
120 | break; | |
121 | } | |
122 | ||
123 | ether_addr_copy(new->addr, mc_list_entry->addr); | |
124 | hlist_add_head(&new->list, mcast_list); | |
125 | ret++; | |
126 | } | |
687937ab LL |
127 | netif_addr_unlock_bh(bridge ? bridge : dev); |
128 | ||
129 | if (bridge) | |
130 | dev_put(bridge); | |
c5caf4ef LL |
131 | |
132 | return ret; | |
133 | } | |
134 | ||
135 | /** | |
136 | * batadv_mcast_mla_is_duplicate - check whether an address is in a list | |
137 | * @mcast_addr: the multicast address to check | |
138 | * @mcast_list: the list with multicast addresses to search in | |
139 | * | |
62fe710f | 140 | * Return: true if the given address is already in the given list. |
c5caf4ef LL |
141 | * Otherwise returns false. |
142 | */ | |
6b5e971a | 143 | static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr, |
c5caf4ef LL |
144 | struct hlist_head *mcast_list) |
145 | { | |
146 | struct batadv_hw_addr *mcast_entry; | |
147 | ||
148 | hlist_for_each_entry(mcast_entry, mcast_list, list) | |
149 | if (batadv_compare_eth(mcast_entry->addr, mcast_addr)) | |
150 | return true; | |
151 | ||
152 | return false; | |
153 | } | |
154 | ||
687937ab LL |
155 | /** |
156 | * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address | |
157 | * @dst: destination to write to - a multicast MAC address | |
158 | * @src: source to read from - a multicast IP address | |
159 | * | |
160 | * Converts a given multicast IPv4/IPv6 address from a bridge | |
161 | * to its matching multicast MAC address and copies it into the given | |
162 | * destination buffer. | |
163 | * | |
164 | * Caller needs to make sure the destination buffer can hold | |
165 | * at least ETH_ALEN bytes. | |
166 | */ | |
167 | static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src) | |
168 | { | |
169 | if (src->proto == htons(ETH_P_IP)) | |
170 | ip_eth_mc_map(src->u.ip4, dst); | |
171 | #if IS_ENABLED(CONFIG_IPV6) | |
172 | else if (src->proto == htons(ETH_P_IPV6)) | |
173 | ipv6_eth_mc_map(&src->u.ip6, dst); | |
174 | #endif | |
175 | else | |
176 | eth_zero_addr(dst); | |
177 | } | |
178 | ||
179 | /** | |
180 | * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners | |
181 | * @dev: a bridge slave whose bridge to collect multicast addresses from | |
182 | * @mcast_list: a list to put found addresses into | |
183 | * | |
184 | * Collects multicast addresses of multicast listeners residing | |
185 | * on foreign, non-mesh devices which we gave access to our mesh via | |
186 | * a bridge on top of the given soft interface, dev, in the given | |
187 | * mcast_list. | |
188 | * | |
189 | * Return: -ENOMEM on memory allocation error or the number of | |
190 | * items added to the mcast_list otherwise. | |
191 | */ | |
192 | static int batadv_mcast_mla_bridge_get(struct net_device *dev, | |
193 | struct hlist_head *mcast_list) | |
194 | { | |
195 | struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list); | |
196 | struct br_ip_list *br_ip_entry, *tmp; | |
197 | struct batadv_hw_addr *new; | |
198 | u8 mcast_addr[ETH_ALEN]; | |
199 | int ret; | |
200 | ||
201 | /* we don't need to detect these devices/listeners, the IGMP/MLD | |
202 | * snooping code of the Linux bridge already does that for us | |
203 | */ | |
204 | ret = br_multicast_list_adjacent(dev, &bridge_mcast_list); | |
205 | if (ret < 0) | |
206 | goto out; | |
207 | ||
208 | list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) { | |
209 | batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr); | |
210 | if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list)) | |
211 | continue; | |
212 | ||
213 | new = kmalloc(sizeof(*new), GFP_ATOMIC); | |
214 | if (!new) { | |
215 | ret = -ENOMEM; | |
216 | break; | |
217 | } | |
218 | ||
219 | ether_addr_copy(new->addr, mcast_addr); | |
220 | hlist_add_head(&new->list, mcast_list); | |
221 | } | |
222 | ||
223 | out: | |
224 | list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) { | |
225 | list_del(&br_ip_entry->list); | |
226 | kfree(br_ip_entry); | |
227 | } | |
228 | ||
229 | return ret; | |
230 | } | |
231 | ||
c5caf4ef LL |
232 | /** |
233 | * batadv_mcast_mla_list_free - free a list of multicast addresses | |
2c72d655 | 234 | * @bat_priv: the bat priv with all the soft interface information |
c5caf4ef LL |
235 | * @mcast_list: the list to free |
236 | * | |
237 | * Removes and frees all items in the given mcast_list. | |
238 | */ | |
2c72d655 SE |
239 | static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv, |
240 | struct hlist_head *mcast_list) | |
c5caf4ef LL |
241 | { |
242 | struct batadv_hw_addr *mcast_entry; | |
243 | struct hlist_node *tmp; | |
244 | ||
2c72d655 SE |
245 | lockdep_assert_held(&bat_priv->tt.commit_lock); |
246 | ||
c5caf4ef LL |
247 | hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) { |
248 | hlist_del(&mcast_entry->list); | |
249 | kfree(mcast_entry); | |
250 | } | |
251 | } | |
252 | ||
253 | /** | |
254 | * batadv_mcast_mla_tt_retract - clean up multicast listener announcements | |
255 | * @bat_priv: the bat priv with all the soft interface information | |
256 | * @mcast_list: a list of addresses which should _not_ be removed | |
257 | * | |
258 | * Retracts the announcement of any multicast listener from the | |
259 | * translation table except the ones listed in the given mcast_list. | |
260 | * | |
261 | * If mcast_list is NULL then all are retracted. | |
262 | */ | |
263 | static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv, | |
264 | struct hlist_head *mcast_list) | |
265 | { | |
266 | struct batadv_hw_addr *mcast_entry; | |
267 | struct hlist_node *tmp; | |
268 | ||
2c72d655 SE |
269 | lockdep_assert_held(&bat_priv->tt.commit_lock); |
270 | ||
c5caf4ef LL |
271 | hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list, |
272 | list) { | |
273 | if (mcast_list && | |
274 | batadv_mcast_mla_is_duplicate(mcast_entry->addr, | |
275 | mcast_list)) | |
276 | continue; | |
277 | ||
278 | batadv_tt_local_remove(bat_priv, mcast_entry->addr, | |
279 | BATADV_NO_FLAGS, | |
280 | "mcast TT outdated", false); | |
281 | ||
282 | hlist_del(&mcast_entry->list); | |
283 | kfree(mcast_entry); | |
284 | } | |
285 | } | |
286 | ||
287 | /** | |
288 | * batadv_mcast_mla_tt_add - add multicast listener announcements | |
289 | * @bat_priv: the bat priv with all the soft interface information | |
290 | * @mcast_list: a list of addresses which are going to get added | |
291 | * | |
292 | * Adds multicast listener announcements from the given mcast_list to the | |
293 | * translation table if they have not been added yet. | |
294 | */ | |
295 | static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv, | |
296 | struct hlist_head *mcast_list) | |
297 | { | |
298 | struct batadv_hw_addr *mcast_entry; | |
299 | struct hlist_node *tmp; | |
300 | ||
2c72d655 SE |
301 | lockdep_assert_held(&bat_priv->tt.commit_lock); |
302 | ||
c5caf4ef LL |
303 | if (!mcast_list) |
304 | return; | |
305 | ||
306 | hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) { | |
307 | if (batadv_mcast_mla_is_duplicate(mcast_entry->addr, | |
308 | &bat_priv->mcast.mla_list)) | |
309 | continue; | |
310 | ||
311 | if (!batadv_tt_local_add(bat_priv->soft_iface, | |
312 | mcast_entry->addr, BATADV_NO_FLAGS, | |
313 | BATADV_NULL_IFINDEX, BATADV_NO_MARK)) | |
314 | continue; | |
315 | ||
316 | hlist_del(&mcast_entry->list); | |
317 | hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list); | |
318 | } | |
319 | } | |
320 | ||
321 | /** | |
322 | * batadv_mcast_has_bridge - check whether the soft-iface is bridged | |
323 | * @bat_priv: the bat priv with all the soft interface information | |
324 | * | |
62fe710f SE |
325 | * Checks whether there is a bridge on top of our soft interface. |
326 | * | |
327 | * Return: true if there is a bridge, false otherwise. | |
c5caf4ef LL |
328 | */ |
329 | static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv) | |
330 | { | |
331 | struct net_device *upper = bat_priv->soft_iface; | |
332 | ||
333 | rcu_read_lock(); | |
334 | do { | |
335 | upper = netdev_master_upper_dev_get_rcu(upper); | |
336 | } while (upper && !(upper->priv_flags & IFF_EBRIDGE)); | |
337 | rcu_read_unlock(); | |
338 | ||
339 | return upper; | |
340 | } | |
341 | ||
72f7b2de LL |
342 | /** |
343 | * batadv_mcast_querier_log - debug output regarding the querier status on link | |
344 | * @bat_priv: the bat priv with all the soft interface information | |
345 | * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD") | |
346 | * @old_state: the previous querier state on our link | |
347 | * @new_state: the new querier state on our link | |
348 | * | |
349 | * Outputs debug messages to the logging facility with log level 'mcast' | |
350 | * regarding changes to the querier status on the link which are relevant | |
351 | * to our multicast optimizations. | |
352 | * | |
353 | * Usually this is about whether a querier appeared or vanished in | |
354 | * our mesh or whether the querier is in the suboptimal position of being | |
355 | * behind our local bridge segment: Snooping switches will directly | |
356 | * forward listener reports to the querier, therefore batman-adv and | |
357 | * the bridge will potentially not see these listeners - the querier is | |
358 | * potentially shadowing listeners from us then. | |
359 | * | |
360 | * This is only interesting for nodes with a bridge on top of their | |
361 | * soft interface. | |
362 | */ | |
363 | static void | |
364 | batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto, | |
365 | struct batadv_mcast_querier_state *old_state, | |
366 | struct batadv_mcast_querier_state *new_state) | |
367 | { | |
368 | if (!old_state->exists && new_state->exists) | |
369 | batadv_info(bat_priv->soft_iface, "%s Querier appeared\n", | |
370 | str_proto); | |
371 | else if (old_state->exists && !new_state->exists) | |
372 | batadv_info(bat_priv->soft_iface, | |
373 | "%s Querier disappeared - multicast optimizations disabled\n", | |
374 | str_proto); | |
375 | else if (!bat_priv->mcast.bridged && !new_state->exists) | |
376 | batadv_info(bat_priv->soft_iface, | |
377 | "No %s Querier present - multicast optimizations disabled\n", | |
378 | str_proto); | |
379 | ||
380 | if (new_state->exists) { | |
381 | if ((!old_state->shadowing && new_state->shadowing) || | |
382 | (!old_state->exists && new_state->shadowing)) | |
383 | batadv_dbg(BATADV_DBG_MCAST, bat_priv, | |
384 | "%s Querier is behind our bridged segment: Might shadow listeners\n", | |
385 | str_proto); | |
386 | else if (old_state->shadowing && !new_state->shadowing) | |
387 | batadv_dbg(BATADV_DBG_MCAST, bat_priv, | |
388 | "%s Querier is not behind our bridged segment\n", | |
389 | str_proto); | |
390 | } | |
391 | } | |
392 | ||
393 | /** | |
394 | * batadv_mcast_bridge_log - debug output for topology changes in bridged setups | |
395 | * @bat_priv: the bat priv with all the soft interface information | |
396 | * @bridged: a flag about whether the soft interface is currently bridged or not | |
397 | * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier | |
398 | * @querier_ipv6: (maybe) new status of a potential, selected MLD querier | |
399 | * | |
400 | * If no bridges are ever used on this node, then this function does nothing. | |
401 | * | |
402 | * Otherwise this function outputs debug information to the 'mcast' log level | |
403 | * which might be relevant to our multicast optimizations. | |
404 | * | |
405 | * More precisely, it outputs information when a bridge interface is added or | |
406 | * removed from a soft interface. And when a bridge is present, it further | |
407 | * outputs information about the querier state which is relevant for the | |
408 | * multicast flags this node is going to set. | |
409 | */ | |
410 | static void | |
411 | batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged, | |
412 | struct batadv_mcast_querier_state *querier_ipv4, | |
413 | struct batadv_mcast_querier_state *querier_ipv6) | |
414 | { | |
415 | if (!bat_priv->mcast.bridged && bridged) | |
416 | batadv_dbg(BATADV_DBG_MCAST, bat_priv, | |
417 | "Bridge added: Setting Unsnoopables(U)-flag\n"); | |
418 | else if (bat_priv->mcast.bridged && !bridged) | |
419 | batadv_dbg(BATADV_DBG_MCAST, bat_priv, | |
420 | "Bridge removed: Unsetting Unsnoopables(U)-flag\n"); | |
421 | ||
422 | if (bridged) { | |
423 | batadv_mcast_querier_log(bat_priv, "IGMP", | |
424 | &bat_priv->mcast.querier_ipv4, | |
425 | querier_ipv4); | |
426 | batadv_mcast_querier_log(bat_priv, "MLD", | |
427 | &bat_priv->mcast.querier_ipv6, | |
428 | querier_ipv6); | |
429 | } | |
430 | } | |
431 | ||
432 | /** | |
433 | * batadv_mcast_flags_logs - output debug information about mcast flag changes | |
434 | * @bat_priv: the bat priv with all the soft interface information | |
435 | * @flags: flags indicating the new multicast state | |
436 | * | |
437 | * Whenever the multicast flags this nodes announces changes (@mcast_flags vs. | |
438 | * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level. | |
439 | */ | |
440 | static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags) | |
441 | { | |
442 | u8 old_flags = bat_priv->mcast.flags; | |
443 | char str_old_flags[] = "[...]"; | |
444 | ||
445 | sprintf(str_old_flags, "[%c%c%c]", | |
446 | (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.', | |
447 | (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.', | |
448 | (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.'); | |
449 | ||
450 | batadv_dbg(BATADV_DBG_MCAST, bat_priv, | |
451 | "Changing multicast flags from '%s' to '[%c%c%c]'\n", | |
452 | bat_priv->mcast.enabled ? str_old_flags : "<undefined>", | |
453 | (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.', | |
454 | (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.', | |
455 | (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.'); | |
456 | } | |
457 | ||
60432d75 LL |
458 | /** |
459 | * batadv_mcast_mla_tvlv_update - update multicast tvlv | |
460 | * @bat_priv: the bat priv with all the soft interface information | |
461 | * | |
462 | * Updates the own multicast tvlv with our current multicast related settings, | |
463 | * capabilities and inabilities. | |
464 | * | |
687937ab LL |
465 | * Return: false if we want all IPv4 && IPv6 multicast traffic and true |
466 | * otherwise. | |
60432d75 LL |
467 | */ |
468 | static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv) | |
469 | { | |
470 | struct batadv_tvlv_mcast_data mcast_data; | |
687937ab LL |
471 | struct batadv_mcast_querier_state querier4 = {false, false}; |
472 | struct batadv_mcast_querier_state querier6 = {false, false}; | |
473 | struct net_device *dev = bat_priv->soft_iface; | |
72f7b2de | 474 | bool bridged; |
60432d75 LL |
475 | |
476 | mcast_data.flags = BATADV_NO_FLAGS; | |
477 | memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved)); | |
478 | ||
72f7b2de LL |
479 | bridged = batadv_mcast_has_bridge(bat_priv); |
480 | if (!bridged) | |
687937ab LL |
481 | goto update; |
482 | ||
483 | #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING) | |
484 | pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n"); | |
485 | #endif | |
486 | ||
487 | querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP); | |
488 | querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP); | |
489 | ||
490 | querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6); | |
491 | querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6); | |
492 | ||
493 | mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES; | |
494 | ||
495 | /* 1) If no querier exists at all, then multicast listeners on | |
496 | * our local TT clients behind the bridge will keep silent. | |
497 | * 2) If the selected querier is on one of our local TT clients, | |
498 | * behind the bridge, then this querier might shadow multicast | |
499 | * listeners on our local TT clients, behind this bridge. | |
500 | * | |
501 | * In both cases, we will signalize other batman nodes that | |
502 | * we need all multicast traffic of the according protocol. | |
60432d75 | 503 | */ |
687937ab LL |
504 | if (!querier4.exists || querier4.shadowing) |
505 | mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4; | |
60432d75 | 506 | |
687937ab LL |
507 | if (!querier6.exists || querier6.shadowing) |
508 | mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6; | |
60432d75 | 509 | |
687937ab | 510 | update: |
72f7b2de LL |
511 | batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6); |
512 | ||
513 | bat_priv->mcast.querier_ipv4.exists = querier4.exists; | |
514 | bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing; | |
515 | ||
516 | bat_priv->mcast.querier_ipv6.exists = querier6.exists; | |
517 | bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing; | |
518 | ||
519 | bat_priv->mcast.bridged = bridged; | |
520 | ||
60432d75 LL |
521 | if (!bat_priv->mcast.enabled || |
522 | mcast_data.flags != bat_priv->mcast.flags) { | |
72f7b2de | 523 | batadv_mcast_flags_log(bat_priv, mcast_data.flags); |
bd2a979e | 524 | batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2, |
60432d75 LL |
525 | &mcast_data, sizeof(mcast_data)); |
526 | bat_priv->mcast.flags = mcast_data.flags; | |
527 | bat_priv->mcast.enabled = true; | |
528 | } | |
529 | ||
687937ab LL |
530 | return !(mcast_data.flags & |
531 | (BATADV_MCAST_WANT_ALL_IPV4 + BATADV_MCAST_WANT_ALL_IPV6)); | |
60432d75 LL |
532 | } |
533 | ||
c5caf4ef LL |
534 | /** |
535 | * batadv_mcast_mla_update - update the own MLAs | |
536 | * @bat_priv: the bat priv with all the soft interface information | |
537 | * | |
60432d75 LL |
538 | * Updates the own multicast listener announcements in the translation |
539 | * table as well as the own, announced multicast tvlv container. | |
c5caf4ef LL |
540 | */ |
541 | void batadv_mcast_mla_update(struct batadv_priv *bat_priv) | |
542 | { | |
543 | struct net_device *soft_iface = bat_priv->soft_iface; | |
544 | struct hlist_head mcast_list = HLIST_HEAD_INIT; | |
545 | int ret; | |
546 | ||
60432d75 | 547 | if (!batadv_mcast_mla_tvlv_update(bat_priv)) |
c5caf4ef LL |
548 | goto update; |
549 | ||
550 | ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list); | |
551 | if (ret < 0) | |
552 | goto out; | |
553 | ||
687937ab LL |
554 | ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list); |
555 | if (ret < 0) | |
556 | goto out; | |
557 | ||
c5caf4ef LL |
558 | update: |
559 | batadv_mcast_mla_tt_retract(bat_priv, &mcast_list); | |
560 | batadv_mcast_mla_tt_add(bat_priv, &mcast_list); | |
561 | ||
562 | out: | |
2c72d655 | 563 | batadv_mcast_mla_list_free(bat_priv, &mcast_list); |
c5caf4ef LL |
564 | } |
565 | ||
bd2a979e LL |
566 | /** |
567 | * batadv_mcast_is_report_ipv4 - check for IGMP reports | |
568 | * @skb: the ethernet frame destined for the mesh | |
569 | * | |
570 | * This call might reallocate skb data. | |
571 | * | |
572 | * Checks whether the given frame is a valid IGMP report. | |
573 | * | |
574 | * Return: If so then true, otherwise false. | |
575 | */ | |
576 | static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb) | |
577 | { | |
578 | if (ip_mc_check_igmp(skb, NULL) < 0) | |
579 | return false; | |
580 | ||
581 | switch (igmp_hdr(skb)->type) { | |
582 | case IGMP_HOST_MEMBERSHIP_REPORT: | |
583 | case IGMPV2_HOST_MEMBERSHIP_REPORT: | |
584 | case IGMPV3_HOST_MEMBERSHIP_REPORT: | |
585 | return true; | |
586 | } | |
587 | ||
588 | return false; | |
589 | } | |
590 | ||
ab49886e LL |
591 | /** |
592 | * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential | |
593 | * @bat_priv: the bat priv with all the soft interface information | |
594 | * @skb: the IPv4 packet to check | |
595 | * @is_unsnoopable: stores whether the destination is snoopable | |
596 | * | |
597 | * Checks whether the given IPv4 packet has the potential to be forwarded with a | |
598 | * mode more optimal than classic flooding. | |
599 | * | |
62fe710f SE |
600 | * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory |
601 | * allocation failure. | |
ab49886e LL |
602 | */ |
603 | static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv, | |
604 | struct sk_buff *skb, | |
605 | bool *is_unsnoopable) | |
606 | { | |
607 | struct iphdr *iphdr; | |
608 | ||
609 | /* We might fail due to out-of-memory -> drop it */ | |
610 | if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr))) | |
611 | return -ENOMEM; | |
612 | ||
bd2a979e LL |
613 | if (batadv_mcast_is_report_ipv4(skb)) |
614 | return -EINVAL; | |
615 | ||
ab49886e LL |
616 | iphdr = ip_hdr(skb); |
617 | ||
618 | /* TODO: Implement Multicast Router Discovery (RFC4286), | |
619 | * then allow scope > link local, too | |
620 | */ | |
621 | if (!ipv4_is_local_multicast(iphdr->daddr)) | |
622 | return -EINVAL; | |
623 | ||
624 | /* link-local multicast listeners behind a bridge are | |
625 | * not snoopable (see RFC4541, section 2.1.2.2) | |
626 | */ | |
627 | *is_unsnoopable = true; | |
628 | ||
629 | return 0; | |
630 | } | |
631 | ||
bd2a979e LL |
632 | #if IS_ENABLED(CONFIG_IPV6) |
633 | /** | |
634 | * batadv_mcast_is_report_ipv6 - check for MLD reports | |
635 | * @skb: the ethernet frame destined for the mesh | |
636 | * | |
637 | * This call might reallocate skb data. | |
638 | * | |
639 | * Checks whether the given frame is a valid MLD report. | |
640 | * | |
641 | * Return: If so then true, otherwise false. | |
642 | */ | |
643 | static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb) | |
644 | { | |
645 | if (ipv6_mc_check_mld(skb, NULL) < 0) | |
646 | return false; | |
647 | ||
648 | switch (icmp6_hdr(skb)->icmp6_type) { | |
649 | case ICMPV6_MGM_REPORT: | |
650 | case ICMPV6_MLD2_REPORT: | |
651 | return true; | |
652 | } | |
653 | ||
654 | return false; | |
655 | } | |
656 | ||
1d8ab8d3 LL |
657 | /** |
658 | * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential | |
659 | * @bat_priv: the bat priv with all the soft interface information | |
660 | * @skb: the IPv6 packet to check | |
ab49886e | 661 | * @is_unsnoopable: stores whether the destination is snoopable |
1d8ab8d3 LL |
662 | * |
663 | * Checks whether the given IPv6 packet has the potential to be forwarded with a | |
664 | * mode more optimal than classic flooding. | |
665 | * | |
62fe710f | 666 | * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory |
1d8ab8d3 LL |
667 | */ |
668 | static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv, | |
ab49886e LL |
669 | struct sk_buff *skb, |
670 | bool *is_unsnoopable) | |
1d8ab8d3 LL |
671 | { |
672 | struct ipv6hdr *ip6hdr; | |
673 | ||
674 | /* We might fail due to out-of-memory -> drop it */ | |
675 | if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr))) | |
676 | return -ENOMEM; | |
677 | ||
bd2a979e LL |
678 | if (batadv_mcast_is_report_ipv6(skb)) |
679 | return -EINVAL; | |
680 | ||
1d8ab8d3 LL |
681 | ip6hdr = ipv6_hdr(skb); |
682 | ||
683 | /* TODO: Implement Multicast Router Discovery (RFC4286), | |
684 | * then allow scope > link local, too | |
685 | */ | |
686 | if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL) | |
687 | return -EINVAL; | |
688 | ||
689 | /* link-local-all-nodes multicast listeners behind a bridge are | |
690 | * not snoopable (see RFC4541, section 3, paragraph 3) | |
691 | */ | |
692 | if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr)) | |
ab49886e | 693 | *is_unsnoopable = true; |
1d8ab8d3 LL |
694 | |
695 | return 0; | |
696 | } | |
bd2a979e | 697 | #endif |
1d8ab8d3 LL |
698 | |
699 | /** | |
700 | * batadv_mcast_forw_mode_check - check for optimized forwarding potential | |
701 | * @bat_priv: the bat priv with all the soft interface information | |
702 | * @skb: the multicast frame to check | |
ab49886e | 703 | * @is_unsnoopable: stores whether the destination is snoopable |
1d8ab8d3 LL |
704 | * |
705 | * Checks whether the given multicast ethernet frame has the potential to be | |
706 | * forwarded with a mode more optimal than classic flooding. | |
707 | * | |
62fe710f | 708 | * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory |
1d8ab8d3 LL |
709 | */ |
710 | static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv, | |
ab49886e LL |
711 | struct sk_buff *skb, |
712 | bool *is_unsnoopable) | |
1d8ab8d3 LL |
713 | { |
714 | struct ethhdr *ethhdr = eth_hdr(skb); | |
715 | ||
716 | if (!atomic_read(&bat_priv->multicast_mode)) | |
717 | return -EINVAL; | |
718 | ||
719 | if (atomic_read(&bat_priv->mcast.num_disabled)) | |
720 | return -EINVAL; | |
721 | ||
722 | switch (ntohs(ethhdr->h_proto)) { | |
ab49886e LL |
723 | case ETH_P_IP: |
724 | return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb, | |
725 | is_unsnoopable); | |
bd2a979e | 726 | #if IS_ENABLED(CONFIG_IPV6) |
1d8ab8d3 | 727 | case ETH_P_IPV6: |
ab49886e LL |
728 | return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb, |
729 | is_unsnoopable); | |
bd2a979e | 730 | #endif |
1d8ab8d3 LL |
731 | default: |
732 | return -EINVAL; | |
733 | } | |
734 | } | |
735 | ||
4c8755d6 | 736 | /** |
6d030de8 AQ |
737 | * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast |
738 | * interest | |
4c8755d6 LL |
739 | * @bat_priv: the bat priv with all the soft interface information |
740 | * @ethhdr: ethernet header of a packet | |
741 | * | |
62fe710f | 742 | * Return: the number of nodes which want all IPv4 multicast traffic if the |
4c8755d6 LL |
743 | * given ethhdr is from an IPv4 packet or the number of nodes which want all |
744 | * IPv6 traffic if it matches an IPv6 packet. | |
745 | */ | |
746 | static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv, | |
747 | struct ethhdr *ethhdr) | |
748 | { | |
749 | switch (ntohs(ethhdr->h_proto)) { | |
750 | case ETH_P_IP: | |
751 | return atomic_read(&bat_priv->mcast.num_want_all_ipv4); | |
752 | case ETH_P_IPV6: | |
753 | return atomic_read(&bat_priv->mcast.num_want_all_ipv6); | |
754 | default: | |
755 | /* we shouldn't be here... */ | |
756 | return 0; | |
757 | } | |
758 | } | |
759 | ||
1d8ab8d3 LL |
760 | /** |
761 | * batadv_mcast_forw_tt_node_get - get a multicast tt node | |
762 | * @bat_priv: the bat priv with all the soft interface information | |
763 | * @ethhdr: the ether header containing the multicast destination | |
764 | * | |
62fe710f | 765 | * Return: an orig_node matching the multicast address provided by ethhdr |
1d8ab8d3 LL |
766 | * via a translation table lookup. This increases the returned nodes refcount. |
767 | */ | |
768 | static struct batadv_orig_node * | |
769 | batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv, | |
770 | struct ethhdr *ethhdr) | |
771 | { | |
772 | return batadv_transtable_search(bat_priv, ethhdr->h_source, | |
773 | ethhdr->h_dest, BATADV_NO_FLAGS); | |
774 | } | |
775 | ||
4c8755d6 | 776 | /** |
6d030de8 | 777 | * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag |
4c8755d6 LL |
778 | * @bat_priv: the bat priv with all the soft interface information |
779 | * | |
62fe710f | 780 | * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and |
4c8755d6 LL |
781 | * increases its refcount. |
782 | */ | |
783 | static struct batadv_orig_node * | |
784 | batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv) | |
785 | { | |
786 | struct batadv_orig_node *tmp_orig_node, *orig_node = NULL; | |
787 | ||
788 | rcu_read_lock(); | |
789 | hlist_for_each_entry_rcu(tmp_orig_node, | |
790 | &bat_priv->mcast.want_all_ipv4_list, | |
791 | mcast_want_all_ipv4_node) { | |
7c124391 | 792 | if (!kref_get_unless_zero(&tmp_orig_node->refcount)) |
4c8755d6 LL |
793 | continue; |
794 | ||
795 | orig_node = tmp_orig_node; | |
796 | break; | |
797 | } | |
798 | rcu_read_unlock(); | |
799 | ||
800 | return orig_node; | |
801 | } | |
802 | ||
803 | /** | |
6d030de8 | 804 | * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag |
4c8755d6 LL |
805 | * @bat_priv: the bat priv with all the soft interface information |
806 | * | |
62fe710f | 807 | * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set |
4c8755d6 LL |
808 | * and increases its refcount. |
809 | */ | |
810 | static struct batadv_orig_node * | |
811 | batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv) | |
812 | { | |
813 | struct batadv_orig_node *tmp_orig_node, *orig_node = NULL; | |
814 | ||
815 | rcu_read_lock(); | |
816 | hlist_for_each_entry_rcu(tmp_orig_node, | |
817 | &bat_priv->mcast.want_all_ipv6_list, | |
818 | mcast_want_all_ipv6_node) { | |
7c124391 | 819 | if (!kref_get_unless_zero(&tmp_orig_node->refcount)) |
4c8755d6 LL |
820 | continue; |
821 | ||
822 | orig_node = tmp_orig_node; | |
823 | break; | |
824 | } | |
825 | rcu_read_unlock(); | |
826 | ||
827 | return orig_node; | |
828 | } | |
829 | ||
830 | /** | |
6d030de8 | 831 | * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag |
4c8755d6 LL |
832 | * @bat_priv: the bat priv with all the soft interface information |
833 | * @ethhdr: an ethernet header to determine the protocol family from | |
834 | * | |
62fe710f | 835 | * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or |
4c8755d6 LL |
836 | * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and |
837 | * increases its refcount. | |
838 | */ | |
839 | static struct batadv_orig_node * | |
840 | batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv, | |
841 | struct ethhdr *ethhdr) | |
842 | { | |
843 | switch (ntohs(ethhdr->h_proto)) { | |
844 | case ETH_P_IP: | |
845 | return batadv_mcast_forw_ipv4_node_get(bat_priv); | |
846 | case ETH_P_IPV6: | |
847 | return batadv_mcast_forw_ipv6_node_get(bat_priv); | |
848 | default: | |
849 | /* we shouldn't be here... */ | |
850 | return NULL; | |
851 | } | |
852 | } | |
853 | ||
ab49886e | 854 | /** |
6d030de8 | 855 | * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag |
ab49886e LL |
856 | * @bat_priv: the bat priv with all the soft interface information |
857 | * | |
62fe710f | 858 | * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag |
ab49886e LL |
859 | * set and increases its refcount. |
860 | */ | |
861 | static struct batadv_orig_node * | |
862 | batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv) | |
863 | { | |
864 | struct batadv_orig_node *tmp_orig_node, *orig_node = NULL; | |
865 | ||
866 | rcu_read_lock(); | |
867 | hlist_for_each_entry_rcu(tmp_orig_node, | |
868 | &bat_priv->mcast.want_all_unsnoopables_list, | |
869 | mcast_want_all_unsnoopables_node) { | |
7c124391 | 870 | if (!kref_get_unless_zero(&tmp_orig_node->refcount)) |
ab49886e LL |
871 | continue; |
872 | ||
873 | orig_node = tmp_orig_node; | |
874 | break; | |
875 | } | |
876 | rcu_read_unlock(); | |
877 | ||
878 | return orig_node; | |
879 | } | |
880 | ||
1d8ab8d3 LL |
881 | /** |
882 | * batadv_mcast_forw_mode - check on how to forward a multicast packet | |
883 | * @bat_priv: the bat priv with all the soft interface information | |
884 | * @skb: The multicast packet to check | |
885 | * @orig: an originator to be set to forward the skb to | |
886 | * | |
62fe710f | 887 | * Return: the forwarding mode as enum batadv_forw_mode and in case of |
1d8ab8d3 LL |
888 | * BATADV_FORW_SINGLE set the orig to the single originator the skb |
889 | * should be forwarded to. | |
890 | */ | |
891 | enum batadv_forw_mode | |
892 | batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb, | |
893 | struct batadv_orig_node **orig) | |
894 | { | |
4c8755d6 | 895 | int ret, tt_count, ip_count, unsnoop_count, total_count; |
ab49886e | 896 | bool is_unsnoopable = false; |
1d8ab8d3 | 897 | struct ethhdr *ethhdr; |
1d8ab8d3 | 898 | |
ab49886e | 899 | ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable); |
1d8ab8d3 LL |
900 | if (ret == -ENOMEM) |
901 | return BATADV_FORW_NONE; | |
902 | else if (ret < 0) | |
903 | return BATADV_FORW_ALL; | |
904 | ||
905 | ethhdr = eth_hdr(skb); | |
906 | ||
907 | tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest, | |
908 | BATADV_NO_FLAGS); | |
4c8755d6 | 909 | ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr); |
ab49886e LL |
910 | unsnoop_count = !is_unsnoopable ? 0 : |
911 | atomic_read(&bat_priv->mcast.num_want_all_unsnoopables); | |
912 | ||
4c8755d6 | 913 | total_count = tt_count + ip_count + unsnoop_count; |
1d8ab8d3 | 914 | |
ab49886e | 915 | switch (total_count) { |
1d8ab8d3 | 916 | case 1: |
ab49886e LL |
917 | if (tt_count) |
918 | *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr); | |
4c8755d6 LL |
919 | else if (ip_count) |
920 | *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr); | |
ab49886e LL |
921 | else if (unsnoop_count) |
922 | *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv); | |
923 | ||
1d8ab8d3 LL |
924 | if (*orig) |
925 | return BATADV_FORW_SINGLE; | |
926 | ||
927 | /* fall through */ | |
928 | case 0: | |
929 | return BATADV_FORW_NONE; | |
930 | default: | |
931 | return BATADV_FORW_ALL; | |
932 | } | |
933 | } | |
934 | ||
ab49886e LL |
935 | /** |
936 | * batadv_mcast_want_unsnoop_update - update unsnoop counter and list | |
937 | * @bat_priv: the bat priv with all the soft interface information | |
938 | * @orig: the orig_node which multicast state might have changed of | |
939 | * @mcast_flags: flags indicating the new multicast state | |
940 | * | |
941 | * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator, | |
942 | * orig, has toggled then this method updates counter and list accordingly. | |
8a4023c5 LL |
943 | * |
944 | * Caller needs to hold orig->mcast_handler_lock. | |
ab49886e LL |
945 | */ |
946 | static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv, | |
947 | struct batadv_orig_node *orig, | |
6b5e971a | 948 | u8 mcast_flags) |
ab49886e | 949 | { |
8a4023c5 LL |
950 | struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node; |
951 | struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list; | |
952 | ||
5274cd68 SE |
953 | lockdep_assert_held(&orig->mcast_handler_lock); |
954 | ||
ab49886e LL |
955 | /* switched from flag unset to set */ |
956 | if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES && | |
957 | !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) { | |
958 | atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables); | |
959 | ||
960 | spin_lock_bh(&bat_priv->mcast.want_lists_lock); | |
8a4023c5 LL |
961 | /* flag checks above + mcast_handler_lock prevents this */ |
962 | WARN_ON(!hlist_unhashed(node)); | |
963 | ||
964 | hlist_add_head_rcu(node, head); | |
ab49886e LL |
965 | spin_unlock_bh(&bat_priv->mcast.want_lists_lock); |
966 | /* switched from flag set to unset */ | |
967 | } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) && | |
968 | orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) { | |
969 | atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables); | |
970 | ||
971 | spin_lock_bh(&bat_priv->mcast.want_lists_lock); | |
8a4023c5 LL |
972 | /* flag checks above + mcast_handler_lock prevents this */ |
973 | WARN_ON(hlist_unhashed(node)); | |
974 | ||
975 | hlist_del_init_rcu(node); | |
ab49886e LL |
976 | spin_unlock_bh(&bat_priv->mcast.want_lists_lock); |
977 | } | |
978 | } | |
979 | ||
4c8755d6 LL |
980 | /** |
981 | * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list | |
982 | * @bat_priv: the bat priv with all the soft interface information | |
983 | * @orig: the orig_node which multicast state might have changed of | |
984 | * @mcast_flags: flags indicating the new multicast state | |
985 | * | |
986 | * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has | |
987 | * toggled then this method updates counter and list accordingly. | |
8a4023c5 LL |
988 | * |
989 | * Caller needs to hold orig->mcast_handler_lock. | |
4c8755d6 LL |
990 | */ |
991 | static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv, | |
992 | struct batadv_orig_node *orig, | |
6b5e971a | 993 | u8 mcast_flags) |
4c8755d6 | 994 | { |
8a4023c5 LL |
995 | struct hlist_node *node = &orig->mcast_want_all_ipv4_node; |
996 | struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list; | |
997 | ||
5274cd68 SE |
998 | lockdep_assert_held(&orig->mcast_handler_lock); |
999 | ||
4c8755d6 LL |
1000 | /* switched from flag unset to set */ |
1001 | if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 && | |
1002 | !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) { | |
1003 | atomic_inc(&bat_priv->mcast.num_want_all_ipv4); | |
1004 | ||
1005 | spin_lock_bh(&bat_priv->mcast.want_lists_lock); | |
8a4023c5 LL |
1006 | /* flag checks above + mcast_handler_lock prevents this */ |
1007 | WARN_ON(!hlist_unhashed(node)); | |
1008 | ||
1009 | hlist_add_head_rcu(node, head); | |
4c8755d6 LL |
1010 | spin_unlock_bh(&bat_priv->mcast.want_lists_lock); |
1011 | /* switched from flag set to unset */ | |
1012 | } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) && | |
1013 | orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) { | |
1014 | atomic_dec(&bat_priv->mcast.num_want_all_ipv4); | |
1015 | ||
1016 | spin_lock_bh(&bat_priv->mcast.want_lists_lock); | |
8a4023c5 LL |
1017 | /* flag checks above + mcast_handler_lock prevents this */ |
1018 | WARN_ON(hlist_unhashed(node)); | |
1019 | ||
1020 | hlist_del_init_rcu(node); | |
4c8755d6 LL |
1021 | spin_unlock_bh(&bat_priv->mcast.want_lists_lock); |
1022 | } | |
1023 | } | |
1024 | ||
1025 | /** | |
1026 | * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list | |
1027 | * @bat_priv: the bat priv with all the soft interface information | |
1028 | * @orig: the orig_node which multicast state might have changed of | |
1029 | * @mcast_flags: flags indicating the new multicast state | |
1030 | * | |
1031 | * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has | |
1032 | * toggled then this method updates counter and list accordingly. | |
8a4023c5 LL |
1033 | * |
1034 | * Caller needs to hold orig->mcast_handler_lock. | |
4c8755d6 LL |
1035 | */ |
1036 | static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv, | |
1037 | struct batadv_orig_node *orig, | |
6b5e971a | 1038 | u8 mcast_flags) |
4c8755d6 | 1039 | { |
8a4023c5 LL |
1040 | struct hlist_node *node = &orig->mcast_want_all_ipv6_node; |
1041 | struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list; | |
1042 | ||
5274cd68 SE |
1043 | lockdep_assert_held(&orig->mcast_handler_lock); |
1044 | ||
4c8755d6 LL |
1045 | /* switched from flag unset to set */ |
1046 | if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 && | |
1047 | !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) { | |
1048 | atomic_inc(&bat_priv->mcast.num_want_all_ipv6); | |
1049 | ||
1050 | spin_lock_bh(&bat_priv->mcast.want_lists_lock); | |
8a4023c5 LL |
1051 | /* flag checks above + mcast_handler_lock prevents this */ |
1052 | WARN_ON(!hlist_unhashed(node)); | |
1053 | ||
1054 | hlist_add_head_rcu(node, head); | |
4c8755d6 LL |
1055 | spin_unlock_bh(&bat_priv->mcast.want_lists_lock); |
1056 | /* switched from flag set to unset */ | |
1057 | } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) && | |
1058 | orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) { | |
1059 | atomic_dec(&bat_priv->mcast.num_want_all_ipv6); | |
1060 | ||
1061 | spin_lock_bh(&bat_priv->mcast.want_lists_lock); | |
8a4023c5 LL |
1062 | /* flag checks above + mcast_handler_lock prevents this */ |
1063 | WARN_ON(hlist_unhashed(node)); | |
1064 | ||
1065 | hlist_del_init_rcu(node); | |
4c8755d6 LL |
1066 | spin_unlock_bh(&bat_priv->mcast.want_lists_lock); |
1067 | } | |
1068 | } | |
1069 | ||
60432d75 | 1070 | /** |
bd2a979e | 1071 | * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container |
60432d75 LL |
1072 | * @bat_priv: the bat priv with all the soft interface information |
1073 | * @orig: the orig_node of the ogm | |
1074 | * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) | |
1075 | * @tvlv_value: tvlv buffer containing the multicast data | |
1076 | * @tvlv_value_len: tvlv buffer length | |
1077 | */ | |
bd2a979e LL |
1078 | static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv, |
1079 | struct batadv_orig_node *orig, | |
1080 | u8 flags, | |
1081 | void *tvlv_value, | |
1082 | u16 tvlv_value_len) | |
60432d75 LL |
1083 | { |
1084 | bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND); | |
6b5e971a | 1085 | u8 mcast_flags = BATADV_NO_FLAGS; |
60432d75 LL |
1086 | bool orig_initialized; |
1087 | ||
8a4023c5 LL |
1088 | if (orig_mcast_enabled && tvlv_value && |
1089 | (tvlv_value_len >= sizeof(mcast_flags))) | |
6b5e971a | 1090 | mcast_flags = *(u8 *)tvlv_value; |
8a4023c5 LL |
1091 | |
1092 | spin_lock_bh(&orig->mcast_handler_lock); | |
9c936e3f LL |
1093 | orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST, |
1094 | &orig->capa_initialized); | |
60432d75 LL |
1095 | |
1096 | /* If mcast support is turned on decrease the disabled mcast node | |
1097 | * counter only if we had increased it for this node before. If this | |
1098 | * is a completely new orig_node no need to decrease the counter. | |
1099 | */ | |
1100 | if (orig_mcast_enabled && | |
9c936e3f | 1101 | !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) { |
60432d75 LL |
1102 | if (orig_initialized) |
1103 | atomic_dec(&bat_priv->mcast.num_disabled); | |
9c936e3f | 1104 | set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities); |
e8829f00 LL |
1105 | /* If mcast support is being switched off or if this is an initial |
1106 | * OGM without mcast support then increase the disabled mcast | |
1107 | * node counter. | |
60432d75 LL |
1108 | */ |
1109 | } else if (!orig_mcast_enabled && | |
9c936e3f | 1110 | (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) || |
e8829f00 | 1111 | !orig_initialized)) { |
60432d75 | 1112 | atomic_inc(&bat_priv->mcast.num_disabled); |
9c936e3f | 1113 | clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities); |
60432d75 LL |
1114 | } |
1115 | ||
9c936e3f | 1116 | set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized); |
60432d75 | 1117 | |
ab49886e | 1118 | batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags); |
4c8755d6 LL |
1119 | batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags); |
1120 | batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags); | |
ab49886e | 1121 | |
60432d75 | 1122 | orig->mcast_flags = mcast_flags; |
8a4023c5 | 1123 | spin_unlock_bh(&orig->mcast_handler_lock); |
60432d75 LL |
1124 | } |
1125 | ||
1126 | /** | |
1127 | * batadv_mcast_init - initialize the multicast optimizations structures | |
1128 | * @bat_priv: the bat priv with all the soft interface information | |
1129 | */ | |
1130 | void batadv_mcast_init(struct batadv_priv *bat_priv) | |
1131 | { | |
bd2a979e LL |
1132 | batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler, |
1133 | NULL, BATADV_TVLV_MCAST, 2, | |
60432d75 LL |
1134 | BATADV_TVLV_HANDLER_OGM_CIFNOTFND); |
1135 | } | |
1136 | ||
4e3e823b LL |
1137 | /** |
1138 | * batadv_mcast_flags_print_header - print own mcast flags to debugfs table | |
1139 | * @bat_priv: the bat priv with all the soft interface information | |
1140 | * @seq: debugfs table seq_file struct | |
1141 | * | |
1142 | * Prints our own multicast flags including a more specific reason why | |
1143 | * they are set, that is prints the bridge and querier state too, to | |
1144 | * the debugfs table specified via @seq. | |
1145 | */ | |
1146 | static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv, | |
1147 | struct seq_file *seq) | |
1148 | { | |
1149 | u8 flags = bat_priv->mcast.flags; | |
1150 | char querier4, querier6, shadowing4, shadowing6; | |
1151 | bool bridged = bat_priv->mcast.bridged; | |
1152 | ||
1153 | if (bridged) { | |
1154 | querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4'; | |
1155 | querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6'; | |
1156 | shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.'; | |
1157 | shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.'; | |
1158 | } else { | |
1159 | querier4 = '?'; | |
1160 | querier6 = '?'; | |
1161 | shadowing4 = '?'; | |
1162 | shadowing6 = '?'; | |
1163 | } | |
1164 | ||
1165 | seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n", | |
1166 | (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.', | |
1167 | (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.', | |
1168 | (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.'); | |
1169 | seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.'); | |
1170 | seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n", | |
1171 | querier4, querier6); | |
1172 | seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n", | |
1173 | shadowing4, shadowing6); | |
1174 | seq_puts(seq, "-------------------------------------------\n"); | |
1175 | seq_printf(seq, " %-10s %s\n", "Originator", "Flags"); | |
1176 | } | |
1177 | ||
1178 | /** | |
1179 | * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes | |
1180 | * @seq: seq file to print on | |
1181 | * @offset: not used | |
1182 | * | |
1183 | * This prints a table of (primary) originators and their according | |
1184 | * multicast flags, including (in the header) our own. | |
1185 | * | |
1186 | * Return: always 0 | |
1187 | */ | |
1188 | int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset) | |
1189 | { | |
1190 | struct net_device *net_dev = (struct net_device *)seq->private; | |
1191 | struct batadv_priv *bat_priv = netdev_priv(net_dev); | |
1192 | struct batadv_hard_iface *primary_if; | |
1193 | struct batadv_hashtable *hash = bat_priv->orig_hash; | |
1194 | struct batadv_orig_node *orig_node; | |
1195 | struct hlist_head *head; | |
1196 | u8 flags; | |
1197 | u32 i; | |
1198 | ||
1199 | primary_if = batadv_seq_print_text_primary_if_get(seq); | |
1200 | if (!primary_if) | |
1201 | return 0; | |
1202 | ||
1203 | batadv_mcast_flags_print_header(bat_priv, seq); | |
1204 | ||
1205 | for (i = 0; i < hash->size; i++) { | |
1206 | head = &hash->table[i]; | |
1207 | ||
1208 | rcu_read_lock(); | |
1209 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { | |
1210 | if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, | |
1211 | &orig_node->capa_initialized)) | |
1212 | continue; | |
1213 | ||
1214 | if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, | |
1215 | &orig_node->capabilities)) { | |
1216 | seq_printf(seq, "%pM -\n", orig_node->orig); | |
1217 | continue; | |
1218 | } | |
1219 | ||
1220 | flags = orig_node->mcast_flags; | |
1221 | ||
1222 | seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig, | |
1223 | (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) | |
1224 | ? 'U' : '.', | |
1225 | (flags & BATADV_MCAST_WANT_ALL_IPV4) | |
1226 | ? '4' : '.', | |
1227 | (flags & BATADV_MCAST_WANT_ALL_IPV6) | |
1228 | ? '6' : '.'); | |
1229 | } | |
1230 | rcu_read_unlock(); | |
1231 | } | |
1232 | ||
1233 | batadv_hardif_put(primary_if); | |
1234 | ||
1235 | return 0; | |
1236 | } | |
1237 | ||
c5caf4ef LL |
1238 | /** |
1239 | * batadv_mcast_free - free the multicast optimizations structures | |
1240 | * @bat_priv: the bat priv with all the soft interface information | |
1241 | */ | |
1242 | void batadv_mcast_free(struct batadv_priv *bat_priv) | |
1243 | { | |
bd2a979e LL |
1244 | batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2); |
1245 | batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2); | |
60432d75 | 1246 | |
af63cf51 | 1247 | spin_lock_bh(&bat_priv->tt.commit_lock); |
c5caf4ef | 1248 | batadv_mcast_mla_tt_retract(bat_priv, NULL); |
af63cf51 | 1249 | spin_unlock_bh(&bat_priv->tt.commit_lock); |
c5caf4ef | 1250 | } |
60432d75 LL |
1251 | |
1252 | /** | |
1253 | * batadv_mcast_purge_orig - reset originator global mcast state modifications | |
1254 | * @orig: the originator which is going to get purged | |
1255 | */ | |
1256 | void batadv_mcast_purge_orig(struct batadv_orig_node *orig) | |
1257 | { | |
1258 | struct batadv_priv *bat_priv = orig->bat_priv; | |
1259 | ||
8a4023c5 LL |
1260 | spin_lock_bh(&orig->mcast_handler_lock); |
1261 | ||
9c936e3f LL |
1262 | if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) && |
1263 | test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized)) | |
60432d75 | 1264 | atomic_dec(&bat_priv->mcast.num_disabled); |
ab49886e LL |
1265 | |
1266 | batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS); | |
4c8755d6 LL |
1267 | batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS); |
1268 | batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS); | |
8a4023c5 LL |
1269 | |
1270 | spin_unlock_bh(&orig->mcast_handler_lock); | |
60432d75 | 1271 | } |