1 /* Copyright (C) 2014 B.A.T.M.A.N. contributors:
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.
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.
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/>.
19 #include "multicast.h"
20 #include "originator.h"
21 #include "hard-interface.h"
22 #include "translation-table.h"
25 * batadv_mcast_mla_softif_get - get softif multicast listeners
26 * @dev: the device to collect multicast addresses from
27 * @mcast_list: a list to put found addresses into
29 * Collect multicast addresses of the local multicast listeners
30 * on the given soft interface, dev, in the given mcast_list.
32 * Returns -ENOMEM on memory allocation error or the number of
33 * items added to the mcast_list otherwise.
35 static int batadv_mcast_mla_softif_get(struct net_device *dev,
36 struct hlist_head *mcast_list)
38 struct netdev_hw_addr *mc_list_entry;
39 struct batadv_hw_addr *new;
42 netif_addr_lock_bh(dev);
43 netdev_for_each_mc_addr(mc_list_entry, dev) {
44 new = kmalloc(sizeof(*new), GFP_ATOMIC);
50 ether_addr_copy(new->addr, mc_list_entry->addr);
51 hlist_add_head(&new->list, mcast_list);
54 netif_addr_unlock_bh(dev);
60 * batadv_mcast_mla_is_duplicate - check whether an address is in a list
61 * @mcast_addr: the multicast address to check
62 * @mcast_list: the list with multicast addresses to search in
64 * Returns true if the given address is already in the given list.
65 * Otherwise returns false.
67 static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
68 struct hlist_head *mcast_list)
70 struct batadv_hw_addr *mcast_entry;
72 hlist_for_each_entry(mcast_entry, mcast_list, list)
73 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
80 * batadv_mcast_mla_list_free - free a list of multicast addresses
81 * @mcast_list: the list to free
83 * Removes and frees all items in the given mcast_list.
85 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
87 struct batadv_hw_addr *mcast_entry;
88 struct hlist_node *tmp;
90 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
91 hlist_del(&mcast_entry->list);
97 * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
98 * @bat_priv: the bat priv with all the soft interface information
99 * @mcast_list: a list of addresses which should _not_ be removed
101 * Retracts the announcement of any multicast listener from the
102 * translation table except the ones listed in the given mcast_list.
104 * If mcast_list is NULL then all are retracted.
106 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
107 struct hlist_head *mcast_list)
109 struct batadv_hw_addr *mcast_entry;
110 struct hlist_node *tmp;
112 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
115 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
119 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
121 "mcast TT outdated", false);
123 hlist_del(&mcast_entry->list);
129 * batadv_mcast_mla_tt_add - add multicast listener announcements
130 * @bat_priv: the bat priv with all the soft interface information
131 * @mcast_list: a list of addresses which are going to get added
133 * Adds multicast listener announcements from the given mcast_list to the
134 * translation table if they have not been added yet.
136 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
137 struct hlist_head *mcast_list)
139 struct batadv_hw_addr *mcast_entry;
140 struct hlist_node *tmp;
145 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
146 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
147 &bat_priv->mcast.mla_list))
150 if (!batadv_tt_local_add(bat_priv->soft_iface,
151 mcast_entry->addr, BATADV_NO_FLAGS,
152 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
155 hlist_del(&mcast_entry->list);
156 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
161 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
162 * @bat_priv: the bat priv with all the soft interface information
164 * Checks whether there is a bridge on top of our soft interface. Returns
165 * true if so, false otherwise.
167 static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
169 struct net_device *upper = bat_priv->soft_iface;
173 upper = netdev_master_upper_dev_get_rcu(upper);
174 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
181 * batadv_mcast_mla_tvlv_update - update multicast tvlv
182 * @bat_priv: the bat priv with all the soft interface information
184 * Updates the own multicast tvlv with our current multicast related settings,
185 * capabilities and inabilities.
187 * Returns true if the tvlv container is registered afterwards. Otherwise
190 static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
192 struct batadv_tvlv_mcast_data mcast_data;
194 mcast_data.flags = BATADV_NO_FLAGS;
195 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
197 /* Avoid attaching MLAs, if there is a bridge on top of our soft
198 * interface, we don't support that yet (TODO)
200 if (batadv_mcast_has_bridge(bat_priv)) {
201 if (bat_priv->mcast.enabled) {
202 batadv_tvlv_container_unregister(bat_priv,
203 BATADV_TVLV_MCAST, 1);
204 bat_priv->mcast.enabled = false;
210 if (!bat_priv->mcast.enabled ||
211 mcast_data.flags != bat_priv->mcast.flags) {
212 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
213 &mcast_data, sizeof(mcast_data));
214 bat_priv->mcast.flags = mcast_data.flags;
215 bat_priv->mcast.enabled = true;
222 * batadv_mcast_mla_update - update the own MLAs
223 * @bat_priv: the bat priv with all the soft interface information
225 * Updates the own multicast listener announcements in the translation
226 * table as well as the own, announced multicast tvlv container.
228 void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
230 struct net_device *soft_iface = bat_priv->soft_iface;
231 struct hlist_head mcast_list = HLIST_HEAD_INIT;
234 if (!batadv_mcast_mla_tvlv_update(bat_priv))
237 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
242 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
243 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
246 batadv_mcast_mla_list_free(&mcast_list);
250 * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
251 * @bat_priv: the bat priv with all the soft interface information
252 * @orig: the orig_node of the ogm
253 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
254 * @tvlv_value: tvlv buffer containing the multicast data
255 * @tvlv_value_len: tvlv buffer length
257 static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
258 struct batadv_orig_node *orig,
261 uint16_t tvlv_value_len)
263 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
264 uint8_t mcast_flags = BATADV_NO_FLAGS;
265 bool orig_initialized;
267 orig_initialized = orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST;
269 /* If mcast support is turned on decrease the disabled mcast node
270 * counter only if we had increased it for this node before. If this
271 * is a completely new orig_node no need to decrease the counter.
273 if (orig_mcast_enabled &&
274 !(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST)) {
275 if (orig_initialized)
276 atomic_dec(&bat_priv->mcast.num_disabled);
277 orig->capabilities |= BATADV_ORIG_CAPA_HAS_MCAST;
278 /* If mcast support is being switched off increase the disabled
279 * mcast node counter.
281 } else if (!orig_mcast_enabled &&
282 orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST) {
283 atomic_inc(&bat_priv->mcast.num_disabled);
284 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_MCAST;
287 orig->capa_initialized |= BATADV_ORIG_CAPA_HAS_MCAST;
289 if (orig_mcast_enabled && tvlv_value &&
290 (tvlv_value_len >= sizeof(mcast_flags)))
291 mcast_flags = *(uint8_t *)tvlv_value;
293 orig->mcast_flags = mcast_flags;
297 * batadv_mcast_init - initialize the multicast optimizations structures
298 * @bat_priv: the bat priv with all the soft interface information
300 void batadv_mcast_init(struct batadv_priv *bat_priv)
302 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
303 NULL, BATADV_TVLV_MCAST, 1,
304 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
308 * batadv_mcast_free - free the multicast optimizations structures
309 * @bat_priv: the bat priv with all the soft interface information
311 void batadv_mcast_free(struct batadv_priv *bat_priv)
313 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
314 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
316 batadv_mcast_mla_tt_retract(bat_priv, NULL);
320 * batadv_mcast_purge_orig - reset originator global mcast state modifications
321 * @orig: the originator which is going to get purged
323 void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
325 struct batadv_priv *bat_priv = orig->bat_priv;
327 if (!(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST))
328 atomic_dec(&bat_priv->mcast.num_disabled);