blob: af2bfef6dca887bebb7f1eff14a2dc298268fba6 [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>
Sven Eckelmann86452f82016-06-25 16:44:06 +020025#include <linux/cache.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020026#include <linux/compiler.h>
Antonio Quartulliced72932013-04-24 16:37:51 +020027#include <linux/crc32c.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/errno.h>
29#include <linux/etherdevice.h>
30#include <linux/fs.h>
31#include <linux/if_ether.h>
Sven Eckelmann86452f82016-06-25 16:44:06 +020032#include <linux/init.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020033#include <linux/jhash.h>
34#include <linux/jiffies.h>
35#include <linux/kernel.h>
Sven Eckelmann6e8ef692016-01-16 10:29:50 +010036#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/list.h>
38#include <linux/lockdep.h>
39#include <linux/netdevice.h>
40#include <linux/rculist.h>
41#include <linux/rcupdate.h>
42#include <linux/seq_file.h>
43#include <linux/slab.h>
44#include <linux/spinlock.h>
45#include <linux/stddef.h>
46#include <linux/string.h>
47#include <linux/workqueue.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048
49#include "bridge_loop_avoidance.h"
50#include "hard-interface.h"
51#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020052#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "multicast.h"
54#include "originator.h"
55#include "packet.h"
56#include "soft-interface.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020057#include "tvlv.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020058
Sven Eckelmann86452f82016-06-25 16:44:06 +020059static struct kmem_cache *batadv_tl_cache __read_mostly;
60static struct kmem_cache *batadv_tg_cache __read_mostly;
61static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
62static struct kmem_cache *batadv_tt_change_cache __read_mostly;
63static struct kmem_cache *batadv_tt_req_cache __read_mostly;
64static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
65
Antonio Quartullidec05072012-11-10 11:00:32 +010066/* hash class keys */
67static struct lock_class_key batadv_tt_local_hash_lock_class_key;
68static struct lock_class_key batadv_tt_global_hash_lock_class_key;
69
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020070static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +020071 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +020072 struct batadv_orig_node *orig_node);
Sven Eckelmanna5130882012-05-16 20:23:16 +020073static void batadv_tt_purge(struct work_struct *work);
74static void
Sven Eckelmann56303d32012-06-05 22:31:31 +020075batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +020076static void batadv_tt_global_del(struct batadv_priv *bat_priv,
77 struct batadv_orig_node *orig_node,
78 const unsigned char *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +020079 unsigned short vid, const char *message,
80 bool roaming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081
Sven Eckelmann62fe7102015-09-15 19:00:48 +020082/**
Antonio Quartullid15cd622015-11-17 16:40:52 +080083 * batadv_compare_tt - check if two TT entries are the same
84 * @node: the list element pointer of the first TT entry
85 * @data2: pointer to the tt_common_entry of the second TT entry
Sven Eckelmann62fe7102015-09-15 19:00:48 +020086 *
Antonio Quartullid15cd622015-11-17 16:40:52 +080087 * Compare the MAC address and the VLAN ID of the two TT entries and check if
88 * they are the same TT client.
Sven Eckelmann4b426b12016-02-22 21:02:39 +010089 * Return: true if the two TT clients are the same, false otherwise
Sven Eckelmann62fe7102015-09-15 19:00:48 +020090 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010091static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
Marek Lindner7aadf882011-02-18 12:28:09 +000092{
Sven Eckelmann56303d32012-06-05 22:31:31 +020093 const void *data1 = container_of(node, struct batadv_tt_common_entry,
Sven Eckelmann747e4222011-05-14 23:14:50 +020094 hash_entry);
Marek Lindner4c718952015-08-06 10:38:54 +020095 const struct batadv_tt_common_entry *tt1 = data1;
96 const struct batadv_tt_common_entry *tt2 = data2;
Marek Lindner7aadf882011-02-18 12:28:09 +000097
Marek Lindner4c718952015-08-06 10:38:54 +020098 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
Marek Lindner7aadf882011-02-18 12:28:09 +000099}
100
Antonio Quartullic018ad32013-06-04 12:11:39 +0200101/**
102 * batadv_choose_tt - return the index of the tt entry in the hash table
103 * @data: pointer to the tt_common_entry object to map
104 * @size: the size of the hash table
105 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200106 * Return: the hash index where the object represented by 'data' should be
Antonio Quartullic018ad32013-06-04 12:11:39 +0200107 * stored at.
108 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200109static inline u32 batadv_choose_tt(const void *data, u32 size)
Antonio Quartullic018ad32013-06-04 12:11:39 +0200110{
111 struct batadv_tt_common_entry *tt;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200112 u32 hash = 0;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200113
114 tt = (struct batadv_tt_common_entry *)data;
Sven Eckelmann36fd61c2015-03-01 09:46:18 +0100115 hash = jhash(&tt->addr, ETH_ALEN, hash);
116 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200117
118 return hash % size;
119}
120
121/**
122 * batadv_tt_hash_find - look for a client in the given hash table
123 * @hash: the hash table to search
124 * @addr: the mac address of the client to look for
125 * @vid: VLAN identifier
126 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200127 * Return: a pointer to the tt_common struct belonging to the searched client if
Antonio Quartullic018ad32013-06-04 12:11:39 +0200128 * found, NULL otherwise.
129 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130static struct batadv_tt_common_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200131batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200132 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000133{
Marek Lindner7aadf882011-02-18 12:28:09 +0000134 struct hlist_head *head;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200135 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200136 u32 index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000137
138 if (!hash)
139 return NULL;
140
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100141 ether_addr_copy(to_search.addr, addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200142 to_search.vid = vid;
143
144 index = batadv_choose_tt(&to_search, hash->size);
Marek Lindner7aadf882011-02-18 12:28:09 +0000145 head = &hash->table[index];
146
147 rcu_read_lock();
Antonio Quartullic018ad32013-06-04 12:11:39 +0200148 hlist_for_each_entry_rcu(tt, head, hash_entry) {
149 if (!batadv_compare_eth(tt, addr))
Marek Lindner7aadf882011-02-18 12:28:09 +0000150 continue;
151
Antonio Quartullic018ad32013-06-04 12:11:39 +0200152 if (tt->vid != vid)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200153 continue;
154
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100155 if (!kref_get_unless_zero(&tt->refcount))
Antonio Quartullic018ad32013-06-04 12:11:39 +0200156 continue;
157
158 tt_tmp = tt;
Marek Lindner7aadf882011-02-18 12:28:09 +0000159 break;
160 }
161 rcu_read_unlock();
162
Antonio Quartullic018ad32013-06-04 12:11:39 +0200163 return tt_tmp;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100164}
165
Antonio Quartullic018ad32013-06-04 12:11:39 +0200166/**
167 * batadv_tt_local_hash_find - search the local table for a given client
168 * @bat_priv: the bat priv with all the soft interface information
169 * @addr: the mac address of the client to look for
170 * @vid: VLAN identifier
171 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200172 * Return: a pointer to the corresponding tt_local_entry struct if the client is
Antonio Quartullic018ad32013-06-04 12:11:39 +0200173 * found, NULL otherwise.
174 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200175static struct batadv_tt_local_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200176batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200177 unsigned short vid)
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100178{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200179 struct batadv_tt_common_entry *tt_common_entry;
180 struct batadv_tt_local_entry *tt_local_entry = NULL;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100181
Antonio Quartullic018ad32013-06-04 12:11:39 +0200182 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
183 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100184 if (tt_common_entry)
185 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186 struct batadv_tt_local_entry,
187 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100188 return tt_local_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000189}
190
Antonio Quartullic018ad32013-06-04 12:11:39 +0200191/**
192 * batadv_tt_global_hash_find - search the global table for a given client
193 * @bat_priv: the bat priv with all the soft interface information
194 * @addr: the mac address of the client to look for
195 * @vid: VLAN identifier
196 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200197 * Return: a pointer to the corresponding tt_global_entry struct if the client
Antonio Quartullic018ad32013-06-04 12:11:39 +0200198 * is found, NULL otherwise.
199 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200200static struct batadv_tt_global_entry *
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200201batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200202 unsigned short vid)
Marek Lindner7aadf882011-02-18 12:28:09 +0000203{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200204 struct batadv_tt_common_entry *tt_common_entry;
205 struct batadv_tt_global_entry *tt_global_entry = NULL;
Marek Lindner7aadf882011-02-18 12:28:09 +0000206
Antonio Quartullic018ad32013-06-04 12:11:39 +0200207 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
208 vid);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100209 if (tt_common_entry)
210 tt_global_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200211 struct batadv_tt_global_entry,
212 common);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100213 return tt_global_entry;
Marek Lindner7aadf882011-02-18 12:28:09 +0000214}
215
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100216/**
Sven Eckelmann86452f82016-06-25 16:44:06 +0200217 * batadv_tt_local_entry_free_rcu - free the tt_local_entry
218 * @rcu: rcu pointer of the tt_local_entry
219 */
220static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
221{
222 struct batadv_tt_local_entry *tt_local_entry;
223
224 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
225 common.rcu);
226
227 kmem_cache_free(batadv_tl_cache, tt_local_entry);
228}
229
230/**
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100231 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
232 * for free after rcu grace period
233 * @ref: kref pointer of the nc_node
234 */
235static void batadv_tt_local_entry_release(struct kref *ref)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200236{
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100237 struct batadv_tt_local_entry *tt_local_entry;
238
239 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
240 common.refcount);
241
Sven Eckelmanna33d9702016-03-11 16:44:05 +0100242 batadv_softif_vlan_put(tt_local_entry->vlan);
243
Sven Eckelmann86452f82016-06-25 16:44:06 +0200244 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200245}
246
Antonio Quartulli21026052013-05-07 00:29:22 +0200247/**
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100248 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100249 * possibly release it
250 * @tt_local_entry: tt_local_entry to be free'd
251 */
252static void
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100253batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100254{
255 kref_put(&tt_local_entry->common.refcount,
256 batadv_tt_local_entry_release);
257}
258
259/**
Sven Eckelmann86452f82016-06-25 16:44:06 +0200260 * batadv_tt_global_entry_free_rcu - free the tt_global_entry
261 * @rcu: rcu pointer of the tt_global_entry
262 */
263static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
264{
265 struct batadv_tt_global_entry *tt_global_entry;
266
267 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
268 common.rcu);
269
270 kmem_cache_free(batadv_tg_cache, tt_global_entry);
271}
272
273/**
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100274 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
275 * for free after rcu grace period
276 * @ref: kref pointer of the nc_node
277 */
278static void batadv_tt_global_entry_release(struct kref *ref)
279{
280 struct batadv_tt_global_entry *tt_global_entry;
281
282 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
283 common.refcount);
284
285 batadv_tt_global_del_orig_list(tt_global_entry);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200286
287 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100288}
289
290/**
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100291 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
292 * possibly release it
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100293 * @tt_global_entry: tt_global_entry to be free'd
Antonio Quartulli21026052013-05-07 00:29:22 +0200294 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200295static void
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100296batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200297{
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100298 kref_put(&tt_global_entry->common.refcount,
299 batadv_tt_global_entry_release);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200300}
301
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100302/**
303 * batadv_tt_global_hash_count - count the number of orig entries
Antonio Quartullid15cd622015-11-17 16:40:52 +0800304 * @bat_priv: the bat priv with all the soft interface information
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100305 * @addr: the mac address of the client to count entries for
306 * @vid: VLAN identifier
307 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200308 * Return: the number of originators advertising the given address/data
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100309 * (excluding ourself).
310 */
311int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200312 const u8 *addr, unsigned short vid)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100313{
314 struct batadv_tt_global_entry *tt_global_entry;
315 int count;
316
317 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
318 if (!tt_global_entry)
319 return 0;
320
321 count = atomic_read(&tt_global_entry->orig_list_count);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100322 batadv_tt_global_entry_put(tt_global_entry);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100323
324 return count;
325}
326
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200327/**
328 * batadv_tt_local_size_mod - change the size by v of the local table identified
329 * by vid
330 * @bat_priv: the bat priv with all the soft interface information
331 * @vid: the VLAN identifier of the sub-table to change
332 * @v: the amount to sum to the local table size
333 */
334static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
335 unsigned short vid, int v)
336{
337 struct batadv_softif_vlan *vlan;
338
339 vlan = batadv_softif_vlan_get(bat_priv, vid);
340 if (!vlan)
341 return;
342
343 atomic_add(v, &vlan->tt.num_entries);
344
Sven Eckelmann9c3bf082016-01-17 11:01:21 +0100345 batadv_softif_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200346}
347
348/**
349 * batadv_tt_local_size_inc - increase by one the local table size for the given
350 * vid
351 * @bat_priv: the bat priv with all the soft interface information
352 * @vid: the VLAN identifier
353 */
354static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
355 unsigned short vid)
356{
357 batadv_tt_local_size_mod(bat_priv, vid, 1);
358}
359
360/**
361 * batadv_tt_local_size_dec - decrease by one the local table size for the given
362 * vid
363 * @bat_priv: the bat priv with all the soft interface information
364 * @vid: the VLAN identifier
365 */
366static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
367 unsigned short vid)
368{
369 batadv_tt_local_size_mod(bat_priv, vid, -1);
370}
371
372/**
Antonio Quartullid15cd622015-11-17 16:40:52 +0800373 * batadv_tt_global_size_mod - change the size by v of the global table
374 * for orig_node identified by vid
375 * @orig_node: the originator for which the table has to be modified
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200376 * @vid: the VLAN identifier
377 * @v: the amount to sum to the global table size
378 */
379static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
380 unsigned short vid, int v)
381{
382 struct batadv_orig_node_vlan *vlan;
383
384 vlan = batadv_orig_node_vlan_new(orig_node, vid);
385 if (!vlan)
386 return;
387
388 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
389 spin_lock_bh(&orig_node->vlan_list_lock);
Sven Eckelmann3db15202016-01-31 13:28:00 +0100390 if (!hlist_unhashed(&vlan->list)) {
391 hlist_del_init_rcu(&vlan->list);
Sven Eckelmann21754e22016-01-17 11:01:24 +0100392 batadv_orig_node_vlan_put(vlan);
Sven Eckelmann3db15202016-01-31 13:28:00 +0100393 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200394 spin_unlock_bh(&orig_node->vlan_list_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200395 }
396
Sven Eckelmann21754e22016-01-17 11:01:24 +0100397 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200398}
399
400/**
401 * batadv_tt_global_size_inc - increase by one the global table size for the
402 * given vid
403 * @orig_node: the originator which global table size has to be decreased
404 * @vid: the vlan identifier
405 */
406static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
407 unsigned short vid)
408{
409 batadv_tt_global_size_mod(orig_node, vid, 1);
410}
411
412/**
413 * batadv_tt_global_size_dec - decrease by one the global table size for the
414 * given vid
415 * @orig_node: the originator which global table size has to be decreased
416 * @vid: the vlan identifier
417 */
418static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
419 unsigned short vid)
420{
421 batadv_tt_global_size_mod(orig_node, vid, -1);
422}
423
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100424/**
Sven Eckelmann86452f82016-06-25 16:44:06 +0200425 * batadv_tt_orig_list_entry_free_rcu - free the orig_entry
426 * @rcu: rcu pointer of the orig_entry
427 */
428static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
429{
430 struct batadv_tt_orig_list_entry *orig_entry;
431
432 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
433
434 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
435}
436
437/**
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100438 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
439 * queue for free after rcu grace period
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100440 * @ref: kref pointer of the tt orig entry
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100441 */
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100442static void batadv_tt_orig_list_entry_release(struct kref *ref)
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100443{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100444 struct batadv_tt_orig_list_entry *orig_entry;
445
446 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
447 refcount);
448
Sven Eckelmann5d967312016-01-17 11:01:09 +0100449 batadv_orig_node_put(orig_entry->orig_node);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200450 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
Sven Eckelmann42eff6a2016-01-05 12:06:20 +0100451}
452
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100453/**
Sven Eckelmann7e2366c2016-01-17 11:01:27 +0100454 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
455 * possibly release it
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100456 * @orig_entry: tt orig entry to be free'd
457 */
Sven Eckelmanna5130882012-05-16 20:23:16 +0200458static void
Sven Eckelmann7e2366c2016-01-17 11:01:27 +0100459batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200460{
Sven Eckelmann6e8ef692016-01-16 10:29:50 +0100461 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200462}
463
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200464/**
465 * batadv_tt_local_event - store a local TT event (ADD/DEL)
466 * @bat_priv: the bat priv with all the soft interface information
467 * @tt_local_entry: the TT entry involved in the event
468 * @event_flags: flags to store in the event structure
469 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200470static void batadv_tt_local_event(struct batadv_priv *bat_priv,
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200471 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200472 u8 event_flags)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200473{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200474 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200475 struct batadv_tt_common_entry *common = &tt_local_entry->common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200476 u8 flags = common->flags | event_flags;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200477 bool event_removed = false;
478 bool del_op_requested, del_op_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200479
Sven Eckelmann86452f82016-06-25 16:44:06 +0200480 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200481 if (!tt_change_node)
482 return;
483
Antonio Quartulliff66c972011-06-30 01:14:00 +0200484 tt_change_node->change.flags = flags;
Antonio Quartullica663042013-12-15 13:26:55 +0100485 memset(tt_change_node->change.reserved, 0,
486 sizeof(tt_change_node->change.reserved));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100487 ether_addr_copy(tt_change_node->change.addr, common->addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +0200488 tt_change_node->change.vid = htons(common->vid);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200489
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200490 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200491
492 /* check for ADD+DEL or DEL+ADD events */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200493 spin_lock_bh(&bat_priv->tt.changes_list_lock);
494 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200495 list) {
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200496 if (!batadv_compare_eth(entry->change.addr, common->addr))
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200497 continue;
498
499 /* DEL+ADD in the same orig interval have no effect and can be
500 * removed to avoid silly behaviour on the receiver side. The
501 * other way around (ADD+DEL) can happen in case of roaming of
502 * a client still in the NEW state. Roaming of NEW clients is
503 * now possible due to automatically recognition of "temporary"
504 * clients
505 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200506 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200507 if (!del_op_requested && del_op_entry)
508 goto del;
509 if (del_op_requested && !del_op_entry)
510 goto del;
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200511
512 /* this is a second add in the same originator interval. It
513 * means that flags have been changed: update them!
514 */
515 if (!del_op_requested && !del_op_entry)
516 entry->change.flags = flags;
517
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200518 continue;
519del:
520 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +0200521 kmem_cache_free(batadv_tt_change_cache, entry);
522 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200523 event_removed = true;
524 goto unlock;
525 }
526
Antonio Quartullia73105b2011-04-27 14:27:44 +0200527 /* track the change in the OGMinterval list */
Sven Eckelmann807736f2012-07-15 22:26:51 +0200528 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200529
530unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +0200531 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200532
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200533 if (event_removed)
Sven Eckelmann807736f2012-07-15 22:26:51 +0200534 atomic_dec(&bat_priv->tt.local_changes);
Antonio Quartulli3b643de2012-05-25 00:00:42 +0200535 else
Sven Eckelmann807736f2012-07-15 22:26:51 +0200536 atomic_inc(&bat_priv->tt.local_changes);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200537}
538
Marek Lindner335fbe02013-04-23 21:40:02 +0800539/**
540 * batadv_tt_len - compute length in bytes of given number of tt changes
541 * @changes_num: number of tt changes
542 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200543 * Return: computed length in bytes.
Marek Lindner335fbe02013-04-23 21:40:02 +0800544 */
545static int batadv_tt_len(int changes_num)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200546{
Marek Lindner335fbe02013-04-23 21:40:02 +0800547 return changes_num * sizeof(struct batadv_tvlv_tt_change);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200548}
549
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200550/**
551 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
552 * @tt_len: available space
553 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200554 * Return: the number of entries.
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200555 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200556static u16 batadv_tt_entries(u16 tt_len)
Antonio Quartulli298e6e62013-05-28 13:14:27 +0200557{
558 return tt_len / batadv_tt_len(1);
559}
560
Marek Lindnera19d3d82013-05-27 15:33:25 +0800561/**
562 * batadv_tt_local_table_transmit_size - calculates the local translation table
563 * size when transmitted over the air
564 * @bat_priv: the bat priv with all the soft interface information
565 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200566 * Return: local translation table size in bytes.
Marek Lindnera19d3d82013-05-27 15:33:25 +0800567 */
568static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
569{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200570 u16 num_vlan = 0;
571 u16 tt_local_entries = 0;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800572 struct batadv_softif_vlan *vlan;
573 int hdr_size;
574
575 rcu_read_lock();
576 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
577 num_vlan++;
578 tt_local_entries += atomic_read(&vlan->tt.num_entries);
579 }
580 rcu_read_unlock();
581
582 /* header size of tvlv encapsulated tt response payload */
583 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
584 hdr_size += sizeof(struct batadv_tvlv_hdr);
585 hdr_size += sizeof(struct batadv_tvlv_tt_data);
586 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
587
588 return hdr_size + batadv_tt_len(tt_local_entries);
589}
590
Sven Eckelmann56303d32012-06-05 22:31:31 +0200591static int batadv_tt_local_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592{
Sven Eckelmann807736f2012-07-15 22:26:51 +0200593 if (bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200594 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
Sven Eckelmann807736f2012-07-15 22:26:51 +0200596 bat_priv->tt.local_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597
Sven Eckelmann807736f2012-07-15 22:26:51 +0200598 if (!bat_priv->tt.local_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200599 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000600
Antonio Quartullidec05072012-11-10 11:00:32 +0100601 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
602 &batadv_tt_local_hash_lock_class_key);
603
Sven Eckelmann5346c352012-05-05 13:27:28 +0200604 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605}
606
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200607static void batadv_tt_global_free(struct batadv_priv *bat_priv,
608 struct batadv_tt_global_entry *tt_global,
609 const char *message)
610{
611 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200612 "Deleting global tt entry %pM (vid: %d): %s\n",
613 tt_global->common.addr,
614 BATADV_PRINT_VID(tt_global->common.vid), message);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200615
616 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200617 batadv_choose_tt, &tt_global->common);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100618 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200619}
620
Antonio Quartullic018ad32013-06-04 12:11:39 +0200621/**
622 * batadv_tt_local_add - add a new client to the local table or update an
623 * existing client
624 * @soft_iface: netdev struct of the mesh interface
625 * @addr: the mac address of the client to add
626 * @vid: VLAN identifier
627 * @ifindex: index of the interface where the client is connected to (useful to
628 * identify wireless clients)
Antonio Quartulli9464d072013-11-16 12:03:48 +0100629 * @mark: the value contained in the skb->mark field of the received packet (if
630 * any)
Marek Lindnera19d3d82013-05-27 15:33:25 +0800631 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200632 * Return: true if the client was successfully added, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +0200633 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200634bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
635 unsigned short vid, int ifindex, u32 mark)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200637 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann170173b2012-10-07 12:02:22 +0200638 struct batadv_tt_local_entry *tt_local;
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100639 struct batadv_tt_global_entry *tt_global = NULL;
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200640 struct net *net = dev_net(soft_iface);
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200641 struct batadv_softif_vlan *vlan;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200642 struct net_device *in_dev = NULL;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200643 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200644 struct batadv_tt_orig_list_entry *orig_entry;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800645 int hash_added, table_size, packet_size_max;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200646 bool ret = false;
647 bool roamed_back = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200648 u8 remote_flags;
649 u32 match_mark;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200651 if (ifindex != BATADV_NULL_IFINDEX)
Andrew Lunn2cd45a02016-04-21 12:57:27 +0200652 in_dev = dev_get_by_index(net, ifindex);
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200653
Antonio Quartullic018ad32013-06-04 12:11:39 +0200654 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100655
656 if (!is_multicast_ether_addr(addr))
657 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Antonio Quartulli47c94652012-09-23 22:38:35 +0200659 if (tt_local) {
660 tt_local->last_seen = jiffies;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200661 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
662 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200663 "Re-adding pending client %pM (vid: %d)\n",
664 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200665 /* whatever the reason why the PENDING flag was set,
666 * this is a client which was enqueued to be removed in
667 * this orig_interval. Since it popped up again, the
668 * flag can be reset like it was never enqueued
669 */
670 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
671 goto add_event;
672 }
673
674 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
675 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200676 "Roaming client %pM (vid: %d) came back to its original location\n",
677 addr, BATADV_PRINT_VID(vid));
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200678 /* the ROAM flag is set because this client roamed away
679 * and the node got a roaming_advertisement message. Now
680 * that the client popped up again at its original
681 * location such flag can be unset
682 */
683 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
684 roamed_back = true;
685 }
686 goto check_roaming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687 }
688
Marek Lindnera19d3d82013-05-27 15:33:25 +0800689 /* Ignore the client if we cannot send it in a full table response. */
690 table_size = batadv_tt_local_table_transmit_size(bat_priv);
691 table_size += batadv_tt_len(1);
692 packet_size_max = atomic_read(&bat_priv->packet_size_max);
693 if (table_size > packet_size_max) {
694 net_ratelimited_function(batadv_info, soft_iface,
695 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
696 table_size, packet_size_max, addr);
697 goto out;
698 }
699
Sven Eckelmann86452f82016-06-25 16:44:06 +0200700 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200701 if (!tt_local)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200702 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200703
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200704 /* increase the refcounter of the related vlan */
705 vlan = batadv_softif_vlan_get(bat_priv, vid);
Simon Wunderlich0b3dd7d2016-06-26 11:16:09 +0200706 if (!vlan) {
707 net_ratelimited_function(batadv_info, soft_iface,
708 "adding TT local entry %pM to non-existent VLAN %d\n",
709 addr, BATADV_PRINT_VID(vid));
Sven Eckelmann86452f82016-06-25 16:44:06 +0200710 kmem_cache_free(batadv_tl_cache, tt_local);
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200711 tt_local = NULL;
Marek Lindner354136b2015-06-09 21:24:36 +0800712 goto out;
Sven Eckelmannfd7dec22015-08-18 13:37:01 +0200713 }
Antonio Quartulli35df3b22014-05-08 17:13:15 +0200714
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200715 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +0200716 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
717 addr, BATADV_PRINT_VID(vid),
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200718 (u8)atomic_read(&bat_priv->tt.vn));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100720 ether_addr_copy(tt_local->common.addr, addr);
Antonio Quartulli8425ec62012-11-19 09:01:44 +0100721 /* The local entry has to be marked as NEW to avoid to send it in
722 * a full table response going out before the next ttvn increment
723 * (consistency check)
724 */
725 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200726 tt_local->common.vid = vid;
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200727 if (batadv_is_wifi_netdev(in_dev))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200728 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
Sven Eckelmann92dcdf02016-01-16 10:29:57 +0100729 kref_init(&tt_local->common.refcount);
730 kref_get(&tt_local->common.refcount);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200731 tt_local->last_seen = jiffies;
732 tt_local->common.added_at = tt_local->last_seen;
Sven Eckelmanna33d9702016-03-11 16:44:05 +0100733 tt_local->vlan = vlan;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734
Linus Lüssingc5caf4e2014-02-15 17:47:49 +0100735 /* the batman interface mac and multicast addresses should never be
736 * purged
737 */
738 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
739 is_multicast_ether_addr(addr))
Antonio Quartulli47c94652012-09-23 22:38:35 +0200740 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741
Sven Eckelmann807736f2012-07-15 22:26:51 +0200742 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200743 batadv_choose_tt, &tt_local->common,
Antonio Quartulli47c94652012-09-23 22:38:35 +0200744 &tt_local->common.hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100745
746 if (unlikely(hash_added != 0)) {
747 /* remove the reference for the hash */
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100748 batadv_tt_local_entry_put(tt_local);
Simon Wunderlich80b3f582011-11-02 20:26:45 +0100749 goto out;
750 }
751
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200752add_event:
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +0200753 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
Antonio Quartulliff66c972011-06-30 01:14:00 +0200754
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200755check_roaming:
756 /* Check whether it is a roaming, but don't do anything if the roaming
757 * process has already been handled
758 */
759 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200760 /* These node are probably going to update their tt table */
Antonio Quartulli47c94652012-09-23 22:38:35 +0200761 head = &tt_global->orig_list;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200762 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800763 hlist_for_each_entry_rcu(orig_entry, head, list) {
Antonio Quartulli47c94652012-09-23 22:38:35 +0200764 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200765 tt_global->common.vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +0200766 orig_entry->orig_node);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +0200767 }
768 rcu_read_unlock();
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200769 if (roamed_back) {
770 batadv_tt_global_free(bat_priv, tt_global,
771 "Roaming canceled");
772 tt_global = NULL;
773 } else {
774 /* The global entry has to be marked as ROAMING and
775 * has to be kept for consistency purpose
776 */
777 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
778 tt_global->roam_at = jiffies;
779 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200780 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +0200781
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200782 /* store the current remote flags before altering them. This helps
783 * understanding is flags are changing or not
784 */
785 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800786
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200787 if (batadv_is_wifi_netdev(in_dev))
788 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
789 else
790 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
791
Antonio Quartulli9464d072013-11-16 12:03:48 +0100792 /* check the mark in the skb: if it's equal to the configured
793 * isolation_mark, it means the packet is coming from an isolated
794 * non-mesh client
795 */
796 match_mark = (mark & bat_priv->isolation_mark_mask);
797 if (bat_priv->isolation_mark_mask &&
798 match_mark == bat_priv->isolation_mark)
799 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
800 else
801 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
802
Antonio Quartulli3c4f7ab2013-10-13 02:50:19 +0200803 /* if any "dynamic" flag has been modified, resend an ADD event for this
804 * entry so that all the nodes can get the new flags
805 */
806 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
807 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
808
809 ret = true;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200810out:
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200811 if (in_dev)
812 dev_put(in_dev);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200813 if (tt_local)
Sven Eckelmann95c0db92016-01-17 11:01:25 +0100814 batadv_tt_local_entry_put(tt_local);
Antonio Quartulli47c94652012-09-23 22:38:35 +0200815 if (tt_global)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +0100816 batadv_tt_global_entry_put(tt_global);
Marek Lindnera19d3d82013-05-27 15:33:25 +0800817 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000818}
819
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800820/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200821 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
822 * within a TT Response directed to another node
823 * @orig_node: originator for which the TT data has to be prepared
824 * @tt_data: uninitialised pointer to the address of the TVLV buffer
825 * @tt_change: uninitialised pointer to the address of the area where the TT
826 * changed can be stored
827 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
828 * function reserves the amount of space needed to send the entire global TT
829 * table. In case of success the value is updated with the real amount of
830 * reserved bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200831 * Allocate the needed amount of memory for the entire TT TVLV and write its
832 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
833 * objects, one per active VLAN served by the originator node.
834 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200835 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200836 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200837static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200838batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
839 struct batadv_tvlv_tt_data **tt_data,
840 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200841 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200842{
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200843 u16 num_vlan = 0;
844 u16 num_entries = 0;
845 u16 change_offset;
846 u16 tvlv_len;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200847 struct batadv_tvlv_tt_vlan_data *tt_vlan;
848 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200849 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200850
851 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800852 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200853 num_vlan++;
854 num_entries += atomic_read(&vlan->tt.num_entries);
855 }
856
857 change_offset = sizeof(**tt_data);
858 change_offset += num_vlan * sizeof(*tt_vlan);
859
860 /* if tt_len is negative, allocate the space needed by the full table */
861 if (*tt_len < 0)
862 *tt_len = batadv_tt_len(num_entries);
863
864 tvlv_len = *tt_len;
865 tvlv_len += change_offset;
866
867 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
868 if (!*tt_data) {
869 *tt_len = 0;
870 goto out;
871 }
872
873 (*tt_data)->flags = BATADV_NO_FLAGS;
874 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
875 (*tt_data)->num_vlan = htons(num_vlan);
876
877 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800878 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200879 tt_vlan->vid = htons(vlan->vid);
880 tt_vlan->crc = htonl(vlan->tt.crc);
881
882 tt_vlan++;
883 }
884
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200885 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200886 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
887
888out:
889 rcu_read_unlock();
890 return tvlv_len;
891}
892
893/**
894 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
895 * node
896 * @bat_priv: the bat priv with all the soft interface information
897 * @tt_data: uninitialised pointer to the address of the TVLV buffer
898 * @tt_change: uninitialised pointer to the address of the area where the TT
899 * changes can be stored
900 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
901 * function reserves the amount of space needed to send the entire local TT
902 * table. In case of success the value is updated with the real amount of
903 * reserved bytes
904 *
905 * Allocate the needed amount of memory for the entire TT TVLV and write its
906 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
907 * objects, one per active VLAN.
908 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200909 * Return: the size of the allocated buffer or 0 in case of failure.
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200910 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200911static u16
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200912batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
913 struct batadv_tvlv_tt_data **tt_data,
914 struct batadv_tvlv_tt_change **tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200915 s32 *tt_len)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200916{
917 struct batadv_tvlv_tt_vlan_data *tt_vlan;
918 struct batadv_softif_vlan *vlan;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200919 u16 num_vlan = 0;
920 u16 num_entries = 0;
921 u16 tvlv_len;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200922 u8 *tt_change_ptr;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200923 int change_offset;
924
925 rcu_read_lock();
926 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
927 num_vlan++;
928 num_entries += atomic_read(&vlan->tt.num_entries);
929 }
930
931 change_offset = sizeof(**tt_data);
932 change_offset += num_vlan * sizeof(*tt_vlan);
933
934 /* if tt_len is negative, allocate the space needed by the full table */
935 if (*tt_len < 0)
936 *tt_len = batadv_tt_len(num_entries);
937
938 tvlv_len = *tt_len;
939 tvlv_len += change_offset;
940
941 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
942 if (!*tt_data) {
943 tvlv_len = 0;
944 goto out;
945 }
946
947 (*tt_data)->flags = BATADV_NO_FLAGS;
948 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
949 (*tt_data)->num_vlan = htons(num_vlan);
950
951 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
952 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
953 tt_vlan->vid = htons(vlan->vid);
954 tt_vlan->crc = htonl(vlan->tt.crc);
955
956 tt_vlan++;
957 }
958
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200959 tt_change_ptr = (u8 *)*tt_data + change_offset;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200960 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
961
962out:
963 rcu_read_unlock();
964 return tvlv_len;
965}
966
967/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800968 * batadv_tt_tvlv_container_update - update the translation table tvlv container
969 * after local tt changes have been committed
970 * @bat_priv: the bat priv with all the soft interface information
971 */
972static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000973{
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800974 struct batadv_tt_change_node *entry, *safe;
975 struct batadv_tvlv_tt_data *tt_data;
976 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200977 int tt_diff_len, tt_change_len = 0;
Sven Eckelmann4f248cf2015-06-09 20:50:49 +0200978 int tt_diff_entries_num = 0;
979 int tt_diff_entries_count = 0;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200980 u16 tvlv_len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000981
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200982 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
983 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800984
985 /* if we have too many changes for one packet don't send any
986 * and wait for the tt table request which will be fragmented
987 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800988 if (tt_diff_len > bat_priv->soft_iface->mtu)
989 tt_diff_len = 0;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800990
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200991 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
992 &tt_change, &tt_diff_len);
993 if (!tvlv_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800994 return;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800995
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800996 tt_data->flags = BATADV_TT_OGM_DIFF;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800997
Marek Lindnere1bf0c12013-04-23 21:40:01 +0800998 if (tt_diff_len == 0)
999 goto container_register;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001000
Sven Eckelmann807736f2012-07-15 22:26:51 +02001001 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1002 atomic_set(&bat_priv->tt.local_changes, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001003
Sven Eckelmann807736f2012-07-15 22:26:51 +02001004 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001005 list) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001006 if (tt_diff_entries_count < tt_diff_entries_num) {
1007 memcpy(tt_change + tt_diff_entries_count,
1008 &entry->change,
1009 sizeof(struct batadv_tvlv_tt_change));
1010 tt_diff_entries_count++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001012 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02001013 kmem_cache_free(batadv_tt_change_cache, entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001014 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001015 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001016
Antonio Quartullia73105b2011-04-27 14:27:44 +02001017 /* Keep the buffer for possible tt_request */
Sven Eckelmann807736f2012-07-15 22:26:51 +02001018 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1019 kfree(bat_priv->tt.last_changeset);
1020 bat_priv->tt.last_changeset_len = 0;
1021 bat_priv->tt.last_changeset = NULL;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001022 tt_change_len = batadv_tt_len(tt_diff_entries_count);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001023 /* check whether this new OGM has no changes due to size problems */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001024 if (tt_diff_entries_count > 0) {
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08001025 /* if kmalloc() fails we will reply with the full table
Antonio Quartullia73105b2011-04-27 14:27:44 +02001026 * instead of providing the diff
1027 */
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001028 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001029 if (bat_priv->tt.last_changeset) {
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001030 memcpy(bat_priv->tt.last_changeset,
1031 tt_change, tt_change_len);
1032 bat_priv->tt.last_changeset_len = tt_diff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001033 }
1034 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02001035 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001037container_register:
1038 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001039 tvlv_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08001040 kfree(tt_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001041}
1042
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001043int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044{
1045 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001046 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001047 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001048 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli85766a82012-11-08 22:16:16 +01001049 struct batadv_tt_local_entry *tt_local;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001050 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001051 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001052 u32 i;
Antonio Quartulli85766a82012-11-08 22:16:16 +01001053 int last_seen_secs;
1054 int last_seen_msecs;
1055 unsigned long last_seen_jiffies;
1056 bool no_purge;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001057 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001058
Marek Lindner30da63a2012-08-03 17:15:46 +02001059 primary_if = batadv_seq_print_text_primary_if_get(seq);
1060 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001061 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001062
Sven Eckelmann86ceb362012-03-07 09:07:45 +01001063 seq_printf(seq,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001064 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001065 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001066 seq_puts(seq,
1067 " Client VID Flags Last seen (CRC )\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001069 for (i = 0; i < hash->size; i++) {
1070 head = &hash->table[i];
1071
Marek Lindner7aadf882011-02-18 12:28:09 +00001072 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001074 head, hash_entry) {
Antonio Quartulli85766a82012-11-08 22:16:16 +01001075 tt_local = container_of(tt_common_entry,
1076 struct batadv_tt_local_entry,
1077 common);
1078 last_seen_jiffies = jiffies - tt_local->last_seen;
1079 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1080 last_seen_secs = last_seen_msecs / 1000;
1081 last_seen_msecs = last_seen_msecs % 1000;
1082
1083 no_purge = tt_common_entry->flags & np_flag;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001084 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001085 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001086 tt_common_entry->addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001087 BATADV_PRINT_VID(tt_common_entry->vid),
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001088 ((tt_common_entry->flags &
1089 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
Antonio Quartulli85766a82012-11-08 22:16:16 +01001090 no_purge ? 'P' : '.',
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001091 ((tt_common_entry->flags &
1092 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1093 ((tt_common_entry->flags &
1094 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1095 ((tt_common_entry->flags &
1096 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1097 ((tt_common_entry->flags &
1098 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
Antonio Quartullia7966d92013-01-24 11:41:39 +01001099 no_purge ? 0 : last_seen_secs,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001100 no_purge ? 0 : last_seen_msecs,
Sven Eckelmanna33d9702016-03-11 16:44:05 +01001101 tt_local->vlan->tt.crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001102 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001103 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001104 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001105out:
1106 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001107 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001108 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001109}
1110
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111static void
1112batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1113 struct batadv_tt_local_entry *tt_local_entry,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001114 u16 flags, const char *message)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001115{
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001116 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001117
Antonio Quartulli015758d2011-07-09 17:52:13 +02001118 /* The local client has to be marked as "pending to be removed" but has
1119 * to be kept in the table in order to send it in a full table
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001120 * response issued before the net ttvn increment (consistency check)
1121 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001122 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
Antonio Quartullic566dbb2012-01-06 21:31:34 +01001123
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001124 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001125 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1126 tt_local_entry->common.addr,
1127 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128}
1129
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001130/**
1131 * batadv_tt_local_remove - logically remove an entry from the local table
1132 * @bat_priv: the bat priv with all the soft interface information
1133 * @addr: the MAC address of the client to remove
Antonio Quartullic018ad32013-06-04 12:11:39 +02001134 * @vid: VLAN identifier
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001135 * @message: message to append to the log on deletion
1136 * @roaming: true if the deletion is due to a roaming event
1137 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001138 * Return: the flags assigned to the local entry before being deleted
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001139 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001140u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1141 unsigned short vid, const char *message,
1142 bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001143{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001144 struct batadv_tt_local_entry *tt_local_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001145 u16 flags, curr_flags = BATADV_NO_FLAGS;
Marek Lindneref727062015-06-17 20:01:36 +08001146 void *tt_entry_exists;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147
Antonio Quartullic018ad32013-06-04 12:11:39 +02001148 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001149 if (!tt_local_entry)
1150 goto out;
1151
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001152 curr_flags = tt_local_entry->common.flags;
1153
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001154 flags = BATADV_TT_CLIENT_DEL;
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001155 /* if this global entry addition is due to a roaming, the node has to
1156 * mark the local entry as "roamed" in order to correctly reroute
1157 * packets later
1158 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001159 if (roaming) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001160 flags |= BATADV_TT_CLIENT_ROAM;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001161 /* mark the local client as ROAMed */
1162 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1163 }
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001164
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001165 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1166 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1167 message);
1168 goto out;
1169 }
1170 /* if this client has been added right now, it is possible to
1171 * immediately purge it
1172 */
Antonio Quartulli3abe4ad2013-04-03 11:15:33 +02001173 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
Marek Lindneref727062015-06-17 20:01:36 +08001174
1175 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1176 batadv_compare_tt,
1177 batadv_choose_tt,
1178 &tt_local_entry->common);
1179 if (!tt_entry_exists)
1180 goto out;
1181
1182 /* extra call to free the local tt entry */
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001183 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001184
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001185out:
1186 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001187 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001188
1189 return curr_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001190}
1191
Marek Lindnera19d3d82013-05-27 15:33:25 +08001192/**
1193 * batadv_tt_local_purge_list - purge inactive tt local entries
1194 * @bat_priv: the bat priv with all the soft interface information
1195 * @head: pointer to the list containing the local tt entries
1196 * @timeout: parameter deciding whether a given tt local entry is considered
1197 * inactive or not
1198 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001199static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
Marek Lindnera19d3d82013-05-27 15:33:25 +08001200 struct hlist_head *head,
1201 int timeout)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001202{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001203 struct batadv_tt_local_entry *tt_local_entry;
1204 struct batadv_tt_common_entry *tt_common_entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001205 struct hlist_node *node_tmp;
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001206
Sasha Levinb67bfe02013-02-27 17:06:00 -08001207 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001208 hash_entry) {
1209 tt_local_entry = container_of(tt_common_entry,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001210 struct batadv_tt_local_entry,
1211 common);
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001212 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1213 continue;
1214
1215 /* entry already marked for deletion */
1216 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1217 continue;
1218
Marek Lindnera19d3d82013-05-27 15:33:25 +08001219 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001220 continue;
1221
1222 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1223 BATADV_TT_CLIENT_DEL, "timed out");
1224 }
1225}
1226
Marek Lindnera19d3d82013-05-27 15:33:25 +08001227/**
1228 * batadv_tt_local_purge - purge inactive tt local entries
1229 * @bat_priv: the bat priv with all the soft interface information
1230 * @timeout: parameter deciding whether a given tt local entry is considered
1231 * inactive or not
1232 */
1233static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1234 int timeout)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001235{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001236 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001237 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001238 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001239 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001240
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001241 for (i = 0; i < hash->size; i++) {
1242 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001243 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001244
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001245 spin_lock_bh(list_lock);
Marek Lindnera19d3d82013-05-27 15:33:25 +08001246 batadv_tt_local_purge_list(bat_priv, head, timeout);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001247 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001248 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001249}
1250
Sven Eckelmann56303d32012-06-05 22:31:31 +02001251static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001252{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001253 struct batadv_hashtable *hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001254 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001255 struct batadv_tt_common_entry *tt_common_entry;
1256 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001257 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001258 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001259 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001260
Sven Eckelmann807736f2012-07-15 22:26:51 +02001261 if (!bat_priv->tt.local_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001262 return;
1263
Sven Eckelmann807736f2012-07-15 22:26:51 +02001264 hash = bat_priv->tt.local_hash;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001265
1266 for (i = 0; i < hash->size; i++) {
1267 head = &hash->table[i];
1268 list_lock = &hash->list_locks[i];
1269
1270 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001271 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001272 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001273 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001274 tt_local = container_of(tt_common_entry,
1275 struct batadv_tt_local_entry,
1276 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02001277
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001278 batadv_tt_local_entry_put(tt_local);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001279 }
1280 spin_unlock_bh(list_lock);
1281 }
1282
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001283 batadv_hash_destroy(hash);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001284
Sven Eckelmann807736f2012-07-15 22:26:51 +02001285 bat_priv->tt.local_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001286}
1287
Sven Eckelmann56303d32012-06-05 22:31:31 +02001288static int batadv_tt_global_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001289{
Sven Eckelmann807736f2012-07-15 22:26:51 +02001290 if (bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001291 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001292
Sven Eckelmann807736f2012-07-15 22:26:51 +02001293 bat_priv->tt.global_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001294
Sven Eckelmann807736f2012-07-15 22:26:51 +02001295 if (!bat_priv->tt.global_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001296 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001297
Antonio Quartullidec05072012-11-10 11:00:32 +01001298 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1299 &batadv_tt_global_hash_lock_class_key);
1300
Sven Eckelmann5346c352012-05-05 13:27:28 +02001301 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001302}
1303
Sven Eckelmann56303d32012-06-05 22:31:31 +02001304static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001305{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001306 struct batadv_tt_change_node *entry, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001307
Sven Eckelmann807736f2012-07-15 22:26:51 +02001308 spin_lock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001309
Sven Eckelmann807736f2012-07-15 22:26:51 +02001310 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
Antonio Quartullia73105b2011-04-27 14:27:44 +02001311 list) {
1312 list_del(&entry->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02001313 kmem_cache_free(batadv_tt_change_cache, entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001314 }
1315
Sven Eckelmann807736f2012-07-15 22:26:51 +02001316 atomic_set(&bat_priv->tt.local_changes, 0);
1317 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001318}
1319
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001320/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08001321 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1322 * @entry: the TT global entry where the orig_list_entry has to be
1323 * extracted from
1324 * @orig_node: the originator for which the orig_list_entry has to be found
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001325 *
Antonio Quartullid15cd622015-11-17 16:40:52 +08001326 * retrieve the orig_tt_list_entry belonging to orig_node from the
Antonio Quartullid657e622012-07-01 14:09:12 +02001327 * batadv_tt_global_entry list
1328 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001329 * Return: it with an increased refcounter, NULL if not found
Antonio Quartullid657e622012-07-01 14:09:12 +02001330 */
1331static struct batadv_tt_orig_list_entry *
1332batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1333 const struct batadv_orig_node *orig_node)
1334{
1335 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1336 const struct hlist_head *head;
Antonio Quartullid657e622012-07-01 14:09:12 +02001337
1338 rcu_read_lock();
1339 head = &entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001340 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
Antonio Quartullid657e622012-07-01 14:09:12 +02001341 if (tmp_orig_entry->orig_node != orig_node)
1342 continue;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001343 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
Antonio Quartullid657e622012-07-01 14:09:12 +02001344 continue;
1345
1346 orig_entry = tmp_orig_entry;
1347 break;
1348 }
1349 rcu_read_unlock();
1350
1351 return orig_entry;
1352}
1353
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001354/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08001355 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1356 * by a given originator
1357 * @entry: the TT global entry to check
1358 * @orig_node: the originator to search in the list
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001359 *
1360 * find out if an orig_node is already in the list of a tt_global_entry.
1361 *
1362 * Return: true if found, false otherwise
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001363 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001364static bool
1365batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1366 const struct batadv_orig_node *orig_node)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001367{
Antonio Quartullid657e622012-07-01 14:09:12 +02001368 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001369 bool found = false;
1370
Antonio Quartullid657e622012-07-01 14:09:12 +02001371 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1372 if (orig_entry) {
1373 found = true;
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001374 batadv_tt_orig_list_entry_put(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001375 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001376
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001377 return found;
1378}
1379
Sven Eckelmanna5130882012-05-16 20:23:16 +02001380static void
Antonio Quartullid657e622012-07-01 14:09:12 +02001381batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001382 struct batadv_orig_node *orig_node, int ttvn)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001383{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001384 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001385
Antonio Quartullid657e622012-07-01 14:09:12 +02001386 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001387 if (orig_entry) {
1388 /* refresh the ttvn: the current value could be a bogus one that
1389 * was added during a "temporary client detection"
1390 */
1391 orig_entry->ttvn = ttvn;
Antonio Quartullid657e622012-07-01 14:09:12 +02001392 goto out;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001393 }
Antonio Quartullid657e622012-07-01 14:09:12 +02001394
Sven Eckelmann86452f82016-06-25 16:44:06 +02001395 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001396 if (!orig_entry)
Antonio Quartullid657e622012-07-01 14:09:12 +02001397 goto out;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001398
1399 INIT_HLIST_NODE(&orig_entry->list);
Sven Eckelmann7c124392016-01-16 10:29:56 +01001400 kref_get(&orig_node->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001401 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001402 orig_entry->orig_node = orig_node;
1403 orig_entry->ttvn = ttvn;
Sven Eckelmann6e8ef692016-01-16 10:29:50 +01001404 kref_init(&orig_entry->refcount);
1405 kref_get(&orig_entry->refcount);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001406
Antonio Quartullid657e622012-07-01 14:09:12 +02001407 spin_lock_bh(&tt_global->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001408 hlist_add_head_rcu(&orig_entry->list,
Antonio Quartullid657e622012-07-01 14:09:12 +02001409 &tt_global->orig_list);
1410 spin_unlock_bh(&tt_global->list_lock);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001411 atomic_inc(&tt_global->orig_list_count);
1412
Antonio Quartullid657e622012-07-01 14:09:12 +02001413out:
1414 if (orig_entry)
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001415 batadv_tt_orig_list_entry_put(orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001416}
1417
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001418/**
1419 * batadv_tt_global_add - add a new TT global entry or update an existing one
1420 * @bat_priv: the bat priv with all the soft interface information
1421 * @orig_node: the originator announcing the client
1422 * @tt_addr: the mac address of the non-mesh client
Antonio Quartullic018ad32013-06-04 12:11:39 +02001423 * @vid: VLAN identifier
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001424 * @flags: TT flags that have to be set for this non-mesh client
1425 * @ttvn: the tt version number ever announcing this non-mesh client
1426 *
1427 * Add a new TT global entry for the given originator. If the entry already
1428 * exists add a new reference to the given originator (a global entry can have
1429 * references to multiple originators) and adjust the flags attribute to reflect
1430 * the function argument.
1431 * If a TT local entry exists for this non-mesh client remove it.
1432 *
1433 * The caller must hold orig_node refcount.
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001434 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001435 * Return: true if the new entry has been added, false otherwise
Antonio Quartullid4ff40f2013-04-18 15:13:01 +02001436 */
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001437static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1438 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001439 const unsigned char *tt_addr,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001440 unsigned short vid, u16 flags, u8 ttvn)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001441{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001442 struct batadv_tt_global_entry *tt_global_entry;
1443 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001444 bool ret = false;
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001445 int hash_added;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001446 struct batadv_tt_common_entry *common;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001447 u16 local_flags;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001448
Antonio Quartullicfd4f752013-08-07 18:28:56 +02001449 /* ignore global entries from backbone nodes */
1450 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1451 return true;
1452
Antonio Quartullic018ad32013-06-04 12:11:39 +02001453 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1454 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001455
1456 /* if the node already has a local client for this entry, it has to wait
1457 * for a roaming advertisement instead of manually messing up the global
1458 * table
1459 */
1460 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1461 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1462 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001463
Antonio Quartullia73105b2011-04-27 14:27:44 +02001464 if (!tt_global_entry) {
Sven Eckelmann86452f82016-06-25 16:44:06 +02001465 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1466 GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001467 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001468 goto out;
1469
Sven Eckelmannc0a55922012-05-12 13:48:55 +02001470 common = &tt_global_entry->common;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001471 ether_addr_copy(common->addr, tt_addr);
Antonio Quartullic018ad32013-06-04 12:11:39 +02001472 common->vid = vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001473
Antonio Quartullid4f44692012-05-25 00:00:54 +02001474 common->flags = flags;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001475 tt_global_entry->roam_at = 0;
Antonio Quartullifdf79322012-08-24 17:54:07 +02001476 /* node must store current time in case of roaming. This is
1477 * needed to purge this entry out on timeout (if nobody claims
1478 * it)
1479 */
1480 if (flags & BATADV_TT_CLIENT_ROAM)
1481 tt_global_entry->roam_at = jiffies;
Sven Eckelmann92dcdf02016-01-16 10:29:57 +01001482 kref_init(&common->refcount);
1483 kref_get(&common->refcount);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001484 common->added_at = jiffies;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001485
1486 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001487 atomic_set(&tt_global_entry->orig_list_count, 0);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001488 spin_lock_init(&tt_global_entry->list_lock);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001489
Sven Eckelmann807736f2012-07-15 22:26:51 +02001490 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001491 batadv_compare_tt,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001492 batadv_choose_tt, common,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001493 &common->hash_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001494
1495 if (unlikely(hash_added != 0)) {
1496 /* remove the reference for the hash */
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001497 batadv_tt_global_entry_put(tt_global_entry);
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001498 goto out_remove;
1499 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001500 } else {
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001501 common = &tt_global_entry->common;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001502 /* If there is already a global entry, we can use this one for
1503 * our processing.
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001504 * But if we are trying to add a temporary client then here are
1505 * two options at this point:
1506 * 1) the global client is not a temporary client: the global
1507 * client has to be left as it is, temporary information
1508 * should never override any already known client state
1509 * 2) the global client is a temporary client: purge the
1510 * originator list and add the new one orig_entry
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001511 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001512 if (flags & BATADV_TT_CLIENT_TEMP) {
1513 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1514 goto out;
1515 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1516 orig_node))
1517 goto out_remove;
1518 batadv_tt_global_del_orig_list(tt_global_entry);
1519 goto add_orig_entry;
1520 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001521
1522 /* if the client was temporary added before receiving the first
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001523 * OGM announcing it, we have to clear the TEMP flag. Also,
1524 * remove the previous temporary orig node and re-add it
1525 * if required. If the orig entry changed, the new one which
1526 * is a non-temporary entry is preferred.
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001527 */
Simon Wunderlicha6cb3902015-09-02 20:09:55 +02001528 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1529 batadv_tt_global_del_orig_list(tt_global_entry);
1530 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1531 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001532
Antonio Quartullie9c001362012-11-07 15:05:33 +01001533 /* the change can carry possible "attribute" flags like the
1534 * TT_CLIENT_WIFI, therefore they have to be copied in the
1535 * client entry
1536 */
Simon Wunderlichad7e2c42015-08-26 16:33:34 +02001537 common->flags |= flags;
Antonio Quartullie9c001362012-11-07 15:05:33 +01001538
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001539 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1540 * one originator left in the list and we previously received a
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001541 * delete + roaming change for this originator.
1542 *
1543 * We should first delete the old originator before adding the
1544 * new one.
1545 */
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001546 if (common->flags & BATADV_TT_CLIENT_ROAM) {
Sven Eckelmanna5130882012-05-16 20:23:16 +02001547 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001548 common->flags &= ~BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001549 tt_global_entry->roam_at = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001550 }
1551 }
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001552add_orig_entry:
Antonio Quartulli30cfd022012-07-05 23:38:29 +02001553 /* add the new orig_entry (if needed) or update it */
Antonio Quartullid657e622012-07-01 14:09:12 +02001554 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001555
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001556 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001557 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1558 common->addr, BATADV_PRINT_VID(common->vid),
1559 orig_node->orig);
Antonio Quartulli1e5d49f2013-05-05 19:32:38 +02001560 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001561
Simon Wunderlich80b3f582011-11-02 20:26:45 +01001562out_remove:
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01001563 /* Do not remove multicast addresses from the local hash on
1564 * global additions
1565 */
1566 if (is_multicast_ether_addr(tt_addr))
1567 goto out;
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001568
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569 /* remove address from local hash if present */
Antonio Quartullic018ad32013-06-04 12:11:39 +02001570 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001571 "global tt received",
Antonio Quartullic1d07432013-01-15 22:17:19 +10001572 flags & BATADV_TT_CLIENT_ROAM);
Antonio Quartulli7f91d062012-08-27 11:44:43 +02001573 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1574
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001575 if (!(flags & BATADV_TT_CLIENT_ROAM))
1576 /* this is a normal global add. Therefore the client is not in a
1577 * roaming state anymore.
1578 */
1579 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1580
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001581out:
1582 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001583 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02001584 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001585 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001586 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001587}
1588
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001589/**
1590 * batadv_transtable_best_orig - Get best originator list entry from tt entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001591 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001592 * @tt_global_entry: global translation table entry to be analyzed
1593 *
1594 * This functon assumes the caller holds rcu_read_lock().
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001595 * Return: best originator list entry or NULL on errors.
Sven Eckelmann981d8902012-10-07 13:34:15 +02001596 */
1597static struct batadv_tt_orig_list_entry *
Antonio Quartulli46274562013-09-03 11:10:24 +02001598batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1599 struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmann981d8902012-10-07 13:34:15 +02001600{
Antonio Quartulli46274562013-09-03 11:10:24 +02001601 struct batadv_neigh_node *router, *best_router = NULL;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001602 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001603 struct hlist_head *head;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001604 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001605
1606 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001607 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001608 router = batadv_orig_router_get(orig_entry->orig_node,
1609 BATADV_IF_DEFAULT);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001610 if (!router)
1611 continue;
1612
Antonio Quartulli46274562013-09-03 11:10:24 +02001613 if (best_router &&
Antonio Quartulli29824a52016-05-25 23:27:31 +08001614 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1615 BATADV_IF_DEFAULT) <= 0) {
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001616 batadv_neigh_node_put(router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001617 continue;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001618 }
1619
Antonio Quartulli46274562013-09-03 11:10:24 +02001620 /* release the refcount for the "old" best */
1621 if (best_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001622 batadv_neigh_node_put(best_router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001623
1624 best_entry = orig_entry;
1625 best_router = router;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001626 }
1627
Antonio Quartulli46274562013-09-03 11:10:24 +02001628 if (best_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001629 batadv_neigh_node_put(best_router);
Antonio Quartulli46274562013-09-03 11:10:24 +02001630
Sven Eckelmann981d8902012-10-07 13:34:15 +02001631 return best_entry;
1632}
1633
Simon Wunderlich1b371d12014-01-15 21:17:54 +01001634/**
1635 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1636 * for this global entry
Antonio Quartulli46274562013-09-03 11:10:24 +02001637 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmann981d8902012-10-07 13:34:15 +02001638 * @tt_global_entry: global translation table entry to be printed
1639 * @seq: debugfs table seq_file struct
1640 *
1641 * This functon assumes the caller holds rcu_read_lock().
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001642 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001643static void
Antonio Quartulli46274562013-09-03 11:10:24 +02001644batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1645 struct batadv_tt_global_entry *tt_global_entry,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001646 struct seq_file *seq)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001647{
Sven Eckelmann981d8902012-10-07 13:34:15 +02001648 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001649 struct batadv_tt_common_entry *tt_common_entry;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001650 struct batadv_orig_node_vlan *vlan;
1651 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001652 u8 last_ttvn;
1653 u16 flags;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001654
1655 tt_common_entry = &tt_global_entry->common;
Sven Eckelmann981d8902012-10-07 13:34:15 +02001656 flags = tt_common_entry->flags;
1657
Antonio Quartulli46274562013-09-03 11:10:24 +02001658 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001659 if (best_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001660 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1661 tt_common_entry->vid);
1662 if (!vlan) {
1663 seq_printf(seq,
1664 " * Cannot retrieve VLAN %d for originator %pM\n",
1665 BATADV_PRINT_VID(tt_common_entry->vid),
1666 best_entry->orig_node->orig);
1667 goto print_list;
1668 }
1669
Sven Eckelmann981d8902012-10-07 13:34:15 +02001670 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
Antonio Quartullif9d8a532012-11-19 09:01:42 +01001671 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001672 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001673 '*', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001674 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001675 best_entry->ttvn, best_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001676 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001677 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1678 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1679 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1680 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001681
Sven Eckelmann21754e22016-01-17 11:01:24 +01001682 batadv_orig_node_vlan_put(vlan);
Sven Eckelmann981d8902012-10-07 13:34:15 +02001683 }
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001684
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001685print_list:
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001686 head = &tt_global_entry->orig_list;
1687
Sasha Levinb67bfe02013-02-27 17:06:00 -08001688 hlist_for_each_entry_rcu(orig_entry, head, list) {
Sven Eckelmann981d8902012-10-07 13:34:15 +02001689 if (best_entry == orig_entry)
1690 continue;
1691
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001692 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1693 tt_common_entry->vid);
1694 if (!vlan) {
1695 seq_printf(seq,
1696 " + Cannot retrieve VLAN %d for originator %pM\n",
1697 BATADV_PRINT_VID(tt_common_entry->vid),
1698 orig_entry->orig_node->orig);
1699 continue;
1700 }
1701
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001702 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
Antonio Quartulli16052782013-06-04 12:11:41 +02001703 seq_printf(seq,
Antonio Quartullidd24ddb2013-11-16 12:03:49 +01001704 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
Sven Eckelmann981d8902012-10-07 13:34:15 +02001705 '+', tt_global_entry->common.addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02001706 BATADV_PRINT_VID(tt_global_entry->common.vid),
Sven Eckelmann981d8902012-10-07 13:34:15 +02001707 orig_entry->ttvn, orig_entry->orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001708 last_ttvn, vlan->tt.crc,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02001709 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1710 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1711 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1712 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001713
Sven Eckelmann21754e22016-01-17 11:01:24 +01001714 batadv_orig_node_vlan_put(vlan);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001715 }
1716}
1717
Sven Eckelmann08c36d32012-05-12 02:09:39 +02001718int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001719{
1720 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001721 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann807736f2012-07-15 22:26:51 +02001722 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001723 struct batadv_tt_common_entry *tt_common_entry;
1724 struct batadv_tt_global_entry *tt_global;
1725 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001726 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001727 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001728
Marek Lindner30da63a2012-08-03 17:15:46 +02001729 primary_if = batadv_seq_print_text_primary_if_get(seq);
1730 if (!primary_if)
Marek Lindner32ae9b22011-04-20 15:40:58 +02001731 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001732
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001733 seq_printf(seq,
1734 "Globally announced TT entries received via the mesh %s\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001735 net_dev->name);
Antonio Quartulli925a6f32016-03-12 10:30:18 +01001736 seq_puts(seq,
1737 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001738
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001739 for (i = 0; i < hash->size; i++) {
1740 head = &hash->table[i];
1741
Marek Lindner7aadf882011-02-18 12:28:09 +00001742 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001743 hlist_for_each_entry_rcu(tt_common_entry,
Marek Lindner7aadf882011-02-18 12:28:09 +00001744 head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02001745 tt_global = container_of(tt_common_entry,
1746 struct batadv_tt_global_entry,
1747 common);
Antonio Quartulli46274562013-09-03 11:10:24 +02001748 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001749 }
Marek Lindner7aadf882011-02-18 12:28:09 +00001750 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001751 }
Marek Lindner32ae9b22011-04-20 15:40:58 +02001752out:
1753 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001754 batadv_hardif_put(primary_if);
Marek Lindner30da63a2012-08-03 17:15:46 +02001755 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001756}
1757
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001758/**
Marek Lindner433ff982015-06-18 16:11:07 +08001759 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001760 * @tt_global_entry: the global entry to remove the orig_entry from
1761 * @orig_entry: the orig entry to remove and free
1762 *
1763 * Remove an orig_entry from its list in the given tt_global_entry and
1764 * free this orig_entry afterwards.
Marek Lindner433ff982015-06-18 16:11:07 +08001765 *
1766 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1767 * part of a list.
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001768 */
1769static void
Marek Lindner433ff982015-06-18 16:11:07 +08001770_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1771 struct batadv_tt_orig_list_entry *orig_entry)
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001772{
Sven Eckelmann2c72d652015-06-21 14:45:14 +02001773 lockdep_assert_held(&tt_global_entry->list_lock);
1774
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001775 batadv_tt_global_size_dec(orig_entry->orig_node,
1776 tt_global_entry->common.vid);
1777 atomic_dec(&tt_global_entry->orig_list_count);
Marek Lindner433ff982015-06-18 16:11:07 +08001778 /* requires holding tt_global_entry->list_lock and orig_entry->list
1779 * being part of a list
1780 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001781 hlist_del_rcu(&orig_entry->list);
Sven Eckelmann7e2366c2016-01-17 11:01:27 +01001782 batadv_tt_orig_list_entry_put(orig_entry);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001783}
1784
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001785/* deletes the orig list of a tt_global_entry */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001786static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001787batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001788{
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001789 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001790 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001791 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001792
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001793 spin_lock_bh(&tt_global_entry->list_lock);
1794 head = &tt_global_entry->orig_list;
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001795 hlist_for_each_entry_safe(orig_entry, safe, head, list)
Marek Lindner433ff982015-06-18 16:11:07 +08001796 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001797 spin_unlock_bh(&tt_global_entry->list_lock);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001798}
1799
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001800/**
1801 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1802 * @bat_priv: the bat priv with all the soft interface information
1803 * @tt_global_entry: the global entry to remove the orig_node from
1804 * @orig_node: the originator announcing the client
1805 * @message: message to append to the log on deletion
1806 *
1807 * Remove the given orig_node and its according orig_entry from the given
1808 * global tt entry.
1809 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001810static void
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001811batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1812 struct batadv_tt_global_entry *tt_global_entry,
1813 struct batadv_orig_node *orig_node,
1814 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001815{
1816 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001817 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001818 struct batadv_tt_orig_list_entry *orig_entry;
Antonio Quartulli16052782013-06-04 12:11:41 +02001819 unsigned short vid;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001820
1821 spin_lock_bh(&tt_global_entry->list_lock);
1822 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001823 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001824 if (orig_entry->orig_node == orig_node) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001825 vid = tt_global_entry->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001826 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001827 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001828 orig_node->orig,
Antonio Quartulli16052782013-06-04 12:11:41 +02001829 tt_global_entry->common.addr,
1830 BATADV_PRINT_VID(vid), message);
Marek Lindner433ff982015-06-18 16:11:07 +08001831 _batadv_tt_global_del_orig_entry(tt_global_entry,
1832 orig_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001833 }
1834 }
1835 spin_unlock_bh(&tt_global_entry->list_lock);
1836}
1837
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001838/* If the client is to be deleted, we check if it is the last origantor entry
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001839 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1840 * timer, otherwise we simply remove the originator scheduled for deletion.
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001841 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001842static void
Sven Eckelmann56303d32012-06-05 22:31:31 +02001843batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1844 struct batadv_tt_global_entry *tt_global_entry,
1845 struct batadv_orig_node *orig_node,
1846 const char *message)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001847{
1848 bool last_entry = true;
1849 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001850 struct batadv_tt_orig_list_entry *orig_entry;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001851
1852 /* no local entry exists, case 1:
1853 * Check if this is the last one or if other entries exist.
1854 */
1855
1856 rcu_read_lock();
1857 head = &tt_global_entry->orig_list;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001858 hlist_for_each_entry_rcu(orig_entry, head, list) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001859 if (orig_entry->orig_node != orig_node) {
1860 last_entry = false;
1861 break;
1862 }
1863 }
1864 rcu_read_unlock();
1865
1866 if (last_entry) {
1867 /* its the last one, mark for roaming. */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001868 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001869 tt_global_entry->roam_at = jiffies;
1870 } else
1871 /* there is another entry, we can simply delete this
1872 * one and can still use the other one.
1873 */
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001874 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1875 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001876}
1877
Antonio Quartullic018ad32013-06-04 12:11:39 +02001878/**
1879 * batadv_tt_global_del - remove a client from the global table
1880 * @bat_priv: the bat priv with all the soft interface information
1881 * @orig_node: an originator serving this client
1882 * @addr: the mac address of the client
1883 * @vid: VLAN identifier
1884 * @message: a message explaining the reason for deleting the client to print
1885 * for debugging purpose
1886 * @roaming: true if the deletion has been triggered by a roaming event
1887 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001888static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1889 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001890 const unsigned char *addr, unsigned short vid,
Sven Eckelmanna5130882012-05-16 20:23:16 +02001891 const char *message, bool roaming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001892{
Sven Eckelmann170173b2012-10-07 12:02:22 +02001893 struct batadv_tt_global_entry *tt_global_entry;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001894 struct batadv_tt_local_entry *local_entry = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001895
Antonio Quartullic018ad32013-06-04 12:11:39 +02001896 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001897 if (!tt_global_entry)
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001898 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001899
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001900 if (!roaming) {
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001901 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1902 orig_node, message);
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001903
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001904 if (hlist_empty(&tt_global_entry->orig_list))
Antonio Quartullibe73b482012-09-23 22:38:36 +02001905 batadv_tt_global_free(bat_priv, tt_global_entry,
1906 message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001907
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001908 goto out;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001909 }
Sven Eckelmann92f90f52011-12-22 20:31:12 +08001910
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001911 /* if we are deleting a global entry due to a roam
1912 * event, there are two possibilities:
1913 * 1) the client roamed from node A to node B => if there
1914 * is only one originator left for this client, we mark
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001915 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001916 * wait for node B to claim it. In case of timeout
1917 * the entry is purged.
1918 *
1919 * If there are other originators left, we directly delete
1920 * the originator.
1921 * 2) the client roamed to us => we can directly delete
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001922 * the global entry, since it is useless now.
1923 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001924 local_entry = batadv_tt_local_hash_find(bat_priv,
Antonio Quartullic018ad32013-06-04 12:11:39 +02001925 tt_global_entry->common.addr,
1926 vid);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001927 if (local_entry) {
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001928 /* local entry exists, case 2: client roamed to us. */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001929 batadv_tt_global_del_orig_list(tt_global_entry);
Antonio Quartullibe73b482012-09-23 22:38:36 +02001930 batadv_tt_global_free(bat_priv, tt_global_entry, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001931 } else
1932 /* no local entry exists, case 1: check for roaming */
Sven Eckelmanna5130882012-05-16 20:23:16 +02001933 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1934 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001935
Antonio Quartullicc47f662011-04-27 14:27:57 +02001936out:
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001937 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001938 batadv_tt_global_entry_put(tt_global_entry);
Sven Eckelmanna5130882012-05-16 20:23:16 +02001939 if (local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01001940 batadv_tt_local_entry_put(local_entry);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001941}
1942
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001943/**
1944 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1945 * given originator matching the provided vid
1946 * @bat_priv: the bat priv with all the soft interface information
1947 * @orig_node: the originator owning the entries to remove
1948 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1949 * removed
1950 * @message: debug message to print as "reason"
1951 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001952void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1953 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001954 s32 match_vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001955 const char *message)
Antonio Quartullia73105b2011-04-27 14:27:44 +02001956{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001957 struct batadv_tt_global_entry *tt_global;
1958 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001959 u32 i;
Sven Eckelmann807736f2012-07-15 22:26:51 +02001960 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001961 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001962 struct hlist_head *head;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001963 spinlock_t *list_lock; /* protects write access to the hash lists */
Antonio Quartulli16052782013-06-04 12:11:41 +02001964 unsigned short vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001965
Simon Wunderlich6e801492011-10-19 10:28:26 +02001966 if (!hash)
1967 return;
1968
Antonio Quartullia73105b2011-04-27 14:27:44 +02001969 for (i = 0; i < hash->size; i++) {
1970 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001971 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001972
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001973 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001974 hlist_for_each_entry_safe(tt_common_entry, safe,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +01001975 head, hash_entry) {
Antonio Quartulli95fb1302013-08-07 18:28:55 +02001976 /* remove only matching entries */
1977 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1978 continue;
1979
Sven Eckelmann56303d32012-06-05 22:31:31 +02001980 tt_global = container_of(tt_common_entry,
1981 struct batadv_tt_global_entry,
1982 common);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001983
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +01001984 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1985 orig_node, message);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02001986
Sven Eckelmann56303d32012-06-05 22:31:31 +02001987 if (hlist_empty(&tt_global->orig_list)) {
Antonio Quartulli16052782013-06-04 12:11:41 +02001988 vid = tt_global->common.vid;
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001989 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02001990 "Deleting global tt entry %pM (vid: %d): %s\n",
1991 tt_global->common.addr,
1992 BATADV_PRINT_VID(vid), message);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001993 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01001994 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001995 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02001996 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02001997 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001998 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02001999 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002000}
2001
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002002static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2003 char **msg)
Antonio Quartullicc47f662011-04-27 14:27:57 +02002004{
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002005 bool purge = false;
2006 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2007 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002008
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002009 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2010 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2011 purge = true;
2012 *msg = "Roaming timeout\n";
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002013 }
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002014
2015 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2016 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2017 purge = true;
2018 *msg = "Temporary client timeout\n";
2019 }
2020
2021 return purge;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002022}
2023
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002024static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002025{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002026 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002027 struct hlist_head *head;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002028 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002029 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002030 u32 i;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002031 char *msg = NULL;
2032 struct batadv_tt_common_entry *tt_common;
2033 struct batadv_tt_global_entry *tt_global;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002034
Antonio Quartullicc47f662011-04-27 14:27:57 +02002035 for (i = 0; i < hash->size; i++) {
2036 head = &hash->table[i];
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002037 list_lock = &hash->list_locks[i];
Antonio Quartullicc47f662011-04-27 14:27:57 +02002038
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002039 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002040 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002041 hash_entry) {
2042 tt_global = container_of(tt_common,
2043 struct batadv_tt_global_entry,
2044 common);
2045
2046 if (!batadv_tt_global_to_purge(tt_global, &msg))
2047 continue;
2048
2049 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02002050 "Deleting global tt entry %pM (vid: %d): %s\n",
2051 tt_global->common.addr,
2052 BATADV_PRINT_VID(tt_global->common.vid),
2053 msg);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002054
Sasha Levinb67bfe02013-02-27 17:06:00 -08002055 hlist_del_rcu(&tt_common->hash_entry);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002056
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002057 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002058 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002059 spin_unlock_bh(list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02002060 }
Antonio Quartullicc47f662011-04-27 14:27:57 +02002061}
2062
Sven Eckelmann56303d32012-06-05 22:31:31 +02002063static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002064{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02002065 struct batadv_hashtable *hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002066 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002067 struct batadv_tt_common_entry *tt_common_entry;
2068 struct batadv_tt_global_entry *tt_global;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002069 struct hlist_node *node_tmp;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002070 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002071 u32 i;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002072
Sven Eckelmann807736f2012-07-15 22:26:51 +02002073 if (!bat_priv->tt.global_hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002074 return;
2075
Sven Eckelmann807736f2012-07-15 22:26:51 +02002076 hash = bat_priv->tt.global_hash;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002077
2078 for (i = 0; i < hash->size; i++) {
2079 head = &hash->table[i];
2080 list_lock = &hash->list_locks[i];
2081
2082 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002083 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002084 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002085 hlist_del_rcu(&tt_common_entry->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02002086 tt_global = container_of(tt_common_entry,
2087 struct batadv_tt_global_entry,
2088 common);
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002089 batadv_tt_global_entry_put(tt_global);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002090 }
2091 spin_unlock_bh(list_lock);
2092 }
2093
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02002094 batadv_hash_destroy(hash);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02002095
Sven Eckelmann807736f2012-07-15 22:26:51 +02002096 bat_priv->tt.global_hash = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002097}
2098
Sven Eckelmann56303d32012-06-05 22:31:31 +02002099static bool
2100_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2101 struct batadv_tt_global_entry *tt_global_entry)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002102{
2103 bool ret = false;
2104
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002105 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2106 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002107 ret = true;
2108
Antonio Quartulli2d2fcc2a2013-11-16 12:03:50 +01002109 /* check if the two clients are marked as isolated */
2110 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2111 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2112 ret = true;
2113
Antonio Quartulli59b699c2011-07-07 15:35:36 +02002114 return ret;
2115}
2116
Antonio Quartullic018ad32013-06-04 12:11:39 +02002117/**
2118 * batadv_transtable_search - get the mesh destination for a given client
2119 * @bat_priv: the bat priv with all the soft interface information
2120 * @src: mac address of the source client
2121 * @addr: mac address of the destination client
2122 * @vid: VLAN identifier
2123 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002124 * Return: a pointer to the originator that was selected as destination in the
Antonio Quartullic018ad32013-06-04 12:11:39 +02002125 * mesh for contacting the client 'addr', NULL otherwise.
2126 * In case of multiple originators serving the same client, the function returns
2127 * the best one (best in terms of metric towards the destination node).
2128 *
2129 * If the two clients are AP isolated the function returns NULL.
2130 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002131struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002132 const u8 *src,
2133 const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002134 unsigned short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002135{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002136 struct batadv_tt_local_entry *tt_local_entry = NULL;
2137 struct batadv_tt_global_entry *tt_global_entry = NULL;
2138 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann981d8902012-10-07 13:34:15 +02002139 struct batadv_tt_orig_list_entry *best_entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002140
Antonio Quartullieceb22a2013-11-16 12:03:51 +01002141 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
Antonio Quartullic018ad32013-06-04 12:11:39 +02002142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
Antonio Quartulli068ee6e2012-09-23 22:38:37 +02002143 if (!tt_local_entry ||
2144 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002145 goto out;
2146 }
Marek Lindner7aadf882011-02-18 12:28:09 +00002147
Antonio Quartullic018ad32013-06-04 12:11:39 +02002148 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02002149 if (!tt_global_entry)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002150 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002151
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002152 /* check whether the clients should not communicate due to AP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002153 * isolation
2154 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02002155 if (tt_local_entry &&
2156 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002157 goto out;
2158
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002159 rcu_read_lock();
Antonio Quartulli46274562013-09-03 11:10:24 +02002160 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002161 /* found anything? */
Sven Eckelmann981d8902012-10-07 13:34:15 +02002162 if (best_entry)
2163 orig_node = best_entry->orig_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +01002164 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002165 orig_node = NULL;
2166 rcu_read_unlock();
Sven Eckelmann981d8902012-10-07 13:34:15 +02002167
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002168out:
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002169 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01002170 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002171 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01002172 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli3d393e42011-07-07 15:35:37 +02002173
Marek Lindner7b36e8e2011-02-18 12:28:10 +00002174 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002175}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002176
Antonio Quartulliced72932013-04-24 16:37:51 +02002177/**
2178 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2179 * to the given orig_node
2180 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002181 * @orig_node: originator for which the CRC should be computed
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002182 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002183 *
2184 * This function computes the checksum for the global table corresponding to a
2185 * specific originator. In particular, the checksum is computed as follows: For
2186 * each client connected to the originator the CRC32C of the MAC address and the
2187 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2188 * together.
2189 *
2190 * The idea behind is that CRC32C should be used as much as possible in order to
2191 * produce a unique hash of the table, but since the order which is used to feed
2192 * the CRC32C function affects the result and since every node in the network
2193 * probably sorts the clients differently, the hash function cannot be directly
2194 * computed over the entire table. Hence the CRC32C is used only on
2195 * the single client entry, while all the results are then xor'ed together
2196 * because the XOR operation can combine them all while trying to reduce the
2197 * noise as much as possible.
2198 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002199 * Return: the checksum of the global table of a given originator.
Antonio Quartulliced72932013-04-24 16:37:51 +02002200 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002201static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2202 struct batadv_orig_node *orig_node,
2203 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002204{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002205 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002206 struct batadv_tt_common_entry *tt_common;
2207 struct batadv_tt_global_entry *tt_global;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002208 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002209 u32 i, crc_tmp, crc = 0;
2210 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002211 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002212
2213 for (i = 0; i < hash->size; i++) {
2214 head = &hash->table[i];
2215
2216 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002217 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Sven Eckelmann56303d32012-06-05 22:31:31 +02002218 tt_global = container_of(tt_common,
2219 struct batadv_tt_global_entry,
2220 common);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002221 /* compute the CRC only for entries belonging to the
2222 * VLAN identified by the vid passed as parameter
2223 */
2224 if (tt_common->vid != vid)
2225 continue;
2226
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002227 /* Roaming clients are in the global table for
2228 * consistency only. They don't have to be
2229 * taken into account while computing the
2230 * global crc
2231 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002232 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002233 continue;
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002234 /* Temporary clients have not been announced yet, so
2235 * they have to be skipped while computing the global
2236 * crc
2237 */
2238 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2239 continue;
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002240
2241 /* find out if this global entry is announced by this
2242 * originator
2243 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002244 if (!batadv_tt_global_entry_has_orig(tt_global,
Sven Eckelmanna5130882012-05-16 20:23:16 +02002245 orig_node))
Simon Wunderlichdb08e6e2011-10-22 20:12:51 +02002246 continue;
2247
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002248 /* use network order to read the VID: this ensures that
2249 * every node reads the bytes in the same order.
2250 */
2251 tmp_vid = htons(tt_common->vid);
2252 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002253
2254 /* compute the CRC on flags that have to be kept in sync
2255 * among nodes
2256 */
2257 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2258 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2259
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002260 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002261 }
2262 rcu_read_unlock();
2263 }
2264
Antonio Quartulliced72932013-04-24 16:37:51 +02002265 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002266}
2267
Antonio Quartulliced72932013-04-24 16:37:51 +02002268/**
2269 * batadv_tt_local_crc - calculates the checksum of the local table
2270 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002271 * @vid: VLAN identifier for which the CRC32 has to be computed
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002272 *
2273 * For details about the computation, please refer to the documentation for
2274 * batadv_tt_global_crc().
2275 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002276 * Return: the checksum of the local table
Antonio Quartulliced72932013-04-24 16:37:51 +02002277 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002278static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2279 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002280{
Sven Eckelmann807736f2012-07-15 22:26:51 +02002281 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002282 struct batadv_tt_common_entry *tt_common;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002283 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002284 u32 i, crc_tmp, crc = 0;
2285 u8 flags;
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002286 __be16 tmp_vid;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002287
2288 for (i = 0; i < hash->size; i++) {
2289 head = &hash->table[i];
2290
2291 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002292 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002293 /* compute the CRC only for entries belonging to the
2294 * VLAN identified by vid
2295 */
2296 if (tt_common->vid != vid)
2297 continue;
2298
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002299 /* not yet committed clients have not to be taken into
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002300 * account while computing the CRC
2301 */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002302 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002303 continue;
Antonio Quartulliced72932013-04-24 16:37:51 +02002304
Antonio Quartullia30e22c2014-02-11 17:05:06 +01002305 /* use network order to read the VID: this ensures that
2306 * every node reads the bytes in the same order.
2307 */
2308 tmp_vid = htons(tt_common->vid);
2309 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
Antonio Quartulli0eb015682013-10-13 02:50:20 +02002310
2311 /* compute the CRC on flags that have to be kept in sync
2312 * among nodes
2313 */
2314 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2315 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2316
Antonio Quartulli0ffa9e82013-06-04 12:11:40 +02002317 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002318 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002319 rcu_read_unlock();
2320 }
2321
Antonio Quartulliced72932013-04-24 16:37:51 +02002322 return crc;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002323}
2324
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002325/**
2326 * batadv_tt_req_node_release - free tt_req node entry
2327 * @ref: kref pointer of the tt req_node entry
2328 */
2329static void batadv_tt_req_node_release(struct kref *ref)
2330{
2331 struct batadv_tt_req_node *tt_req_node;
2332
2333 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2334
Sven Eckelmann86452f82016-06-25 16:44:06 +02002335 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002336}
2337
2338/**
2339 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2340 * possibly release it
2341 * @tt_req_node: tt_req_node to be free'd
2342 */
2343static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2344{
2345 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2346}
2347
Sven Eckelmann56303d32012-06-05 22:31:31 +02002348static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002349{
Marek Lindner7c26a532015-06-28 22:16:06 +08002350 struct batadv_tt_req_node *node;
2351 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002352
Sven Eckelmann807736f2012-07-15 22:26:51 +02002353 spin_lock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002354
Marek Lindner7c26a532015-06-28 22:16:06 +08002355 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2356 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002357 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002358 }
2359
Sven Eckelmann807736f2012-07-15 22:26:51 +02002360 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002361}
2362
Sven Eckelmann56303d32012-06-05 22:31:31 +02002363static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2364 struct batadv_orig_node *orig_node,
Antonio Quartullie8cf2342013-05-28 13:14:28 +02002365 const void *tt_buff,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002366 u16 tt_buff_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002367{
Antonio Quartullia73105b2011-04-27 14:27:44 +02002368 /* Replace the old buffer only if I received something in the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002369 * last OGM (the OGM could carry no changes)
2370 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002371 spin_lock_bh(&orig_node->tt_buff_lock);
2372 if (tt_buff_len > 0) {
2373 kfree(orig_node->tt_buff);
2374 orig_node->tt_buff_len = 0;
2375 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2376 if (orig_node->tt_buff) {
2377 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2378 orig_node->tt_buff_len = tt_buff_len;
2379 }
2380 }
2381 spin_unlock_bh(&orig_node->tt_buff_lock);
2382}
2383
Sven Eckelmann56303d32012-06-05 22:31:31 +02002384static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002385{
Marek Lindner7c26a532015-06-28 22:16:06 +08002386 struct batadv_tt_req_node *node;
2387 struct hlist_node *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002388
Sven Eckelmann807736f2012-07-15 22:26:51 +02002389 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002390 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002391 if (batadv_has_timed_out(node->issued_at,
2392 BATADV_TT_REQUEST_TIMEOUT)) {
Marek Lindner7c26a532015-06-28 22:16:06 +08002393 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002394 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002395 }
2396 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002397 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002398}
2399
Marek Lindner383b8632015-06-18 16:24:24 +08002400/**
2401 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2402 * @bat_priv: the bat priv with all the soft interface information
2403 * @orig_node: orig node this request is being issued for
2404 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002405 * Return: the pointer to the new tt_req_node struct if no request
Marek Lindner383b8632015-06-18 16:24:24 +08002406 * has already been issued for this orig_node, NULL otherwise.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002407 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02002408static struct batadv_tt_req_node *
Marek Lindner383b8632015-06-18 16:24:24 +08002409batadv_tt_req_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +02002410 struct batadv_orig_node *orig_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002411{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002412 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002413
Sven Eckelmann807736f2012-07-15 22:26:51 +02002414 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08002415 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002416 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2417 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02002418 BATADV_TT_REQUEST_TIMEOUT))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002419 goto unlock;
2420 }
2421
Sven Eckelmann86452f82016-06-25 16:44:06 +02002422 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002423 if (!tt_req_node)
2424 goto unlock;
2425
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002426 kref_init(&tt_req_node->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002427 ether_addr_copy(tt_req_node->addr, orig_node->orig);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002428 tt_req_node->issued_at = jiffies;
2429
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002430 kref_get(&tt_req_node->refcount);
Marek Lindner7c26a532015-06-28 22:16:06 +08002431 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002432unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002433 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002434 return tt_req_node;
2435}
2436
Marek Lindner335fbe02013-04-23 21:40:02 +08002437/**
2438 * batadv_tt_local_valid - verify that given tt entry is a valid one
2439 * @entry_ptr: to be checked local tt entry
2440 * @data_ptr: not used but definition required to satisfy the callback prototype
2441 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002442 * Return: true if the entry is a valid, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002443 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002444static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002445{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002446 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002447
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002448 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002449 return false;
2450 return true;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02002451}
2452
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002453static bool batadv_tt_global_valid(const void *entry_ptr,
2454 const void *data_ptr)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002455{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002456 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2457 const struct batadv_tt_global_entry *tt_global_entry;
2458 const struct batadv_orig_node *orig_node = data_ptr;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002459
Antonio Quartulli30cfd022012-07-05 23:38:29 +02002460 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2461 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002462 return false;
Antonio Quartullicc47f662011-04-27 14:27:57 +02002463
Sven Eckelmann56303d32012-06-05 22:31:31 +02002464 tt_global_entry = container_of(tt_common_entry,
2465 struct batadv_tt_global_entry,
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002466 common);
2467
Sven Eckelmanna5130882012-05-16 20:23:16 +02002468 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002469}
2470
Marek Lindner335fbe02013-04-23 21:40:02 +08002471/**
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002472 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2473 * specified tt hash
Marek Lindner335fbe02013-04-23 21:40:02 +08002474 * @bat_priv: the bat priv with all the soft interface information
2475 * @hash: hash table containing the tt entries
2476 * @tt_len: expected tvlv tt data buffer length in number of bytes
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002477 * @tvlv_buff: pointer to the buffer to fill with the TT data
Marek Lindner335fbe02013-04-23 21:40:02 +08002478 * @valid_cb: function to filter tt change entries
2479 * @cb_data: data passed to the filter function as argument
Marek Lindner335fbe02013-04-23 21:40:02 +08002480 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002481static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2482 struct batadv_hashtable *hash,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002483 void *tvlv_buff, u16 tt_len,
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002484 bool (*valid_cb)(const void *,
2485 const void *),
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002486 void *cb_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002487{
Sven Eckelmann56303d32012-06-05 22:31:31 +02002488 struct batadv_tt_common_entry *tt_common_entry;
Marek Lindner335fbe02013-04-23 21:40:02 +08002489 struct batadv_tvlv_tt_change *tt_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002490 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002491 u16 tt_tot, tt_num_entries = 0;
2492 u32 i;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002493
Antonio Quartulli298e6e62013-05-28 13:14:27 +02002494 tt_tot = batadv_tt_entries(tt_len);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002495 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002496
2497 rcu_read_lock();
2498 for (i = 0; i < hash->size; i++) {
2499 head = &hash->table[i];
2500
Sasha Levinb67bfe02013-02-27 17:06:00 -08002501 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002502 head, hash_entry) {
Marek Lindner335fbe02013-04-23 21:40:02 +08002503 if (tt_tot == tt_num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002504 break;
2505
Antonio Quartulli48100ba2011-10-30 12:17:33 +01002506 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002507 continue;
2508
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01002509 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
Antonio Quartulli27b37eb2012-11-08 14:21:11 +01002510 tt_change->flags = tt_common_entry->flags;
Antonio Quartullic018ad32013-06-04 12:11:39 +02002511 tt_change->vid = htons(tt_common_entry->vid);
Antonio Quartullica663042013-12-15 13:26:55 +01002512 memset(tt_change->reserved, 0,
2513 sizeof(tt_change->reserved));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002514
Marek Lindner335fbe02013-04-23 21:40:02 +08002515 tt_num_entries++;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002516 tt_change++;
2517 }
2518 }
2519 rcu_read_unlock();
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002520}
Antonio Quartullia73105b2011-04-27 14:27:44 +02002521
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002522/**
2523 * batadv_tt_global_check_crc - check if all the CRCs are correct
2524 * @orig_node: originator for which the CRCs have to be checked
2525 * @tt_vlan: pointer to the first tvlv VLAN entry
2526 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002527 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002528 * Return: true if all the received CRCs match the locally stored ones, false
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002529 * otherwise
2530 */
2531static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2532 struct batadv_tvlv_tt_vlan_data *tt_vlan,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002533 u16 num_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002534{
2535 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2536 struct batadv_orig_node_vlan *vlan;
Simon Wunderlichc169c592015-09-02 20:09:56 +02002537 int i, orig_num_vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002538 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002539
2540 /* check if each received CRC matches the locally stored one */
2541 for (i = 0; i < num_vlan; i++) {
2542 tt_vlan_tmp = tt_vlan + i;
2543
2544 /* if orig_node is a backbone node for this VLAN, don't check
2545 * the CRC as we ignore all the global entries over it
2546 */
2547 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002548 orig_node->orig,
2549 ntohs(tt_vlan_tmp->vid)))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002550 continue;
2551
2552 vlan = batadv_orig_node_vlan_get(orig_node,
2553 ntohs(tt_vlan_tmp->vid));
2554 if (!vlan)
2555 return false;
2556
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002557 crc = vlan->tt.crc;
Sven Eckelmann21754e22016-01-17 11:01:24 +01002558 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli91c2b1a2014-01-28 02:06:47 +01002559
2560 if (crc != ntohl(tt_vlan_tmp->crc))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002561 return false;
2562 }
2563
Simon Wunderlichc169c592015-09-02 20:09:56 +02002564 /* check if any excess VLANs exist locally for the originator
2565 * which are not mentioned in the TVLV from the originator.
2566 */
2567 rcu_read_lock();
2568 orig_num_vlan = 0;
2569 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2570 orig_num_vlan++;
2571 rcu_read_unlock();
2572
2573 if (orig_num_vlan > num_vlan)
2574 return false;
2575
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002576 return true;
2577}
2578
2579/**
2580 * batadv_tt_local_update_crc - update all the local CRCs
2581 * @bat_priv: the bat priv with all the soft interface information
2582 */
2583static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2584{
2585 struct batadv_softif_vlan *vlan;
2586
2587 /* recompute the global CRC for each VLAN */
2588 rcu_read_lock();
2589 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2590 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2591 }
2592 rcu_read_unlock();
2593}
2594
2595/**
2596 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2597 * @bat_priv: the bat priv with all the soft interface information
2598 * @orig_node: the orig_node for which the CRCs have to be updated
2599 */
2600static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2601 struct batadv_orig_node *orig_node)
2602{
2603 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002604 u32 crc;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002605
2606 /* recompute the global CRC for each VLAN */
2607 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08002608 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002609 /* if orig_node is a backbone node for this VLAN, don't compute
2610 * the CRC as we ignore all the global entries over it
2611 */
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002612 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2613 vlan->vid))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002614 continue;
2615
2616 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2617 vlan->tt.crc = crc;
2618 }
2619 rcu_read_unlock();
Antonio Quartullia73105b2011-04-27 14:27:44 +02002620}
2621
Antonio Quartulliced72932013-04-24 16:37:51 +02002622/**
2623 * batadv_send_tt_request - send a TT Request message to a given node
2624 * @bat_priv: the bat priv with all the soft interface information
2625 * @dst_orig_node: the destination of the message
2626 * @ttvn: the version number that the source of the message is looking for
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002627 * @tt_vlan: pointer to the first tvlv VLAN object to request
2628 * @num_vlan: number of tvlv VLAN entries
Antonio Quartulliced72932013-04-24 16:37:51 +02002629 * @full_table: ask for the entire translation table if true, while only for the
2630 * last TT diff otherwise
Antonio Quartullid15cd622015-11-17 16:40:52 +08002631 *
2632 * Return: true if the TT Request was sent, false otherwise
Antonio Quartulliced72932013-04-24 16:37:51 +02002633 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +01002634static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2635 struct batadv_orig_node *dst_orig_node,
2636 u8 ttvn,
2637 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2638 u16 num_vlan, bool full_table)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002639{
Marek Lindner335fbe02013-04-23 21:40:02 +08002640 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002641 struct batadv_tt_req_node *tt_req_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002642 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2643 struct batadv_hard_iface *primary_if;
Marek Lindner335fbe02013-04-23 21:40:02 +08002644 bool ret = false;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002645 int i, size;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002646
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002647 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002648 if (!primary_if)
2649 goto out;
2650
2651 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002652 * reply from the same orig_node yet
2653 */
Marek Lindner383b8632015-06-18 16:24:24 +08002654 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002655 if (!tt_req_node)
2656 goto out;
2657
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002658 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2659 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
Marek Lindner335fbe02013-04-23 21:40:02 +08002660 if (!tvlv_tt_data)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002661 goto out;
2662
Marek Lindner335fbe02013-04-23 21:40:02 +08002663 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2664 tvlv_tt_data->ttvn = ttvn;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002665 tvlv_tt_data->num_vlan = htons(num_vlan);
2666
2667 /* send all the CRCs within the request. This is needed by intermediate
2668 * nodes to ensure they have the correct table before replying
2669 */
2670 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2671 for (i = 0; i < num_vlan; i++) {
2672 tt_vlan_req->vid = tt_vlan->vid;
2673 tt_vlan_req->crc = tt_vlan->crc;
2674
2675 tt_vlan_req++;
2676 tt_vlan++;
2677 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02002678
2679 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002680 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002681
Martin Hundebøllbb351ba2012-10-16 16:13:48 +02002682 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002683 dst_orig_node->orig, full_table ? 'F' : '.');
Antonio Quartullia73105b2011-04-27 14:27:44 +02002684
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002685 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
Marek Lindner335fbe02013-04-23 21:40:02 +08002686 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2687 dst_orig_node->orig, BATADV_TVLV_TT, 1,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002688 tvlv_tt_data, size);
Marek Lindner335fbe02013-04-23 21:40:02 +08002689 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002690
2691out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02002692 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002693 batadv_hardif_put(primary_if);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002694
Antonio Quartullia73105b2011-04-27 14:27:44 +02002695 if (ret && tt_req_node) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002696 spin_lock_bh(&bat_priv->tt.req_list_lock);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002697 if (!hlist_unhashed(&tt_req_node->list)) {
2698 hlist_del_init(&tt_req_node->list);
2699 batadv_tt_req_node_put(tt_req_node);
2700 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02002701 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002702 }
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02002703
2704 if (tt_req_node)
2705 batadv_tt_req_node_put(tt_req_node);
2706
Marek Lindner335fbe02013-04-23 21:40:02 +08002707 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002708 return ret;
2709}
2710
Marek Lindner335fbe02013-04-23 21:40:02 +08002711/**
2712 * batadv_send_other_tt_response - send reply to tt request concerning another
2713 * node's translation table
2714 * @bat_priv: the bat priv with all the soft interface information
2715 * @tt_data: tt data containing the tt request information
2716 * @req_src: mac address of tt request sender
2717 * @req_dst: mac address of tt request recipient
2718 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002719 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002720 */
2721static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2722 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002723 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002724{
Sven Eckelmann170173b2012-10-07 12:02:22 +02002725 struct batadv_orig_node *req_dst_orig_node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002726 struct batadv_orig_node *res_dst_orig_node = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002727 struct batadv_tvlv_tt_change *tt_change;
Marek Lindner335fbe02013-04-23 21:40:02 +08002728 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002729 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindner335fbe02013-04-23 21:40:02 +08002730 bool ret = false, full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002731 u8 orig_ttvn, req_ttvn;
2732 u16 tvlv_len;
2733 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002734
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002735 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002736 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002737 req_src, tt_data->ttvn, req_dst,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002738 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002739
2740 /* Let's get the orig node of the REAL destination */
Marek Lindner335fbe02013-04-23 21:40:02 +08002741 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002742 if (!req_dst_orig_node)
2743 goto out;
2744
Marek Lindner335fbe02013-04-23 21:40:02 +08002745 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002746 if (!res_dst_orig_node)
2747 goto out;
2748
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002749 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002750 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002751
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002752 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
Marek Lindner335fbe02013-04-23 21:40:02 +08002753 /* this node doesn't have the requested data */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002754 if (orig_ttvn != req_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002755 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2756 ntohs(tt_data->num_vlan)))
Antonio Quartullia73105b2011-04-27 14:27:44 +02002757 goto out;
2758
Antonio Quartulli015758d2011-07-09 17:52:13 +02002759 /* If the full table has been explicitly requested */
Marek Lindner335fbe02013-04-23 21:40:02 +08002760 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
Antonio Quartullia73105b2011-04-27 14:27:44 +02002761 !req_dst_orig_node->tt_buff)
2762 full_table = true;
2763 else
2764 full_table = false;
2765
Marek Lindner335fbe02013-04-23 21:40:02 +08002766 /* TT fragmentation hasn't been implemented yet, so send as many
2767 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002768 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002769 if (!full_table) {
2770 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2771 tt_len = req_dst_orig_node->tt_buff_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002772
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002773 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2774 &tvlv_tt_data,
2775 &tt_change,
2776 &tt_len);
2777 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002778 goto unlock;
2779
Antonio Quartullia73105b2011-04-27 14:27:44 +02002780 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002781 memcpy(tt_change, req_dst_orig_node->tt_buff,
Antonio Quartullia73105b2011-04-27 14:27:44 +02002782 req_dst_orig_node->tt_buff_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002783 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2784 } else {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002785 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2786 * in the initial part
2787 */
2788 tt_len = -1;
2789 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2790 &tvlv_tt_data,
2791 &tt_change,
2792 &tt_len);
2793 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002794 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002795
2796 /* fill the rest of the tvlv with the real TT entries */
2797 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2798 tt_change, tt_len,
2799 batadv_tt_global_valid,
2800 req_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002801 }
2802
Marek Lindnera19d3d82013-05-27 15:33:25 +08002803 /* Don't send the response, if larger than fragmented packet. */
2804 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2805 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2806 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2807 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2808 res_dst_orig_node->orig);
2809 goto out;
2810 }
2811
Marek Lindner335fbe02013-04-23 21:40:02 +08002812 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2813 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002814
2815 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002816 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002817
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002818 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002819 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2820 res_dst_orig_node->orig, req_dst_orig_node->orig,
2821 full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002822
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002823 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002824
Marek Lindner335fbe02013-04-23 21:40:02 +08002825 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002826 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2827 tvlv_len);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +02002828
Marek Lindner335fbe02013-04-23 21:40:02 +08002829 ret = true;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002830 goto out;
2831
2832unlock:
2833 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2834
2835out:
2836 if (res_dst_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01002837 batadv_orig_node_put(res_dst_orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002838 if (req_dst_orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01002839 batadv_orig_node_put(req_dst_orig_node);
Marek Lindner335fbe02013-04-23 21:40:02 +08002840 kfree(tvlv_tt_data);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002841 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002842}
Sven Eckelmann96412692012-06-05 22:31:30 +02002843
Marek Lindner335fbe02013-04-23 21:40:02 +08002844/**
2845 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2846 * translation table
2847 * @bat_priv: the bat priv with all the soft interface information
2848 * @tt_data: tt data containing the tt request information
2849 * @req_src: mac address of tt request sender
2850 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002851 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002852 */
2853static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2854 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002855 u8 *req_src)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002856{
Marek Lindner335fbe02013-04-23 21:40:02 +08002857 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
Sven Eckelmann56303d32012-06-05 22:31:31 +02002858 struct batadv_hard_iface *primary_if = NULL;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002859 struct batadv_tvlv_tt_change *tt_change;
2860 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002861 u8 my_ttvn, req_ttvn;
2862 u16 tvlv_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002863 bool full_table;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002864 s32 tt_len;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002865
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002866 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02002867 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08002868 req_src, tt_data->ttvn,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02002869 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02002870
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002871 spin_lock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002872
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002873 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Marek Lindner335fbe02013-04-23 21:40:02 +08002874 req_ttvn = tt_data->ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002875
Marek Lindner335fbe02013-04-23 21:40:02 +08002876 orig_node = batadv_orig_hash_find(bat_priv, req_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002877 if (!orig_node)
2878 goto out;
2879
Sven Eckelmanne5d89252012-05-12 13:48:54 +02002880 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002881 if (!primary_if)
2882 goto out;
2883
2884 /* If the full table has been explicitly requested or the gap
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002885 * is too big send the whole local translation table
2886 */
Marek Lindner335fbe02013-04-23 21:40:02 +08002887 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
Sven Eckelmann807736f2012-07-15 22:26:51 +02002888 !bat_priv->tt.last_changeset)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002889 full_table = true;
2890 else
2891 full_table = false;
2892
Marek Lindner335fbe02013-04-23 21:40:02 +08002893 /* TT fragmentation hasn't been implemented yet, so send as many
2894 * TT entries fit a single packet as possible only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02002895 */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002896 if (!full_table) {
Sven Eckelmann807736f2012-07-15 22:26:51 +02002897 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002898
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002899 tt_len = bat_priv->tt.last_changeset_len;
2900 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2901 &tvlv_tt_data,
2902 &tt_change,
2903 &tt_len);
2904 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002905 goto unlock;
2906
Marek Lindner335fbe02013-04-23 21:40:02 +08002907 /* Copy the last orig_node's OGM buffer */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002908 memcpy(tt_change, bat_priv->tt.last_changeset,
Sven Eckelmann807736f2012-07-15 22:26:51 +02002909 bat_priv->tt.last_changeset_len);
2910 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002911 } else {
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002912 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002913
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002914 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2915 * in the initial part
2916 */
2917 tt_len = -1;
2918 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2919 &tvlv_tt_data,
2920 &tt_change,
2921 &tt_len);
2922 if (!tt_len)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002923 goto out;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002924
2925 /* fill the rest of the tvlv with the real TT entries */
2926 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2927 tt_change, tt_len,
2928 batadv_tt_local_valid, NULL);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002929 }
2930
Marek Lindner335fbe02013-04-23 21:40:02 +08002931 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2932 tvlv_tt_data->ttvn = req_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002933
2934 if (full_table)
Marek Lindner335fbe02013-04-23 21:40:02 +08002935 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002936
Sven Eckelmann39c75a52012-06-03 22:19:22 +02002937 batadv_dbg(BATADV_DBG_TT, bat_priv,
Marek Lindner335fbe02013-04-23 21:40:02 +08002938 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2939 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002940
Sven Eckelmannd69909d2012-06-03 22:19:20 +02002941 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02002942
Marek Lindner335fbe02013-04-23 21:40:02 +08002943 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02002944 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2945 tvlv_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08002946
Antonio Quartullia73105b2011-04-27 14:27:44 +02002947 goto out;
2948
2949unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02002950 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002951out:
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02002952 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002953 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01002954 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002955 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01002956 batadv_hardif_put(primary_if);
Marek Lindner335fbe02013-04-23 21:40:02 +08002957 kfree(tvlv_tt_data);
2958 /* The packet was for this host, so it doesn't need to be re-routed */
Antonio Quartullia73105b2011-04-27 14:27:44 +02002959 return true;
2960}
2961
Marek Lindner335fbe02013-04-23 21:40:02 +08002962/**
2963 * batadv_send_tt_response - send reply to tt request
2964 * @bat_priv: the bat priv with all the soft interface information
2965 * @tt_data: tt data containing the tt request information
2966 * @req_src: mac address of tt request sender
2967 * @req_dst: mac address of tt request recipient
2968 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02002969 * Return: true if tt request reply was sent, false otherwise.
Marek Lindner335fbe02013-04-23 21:40:02 +08002970 */
2971static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2972 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002973 u8 *req_src, u8 *req_dst)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002974{
Antonio Quartullicfd4f752013-08-07 18:28:56 +02002975 if (batadv_is_my_mac(bat_priv, req_dst))
Marek Lindner335fbe02013-04-23 21:40:02 +08002976 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
Antonio Quartulli24820df2014-09-01 14:37:25 +02002977 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2978 req_dst);
Antonio Quartullia73105b2011-04-27 14:27:44 +02002979}
2980
Sven Eckelmann56303d32012-06-05 22:31:31 +02002981static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2982 struct batadv_orig_node *orig_node,
Marek Lindner335fbe02013-04-23 21:40:02 +08002983 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02002984 u16 tt_num_changes, u8 ttvn)
Antonio Quartullia73105b2011-04-27 14:27:44 +02002985{
2986 int i;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002987 int roams;
Antonio Quartullia73105b2011-04-27 14:27:44 +02002988
2989 for (i = 0; i < tt_num_changes; i++) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02002990 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2991 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmanna5130882012-05-16 20:23:16 +02002992 batadv_tt_global_del(bat_priv, orig_node,
2993 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02002994 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02002995 "tt removed by changes",
2996 roams);
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002997 } else {
Sven Eckelmann08c36d32012-05-12 02:09:39 +02002998 if (!batadv_tt_global_add(bat_priv, orig_node,
Antonio Quartullid4f44692012-05-25 00:00:54 +02002999 (tt_change + i)->addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003000 ntohs((tt_change + i)->vid),
Antonio Quartullid4f44692012-05-25 00:00:54 +02003001 (tt_change + i)->flags, ttvn))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003002 /* In case of problem while storing a
3003 * global_entry, we stop the updating
3004 * procedure without committing the
3005 * ttvn change. This will avoid to send
3006 * corrupted data on tt_request
3007 */
3008 return;
Sven Eckelmann08c36d32012-05-12 02:09:39 +02003009 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003010 }
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003011 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003012}
3013
Sven Eckelmann56303d32012-06-05 22:31:31 +02003014static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003015 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003016 u8 ttvn, u8 *resp_src,
3017 u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003018{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003019 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003020
Marek Lindner335fbe02013-04-23 21:40:02 +08003021 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003022 if (!orig_node)
3023 goto out;
3024
3025 /* Purge the old table first.. */
Antonio Quartulli95fb1302013-08-07 18:28:55 +02003026 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3027 "Received full table");
Antonio Quartullia73105b2011-04-27 14:27:44 +02003028
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003029 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3030 ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003031
3032 spin_lock_bh(&orig_node->tt_buff_lock);
3033 kfree(orig_node->tt_buff);
3034 orig_node->tt_buff_len = 0;
3035 orig_node->tt_buff = NULL;
3036 spin_unlock_bh(&orig_node->tt_buff_lock);
3037
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003038 atomic_set(&orig_node->last_ttvn, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003039
3040out:
3041 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003042 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003043}
3044
Sven Eckelmann56303d32012-06-05 22:31:31 +02003045static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3046 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003047 u16 tt_num_changes, u8 ttvn,
Marek Lindner335fbe02013-04-23 21:40:02 +08003048 struct batadv_tvlv_tt_change *tt_change)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003049{
Sven Eckelmanna5130882012-05-16 20:23:16 +02003050 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3051 tt_num_changes, ttvn);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003052
Antonio Quartullie8cf2342013-05-28 13:14:28 +02003053 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3054 batadv_tt_len(tt_num_changes));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003055 atomic_set(&orig_node->last_ttvn, ttvn);
3056}
3057
Antonio Quartullic018ad32013-06-04 12:11:39 +02003058/**
3059 * batadv_is_my_client - check if a client is served by the local node
3060 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli3f687852014-11-02 11:29:56 +01003061 * @addr: the mac address of the client to check
Antonio Quartullic018ad32013-06-04 12:11:39 +02003062 * @vid: VLAN identifier
3063 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003064 * Return: true if the client is served by this node, false otherwise.
Antonio Quartullic018ad32013-06-04 12:11:39 +02003065 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003066bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003067 unsigned short vid)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003068{
Sven Eckelmann170173b2012-10-07 12:02:22 +02003069 struct batadv_tt_local_entry *tt_local_entry;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003070 bool ret = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003071
Antonio Quartullic018ad32013-06-04 12:11:39 +02003072 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003073 if (!tt_local_entry)
3074 goto out;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003075 /* Check if the client has been logically deleted (but is kept for
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003076 * consistency purpose)
3077 */
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003078 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3079 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003080 goto out;
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003081 ret = true;
3082out:
Antonio Quartullia73105b2011-04-27 14:27:44 +02003083 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003084 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7683fdc2011-04-27 14:28:07 +02003085 return ret;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003086}
3087
Marek Lindner335fbe02013-04-23 21:40:02 +08003088/**
3089 * batadv_handle_tt_response - process incoming tt reply
3090 * @bat_priv: the bat priv with all the soft interface information
3091 * @tt_data: tt data containing the tt request information
3092 * @resp_src: mac address of tt reply sender
3093 * @num_entries: number of tt change entries appended to the tt data
3094 */
3095static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3096 struct batadv_tvlv_tt_data *tt_data,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003097 u8 *resp_src, u16 num_entries)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003098{
Marek Lindner7c26a532015-06-28 22:16:06 +08003099 struct batadv_tt_req_node *node;
3100 struct hlist_node *safe;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003101 struct batadv_orig_node *orig_node = NULL;
Marek Lindner335fbe02013-04-23 21:40:02 +08003102 struct batadv_tvlv_tt_change *tt_change;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003103 u8 *tvlv_ptr = (u8 *)tt_data;
3104 u16 change_offset;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003105
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003106 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003107 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
Marek Lindner335fbe02013-04-23 21:40:02 +08003108 resp_src, tt_data->ttvn, num_entries,
Sven Eckelmanna2f2b6c2015-04-23 18:22:24 +02003109 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003110
Marek Lindner335fbe02013-04-23 21:40:02 +08003111 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003112 if (!orig_node)
3113 goto out;
3114
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003115 spin_lock_bh(&orig_node->tt_lock);
3116
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003117 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3118 change_offset *= ntohs(tt_data->num_vlan);
3119 change_offset += sizeof(*tt_data);
3120 tvlv_ptr += change_offset;
3121
3122 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
Marek Lindner335fbe02013-04-23 21:40:02 +08003123 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003124 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3125 resp_src, num_entries);
Sven Eckelmann96412692012-06-05 22:31:30 +02003126 } else {
Marek Lindner335fbe02013-04-23 21:40:02 +08003127 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3128 tt_data->ttvn, tt_change);
Sven Eckelmann96412692012-06-05 22:31:30 +02003129 }
Antonio Quartullia73105b2011-04-27 14:27:44 +02003130
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003131 /* Recalculate the CRC for this orig_node and store it */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003132 batadv_tt_global_update_crc(bat_priv, orig_node);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003133
3134 spin_unlock_bh(&orig_node->tt_lock);
3135
Antonio Quartullia73105b2011-04-27 14:27:44 +02003136 /* Delete the tt_req_node from pending tt_requests list */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003137 spin_lock_bh(&bat_priv->tt.req_list_lock);
Marek Lindner7c26a532015-06-28 22:16:06 +08003138 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
Marek Lindner335fbe02013-04-23 21:40:02 +08003139 if (!batadv_compare_eth(node->addr, resp_src))
Antonio Quartullia73105b2011-04-27 14:27:44 +02003140 continue;
Marek Lindner7c26a532015-06-28 22:16:06 +08003141 hlist_del_init(&node->list);
Sven Eckelmann9c4604a2016-06-26 11:16:10 +02003142 batadv_tt_req_node_put(node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003143 }
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003144
Sven Eckelmann807736f2012-07-15 22:26:51 +02003145 spin_unlock_bh(&bat_priv->tt.req_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003146out:
3147 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003148 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003149}
3150
Sven Eckelmann56303d32012-06-05 22:31:31 +02003151static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003152{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003153 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullia73105b2011-04-27 14:27:44 +02003154
Sven Eckelmann807736f2012-07-15 22:26:51 +02003155 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003156
Sven Eckelmann807736f2012-07-15 22:26:51 +02003157 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Antonio Quartullicc47f662011-04-27 14:27:57 +02003158 list_del(&node->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02003159 kmem_cache_free(batadv_tt_roam_cache, node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003160 }
3161
Sven Eckelmann807736f2012-07-15 22:26:51 +02003162 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003163}
3164
Sven Eckelmann56303d32012-06-05 22:31:31 +02003165static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003166{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003167 struct batadv_tt_roam_node *node, *safe;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003168
Sven Eckelmann807736f2012-07-15 22:26:51 +02003169 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3170 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003171 if (!batadv_has_timed_out(node->first_time,
3172 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003173 continue;
3174
3175 list_del(&node->list);
Sven Eckelmann86452f82016-06-25 16:44:06 +02003176 kmem_cache_free(batadv_tt_roam_cache, node);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003177 }
Sven Eckelmann807736f2012-07-15 22:26:51 +02003178 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003179}
3180
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003181/**
Antonio Quartullid15cd622015-11-17 16:40:52 +08003182 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3183 * @bat_priv: the bat priv with all the soft interface information
3184 * @client: mac address of the roaming client
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003185 *
3186 * This function checks whether the client already reached the
Antonio Quartullicc47f662011-04-27 14:27:57 +02003187 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3188 * will not be sent.
3189 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003190 * Return: true if the ROAMING_ADV can be sent, false otherwise
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003191 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003192static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003193{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003194 struct batadv_tt_roam_node *tt_roam_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003195 bool ret = false;
3196
Sven Eckelmann807736f2012-07-15 22:26:51 +02003197 spin_lock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003198 /* The new tt_req will be issued only if I'm not waiting for a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003199 * reply from the same orig_node yet
3200 */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003201 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003202 if (!batadv_compare_eth(tt_roam_node->addr, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003203 continue;
3204
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003205 if (batadv_has_timed_out(tt_roam_node->first_time,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003206 BATADV_ROAMING_MAX_TIME))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003207 continue;
3208
Sven Eckelmann3e348192012-05-16 20:23:22 +02003209 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003210 /* Sorry, you roamed too many times! */
3211 goto unlock;
3212 ret = true;
3213 break;
3214 }
3215
3216 if (!ret) {
Sven Eckelmann86452f82016-06-25 16:44:06 +02003217 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3218 GFP_ATOMIC);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003219 if (!tt_roam_node)
3220 goto unlock;
3221
3222 tt_roam_node->first_time = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003223 atomic_set(&tt_roam_node->counter,
3224 BATADV_ROAMING_MAX_COUNT - 1);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01003225 ether_addr_copy(tt_roam_node->addr, client);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003226
Sven Eckelmann807736f2012-07-15 22:26:51 +02003227 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003228 ret = true;
3229 }
3230
3231unlock:
Sven Eckelmann807736f2012-07-15 22:26:51 +02003232 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003233 return ret;
3234}
3235
Antonio Quartullic018ad32013-06-04 12:11:39 +02003236/**
3237 * batadv_send_roam_adv - send a roaming advertisement message
3238 * @bat_priv: the bat priv with all the soft interface information
3239 * @client: mac address of the roaming client
3240 * @vid: VLAN identifier
3241 * @orig_node: message destination
3242 *
3243 * Send a ROAMING_ADV message to the node which was previously serving this
3244 * client. This is done to inform the node that from now on all traffic destined
3245 * for this particular roamed client has to be forwarded to the sender of the
3246 * roaming message.
3247 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003248static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003249 unsigned short vid,
Sven Eckelmann56303d32012-06-05 22:31:31 +02003250 struct batadv_orig_node *orig_node)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003251{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003252 struct batadv_hard_iface *primary_if;
Marek Lindner122edaa2013-04-23 21:40:03 +08003253 struct batadv_tvlv_roam_adv tvlv_roam;
3254
3255 primary_if = batadv_primary_if_get_selected(bat_priv);
3256 if (!primary_if)
3257 goto out;
Antonio Quartullicc47f662011-04-27 14:27:57 +02003258
3259 /* before going on we have to check whether the client has
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003260 * already roamed to us too many times
3261 */
Sven Eckelmanna5130882012-05-16 20:23:16 +02003262 if (!batadv_tt_check_roam_count(bat_priv, client))
Antonio Quartullicc47f662011-04-27 14:27:57 +02003263 goto out;
3264
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003265 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003266 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3267 orig_node->orig, client, BATADV_PRINT_VID(vid));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003268
Sven Eckelmannd69909d2012-06-03 22:19:20 +02003269 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
Martin Hundebøllf8214862012-04-20 17:02:45 +02003270
Marek Lindner122edaa2013-04-23 21:40:03 +08003271 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
Antonio Quartullic018ad32013-06-04 12:11:39 +02003272 tvlv_roam.vid = htons(vid);
Marek Lindner122edaa2013-04-23 21:40:03 +08003273
3274 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3275 orig_node->orig, BATADV_TVLV_ROAM, 1,
3276 &tvlv_roam, sizeof(tvlv_roam));
Antonio Quartullicc47f662011-04-27 14:27:57 +02003277
3278out:
Marek Lindner122edaa2013-04-23 21:40:03 +08003279 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01003280 batadv_hardif_put(primary_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003281}
3282
Sven Eckelmanna5130882012-05-16 20:23:16 +02003283static void batadv_tt_purge(struct work_struct *work)
Antonio Quartullia73105b2011-04-27 14:27:44 +02003284{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003285 struct delayed_work *delayed_work;
Sven Eckelmann807736f2012-07-15 22:26:51 +02003286 struct batadv_priv_tt *priv_tt;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003287 struct batadv_priv *bat_priv;
3288
Geliang Tang4ba4bc02015-12-28 23:43:37 +08003289 delayed_work = to_delayed_work(work);
Sven Eckelmann807736f2012-07-15 22:26:51 +02003290 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3291 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003292
Marek Lindnera19d3d82013-05-27 15:33:25 +08003293 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003294 batadv_tt_global_purge(bat_priv);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003295 batadv_tt_req_purge(bat_priv);
3296 batadv_tt_roam_purge(bat_priv);
Antonio Quartullia73105b2011-04-27 14:27:44 +02003297
Antonio Quartulli72414442012-12-25 13:14:37 +01003298 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3299 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
Antonio Quartullia73105b2011-04-27 14:27:44 +02003300}
Antonio Quartullicc47f662011-04-27 14:27:57 +02003301
Sven Eckelmann56303d32012-06-05 22:31:31 +02003302void batadv_tt_free(struct batadv_priv *bat_priv)
Antonio Quartullicc47f662011-04-27 14:27:57 +02003303{
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003304 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3305 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3306
Sven Eckelmann807736f2012-07-15 22:26:51 +02003307 cancel_delayed_work_sync(&bat_priv->tt.work);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003308
Sven Eckelmanna5130882012-05-16 20:23:16 +02003309 batadv_tt_local_table_free(bat_priv);
3310 batadv_tt_global_table_free(bat_priv);
3311 batadv_tt_req_list_free(bat_priv);
3312 batadv_tt_changes_list_free(bat_priv);
3313 batadv_tt_roam_list_free(bat_priv);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003314
Sven Eckelmann807736f2012-07-15 22:26:51 +02003315 kfree(bat_priv->tt.last_changeset);
Antonio Quartullicc47f662011-04-27 14:27:57 +02003316}
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003317
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003318/**
3319 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3320 * table and possibly count them in the TT size
3321 * @bat_priv: the bat priv with all the soft interface information
3322 * @flags: the flag to switch
3323 * @enable: whether to set or unset the flag
3324 * @count: whether to increase the TT size by the number of changed entries
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003325 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003326static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3327 bool enable, bool count)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003328{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003329 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3330 struct batadv_tt_common_entry *tt_common_entry;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003331 u16 changed_num = 0;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003332 struct hlist_head *head;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003333 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003334
3335 if (!hash)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003336 return;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003337
3338 for (i = 0; i < hash->size; i++) {
3339 head = &hash->table[i];
3340
3341 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003342 hlist_for_each_entry_rcu(tt_common_entry,
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003343 head, hash_entry) {
Antonio Quartulli697f2532011-11-07 16:47:01 +01003344 if (enable) {
3345 if ((tt_common_entry->flags & flags) == flags)
3346 continue;
3347 tt_common_entry->flags |= flags;
3348 } else {
3349 if (!(tt_common_entry->flags & flags))
3350 continue;
3351 tt_common_entry->flags &= ~flags;
3352 }
3353 changed_num++;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003354
3355 if (!count)
3356 continue;
3357
3358 batadv_tt_local_size_inc(bat_priv,
3359 tt_common_entry->vid);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003360 }
3361 rcu_read_unlock();
3362 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003363}
3364
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003365/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003366static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003367{
Sven Eckelmann807736f2012-07-15 22:26:51 +02003368 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02003369 struct batadv_tt_common_entry *tt_common;
3370 struct batadv_tt_local_entry *tt_local;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003371 struct hlist_node *node_tmp;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003372 struct hlist_head *head;
3373 spinlock_t *list_lock; /* protects write access to the hash lists */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003374 u32 i;
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003375
3376 if (!hash)
3377 return;
3378
3379 for (i = 0; i < hash->size; i++) {
3380 head = &hash->table[i];
3381 list_lock = &hash->list_locks[i];
3382
3383 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003384 hlist_for_each_entry_safe(tt_common, node_tmp, head,
Sven Eckelmannacd34af2012-06-03 22:19:21 +02003385 hash_entry) {
3386 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003387 continue;
3388
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003389 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003390 "Deleting local tt entry (%pM, vid: %d): pending\n",
3391 tt_common->addr,
3392 BATADV_PRINT_VID(tt_common->vid));
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003393
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003394 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003395 hlist_del_rcu(&tt_common->hash_entry);
Sven Eckelmann56303d32012-06-05 22:31:31 +02003396 tt_local = container_of(tt_common,
3397 struct batadv_tt_local_entry,
3398 common);
Antonio Quartulli35df3b22014-05-08 17:13:15 +02003399
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003400 batadv_tt_local_entry_put(tt_local);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003401 }
3402 spin_unlock_bh(list_lock);
3403 }
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003404}
3405
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003406/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003407 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3408 * which have been queued in the time since the last commit
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003409 * @bat_priv: the bat priv with all the soft interface information
Marek Lindnera19d3d82013-05-27 15:33:25 +08003410 *
3411 * Caller must hold tt->commit_lock.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003412 */
Marek Lindnera19d3d82013-05-27 15:33:25 +08003413static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003414{
Sven Eckelmann5274cd62015-06-21 14:45:15 +02003415 lockdep_assert_held(&bat_priv->tt.commit_lock);
3416
Linus Lüssingc5caf4e2014-02-15 17:47:49 +01003417 /* Update multicast addresses in local translation table */
3418 batadv_mcast_mla_update(bat_priv);
3419
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003420 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3421 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3422 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003423 return;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003424 }
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003425
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003426 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003427
Sven Eckelmanna5130882012-05-16 20:23:16 +02003428 batadv_tt_local_purge_pending_clients(bat_priv);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003429 batadv_tt_local_update_crc(bat_priv);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003430
3431 /* Increment the TTVN only once per OGM interval */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003432 atomic_inc(&bat_priv->tt.vn);
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003433 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02003434 "Local changes committed, updating to ttvn %u\n",
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003435 (u8)atomic_read(&bat_priv->tt.vn));
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +08003436
3437 /* reset the sending counter */
Sven Eckelmann807736f2012-07-15 22:26:51 +02003438 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003439 batadv_tt_tvlv_container_update(bat_priv);
Marek Lindnera19d3d82013-05-27 15:33:25 +08003440}
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003441
Marek Lindnera19d3d82013-05-27 15:33:25 +08003442/**
3443 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3444 * have been queued in the time since the last commit
3445 * @bat_priv: the bat priv with all the soft interface information
3446 */
3447void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3448{
3449 spin_lock_bh(&bat_priv->tt.commit_lock);
3450 batadv_tt_local_commit_changes_nolock(bat_priv);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003451 spin_unlock_bh(&bat_priv->tt.commit_lock);
Antonio Quartulli058d0e22011-07-07 01:40:58 +02003452}
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003453
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003454bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3455 unsigned short vid)
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003456{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003457 struct batadv_tt_local_entry *tt_local_entry = NULL;
3458 struct batadv_tt_global_entry *tt_global_entry = NULL;
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003459 struct batadv_softif_vlan *vlan;
Marek Lindner5870adc2012-06-20 17:16:05 +02003460 bool ret = false;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003461
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003462 vlan = batadv_softif_vlan_get(bat_priv, vid);
Markus Elfringe087f342015-11-03 19:20:34 +01003463 if (!vlan)
3464 return false;
3465
3466 if (!atomic_read(&vlan->ap_isolation))
Marek Lindner5870adc2012-06-20 17:16:05 +02003467 goto out;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003468
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003469 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003470 if (!tt_local_entry)
3471 goto out;
3472
Antonio Quartullib8cbd812013-07-02 11:04:36 +02003473 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003474 if (!tt_global_entry)
3475 goto out;
3476
Antonio Quartulli1f129fe2012-06-25 20:49:50 +00003477 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003478 goto out;
3479
Marek Lindner5870adc2012-06-20 17:16:05 +02003480 ret = true;
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003481
3482out:
Sven Eckelmann9c3bf082016-01-17 11:01:21 +01003483 batadv_softif_vlan_put(vlan);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003484 if (tt_global_entry)
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01003485 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003486 if (tt_local_entry)
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003487 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli59b699c2011-07-07 15:35:36 +02003488 return ret;
3489}
Marek Lindnera943cac2011-07-30 13:10:18 +02003490
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003491/**
3492 * batadv_tt_update_orig - update global translation table with new tt
3493 * information received via ogms
3494 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmanne51f0392015-09-06 21:38:51 +02003495 * @orig_node: the orig_node of the ogm
3496 * @tt_buff: pointer to the first tvlv VLAN entry
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003497 * @tt_num_vlan: number of tvlv VLAN entries
3498 * @tt_change: pointer to the first entry in the TT buffer
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003499 * @tt_num_changes: number of tt changes inside the tt buffer
3500 * @ttvn: translation table version number of this changeset
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003501 */
3502static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3503 struct batadv_orig_node *orig_node,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003504 const void *tt_buff, u16 tt_num_vlan,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003505 struct batadv_tvlv_tt_change *tt_change,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003506 u16 tt_num_changes, u8 ttvn)
Marek Lindnera943cac2011-07-30 13:10:18 +02003507{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003508 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003509 struct batadv_tvlv_tt_vlan_data *tt_vlan;
Marek Lindnera943cac2011-07-30 13:10:18 +02003510 bool full_table = true;
Linus Lüssinge17931d2014-02-15 17:47:50 +01003511 bool has_tt_init;
Marek Lindnera943cac2011-07-30 13:10:18 +02003512
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003513 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
Linus Lüssingac4eebd2015-06-16 17:10:24 +02003514 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3515 &orig_node->capa_initialized);
Linus Lüssinge17931d2014-02-15 17:47:50 +01003516
Antonio Quartulli17071572011-11-07 16:36:40 +01003517 /* orig table not initialised AND first diff is in the OGM OR the ttvn
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003518 * increased by one -> we can apply the attached changes
3519 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003520 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003521 /* the OGM could not contain the changes due to their size or
Sven Eckelmann42d0b042012-06-03 22:19:17 +02003522 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3523 * times.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003524 * In this case send a tt request
3525 */
Marek Lindnera943cac2011-07-30 13:10:18 +02003526 if (!tt_num_changes) {
3527 full_table = false;
3528 goto request_table;
3529 }
3530
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003531 spin_lock_bh(&orig_node->tt_lock);
3532
Sven Eckelmanna5130882012-05-16 20:23:16 +02003533 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
Sven Eckelmann96412692012-06-05 22:31:30 +02003534 ttvn, tt_change);
Marek Lindnera943cac2011-07-30 13:10:18 +02003535
3536 /* Even if we received the precomputed crc with the OGM, we
3537 * prefer to recompute it to spot any possible inconsistency
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003538 * in the global table
3539 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003540 batadv_tt_global_update_crc(bat_priv, orig_node);
Marek Lindnera943cac2011-07-30 13:10:18 +02003541
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02003542 spin_unlock_bh(&orig_node->tt_lock);
3543
Marek Lindnera943cac2011-07-30 13:10:18 +02003544 /* The ttvn alone is not enough to guarantee consistency
3545 * because a single value could represent different states
3546 * (due to the wrap around). Thus a node has to check whether
3547 * the resulting table (after applying the changes) is still
3548 * consistent or not. E.g. a node could disconnect while its
3549 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3550 * checking the CRC value is mandatory to detect the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003551 * inconsistency
3552 */
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003553 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3554 tt_num_vlan))
Marek Lindnera943cac2011-07-30 13:10:18 +02003555 goto request_table;
Marek Lindnera943cac2011-07-30 13:10:18 +02003556 } else {
3557 /* if we missed more than one change or our tables are not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02003558 * in sync anymore -> request fresh tt data
3559 */
Linus Lüssinge17931d2014-02-15 17:47:50 +01003560 if (!has_tt_init || ttvn != orig_ttvn ||
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003561 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3562 tt_num_vlan)) {
Marek Lindnera943cac2011-07-30 13:10:18 +02003563request_table:
Sven Eckelmann39c75a52012-06-03 22:19:22 +02003564 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003565 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3566 orig_node->orig, ttvn, orig_ttvn,
3567 tt_num_changes);
Sven Eckelmanna5130882012-05-16 20:23:16 +02003568 batadv_send_tt_request(bat_priv, orig_node, ttvn,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003569 tt_vlan, tt_num_vlan,
3570 full_table);
Marek Lindnera943cac2011-07-30 13:10:18 +02003571 return;
3572 }
3573 }
3574}
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003575
Antonio Quartullic018ad32013-06-04 12:11:39 +02003576/**
3577 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3578 * @bat_priv: the bat priv with all the soft interface information
3579 * @addr: the mac address of the client to check
3580 * @vid: VLAN identifier
3581 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003582 * Return: true if we know that the client has moved from its old originator
Antonio Quartullic018ad32013-06-04 12:11:39 +02003583 * to another one. This entry is still kept for consistency purposes and will be
3584 * deleted later by a DEL or because of timeout
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003585 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02003586bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003587 u8 *addr, unsigned short vid)
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003588{
Sven Eckelmann56303d32012-06-05 22:31:31 +02003589 struct batadv_tt_global_entry *tt_global_entry;
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003590 bool ret = false;
3591
Antonio Quartullic018ad32013-06-04 12:11:39 +02003592 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003593 if (!tt_global_entry)
3594 goto out;
3595
Antonio Quartullic1d07432013-01-15 22:17:19 +10003596 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01003597 batadv_tt_global_entry_put(tt_global_entry);
Antonio Quartulli3275e7c2012-03-16 18:03:28 +01003598out:
3599 return ret;
3600}
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003601
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003602/**
3603 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3604 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartullic018ad32013-06-04 12:11:39 +02003605 * @addr: the mac address of the local client to query
3606 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003607 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003608 * Return: true if the local client is known to be roaming (it is not served by
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003609 * this node anymore) or not. If yes, the client is still present in the table
3610 * to keep the latter consistent with the node TTVN
3611 */
3612bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003613 u8 *addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003614{
3615 struct batadv_tt_local_entry *tt_local_entry;
3616 bool ret = false;
3617
Antonio Quartullic018ad32013-06-04 12:11:39 +02003618 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003619 if (!tt_local_entry)
3620 goto out;
3621
3622 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
Sven Eckelmann95c0db92016-01-17 11:01:25 +01003623 batadv_tt_local_entry_put(tt_local_entry);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003624out:
3625 return ret;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02003626}
3627
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003628bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3629 struct batadv_orig_node *orig_node,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003630 const unsigned char *addr,
Antonio Quartulli16052782013-06-04 12:11:41 +02003631 unsigned short vid)
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003632{
3633 bool ret = false;
3634
Antonio Quartulli16052782013-06-04 12:11:41 +02003635 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003636 BATADV_TT_CLIENT_TEMP,
3637 atomic_read(&orig_node->last_ttvn)))
3638 goto out;
3639
3640 batadv_dbg(BATADV_DBG_TT, bat_priv,
Antonio Quartulli16052782013-06-04 12:11:41 +02003641 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3642 addr, BATADV_PRINT_VID(vid), orig_node->orig);
Antonio Quartulli30cfd022012-07-05 23:38:29 +02003643 ret = true;
3644out:
3645 return ret;
3646}
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003647
3648/**
Marek Lindnera19d3d82013-05-27 15:33:25 +08003649 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3650 * maximum packet size that can be transported through the mesh
3651 * @soft_iface: netdev struct of the mesh interface
3652 *
3653 * Remove entries older than 'timeout' and half timeout if more entries need
3654 * to be removed.
3655 */
3656void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3657{
3658 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3659 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3660 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3661 bool reduced = false;
3662
3663 spin_lock_bh(&bat_priv->tt.commit_lock);
3664
3665 while (true) {
3666 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3667 if (packet_size_max >= table_size)
3668 break;
3669
3670 batadv_tt_local_purge(bat_priv, timeout);
3671 batadv_tt_local_purge_pending_clients(bat_priv);
3672
3673 timeout /= 2;
3674 reduced = true;
3675 net_ratelimited_function(batadv_info, soft_iface,
3676 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3677 packet_size_max);
3678 }
3679
3680 /* commit these changes immediately, to avoid synchronization problem
3681 * with the TTVN
3682 */
3683 if (reduced)
3684 batadv_tt_local_commit_changes_nolock(bat_priv);
3685
3686 spin_unlock_bh(&bat_priv->tt.commit_lock);
3687}
3688
3689/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003690 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3691 * @bat_priv: the bat priv with all the soft interface information
3692 * @orig: the orig_node of the ogm
3693 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3694 * @tvlv_value: tvlv buffer containing the gateway data
3695 * @tvlv_value_len: tvlv buffer length
3696 */
3697static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3698 struct batadv_orig_node *orig,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003699 u8 flags, void *tvlv_value,
3700 u16 tvlv_value_len)
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003701{
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003702 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3703 struct batadv_tvlv_tt_change *tt_change;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003704 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003705 u16 num_entries, num_vlan;
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003706
3707 if (tvlv_value_len < sizeof(*tt_data))
3708 return;
3709
3710 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3711 tvlv_value_len -= sizeof(*tt_data);
3712
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003713 num_vlan = ntohs(tt_data->num_vlan);
3714
3715 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3716 return;
3717
3718 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3719 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3720 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3721
Antonio Quartulli298e6e62013-05-28 13:14:27 +02003722 num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003723
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003724 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3725 num_entries, tt_data->ttvn);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003726}
3727
3728/**
Marek Lindner335fbe02013-04-23 21:40:02 +08003729 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3730 * container
3731 * @bat_priv: the bat priv with all the soft interface information
3732 * @src: mac address of tt tvlv sender
3733 * @dst: mac address of tt tvlv recipient
3734 * @tvlv_value: tvlv buffer containing the tt data
3735 * @tvlv_value_len: tvlv buffer length
3736 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003737 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner335fbe02013-04-23 21:40:02 +08003738 * otherwise.
3739 */
3740static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003741 u8 *src, u8 *dst,
Marek Lindner335fbe02013-04-23 21:40:02 +08003742 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003743 u16 tvlv_value_len)
Marek Lindner335fbe02013-04-23 21:40:02 +08003744{
3745 struct batadv_tvlv_tt_data *tt_data;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003746 u16 tt_vlan_len, tt_num_entries;
Marek Lindner335fbe02013-04-23 21:40:02 +08003747 char tt_flag;
3748 bool ret;
3749
3750 if (tvlv_value_len < sizeof(*tt_data))
3751 return NET_RX_SUCCESS;
3752
3753 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3754 tvlv_value_len -= sizeof(*tt_data);
3755
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003756 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3757 tt_vlan_len *= ntohs(tt_data->num_vlan);
3758
3759 if (tvlv_value_len < tt_vlan_len)
3760 return NET_RX_SUCCESS;
3761
3762 tvlv_value_len -= tt_vlan_len;
3763 tt_num_entries = batadv_tt_entries(tvlv_value_len);
Marek Lindner335fbe02013-04-23 21:40:02 +08003764
3765 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3766 case BATADV_TT_REQUEST:
3767 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3768
3769 /* If this node cannot provide a TT response the tt_request is
3770 * forwarded
3771 */
3772 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3773 if (!ret) {
3774 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3775 tt_flag = 'F';
3776 else
3777 tt_flag = '.';
3778
3779 batadv_dbg(BATADV_DBG_TT, bat_priv,
3780 "Routing TT_REQUEST to %pM [%c]\n",
3781 dst, tt_flag);
3782 /* tvlv API will re-route the packet */
3783 return NET_RX_DROP;
3784 }
3785 break;
3786 case BATADV_TT_RESPONSE:
3787 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3788
3789 if (batadv_is_my_mac(bat_priv, dst)) {
3790 batadv_handle_tt_response(bat_priv, tt_data,
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02003791 src, tt_num_entries);
Marek Lindner335fbe02013-04-23 21:40:02 +08003792 return NET_RX_SUCCESS;
3793 }
3794
3795 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3796 tt_flag = 'F';
3797 else
3798 tt_flag = '.';
3799
3800 batadv_dbg(BATADV_DBG_TT, bat_priv,
3801 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3802
3803 /* tvlv API will re-route the packet */
3804 return NET_RX_DROP;
3805 }
3806
3807 return NET_RX_SUCCESS;
3808}
3809
3810/**
Marek Lindner122edaa2013-04-23 21:40:03 +08003811 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3812 * @bat_priv: the bat priv with all the soft interface information
3813 * @src: mac address of tt tvlv sender
3814 * @dst: mac address of tt tvlv recipient
3815 * @tvlv_value: tvlv buffer containing the tt data
3816 * @tvlv_value_len: tvlv buffer length
3817 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003818 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
Marek Lindner122edaa2013-04-23 21:40:03 +08003819 * otherwise.
3820 */
3821static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003822 u8 *src, u8 *dst,
Marek Lindner122edaa2013-04-23 21:40:03 +08003823 void *tvlv_value,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003824 u16 tvlv_value_len)
Marek Lindner122edaa2013-04-23 21:40:03 +08003825{
3826 struct batadv_tvlv_roam_adv *roaming_adv;
3827 struct batadv_orig_node *orig_node = NULL;
3828
3829 /* If this node is not the intended recipient of the
3830 * roaming advertisement the packet is forwarded
3831 * (the tvlv API will re-route the packet).
3832 */
3833 if (!batadv_is_my_mac(bat_priv, dst))
3834 return NET_RX_DROP;
3835
Marek Lindner122edaa2013-04-23 21:40:03 +08003836 if (tvlv_value_len < sizeof(*roaming_adv))
3837 goto out;
3838
3839 orig_node = batadv_orig_hash_find(bat_priv, src);
3840 if (!orig_node)
3841 goto out;
3842
3843 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3844 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3845
3846 batadv_dbg(BATADV_DBG_TT, bat_priv,
3847 "Received ROAMING_ADV from %pM (client %pM)\n",
3848 src, roaming_adv->client);
3849
3850 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
Antonio Quartullic018ad32013-06-04 12:11:39 +02003851 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
Marek Lindner122edaa2013-04-23 21:40:03 +08003852 atomic_read(&orig_node->last_ttvn) + 1);
3853
3854out:
3855 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01003856 batadv_orig_node_put(orig_node);
Marek Lindner122edaa2013-04-23 21:40:03 +08003857 return NET_RX_SUCCESS;
3858}
3859
3860/**
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003861 * batadv_tt_init - initialise the translation table internals
3862 * @bat_priv: the bat priv with all the soft interface information
3863 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003864 * Return: 0 on success or negative error number in case of failure.
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003865 */
3866int batadv_tt_init(struct batadv_priv *bat_priv)
3867{
3868 int ret;
3869
Antonio Quartulli0eb015682013-10-13 02:50:20 +02003870 /* synchronized flags must be remote */
3871 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3872
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003873 ret = batadv_tt_local_init(bat_priv);
3874 if (ret < 0)
3875 return ret;
3876
3877 ret = batadv_tt_global_init(bat_priv);
3878 if (ret < 0)
3879 return ret;
3880
3881 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
Marek Lindner335fbe02013-04-23 21:40:02 +08003882 batadv_tt_tvlv_unicast_handler_v1,
3883 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003884
Marek Lindner122edaa2013-04-23 21:40:03 +08003885 batadv_tvlv_handler_register(bat_priv, NULL,
3886 batadv_roam_tvlv_unicast_handler_v1,
3887 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3888
Marek Lindnere1bf0c12013-04-23 21:40:01 +08003889 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3890 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3891 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3892
3893 return 1;
3894}
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003895
3896/**
3897 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3898 * @bat_priv: the bat priv with all the soft interface information
3899 * @addr: the mac address of the client
3900 * @vid: the identifier of the VLAN where this client is connected
3901 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02003902 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003903 * otherwise
3904 */
3905bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02003906 const u8 *addr, unsigned short vid)
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003907{
3908 struct batadv_tt_global_entry *tt;
3909 bool ret;
3910
3911 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3912 if (!tt)
3913 return false;
3914
3915 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3916
Sven Eckelmann5dafd8a2016-01-17 11:01:26 +01003917 batadv_tt_global_entry_put(tt);
Antonio Quartulli42cb0be2013-11-16 12:03:52 +01003918
3919 return ret;
3920}
Sven Eckelmann86452f82016-06-25 16:44:06 +02003921
3922/**
3923 * batadv_tt_cache_init - Initialize tt memory object cache
3924 *
3925 * Return: 0 on success or negative error number in case of failure.
3926 */
3927int __init batadv_tt_cache_init(void)
3928{
3929 size_t tl_size = sizeof(struct batadv_tt_local_entry);
3930 size_t tg_size = sizeof(struct batadv_tt_global_entry);
3931 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
3932 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
3933 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
3934 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
3935
3936 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
3937 SLAB_HWCACHE_ALIGN, NULL);
3938 if (!batadv_tl_cache)
3939 return -ENOMEM;
3940
3941 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
3942 SLAB_HWCACHE_ALIGN, NULL);
3943 if (!batadv_tg_cache)
3944 goto err_tt_tl_destroy;
3945
3946 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
3947 tt_orig_size, 0,
3948 SLAB_HWCACHE_ALIGN, NULL);
3949 if (!batadv_tt_orig_cache)
3950 goto err_tt_tg_destroy;
3951
3952 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
3953 tt_change_size, 0,
3954 SLAB_HWCACHE_ALIGN, NULL);
3955 if (!batadv_tt_change_cache)
3956 goto err_tt_orig_destroy;
3957
3958 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
3959 tt_req_size, 0,
3960 SLAB_HWCACHE_ALIGN, NULL);
3961 if (!batadv_tt_req_cache)
3962 goto err_tt_change_destroy;
3963
3964 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
3965 tt_roam_size, 0,
3966 SLAB_HWCACHE_ALIGN, NULL);
3967 if (!batadv_tt_roam_cache)
3968 goto err_tt_req_destroy;
3969
3970 return 0;
3971
3972err_tt_req_destroy:
3973 kmem_cache_destroy(batadv_tt_req_cache);
3974 batadv_tt_req_cache = NULL;
3975err_tt_change_destroy:
3976 kmem_cache_destroy(batadv_tt_change_cache);
3977 batadv_tt_change_cache = NULL;
3978err_tt_orig_destroy:
3979 kmem_cache_destroy(batadv_tt_orig_cache);
3980 batadv_tt_orig_cache = NULL;
3981err_tt_tg_destroy:
3982 kmem_cache_destroy(batadv_tg_cache);
3983 batadv_tg_cache = NULL;
3984err_tt_tl_destroy:
3985 kmem_cache_destroy(batadv_tl_cache);
3986 batadv_tl_cache = NULL;
3987
3988 return -ENOMEM;
3989}
3990
3991/**
3992 * batadv_tt_cache_destroy - Destroy tt memory object cache
3993 */
3994void batadv_tt_cache_destroy(void)
3995{
3996 kmem_cache_destroy(batadv_tl_cache);
3997 kmem_cache_destroy(batadv_tg_cache);
3998 kmem_cache_destroy(batadv_tt_orig_cache);
3999 kmem_cache_destroy(batadv_tt_change_cache);
4000 kmem_cache_destroy(batadv_tt_req_cache);
4001 kmem_cache_destroy(batadv_tt_roam_cache);
4002}