blob: 68494a10848ccc93ff5d089a40dfae4f9793e2e1 [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
Antonio Quartulli35c133a2012-03-14 13:03:01 +01003 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00004 *
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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "translation-table.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020021#include <linux/atomic.h>
Linus Lüssingac4eebd2015-06-16 17:10:24 +020022#include <linux/bitops.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/bug.h>
24#include <linux/byteorder/generic.h>
25#include <linux/compiler.h>
Antonio Quartulliced72932013-04-24 16:37:51 +020026#include <linux/crc32c.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/errno.h>
28#include <linux/etherdevice.h>
29#include <linux/fs.h>
30#include <linux/if_ether.h>
31#include <linux/jhash.h>
32#include <linux/jiffies.h>
33#include <linux/kernel.h>
Sven Eckelmann6e8ef692016-01-16 10:29:50 +010034#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/list.h>
36#include <linux/lockdep.h>
37#include <linux/netdevice.h>
38#include <linux/rculist.h>
39#include <linux/rcupdate.h>
40#include <linux/seq_file.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/stddef.h>
44#include <linux/string.h>
45#include <linux/workqueue.h>
46#include <net/net_namespace.h>
47
48#include "bridge_loop_avoidance.h"
49#include "hard-interface.h"
50#include "hash.h"
51#include "multicast.h"
52#include "originator.h"
53#include "packet.h"
54#include "soft-interface.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020055
Antonio Quartullidec05072012-11-10 11:00:32 +010056/* hash class keys */
57static struct lock_class_key batadv_tt_local_hash_lock_class_key;
58static struct lock_class_key batadv_tt_global_hash_lock_class_key;
59
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020060static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020061 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020062 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020063static void batadv_tt_purge(struct work_struct *work);
64static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020065batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020066static void batadv_tt_global_del(struct batadv_priv *bat_priv,
67 struct batadv_orig_node *orig_node,
68 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020069 unsigned short vid, const char *message,
70 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000071
Sven Eckelmann62fe7102015-09-15 19:00:48 +020072/**
Antonio Quartullid15cd622015-11-17 16:40:52 +080073 * batadv_compare_tt - check if two TT entries are the same
74 * @node: the list element pointer of the first TT entry
75 * @data2: pointer to the tt_common_entry of the second TT entry
Sven Eckelmann62fe7102015-09-15 19:00:48 +020076 *
Antonio Quartullid15cd622015-11-17 16:40:52 +080077 * Compare the MAC address and the VLAN ID of the two TT entries and check if
78 * they are the same TT client.
79 * Return: 1 if the two TT clients are the same, 0 otherwise
Sven Eckelmann62fe7102015-09-15 19:00:48 +020080 */
Sven Eckelmanna5130882012-05-16 20:23:16 +020081static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000082{
Sven Eckelmann56303d32012-06-05 22:31:31 +020083 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020084 hash_entry);
Marek Lindner4c718952015-08-06 10:38:54 +020085 const struct batadv_tt_common_entry *tt1 = data1;
86 const struct batadv_tt_common_entry *tt2 = data2;
Marek Lindner7aadf882011-02-18 12:28:09 +000087
Marek Lindner4c718952015-08-06 10:38:54 +020088 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000089}
90
Antonio Quartullic018ad32013-06-04 12:11:39 +020091/**
92 * batadv_choose_tt - return the index of the tt entry in the hash table
93 * @data: pointer to the tt_common_entry object to map
94 * @size: the size of the hash table
95 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020096 * Return: the hash index where the object represented by 'data' should be
Antonio Quartullic018ad32013-06-04 12:11:39 +020097 * stored at.
98 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020099static inline u32 batadv_choose_tt(const void *data, u32 size)
Antonio Quartullic018ad32013-06-04 12:11:39 +0200100{
101 struct batadv_tt_common_entry *tt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200102 u32 hash = 0;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200103
104 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100105 hash = jhash(&tt->addr, ETH_ALEN, hash);
106 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200107
108 return hash % size;
109}
110
111/**
112 * batadv_tt_hash_find - look for a client in the given hash table
113 * @hash: the hash table to search
114 * @addr: the mac address of the client to look for
115 * @vid: VLAN identifier
116 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200117 * Return: a pointer to the tt_common struct belonging to the searched client if
Antonio Quartullic018ad32013-06-04 12:11:39 +0200118 * found, NULL otherwise.
119 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200120static struct batadv_tt_common_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200121batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200122 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000123{
Marek Lindner7aadf882011-02-18 12:28:09 +0000124 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200125 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200126 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000127
128 if (!hash)
129 return NULL;
130
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100131 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200132 to_search.vid = vid;
133
134 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000135 head = &hash->table[index];
136
137 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200138 hlist_for_each_entry_rcu(tt, head, hash_entry) {
139 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000140 continue;
141
Antonio Quartullic018ad32013-06-04 12:11:39 +0200142 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200143 continue;
144
Antonio Quartullic018ad32013-06-04 12:11:39 +0200145 if (!atomic_inc_not_zero(&tt->refcount))
146 continue;
147
148 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000149 break;
150 }
151 rcu_read_unlock();
152
Antonio Quartullic018ad32013-06-04 12:11:39 +0200153 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100154}
155
Antonio Quartullic018ad32013-06-04 12:11:39 +0200156/**
157 * batadv_tt_local_hash_find - search the local table for a given client
158 * @bat_priv: the bat priv with all the soft interface information
159 * @addr: the mac address of the client to look for
160 * @vid: VLAN identifier
161 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200162 * Return: a pointer to the corresponding tt_local_entry struct if the client is
Antonio Quartullic018ad32013-06-04 12:11:39 +0200163 * found, NULL otherwise.
164 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165static struct batadv_tt_local_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200166batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200167 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_tt_common_entry *tt_common_entry;
170 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100171
Antonio Quartullic018ad32013-06-04 12:11:39 +0200172 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
173 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100174 if (tt_common_entry)
175 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200176 struct batadv_tt_local_entry,
177 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100178 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000179}
180
Antonio Quartullic018ad32013-06-04 12:11:39 +0200181/**
182 * batadv_tt_global_hash_find - search the global table for a given client
183 * @bat_priv: the bat priv with all the soft interface information
184 * @addr: the mac address of the client to look for
185 * @vid: VLAN identifier
186 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200187 * Return: a pointer to the corresponding tt_global_entry struct if the client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200188 * is found, NULL otherwise.
189 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200190static struct batadv_tt_global_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200191batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200192 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000193{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200194 struct batadv_tt_common_entry *tt_common_entry;
195 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000196
Antonio Quartullic018ad32013-06-04 12:11:39 +0200197 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
198 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100199 if (tt_common_entry)
200 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200201 struct batadv_tt_global_entry,
202 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100203 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000204}
205
Sven Eckelmanna5130882012-05-16 20:23:16 +0200206static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200207batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200208{
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100209 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
210 kfree_rcu(tt_local_entry, common.rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200211}
212
Antonio Quartulli21026052013-05-07 00:29:22 +0200213/**
214 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
215 * tt_global_entry and possibly free it
216 * @tt_global_entry: the object to free
217 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200218static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200219batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200220{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200221 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
Sven Eckelmanna5130882012-05-16 20:23:16 +0200222 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli21026052013-05-07 00:29:22 +0200223 kfree_rcu(tt_global_entry, common.rcu);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200224 }
225}
226
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100227/**
228 * batadv_tt_global_hash_count - count the number of orig entries
Antonio Quartullid15cd622015-11-17 16:40:52 +0800229 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100230 * @addr: the mac address of the client to count entries for
231 * @vid: VLAN identifier
232 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200233 * Return: the number of originators advertising the given address/data
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100234 * (excluding ourself).
235 */
236int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200237 const u8 *addr, unsigned short vid)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100238{
239 struct batadv_tt_global_entry *tt_global_entry;
240 int count;
241
242 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
243 if (!tt_global_entry)
244 return 0;
245
246 count = atomic_read(&tt_global_entry->orig_list_count);
247 batadv_tt_global_entry_free_ref(tt_global_entry);
248
249 return count;
250}
251
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200252/**
253 * batadv_tt_local_size_mod - change the size by v of the local table identified
254 * by vid
255 * @bat_priv: the bat priv with all the soft interface information
256 * @vid: the VLAN identifier of the sub-table to change
257 * @v: the amount to sum to the local table size
258 */
259static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
260 unsigned short vid, int v)
261{
262 struct batadv_softif_vlan *vlan;
263
264 vlan = batadv_softif_vlan_get(bat_priv, vid);
265 if (!vlan)
266 return;
267
268 atomic_add(v, &vlan->tt.num_entries);
269
270 batadv_softif_vlan_free_ref(vlan);
271}
272
273/**
274 * batadv_tt_local_size_inc - increase by one the local table size for the given
275 * vid
276 * @bat_priv: the bat priv with all the soft interface information
277 * @vid: the VLAN identifier
278 */
279static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
280 unsigned short vid)
281{
282 batadv_tt_local_size_mod(bat_priv, vid, 1);
283}
284
285/**
286 * batadv_tt_local_size_dec - decrease by one the local table size for the given
287 * vid
288 * @bat_priv: the bat priv with all the soft interface information
289 * @vid: the VLAN identifier
290 */
291static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
292 unsigned short vid)
293{
294 batadv_tt_local_size_mod(bat_priv, vid, -1);
295}
296
297/**
Antonio Quartullid15cd622015-11-17 16:40:52 +0800298 * batadv_tt_global_size_mod - change the size by v of the global table
299 * for orig_node identified by vid
300 * @orig_node: the originator for which the table has to be modified
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200301 * @vid: the VLAN identifier
302 * @v: the amount to sum to the global table size
303 */
304static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
305 unsigned short vid, int v)
306{
307 struct batadv_orig_node_vlan *vlan;
308
309 vlan = batadv_orig_node_vlan_new(orig_node, vid);
310 if (!vlan)
311 return;
312
313 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
314 spin_lock_bh(&orig_node->vlan_list_lock);
Marek Lindnera1210482015-06-22 00:30:23 +0800315 hlist_del_init_rcu(&vlan->list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200316 spin_unlock_bh(&orig_node->vlan_list_lock);
317 batadv_orig_node_vlan_free_ref(vlan);
318 }
319
320 batadv_orig_node_vlan_free_ref(vlan);
321}
322
323/**
324 * batadv_tt_global_size_inc - increase by one the global table size for the
325 * given vid
326 * @orig_node: the originator which global table size has to be decreased
327 * @vid: the vlan identifier
328 */
329static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
330 unsigned short vid)
331{
332 batadv_tt_global_size_mod(orig_node, vid, 1);
333}
334
335/**
336 * batadv_tt_global_size_dec - decrease by one the global table size for the
337 * given vid
338 * @orig_node: the originator which global table size has to be decreased
339 * @vid: the vlan identifier
340 */
341static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
342 unsigned short vid)
343{
344 batadv_tt_global_size_mod(orig_node, vid, -1);
345}
346
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100347/**
348 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
349 * queue for free after rcu grace period
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100350 * @ref: kref pointer of the tt orig entry
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100351 */
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100352static void batadv_tt_orig_list_entry_release(struct kref *ref)
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100353{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100354 struct batadv_tt_orig_list_entry *orig_entry;
355
356 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
357 refcount);
358
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100359 batadv_orig_node_free_ref(orig_entry->orig_node);
360 kfree_rcu(orig_entry, rcu);
361}
362
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100363/**
364 * batadv_tt_orig_list_entry_free_ref - decrement the tt orig entry refcounter
365 * and possibly release it
366 * @orig_entry: tt orig entry to be free'd
367 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200368static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200369batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200370{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100371 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200372}
373
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200374/**
375 * batadv_tt_local_event - store a local TT event (ADD/DEL)
376 * @bat_priv: the bat priv with all the soft interface information
377 * @tt_local_entry: the TT entry involved in the event
378 * @event_flags: flags to store in the event structure
379 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200380static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200381 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200382 u8 event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200383{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200384 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200385 struct batadv_tt_common_entry *common = &tt_local_entry->common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200386 u8 flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200387 bool event_removed = false;
388 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200389
390 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200391 if (!tt_change_node)
392 return;
393
Antonio Quartulliff66c972011-06-30 01:14:00 +0200394 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100395 memset(tt_change_node->change.reserved, 0,
396 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100397 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200398 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200399
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200400 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200401
402 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200403 spin_lock_bh(&bat_priv->tt.changes_list_lock);
404 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200405 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200406 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200407 continue;
408
409 /* DEL+ADD in the same orig interval have no effect and can be
410 * removed to avoid silly behaviour on the receiver side. The
411 * other way around (ADD+DEL) can happen in case of roaming of
412 * a client still in the NEW state. Roaming of NEW clients is
413 * now possible due to automatically recognition of "temporary"
414 * clients
415 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200416 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200417 if (!del_op_requested && del_op_entry)
418 goto del;
419 if (del_op_requested && !del_op_entry)
420 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200421
422 /* this is a second add in the same originator interval. It
423 * means that flags have been changed: update them!
424 */
425 if (!del_op_requested && !del_op_entry)
426 entry->change.flags = flags;
427
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200428 continue;
429del:
430 list_del(&entry->list);
431 kfree(entry);
Jesper Juhl155e4e12012-08-07 08:32:34 +0000432 kfree(tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200433 event_removed = true;
434 goto unlock;
435 }
436
Antonio Quartullia73105b2011-04-27 14:27:44 +0200437 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200438 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200439
440unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200441 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200442
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200443 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200444 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200445 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200446 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200447}
448
Marek Lindner335fbe02013-04-23 21:40:02 +0800449/**
450 * batadv_tt_len - compute length in bytes of given number of tt changes
451 * @changes_num: number of tt changes
452 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200453 * Return: computed length in bytes.
Marek Lindner335fbe02013-04-23 21:40:02 +0800454 */
455static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200456{
Marek Lindner335fbe02013-04-23 21:40:02 +0800457 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200458}
459
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200460/**
461 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
462 * @tt_len: available space
463 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200464 * Return: the number of entries.
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200465 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200466static u16 batadv_tt_entries(u16 tt_len)
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200467{
468 return tt_len / batadv_tt_len(1);
469}
470
Marek Lindnera19d3d82013-05-27 15:33:25 +0800471/**
472 * batadv_tt_local_table_transmit_size - calculates the local translation table
473 * size when transmitted over the air
474 * @bat_priv: the bat priv with all the soft interface information
475 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200476 * Return: local translation table size in bytes.
Marek Lindnera19d3d82013-05-27 15:33:25 +0800477 */
478static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
479{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200480 u16 num_vlan = 0;
481 u16 tt_local_entries = 0;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800482 struct batadv_softif_vlan *vlan;
483 int hdr_size;
484
485 rcu_read_lock();
486 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
487 num_vlan++;
488 tt_local_entries += atomic_read(&vlan->tt.num_entries);
489 }
490 rcu_read_unlock();
491
492 /* header size of tvlv encapsulated tt response payload */
493 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
494 hdr_size += sizeof(struct batadv_tvlv_hdr);
495 hdr_size += sizeof(struct batadv_tvlv_tt_data);
496 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
497
498 return hdr_size + batadv_tt_len(tt_local_entries);
499}
500
Sven Eckelmann56303d32012-06-05 22:31:31 +0200501static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200503 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200504 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505
Sven Eckelmann807736f2012-07-15 22:26:51 +0200506 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Sven Eckelmann807736f2012-07-15 22:26:51 +0200508 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200509 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510
Antonio Quartullidec05072012-11-10 11:00:32 +0100511 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
512 &batadv_tt_local_hash_lock_class_key);
513
Sven Eckelmann5346c352012-05-05 13:27:28 +0200514 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515}
516
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200517static void batadv_tt_global_free(struct batadv_priv *bat_priv,
518 struct batadv_tt_global_entry *tt_global,
519 const char *message)
520{
521 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200522 "Deleting global tt entry %pM (vid: %d): %s\n",
523 tt_global->common.addr,
524 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200525
526 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200527 batadv_choose_tt, &tt_global->common);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200528 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200529}
530
Antonio Quartullic018ad32013-06-04 12:11:39 +0200531/**
532 * batadv_tt_local_add - add a new client to the local table or update an
533 * existing client
534 * @soft_iface: netdev struct of the mesh interface
535 * @addr: the mac address of the client to add
536 * @vid: VLAN identifier
537 * @ifindex: index of the interface where the client is connected to (useful to
538 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100539 * @mark: the value contained in the skb->mark field of the received packet (if
540 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800541 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200542 * Return: true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200543 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200544bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
545 unsigned short vid, int ifindex, u32 mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200547 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200548 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100549 struct batadv_tt_global_entry *tt_global = NULL;
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200550 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200551 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200552 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200553 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800554 int hash_added, table_size, packet_size_max;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200555 bool ret = false;
556 bool roamed_back = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200557 u8 remote_flags;
558 u32 match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200560 if (ifindex != BATADV_NULL_IFINDEX)
561 in_dev = dev_get_by_index(&init_net, ifindex);
562
Antonio Quartullic018ad32013-06-04 12:11:39 +0200563 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100564
565 if (!is_multicast_ether_addr(addr))
566 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567
Antonio Quartulli47c94652012-09-23 22:38:35 +0200568 if (tt_local) {
569 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200570 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
571 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200572 "Re-adding pending client %pM (vid: %d)\n",
573 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200574 /* whatever the reason why the PENDING flag was set,
575 * this is a client which was enqueued to be removed in
576 * this orig_interval. Since it popped up again, the
577 * flag can be reset like it was never enqueued
578 */
579 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
580 goto add_event;
581 }
582
583 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
584 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200585 "Roaming client %pM (vid: %d) came back to its original location\n",
586 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200587 /* the ROAM flag is set because this client roamed away
588 * and the node got a roaming_advertisement message. Now
589 * that the client popped up again at its original
590 * location such flag can be unset
591 */
592 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
593 roamed_back = true;
594 }
595 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 }
597
Marek Lindnera19d3d82013-05-27 15:33:25 +0800598 /* Ignore the client if we cannot send it in a full table response. */
599 table_size = batadv_tt_local_table_transmit_size(bat_priv);
600 table_size += batadv_tt_len(1);
601 packet_size_max = atomic_read(&bat_priv->packet_size_max);
602 if (table_size > packet_size_max) {
603 net_ratelimited_function(batadv_info, soft_iface,
604 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
605 table_size, packet_size_max, addr);
606 goto out;
607 }
608
Antonio Quartulli47c94652012-09-23 22:38:35 +0200609 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
610 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200611 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200612
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200613 /* increase the refcounter of the related vlan */
614 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +0800615 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200616 addr, BATADV_PRINT_VID(vid))) {
617 kfree(tt_local);
618 tt_local = NULL;
Marek Lindner354136b2015-06-09 21:24:36 +0800619 goto out;
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200620 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200621
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200622 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200623 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
624 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200625 (u8)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100627 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100628 /* The local entry has to be marked as NEW to avoid to send it in
629 * a full table response going out before the next ttvn increment
630 * (consistency check)
631 */
632 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200633 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200634 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200635 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
636 atomic_set(&tt_local->common.refcount, 2);
637 tt_local->last_seen = jiffies;
638 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100640 /* the batman interface mac and multicast addresses should never be
641 * purged
642 */
643 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
644 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200645 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646
Sven Eckelmann807736f2012-07-15 22:26:51 +0200647 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200648 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200649 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100650
651 if (unlikely(hash_added != 0)) {
652 /* remove the reference for the hash */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200653 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200654 batadv_softif_vlan_free_ref(vlan);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100655 goto out;
656 }
657
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200658add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200659 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200660
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200661check_roaming:
662 /* Check whether it is a roaming, but don't do anything if the roaming
663 * process has already been handled
664 */
665 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200666 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200667 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200668 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800669 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200670 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200671 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200672 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200673 }
674 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200675 if (roamed_back) {
676 batadv_tt_global_free(bat_priv, tt_global,
677 "Roaming canceled");
678 tt_global = NULL;
679 } else {
680 /* The global entry has to be marked as ROAMING and
681 * has to be kept for consistency purpose
682 */
683 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
684 tt_global->roam_at = jiffies;
685 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200686 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200687
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200688 /* store the current remote flags before altering them. This helps
689 * understanding is flags are changing or not
690 */
691 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800692
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200693 if (batadv_is_wifi_netdev(in_dev))
694 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
695 else
696 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
697
Antonio Quartulli9464d072013-11-16 12:03:48 +0100698 /* check the mark in the skb: if it's equal to the configured
699 * isolation_mark, it means the packet is coming from an isolated
700 * non-mesh client
701 */
702 match_mark = (mark & bat_priv->isolation_mark_mask);
703 if (bat_priv->isolation_mark_mask &&
704 match_mark == bat_priv->isolation_mark)
705 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
706 else
707 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
708
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200709 /* if any "dynamic" flag has been modified, resend an ADD event for this
710 * entry so that all the nodes can get the new flags
711 */
712 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
713 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
714
715 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200716out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200717 if (in_dev)
718 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200719 if (tt_local)
720 batadv_tt_local_entry_free_ref(tt_local);
721 if (tt_global)
722 batadv_tt_global_entry_free_ref(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800723 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724}
725
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800726/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200727 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
728 * within a TT Response directed to another node
729 * @orig_node: originator for which the TT data has to be prepared
730 * @tt_data: uninitialised pointer to the address of the TVLV buffer
731 * @tt_change: uninitialised pointer to the address of the area where the TT
732 * changed can be stored
733 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
734 * function reserves the amount of space needed to send the entire global TT
735 * table. In case of success the value is updated with the real amount of
736 * reserved bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200737 * Allocate the needed amount of memory for the entire TT TVLV and write its
738 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
739 * objects, one per active VLAN served by the originator node.
740 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200741 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200742 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200743static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200744batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
745 struct batadv_tvlv_tt_data **tt_data,
746 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200747 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200748{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200749 u16 num_vlan = 0;
750 u16 num_entries = 0;
751 u16 change_offset;
752 u16 tvlv_len;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200753 struct batadv_tvlv_tt_vlan_data *tt_vlan;
754 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200755 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200756
757 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800758 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200759 num_vlan++;
760 num_entries += atomic_read(&vlan->tt.num_entries);
761 }
762
763 change_offset = sizeof(**tt_data);
764 change_offset += num_vlan * sizeof(*tt_vlan);
765
766 /* if tt_len is negative, allocate the space needed by the full table */
767 if (*tt_len < 0)
768 *tt_len = batadv_tt_len(num_entries);
769
770 tvlv_len = *tt_len;
771 tvlv_len += change_offset;
772
773 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
774 if (!*tt_data) {
775 *tt_len = 0;
776 goto out;
777 }
778
779 (*tt_data)->flags = BATADV_NO_FLAGS;
780 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
781 (*tt_data)->num_vlan = htons(num_vlan);
782
783 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800784 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200785 tt_vlan->vid = htons(vlan->vid);
786 tt_vlan->crc = htonl(vlan->tt.crc);
787
788 tt_vlan++;
789 }
790
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200791 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200792 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
793
794out:
795 rcu_read_unlock();
796 return tvlv_len;
797}
798
799/**
800 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
801 * node
802 * @bat_priv: the bat priv with all the soft interface information
803 * @tt_data: uninitialised pointer to the address of the TVLV buffer
804 * @tt_change: uninitialised pointer to the address of the area where the TT
805 * changes can be stored
806 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
807 * function reserves the amount of space needed to send the entire local TT
808 * table. In case of success the value is updated with the real amount of
809 * reserved bytes
810 *
811 * Allocate the needed amount of memory for the entire TT TVLV and write its
812 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
813 * objects, one per active VLAN.
814 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200815 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200816 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200817static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200818batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
819 struct batadv_tvlv_tt_data **tt_data,
820 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200821 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200822{
823 struct batadv_tvlv_tt_vlan_data *tt_vlan;
824 struct batadv_softif_vlan *vlan;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200825 u16 num_vlan = 0;
826 u16 num_entries = 0;
827 u16 tvlv_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200828 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200829 int change_offset;
830
831 rcu_read_lock();
832 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
833 num_vlan++;
834 num_entries += atomic_read(&vlan->tt.num_entries);
835 }
836
837 change_offset = sizeof(**tt_data);
838 change_offset += num_vlan * sizeof(*tt_vlan);
839
840 /* if tt_len is negative, allocate the space needed by the full table */
841 if (*tt_len < 0)
842 *tt_len = batadv_tt_len(num_entries);
843
844 tvlv_len = *tt_len;
845 tvlv_len += change_offset;
846
847 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
848 if (!*tt_data) {
849 tvlv_len = 0;
850 goto out;
851 }
852
853 (*tt_data)->flags = BATADV_NO_FLAGS;
854 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
855 (*tt_data)->num_vlan = htons(num_vlan);
856
857 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
858 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
859 tt_vlan->vid = htons(vlan->vid);
860 tt_vlan->crc = htonl(vlan->tt.crc);
861
862 tt_vlan++;
863 }
864
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200865 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200866 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
867
868out:
869 rcu_read_unlock();
870 return tvlv_len;
871}
872
873/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800874 * batadv_tt_tvlv_container_update - update the translation table tvlv container
875 * after local tt changes have been committed
876 * @bat_priv: the bat priv with all the soft interface information
877 */
878static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800880 struct batadv_tt_change_node *entry, *safe;
881 struct batadv_tvlv_tt_data *tt_data;
882 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200883 int tt_diff_len, tt_change_len = 0;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200884 int tt_diff_entries_num = 0;
885 int tt_diff_entries_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200886 u16 tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200888 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
889 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800890
891 /* if we have too many changes for one packet don't send any
892 * and wait for the tt table request which will be fragmented
893 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800894 if (tt_diff_len > bat_priv->soft_iface->mtu)
895 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800896
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200897 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
898 &tt_change, &tt_diff_len);
899 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800900 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800901
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800902 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800903
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800904 if (tt_diff_len == 0)
905 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800906
Sven Eckelmann807736f2012-07-15 22:26:51 +0200907 spin_lock_bh(&bat_priv->tt.changes_list_lock);
908 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000909
Sven Eckelmann807736f2012-07-15 22:26:51 +0200910 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100911 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800912 if (tt_diff_entries_count < tt_diff_entries_num) {
913 memcpy(tt_change + tt_diff_entries_count,
914 &entry->change,
915 sizeof(struct batadv_tvlv_tt_change));
916 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917 }
Antonio Quartullia73105b2011-04-27 14:27:44 +0200918 list_del(&entry->list);
919 kfree(entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000920 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200921 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000922
Antonio Quartullia73105b2011-04-27 14:27:44 +0200923 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200924 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
925 kfree(bat_priv->tt.last_changeset);
926 bat_priv->tt.last_changeset_len = 0;
927 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800928 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800929 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800930 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800931 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +0200932 * instead of providing the diff
933 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800934 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200935 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800936 memcpy(bat_priv->tt.last_changeset,
937 tt_change, tt_change_len);
938 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200939 }
940 }
Sven Eckelmann807736f2012-07-15 22:26:51 +0200941 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800943container_register:
944 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200945 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800946 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947}
948
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200949int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950{
951 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200952 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +0200953 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200954 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100955 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200956 struct batadv_hard_iface *primary_if;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200957 struct batadv_softif_vlan *vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000958 struct hlist_head *head;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200959 unsigned short vid;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200960 u32 i;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100961 int last_seen_secs;
962 int last_seen_msecs;
963 unsigned long last_seen_jiffies;
964 bool no_purge;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200965 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966
Marek Lindner30da63a2012-08-03 17:15:46 +0200967 primary_if = batadv_seq_print_text_primary_if_get(seq);
968 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200969 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000970
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100971 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200972 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200973 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
Antonio Quartullidd24ddb2013-11-16 12:03:49 +0100974 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200975 "Flags", "Last seen", "CRC");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000976
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977 for (i = 0; i < hash->size; i++) {
978 head = &hash->table[i];
979
Marek Lindner7aadf882011-02-18 12:28:09 +0000980 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800981 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +0000982 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +0100983 tt_local = container_of(tt_common_entry,
984 struct batadv_tt_local_entry,
985 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200986 vid = tt_common_entry->vid;
Antonio Quartulli85766a82012-11-08 22:16:16 +0100987 last_seen_jiffies = jiffies - tt_local->last_seen;
988 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
989 last_seen_secs = last_seen_msecs / 1000;
990 last_seen_msecs = last_seen_msecs % 1000;
991
992 no_purge = tt_common_entry->flags & np_flag;
993
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200994 vlan = batadv_softif_vlan_get(bat_priv, vid);
995 if (!vlan) {
996 seq_printf(seq, "Cannot retrieve VLAN %d\n",
997 BATADV_PRINT_VID(vid));
998 continue;
999 }
1000
1001 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001002 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001003 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001004 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001005 ((tt_common_entry->flags &
1006 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +01001007 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001008 ((tt_common_entry->flags &
1009 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1010 ((tt_common_entry->flags &
1011 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1012 ((tt_common_entry->flags &
1013 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1014 ((tt_common_entry->flags &
1015 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +01001016 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001017 no_purge ? 0 : last_seen_msecs,
1018 vlan->tt.crc);
1019
1020 batadv_softif_vlan_free_ref(vlan);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001022 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001024out:
1025 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001026 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001027 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028}
1029
Sven Eckelmann56303d32012-06-05 22:31:31 +02001030static void
1031batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1032 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001033 u16 flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001034{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001035 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036
Antonio Quartulli015758d2011-07-09 17:52:13 +02001037 /* The local client has to be marked as "pending to be removed" but has
1038 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001039 * response issued before the net ttvn increment (consistency check)
1040 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001041 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001042
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001043 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001044 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1045 tt_local_entry->common.addr,
1046 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047}
1048
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001049/**
1050 * batadv_tt_local_remove - logically remove an entry from the local table
1051 * @bat_priv: the bat priv with all the soft interface information
1052 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001053 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001054 * @message: message to append to the log on deletion
1055 * @roaming: true if the deletion is due to a roaming event
1056 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001057 * Return: the flags assigned to the local entry before being deleted
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001058 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001059u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1060 unsigned short vid, const char *message,
1061 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001062{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001063 struct batadv_tt_local_entry *tt_local_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001064 u16 flags, curr_flags = BATADV_NO_FLAGS;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001065 struct batadv_softif_vlan *vlan;
Marek Lindneref727062015-06-17 20:01:36 +08001066 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001067
Antonio Quartullic018ad32013-06-04 12:11:39 +02001068 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001069 if (!tt_local_entry)
1070 goto out;
1071
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001072 curr_flags = tt_local_entry->common.flags;
1073
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001074 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001075 /* if this global entry addition is due to a roaming, the node has to
1076 * mark the local entry as "roamed" in order to correctly reroute
1077 * packets later
1078 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001079 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001080 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001081 /* mark the local client as ROAMed */
1082 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1083 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001084
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001085 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1086 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1087 message);
1088 goto out;
1089 }
1090 /* if this client has been added right now, it is possible to
1091 * immediately purge it
1092 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001093 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001094
1095 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1096 batadv_compare_tt,
1097 batadv_choose_tt,
1098 &tt_local_entry->common);
1099 if (!tt_entry_exists)
1100 goto out;
1101
1102 /* extra call to free the local tt entry */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001103 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001104
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001105 /* decrease the reference held for this vlan */
1106 vlan = batadv_softif_vlan_get(bat_priv, vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001107 if (!vlan)
1108 goto out;
1109
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001110 batadv_softif_vlan_free_ref(vlan);
1111 batadv_softif_vlan_free_ref(vlan);
1112
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001113out:
1114 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001115 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001116
1117 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001118}
1119
Marek Lindnera19d3d82013-05-27 15:33:25 +08001120/**
1121 * batadv_tt_local_purge_list - purge inactive tt local entries
1122 * @bat_priv: the bat priv with all the soft interface information
1123 * @head: pointer to the list containing the local tt entries
1124 * @timeout: parameter deciding whether a given tt local entry is considered
1125 * inactive or not
1126 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001127static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001128 struct hlist_head *head,
1129 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001131 struct batadv_tt_local_entry *tt_local_entry;
1132 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001133 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001134
Sasha Levinb67bfe02013-02-27 17:06:00 -08001135 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001136 hash_entry) {
1137 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001138 struct batadv_tt_local_entry,
1139 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001140 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1141 continue;
1142
1143 /* entry already marked for deletion */
1144 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1145 continue;
1146
Marek Lindnera19d3d82013-05-27 15:33:25 +08001147 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001148 continue;
1149
1150 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1151 BATADV_TT_CLIENT_DEL, "timed out");
1152 }
1153}
1154
Marek Lindnera19d3d82013-05-27 15:33:25 +08001155/**
1156 * batadv_tt_local_purge - purge inactive tt local entries
1157 * @bat_priv: the bat priv with all the soft interface information
1158 * @timeout: parameter deciding whether a given tt local entry is considered
1159 * inactive or not
1160 */
1161static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1162 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001163{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001164 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001165 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001166 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001167 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001168
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169 for (i = 0; i < hash->size; i++) {
1170 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001171 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001172
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001173 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001174 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001175 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001176 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001177}
1178
Sven Eckelmann56303d32012-06-05 22:31:31 +02001179static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001181 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001182 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001183 struct batadv_tt_common_entry *tt_common_entry;
1184 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001185 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001186 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001187 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001188 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001189
Sven Eckelmann807736f2012-07-15 22:26:51 +02001190 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001191 return;
1192
Sven Eckelmann807736f2012-07-15 22:26:51 +02001193 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001194
1195 for (i = 0; i < hash->size; i++) {
1196 head = &hash->table[i];
1197 list_lock = &hash->list_locks[i];
1198
1199 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001200 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001201 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001202 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001203 tt_local = container_of(tt_common_entry,
1204 struct batadv_tt_local_entry,
1205 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001206
1207 /* decrease the reference held for this vlan */
1208 vlan = batadv_softif_vlan_get(bat_priv,
1209 tt_common_entry->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08001210 if (vlan) {
1211 batadv_softif_vlan_free_ref(vlan);
1212 batadv_softif_vlan_free_ref(vlan);
1213 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001214
Sven Eckelmann56303d32012-06-05 22:31:31 +02001215 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001216 }
1217 spin_unlock_bh(list_lock);
1218 }
1219
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001220 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001221
Sven Eckelmann807736f2012-07-15 22:26:51 +02001222 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001223}
1224
Sven Eckelmann56303d32012-06-05 22:31:31 +02001225static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001226{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001227 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001228 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229
Sven Eckelmann807736f2012-07-15 22:26:51 +02001230 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001231
Sven Eckelmann807736f2012-07-15 22:26:51 +02001232 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001233 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001234
Antonio Quartullidec05072012-11-10 11:00:32 +01001235 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1236 &batadv_tt_global_hash_lock_class_key);
1237
Sven Eckelmann5346c352012-05-05 13:27:28 +02001238 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001239}
1240
Sven Eckelmann56303d32012-06-05 22:31:31 +02001241static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001242{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001243 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001244
Sven Eckelmann807736f2012-07-15 22:26:51 +02001245 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001246
Sven Eckelmann807736f2012-07-15 22:26:51 +02001247 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001248 list) {
1249 list_del(&entry->list);
1250 kfree(entry);
1251 }
1252
Sven Eckelmann807736f2012-07-15 22:26:51 +02001253 atomic_set(&bat_priv->tt.local_changes, 0);
1254 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001255}
1256
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001257/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08001258 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1259 * @entry: the TT global entry where the orig_list_entry has to be
1260 * extracted from
1261 * @orig_node: the originator for which the orig_list_entry has to be found
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001262 *
Antonio Quartullid15cd622015-11-17 16:40:52 +08001263 * retrieve the orig_tt_list_entry belonging to orig_node from the
Antonio Quartullid657e622012-07-01 14:09:12 +02001264 * batadv_tt_global_entry list
1265 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001266 * Return: it with an increased refcounter, NULL if not found
Antonio Quartullid657e622012-07-01 14:09:12 +02001267 */
1268static struct batadv_tt_orig_list_entry *
1269batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1270 const struct batadv_orig_node *orig_node)
1271{
1272 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1273 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001274
1275 rcu_read_lock();
1276 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001277 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001278 if (tmp_orig_entry->orig_node != orig_node)
1279 continue;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001280 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
Antonio Quartullid657e622012-07-01 14:09:12 +02001281 continue;
1282
1283 orig_entry = tmp_orig_entry;
1284 break;
1285 }
1286 rcu_read_unlock();
1287
1288 return orig_entry;
1289}
1290
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001291/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08001292 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1293 * by a given originator
1294 * @entry: the TT global entry to check
1295 * @orig_node: the originator to search in the list
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001296 *
1297 * find out if an orig_node is already in the list of a tt_global_entry.
1298 *
1299 * Return: true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001300 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001301static bool
1302batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1303 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001304{
Antonio Quartullid657e622012-07-01 14:09:12 +02001305 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001306 bool found = false;
1307
Antonio Quartullid657e622012-07-01 14:09:12 +02001308 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1309 if (orig_entry) {
1310 found = true;
1311 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001312 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001313
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001314 return found;
1315}
1316
Sven Eckelmanna5130882012-05-16 20:23:16 +02001317static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001318batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001319 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001320{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001321 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001322
Antonio Quartullid657e622012-07-01 14:09:12 +02001323 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001324 if (orig_entry) {
1325 /* refresh the ttvn: the current value could be a bogus one that
1326 * was added during a "temporary client detection"
1327 */
1328 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001329 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001330 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001331
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001332 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1333 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001334 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001335
1336 INIT_HLIST_NODE(&orig_entry->list);
1337 atomic_inc(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001338 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001339 orig_entry->orig_node = orig_node;
1340 orig_entry->ttvn = ttvn;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001341 kref_init(&orig_entry->refcount);
1342 kref_get(&orig_entry->refcount);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001343
Antonio Quartullid657e622012-07-01 14:09:12 +02001344 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001345 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001346 &tt_global->orig_list);
1347 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001348 atomic_inc(&tt_global->orig_list_count);
1349
Antonio Quartullid657e622012-07-01 14:09:12 +02001350out:
1351 if (orig_entry)
1352 batadv_tt_orig_list_entry_free_ref(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001353}
1354
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001355/**
1356 * batadv_tt_global_add - add a new TT global entry or update an existing one
1357 * @bat_priv: the bat priv with all the soft interface information
1358 * @orig_node: the originator announcing the client
1359 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001360 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001361 * @flags: TT flags that have to be set for this non-mesh client
1362 * @ttvn: the tt version number ever announcing this non-mesh client
1363 *
1364 * Add a new TT global entry for the given originator. If the entry already
1365 * exists add a new reference to the given originator (a global entry can have
1366 * references to multiple originators) and adjust the flags attribute to reflect
1367 * the function argument.
1368 * If a TT local entry exists for this non-mesh client remove it.
1369 *
1370 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001371 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001372 * Return: true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001373 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001374static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1375 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001376 const unsigned char *tt_addr,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001377 unsigned short vid, u16 flags, u8 ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001378{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001379 struct batadv_tt_global_entry *tt_global_entry;
1380 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001381 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001382 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001383 struct batadv_tt_common_entry *common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001384 u16 local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001385
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001386 /* ignore global entries from backbone nodes */
1387 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1388 return true;
1389
Antonio Quartullic018ad32013-06-04 12:11:39 +02001390 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1391 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001392
1393 /* if the node already has a local client for this entry, it has to wait
1394 * for a roaming advertisement instead of manually messing up the global
1395 * table
1396 */
1397 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1398 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1399 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001400
Antonio Quartullia73105b2011-04-27 14:27:44 +02001401 if (!tt_global_entry) {
Antonio Quartullid4f44692012-05-25 00:00:54 +02001402 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001403 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001404 goto out;
1405
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001406 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001407 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001408 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001409
Antonio Quartullid4f44692012-05-25 00:00:54 +02001410 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001411 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001412 /* node must store current time in case of roaming. This is
1413 * needed to purge this entry out on timeout (if nobody claims
1414 * it)
1415 */
1416 if (flags & BATADV_TT_CLIENT_ROAM)
1417 tt_global_entry->roam_at = jiffies;
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001418 atomic_set(&common->refcount, 2);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001419 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001420
1421 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001422 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001423 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001424
Sven Eckelmann807736f2012-07-15 22:26:51 +02001425 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001426 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001427 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001428 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001429
1430 if (unlikely(hash_added != 0)) {
1431 /* remove the reference for the hash */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001432 batadv_tt_global_entry_free_ref(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001433 goto out_remove;
1434 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001435 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001436 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001437 /* If there is already a global entry, we can use this one for
1438 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001439 * But if we are trying to add a temporary client then here are
1440 * two options at this point:
1441 * 1) the global client is not a temporary client: the global
1442 * client has to be left as it is, temporary information
1443 * should never override any already known client state
1444 * 2) the global client is a temporary client: purge the
1445 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001446 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001447 if (flags & BATADV_TT_CLIENT_TEMP) {
1448 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1449 goto out;
1450 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1451 orig_node))
1452 goto out_remove;
1453 batadv_tt_global_del_orig_list(tt_global_entry);
1454 goto add_orig_entry;
1455 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001456
1457 /* if the client was temporary added before receiving the first
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001458 * OGM announcing it, we have to clear the TEMP flag. Also,
1459 * remove the previous temporary orig node and re-add it
1460 * if required. If the orig entry changed, the new one which
1461 * is a non-temporary entry is preferred.
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001462 */
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001463 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1464 batadv_tt_global_del_orig_list(tt_global_entry);
1465 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1466 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001467
Antonio Quartullie9c001362012-11-07 15:05:33 +01001468 /* the change can carry possible "attribute" flags like the
1469 * TT_CLIENT_WIFI, therefore they have to be copied in the
1470 * client entry
1471 */
Simon Wunderlichad7e2c42015-08-26 16:33:34 +02001472 common->flags |= flags;
Antonio Quartullie9c001362012-11-07 15:05:33 +01001473
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001474 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1475 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001476 * delete + roaming change for this originator.
1477 *
1478 * We should first delete the old originator before adding the
1479 * new one.
1480 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001481 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001482 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001483 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001484 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001485 }
1486 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001487add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001488 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001489 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001491 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001492 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1493 common->addr, BATADV_PRINT_VID(common->vid),
1494 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001495 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001496
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001497out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001498 /* Do not remove multicast addresses from the local hash on
1499 * global additions
1500 */
1501 if (is_multicast_ether_addr(tt_addr))
1502 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001503
Antonio Quartullia73105b2011-04-27 14:27:44 +02001504 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001505 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001506 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001507 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001508 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1509
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001510 if (!(flags & BATADV_TT_CLIENT_ROAM))
1511 /* this is a normal global add. Therefore the client is not in a
1512 * roaming state anymore.
1513 */
1514 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1515
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001516out:
1517 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001518 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001519 if (tt_local_entry)
1520 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001521 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001522}
1523
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001524/**
1525 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001526 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001527 * @tt_global_entry: global translation table entry to be analyzed
1528 *
1529 * This functon assumes the caller holds rcu_read_lock().
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001530 * Return: best originator list entry or NULL on errors.
Sven Eckelmann981d8902012-10-07 13:34:15 +02001531 */
1532static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001533batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1534 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001535{
Antonio Quartulli46274562013-09-03 11:10:24 +02001536 struct batadv_neigh_node *router, *best_router = NULL;
1537 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001538 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001539 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001540
1541 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001542 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001543 router = batadv_orig_router_get(orig_entry->orig_node,
1544 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001545 if (!router)
1546 continue;
1547
Antonio Quartulli46274562013-09-03 11:10:24 +02001548 if (best_router &&
Simon Wunderlich89652332013-11-13 19:14:46 +01001549 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1550 best_router, BATADV_IF_DEFAULT) <= 0) {
Antonio Quartulli46274562013-09-03 11:10:24 +02001551 batadv_neigh_node_free_ref(router);
1552 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001553 }
1554
Antonio Quartulli46274562013-09-03 11:10:24 +02001555 /* release the refcount for the "old" best */
1556 if (best_router)
1557 batadv_neigh_node_free_ref(best_router);
1558
1559 best_entry = orig_entry;
1560 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001561 }
1562
Antonio Quartulli46274562013-09-03 11:10:24 +02001563 if (best_router)
1564 batadv_neigh_node_free_ref(best_router);
1565
Sven Eckelmann981d8902012-10-07 13:34:15 +02001566 return best_entry;
1567}
1568
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001569/**
1570 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1571 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001572 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001573 * @tt_global_entry: global translation table entry to be printed
1574 * @seq: debugfs table seq_file struct
1575 *
1576 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001577 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001578static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001579batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1580 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001581 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001582{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001583 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001584 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001585 struct batadv_orig_node_vlan *vlan;
1586 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001587 u8 last_ttvn;
1588 u16 flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001589
1590 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001591 flags = tt_common_entry->flags;
1592
Antonio Quartulli46274562013-09-03 11:10:24 +02001593 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001594 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001595 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1596 tt_common_entry->vid);
1597 if (!vlan) {
1598 seq_printf(seq,
1599 " * Cannot retrieve VLAN %d for originator %pM\n",
1600 BATADV_PRINT_VID(tt_common_entry->vid),
1601 best_entry->orig_node->orig);
1602 goto print_list;
1603 }
1604
Sven Eckelmann981d8902012-10-07 13:34:15 +02001605 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001606 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001607 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001608 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001609 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001610 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001611 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001612 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1613 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1614 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1615 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001616
1617 batadv_orig_node_vlan_free_ref(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001618 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001619
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001620print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001621 head = &tt_global_entry->orig_list;
1622
Sasha Levinb67bfe02013-02-27 17:06:00 -08001623 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001624 if (best_entry == orig_entry)
1625 continue;
1626
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001627 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1628 tt_common_entry->vid);
1629 if (!vlan) {
1630 seq_printf(seq,
1631 " + Cannot retrieve VLAN %d for originator %pM\n",
1632 BATADV_PRINT_VID(tt_common_entry->vid),
1633 orig_entry->orig_node->orig);
1634 continue;
1635 }
1636
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001637 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001638 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001639 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001640 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001641 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001642 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001643 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001644 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1645 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1646 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1647 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001648
1649 batadv_orig_node_vlan_free_ref(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001650 }
1651}
1652
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001653int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001654{
1655 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001656 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001657 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001658 struct batadv_tt_common_entry *tt_common_entry;
1659 struct batadv_tt_global_entry *tt_global;
1660 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001661 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001662 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001663
Marek Lindner30da63a2012-08-03 17:15:46 +02001664 primary_if = batadv_seq_print_text_primary_if_get(seq);
1665 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001666 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001667
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001668 seq_printf(seq,
1669 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001670 net_dev->name);
Antonio Quartulli16052782013-06-04 12:11:41 +02001671 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1672 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1673 "CRC", "Flags");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001674
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001675 for (i = 0; i < hash->size; i++) {
1676 head = &hash->table[i];
1677
Marek Lindner7aadf882011-02-18 12:28:09 +00001678 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001679 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001680 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001681 tt_global = container_of(tt_common_entry,
1682 struct batadv_tt_global_entry,
1683 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001684 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001685 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001686 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001687 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001688out:
1689 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001690 batadv_hardif_free_ref(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001691 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001692}
1693
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001694/**
Marek Lindner433ff98f2015-06-18 16:11:07 +08001695 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001696 * @tt_global_entry: the global entry to remove the orig_entry from
1697 * @orig_entry: the orig entry to remove and free
1698 *
1699 * Remove an orig_entry from its list in the given tt_global_entry and
1700 * free this orig_entry afterwards.
Marek Lindner433ff98f2015-06-18 16:11:07 +08001701 *
1702 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1703 * part of a list.
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001704 */
1705static void
Marek Lindner433ff98f2015-06-18 16:11:07 +08001706_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1707 struct batadv_tt_orig_list_entry *orig_entry)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001708{
Sven Eckelmann2c72d652015-06-21 14:45:14 +02001709 lockdep_assert_held(&tt_global_entry->list_lock);
1710
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001711 batadv_tt_global_size_dec(orig_entry->orig_node,
1712 tt_global_entry->common.vid);
1713 atomic_dec(&tt_global_entry->orig_list_count);
Marek Lindner433ff98f2015-06-18 16:11:07 +08001714 /* requires holding tt_global_entry->list_lock and orig_entry->list
1715 * being part of a list
1716 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001717 hlist_del_rcu(&orig_entry->list);
1718 batadv_tt_orig_list_entry_free_ref(orig_entry);
1719}
1720
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001721/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001722static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001723batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001724{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001725 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001726 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001727 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001728
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001729 spin_lock_bh(&tt_global_entry->list_lock);
1730 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001731 hlist_for_each_entry_safe(orig_entry, safe, head, list)
Marek Lindner433ff98f2015-06-18 16:11:07 +08001732 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001733 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001734}
1735
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001736/**
1737 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1738 * @bat_priv: the bat priv with all the soft interface information
1739 * @tt_global_entry: the global entry to remove the orig_node from
1740 * @orig_node: the originator announcing the client
1741 * @message: message to append to the log on deletion
1742 *
1743 * Remove the given orig_node and its according orig_entry from the given
1744 * global tt entry.
1745 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001746static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001747batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1748 struct batadv_tt_global_entry *tt_global_entry,
1749 struct batadv_orig_node *orig_node,
1750 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001751{
1752 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001753 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001754 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001755 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001756
1757 spin_lock_bh(&tt_global_entry->list_lock);
1758 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001759 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001760 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001761 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001762 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001763 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001764 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001765 tt_global_entry->common.addr,
1766 BATADV_PRINT_VID(vid), message);
Marek Lindner433ff98f2015-06-18 16:11:07 +08001767 _batadv_tt_global_del_orig_entry(tt_global_entry,
1768 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001769 }
1770 }
1771 spin_unlock_bh(&tt_global_entry->list_lock);
1772}
1773
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001774/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001775 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1776 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001777 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001778static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001779batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1780 struct batadv_tt_global_entry *tt_global_entry,
1781 struct batadv_orig_node *orig_node,
1782 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001783{
1784 bool last_entry = true;
1785 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001786 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001787
1788 /* no local entry exists, case 1:
1789 * Check if this is the last one or if other entries exist.
1790 */
1791
1792 rcu_read_lock();
1793 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001794 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001795 if (orig_entry->orig_node != orig_node) {
1796 last_entry = false;
1797 break;
1798 }
1799 }
1800 rcu_read_unlock();
1801
1802 if (last_entry) {
1803 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001804 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001805 tt_global_entry->roam_at = jiffies;
1806 } else
1807 /* there is another entry, we can simply delete this
1808 * one and can still use the other one.
1809 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001810 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1811 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001812}
1813
Antonio Quartullic018ad32013-06-04 12:11:39 +02001814/**
1815 * batadv_tt_global_del - remove a client from the global table
1816 * @bat_priv: the bat priv with all the soft interface information
1817 * @orig_node: an originator serving this client
1818 * @addr: the mac address of the client
1819 * @vid: VLAN identifier
1820 * @message: a message explaining the reason for deleting the client to print
1821 * for debugging purpose
1822 * @roaming: true if the deletion has been triggered by a roaming event
1823 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001824static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1825 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001826 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001827 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001828{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001829 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001830 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001831
Antonio Quartullic018ad32013-06-04 12:11:39 +02001832 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001833 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001834 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001835
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001836 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001837 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1838 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001839
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001840 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001841 batadv_tt_global_free(bat_priv, tt_global_entry,
1842 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001843
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001844 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001845 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001846
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001847 /* if we are deleting a global entry due to a roam
1848 * event, there are two possibilities:
1849 * 1) the client roamed from node A to node B => if there
1850 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001851 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001852 * wait for node B to claim it. In case of timeout
1853 * the entry is purged.
1854 *
1855 * If there are other originators left, we directly delete
1856 * the originator.
1857 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001858 * the global entry, since it is useless now.
1859 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001860 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001861 tt_global_entry->common.addr,
1862 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001863 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001864 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001865 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001866 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001867 } else
1868 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001869 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1870 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001871
Antonio Quartullicc47f662011-04-27 14:27:57 +02001872out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001873 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02001874 batadv_tt_global_entry_free_ref(tt_global_entry);
1875 if (local_entry)
1876 batadv_tt_local_entry_free_ref(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001877}
1878
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001879/**
1880 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1881 * given originator matching the provided vid
1882 * @bat_priv: the bat priv with all the soft interface information
1883 * @orig_node: the originator owning the entries to remove
1884 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1885 * removed
1886 * @message: debug message to print as "reason"
1887 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001888void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1889 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001890 s32 match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001891 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001892{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001893 struct batadv_tt_global_entry *tt_global;
1894 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001895 u32 i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001896 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001897 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001898 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001899 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001900 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001901
Simon Wunderlich6e801492011-10-19 10:28:26 +02001902 if (!hash)
1903 return;
1904
Antonio Quartullia73105b2011-04-27 14:27:44 +02001905 for (i = 0; i < hash->size; i++) {
1906 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001907 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001908
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001909 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001910 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001911 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001912 /* remove only matching entries */
1913 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1914 continue;
1915
Sven Eckelmann56303d32012-06-05 22:31:31 +02001916 tt_global = container_of(tt_common_entry,
1917 struct batadv_tt_global_entry,
1918 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001919
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001920 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1921 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001922
Sven Eckelmann56303d32012-06-05 22:31:31 +02001923 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001924 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001925 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001926 "Deleting global tt entry %pM (vid: %d): %s\n",
1927 tt_global->common.addr,
1928 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001929 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001930 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001931 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001932 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001933 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001934 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02001935 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001936}
1937
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001938static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1939 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02001940{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001941 bool purge = false;
1942 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1943 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001944
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001945 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1946 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1947 purge = true;
1948 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001949 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001950
1951 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1952 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1953 purge = true;
1954 *msg = "Temporary client timeout\n";
1955 }
1956
1957 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001958}
1959
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001960static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001961{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001962 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001963 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001964 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001965 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001966 u32 i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001967 char *msg = NULL;
1968 struct batadv_tt_common_entry *tt_common;
1969 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001970
Antonio Quartullicc47f662011-04-27 14:27:57 +02001971 for (i = 0; i < hash->size; i++) {
1972 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001973 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02001974
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001975 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001976 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001977 hash_entry) {
1978 tt_global = container_of(tt_common,
1979 struct batadv_tt_global_entry,
1980 common);
1981
1982 if (!batadv_tt_global_to_purge(tt_global, &msg))
1983 continue;
1984
1985 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001986 "Deleting global tt entry %pM (vid: %d): %s\n",
1987 tt_global->common.addr,
1988 BATADV_PRINT_VID(tt_global->common.vid),
1989 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001990
Sasha Levinb67bfe02013-02-27 17:06:00 -08001991 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001992
1993 batadv_tt_global_entry_free_ref(tt_global);
1994 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001995 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001996 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02001997}
1998
Sven Eckelmann56303d32012-06-05 22:31:31 +02001999static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002000{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002001 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002002 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002003 struct batadv_tt_common_entry *tt_common_entry;
2004 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002005 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002006 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002007 u32 i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002008
Sven Eckelmann807736f2012-07-15 22:26:51 +02002009 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002010 return;
2011
Sven Eckelmann807736f2012-07-15 22:26:51 +02002012 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002013
2014 for (i = 0; i < hash->size; i++) {
2015 head = &hash->table[i];
2016 list_lock = &hash->list_locks[i];
2017
2018 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002019 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002020 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002021 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002022 tt_global = container_of(tt_common_entry,
2023 struct batadv_tt_global_entry,
2024 common);
2025 batadv_tt_global_entry_free_ref(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002026 }
2027 spin_unlock_bh(list_lock);
2028 }
2029
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02002030 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002031
Sven Eckelmann807736f2012-07-15 22:26:51 +02002032 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002033}
2034
Sven Eckelmann56303d32012-06-05 22:31:31 +02002035static bool
2036_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2037 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002038{
2039 bool ret = false;
2040
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002041 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2042 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002043 ret = true;
2044
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002045 /* check if the two clients are marked as isolated */
2046 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2047 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2048 ret = true;
2049
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002050 return ret;
2051}
2052
Antonio Quartullic018ad32013-06-04 12:11:39 +02002053/**
2054 * batadv_transtable_search - get the mesh destination for a given client
2055 * @bat_priv: the bat priv with all the soft interface information
2056 * @src: mac address of the source client
2057 * @addr: mac address of the destination client
2058 * @vid: VLAN identifier
2059 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002060 * Return: a pointer to the originator that was selected as destination in the
Antonio Quartullic018ad32013-06-04 12:11:39 +02002061 * mesh for contacting the client 'addr', NULL otherwise.
2062 * In case of multiple originators serving the same client, the function returns
2063 * the best one (best in terms of metric towards the destination node).
2064 *
2065 * If the two clients are AP isolated the function returns NULL.
2066 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002067struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002068 const u8 *src,
2069 const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002070 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002071{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002072 struct batadv_tt_local_entry *tt_local_entry = NULL;
2073 struct batadv_tt_global_entry *tt_global_entry = NULL;
2074 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002075 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002076
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002077 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002078 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002079 if (!tt_local_entry ||
2080 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002081 goto out;
2082 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002083
Antonio Quartullic018ad32013-06-04 12:11:39 +02002084 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002085 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002086 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002087
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002088 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002089 * isolation
2090 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002091 if (tt_local_entry &&
2092 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002093 goto out;
2094
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002095 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002096 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002097 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002098 if (best_entry)
2099 orig_node = best_entry->orig_node;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002100 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2101 orig_node = NULL;
2102 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002103
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002104out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002105 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002106 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002107 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002108 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002109
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002110 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002111}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002112
Antonio Quartulliced72932013-04-24 16:37:51 +02002113/**
2114 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2115 * to the given orig_node
2116 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002117 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002118 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002119 *
2120 * This function computes the checksum for the global table corresponding to a
2121 * specific originator. In particular, the checksum is computed as follows: For
2122 * each client connected to the originator the CRC32C of the MAC address and the
2123 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2124 * together.
2125 *
2126 * The idea behind is that CRC32C should be used as much as possible in order to
2127 * produce a unique hash of the table, but since the order which is used to feed
2128 * the CRC32C function affects the result and since every node in the network
2129 * probably sorts the clients differently, the hash function cannot be directly
2130 * computed over the entire table. Hence the CRC32C is used only on
2131 * the single client entry, while all the results are then xor'ed together
2132 * because the XOR operation can combine them all while trying to reduce the
2133 * noise as much as possible.
2134 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002135 * Return: the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002136 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002137static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2138 struct batadv_orig_node *orig_node,
2139 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002140{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002141 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002142 struct batadv_tt_common_entry *tt_common;
2143 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002144 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002145 u32 i, crc_tmp, crc = 0;
2146 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002147 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002148
2149 for (i = 0; i < hash->size; i++) {
2150 head = &hash->table[i];
2151
2152 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002153 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002154 tt_global = container_of(tt_common,
2155 struct batadv_tt_global_entry,
2156 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002157 /* compute the CRC only for entries belonging to the
2158 * VLAN identified by the vid passed as parameter
2159 */
2160 if (tt_common->vid != vid)
2161 continue;
2162
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002163 /* Roaming clients are in the global table for
2164 * consistency only. They don't have to be
2165 * taken into account while computing the
2166 * global crc
2167 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002168 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002169 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002170 /* Temporary clients have not been announced yet, so
2171 * they have to be skipped while computing the global
2172 * crc
2173 */
2174 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2175 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002176
2177 /* find out if this global entry is announced by this
2178 * originator
2179 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002180 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002181 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002182 continue;
2183
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002184 /* use network order to read the VID: this ensures that
2185 * every node reads the bytes in the same order.
2186 */
2187 tmp_vid = htons(tt_common->vid);
2188 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002189
2190 /* compute the CRC on flags that have to be kept in sync
2191 * among nodes
2192 */
2193 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2194 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2195
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002196 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002197 }
2198 rcu_read_unlock();
2199 }
2200
Antonio Quartulliced72932013-04-24 16:37:51 +02002201 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002202}
2203
Antonio Quartulliced72932013-04-24 16:37:51 +02002204/**
2205 * batadv_tt_local_crc - calculates the checksum of the local table
2206 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002207 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002208 *
2209 * For details about the computation, please refer to the documentation for
2210 * batadv_tt_global_crc().
2211 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002212 * Return: the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002213 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002214static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2215 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002216{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002217 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002218 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002219 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002220 u32 i, crc_tmp, crc = 0;
2221 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002222 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002223
2224 for (i = 0; i < hash->size; i++) {
2225 head = &hash->table[i];
2226
2227 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002228 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002229 /* compute the CRC only for entries belonging to the
2230 * VLAN identified by vid
2231 */
2232 if (tt_common->vid != vid)
2233 continue;
2234
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002235 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002236 * account while computing the CRC
2237 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002238 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002239 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002240
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002241 /* use network order to read the VID: this ensures that
2242 * every node reads the bytes in the same order.
2243 */
2244 tmp_vid = htons(tt_common->vid);
2245 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002246
2247 /* compute the CRC on flags that have to be kept in sync
2248 * among nodes
2249 */
2250 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2251 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2252
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002253 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002254 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002255 rcu_read_unlock();
2256 }
2257
Antonio Quartulliced72932013-04-24 16:37:51 +02002258 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002259}
2260
Sven Eckelmann56303d32012-06-05 22:31:31 +02002261static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002262{
Marek Lindner7c26a532015-06-28 22:16:06 +08002263 struct batadv_tt_req_node *node;
2264 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002265
Sven Eckelmann807736f2012-07-15 22:26:51 +02002266 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002267
Marek Lindner7c26a532015-06-28 22:16:06 +08002268 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2269 hlist_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002270 kfree(node);
2271 }
2272
Sven Eckelmann807736f2012-07-15 22:26:51 +02002273 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002274}
2275
Sven Eckelmann56303d32012-06-05 22:31:31 +02002276static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2277 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002278 const void *tt_buff,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002279 u16 tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002280{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002281 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002282 * last OGM (the OGM could carry no changes)
2283 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002284 spin_lock_bh(&orig_node->tt_buff_lock);
2285 if (tt_buff_len > 0) {
2286 kfree(orig_node->tt_buff);
2287 orig_node->tt_buff_len = 0;
2288 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2289 if (orig_node->tt_buff) {
2290 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2291 orig_node->tt_buff_len = tt_buff_len;
2292 }
2293 }
2294 spin_unlock_bh(&orig_node->tt_buff_lock);
2295}
2296
Sven Eckelmann56303d32012-06-05 22:31:31 +02002297static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002298{
Marek Lindner7c26a532015-06-28 22:16:06 +08002299 struct batadv_tt_req_node *node;
2300 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002301
Sven Eckelmann807736f2012-07-15 22:26:51 +02002302 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002303 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002304 if (batadv_has_timed_out(node->issued_at,
2305 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner7c26a532015-06-28 22:16:06 +08002306 hlist_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002307 kfree(node);
2308 }
2309 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002310 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002311}
2312
Marek Lindner383b8632015-06-18 16:24:24 +08002313/**
2314 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2315 * @bat_priv: the bat priv with all the soft interface information
2316 * @orig_node: orig node this request is being issued for
2317 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002318 * Return: the pointer to the new tt_req_node struct if no request
Marek Lindner383b8632015-06-18 16:24:24 +08002319 * has already been issued for this orig_node, NULL otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002320 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002321static struct batadv_tt_req_node *
Marek Lindner383b8632015-06-18 16:24:24 +08002322batadv_tt_req_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002323 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002324{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002325 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002326
Sven Eckelmann807736f2012-07-15 22:26:51 +02002327 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002328 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002329 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2330 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002331 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002332 goto unlock;
2333 }
2334
2335 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2336 if (!tt_req_node)
2337 goto unlock;
2338
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002339 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002340 tt_req_node->issued_at = jiffies;
2341
Marek Lindner7c26a532015-06-28 22:16:06 +08002342 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002343unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002344 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002345 return tt_req_node;
2346}
2347
Marek Lindner335fbe02013-04-23 21:40:02 +08002348/**
2349 * batadv_tt_local_valid - verify that given tt entry is a valid one
2350 * @entry_ptr: to be checked local tt entry
2351 * @data_ptr: not used but definition required to satisfy the callback prototype
2352 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002353 * Return: 1 if the entry is a valid, 0 otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002354 */
2355static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002356{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002357 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002358
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002359 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002360 return 0;
2361 return 1;
2362}
2363
Sven Eckelmanna5130882012-05-16 20:23:16 +02002364static int batadv_tt_global_valid(const void *entry_ptr,
2365 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002366{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002367 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2368 const struct batadv_tt_global_entry *tt_global_entry;
2369 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002370
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002371 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2372 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002373 return 0;
2374
Sven Eckelmann56303d32012-06-05 22:31:31 +02002375 tt_global_entry = container_of(tt_common_entry,
2376 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002377 common);
2378
Sven Eckelmanna5130882012-05-16 20:23:16 +02002379 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002380}
2381
Marek Lindner335fbe02013-04-23 21:40:02 +08002382/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002383 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2384 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002385 * @bat_priv: the bat priv with all the soft interface information
2386 * @hash: hash table containing the tt entries
2387 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002388 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002389 * @valid_cb: function to filter tt change entries
2390 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002391 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002392static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2393 struct batadv_hashtable *hash,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002394 void *tvlv_buff, u16 tt_len,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002395 int (*valid_cb)(const void *, const void *),
2396 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002397{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002398 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002399 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002400 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002401 u16 tt_tot, tt_num_entries = 0;
2402 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002403
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002404 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002405 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002406
2407 rcu_read_lock();
2408 for (i = 0; i < hash->size; i++) {
2409 head = &hash->table[i];
2410
Sasha Levinb67bfe02013-02-27 17:06:00 -08002411 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002412 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002413 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002414 break;
2415
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002416 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002417 continue;
2418
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002419 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002420 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002421 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002422 memset(tt_change->reserved, 0,
2423 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002424
Marek Lindner335fbe02013-04-23 21:40:02 +08002425 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002426 tt_change++;
2427 }
2428 }
2429 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002430}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002431
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002432/**
2433 * batadv_tt_global_check_crc - check if all the CRCs are correct
2434 * @orig_node: originator for which the CRCs have to be checked
2435 * @tt_vlan: pointer to the first tvlv VLAN entry
2436 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002437 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002438 * Return: true if all the received CRCs match the locally stored ones, false
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002439 * otherwise
2440 */
2441static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2442 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002443 u16 num_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002444{
2445 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2446 struct batadv_orig_node_vlan *vlan;
Simon Wunderlichc169c592015-09-02 20:09:56 +02002447 int i, orig_num_vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002448 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002449
2450 /* check if each received CRC matches the locally stored one */
2451 for (i = 0; i < num_vlan; i++) {
2452 tt_vlan_tmp = tt_vlan + i;
2453
2454 /* if orig_node is a backbone node for this VLAN, don't check
2455 * the CRC as we ignore all the global entries over it
2456 */
2457 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002458 orig_node->orig,
2459 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002460 continue;
2461
2462 vlan = batadv_orig_node_vlan_get(orig_node,
2463 ntohs(tt_vlan_tmp->vid));
2464 if (!vlan)
2465 return false;
2466
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002467 crc = vlan->tt.crc;
2468 batadv_orig_node_vlan_free_ref(vlan);
2469
2470 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002471 return false;
2472 }
2473
Simon Wunderlichc169c592015-09-02 20:09:56 +02002474 /* check if any excess VLANs exist locally for the originator
2475 * which are not mentioned in the TVLV from the originator.
2476 */
2477 rcu_read_lock();
2478 orig_num_vlan = 0;
2479 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2480 orig_num_vlan++;
2481 rcu_read_unlock();
2482
2483 if (orig_num_vlan > num_vlan)
2484 return false;
2485
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002486 return true;
2487}
2488
2489/**
2490 * batadv_tt_local_update_crc - update all the local CRCs
2491 * @bat_priv: the bat priv with all the soft interface information
2492 */
2493static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2494{
2495 struct batadv_softif_vlan *vlan;
2496
2497 /* recompute the global CRC for each VLAN */
2498 rcu_read_lock();
2499 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2500 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2501 }
2502 rcu_read_unlock();
2503}
2504
2505/**
2506 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2507 * @bat_priv: the bat priv with all the soft interface information
2508 * @orig_node: the orig_node for which the CRCs have to be updated
2509 */
2510static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2511 struct batadv_orig_node *orig_node)
2512{
2513 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002514 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002515
2516 /* recompute the global CRC for each VLAN */
2517 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08002518 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002519 /* if orig_node is a backbone node for this VLAN, don't compute
2520 * the CRC as we ignore all the global entries over it
2521 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002522 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2523 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002524 continue;
2525
2526 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2527 vlan->tt.crc = crc;
2528 }
2529 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002530}
2531
Antonio Quartulliced72932013-04-24 16:37:51 +02002532/**
2533 * batadv_send_tt_request - send a TT Request message to a given node
2534 * @bat_priv: the bat priv with all the soft interface information
2535 * @dst_orig_node: the destination of the message
2536 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002537 * @tt_vlan: pointer to the first tvlv VLAN object to request
2538 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002539 * @full_table: ask for the entire translation table if true, while only for the
2540 * last TT diff otherwise
Antonio Quartullid15cd622015-11-17 16:40:52 +08002541 *
2542 * Return: true if the TT Request was sent, false otherwise
Antonio Quartulliced72932013-04-24 16:37:51 +02002543 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002544static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2545 struct batadv_orig_node *dst_orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002546 u8 ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002547 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002548 u16 num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002549{
Marek Lindner335fbe02013-04-23 21:40:02 +08002550 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002551 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002552 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2553 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002554 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002555 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002556
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002557 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002558 if (!primary_if)
2559 goto out;
2560
2561 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002562 * reply from the same orig_node yet
2563 */
Marek Lindner383b8632015-06-18 16:24:24 +08002564 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002565 if (!tt_req_node)
2566 goto out;
2567
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002568 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2569 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002570 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002571 goto out;
2572
Marek Lindner335fbe02013-04-23 21:40:02 +08002573 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2574 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002575 tvlv_tt_data->num_vlan = htons(num_vlan);
2576
2577 /* send all the CRCs within the request. This is needed by intermediate
2578 * nodes to ensure they have the correct table before replying
2579 */
2580 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2581 for (i = 0; i < num_vlan; i++) {
2582 tt_vlan_req->vid = tt_vlan->vid;
2583 tt_vlan_req->crc = tt_vlan->crc;
2584
2585 tt_vlan_req++;
2586 tt_vlan++;
2587 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002588
2589 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002590 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002591
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002592 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002593 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002594
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002595 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002596 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2597 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002598 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002599 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002600
2601out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002602 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002603 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002604 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002605 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002606 /* hlist_del_init() verifies tt_req_node still is in the list */
2607 hlist_del_init(&tt_req_node->list);
Sven Eckelmann807736f2012-07-15 22:26:51 +02002608 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002609 kfree(tt_req_node);
2610 }
Marek Lindner335fbe02013-04-23 21:40:02 +08002611 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002612 return ret;
2613}
2614
Marek Lindner335fbe02013-04-23 21:40:02 +08002615/**
2616 * batadv_send_other_tt_response - send reply to tt request concerning another
2617 * node's translation table
2618 * @bat_priv: the bat priv with all the soft interface information
2619 * @tt_data: tt data containing the tt request information
2620 * @req_src: mac address of tt request sender
2621 * @req_dst: mac address of tt request recipient
2622 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002623 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002624 */
2625static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2626 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002627 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002628{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002629 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002630 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002631 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002632 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002633 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002634 bool ret = false, full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002635 u8 orig_ttvn, req_ttvn;
2636 u16 tvlv_len;
2637 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002638
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002639 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002640 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002641 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002642 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002643
2644 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002645 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002646 if (!req_dst_orig_node)
2647 goto out;
2648
Marek Lindner335fbe02013-04-23 21:40:02 +08002649 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002650 if (!res_dst_orig_node)
2651 goto out;
2652
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002653 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002654 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002655
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002656 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002657 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002658 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002659 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2660 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661 goto out;
2662
Antonio Quartulli015758d2011-07-09 17:52:13 +02002663 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002664 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002665 !req_dst_orig_node->tt_buff)
2666 full_table = true;
2667 else
2668 full_table = false;
2669
Marek Lindner335fbe02013-04-23 21:40:02 +08002670 /* TT fragmentation hasn't been implemented yet, so send as many
2671 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002672 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002673 if (!full_table) {
2674 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2675 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002676
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002677 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2678 &tvlv_tt_data,
2679 &tt_change,
2680 &tt_len);
2681 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002682 goto unlock;
2683
Antonio Quartullia73105b2011-04-27 14:27:44 +02002684 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002685 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002686 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002687 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2688 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002689 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2690 * in the initial part
2691 */
2692 tt_len = -1;
2693 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2694 &tvlv_tt_data,
2695 &tt_change,
2696 &tt_len);
2697 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002698 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002699
2700 /* fill the rest of the tvlv with the real TT entries */
2701 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2702 tt_change, tt_len,
2703 batadv_tt_global_valid,
2704 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002705 }
2706
Marek Lindnera19d3d82013-05-27 15:33:25 +08002707 /* Don't send the response, if larger than fragmented packet. */
2708 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2709 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2710 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2711 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2712 res_dst_orig_node->orig);
2713 goto out;
2714 }
2715
Marek Lindner335fbe02013-04-23 21:40:02 +08002716 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2717 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002718
2719 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002720 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002721
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002722 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002723 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2724 res_dst_orig_node->orig, req_dst_orig_node->orig,
2725 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002726
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002727 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002728
Marek Lindner335fbe02013-04-23 21:40:02 +08002729 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002730 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2731 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002732
Marek Lindner335fbe02013-04-23 21:40:02 +08002733 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002734 goto out;
2735
2736unlock:
2737 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2738
2739out:
2740 if (res_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002741 batadv_orig_node_free_ref(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002742 if (req_dst_orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002743 batadv_orig_node_free_ref(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002744 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002745 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002746}
Sven Eckelmann96412692012-06-05 22:31:30 +02002747
Marek Lindner335fbe02013-04-23 21:40:02 +08002748/**
2749 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2750 * translation table
2751 * @bat_priv: the bat priv with all the soft interface information
2752 * @tt_data: tt data containing the tt request information
2753 * @req_src: mac address of tt request sender
2754 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002755 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002756 */
2757static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2758 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002759 u8 *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002760{
Marek Lindner335fbe02013-04-23 21:40:02 +08002761 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002762 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002763 struct batadv_tvlv_tt_change *tt_change;
2764 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002765 u8 my_ttvn, req_ttvn;
2766 u16 tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002767 bool full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002768 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002769
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002770 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002771 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002772 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002773 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002774
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002775 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002776
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002777 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002778 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002779
Marek Lindner335fbe02013-04-23 21:40:02 +08002780 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002781 if (!orig_node)
2782 goto out;
2783
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002784 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002785 if (!primary_if)
2786 goto out;
2787
2788 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002789 * is too big send the whole local translation table
2790 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002791 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002792 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002793 full_table = true;
2794 else
2795 full_table = false;
2796
Marek Lindner335fbe02013-04-23 21:40:02 +08002797 /* TT fragmentation hasn't been implemented yet, so send as many
2798 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002799 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002800 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002801 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002802
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002803 tt_len = bat_priv->tt.last_changeset_len;
2804 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2805 &tvlv_tt_data,
2806 &tt_change,
2807 &tt_len);
2808 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002809 goto unlock;
2810
Marek Lindner335fbe02013-04-23 21:40:02 +08002811 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002812 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002813 bat_priv->tt.last_changeset_len);
2814 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002815 } else {
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002816 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002817
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002818 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2819 * in the initial part
2820 */
2821 tt_len = -1;
2822 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2823 &tvlv_tt_data,
2824 &tt_change,
2825 &tt_len);
2826 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002827 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002828
2829 /* fill the rest of the tvlv with the real TT entries */
2830 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2831 tt_change, tt_len,
2832 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002833 }
2834
Marek Lindner335fbe02013-04-23 21:40:02 +08002835 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2836 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002837
2838 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002839 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002840
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002841 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002842 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2843 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002844
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002845 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002846
Marek Lindner335fbe02013-04-23 21:40:02 +08002847 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002848 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2849 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002850
Antonio Quartullia73105b2011-04-27 14:27:44 +02002851 goto out;
2852
2853unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002854 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002855out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002856 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002857 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002858 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002859 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002860 batadv_hardif_free_ref(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002861 kfree(tvlv_tt_data);
2862 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002863 return true;
2864}
2865
Marek Lindner335fbe02013-04-23 21:40:02 +08002866/**
2867 * batadv_send_tt_response - send reply to tt request
2868 * @bat_priv: the bat priv with all the soft interface information
2869 * @tt_data: tt data containing the tt request information
2870 * @req_src: mac address of tt request sender
2871 * @req_dst: mac address of tt request recipient
2872 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002873 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002874 */
2875static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2876 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002877 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002878{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002879 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002880 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002881 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2882 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002883}
2884
Sven Eckelmann56303d32012-06-05 22:31:31 +02002885static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2886 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002887 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002888 u16 tt_num_changes, u8 ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002889{
2890 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002891 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002892
2893 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002894 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2895 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002896 batadv_tt_global_del(bat_priv, orig_node,
2897 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002898 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002899 "tt removed by changes",
2900 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002901 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002902 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002903 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002904 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002905 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002906 /* In case of problem while storing a
2907 * global_entry, we stop the updating
2908 * procedure without committing the
2909 * ttvn change. This will avoid to send
2910 * corrupted data on tt_request
2911 */
2912 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002913 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002914 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02002915 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002916}
2917
Sven Eckelmann56303d32012-06-05 22:31:31 +02002918static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002919 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002920 u8 ttvn, u8 *resp_src,
2921 u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002922{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002923 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002924
Marek Lindner335fbe02013-04-23 21:40:02 +08002925 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002926 if (!orig_node)
2927 goto out;
2928
2929 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02002930 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2931 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02002932
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002933 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2934 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002935
2936 spin_lock_bh(&orig_node->tt_buff_lock);
2937 kfree(orig_node->tt_buff);
2938 orig_node->tt_buff_len = 0;
2939 orig_node->tt_buff = NULL;
2940 spin_unlock_bh(&orig_node->tt_buff_lock);
2941
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002942 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002943
2944out:
2945 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02002946 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002947}
2948
Sven Eckelmann56303d32012-06-05 22:31:31 +02002949static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2950 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002951 u16 tt_num_changes, u8 ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08002952 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002953{
Sven Eckelmanna5130882012-05-16 20:23:16 +02002954 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2955 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002956
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002957 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2958 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002959 atomic_set(&orig_node->last_ttvn, ttvn);
2960}
2961
Antonio Quartullic018ad32013-06-04 12:11:39 +02002962/**
2963 * batadv_is_my_client - check if a client is served by the local node
2964 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01002965 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02002966 * @vid: VLAN identifier
2967 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002968 * Return: true if the client is served by this node, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +02002969 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002970bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002971 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002972{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002973 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002974 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002975
Antonio Quartullic018ad32013-06-04 12:11:39 +02002976 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002977 if (!tt_local_entry)
2978 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002979 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002980 * consistency purpose)
2981 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02002982 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2983 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002984 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002985 ret = true;
2986out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002987 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02002988 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002989 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002990}
2991
Marek Lindner335fbe02013-04-23 21:40:02 +08002992/**
2993 * batadv_handle_tt_response - process incoming tt reply
2994 * @bat_priv: the bat priv with all the soft interface information
2995 * @tt_data: tt data containing the tt request information
2996 * @resp_src: mac address of tt reply sender
2997 * @num_entries: number of tt change entries appended to the tt data
2998 */
2999static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3000 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003001 u8 *resp_src, u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003002{
Marek Lindner7c26a532015-06-28 22:16:06 +08003003 struct batadv_tt_req_node *node;
3004 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003005 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08003006 struct batadv_tvlv_tt_change *tt_change;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003007 u8 *tvlv_ptr = (u8 *)tt_data;
3008 u16 change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003009
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003010 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003011 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003012 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003013 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003014
Marek Lindner335fbe02013-04-23 21:40:02 +08003015 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003016 if (!orig_node)
3017 goto out;
3018
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003019 spin_lock_bh(&orig_node->tt_lock);
3020
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003021 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3022 change_offset *= ntohs(tt_data->num_vlan);
3023 change_offset += sizeof(*tt_data);
3024 tvlv_ptr += change_offset;
3025
3026 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08003027 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003028 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3029 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02003030 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08003031 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3032 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02003033 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003034
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003035 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003036 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003037
3038 spin_unlock_bh(&orig_node->tt_lock);
3039
Antonio Quartullia73105b2011-04-27 14:27:44 +02003040 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003041 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08003042 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08003043 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003044 continue;
Marek Lindner7c26a532015-06-28 22:16:06 +08003045 hlist_del_init(&node->list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003046 kfree(node);
3047 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003048
Sven Eckelmann807736f2012-07-15 22:26:51 +02003049 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003050out:
3051 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02003052 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003053}
3054
Sven Eckelmann56303d32012-06-05 22:31:31 +02003055static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003056{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003057 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003058
Sven Eckelmann807736f2012-07-15 22:26:51 +02003059 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003060
Sven Eckelmann807736f2012-07-15 22:26:51 +02003061 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02003062 list_del(&node->list);
3063 kfree(node);
3064 }
3065
Sven Eckelmann807736f2012-07-15 22:26:51 +02003066 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003067}
3068
Sven Eckelmann56303d32012-06-05 22:31:31 +02003069static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003070{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003071 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003072
Sven Eckelmann807736f2012-07-15 22:26:51 +02003073 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3074 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003075 if (!batadv_has_timed_out(node->first_time,
3076 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003077 continue;
3078
3079 list_del(&node->list);
3080 kfree(node);
3081 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003082 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003083}
3084
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003085/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08003086 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3087 * @bat_priv: the bat priv with all the soft interface information
3088 * @client: mac address of the roaming client
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003089 *
3090 * This function checks whether the client already reached the
Antonio Quartullicc47f662011-04-27 14:27:57 +02003091 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3092 * will not be sent.
3093 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003094 * Return: true if the ROAMING_ADV can be sent, false otherwise
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003095 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003096static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003097{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003098 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003099 bool ret = false;
3100
Sven Eckelmann807736f2012-07-15 22:26:51 +02003101 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003102 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003103 * reply from the same orig_node yet
3104 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003105 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003106 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003107 continue;
3108
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003109 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003110 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003111 continue;
3112
Sven Eckelmann3e348192012-05-16 20:23:22 +02003113 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003114 /* Sorry, you roamed too many times! */
3115 goto unlock;
3116 ret = true;
3117 break;
3118 }
3119
3120 if (!ret) {
3121 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3122 if (!tt_roam_node)
3123 goto unlock;
3124
3125 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003126 atomic_set(&tt_roam_node->counter,
3127 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003128 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003129
Sven Eckelmann807736f2012-07-15 22:26:51 +02003130 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003131 ret = true;
3132 }
3133
3134unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003135 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003136 return ret;
3137}
3138
Antonio Quartullic018ad32013-06-04 12:11:39 +02003139/**
3140 * batadv_send_roam_adv - send a roaming advertisement message
3141 * @bat_priv: the bat priv with all the soft interface information
3142 * @client: mac address of the roaming client
3143 * @vid: VLAN identifier
3144 * @orig_node: message destination
3145 *
3146 * Send a ROAMING_ADV message to the node which was previously serving this
3147 * client. This is done to inform the node that from now on all traffic destined
3148 * for this particular roamed client has to be forwarded to the sender of the
3149 * roaming message.
3150 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003151static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003152 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003153 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003154{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003155 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003156 struct batadv_tvlv_roam_adv tvlv_roam;
3157
3158 primary_if = batadv_primary_if_get_selected(bat_priv);
3159 if (!primary_if)
3160 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003161
3162 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003163 * already roamed to us too many times
3164 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003165 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003166 goto out;
3167
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003168 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003169 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3170 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003171
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003172 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003173
Marek Lindner122edaa2013-04-23 21:40:03 +08003174 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003175 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003176
3177 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3178 orig_node->orig, BATADV_TVLV_ROAM, 1,
3179 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003180
3181out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003182 if (primary_if)
3183 batadv_hardif_free_ref(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003184}
3185
Sven Eckelmanna5130882012-05-16 20:23:16 +02003186static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003187{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003188 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003189 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003190 struct batadv_priv *bat_priv;
3191
3192 delayed_work = container_of(work, struct delayed_work, work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003193 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3194 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003195
Marek Lindnera19d3d82013-05-27 15:33:25 +08003196 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003197 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003198 batadv_tt_req_purge(bat_priv);
3199 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003200
Antonio Quartulli72414442012-12-25 13:14:37 +01003201 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3202 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003203}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003204
Sven Eckelmann56303d32012-06-05 22:31:31 +02003205void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003206{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003207 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3208 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3209
Sven Eckelmann807736f2012-07-15 22:26:51 +02003210 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003211
Sven Eckelmanna5130882012-05-16 20:23:16 +02003212 batadv_tt_local_table_free(bat_priv);
3213 batadv_tt_global_table_free(bat_priv);
3214 batadv_tt_req_list_free(bat_priv);
3215 batadv_tt_changes_list_free(bat_priv);
3216 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003217
Sven Eckelmann807736f2012-07-15 22:26:51 +02003218 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003219}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003220
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003221/**
3222 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3223 * table and possibly count them in the TT size
3224 * @bat_priv: the bat priv with all the soft interface information
3225 * @flags: the flag to switch
3226 * @enable: whether to set or unset the flag
3227 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003228 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003229static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3230 bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003231{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003232 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3233 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003234 u16 changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003235 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003236 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003237
3238 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003239 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003240
3241 for (i = 0; i < hash->size; i++) {
3242 head = &hash->table[i];
3243
3244 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003245 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003246 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003247 if (enable) {
3248 if ((tt_common_entry->flags & flags) == flags)
3249 continue;
3250 tt_common_entry->flags |= flags;
3251 } else {
3252 if (!(tt_common_entry->flags & flags))
3253 continue;
3254 tt_common_entry->flags &= ~flags;
3255 }
3256 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003257
3258 if (!count)
3259 continue;
3260
3261 batadv_tt_local_size_inc(bat_priv,
3262 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003263 }
3264 rcu_read_unlock();
3265 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003266}
3267
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003268/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003269static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003270{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003271 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003272 struct batadv_tt_common_entry *tt_common;
3273 struct batadv_tt_local_entry *tt_local;
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003274 struct batadv_softif_vlan *vlan;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003275 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003276 struct hlist_head *head;
3277 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003278 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003279
3280 if (!hash)
3281 return;
3282
3283 for (i = 0; i < hash->size; i++) {
3284 head = &hash->table[i];
3285 list_lock = &hash->list_locks[i];
3286
3287 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003288 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003289 hash_entry) {
3290 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003291 continue;
3292
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003293 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003294 "Deleting local tt entry (%pM, vid: %d): pending\n",
3295 tt_common->addr,
3296 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003297
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003298 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003299 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003300 tt_local = container_of(tt_common,
3301 struct batadv_tt_local_entry,
3302 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003303
3304 /* decrease the reference held for this vlan */
3305 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
Marek Lindner354136b2015-06-09 21:24:36 +08003306 if (vlan) {
3307 batadv_softif_vlan_free_ref(vlan);
3308 batadv_softif_vlan_free_ref(vlan);
3309 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003310
Sven Eckelmann56303d32012-06-05 22:31:31 +02003311 batadv_tt_local_entry_free_ref(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003312 }
3313 spin_unlock_bh(list_lock);
3314 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003315}
3316
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003317/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003318 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3319 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003320 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003321 *
3322 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003323 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003324static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003325{
Sven Eckelmann5274cd62015-06-21 14:45:15 +02003326 lockdep_assert_held(&bat_priv->tt.commit_lock);
3327
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003328 /* Update multicast addresses in local translation table */
3329 batadv_mcast_mla_update(bat_priv);
3330
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003331 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3332 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3333 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003334 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003335 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003336
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003337 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003338
Sven Eckelmanna5130882012-05-16 20:23:16 +02003339 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003340 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003341
3342 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003343 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003344 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003345 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003346 (u8)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003347
3348 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003349 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003350 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003351}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003352
Marek Lindnera19d3d82013-05-27 15:33:25 +08003353/**
3354 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3355 * have been queued in the time since the last commit
3356 * @bat_priv: the bat priv with all the soft interface information
3357 */
3358void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3359{
3360 spin_lock_bh(&bat_priv->tt.commit_lock);
3361 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003362 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003363}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003364
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003365bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3366 unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003367{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003368 struct batadv_tt_local_entry *tt_local_entry = NULL;
3369 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003370 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003371 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003372
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003373 vlan = batadv_softif_vlan_get(bat_priv, vid);
Markus Elfringe087f342015-11-03 19:20:34 +01003374 if (!vlan)
3375 return false;
3376
3377 if (!atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003378 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003379
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003380 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003381 if (!tt_local_entry)
3382 goto out;
3383
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003384 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003385 if (!tt_global_entry)
3386 goto out;
3387
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003388 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003389 goto out;
3390
Marek Lindner5870adc2012-06-20 17:16:05 +02003391 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003392
3393out:
Markus Elfringf75a33a2015-11-03 19:20:34 +01003394 batadv_softif_vlan_free_ref(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003395 if (tt_global_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003396 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003397 if (tt_local_entry)
Sven Eckelmanna5130882012-05-16 20:23:16 +02003398 batadv_tt_local_entry_free_ref(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003399 return ret;
3400}
Marek Lindnera943cac2011-07-30 13:10:18 +02003401
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003402/**
3403 * batadv_tt_update_orig - update global translation table with new tt
3404 * information received via ogms
3405 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmanne51f0392015-09-06 21:38:51 +02003406 * @orig_node: the orig_node of the ogm
3407 * @tt_buff: pointer to the first tvlv VLAN entry
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003408 * @tt_num_vlan: number of tvlv VLAN entries
3409 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003410 * @tt_num_changes: number of tt changes inside the tt buffer
3411 * @ttvn: translation table version number of this changeset
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003412 */
3413static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3414 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003415 const void *tt_buff, u16 tt_num_vlan,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003416 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003417 u16 tt_num_changes, u8 ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003418{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003419 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003420 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003421 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003422 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003423
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003424 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003425 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3426 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003427
Antonio Quartulli17071572011-11-07 16:36:40 +01003428 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003429 * increased by one -> we can apply the attached changes
3430 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003431 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003432 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003433 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3434 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003435 * In this case send a tt request
3436 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003437 if (!tt_num_changes) {
3438 full_table = false;
3439 goto request_table;
3440 }
3441
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003442 spin_lock_bh(&orig_node->tt_lock);
3443
Sven Eckelmanna5130882012-05-16 20:23:16 +02003444 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003445 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003446
3447 /* Even if we received the precomputed crc with the OGM, we
3448 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003449 * in the global table
3450 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003451 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003452
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003453 spin_unlock_bh(&orig_node->tt_lock);
3454
Marek Lindnera943cac2011-07-30 13:10:18 +02003455 /* The ttvn alone is not enough to guarantee consistency
3456 * because a single value could represent different states
3457 * (due to the wrap around). Thus a node has to check whether
3458 * the resulting table (after applying the changes) is still
3459 * consistent or not. E.g. a node could disconnect while its
3460 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3461 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003462 * inconsistency
3463 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003464 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3465 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003466 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003467 } else {
3468 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003469 * in sync anymore -> request fresh tt data
3470 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003471 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003472 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3473 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003474request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003475 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003476 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3477 orig_node->orig, ttvn, orig_ttvn,
3478 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003479 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003480 tt_vlan, tt_num_vlan,
3481 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003482 return;
3483 }
3484 }
3485}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003486
Antonio Quartullic018ad32013-06-04 12:11:39 +02003487/**
3488 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3489 * @bat_priv: the bat priv with all the soft interface information
3490 * @addr: the mac address of the client to check
3491 * @vid: VLAN identifier
3492 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003493 * Return: true if we know that the client has moved from its old originator
Antonio Quartullic018ad32013-06-04 12:11:39 +02003494 * to another one. This entry is still kept for consistency purposes and will be
3495 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003496 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003497bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003498 u8 *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003499{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003500 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003501 bool ret = false;
3502
Antonio Quartullic018ad32013-06-04 12:11:39 +02003503 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003504 if (!tt_global_entry)
3505 goto out;
3506
Antonio Quartullic1d07432013-01-15 22:17:19 +10003507 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02003508 batadv_tt_global_entry_free_ref(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003509out:
3510 return ret;
3511}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003512
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003513/**
3514 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3515 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003516 * @addr: the mac address of the local client to query
3517 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003518 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003519 * Return: true if the local client is known to be roaming (it is not served by
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003520 * this node anymore) or not. If yes, the client is still present in the table
3521 * to keep the latter consistent with the node TTVN
3522 */
3523bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003524 u8 *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003525{
3526 struct batadv_tt_local_entry *tt_local_entry;
3527 bool ret = false;
3528
Antonio Quartullic018ad32013-06-04 12:11:39 +02003529 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003530 if (!tt_local_entry)
3531 goto out;
3532
3533 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3534 batadv_tt_local_entry_free_ref(tt_local_entry);
3535out:
3536 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003537}
3538
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003539bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3540 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003541 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003542 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003543{
3544 bool ret = false;
3545
Antonio Quartulli16052782013-06-04 12:11:41 +02003546 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003547 BATADV_TT_CLIENT_TEMP,
3548 atomic_read(&orig_node->last_ttvn)))
3549 goto out;
3550
3551 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003552 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3553 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003554 ret = true;
3555out:
3556 return ret;
3557}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003558
3559/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003560 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3561 * maximum packet size that can be transported through the mesh
3562 * @soft_iface: netdev struct of the mesh interface
3563 *
3564 * Remove entries older than 'timeout' and half timeout if more entries need
3565 * to be removed.
3566 */
3567void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3568{
3569 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3570 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3571 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3572 bool reduced = false;
3573
3574 spin_lock_bh(&bat_priv->tt.commit_lock);
3575
3576 while (true) {
3577 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3578 if (packet_size_max >= table_size)
3579 break;
3580
3581 batadv_tt_local_purge(bat_priv, timeout);
3582 batadv_tt_local_purge_pending_clients(bat_priv);
3583
3584 timeout /= 2;
3585 reduced = true;
3586 net_ratelimited_function(batadv_info, soft_iface,
3587 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3588 packet_size_max);
3589 }
3590
3591 /* commit these changes immediately, to avoid synchronization problem
3592 * with the TTVN
3593 */
3594 if (reduced)
3595 batadv_tt_local_commit_changes_nolock(bat_priv);
3596
3597 spin_unlock_bh(&bat_priv->tt.commit_lock);
3598}
3599
3600/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003601 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3602 * @bat_priv: the bat priv with all the soft interface information
3603 * @orig: the orig_node of the ogm
3604 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3605 * @tvlv_value: tvlv buffer containing the gateway data
3606 * @tvlv_value_len: tvlv buffer length
3607 */
3608static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3609 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003610 u8 flags, void *tvlv_value,
3611 u16 tvlv_value_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003612{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003613 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3614 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003615 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003616 u16 num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003617
3618 if (tvlv_value_len < sizeof(*tt_data))
3619 return;
3620
3621 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3622 tvlv_value_len -= sizeof(*tt_data);
3623
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003624 num_vlan = ntohs(tt_data->num_vlan);
3625
3626 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3627 return;
3628
3629 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3630 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3631 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3632
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003633 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003634
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003635 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3636 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003637}
3638
3639/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003640 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3641 * container
3642 * @bat_priv: the bat priv with all the soft interface information
3643 * @src: mac address of tt tvlv sender
3644 * @dst: mac address of tt tvlv recipient
3645 * @tvlv_value: tvlv buffer containing the tt data
3646 * @tvlv_value_len: tvlv buffer length
3647 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003648 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner335fbe02013-04-23 21:40:02 +08003649 * otherwise.
3650 */
3651static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003652 u8 *src, u8 *dst,
Marek Lindner335fbe02013-04-23 21:40:02 +08003653 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003654 u16 tvlv_value_len)
Marek Lindner335fbe02013-04-23 21:40:02 +08003655{
3656 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003657 u16 tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003658 char tt_flag;
3659 bool ret;
3660
3661 if (tvlv_value_len < sizeof(*tt_data))
3662 return NET_RX_SUCCESS;
3663
3664 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3665 tvlv_value_len -= sizeof(*tt_data);
3666
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003667 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3668 tt_vlan_len *= ntohs(tt_data->num_vlan);
3669
3670 if (tvlv_value_len < tt_vlan_len)
3671 return NET_RX_SUCCESS;
3672
3673 tvlv_value_len -= tt_vlan_len;
3674 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003675
3676 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3677 case BATADV_TT_REQUEST:
3678 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3679
3680 /* If this node cannot provide a TT response the tt_request is
3681 * forwarded
3682 */
3683 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3684 if (!ret) {
3685 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3686 tt_flag = 'F';
3687 else
3688 tt_flag = '.';
3689
3690 batadv_dbg(BATADV_DBG_TT, bat_priv,
3691 "Routing TT_REQUEST to %pM [%c]\n",
3692 dst, tt_flag);
3693 /* tvlv API will re-route the packet */
3694 return NET_RX_DROP;
3695 }
3696 break;
3697 case BATADV_TT_RESPONSE:
3698 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3699
3700 if (batadv_is_my_mac(bat_priv, dst)) {
3701 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003702 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003703 return NET_RX_SUCCESS;
3704 }
3705
3706 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3707 tt_flag = 'F';
3708 else
3709 tt_flag = '.';
3710
3711 batadv_dbg(BATADV_DBG_TT, bat_priv,
3712 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3713
3714 /* tvlv API will re-route the packet */
3715 return NET_RX_DROP;
3716 }
3717
3718 return NET_RX_SUCCESS;
3719}
3720
3721/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003722 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3723 * @bat_priv: the bat priv with all the soft interface information
3724 * @src: mac address of tt tvlv sender
3725 * @dst: mac address of tt tvlv recipient
3726 * @tvlv_value: tvlv buffer containing the tt data
3727 * @tvlv_value_len: tvlv buffer length
3728 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003729 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner122edaa2013-04-23 21:40:03 +08003730 * otherwise.
3731 */
3732static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003733 u8 *src, u8 *dst,
Marek Lindner122edaa2013-04-23 21:40:03 +08003734 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003735 u16 tvlv_value_len)
Marek Lindner122edaa2013-04-23 21:40:03 +08003736{
3737 struct batadv_tvlv_roam_adv *roaming_adv;
3738 struct batadv_orig_node *orig_node = NULL;
3739
3740 /* If this node is not the intended recipient of the
3741 * roaming advertisement the packet is forwarded
3742 * (the tvlv API will re-route the packet).
3743 */
3744 if (!batadv_is_my_mac(bat_priv, dst))
3745 return NET_RX_DROP;
3746
Marek Lindner122edaa2013-04-23 21:40:03 +08003747 if (tvlv_value_len < sizeof(*roaming_adv))
3748 goto out;
3749
3750 orig_node = batadv_orig_hash_find(bat_priv, src);
3751 if (!orig_node)
3752 goto out;
3753
3754 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3755 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3756
3757 batadv_dbg(BATADV_DBG_TT, bat_priv,
3758 "Received ROAMING_ADV from %pM (client %pM)\n",
3759 src, roaming_adv->client);
3760
3761 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003762 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003763 atomic_read(&orig_node->last_ttvn) + 1);
3764
3765out:
3766 if (orig_node)
3767 batadv_orig_node_free_ref(orig_node);
3768 return NET_RX_SUCCESS;
3769}
3770
3771/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003772 * batadv_tt_init - initialise the translation table internals
3773 * @bat_priv: the bat priv with all the soft interface information
3774 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003775 * Return: 0 on success or negative error number in case of failure.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003776 */
3777int batadv_tt_init(struct batadv_priv *bat_priv)
3778{
3779 int ret;
3780
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003781 /* synchronized flags must be remote */
3782 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3783
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003784 ret = batadv_tt_local_init(bat_priv);
3785 if (ret < 0)
3786 return ret;
3787
3788 ret = batadv_tt_global_init(bat_priv);
3789 if (ret < 0)
3790 return ret;
3791
3792 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003793 batadv_tt_tvlv_unicast_handler_v1,
3794 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003795
Marek Lindner122edaa2013-04-23 21:40:03 +08003796 batadv_tvlv_handler_register(bat_priv, NULL,
3797 batadv_roam_tvlv_unicast_handler_v1,
3798 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3799
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003800 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3801 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3802 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3803
3804 return 1;
3805}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003806
3807/**
3808 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3809 * @bat_priv: the bat priv with all the soft interface information
3810 * @addr: the mac address of the client
3811 * @vid: the identifier of the VLAN where this client is connected
3812 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003813 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003814 * otherwise
3815 */
3816bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003817 const u8 *addr, unsigned short vid)
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003818{
3819 struct batadv_tt_global_entry *tt;
3820 bool ret;
3821
3822 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3823 if (!tt)
3824 return false;
3825
3826 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3827
3828 batadv_tt_global_entry_free_ref(tt);
3829
3830 return ret;
3831}