blob: 1d1627fe0de013c1c80340d101b0c3b5b22b08d5 [file] [log] [blame]
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001/* Copyright (C) 2014 B.A.T.M.A.N. contributors:
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
18#include "main.h"
19#include "multicast.h"
20#include "originator.h"
21#include "hard-interface.h"
22#include "translation-table.h"
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +010023#include "multicast.h"
Linus Lüssingc5caf4e2014-02-15 17:47:49 +010024
25/**
26 * batadv_mcast_mla_softif_get - get softif multicast listeners
27 * @dev: the device to collect multicast addresses from
28 * @mcast_list: a list to put found addresses into
29 *
30 * Collect multicast addresses of the local multicast listeners
31 * on the given soft interface, dev, in the given mcast_list.
32 *
33 * Returns -ENOMEM on memory allocation error or the number of
34 * items added to the mcast_list otherwise.
35 */
36static int batadv_mcast_mla_softif_get(struct net_device *dev,
37 struct hlist_head *mcast_list)
38{
39 struct netdev_hw_addr *mc_list_entry;
40 struct batadv_hw_addr *new;
41 int ret = 0;
42
43 netif_addr_lock_bh(dev);
44 netdev_for_each_mc_addr(mc_list_entry, dev) {
45 new = kmalloc(sizeof(*new), GFP_ATOMIC);
46 if (!new) {
47 ret = -ENOMEM;
48 break;
49 }
50
51 ether_addr_copy(new->addr, mc_list_entry->addr);
52 hlist_add_head(&new->list, mcast_list);
53 ret++;
54 }
55 netif_addr_unlock_bh(dev);
56
57 return ret;
58}
59
60/**
61 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
62 * @mcast_addr: the multicast address to check
63 * @mcast_list: the list with multicast addresses to search in
64 *
65 * Returns true if the given address is already in the given list.
66 * Otherwise returns false.
67 */
68static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
69 struct hlist_head *mcast_list)
70{
71 struct batadv_hw_addr *mcast_entry;
72
73 hlist_for_each_entry(mcast_entry, mcast_list, list)
74 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
75 return true;
76
77 return false;
78}
79
80/**
81 * batadv_mcast_mla_list_free - free a list of multicast addresses
82 * @mcast_list: the list to free
83 *
84 * Removes and frees all items in the given mcast_list.
85 */
86static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
87{
88 struct batadv_hw_addr *mcast_entry;
89 struct hlist_node *tmp;
90
91 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
92 hlist_del(&mcast_entry->list);
93 kfree(mcast_entry);
94 }
95}
96
97/**
98 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
99 * @bat_priv: the bat priv with all the soft interface information
100 * @mcast_list: a list of addresses which should _not_ be removed
101 *
102 * Retracts the announcement of any multicast listener from the
103 * translation table except the ones listed in the given mcast_list.
104 *
105 * If mcast_list is NULL then all are retracted.
106 */
107static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
108 struct hlist_head *mcast_list)
109{
110 struct batadv_hw_addr *mcast_entry;
111 struct hlist_node *tmp;
112
113 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
114 list) {
115 if (mcast_list &&
116 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
117 mcast_list))
118 continue;
119
120 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
121 BATADV_NO_FLAGS,
122 "mcast TT outdated", false);
123
124 hlist_del(&mcast_entry->list);
125 kfree(mcast_entry);
126 }
127}
128
129/**
130 * batadv_mcast_mla_tt_add - add multicast listener announcements
131 * @bat_priv: the bat priv with all the soft interface information
132 * @mcast_list: a list of addresses which are going to get added
133 *
134 * Adds multicast listener announcements from the given mcast_list to the
135 * translation table if they have not been added yet.
136 */
137static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
138 struct hlist_head *mcast_list)
139{
140 struct batadv_hw_addr *mcast_entry;
141 struct hlist_node *tmp;
142
143 if (!mcast_list)
144 return;
145
146 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
147 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
148 &bat_priv->mcast.mla_list))
149 continue;
150
151 if (!batadv_tt_local_add(bat_priv->soft_iface,
152 mcast_entry->addr, BATADV_NO_FLAGS,
153 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
154 continue;
155
156 hlist_del(&mcast_entry->list);
157 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
158 }
159}
160
161/**
162 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
163 * @bat_priv: the bat priv with all the soft interface information
164 *
165 * Checks whether there is a bridge on top of our soft interface. Returns
166 * true if so, false otherwise.
167 */
168static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
169{
170 struct net_device *upper = bat_priv->soft_iface;
171
172 rcu_read_lock();
173 do {
174 upper = netdev_master_upper_dev_get_rcu(upper);
175 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
176 rcu_read_unlock();
177
178 return upper;
179}
180
181/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100182 * batadv_mcast_mla_tvlv_update - update multicast tvlv
183 * @bat_priv: the bat priv with all the soft interface information
184 *
185 * Updates the own multicast tvlv with our current multicast related settings,
186 * capabilities and inabilities.
187 *
188 * Returns true if the tvlv container is registered afterwards. Otherwise
189 * returns false.
190 */
191static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
192{
193 struct batadv_tvlv_mcast_data mcast_data;
194
195 mcast_data.flags = BATADV_NO_FLAGS;
196 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
197
198 /* Avoid attaching MLAs, if there is a bridge on top of our soft
199 * interface, we don't support that yet (TODO)
200 */
201 if (batadv_mcast_has_bridge(bat_priv)) {
202 if (bat_priv->mcast.enabled) {
203 batadv_tvlv_container_unregister(bat_priv,
204 BATADV_TVLV_MCAST, 1);
205 bat_priv->mcast.enabled = false;
206 }
207
208 return false;
209 }
210
211 if (!bat_priv->mcast.enabled ||
212 mcast_data.flags != bat_priv->mcast.flags) {
213 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
214 &mcast_data, sizeof(mcast_data));
215 bat_priv->mcast.flags = mcast_data.flags;
216 bat_priv->mcast.enabled = true;
217 }
218
219 return true;
220}
221
222/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100223 * batadv_mcast_mla_update - update the own MLAs
224 * @bat_priv: the bat priv with all the soft interface information
225 *
Linus Lüssing60432d72014-02-15 17:47:51 +0100226 * Updates the own multicast listener announcements in the translation
227 * table as well as the own, announced multicast tvlv container.
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100228 */
229void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
230{
231 struct net_device *soft_iface = bat_priv->soft_iface;
232 struct hlist_head mcast_list = HLIST_HEAD_INIT;
233 int ret;
234
Linus Lüssing60432d72014-02-15 17:47:51 +0100235 if (!batadv_mcast_mla_tvlv_update(bat_priv))
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100236 goto update;
237
238 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
239 if (ret < 0)
240 goto out;
241
242update:
243 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
244 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
245
246out:
247 batadv_mcast_mla_list_free(&mcast_list);
248}
249
250/**
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100251 * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
252 * @bat_priv: the bat priv with all the soft interface information
253 * @skb: the IPv6 packet to check
254 *
255 * Checks whether the given IPv6 packet has the potential to be forwarded with a
256 * mode more optimal than classic flooding.
257 *
258 * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
259 * of memory.
260 */
261static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
262 struct sk_buff *skb)
263{
264 struct ipv6hdr *ip6hdr;
265
266 /* We might fail due to out-of-memory -> drop it */
267 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
268 return -ENOMEM;
269
270 ip6hdr = ipv6_hdr(skb);
271
272 /* TODO: Implement Multicast Router Discovery (RFC4286),
273 * then allow scope > link local, too
274 */
275 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
276 return -EINVAL;
277
278 /* link-local-all-nodes multicast listeners behind a bridge are
279 * not snoopable (see RFC4541, section 3, paragraph 3)
280 */
281 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
282 return -EINVAL;
283
284 return 0;
285}
286
287/**
288 * batadv_mcast_forw_mode_check - check for optimized forwarding potential
289 * @bat_priv: the bat priv with all the soft interface information
290 * @skb: the multicast frame to check
291 *
292 * Checks whether the given multicast ethernet frame has the potential to be
293 * forwarded with a mode more optimal than classic flooding.
294 *
295 * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
296 * of memory.
297 */
298static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
299 struct sk_buff *skb)
300{
301 struct ethhdr *ethhdr = eth_hdr(skb);
302
303 if (!atomic_read(&bat_priv->multicast_mode))
304 return -EINVAL;
305
306 if (atomic_read(&bat_priv->mcast.num_disabled))
307 return -EINVAL;
308
309 switch (ntohs(ethhdr->h_proto)) {
310 case ETH_P_IPV6:
311 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb);
312 default:
313 return -EINVAL;
314 }
315}
316
317/**
318 * batadv_mcast_forw_tt_node_get - get a multicast tt node
319 * @bat_priv: the bat priv with all the soft interface information
320 * @ethhdr: the ether header containing the multicast destination
321 *
322 * Returns an orig_node matching the multicast address provided by ethhdr
323 * via a translation table lookup. This increases the returned nodes refcount.
324 */
325static struct batadv_orig_node *
326batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
327 struct ethhdr *ethhdr)
328{
329 return batadv_transtable_search(bat_priv, ethhdr->h_source,
330 ethhdr->h_dest, BATADV_NO_FLAGS);
331}
332
333/**
334 * batadv_mcast_forw_mode - check on how to forward a multicast packet
335 * @bat_priv: the bat priv with all the soft interface information
336 * @skb: The multicast packet to check
337 * @orig: an originator to be set to forward the skb to
338 *
339 * Returns the forwarding mode as enum batadv_forw_mode and in case of
340 * BATADV_FORW_SINGLE set the orig to the single originator the skb
341 * should be forwarded to.
342 */
343enum batadv_forw_mode
344batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
345 struct batadv_orig_node **orig)
346{
347 struct ethhdr *ethhdr;
348 int ret, tt_count;
349
350 ret = batadv_mcast_forw_mode_check(bat_priv, skb);
351 if (ret == -ENOMEM)
352 return BATADV_FORW_NONE;
353 else if (ret < 0)
354 return BATADV_FORW_ALL;
355
356 ethhdr = eth_hdr(skb);
357
358 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
359 BATADV_NO_FLAGS);
360
361 switch (tt_count) {
362 case 1:
363 *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
364 if (*orig)
365 return BATADV_FORW_SINGLE;
366
367 /* fall through */
368 case 0:
369 return BATADV_FORW_NONE;
370 default:
371 return BATADV_FORW_ALL;
372 }
373}
374
375/**
Linus Lüssing60432d72014-02-15 17:47:51 +0100376 * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
377 * @bat_priv: the bat priv with all the soft interface information
378 * @orig: the orig_node of the ogm
379 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
380 * @tvlv_value: tvlv buffer containing the multicast data
381 * @tvlv_value_len: tvlv buffer length
382 */
383static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
384 struct batadv_orig_node *orig,
385 uint8_t flags,
386 void *tvlv_value,
387 uint16_t tvlv_value_len)
388{
389 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
390 uint8_t mcast_flags = BATADV_NO_FLAGS;
391 bool orig_initialized;
392
393 orig_initialized = orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST;
394
395 /* If mcast support is turned on decrease the disabled mcast node
396 * counter only if we had increased it for this node before. If this
397 * is a completely new orig_node no need to decrease the counter.
398 */
399 if (orig_mcast_enabled &&
400 !(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST)) {
401 if (orig_initialized)
402 atomic_dec(&bat_priv->mcast.num_disabled);
403 orig->capabilities |= BATADV_ORIG_CAPA_HAS_MCAST;
404 /* If mcast support is being switched off increase the disabled
405 * mcast node counter.
406 */
407 } else if (!orig_mcast_enabled &&
408 orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST) {
409 atomic_inc(&bat_priv->mcast.num_disabled);
410 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_MCAST;
411 }
412
413 orig->capa_initialized |= BATADV_ORIG_CAPA_HAS_MCAST;
414
415 if (orig_mcast_enabled && tvlv_value &&
416 (tvlv_value_len >= sizeof(mcast_flags)))
417 mcast_flags = *(uint8_t *)tvlv_value;
418
419 orig->mcast_flags = mcast_flags;
420}
421
422/**
423 * batadv_mcast_init - initialize the multicast optimizations structures
424 * @bat_priv: the bat priv with all the soft interface information
425 */
426void batadv_mcast_init(struct batadv_priv *bat_priv)
427{
428 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
429 NULL, BATADV_TVLV_MCAST, 1,
430 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
431}
432
433/**
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100434 * batadv_mcast_free - free the multicast optimizations structures
435 * @bat_priv: the bat priv with all the soft interface information
436 */
437void batadv_mcast_free(struct batadv_priv *bat_priv)
438{
Linus Lüssing60432d72014-02-15 17:47:51 +0100439 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
440 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
441
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100442 batadv_mcast_mla_tt_retract(bat_priv, NULL);
443}
Linus Lüssing60432d72014-02-15 17:47:51 +0100444
445/**
446 * batadv_mcast_purge_orig - reset originator global mcast state modifications
447 * @orig: the originator which is going to get purged
448 */
449void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
450{
451 struct batadv_priv *bat_priv = orig->bat_priv;
452
453 if (!(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST))
454 atomic_dec(&bat_priv->mcast.num_disabled);
455}