]> Git Repo - linux.git/blame - net/batman-adv/translation-table.c
batman-adv: netlink: hardif query
[linux.git] / net / batman-adv / translation-table.c
CommitLineData
0046b040 1/* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
c6c8fea2 2 *
35c133a0 3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
c6c8fea2
SE
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
c6c8fea2 18#include "translation-table.h"
1e2c2a4f
SE
19#include "main.h"
20
21#include <linux/atomic.h>
ac4eebd4 22#include <linux/bitops.h>
1e2c2a4f
SE
23#include <linux/bug.h>
24#include <linux/byteorder/generic.h>
86452f81 25#include <linux/cache.h>
1e2c2a4f
SE
26#include <linux/compiler.h>
27#include <linux/crc32c.h>
28#include <linux/errno.h>
29#include <linux/etherdevice.h>
30#include <linux/fs.h>
31#include <linux/if_ether.h>
86452f81 32#include <linux/init.h>
1e2c2a4f
SE
33#include <linux/jhash.h>
34#include <linux/jiffies.h>
35#include <linux/kernel.h>
6e8ef69d 36#include <linux/kref.h>
1e2c2a4f
SE
37#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>
1e2c2a4f
SE
48
49#include "bridge_loop_avoidance.h"
32ae9b22 50#include "hard-interface.h"
c6c8fea2 51#include "hash.h"
ba412080 52#include "log.h"
c5caf4ef 53#include "multicast.h"
1e2c2a4f
SE
54#include "originator.h"
55#include "packet.h"
56#include "soft-interface.h"
1f8dce49 57#include "tvlv.h"
a73105b8 58
86452f81
SE
59static 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
dec05074
AQ
66/* 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
6b5e971a 70static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
c018ad3d 71 unsigned short vid,
56303d34 72 struct batadv_orig_node *orig_node);
a513088d
SE
73static void batadv_tt_purge(struct work_struct *work);
74static void
56303d34 75batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
30cfd02b
AQ
76static void batadv_tt_global_del(struct batadv_priv *bat_priv,
77 struct batadv_orig_node *orig_node,
78 const unsigned char *addr,
c018ad3d
AQ
79 unsigned short vid, const char *message,
80 bool roaming);
c6c8fea2 81
62fe710f 82/**
d15cd622
AQ
83 * 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
62fe710f 86 *
d15cd622
AQ
87 * Compare the MAC address and the VLAN ID of the two TT entries and check if
88 * they are the same TT client.
4b426b10 89 * Return: true if the two TT clients are the same, false otherwise
62fe710f 90 */
4b426b10 91static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
7aadf889 92{
56303d34 93 const void *data1 = container_of(node, struct batadv_tt_common_entry,
747e4221 94 hash_entry);
4c718958
ML
95 const struct batadv_tt_common_entry *tt1 = data1;
96 const struct batadv_tt_common_entry *tt2 = data2;
7aadf889 97
4c718958 98 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
7aadf889
ML
99}
100
c018ad3d
AQ
101/**
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 *
62fe710f 106 * Return: the hash index where the object represented by 'data' should be
c018ad3d
AQ
107 * stored at.
108 */
6b5e971a 109static inline u32 batadv_choose_tt(const void *data, u32 size)
c018ad3d
AQ
110{
111 struct batadv_tt_common_entry *tt;
6b5e971a 112 u32 hash = 0;
c018ad3d
AQ
113
114 tt = (struct batadv_tt_common_entry *)data;
36fd61cb
SE
115 hash = jhash(&tt->addr, ETH_ALEN, hash);
116 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
c018ad3d
AQ
117
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 *
62fe710f 127 * Return: a pointer to the tt_common struct belonging to the searched client if
c018ad3d
AQ
128 * found, NULL otherwise.
129 */
56303d34 130static struct batadv_tt_common_entry *
6b5e971a 131batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
c018ad3d 132 unsigned short vid)
7aadf889 133{
7aadf889 134 struct hlist_head *head;
c018ad3d 135 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
6b5e971a 136 u32 index;
7aadf889
ML
137
138 if (!hash)
139 return NULL;
140
8fdd0153 141 ether_addr_copy(to_search.addr, addr);
c018ad3d
AQ
142 to_search.vid = vid;
143
144 index = batadv_choose_tt(&to_search, hash->size);
7aadf889
ML
145 head = &hash->table[index];
146
147 rcu_read_lock();
c018ad3d
AQ
148 hlist_for_each_entry_rcu(tt, head, hash_entry) {
149 if (!batadv_compare_eth(tt, addr))
150 continue;
151
152 if (tt->vid != vid)
7aadf889
ML
153 continue;
154
92dcdf09 155 if (!kref_get_unless_zero(&tt->refcount))
7683fdc1
AQ
156 continue;
157
c018ad3d 158 tt_tmp = tt;
7aadf889
ML
159 break;
160 }
161 rcu_read_unlock();
162
c018ad3d 163 return tt_tmp;
7aadf889
ML
164}
165
c018ad3d
AQ
166/**
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 *
62fe710f 172 * Return: a pointer to the corresponding tt_local_entry struct if the client is
c018ad3d
AQ
173 * found, NULL otherwise.
174 */
56303d34 175static struct batadv_tt_local_entry *
6b5e971a 176batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
c018ad3d 177 unsigned short vid)
7aadf889 178{
56303d34
SE
179 struct batadv_tt_common_entry *tt_common_entry;
180 struct batadv_tt_local_entry *tt_local_entry = NULL;
7aadf889 181
c018ad3d
AQ
182 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
183 vid);
48100bac
AQ
184 if (tt_common_entry)
185 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
186 struct batadv_tt_local_entry,
187 common);
48100bac
AQ
188 return tt_local_entry;
189}
7aadf889 190
c018ad3d
AQ
191/**
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 *
62fe710f 197 * Return: a pointer to the corresponding tt_global_entry struct if the client
c018ad3d
AQ
198 * is found, NULL otherwise.
199 */
56303d34 200static struct batadv_tt_global_entry *
6b5e971a 201batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
c018ad3d 202 unsigned short vid)
48100bac 203{
56303d34
SE
204 struct batadv_tt_common_entry *tt_common_entry;
205 struct batadv_tt_global_entry *tt_global_entry = NULL;
7683fdc1 206
c018ad3d
AQ
207 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
208 vid);
48100bac
AQ
209 if (tt_common_entry)
210 tt_global_entry = container_of(tt_common_entry,
56303d34
SE
211 struct batadv_tt_global_entry,
212 common);
48100bac 213 return tt_global_entry;
7aadf889
ML
214}
215
86452f81
SE
216/**
217 * 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
92dcdf09
SE
230/**
231 * 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)
236{
237 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
a33d970d
SE
242 batadv_softif_vlan_put(tt_local_entry->vlan);
243
86452f81 244 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
92dcdf09
SE
245}
246
247/**
95c0db90 248 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
92dcdf09
SE
249 * possibly release it
250 * @tt_local_entry: tt_local_entry to be free'd
251 */
a513088d 252static void
95c0db90 253batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
7683fdc1 254{
92dcdf09
SE
255 kref_put(&tt_local_entry->common.refcount,
256 batadv_tt_local_entry_release);
7683fdc1
AQ
257}
258
86452f81
SE
259/**
260 * 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
21026059 273/**
92dcdf09
SE
274 * 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);
86452f81
SE
286
287 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
92dcdf09
SE
288}
289
290/**
5dafd8a6
SE
291 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
292 * possibly release it
92dcdf09 293 * @tt_global_entry: tt_global_entry to be free'd
21026059 294 */
a513088d 295static void
5dafd8a6 296batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
7683fdc1 297{
92dcdf09
SE
298 kref_put(&tt_global_entry->common.refcount,
299 batadv_tt_global_entry_release);
db08e6e5
SW
300}
301
1d8ab8d3
LL
302/**
303 * batadv_tt_global_hash_count - count the number of orig entries
d15cd622 304 * @bat_priv: the bat priv with all the soft interface information
1d8ab8d3
LL
305 * @addr: the mac address of the client to count entries for
306 * @vid: VLAN identifier
307 *
62fe710f 308 * Return: the number of originators advertising the given address/data
1d8ab8d3
LL
309 * (excluding ourself).
310 */
311int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
6b5e971a 312 const u8 *addr, unsigned short vid)
1d8ab8d3
LL
313{
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);
5dafd8a6 322 batadv_tt_global_entry_put(tt_global_entry);
1d8ab8d3
LL
323
324 return count;
325}
326
7ea7b4a1
AQ
327/**
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
9c3bf081 345 batadv_softif_vlan_put(vlan);
7ea7b4a1
AQ
346}
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/**
d15cd622
AQ
373 * 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
7ea7b4a1
AQ
376 * @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);
3db15209
SE
390 if (!hlist_unhashed(&vlan->list)) {
391 hlist_del_init_rcu(&vlan->list);
21754e25 392 batadv_orig_node_vlan_put(vlan);
3db15209 393 }
7ea7b4a1 394 spin_unlock_bh(&orig_node->vlan_list_lock);
7ea7b4a1
AQ
395 }
396
21754e25 397 batadv_orig_node_vlan_put(vlan);
7ea7b4a1
AQ
398}
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
86452f81
SE
424/**
425 * 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
42eff6a6
SE
437/**
438 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
439 * queue for free after rcu grace period
6e8ef69d 440 * @ref: kref pointer of the tt orig entry
42eff6a6 441 */
6e8ef69d 442static void batadv_tt_orig_list_entry_release(struct kref *ref)
42eff6a6 443{
6e8ef69d
SE
444 struct batadv_tt_orig_list_entry *orig_entry;
445
446 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
447 refcount);
448
5d967310 449 batadv_orig_node_put(orig_entry->orig_node);
86452f81 450 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
42eff6a6
SE
451}
452
6e8ef69d 453/**
7e2366c6
SE
454 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
455 * possibly release it
6e8ef69d
SE
456 * @orig_entry: tt orig entry to be free'd
457 */
a513088d 458static void
7e2366c6 459batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
db08e6e5 460{
6e8ef69d 461 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
7683fdc1
AQ
462}
463
3abe4adb
AQ
464/**
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 */
56303d34 470static void batadv_tt_local_event(struct batadv_priv *bat_priv,
3abe4adb 471 struct batadv_tt_local_entry *tt_local_entry,
6b5e971a 472 u8 event_flags)
a73105b8 473{
56303d34 474 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
3abe4adb 475 struct batadv_tt_common_entry *common = &tt_local_entry->common;
6b5e971a 476 u8 flags = common->flags | event_flags;
3b643de5
AQ
477 bool event_removed = false;
478 bool del_op_requested, del_op_entry;
a73105b8 479
86452f81 480 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
a73105b8
AQ
481 if (!tt_change_node)
482 return;
483
ff66c975 484 tt_change_node->change.flags = flags;
ca663046
AQ
485 memset(tt_change_node->change.reserved, 0,
486 sizeof(tt_change_node->change.reserved));
8fdd0153 487 ether_addr_copy(tt_change_node->change.addr, common->addr);
c018ad3d 488 tt_change_node->change.vid = htons(common->vid);
a73105b8 489
acd34afa 490 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
491
492 /* check for ADD+DEL or DEL+ADD events */
807736f6
SE
493 spin_lock_bh(&bat_priv->tt.changes_list_lock);
494 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
3b643de5 495 list) {
3abe4adb 496 if (!batadv_compare_eth(entry->change.addr, common->addr))
3b643de5
AQ
497 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 */
acd34afa 506 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
507 if (!del_op_requested && del_op_entry)
508 goto del;
509 if (del_op_requested && !del_op_entry)
510 goto del;
3c4f7ab6
AQ
511
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
3b643de5
AQ
518 continue;
519del:
520 list_del(&entry->list);
86452f81
SE
521 kmem_cache_free(batadv_tt_change_cache, entry);
522 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
3b643de5
AQ
523 event_removed = true;
524 goto unlock;
525 }
526
a73105b8 527 /* track the change in the OGMinterval list */
807736f6 528 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
3b643de5
AQ
529
530unlock:
807736f6 531 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 532
3b643de5 533 if (event_removed)
807736f6 534 atomic_dec(&bat_priv->tt.local_changes);
3b643de5 535 else
807736f6 536 atomic_inc(&bat_priv->tt.local_changes);
a73105b8
AQ
537}
538
335fbe0f
ML
539/**
540 * batadv_tt_len - compute length in bytes of given number of tt changes
541 * @changes_num: number of tt changes
542 *
62fe710f 543 * Return: computed length in bytes.
335fbe0f
ML
544 */
545static int batadv_tt_len(int changes_num)
a73105b8 546{
335fbe0f 547 return changes_num * sizeof(struct batadv_tvlv_tt_change);
a73105b8
AQ
548}
549
298e6e68
AQ
550/**
551 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
552 * @tt_len: available space
553 *
62fe710f 554 * Return: the number of entries.
298e6e68 555 */
6b5e971a 556static u16 batadv_tt_entries(u16 tt_len)
298e6e68
AQ
557{
558 return tt_len / batadv_tt_len(1);
559}
560
a19d3d85
ML
561/**
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 *
62fe710f 566 * Return: local translation table size in bytes.
a19d3d85
ML
567 */
568static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
569{
4f248cff
SE
570 u16 num_vlan = 0;
571 u16 tt_local_entries = 0;
a19d3d85
ML
572 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
56303d34 591static int batadv_tt_local_init(struct batadv_priv *bat_priv)
c6c8fea2 592{
807736f6 593 if (bat_priv->tt.local_hash)
5346c35e 594 return 0;
c6c8fea2 595
807736f6 596 bat_priv->tt.local_hash = batadv_hash_new(1024);
c6c8fea2 597
807736f6 598 if (!bat_priv->tt.local_hash)
5346c35e 599 return -ENOMEM;
c6c8fea2 600
dec05074
AQ
601 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
602 &batadv_tt_local_hash_lock_class_key);
603
5346c35e 604 return 0;
c6c8fea2
SE
605}
606
068ee6e2
AQ
607static 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,
16052789
AQ
612 "Deleting global tt entry %pM (vid: %d): %s\n",
613 tt_global->common.addr,
614 BATADV_PRINT_VID(tt_global->common.vid), message);
068ee6e2
AQ
615
616 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
c018ad3d 617 batadv_choose_tt, &tt_global->common);
5dafd8a6 618 batadv_tt_global_entry_put(tt_global);
068ee6e2
AQ
619}
620
c018ad3d
AQ
621/**
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)
9464d071
AQ
629 * @mark: the value contained in the skb->mark field of the received packet (if
630 * any)
a19d3d85 631 *
62fe710f 632 * Return: true if the client was successfully added, false otherwise.
c018ad3d 633 */
6b5e971a
SE
634bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
635 unsigned short vid, int ifindex, u32 mark)
c6c8fea2 636{
56303d34 637 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
170173bf 638 struct batadv_tt_local_entry *tt_local;
c5caf4ef 639 struct batadv_tt_global_entry *tt_global = NULL;
2cd45a06 640 struct net *net = dev_net(soft_iface);
35df3b29 641 struct batadv_softif_vlan *vlan;
0c69aecc 642 struct net_device *in_dev = NULL;
db08e6e5 643 struct hlist_head *head;
56303d34 644 struct batadv_tt_orig_list_entry *orig_entry;
a19d3d85 645 int hash_added, table_size, packet_size_max;
4f248cff
SE
646 bool ret = false;
647 bool roamed_back = false;
6b5e971a
SE
648 u8 remote_flags;
649 u32 match_mark;
c6c8fea2 650
0c69aecc 651 if (ifindex != BATADV_NULL_IFINDEX)
2cd45a06 652 in_dev = dev_get_by_index(net, ifindex);
0c69aecc 653
c018ad3d 654 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
c5caf4ef
LL
655
656 if (!is_multicast_ether_addr(addr))
657 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
c6c8fea2 658
47c94655
AQ
659 if (tt_local) {
660 tt_local->last_seen = jiffies;
068ee6e2
AQ
661 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
662 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
663 "Re-adding pending client %pM (vid: %d)\n",
664 addr, BATADV_PRINT_VID(vid));
068ee6e2
AQ
665 /* 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,
16052789
AQ
676 "Roaming client %pM (vid: %d) came back to its original location\n",
677 addr, BATADV_PRINT_VID(vid));
068ee6e2
AQ
678 /* 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;
c6c8fea2
SE
687 }
688
a19d3d85
ML
689 /* 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
86452f81 700 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
47c94655 701 if (!tt_local)
7683fdc1 702 goto out;
a73105b8 703
35df3b29
AQ
704 /* increase the refcounter of the related vlan */
705 vlan = batadv_softif_vlan_get(bat_priv, vid);
0b3dd7df
SW
706 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));
86452f81 710 kmem_cache_free(batadv_tl_cache, tt_local);
fd7dec25 711 tt_local = NULL;
354136bc 712 goto out;
fd7dec25 713 }
35df3b29 714
39c75a51 715 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
716 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
717 addr, BATADV_PRINT_VID(vid),
6b5e971a 718 (u8)atomic_read(&bat_priv->tt.vn));
c6c8fea2 719
8fdd0153 720 ether_addr_copy(tt_local->common.addr, addr);
8425ec6a
AQ
721 /* 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;
c018ad3d 726 tt_local->common.vid = vid;
0c69aecc 727 if (batadv_is_wifi_netdev(in_dev))
47c94655 728 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
92dcdf09
SE
729 kref_init(&tt_local->common.refcount);
730 kref_get(&tt_local->common.refcount);
47c94655
AQ
731 tt_local->last_seen = jiffies;
732 tt_local->common.added_at = tt_local->last_seen;
a33d970d 733 tt_local->vlan = vlan;
c6c8fea2 734
c5caf4ef
LL
735 /* 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))
47c94655 740 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 741
807736f6 742 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
c018ad3d 743 batadv_choose_tt, &tt_local->common,
47c94655 744 &tt_local->common.hash_entry);
80b3f58c
SW
745
746 if (unlikely(hash_added != 0)) {
747 /* remove the reference for the hash */
95c0db90 748 batadv_tt_local_entry_put(tt_local);
80b3f58c
SW
749 goto out;
750 }
751
068ee6e2 752add_event:
3abe4adb 753 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
ff66c975 754
068ee6e2
AQ
755check_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)) {
db08e6e5 760 /* These node are probably going to update their tt table */
47c94655 761 head = &tt_global->orig_list;
db08e6e5 762 rcu_read_lock();
b67bfe0d 763 hlist_for_each_entry_rcu(orig_entry, head, list) {
47c94655 764 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
c018ad3d 765 tt_global->common.vid,
a513088d 766 orig_entry->orig_node);
db08e6e5
SW
767 }
768 rcu_read_unlock();
068ee6e2
AQ
769 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 }
7683fdc1 780 }
068ee6e2 781
3c4f7ab6
AQ
782 /* 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;
786
787 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;
a19d3d85 791
9464d071
AQ
792 /* 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
3c4f7ab6
AQ
803 /* 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;
7683fdc1 810out:
0c69aecc
AQ
811 if (in_dev)
812 dev_put(in_dev);
47c94655 813 if (tt_local)
95c0db90 814 batadv_tt_local_entry_put(tt_local);
47c94655 815 if (tt_global)
5dafd8a6 816 batadv_tt_global_entry_put(tt_global);
a19d3d85 817 return ret;
c6c8fea2
SE
818}
819
7ea7b4a1
AQ
820/**
821 * 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
7ea7b4a1
AQ
831 * 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 *
62fe710f 835 * Return: the size of the allocated buffer or 0 in case of failure.
7ea7b4a1 836 */
6b5e971a 837static u16
7ea7b4a1
AQ
838batadv_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,
6b5e971a 841 s32 *tt_len)
7ea7b4a1 842{
4f248cff
SE
843 u16 num_vlan = 0;
844 u16 num_entries = 0;
845 u16 change_offset;
846 u16 tvlv_len;
7ea7b4a1
AQ
847 struct batadv_tvlv_tt_vlan_data *tt_vlan;
848 struct batadv_orig_node_vlan *vlan;
6b5e971a 849 u8 *tt_change_ptr;
7ea7b4a1
AQ
850
851 rcu_read_lock();
d0fa4f3f 852 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
853 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);
d0fa4f3f 878 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
879 tt_vlan->vid = htons(vlan->vid);
880 tt_vlan->crc = htonl(vlan->tt.crc);
881
882 tt_vlan++;
883 }
884
6b5e971a 885 tt_change_ptr = (u8 *)*tt_data + change_offset;
7ea7b4a1
AQ
886 *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 *
62fe710f 909 * Return: the size of the allocated buffer or 0 in case of failure.
7ea7b4a1 910 */
6b5e971a 911static u16
7ea7b4a1
AQ
912batadv_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,
6b5e971a 915 s32 *tt_len)
7ea7b4a1
AQ
916{
917 struct batadv_tvlv_tt_vlan_data *tt_vlan;
918 struct batadv_softif_vlan *vlan;
4f248cff
SE
919 u16 num_vlan = 0;
920 u16 num_entries = 0;
921 u16 tvlv_len;
6b5e971a 922 u8 *tt_change_ptr;
7ea7b4a1
AQ
923 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
6b5e971a 959 tt_change_ptr = (u8 *)*tt_data + change_offset;
7ea7b4a1
AQ
960 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
961
962out:
963 rcu_read_unlock();
964 return tvlv_len;
965}
966
e1bf0c14
ML
967/**
968 * 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)
be9aa4c1 973{
e1bf0c14
ML
974 struct batadv_tt_change_node *entry, *safe;
975 struct batadv_tvlv_tt_data *tt_data;
976 struct batadv_tvlv_tt_change *tt_change;
7ea7b4a1 977 int tt_diff_len, tt_change_len = 0;
4f248cff
SE
978 int tt_diff_entries_num = 0;
979 int tt_diff_entries_count = 0;
6b5e971a 980 u16 tvlv_len;
be9aa4c1 981
7ea7b4a1
AQ
982 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
983 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
be9aa4c1
ML
984
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 */
e1bf0c14
ML
988 if (tt_diff_len > bat_priv->soft_iface->mtu)
989 tt_diff_len = 0;
be9aa4c1 990
7ea7b4a1
AQ
991 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
992 &tt_change, &tt_diff_len);
993 if (!tvlv_len)
e1bf0c14 994 return;
be9aa4c1 995
e1bf0c14 996 tt_data->flags = BATADV_TT_OGM_DIFF;
c6c8fea2 997
e1bf0c14
ML
998 if (tt_diff_len == 0)
999 goto container_register;
be9aa4c1 1000
807736f6
SE
1001 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1002 atomic_set(&bat_priv->tt.local_changes, 0);
c6c8fea2 1003
807736f6 1004 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
7c64fd98 1005 list) {
e1bf0c14
ML
1006 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++;
c6c8fea2 1011 }
a73105b8 1012 list_del(&entry->list);
86452f81 1013 kmem_cache_free(batadv_tt_change_cache, entry);
c6c8fea2 1014 }
807736f6 1015 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8
AQ
1016
1017 /* Keep the buffer for possible tt_request */
807736f6
SE
1018 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;
e1bf0c14 1022 tt_change_len = batadv_tt_len(tt_diff_entries_count);
be9aa4c1 1023 /* check whether this new OGM has no changes due to size problems */
e1bf0c14 1024 if (tt_diff_entries_count > 0) {
be9aa4c1 1025 /* if kmalloc() fails we will reply with the full table
a73105b8
AQ
1026 * instead of providing the diff
1027 */
e1bf0c14 1028 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
807736f6 1029 if (bat_priv->tt.last_changeset) {
e1bf0c14
ML
1030 memcpy(bat_priv->tt.last_changeset,
1031 tt_change, tt_change_len);
1032 bat_priv->tt.last_changeset_len = tt_diff_len;
a73105b8
AQ
1033 }
1034 }
807736f6 1035 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
c6c8fea2 1036
e1bf0c14
ML
1037container_register:
1038 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
7ea7b4a1 1039 tvlv_len);
e1bf0c14 1040 kfree(tt_data);
c6c8fea2
SE
1041}
1042
08c36d3e 1043int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1044{
1045 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1046 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1047 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 1048 struct batadv_tt_common_entry *tt_common_entry;
85766a82 1049 struct batadv_tt_local_entry *tt_local;
56303d34 1050 struct batadv_hard_iface *primary_if;
c6c8fea2 1051 struct hlist_head *head;
6b5e971a 1052 u32 i;
85766a82
AQ
1053 int last_seen_secs;
1054 int last_seen_msecs;
1055 unsigned long last_seen_jiffies;
1056 bool no_purge;
6b5e971a 1057 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 1058
30da63a6
ML
1059 primary_if = batadv_seq_print_text_primary_if_get(seq);
1060 if (!primary_if)
32ae9b22 1061 goto out;
c6c8fea2 1062
86ceb360 1063 seq_printf(seq,
7ea7b4a1 1064 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
6b5e971a 1065 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
925a6f37
AQ
1066 seq_puts(seq,
1067 " Client VID Flags Last seen (CRC )\n");
c6c8fea2 1068
c6c8fea2
SE
1069 for (i = 0; i < hash->size; i++) {
1070 head = &hash->table[i];
1071
7aadf889 1072 rcu_read_lock();
b67bfe0d 1073 hlist_for_each_entry_rcu(tt_common_entry,
7aadf889 1074 head, hash_entry) {
85766a82
AQ
1075 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;
7ea7b4a1 1084 seq_printf(seq,
dd24ddb2 1085 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
7c64fd98 1086 tt_common_entry->addr,
16052789 1087 BATADV_PRINT_VID(tt_common_entry->vid),
a2f2b6cd
SE
1088 ((tt_common_entry->flags &
1089 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
85766a82 1090 no_purge ? 'P' : '.',
a2f2b6cd
SE
1091 ((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' : '.'),
a7966d90 1099 no_purge ? 0 : last_seen_secs,
7ea7b4a1 1100 no_purge ? 0 : last_seen_msecs,
a33d970d 1101 tt_local->vlan->tt.crc);
c6c8fea2 1102 }
7aadf889 1103 rcu_read_unlock();
c6c8fea2 1104 }
32ae9b22
ML
1105out:
1106 if (primary_if)
82047ad7 1107 batadv_hardif_put(primary_if);
30da63a6 1108 return 0;
c6c8fea2
SE
1109}
1110
56303d34
SE
1111static void
1112batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1113 struct batadv_tt_local_entry *tt_local_entry,
6b5e971a 1114 u16 flags, const char *message)
c6c8fea2 1115{
3abe4adb 1116 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
a73105b8 1117
015758d0
AQ
1118 /* 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
9cfc7bd6
SE
1120 * response issued before the net ttvn increment (consistency check)
1121 */
acd34afa 1122 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
c566dbbe 1123
39c75a51 1124 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1125 "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);
c6c8fea2
SE
1128}
1129
7f91d06c
AQ
1130/**
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
c018ad3d 1134 * @vid: VLAN identifier
7f91d06c
AQ
1135 * @message: message to append to the log on deletion
1136 * @roaming: true if the deletion is due to a roaming event
1137 *
62fe710f 1138 * Return: the flags assigned to the local entry before being deleted
7f91d06c 1139 */
6b5e971a
SE
1140u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1141 unsigned short vid, const char *message,
1142 bool roaming)
c6c8fea2 1143{
170173bf 1144 struct batadv_tt_local_entry *tt_local_entry;
6b5e971a 1145 u16 flags, curr_flags = BATADV_NO_FLAGS;
ef72706a 1146 void *tt_entry_exists;
c6c8fea2 1147
c018ad3d 1148 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7683fdc1
AQ
1149 if (!tt_local_entry)
1150 goto out;
1151
7f91d06c
AQ
1152 curr_flags = tt_local_entry->common.flags;
1153
acd34afa 1154 flags = BATADV_TT_CLIENT_DEL;
068ee6e2
AQ
1155 /* 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 */
7c1fd91d 1159 if (roaming) {
acd34afa 1160 flags |= BATADV_TT_CLIENT_ROAM;
7c1fd91d
AQ
1161 /* mark the local client as ROAMed */
1162 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1163 }
42d0b044 1164
068ee6e2
AQ
1165 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 */
3abe4adb 1173 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
ef72706a
ML
1174
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 */
95c0db90 1183 batadv_tt_local_entry_put(tt_local_entry);
7f91d06c 1184
7683fdc1
AQ
1185out:
1186 if (tt_local_entry)
95c0db90 1187 batadv_tt_local_entry_put(tt_local_entry);
7f91d06c
AQ
1188
1189 return curr_flags;
c6c8fea2
SE
1190}
1191
a19d3d85
ML
1192/**
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 */
56303d34 1199static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
a19d3d85
ML
1200 struct hlist_head *head,
1201 int timeout)
c6c8fea2 1202{
56303d34
SE
1203 struct batadv_tt_local_entry *tt_local_entry;
1204 struct batadv_tt_common_entry *tt_common_entry;
b67bfe0d 1205 struct hlist_node *node_tmp;
acd34afa 1206
b67bfe0d 1207 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
acd34afa
SE
1208 hash_entry) {
1209 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
1210 struct batadv_tt_local_entry,
1211 common);
acd34afa
SE
1212 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
a19d3d85 1219 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
acd34afa
SE
1220 continue;
1221
1222 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1223 BATADV_TT_CLIENT_DEL, "timed out");
1224 }
1225}
1226
a19d3d85
ML
1227/**
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)
acd34afa 1235{
807736f6 1236 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
c6c8fea2 1237 struct hlist_head *head;
7683fdc1 1238 spinlock_t *list_lock; /* protects write access to the hash lists */
6b5e971a 1239 u32 i;
c6c8fea2 1240
c6c8fea2
SE
1241 for (i = 0; i < hash->size; i++) {
1242 head = &hash->table[i];
7683fdc1 1243 list_lock = &hash->list_locks[i];
c6c8fea2 1244
7683fdc1 1245 spin_lock_bh(list_lock);
a19d3d85 1246 batadv_tt_local_purge_list(bat_priv, head, timeout);
7683fdc1 1247 spin_unlock_bh(list_lock);
c6c8fea2 1248 }
c6c8fea2
SE
1249}
1250
56303d34 1251static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
c6c8fea2 1252{
5bf74e9c 1253 struct batadv_hashtable *hash;
a73105b8 1254 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
1255 struct batadv_tt_common_entry *tt_common_entry;
1256 struct batadv_tt_local_entry *tt_local;
b67bfe0d 1257 struct hlist_node *node_tmp;
7683fdc1 1258 struct hlist_head *head;
6b5e971a 1259 u32 i;
a73105b8 1260
807736f6 1261 if (!bat_priv->tt.local_hash)
c6c8fea2
SE
1262 return;
1263
807736f6 1264 hash = bat_priv->tt.local_hash;
a73105b8
AQ
1265
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);
b67bfe0d 1271 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
a73105b8 1272 head, hash_entry) {
b67bfe0d 1273 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34
SE
1274 tt_local = container_of(tt_common_entry,
1275 struct batadv_tt_local_entry,
1276 common);
35df3b29 1277
95c0db90 1278 batadv_tt_local_entry_put(tt_local);
a73105b8
AQ
1279 }
1280 spin_unlock_bh(list_lock);
1281 }
1282
1a8eaf07 1283 batadv_hash_destroy(hash);
a73105b8 1284
807736f6 1285 bat_priv->tt.local_hash = NULL;
c6c8fea2
SE
1286}
1287
56303d34 1288static int batadv_tt_global_init(struct batadv_priv *bat_priv)
c6c8fea2 1289{
807736f6 1290 if (bat_priv->tt.global_hash)
5346c35e 1291 return 0;
c6c8fea2 1292
807736f6 1293 bat_priv->tt.global_hash = batadv_hash_new(1024);
c6c8fea2 1294
807736f6 1295 if (!bat_priv->tt.global_hash)
5346c35e 1296 return -ENOMEM;
c6c8fea2 1297
dec05074
AQ
1298 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1299 &batadv_tt_global_hash_lock_class_key);
1300
5346c35e 1301 return 0;
c6c8fea2
SE
1302}
1303
56303d34 1304static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
c6c8fea2 1305{
56303d34 1306 struct batadv_tt_change_node *entry, *safe;
c6c8fea2 1307
807736f6 1308 spin_lock_bh(&bat_priv->tt.changes_list_lock);
c6c8fea2 1309
807736f6 1310 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
a73105b8
AQ
1311 list) {
1312 list_del(&entry->list);
86452f81 1313 kmem_cache_free(batadv_tt_change_cache, entry);
a73105b8 1314 }
c6c8fea2 1315
807736f6
SE
1316 atomic_set(&bat_priv->tt.local_changes, 0);
1317 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 1318}
c6c8fea2 1319
62fe710f 1320/**
d15cd622
AQ
1321 * 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
62fe710f 1325 *
d15cd622 1326 * retrieve the orig_tt_list_entry belonging to orig_node from the
d657e621
AQ
1327 * batadv_tt_global_entry list
1328 *
62fe710f 1329 * Return: it with an increased refcounter, NULL if not found
db08e6e5 1330 */
d657e621
AQ
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)
db08e6e5 1334{
d657e621 1335 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
db08e6e5 1336 const struct hlist_head *head;
db08e6e5
SW
1337
1338 rcu_read_lock();
1339 head = &entry->orig_list;
b67bfe0d 1340 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
d657e621
AQ
1341 if (tmp_orig_entry->orig_node != orig_node)
1342 continue;
6e8ef69d 1343 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
d657e621
AQ
1344 continue;
1345
1346 orig_entry = tmp_orig_entry;
1347 break;
db08e6e5
SW
1348 }
1349 rcu_read_unlock();
d657e621
AQ
1350
1351 return orig_entry;
1352}
1353
62fe710f 1354/**
d15cd622
AQ
1355 * 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
62fe710f
SE
1359 *
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
d657e621
AQ
1363 */
1364static bool
1365batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1366 const struct batadv_orig_node *orig_node)
1367{
1368 struct batadv_tt_orig_list_entry *orig_entry;
1369 bool found = false;
1370
1371 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1372 if (orig_entry) {
1373 found = true;
7e2366c6 1374 batadv_tt_orig_list_entry_put(orig_entry);
d657e621
AQ
1375 }
1376
db08e6e5
SW
1377 return found;
1378}
1379
a513088d 1380static void
d657e621 1381batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
56303d34 1382 struct batadv_orig_node *orig_node, int ttvn)
db08e6e5 1383{
56303d34 1384 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5 1385
d657e621 1386 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
30cfd02b
AQ
1387 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;
d657e621 1392 goto out;
30cfd02b 1393 }
d657e621 1394
86452f81 1395 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
db08e6e5 1396 if (!orig_entry)
d657e621 1397 goto out;
db08e6e5
SW
1398
1399 INIT_HLIST_NODE(&orig_entry->list);
7c124391 1400 kref_get(&orig_node->refcount);
7ea7b4a1 1401 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
db08e6e5
SW
1402 orig_entry->orig_node = orig_node;
1403 orig_entry->ttvn = ttvn;
6e8ef69d
SE
1404 kref_init(&orig_entry->refcount);
1405 kref_get(&orig_entry->refcount);
db08e6e5 1406
d657e621 1407 spin_lock_bh(&tt_global->list_lock);
db08e6e5 1408 hlist_add_head_rcu(&orig_entry->list,
d657e621
AQ
1409 &tt_global->orig_list);
1410 spin_unlock_bh(&tt_global->list_lock);
1d8ab8d3
LL
1411 atomic_inc(&tt_global->orig_list_count);
1412
d657e621
AQ
1413out:
1414 if (orig_entry)
7e2366c6 1415 batadv_tt_orig_list_entry_put(orig_entry);
db08e6e5
SW
1416}
1417
d4ff40f6
AQ
1418/**
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
c018ad3d 1423 * @vid: VLAN identifier
d4ff40f6
AQ
1424 * @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.
1e5d49fc 1434 *
62fe710f 1435 * Return: true if the new entry has been added, false otherwise
d4ff40f6 1436 */
1e5d49fc
AQ
1437static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1438 struct batadv_orig_node *orig_node,
c018ad3d 1439 const unsigned char *tt_addr,
6b5e971a 1440 unsigned short vid, u16 flags, u8 ttvn)
a73105b8 1441{
170173bf
SE
1442 struct batadv_tt_global_entry *tt_global_entry;
1443 struct batadv_tt_local_entry *tt_local_entry;
1e5d49fc 1444 bool ret = false;
80b3f58c 1445 int hash_added;
56303d34 1446 struct batadv_tt_common_entry *common;
6b5e971a 1447 u16 local_flags;
c6c8fea2 1448
cfd4f757
AQ
1449 /* ignore global entries from backbone nodes */
1450 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1451 return true;
1452
c018ad3d
AQ
1453 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);
068ee6e2
AQ
1455
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;
a73105b8
AQ
1463
1464 if (!tt_global_entry) {
86452f81
SE
1465 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1466 GFP_ATOMIC);
a73105b8 1467 if (!tt_global_entry)
7683fdc1
AQ
1468 goto out;
1469
c0a55929 1470 common = &tt_global_entry->common;
8fdd0153 1471 ether_addr_copy(common->addr, tt_addr);
c018ad3d 1472 common->vid = vid;
db08e6e5 1473
d4f44692 1474 common->flags = flags;
cc47f66e 1475 tt_global_entry->roam_at = 0;
fdf79320
AQ
1476 /* 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;
92dcdf09
SE
1482 kref_init(&common->refcount);
1483 kref_get(&common->refcount);
30cfd02b 1484 common->added_at = jiffies;
db08e6e5
SW
1485
1486 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1d8ab8d3 1487 atomic_set(&tt_global_entry->orig_list_count, 0);
db08e6e5 1488 spin_lock_init(&tt_global_entry->list_lock);
7683fdc1 1489
807736f6 1490 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
a513088d 1491 batadv_compare_tt,
c018ad3d 1492 batadv_choose_tt, common,
a513088d 1493 &common->hash_entry);
80b3f58c
SW
1494
1495 if (unlikely(hash_added != 0)) {
1496 /* remove the reference for the hash */
5dafd8a6 1497 batadv_tt_global_entry_put(tt_global_entry);
80b3f58c
SW
1498 goto out_remove;
1499 }
a73105b8 1500 } else {
068ee6e2 1501 common = &tt_global_entry->common;
30cfd02b
AQ
1502 /* If there is already a global entry, we can use this one for
1503 * our processing.
068ee6e2
AQ
1504 * 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
30cfd02b 1511 */
068ee6e2
AQ
1512 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 }
30cfd02b
AQ
1521
1522 /* if the client was temporary added before receiving the first
a6cb3909
SW
1523 * 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.
30cfd02b 1527 */
a6cb3909
SW
1528 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 }
db08e6e5 1532
e9c00136
AQ
1533 /* 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 */
ad7e2c46 1537 common->flags |= flags;
e9c00136 1538
acd34afa
SE
1539 /* 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
db08e6e5
SW
1541 * delete + roaming change for this originator.
1542 *
1543 * We should first delete the old originator before adding the
1544 * new one.
1545 */
068ee6e2 1546 if (common->flags & BATADV_TT_CLIENT_ROAM) {
a513088d 1547 batadv_tt_global_del_orig_list(tt_global_entry);
068ee6e2 1548 common->flags &= ~BATADV_TT_CLIENT_ROAM;
db08e6e5 1549 tt_global_entry->roam_at = 0;
a73105b8
AQ
1550 }
1551 }
068ee6e2 1552add_orig_entry:
30cfd02b 1553 /* add the new orig_entry (if needed) or update it */
d657e621 1554 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
c6c8fea2 1555
39c75a51 1556 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1557 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1558 common->addr, BATADV_PRINT_VID(common->vid),
1559 orig_node->orig);
1e5d49fc 1560 ret = true;
c6c8fea2 1561
80b3f58c 1562out_remove:
c5caf4ef
LL
1563 /* 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;
7f91d06c 1568
a73105b8 1569 /* remove address from local hash if present */
c018ad3d 1570 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
7f91d06c 1571 "global tt received",
c1d07431 1572 flags & BATADV_TT_CLIENT_ROAM);
7f91d06c
AQ
1573 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1574
068ee6e2
AQ
1575 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
7683fdc1
AQ
1581out:
1582 if (tt_global_entry)
5dafd8a6 1583 batadv_tt_global_entry_put(tt_global_entry);
068ee6e2 1584 if (tt_local_entry)
95c0db90 1585 batadv_tt_local_entry_put(tt_local_entry);
7683fdc1 1586 return ret;
c6c8fea2
SE
1587}
1588
1b371d13
SW
1589/**
1590 * batadv_transtable_best_orig - Get best originator list entry from tt entry
4627456a 1591 * @bat_priv: the bat priv with all the soft interface information
981d8900
SE
1592 * @tt_global_entry: global translation table entry to be analyzed
1593 *
1594 * This functon assumes the caller holds rcu_read_lock().
62fe710f 1595 * Return: best originator list entry or NULL on errors.
981d8900
SE
1596 */
1597static struct batadv_tt_orig_list_entry *
4627456a
AQ
1598batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1599 struct batadv_tt_global_entry *tt_global_entry)
981d8900 1600{
4627456a 1601 struct batadv_neigh_node *router, *best_router = NULL;
29824a55 1602 struct batadv_algo_ops *bao = bat_priv->algo_ops;
981d8900 1603 struct hlist_head *head;
981d8900 1604 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
981d8900
SE
1605
1606 head = &tt_global_entry->orig_list;
b67bfe0d 1607 hlist_for_each_entry_rcu(orig_entry, head, list) {
7351a482
SW
1608 router = batadv_orig_router_get(orig_entry->orig_node,
1609 BATADV_IF_DEFAULT);
981d8900
SE
1610 if (!router)
1611 continue;
1612
4627456a 1613 if (best_router &&
29824a55
AQ
1614 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1615 BATADV_IF_DEFAULT) <= 0) {
25bb2509 1616 batadv_neigh_node_put(router);
4627456a 1617 continue;
981d8900
SE
1618 }
1619
4627456a
AQ
1620 /* release the refcount for the "old" best */
1621 if (best_router)
25bb2509 1622 batadv_neigh_node_put(best_router);
4627456a
AQ
1623
1624 best_entry = orig_entry;
1625 best_router = router;
981d8900
SE
1626 }
1627
4627456a 1628 if (best_router)
25bb2509 1629 batadv_neigh_node_put(best_router);
4627456a 1630
981d8900
SE
1631 return best_entry;
1632}
1633
1b371d13
SW
1634/**
1635 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1636 * for this global entry
4627456a 1637 * @bat_priv: the bat priv with all the soft interface information
981d8900
SE
1638 * @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().
db08e6e5 1642 */
a513088d 1643static void
4627456a
AQ
1644batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1645 struct batadv_tt_global_entry *tt_global_entry,
a513088d 1646 struct seq_file *seq)
db08e6e5 1647{
981d8900 1648 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
56303d34 1649 struct batadv_tt_common_entry *tt_common_entry;
7ea7b4a1
AQ
1650 struct batadv_orig_node_vlan *vlan;
1651 struct hlist_head *head;
6b5e971a
SE
1652 u8 last_ttvn;
1653 u16 flags;
db08e6e5
SW
1654
1655 tt_common_entry = &tt_global_entry->common;
981d8900
SE
1656 flags = tt_common_entry->flags;
1657
4627456a 1658 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
981d8900 1659 if (best_entry) {
7ea7b4a1
AQ
1660 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
981d8900 1670 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
f9d8a537 1671 seq_printf(seq,
dd24ddb2 1672 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
981d8900 1673 '*', tt_global_entry->common.addr,
16052789 1674 BATADV_PRINT_VID(tt_global_entry->common.vid),
981d8900 1675 best_entry->ttvn, best_entry->orig_node->orig,
7ea7b4a1 1676 last_ttvn, vlan->tt.crc,
a2f2b6cd
SE
1677 ((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' : '.'));
7ea7b4a1 1681
21754e25 1682 batadv_orig_node_vlan_put(vlan);
981d8900 1683 }
db08e6e5 1684
7ea7b4a1 1685print_list:
db08e6e5
SW
1686 head = &tt_global_entry->orig_list;
1687
b67bfe0d 1688 hlist_for_each_entry_rcu(orig_entry, head, list) {
981d8900
SE
1689 if (best_entry == orig_entry)
1690 continue;
1691
7ea7b4a1
AQ
1692 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
db08e6e5 1702 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
16052789 1703 seq_printf(seq,
dd24ddb2 1704 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
981d8900 1705 '+', tt_global_entry->common.addr,
16052789 1706 BATADV_PRINT_VID(tt_global_entry->common.vid),
981d8900 1707 orig_entry->ttvn, orig_entry->orig_node->orig,
7ea7b4a1 1708 last_ttvn, vlan->tt.crc,
a2f2b6cd
SE
1709 ((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' : '.'));
7ea7b4a1 1713
21754e25 1714 batadv_orig_node_vlan_put(vlan);
db08e6e5
SW
1715 }
1716}
1717
08c36d3e 1718int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1719{
1720 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1721 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1722 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
1723 struct batadv_tt_common_entry *tt_common_entry;
1724 struct batadv_tt_global_entry *tt_global;
1725 struct batadv_hard_iface *primary_if;
c6c8fea2 1726 struct hlist_head *head;
6b5e971a 1727 u32 i;
c6c8fea2 1728
30da63a6
ML
1729 primary_if = batadv_seq_print_text_primary_if_get(seq);
1730 if (!primary_if)
32ae9b22 1731 goto out;
c6c8fea2 1732
2dafb49d
AQ
1733 seq_printf(seq,
1734 "Globally announced TT entries received via the mesh %s\n",
c6c8fea2 1735 net_dev->name);
925a6f37
AQ
1736 seq_puts(seq,
1737 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
c6c8fea2 1738
c6c8fea2
SE
1739 for (i = 0; i < hash->size; i++) {
1740 head = &hash->table[i];
1741
7aadf889 1742 rcu_read_lock();
b67bfe0d 1743 hlist_for_each_entry_rcu(tt_common_entry,
7aadf889 1744 head, hash_entry) {
56303d34
SE
1745 tt_global = container_of(tt_common_entry,
1746 struct batadv_tt_global_entry,
1747 common);
4627456a 1748 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
c6c8fea2 1749 }
7aadf889 1750 rcu_read_unlock();
c6c8fea2 1751 }
32ae9b22
ML
1752out:
1753 if (primary_if)
82047ad7 1754 batadv_hardif_put(primary_if);
30da63a6 1755 return 0;
c6c8fea2
SE
1756}
1757
1d8ab8d3 1758/**
433ff98f 1759 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1d8ab8d3
LL
1760 * @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.
433ff98f
ML
1765 *
1766 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1767 * part of a list.
1d8ab8d3
LL
1768 */
1769static void
433ff98f
ML
1770_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1771 struct batadv_tt_orig_list_entry *orig_entry)
1d8ab8d3 1772{
2c72d655
SE
1773 lockdep_assert_held(&tt_global_entry->list_lock);
1774
1d8ab8d3
LL
1775 batadv_tt_global_size_dec(orig_entry->orig_node,
1776 tt_global_entry->common.vid);
1777 atomic_dec(&tt_global_entry->orig_list_count);
433ff98f
ML
1778 /* requires holding tt_global_entry->list_lock and orig_entry->list
1779 * being part of a list
1780 */
1d8ab8d3 1781 hlist_del_rcu(&orig_entry->list);
7e2366c6 1782 batadv_tt_orig_list_entry_put(orig_entry);
1d8ab8d3
LL
1783}
1784
db08e6e5 1785/* deletes the orig list of a tt_global_entry */
a513088d 1786static void
56303d34 1787batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
c6c8fea2 1788{
db08e6e5 1789 struct hlist_head *head;
b67bfe0d 1790 struct hlist_node *safe;
56303d34 1791 struct batadv_tt_orig_list_entry *orig_entry;
a73105b8 1792
db08e6e5
SW
1793 spin_lock_bh(&tt_global_entry->list_lock);
1794 head = &tt_global_entry->orig_list;
1d8ab8d3 1795 hlist_for_each_entry_safe(orig_entry, safe, head, list)
433ff98f 1796 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
db08e6e5 1797 spin_unlock_bh(&tt_global_entry->list_lock);
db08e6e5
SW
1798}
1799
1d8ab8d3
LL
1800/**
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 */
a513088d 1810static void
1d8ab8d3
LL
1811batadv_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)
db08e6e5
SW
1815{
1816 struct hlist_head *head;
b67bfe0d 1817 struct hlist_node *safe;
56303d34 1818 struct batadv_tt_orig_list_entry *orig_entry;
16052789 1819 unsigned short vid;
db08e6e5
SW
1820
1821 spin_lock_bh(&tt_global_entry->list_lock);
1822 head = &tt_global_entry->orig_list;
b67bfe0d 1823 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
db08e6e5 1824 if (orig_entry->orig_node == orig_node) {
16052789 1825 vid = tt_global_entry->common.vid;
39c75a51 1826 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 1827 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1eda58bf 1828 orig_node->orig,
16052789
AQ
1829 tt_global_entry->common.addr,
1830 BATADV_PRINT_VID(vid), message);
433ff98f
ML
1831 _batadv_tt_global_del_orig_entry(tt_global_entry,
1832 orig_entry);
db08e6e5
SW
1833 }
1834 }
1835 spin_unlock_bh(&tt_global_entry->list_lock);
1836}
1837
db08e6e5 1838/* If the client is to be deleted, we check if it is the last origantor entry
acd34afa
SE
1839 * 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.
db08e6e5 1841 */
a513088d 1842static void
56303d34
SE
1843batadv_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)
db08e6e5
SW
1847{
1848 bool last_entry = true;
1849 struct hlist_head *head;
56303d34 1850 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5
SW
1851
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;
b67bfe0d 1858 hlist_for_each_entry_rcu(orig_entry, head, list) {
db08e6e5
SW
1859 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. */
acd34afa 1868 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
db08e6e5
SW
1869 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 */
1d8ab8d3
LL
1874 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1875 orig_node, message);
db08e6e5
SW
1876}
1877
c018ad3d
AQ
1878/**
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 */
56303d34
SE
1888static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1889 struct batadv_orig_node *orig_node,
c018ad3d 1890 const unsigned char *addr, unsigned short vid,
a513088d 1891 const char *message, bool roaming)
a73105b8 1892{
170173bf 1893 struct batadv_tt_global_entry *tt_global_entry;
56303d34 1894 struct batadv_tt_local_entry *local_entry = NULL;
a73105b8 1895
c018ad3d 1896 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
db08e6e5 1897 if (!tt_global_entry)
7683fdc1 1898 goto out;
a73105b8 1899
db08e6e5 1900 if (!roaming) {
1d8ab8d3
LL
1901 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1902 orig_node, message);
db08e6e5
SW
1903
1904 if (hlist_empty(&tt_global_entry->orig_list))
be73b488
AQ
1905 batadv_tt_global_free(bat_priv, tt_global_entry,
1906 message);
db08e6e5
SW
1907
1908 goto out;
1909 }
92f90f56
SE
1910
1911 /* if we are deleting a global entry due to a roam
1912 * event, there are two possibilities:
db08e6e5
SW
1913 * 1) the client roamed from node A to node B => if there
1914 * is only one originator left for this client, we mark
acd34afa 1915 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
92f90f56
SE
1916 * wait for node B to claim it. In case of timeout
1917 * the entry is purged.
db08e6e5
SW
1918 *
1919 * If there are other originators left, we directly delete
1920 * the originator.
92f90f56 1921 * 2) the client roamed to us => we can directly delete
9cfc7bd6
SE
1922 * the global entry, since it is useless now.
1923 */
a513088d 1924 local_entry = batadv_tt_local_hash_find(bat_priv,
c018ad3d
AQ
1925 tt_global_entry->common.addr,
1926 vid);
a513088d 1927 if (local_entry) {
db08e6e5 1928 /* local entry exists, case 2: client roamed to us. */
a513088d 1929 batadv_tt_global_del_orig_list(tt_global_entry);
be73b488 1930 batadv_tt_global_free(bat_priv, tt_global_entry, message);
db08e6e5
SW
1931 } else
1932 /* no local entry exists, case 1: check for roaming */
a513088d
SE
1933 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1934 orig_node, message);
92f90f56 1935
cc47f66e 1936out:
7683fdc1 1937 if (tt_global_entry)
5dafd8a6 1938 batadv_tt_global_entry_put(tt_global_entry);
a513088d 1939 if (local_entry)
95c0db90 1940 batadv_tt_local_entry_put(local_entry);
a73105b8
AQ
1941}
1942
95fb130d
AQ
1943/**
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 */
56303d34
SE
1952void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1953 struct batadv_orig_node *orig_node,
6b5e971a 1954 s32 match_vid,
56303d34 1955 const char *message)
c6c8fea2 1956{
56303d34
SE
1957 struct batadv_tt_global_entry *tt_global;
1958 struct batadv_tt_common_entry *tt_common_entry;
6b5e971a 1959 u32 i;
807736f6 1960 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
b67bfe0d 1961 struct hlist_node *safe;
a73105b8 1962 struct hlist_head *head;
7683fdc1 1963 spinlock_t *list_lock; /* protects write access to the hash lists */
16052789 1964 unsigned short vid;
c6c8fea2 1965
6e801494
SW
1966 if (!hash)
1967 return;
1968
a73105b8
AQ
1969 for (i = 0; i < hash->size; i++) {
1970 head = &hash->table[i];
7683fdc1 1971 list_lock = &hash->list_locks[i];
c6c8fea2 1972
7683fdc1 1973 spin_lock_bh(list_lock);
b67bfe0d 1974 hlist_for_each_entry_safe(tt_common_entry, safe,
7c64fd98 1975 head, hash_entry) {
95fb130d
AQ
1976 /* remove only matching entries */
1977 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1978 continue;
1979
56303d34
SE
1980 tt_global = container_of(tt_common_entry,
1981 struct batadv_tt_global_entry,
1982 common);
db08e6e5 1983
1d8ab8d3
LL
1984 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1985 orig_node, message);
db08e6e5 1986
56303d34 1987 if (hlist_empty(&tt_global->orig_list)) {
16052789 1988 vid = tt_global->common.vid;
39c75a51 1989 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1990 "Deleting global tt entry %pM (vid: %d): %s\n",
1991 tt_global->common.addr,
1992 BATADV_PRINT_VID(vid), message);
b67bfe0d 1993 hlist_del_rcu(&tt_common_entry->hash_entry);
5dafd8a6 1994 batadv_tt_global_entry_put(tt_global);
7683fdc1 1995 }
a73105b8 1996 }
7683fdc1 1997 spin_unlock_bh(list_lock);
c6c8fea2 1998 }
ac4eebd4 1999 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
c6c8fea2
SE
2000}
2001
30cfd02b
AQ
2002static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2003 char **msg)
cc47f66e 2004{
30cfd02b
AQ
2005 bool purge = false;
2006 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2007 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
42d0b044 2008
30cfd02b
AQ
2009 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";
2013 }
42d0b044 2014
30cfd02b
AQ
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";
42d0b044 2019 }
30cfd02b
AQ
2020
2021 return purge;
42d0b044
SE
2022}
2023
30cfd02b 2024static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
42d0b044 2025{
807736f6 2026 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
cc47f66e 2027 struct hlist_head *head;
b67bfe0d 2028 struct hlist_node *node_tmp;
7683fdc1 2029 spinlock_t *list_lock; /* protects write access to the hash lists */
6b5e971a 2030 u32 i;
30cfd02b
AQ
2031 char *msg = NULL;
2032 struct batadv_tt_common_entry *tt_common;
2033 struct batadv_tt_global_entry *tt_global;
cc47f66e 2034
cc47f66e
AQ
2035 for (i = 0; i < hash->size; i++) {
2036 head = &hash->table[i];
7683fdc1 2037 list_lock = &hash->list_locks[i];
cc47f66e 2038
7683fdc1 2039 spin_lock_bh(list_lock);
b67bfe0d 2040 hlist_for_each_entry_safe(tt_common, node_tmp, head,
30cfd02b
AQ
2041 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,
16052789
AQ
2050 "Deleting global tt entry %pM (vid: %d): %s\n",
2051 tt_global->common.addr,
2052 BATADV_PRINT_VID(tt_global->common.vid),
2053 msg);
30cfd02b 2054
b67bfe0d 2055 hlist_del_rcu(&tt_common->hash_entry);
30cfd02b 2056
5dafd8a6 2057 batadv_tt_global_entry_put(tt_global);
30cfd02b 2058 }
7683fdc1 2059 spin_unlock_bh(list_lock);
cc47f66e 2060 }
cc47f66e
AQ
2061}
2062
56303d34 2063static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
c6c8fea2 2064{
5bf74e9c 2065 struct batadv_hashtable *hash;
7683fdc1 2066 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
2067 struct batadv_tt_common_entry *tt_common_entry;
2068 struct batadv_tt_global_entry *tt_global;
b67bfe0d 2069 struct hlist_node *node_tmp;
7683fdc1 2070 struct hlist_head *head;
6b5e971a 2071 u32 i;
7683fdc1 2072
807736f6 2073 if (!bat_priv->tt.global_hash)
c6c8fea2
SE
2074 return;
2075
807736f6 2076 hash = bat_priv->tt.global_hash;
7683fdc1
AQ
2077
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);
b67bfe0d 2083 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
7683fdc1 2084 head, hash_entry) {
b67bfe0d 2085 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34
SE
2086 tt_global = container_of(tt_common_entry,
2087 struct batadv_tt_global_entry,
2088 common);
5dafd8a6 2089 batadv_tt_global_entry_put(tt_global);
7683fdc1
AQ
2090 }
2091 spin_unlock_bh(list_lock);
2092 }
2093
1a8eaf07 2094 batadv_hash_destroy(hash);
7683fdc1 2095
807736f6 2096 bat_priv->tt.global_hash = NULL;
c6c8fea2
SE
2097}
2098
56303d34
SE
2099static bool
2100_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2101 struct batadv_tt_global_entry *tt_global_entry)
59b699cd
AQ
2102{
2103 bool ret = false;
2104
acd34afa
SE
2105 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2106 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
59b699cd
AQ
2107 ret = true;
2108
2d2fcc2a
AQ
2109 /* 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
59b699cd
AQ
2114 return ret;
2115}
2116
c018ad3d
AQ
2117/**
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 *
62fe710f 2124 * Return: a pointer to the originator that was selected as destination in the
c018ad3d
AQ
2125 * 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 */
56303d34 2131struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
6b5e971a
SE
2132 const u8 *src,
2133 const u8 *addr,
c018ad3d 2134 unsigned short vid)
c6c8fea2 2135{
56303d34
SE
2136 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;
981d8900 2139 struct batadv_tt_orig_list_entry *best_entry;
b8cbd81d 2140
eceb22ae 2141 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
c018ad3d 2142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
068ee6e2
AQ
2143 if (!tt_local_entry ||
2144 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
3d393e47
AQ
2145 goto out;
2146 }
7aadf889 2147
c018ad3d 2148 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2dafb49d 2149 if (!tt_global_entry)
7b36e8ee 2150 goto out;
7aadf889 2151
3d393e47 2152 /* check whether the clients should not communicate due to AP
9cfc7bd6
SE
2153 * isolation
2154 */
a513088d
SE
2155 if (tt_local_entry &&
2156 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3d393e47
AQ
2157 goto out;
2158
db08e6e5 2159 rcu_read_lock();
4627456a 2160 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
db08e6e5 2161 /* found anything? */
981d8900
SE
2162 if (best_entry)
2163 orig_node = best_entry->orig_node;
7c124391 2164 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
db08e6e5
SW
2165 orig_node = NULL;
2166 rcu_read_unlock();
981d8900 2167
7b36e8ee 2168out:
3d393e47 2169 if (tt_global_entry)
5dafd8a6 2170 batadv_tt_global_entry_put(tt_global_entry);
3d393e47 2171 if (tt_local_entry)
95c0db90 2172 batadv_tt_local_entry_put(tt_local_entry);
3d393e47 2173
7b36e8ee 2174 return orig_node;
c6c8fea2 2175}
a73105b8 2176
ced72933
AQ
2177/**
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
0ffa9e8d 2181 * @orig_node: originator for which the CRC should be computed
7ea7b4a1 2182 * @vid: VLAN identifier for which the CRC32 has to be computed
0ffa9e8d
AQ
2183 *
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 *
62fe710f 2199 * Return: the checksum of the global table of a given originator.
ced72933 2200 */
6b5e971a
SE
2201static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2202 struct batadv_orig_node *orig_node,
2203 unsigned short vid)
a73105b8 2204{
807736f6 2205 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
2206 struct batadv_tt_common_entry *tt_common;
2207 struct batadv_tt_global_entry *tt_global;
a73105b8 2208 struct hlist_head *head;
6b5e971a
SE
2209 u32 i, crc_tmp, crc = 0;
2210 u8 flags;
a30e22ca 2211 __be16 tmp_vid;
a73105b8
AQ
2212
2213 for (i = 0; i < hash->size; i++) {
2214 head = &hash->table[i];
2215
2216 rcu_read_lock();
b67bfe0d 2217 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
56303d34
SE
2218 tt_global = container_of(tt_common,
2219 struct batadv_tt_global_entry,
2220 common);
7ea7b4a1
AQ
2221 /* 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
db08e6e5
SW
2227 /* 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 */
acd34afa 2232 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
db08e6e5 2233 continue;
30cfd02b
AQ
2234 /* 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;
db08e6e5
SW
2240
2241 /* find out if this global entry is announced by this
2242 * originator
2243 */
56303d34 2244 if (!batadv_tt_global_entry_has_orig(tt_global,
a513088d 2245 orig_node))
db08e6e5
SW
2246 continue;
2247
a30e22ca
AQ
2248 /* 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));
0eb01568
AQ
2253
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
0ffa9e8d 2260 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
a73105b8
AQ
2261 }
2262 rcu_read_unlock();
2263 }
2264
ced72933 2265 return crc;
a73105b8
AQ
2266}
2267
ced72933
AQ
2268/**
2269 * batadv_tt_local_crc - calculates the checksum of the local table
2270 * @bat_priv: the bat priv with all the soft interface information
7ea7b4a1 2271 * @vid: VLAN identifier for which the CRC32 has to be computed
0ffa9e8d
AQ
2272 *
2273 * For details about the computation, please refer to the documentation for
2274 * batadv_tt_global_crc().
2275 *
62fe710f 2276 * Return: the checksum of the local table
ced72933 2277 */
6b5e971a
SE
2278static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2279 unsigned short vid)
a73105b8 2280{
807736f6 2281 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 2282 struct batadv_tt_common_entry *tt_common;
a73105b8 2283 struct hlist_head *head;
6b5e971a
SE
2284 u32 i, crc_tmp, crc = 0;
2285 u8 flags;
a30e22ca 2286 __be16 tmp_vid;
a73105b8
AQ
2287
2288 for (i = 0; i < hash->size; i++) {
2289 head = &hash->table[i];
2290
2291 rcu_read_lock();
b67bfe0d 2292 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
7ea7b4a1
AQ
2293 /* compute the CRC only for entries belonging to the
2294 * VLAN identified by vid
2295 */
2296 if (tt_common->vid != vid)
2297 continue;
2298
058d0e26 2299 /* not yet committed clients have not to be taken into
9cfc7bd6
SE
2300 * account while computing the CRC
2301 */
acd34afa 2302 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
058d0e26 2303 continue;
ced72933 2304
a30e22ca
AQ
2305 /* 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));
0eb01568
AQ
2310
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
0ffa9e8d 2317 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
a73105b8 2318 }
a73105b8
AQ
2319 rcu_read_unlock();
2320 }
2321
ced72933 2322 return crc;
a73105b8
AQ
2323}
2324
9c4604a2
SE
2325/**
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
86452f81 2335 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
9c4604a2
SE
2336}
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
56303d34 2348static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
a73105b8 2349{
7c26a53b
ML
2350 struct batadv_tt_req_node *node;
2351 struct hlist_node *safe;
a73105b8 2352
807736f6 2353 spin_lock_bh(&bat_priv->tt.req_list_lock);
a73105b8 2354
7c26a53b
ML
2355 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2356 hlist_del_init(&node->list);
9c4604a2 2357 batadv_tt_req_node_put(node);
a73105b8
AQ
2358 }
2359
807736f6 2360 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2361}
2362
56303d34
SE
2363static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2364 struct batadv_orig_node *orig_node,
e8cf234a 2365 const void *tt_buff,
6b5e971a 2366 u16 tt_buff_len)
a73105b8 2367{
a73105b8 2368 /* Replace the old buffer only if I received something in the
9cfc7bd6
SE
2369 * last OGM (the OGM could carry no changes)
2370 */
a73105b8
AQ
2371 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
56303d34 2384static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
a73105b8 2385{
7c26a53b
ML
2386 struct batadv_tt_req_node *node;
2387 struct hlist_node *safe;
a73105b8 2388
807736f6 2389 spin_lock_bh(&bat_priv->tt.req_list_lock);
7c26a53b 2390 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
42d0b044
SE
2391 if (batadv_has_timed_out(node->issued_at,
2392 BATADV_TT_REQUEST_TIMEOUT)) {
7c26a53b 2393 hlist_del_init(&node->list);
9c4604a2 2394 batadv_tt_req_node_put(node);
a73105b8
AQ
2395 }
2396 }
807736f6 2397 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2398}
2399
383b8636
ML
2400/**
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 *
62fe710f 2405 * Return: the pointer to the new tt_req_node struct if no request
383b8636 2406 * has already been issued for this orig_node, NULL otherwise.
9cfc7bd6 2407 */
56303d34 2408static struct batadv_tt_req_node *
383b8636 2409batadv_tt_req_node_new(struct batadv_priv *bat_priv,
56303d34 2410 struct batadv_orig_node *orig_node)
a73105b8 2411{
56303d34 2412 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
a73105b8 2413
807736f6 2414 spin_lock_bh(&bat_priv->tt.req_list_lock);
7c26a53b 2415 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1eda58bf
SE
2416 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2417 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
42d0b044 2418 BATADV_TT_REQUEST_TIMEOUT))
a73105b8
AQ
2419 goto unlock;
2420 }
2421
86452f81 2422 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
a73105b8
AQ
2423 if (!tt_req_node)
2424 goto unlock;
2425
9c4604a2 2426 kref_init(&tt_req_node->refcount);
8fdd0153 2427 ether_addr_copy(tt_req_node->addr, orig_node->orig);
a73105b8
AQ
2428 tt_req_node->issued_at = jiffies;
2429
9c4604a2 2430 kref_get(&tt_req_node->refcount);
7c26a53b 2431 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
a73105b8 2432unlock:
807736f6 2433 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2434 return tt_req_node;
2435}
2436
335fbe0f
ML
2437/**
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 *
4b426b10 2442 * Return: true if the entry is a valid, false otherwise.
335fbe0f 2443 */
4b426b10 2444static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
058d0e26 2445{
56303d34 2446 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
058d0e26 2447
acd34afa 2448 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
4b426b10
SE
2449 return false;
2450 return true;
058d0e26
AQ
2451}
2452
4b426b10
SE
2453static bool batadv_tt_global_valid(const void *entry_ptr,
2454 const void *data_ptr)
a73105b8 2455{
56303d34
SE
2456 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;
a73105b8 2459
30cfd02b
AQ
2460 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2461 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
4b426b10 2462 return false;
cc47f66e 2463
56303d34
SE
2464 tt_global_entry = container_of(tt_common_entry,
2465 struct batadv_tt_global_entry,
48100bac
AQ
2466 common);
2467
a513088d 2468 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
a73105b8
AQ
2469}
2470
335fbe0f 2471/**
7ea7b4a1
AQ
2472 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2473 * specified tt hash
335fbe0f
ML
2474 * @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
7ea7b4a1 2477 * @tvlv_buff: pointer to the buffer to fill with the TT data
335fbe0f
ML
2478 * @valid_cb: function to filter tt change entries
2479 * @cb_data: data passed to the filter function as argument
335fbe0f 2480 */
7ea7b4a1
AQ
2481static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2482 struct batadv_hashtable *hash,
6b5e971a 2483 void *tvlv_buff, u16 tt_len,
4b426b10
SE
2484 bool (*valid_cb)(const void *,
2485 const void *),
7ea7b4a1 2486 void *cb_data)
a73105b8 2487{
56303d34 2488 struct batadv_tt_common_entry *tt_common_entry;
335fbe0f 2489 struct batadv_tvlv_tt_change *tt_change;
a73105b8 2490 struct hlist_head *head;
6b5e971a
SE
2491 u16 tt_tot, tt_num_entries = 0;
2492 u32 i;
a73105b8 2493
298e6e68 2494 tt_tot = batadv_tt_entries(tt_len);
7ea7b4a1 2495 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
a73105b8
AQ
2496
2497 rcu_read_lock();
2498 for (i = 0; i < hash->size; i++) {
2499 head = &hash->table[i];
2500
b67bfe0d 2501 hlist_for_each_entry_rcu(tt_common_entry,
a73105b8 2502 head, hash_entry) {
335fbe0f 2503 if (tt_tot == tt_num_entries)
a73105b8
AQ
2504 break;
2505
48100bac 2506 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
a73105b8
AQ
2507 continue;
2508
8fdd0153 2509 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
27b37ebf 2510 tt_change->flags = tt_common_entry->flags;
c018ad3d 2511 tt_change->vid = htons(tt_common_entry->vid);
ca663046
AQ
2512 memset(tt_change->reserved, 0,
2513 sizeof(tt_change->reserved));
a73105b8 2514
335fbe0f 2515 tt_num_entries++;
a73105b8
AQ
2516 tt_change++;
2517 }
2518 }
2519 rcu_read_unlock();
7ea7b4a1 2520}
a73105b8 2521
7ea7b4a1
AQ
2522/**
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
7ea7b4a1 2527 *
62fe710f 2528 * Return: true if all the received CRCs match the locally stored ones, false
7ea7b4a1
AQ
2529 * otherwise
2530 */
2531static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2532 struct batadv_tvlv_tt_vlan_data *tt_vlan,
6b5e971a 2533 u16 num_vlan)
7ea7b4a1
AQ
2534{
2535 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2536 struct batadv_orig_node_vlan *vlan;
c169c59d 2537 int i, orig_num_vlan;
6b5e971a 2538 u32 crc;
7ea7b4a1
AQ
2539
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,
cfd4f757
AQ
2548 orig_node->orig,
2549 ntohs(tt_vlan_tmp->vid)))
7ea7b4a1
AQ
2550 continue;
2551
2552 vlan = batadv_orig_node_vlan_get(orig_node,
2553 ntohs(tt_vlan_tmp->vid));
2554 if (!vlan)
2555 return false;
2556
91c2b1a9 2557 crc = vlan->tt.crc;
21754e25 2558 batadv_orig_node_vlan_put(vlan);
91c2b1a9
AQ
2559
2560 if (crc != ntohl(tt_vlan_tmp->crc))
7ea7b4a1
AQ
2561 return false;
2562 }
2563
c169c59d
SW
2564 /* 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
7ea7b4a1
AQ
2576 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;
6b5e971a 2604 u32 crc;
7ea7b4a1
AQ
2605
2606 /* recompute the global CRC for each VLAN */
2607 rcu_read_lock();
d0fa4f3f 2608 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
2609 /* 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 */
cfd4f757
AQ
2612 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2613 vlan->vid))
7ea7b4a1
AQ
2614 continue;
2615
2616 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2617 vlan->tt.crc = crc;
2618 }
2619 rcu_read_unlock();
a73105b8
AQ
2620}
2621
ced72933
AQ
2622/**
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
7ea7b4a1
AQ
2627 * @tt_vlan: pointer to the first tvlv VLAN object to request
2628 * @num_vlan: number of tvlv VLAN entries
ced72933
AQ
2629 * @full_table: ask for the entire translation table if true, while only for the
2630 * last TT diff otherwise
d15cd622
AQ
2631 *
2632 * Return: true if the TT Request was sent, false otherwise
ced72933 2633 */
4b426b10
SE
2634static 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)
a73105b8 2639{
335fbe0f 2640 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
56303d34 2641 struct batadv_tt_req_node *tt_req_node = NULL;
7ea7b4a1
AQ
2642 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2643 struct batadv_hard_iface *primary_if;
335fbe0f 2644 bool ret = false;
7ea7b4a1 2645 int i, size;
a73105b8 2646
e5d89254 2647 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
2648 if (!primary_if)
2649 goto out;
2650
2651 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
2652 * reply from the same orig_node yet
2653 */
383b8636 2654 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
a73105b8
AQ
2655 if (!tt_req_node)
2656 goto out;
2657
7ea7b4a1
AQ
2658 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2659 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
335fbe0f 2660 if (!tvlv_tt_data)
a73105b8
AQ
2661 goto out;
2662
335fbe0f
ML
2663 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2664 tvlv_tt_data->ttvn = ttvn;
7ea7b4a1
AQ
2665 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 }
a73105b8
AQ
2678
2679 if (full_table)
335fbe0f 2680 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 2681
bb351ba0 2682 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
335fbe0f 2683 dst_orig_node->orig, full_table ? 'F' : '.');
a73105b8 2684
d69909d2 2685 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
335fbe0f
ML
2686 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2687 dst_orig_node->orig, BATADV_TVLV_TT, 1,
7ea7b4a1 2688 tvlv_tt_data, size);
335fbe0f 2689 ret = true;
a73105b8
AQ
2690
2691out:
a73105b8 2692 if (primary_if)
82047ad7 2693 batadv_hardif_put(primary_if);
9c4604a2 2694
a73105b8 2695 if (ret && tt_req_node) {
807736f6 2696 spin_lock_bh(&bat_priv->tt.req_list_lock);
9c4604a2
SE
2697 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 }
807736f6 2701 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8 2702 }
9c4604a2
SE
2703
2704 if (tt_req_node)
2705 batadv_tt_req_node_put(tt_req_node);
2706
335fbe0f 2707 kfree(tvlv_tt_data);
a73105b8
AQ
2708 return ret;
2709}
2710
335fbe0f
ML
2711/**
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 *
62fe710f 2719 * Return: true if tt request reply was sent, false otherwise.
335fbe0f
ML
2720 */
2721static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2722 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 2723 u8 *req_src, u8 *req_dst)
a73105b8 2724{
170173bf 2725 struct batadv_orig_node *req_dst_orig_node;
56303d34 2726 struct batadv_orig_node *res_dst_orig_node = NULL;
7ea7b4a1 2727 struct batadv_tvlv_tt_change *tt_change;
335fbe0f 2728 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
7ea7b4a1 2729 struct batadv_tvlv_tt_vlan_data *tt_vlan;
335fbe0f 2730 bool ret = false, full_table;
6b5e971a
SE
2731 u8 orig_ttvn, req_ttvn;
2732 u16 tvlv_len;
2733 s32 tt_len;
a73105b8 2734
39c75a51 2735 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2736 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
335fbe0f 2737 req_src, tt_data->ttvn, req_dst,
a2f2b6cd 2738 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8
AQ
2739
2740 /* Let's get the orig node of the REAL destination */
335fbe0f 2741 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
a73105b8
AQ
2742 if (!req_dst_orig_node)
2743 goto out;
2744
335fbe0f 2745 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
a73105b8
AQ
2746 if (!res_dst_orig_node)
2747 goto out;
2748
6b5e971a 2749 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
335fbe0f 2750 req_ttvn = tt_data->ttvn;
a73105b8 2751
7ea7b4a1 2752 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
335fbe0f 2753 /* this node doesn't have the requested data */
a73105b8 2754 if (orig_ttvn != req_ttvn ||
7ea7b4a1
AQ
2755 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2756 ntohs(tt_data->num_vlan)))
a73105b8
AQ
2757 goto out;
2758
015758d0 2759 /* If the full table has been explicitly requested */
335fbe0f 2760 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
a73105b8
AQ
2761 !req_dst_orig_node->tt_buff)
2762 full_table = true;
2763 else
2764 full_table = false;
2765
335fbe0f
ML
2766 /* TT fragmentation hasn't been implemented yet, so send as many
2767 * TT entries fit a single packet as possible only
9cfc7bd6 2768 */
a73105b8
AQ
2769 if (!full_table) {
2770 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2771 tt_len = req_dst_orig_node->tt_buff_len;
a73105b8 2772
7ea7b4a1
AQ
2773 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)
a73105b8
AQ
2778 goto unlock;
2779
a73105b8 2780 /* Copy the last orig_node's OGM buffer */
7ea7b4a1 2781 memcpy(tt_change, req_dst_orig_node->tt_buff,
a73105b8 2782 req_dst_orig_node->tt_buff_len);
a73105b8
AQ
2783 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2784 } else {
7ea7b4a1
AQ
2785 /* 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)
a73105b8 2794 goto out;
7ea7b4a1
AQ
2795
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);
a73105b8
AQ
2801 }
2802
a19d3d85
ML
2803 /* 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
335fbe0f
ML
2812 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2813 tvlv_tt_data->ttvn = req_ttvn;
a73105b8
AQ
2814
2815 if (full_table)
335fbe0f 2816 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 2817
39c75a51 2818 batadv_dbg(BATADV_DBG_TT, bat_priv,
335fbe0f
ML
2819 "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);
a73105b8 2822
d69909d2 2823 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 2824
335fbe0f 2825 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
7ea7b4a1
AQ
2826 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2827 tvlv_len);
e91ecfc6 2828
335fbe0f 2829 ret = true;
a73105b8
AQ
2830 goto out;
2831
2832unlock:
2833 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2834
2835out:
2836 if (res_dst_orig_node)
5d967310 2837 batadv_orig_node_put(res_dst_orig_node);
a73105b8 2838 if (req_dst_orig_node)
5d967310 2839 batadv_orig_node_put(req_dst_orig_node);
335fbe0f 2840 kfree(tvlv_tt_data);
a73105b8 2841 return ret;
a73105b8 2842}
96412690 2843
335fbe0f
ML
2844/**
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 *
62fe710f 2851 * Return: true if tt request reply was sent, false otherwise.
335fbe0f
ML
2852 */
2853static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2854 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 2855 u8 *req_src)
a73105b8 2856{
335fbe0f 2857 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
56303d34 2858 struct batadv_hard_iface *primary_if = NULL;
7ea7b4a1
AQ
2859 struct batadv_tvlv_tt_change *tt_change;
2860 struct batadv_orig_node *orig_node;
6b5e971a
SE
2861 u8 my_ttvn, req_ttvn;
2862 u16 tvlv_len;
a73105b8 2863 bool full_table;
6b5e971a 2864 s32 tt_len;
a73105b8 2865
39c75a51 2866 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2867 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
335fbe0f 2868 req_src, tt_data->ttvn,
a2f2b6cd 2869 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8 2870
a70a9aa9 2871 spin_lock_bh(&bat_priv->tt.commit_lock);
a73105b8 2872
6b5e971a 2873 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
335fbe0f 2874 req_ttvn = tt_data->ttvn;
a73105b8 2875
335fbe0f 2876 orig_node = batadv_orig_hash_find(bat_priv, req_src);
a73105b8
AQ
2877 if (!orig_node)
2878 goto out;
2879
e5d89254 2880 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
2881 if (!primary_if)
2882 goto out;
2883
2884 /* If the full table has been explicitly requested or the gap
9cfc7bd6
SE
2885 * is too big send the whole local translation table
2886 */
335fbe0f 2887 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
807736f6 2888 !bat_priv->tt.last_changeset)
a73105b8
AQ
2889 full_table = true;
2890 else
2891 full_table = false;
2892
335fbe0f
ML
2893 /* TT fragmentation hasn't been implemented yet, so send as many
2894 * TT entries fit a single packet as possible only
9cfc7bd6 2895 */
a73105b8 2896 if (!full_table) {
807736f6 2897 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 2898
7ea7b4a1
AQ
2899 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)
a73105b8
AQ
2905 goto unlock;
2906
335fbe0f 2907 /* Copy the last orig_node's OGM buffer */
7ea7b4a1 2908 memcpy(tt_change, bat_priv->tt.last_changeset,
807736f6
SE
2909 bat_priv->tt.last_changeset_len);
2910 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 2911 } else {
6b5e971a 2912 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
335fbe0f 2913
7ea7b4a1
AQ
2914 /* 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)
a73105b8 2923 goto out;
7ea7b4a1
AQ
2924
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);
a73105b8
AQ
2929 }
2930
335fbe0f
ML
2931 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2932 tvlv_tt_data->ttvn = req_ttvn;
a73105b8
AQ
2933
2934 if (full_table)
335fbe0f 2935 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 2936
39c75a51 2937 batadv_dbg(BATADV_DBG_TT, bat_priv,
335fbe0f
ML
2938 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2939 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
a73105b8 2940
d69909d2 2941 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 2942
335fbe0f 2943 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
7ea7b4a1
AQ
2944 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2945 tvlv_len);
335fbe0f 2946
a73105b8
AQ
2947 goto out;
2948
2949unlock:
807736f6 2950 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 2951out:
a70a9aa9 2952 spin_unlock_bh(&bat_priv->tt.commit_lock);
a73105b8 2953 if (orig_node)
5d967310 2954 batadv_orig_node_put(orig_node);
a73105b8 2955 if (primary_if)
82047ad7 2956 batadv_hardif_put(primary_if);
335fbe0f
ML
2957 kfree(tvlv_tt_data);
2958 /* The packet was for this host, so it doesn't need to be re-routed */
a73105b8
AQ
2959 return true;
2960}
2961
335fbe0f
ML
2962/**
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 *
62fe710f 2969 * Return: true if tt request reply was sent, false otherwise.
335fbe0f
ML
2970 */
2971static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2972 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 2973 u8 *req_src, u8 *req_dst)
a73105b8 2974{
cfd4f757 2975 if (batadv_is_my_mac(bat_priv, req_dst))
335fbe0f 2976 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
24820df1
AQ
2977 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2978 req_dst);
a73105b8
AQ
2979}
2980
56303d34
SE
2981static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2982 struct batadv_orig_node *orig_node,
335fbe0f 2983 struct batadv_tvlv_tt_change *tt_change,
6b5e971a 2984 u16 tt_num_changes, u8 ttvn)
a73105b8
AQ
2985{
2986 int i;
a513088d 2987 int roams;
a73105b8
AQ
2988
2989 for (i = 0; i < tt_num_changes; i++) {
acd34afa
SE
2990 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2991 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
a513088d
SE
2992 batadv_tt_global_del(bat_priv, orig_node,
2993 (tt_change + i)->addr,
c018ad3d 2994 ntohs((tt_change + i)->vid),
d4f44692
AQ
2995 "tt removed by changes",
2996 roams);
08c36d3e 2997 } else {
08c36d3e 2998 if (!batadv_tt_global_add(bat_priv, orig_node,
d4f44692 2999 (tt_change + i)->addr,
c018ad3d 3000 ntohs((tt_change + i)->vid),
d4f44692 3001 (tt_change + i)->flags, ttvn))
a73105b8
AQ
3002 /* 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;
08c36d3e 3009 }
a73105b8 3010 }
ac4eebd4 3011 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
a73105b8
AQ
3012}
3013
56303d34 3014static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
7ea7b4a1 3015 struct batadv_tvlv_tt_change *tt_change,
6b5e971a
SE
3016 u8 ttvn, u8 *resp_src,
3017 u16 num_entries)
a73105b8 3018{
170173bf 3019 struct batadv_orig_node *orig_node;
a73105b8 3020
335fbe0f 3021 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
a73105b8
AQ
3022 if (!orig_node)
3023 goto out;
3024
3025 /* Purge the old table first.. */
95fb130d
AQ
3026 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3027 "Received full table");
a73105b8 3028
7ea7b4a1
AQ
3029 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3030 ttvn);
a73105b8
AQ
3031
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
7ea7b4a1 3038 atomic_set(&orig_node->last_ttvn, ttvn);
a73105b8
AQ
3039
3040out:
3041 if (orig_node)
5d967310 3042 batadv_orig_node_put(orig_node);
a73105b8
AQ
3043}
3044
56303d34
SE
3045static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3046 struct batadv_orig_node *orig_node,
6b5e971a 3047 u16 tt_num_changes, u8 ttvn,
335fbe0f 3048 struct batadv_tvlv_tt_change *tt_change)
a73105b8 3049{
a513088d
SE
3050 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3051 tt_num_changes, ttvn);
a73105b8 3052
e8cf234a
AQ
3053 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3054 batadv_tt_len(tt_num_changes));
a73105b8
AQ
3055 atomic_set(&orig_node->last_ttvn, ttvn);
3056}
3057
c018ad3d
AQ
3058/**
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
3f68785e 3061 * @addr: the mac address of the client to check
c018ad3d
AQ
3062 * @vid: VLAN identifier
3063 *
62fe710f 3064 * Return: true if the client is served by this node, false otherwise.
c018ad3d 3065 */
6b5e971a 3066bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
c018ad3d 3067 unsigned short vid)
a73105b8 3068{
170173bf 3069 struct batadv_tt_local_entry *tt_local_entry;
7683fdc1 3070 bool ret = false;
a73105b8 3071
c018ad3d 3072 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7683fdc1
AQ
3073 if (!tt_local_entry)
3074 goto out;
058d0e26 3075 /* Check if the client has been logically deleted (but is kept for
9cfc7bd6
SE
3076 * consistency purpose)
3077 */
7c1fd91d
AQ
3078 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3079 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
058d0e26 3080 goto out;
7683fdc1
AQ
3081 ret = true;
3082out:
a73105b8 3083 if (tt_local_entry)
95c0db90 3084 batadv_tt_local_entry_put(tt_local_entry);
7683fdc1 3085 return ret;
a73105b8
AQ
3086}
3087
335fbe0f
ML
3088/**
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,
6b5e971a 3097 u8 *resp_src, u16 num_entries)
a73105b8 3098{
7c26a53b
ML
3099 struct batadv_tt_req_node *node;
3100 struct hlist_node *safe;
56303d34 3101 struct batadv_orig_node *orig_node = NULL;
335fbe0f 3102 struct batadv_tvlv_tt_change *tt_change;
6b5e971a
SE
3103 u8 *tvlv_ptr = (u8 *)tt_data;
3104 u16 change_offset;
a73105b8 3105
39c75a51 3106 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3107 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
335fbe0f 3108 resp_src, tt_data->ttvn, num_entries,
a2f2b6cd 3109 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8 3110
335fbe0f 3111 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
a73105b8
AQ
3112 if (!orig_node)
3113 goto out;
3114
a70a9aa9
AQ
3115 spin_lock_bh(&orig_node->tt_lock);
3116
7ea7b4a1
AQ
3117 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;
335fbe0f 3123 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
7ea7b4a1
AQ
3124 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3125 resp_src, num_entries);
96412690 3126 } else {
335fbe0f
ML
3127 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3128 tt_data->ttvn, tt_change);
96412690 3129 }
a73105b8 3130
a70a9aa9 3131 /* Recalculate the CRC for this orig_node and store it */
7ea7b4a1 3132 batadv_tt_global_update_crc(bat_priv, orig_node);
a70a9aa9
AQ
3133
3134 spin_unlock_bh(&orig_node->tt_lock);
3135
a73105b8 3136 /* Delete the tt_req_node from pending tt_requests list */
807736f6 3137 spin_lock_bh(&bat_priv->tt.req_list_lock);
7c26a53b 3138 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
335fbe0f 3139 if (!batadv_compare_eth(node->addr, resp_src))
a73105b8 3140 continue;
7c26a53b 3141 hlist_del_init(&node->list);
9c4604a2 3142 batadv_tt_req_node_put(node);
a73105b8 3143 }
7ea7b4a1 3144
807736f6 3145 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
3146out:
3147 if (orig_node)
5d967310 3148 batadv_orig_node_put(orig_node);
a73105b8
AQ
3149}
3150
56303d34 3151static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
a73105b8 3152{
56303d34 3153 struct batadv_tt_roam_node *node, *safe;
a73105b8 3154
807736f6 3155 spin_lock_bh(&bat_priv->tt.roam_list_lock);
a73105b8 3156
807736f6 3157 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
cc47f66e 3158 list_del(&node->list);
86452f81 3159 kmem_cache_free(batadv_tt_roam_cache, node);
cc47f66e
AQ
3160 }
3161
807736f6 3162 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3163}
3164
56303d34 3165static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
cc47f66e 3166{
56303d34 3167 struct batadv_tt_roam_node *node, *safe;
cc47f66e 3168
807736f6
SE
3169 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3170 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
42d0b044
SE
3171 if (!batadv_has_timed_out(node->first_time,
3172 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
3173 continue;
3174
3175 list_del(&node->list);
86452f81 3176 kmem_cache_free(batadv_tt_roam_cache, node);
cc47f66e 3177 }
807736f6 3178 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3179}
3180
62fe710f 3181/**
d15cd622
AQ
3182 * 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
62fe710f
SE
3185 *
3186 * This function checks whether the client already reached the
cc47f66e
AQ
3187 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3188 * will not be sent.
3189 *
62fe710f 3190 * Return: true if the ROAMING_ADV can be sent, false otherwise
9cfc7bd6 3191 */
6b5e971a 3192static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
cc47f66e 3193{
56303d34 3194 struct batadv_tt_roam_node *tt_roam_node;
cc47f66e
AQ
3195 bool ret = false;
3196
807736f6 3197 spin_lock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e 3198 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
3199 * reply from the same orig_node yet
3200 */
807736f6 3201 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
1eda58bf 3202 if (!batadv_compare_eth(tt_roam_node->addr, client))
cc47f66e
AQ
3203 continue;
3204
1eda58bf 3205 if (batadv_has_timed_out(tt_roam_node->first_time,
42d0b044 3206 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
3207 continue;
3208
3e34819e 3209 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
cc47f66e
AQ
3210 /* Sorry, you roamed too many times! */
3211 goto unlock;
3212 ret = true;
3213 break;
3214 }
3215
3216 if (!ret) {
86452f81
SE
3217 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3218 GFP_ATOMIC);
cc47f66e
AQ
3219 if (!tt_roam_node)
3220 goto unlock;
3221
3222 tt_roam_node->first_time = jiffies;
42d0b044
SE
3223 atomic_set(&tt_roam_node->counter,
3224 BATADV_ROAMING_MAX_COUNT - 1);
8fdd0153 3225 ether_addr_copy(tt_roam_node->addr, client);
cc47f66e 3226
807736f6 3227 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
cc47f66e
AQ
3228 ret = true;
3229 }
3230
3231unlock:
807736f6 3232 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3233 return ret;
3234}
3235
c018ad3d
AQ
3236/**
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 */
6b5e971a 3248static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
c018ad3d 3249 unsigned short vid,
56303d34 3250 struct batadv_orig_node *orig_node)
cc47f66e 3251{
56303d34 3252 struct batadv_hard_iface *primary_if;
122edaa0
ML
3253 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;
cc47f66e
AQ
3258
3259 /* before going on we have to check whether the client has
9cfc7bd6
SE
3260 * already roamed to us too many times
3261 */
a513088d 3262 if (!batadv_tt_check_roam_count(bat_priv, client))
cc47f66e
AQ
3263 goto out;
3264
39c75a51 3265 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3266 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3267 orig_node->orig, client, BATADV_PRINT_VID(vid));
cc47f66e 3268
d69909d2 3269 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
f8214865 3270
122edaa0 3271 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
c018ad3d 3272 tvlv_roam.vid = htons(vid);
122edaa0
ML
3273
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));
cc47f66e
AQ
3277
3278out:
122edaa0 3279 if (primary_if)
82047ad7 3280 batadv_hardif_put(primary_if);
a73105b8
AQ
3281}
3282
a513088d 3283static void batadv_tt_purge(struct work_struct *work)
a73105b8 3284{
56303d34 3285 struct delayed_work *delayed_work;
807736f6 3286 struct batadv_priv_tt *priv_tt;
56303d34
SE
3287 struct batadv_priv *bat_priv;
3288
4ba4bc0f 3289 delayed_work = to_delayed_work(work);
807736f6
SE
3290 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3291 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
a73105b8 3292
a19d3d85 3293 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
30cfd02b 3294 batadv_tt_global_purge(bat_priv);
a513088d
SE
3295 batadv_tt_req_purge(bat_priv);
3296 batadv_tt_roam_purge(bat_priv);
a73105b8 3297
72414442
AQ
3298 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3299 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
a73105b8 3300}
cc47f66e 3301
56303d34 3302void batadv_tt_free(struct batadv_priv *bat_priv)
cc47f66e 3303{
e1bf0c14
ML
3304 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3305 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3306
807736f6 3307 cancel_delayed_work_sync(&bat_priv->tt.work);
cc47f66e 3308
a513088d
SE
3309 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);
cc47f66e 3314
807736f6 3315 kfree(bat_priv->tt.last_changeset);
cc47f66e 3316}
058d0e26 3317
7ea7b4a1
AQ
3318/**
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
9cfc7bd6 3325 */
6b5e971a
SE
3326static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3327 bool enable, bool count)
058d0e26 3328{
7ea7b4a1
AQ
3329 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3330 struct batadv_tt_common_entry *tt_common_entry;
6b5e971a 3331 u16 changed_num = 0;
058d0e26 3332 struct hlist_head *head;
6b5e971a 3333 u32 i;
058d0e26
AQ
3334
3335 if (!hash)
7ea7b4a1 3336 return;
058d0e26
AQ
3337
3338 for (i = 0; i < hash->size; i++) {
3339 head = &hash->table[i];
3340
3341 rcu_read_lock();
b67bfe0d 3342 hlist_for_each_entry_rcu(tt_common_entry,
058d0e26 3343 head, hash_entry) {
697f2531
AQ
3344 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++;
7ea7b4a1
AQ
3354
3355 if (!count)
3356 continue;
3357
3358 batadv_tt_local_size_inc(bat_priv,
3359 tt_common_entry->vid);
058d0e26
AQ
3360 }
3361 rcu_read_unlock();
3362 }
058d0e26
AQ
3363}
3364
acd34afa 3365/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
56303d34 3366static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
058d0e26 3367{
807736f6 3368 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34
SE
3369 struct batadv_tt_common_entry *tt_common;
3370 struct batadv_tt_local_entry *tt_local;
b67bfe0d 3371 struct hlist_node *node_tmp;
058d0e26
AQ
3372 struct hlist_head *head;
3373 spinlock_t *list_lock; /* protects write access to the hash lists */
6b5e971a 3374 u32 i;
058d0e26
AQ
3375
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);
b67bfe0d 3384 hlist_for_each_entry_safe(tt_common, node_tmp, head,
acd34afa
SE
3385 hash_entry) {
3386 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
058d0e26
AQ
3387 continue;
3388
39c75a51 3389 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3390 "Deleting local tt entry (%pM, vid: %d): pending\n",
3391 tt_common->addr,
3392 BATADV_PRINT_VID(tt_common->vid));
058d0e26 3393
7ea7b4a1 3394 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
b67bfe0d 3395 hlist_del_rcu(&tt_common->hash_entry);
56303d34
SE
3396 tt_local = container_of(tt_common,
3397 struct batadv_tt_local_entry,
3398 common);
35df3b29 3399
95c0db90 3400 batadv_tt_local_entry_put(tt_local);
058d0e26
AQ
3401 }
3402 spin_unlock_bh(list_lock);
3403 }
058d0e26
AQ
3404}
3405
e1bf0c14 3406/**
a19d3d85
ML
3407 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3408 * which have been queued in the time since the last commit
e1bf0c14 3409 * @bat_priv: the bat priv with all the soft interface information
a19d3d85
ML
3410 *
3411 * Caller must hold tt->commit_lock.
e1bf0c14 3412 */
a19d3d85 3413static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
058d0e26 3414{
5274cd68
SE
3415 lockdep_assert_held(&bat_priv->tt.commit_lock);
3416
c5caf4ef
LL
3417 /* Update multicast addresses in local translation table */
3418 batadv_mcast_mla_update(bat_priv);
3419
e1bf0c14
ML
3420 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);
a19d3d85 3423 return;
e1bf0c14 3424 }
be9aa4c1 3425
7ea7b4a1 3426 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
be9aa4c1 3427
a513088d 3428 batadv_tt_local_purge_pending_clients(bat_priv);
7ea7b4a1 3429 batadv_tt_local_update_crc(bat_priv);
058d0e26
AQ
3430
3431 /* Increment the TTVN only once per OGM interval */
807736f6 3432 atomic_inc(&bat_priv->tt.vn);
39c75a51 3433 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3434 "Local changes committed, updating to ttvn %u\n",
6b5e971a 3435 (u8)atomic_read(&bat_priv->tt.vn));
be9aa4c1
ML
3436
3437 /* reset the sending counter */
807736f6 3438 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
e1bf0c14 3439 batadv_tt_tvlv_container_update(bat_priv);
a19d3d85 3440}
a70a9aa9 3441
a19d3d85
ML
3442/**
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);
a70a9aa9 3451 spin_unlock_bh(&bat_priv->tt.commit_lock);
058d0e26 3452}
59b699cd 3453
6b5e971a
SE
3454bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3455 unsigned short vid)
59b699cd 3456{
56303d34
SE
3457 struct batadv_tt_local_entry *tt_local_entry = NULL;
3458 struct batadv_tt_global_entry *tt_global_entry = NULL;
b8cbd81d 3459 struct batadv_softif_vlan *vlan;
5870adc6 3460 bool ret = false;
59b699cd 3461
b8cbd81d 3462 vlan = batadv_softif_vlan_get(bat_priv, vid);
e087f34f
ME
3463 if (!vlan)
3464 return false;
3465
3466 if (!atomic_read(&vlan->ap_isolation))
5870adc6 3467 goto out;
59b699cd 3468
b8cbd81d 3469 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
59b699cd
AQ
3470 if (!tt_local_entry)
3471 goto out;
3472
b8cbd81d 3473 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
59b699cd
AQ
3474 if (!tt_global_entry)
3475 goto out;
3476
1f129fef 3477 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
59b699cd
AQ
3478 goto out;
3479
5870adc6 3480 ret = true;
59b699cd
AQ
3481
3482out:
9c3bf081 3483 batadv_softif_vlan_put(vlan);
59b699cd 3484 if (tt_global_entry)
5dafd8a6 3485 batadv_tt_global_entry_put(tt_global_entry);
59b699cd 3486 if (tt_local_entry)
95c0db90 3487 batadv_tt_local_entry_put(tt_local_entry);
59b699cd
AQ
3488 return ret;
3489}
a943cac1 3490
e1bf0c14
ML
3491/**
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
e51f0397
SE
3495 * @orig_node: the orig_node of the ogm
3496 * @tt_buff: pointer to the first tvlv VLAN entry
7ea7b4a1
AQ
3497 * @tt_num_vlan: number of tvlv VLAN entries
3498 * @tt_change: pointer to the first entry in the TT buffer
e1bf0c14
ML
3499 * @tt_num_changes: number of tt changes inside the tt buffer
3500 * @ttvn: translation table version number of this changeset
e1bf0c14
ML
3501 */
3502static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3503 struct batadv_orig_node *orig_node,
6b5e971a 3504 const void *tt_buff, u16 tt_num_vlan,
7ea7b4a1 3505 struct batadv_tvlv_tt_change *tt_change,
6b5e971a 3506 u16 tt_num_changes, u8 ttvn)
a943cac1 3507{
6b5e971a 3508 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
7ea7b4a1 3509 struct batadv_tvlv_tt_vlan_data *tt_vlan;
a943cac1 3510 bool full_table = true;
e17931d1 3511 bool has_tt_init;
a943cac1 3512
7ea7b4a1 3513 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
ac4eebd4
LL
3514 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3515 &orig_node->capa_initialized);
e17931d1 3516
17071578 3517 /* orig table not initialised AND first diff is in the OGM OR the ttvn
9cfc7bd6
SE
3518 * increased by one -> we can apply the attached changes
3519 */
e17931d1 3520 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
a943cac1 3521 /* the OGM could not contain the changes due to their size or
42d0b044
SE
3522 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3523 * times.
9cfc7bd6
SE
3524 * In this case send a tt request
3525 */
a943cac1
ML
3526 if (!tt_num_changes) {
3527 full_table = false;
3528 goto request_table;
3529 }
3530
a70a9aa9
AQ
3531 spin_lock_bh(&orig_node->tt_lock);
3532
a513088d 3533 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
96412690 3534 ttvn, tt_change);
a943cac1
ML
3535
3536 /* Even if we received the precomputed crc with the OGM, we
3537 * prefer to recompute it to spot any possible inconsistency
9cfc7bd6
SE
3538 * in the global table
3539 */
7ea7b4a1 3540 batadv_tt_global_update_crc(bat_priv, orig_node);
a943cac1 3541
a70a9aa9
AQ
3542 spin_unlock_bh(&orig_node->tt_lock);
3543
a943cac1
ML
3544 /* 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
9cfc7bd6
SE
3551 * inconsistency
3552 */
7ea7b4a1
AQ
3553 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3554 tt_num_vlan))
a943cac1 3555 goto request_table;
a943cac1
ML
3556 } else {
3557 /* if we missed more than one change or our tables are not
9cfc7bd6
SE
3558 * in sync anymore -> request fresh tt data
3559 */
e17931d1 3560 if (!has_tt_init || ttvn != orig_ttvn ||
7ea7b4a1
AQ
3561 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3562 tt_num_vlan)) {
a943cac1 3563request_table:
39c75a51 3564 batadv_dbg(BATADV_DBG_TT, bat_priv,
7ea7b4a1
AQ
3565 "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);
a513088d 3568 batadv_send_tt_request(bat_priv, orig_node, ttvn,
7ea7b4a1
AQ
3569 tt_vlan, tt_num_vlan,
3570 full_table);
a943cac1
ML
3571 return;
3572 }
3573 }
3574}
3275e7cc 3575
c018ad3d
AQ
3576/**
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 *
62fe710f 3582 * Return: true if we know that the client has moved from its old originator
c018ad3d
AQ
3583 * to another one. This entry is still kept for consistency purposes and will be
3584 * deleted later by a DEL or because of timeout
3275e7cc 3585 */
56303d34 3586bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
6b5e971a 3587 u8 *addr, unsigned short vid)
3275e7cc 3588{
56303d34 3589 struct batadv_tt_global_entry *tt_global_entry;
3275e7cc
AQ
3590 bool ret = false;
3591
c018ad3d 3592 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3275e7cc
AQ
3593 if (!tt_global_entry)
3594 goto out;
3595
c1d07431 3596 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
5dafd8a6 3597 batadv_tt_global_entry_put(tt_global_entry);
3275e7cc
AQ
3598out:
3599 return ret;
3600}
30cfd02b 3601
7c1fd91d
AQ
3602/**
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
c018ad3d
AQ
3605 * @addr: the mac address of the local client to query
3606 * @vid: VLAN identifier
7c1fd91d 3607 *
62fe710f 3608 * Return: true if the local client is known to be roaming (it is not served by
7c1fd91d
AQ
3609 * 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,
6b5e971a 3613 u8 *addr, unsigned short vid)
7c1fd91d
AQ
3614{
3615 struct batadv_tt_local_entry *tt_local_entry;
3616 bool ret = false;
3617
c018ad3d 3618 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7c1fd91d
AQ
3619 if (!tt_local_entry)
3620 goto out;
3621
3622 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
95c0db90 3623 batadv_tt_local_entry_put(tt_local_entry);
7c1fd91d
AQ
3624out:
3625 return ret;
7c1fd91d
AQ
3626}
3627
30cfd02b
AQ
3628bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3629 struct batadv_orig_node *orig_node,
c018ad3d 3630 const unsigned char *addr,
16052789 3631 unsigned short vid)
30cfd02b
AQ
3632{
3633 bool ret = false;
3634
16052789 3635 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
30cfd02b
AQ
3636 BATADV_TT_CLIENT_TEMP,
3637 atomic_read(&orig_node->last_ttvn)))
3638 goto out;
3639
3640 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3641 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3642 addr, BATADV_PRINT_VID(vid), orig_node->orig);
30cfd02b
AQ
3643 ret = true;
3644out:
3645 return ret;
3646}
e1bf0c14 3647
a19d3d85
ML
3648/**
3649 * 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
e1bf0c14
ML
3689/**
3690 * 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,
6b5e971a
SE
3699 u8 flags, void *tvlv_value,
3700 u16 tvlv_value_len)
e1bf0c14 3701{
7ea7b4a1
AQ
3702 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3703 struct batadv_tvlv_tt_change *tt_change;
e1bf0c14 3704 struct batadv_tvlv_tt_data *tt_data;
6b5e971a 3705 u16 num_entries, num_vlan;
e1bf0c14
ML
3706
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
7ea7b4a1
AQ
3713 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
298e6e68 3722 num_entries = batadv_tt_entries(tvlv_value_len);
e1bf0c14 3723
7ea7b4a1
AQ
3724 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3725 num_entries, tt_data->ttvn);
e1bf0c14
ML
3726}
3727
335fbe0f
ML
3728/**
3729 * 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 *
62fe710f 3737 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
335fbe0f
ML
3738 * otherwise.
3739 */
3740static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
6b5e971a 3741 u8 *src, u8 *dst,
335fbe0f 3742 void *tvlv_value,
6b5e971a 3743 u16 tvlv_value_len)
335fbe0f
ML
3744{
3745 struct batadv_tvlv_tt_data *tt_data;
6b5e971a 3746 u16 tt_vlan_len, tt_num_entries;
335fbe0f
ML
3747 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
7ea7b4a1
AQ
3756 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);
335fbe0f
ML
3764
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,
7ea7b4a1 3791 src, tt_num_entries);
335fbe0f
ML
3792 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
122edaa0
ML
3810/**
3811 * 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 *
62fe710f 3818 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
122edaa0
ML
3819 * otherwise.
3820 */
3821static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
6b5e971a 3822 u8 *src, u8 *dst,
122edaa0 3823 void *tvlv_value,
6b5e971a 3824 u16 tvlv_value_len)
122edaa0
ML
3825{
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
122edaa0
ML
3836 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,
c018ad3d 3851 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
122edaa0
ML
3852 atomic_read(&orig_node->last_ttvn) + 1);
3853
3854out:
3855 if (orig_node)
5d967310 3856 batadv_orig_node_put(orig_node);
122edaa0
ML
3857 return NET_RX_SUCCESS;
3858}
3859
e1bf0c14
ML
3860/**
3861 * batadv_tt_init - initialise the translation table internals
3862 * @bat_priv: the bat priv with all the soft interface information
3863 *
62fe710f 3864 * Return: 0 on success or negative error number in case of failure.
e1bf0c14
ML
3865 */
3866int batadv_tt_init(struct batadv_priv *bat_priv)
3867{
3868 int ret;
3869
0eb01568
AQ
3870 /* synchronized flags must be remote */
3871 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3872
e1bf0c14
ML
3873 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,
335fbe0f
ML
3882 batadv_tt_tvlv_unicast_handler_v1,
3883 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
e1bf0c14 3884
122edaa0
ML
3885 batadv_tvlv_handler_register(bat_priv, NULL,
3886 batadv_roam_tvlv_unicast_handler_v1,
3887 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3888
e1bf0c14
ML
3889 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}
42cb0bef
AQ
3895
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 *
62fe710f 3902 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
42cb0bef
AQ
3903 * otherwise
3904 */
3905bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
6b5e971a 3906 const u8 *addr, unsigned short vid)
42cb0bef
AQ
3907{
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
5dafd8a6 3917 batadv_tt_global_entry_put(tt);
42cb0bef
AQ
3918
3919 return ret;
3920}
86452f81
SE
3921
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}
This page took 1.169315 seconds and 4 git commands to generate.