blob: e099fd67403c203314e1363eec0fcdfe07e7720f [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"
23
24/**
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
28 *
29 * Collect multicast addresses of the local multicast listeners
30 * on the given soft interface, dev, in the given mcast_list.
31 *
32 * Returns -ENOMEM on memory allocation error or the number of
33 * items added to the mcast_list otherwise.
34 */
35static int batadv_mcast_mla_softif_get(struct net_device *dev,
36 struct hlist_head *mcast_list)
37{
38 struct netdev_hw_addr *mc_list_entry;
39 struct batadv_hw_addr *new;
40 int ret = 0;
41
42 netif_addr_lock_bh(dev);
43 netdev_for_each_mc_addr(mc_list_entry, dev) {
44 new = kmalloc(sizeof(*new), GFP_ATOMIC);
45 if (!new) {
46 ret = -ENOMEM;
47 break;
48 }
49
50 ether_addr_copy(new->addr, mc_list_entry->addr);
51 hlist_add_head(&new->list, mcast_list);
52 ret++;
53 }
54 netif_addr_unlock_bh(dev);
55
56 return ret;
57}
58
59/**
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
63 *
64 * Returns true if the given address is already in the given list.
65 * Otherwise returns false.
66 */
67static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
68 struct hlist_head *mcast_list)
69{
70 struct batadv_hw_addr *mcast_entry;
71
72 hlist_for_each_entry(mcast_entry, mcast_list, list)
73 if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
74 return true;
75
76 return false;
77}
78
79/**
80 * batadv_mcast_mla_list_free - free a list of multicast addresses
81 * @mcast_list: the list to free
82 *
83 * Removes and frees all items in the given mcast_list.
84 */
85static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
86{
87 struct batadv_hw_addr *mcast_entry;
88 struct hlist_node *tmp;
89
90 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
91 hlist_del(&mcast_entry->list);
92 kfree(mcast_entry);
93 }
94}
95
96/**
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
100 *
101 * Retracts the announcement of any multicast listener from the
102 * translation table except the ones listed in the given mcast_list.
103 *
104 * If mcast_list is NULL then all are retracted.
105 */
106static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
107 struct hlist_head *mcast_list)
108{
109 struct batadv_hw_addr *mcast_entry;
110 struct hlist_node *tmp;
111
112 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
113 list) {
114 if (mcast_list &&
115 batadv_mcast_mla_is_duplicate(mcast_entry->addr,
116 mcast_list))
117 continue;
118
119 batadv_tt_local_remove(bat_priv, mcast_entry->addr,
120 BATADV_NO_FLAGS,
121 "mcast TT outdated", false);
122
123 hlist_del(&mcast_entry->list);
124 kfree(mcast_entry);
125 }
126}
127
128/**
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
132 *
133 * Adds multicast listener announcements from the given mcast_list to the
134 * translation table if they have not been added yet.
135 */
136static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
137 struct hlist_head *mcast_list)
138{
139 struct batadv_hw_addr *mcast_entry;
140 struct hlist_node *tmp;
141
142 if (!mcast_list)
143 return;
144
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))
148 continue;
149
150 if (!batadv_tt_local_add(bat_priv->soft_iface,
151 mcast_entry->addr, BATADV_NO_FLAGS,
152 BATADV_NULL_IFINDEX, BATADV_NO_MARK))
153 continue;
154
155 hlist_del(&mcast_entry->list);
156 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
157 }
158}
159
160/**
161 * batadv_mcast_has_bridge - check whether the soft-iface is bridged
162 * @bat_priv: the bat priv with all the soft interface information
163 *
164 * Checks whether there is a bridge on top of our soft interface. Returns
165 * true if so, false otherwise.
166 */
167static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
168{
169 struct net_device *upper = bat_priv->soft_iface;
170
171 rcu_read_lock();
172 do {
173 upper = netdev_master_upper_dev_get_rcu(upper);
174 } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
175 rcu_read_unlock();
176
177 return upper;
178}
179
180/**
181 * batadv_mcast_mla_update - update the own MLAs
182 * @bat_priv: the bat priv with all the soft interface information
183 *
184 * Update the own multicast listener announcements in the translation
185 * table.
186 */
187void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
188{
189 struct net_device *soft_iface = bat_priv->soft_iface;
190 struct hlist_head mcast_list = HLIST_HEAD_INIT;
191 int ret;
192
193 /* Avoid attaching MLAs, if there is a bridge on top of our soft
194 * interface, we don't support that yet (TODO)
195 */
196 if (batadv_mcast_has_bridge(bat_priv))
197 goto update;
198
199 ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
200 if (ret < 0)
201 goto out;
202
203update:
204 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
205 batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
206
207out:
208 batadv_mcast_mla_list_free(&mcast_list);
209}
210
211/**
212 * batadv_mcast_free - free the multicast optimizations structures
213 * @bat_priv: the bat priv with all the soft interface information
214 */
215void batadv_mcast_free(struct batadv_priv *bat_priv)
216{
217 batadv_mcast_mla_tt_retract(bat_priv, NULL);
218}