]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/batman-adv/translation-table.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / net / batman-adv / translation-table.c
CommitLineData
ac79cbb9 1/* Copyright (C) 2007-2017 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>
d34f0550 40#include <linux/netlink.h>
1e2c2a4f
SE
41#include <linux/rculist.h>
42#include <linux/rcupdate.h>
43#include <linux/seq_file.h>
d34f0550 44#include <linux/skbuff.h>
1e2c2a4f
SE
45#include <linux/slab.h>
46#include <linux/spinlock.h>
47#include <linux/stddef.h>
48#include <linux/string.h>
49#include <linux/workqueue.h>
d34f0550
MS
50#include <net/genetlink.h>
51#include <net/netlink.h>
52#include <net/sock.h>
53#include <uapi/linux/batman_adv.h>
1e2c2a4f
SE
54
55#include "bridge_loop_avoidance.h"
32ae9b22 56#include "hard-interface.h"
c6c8fea2 57#include "hash.h"
ba412080 58#include "log.h"
d34f0550 59#include "netlink.h"
1e2c2a4f
SE
60#include "originator.h"
61#include "packet.h"
62#include "soft-interface.h"
1f8dce49 63#include "tvlv.h"
a73105b8 64
86452f81
SE
65static struct kmem_cache *batadv_tl_cache __read_mostly;
66static struct kmem_cache *batadv_tg_cache __read_mostly;
67static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
68static struct kmem_cache *batadv_tt_change_cache __read_mostly;
69static struct kmem_cache *batadv_tt_req_cache __read_mostly;
70static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
71
dec05074
AQ
72/* hash class keys */
73static struct lock_class_key batadv_tt_local_hash_lock_class_key;
74static struct lock_class_key batadv_tt_global_hash_lock_class_key;
75
6b5e971a 76static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
c018ad3d 77 unsigned short vid,
56303d34 78 struct batadv_orig_node *orig_node);
a513088d
SE
79static void batadv_tt_purge(struct work_struct *work);
80static void
56303d34 81batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
30cfd02b
AQ
82static void batadv_tt_global_del(struct batadv_priv *bat_priv,
83 struct batadv_orig_node *orig_node,
84 const unsigned char *addr,
c018ad3d
AQ
85 unsigned short vid, const char *message,
86 bool roaming);
c6c8fea2 87
62fe710f 88/**
d15cd622
AQ
89 * batadv_compare_tt - check if two TT entries are the same
90 * @node: the list element pointer of the first TT entry
91 * @data2: pointer to the tt_common_entry of the second TT entry
62fe710f 92 *
d15cd622
AQ
93 * Compare the MAC address and the VLAN ID of the two TT entries and check if
94 * they are the same TT client.
4b426b10 95 * Return: true if the two TT clients are the same, false otherwise
62fe710f 96 */
4b426b10 97static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
7aadf889 98{
56303d34 99 const void *data1 = container_of(node, struct batadv_tt_common_entry,
747e4221 100 hash_entry);
4c718958
ML
101 const struct batadv_tt_common_entry *tt1 = data1;
102 const struct batadv_tt_common_entry *tt2 = data2;
7aadf889 103
4c718958 104 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
7aadf889
ML
105}
106
c018ad3d
AQ
107/**
108 * batadv_choose_tt - return the index of the tt entry in the hash table
109 * @data: pointer to the tt_common_entry object to map
110 * @size: the size of the hash table
111 *
62fe710f 112 * Return: the hash index where the object represented by 'data' should be
c018ad3d
AQ
113 * stored at.
114 */
6b5e971a 115static inline u32 batadv_choose_tt(const void *data, u32 size)
c018ad3d
AQ
116{
117 struct batadv_tt_common_entry *tt;
6b5e971a 118 u32 hash = 0;
c018ad3d
AQ
119
120 tt = (struct batadv_tt_common_entry *)data;
36fd61cb
SE
121 hash = jhash(&tt->addr, ETH_ALEN, hash);
122 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
c018ad3d
AQ
123
124 return hash % size;
125}
126
127/**
128 * batadv_tt_hash_find - look for a client in the given hash table
129 * @hash: the hash table to search
130 * @addr: the mac address of the client to look for
131 * @vid: VLAN identifier
132 *
62fe710f 133 * Return: a pointer to the tt_common struct belonging to the searched client if
c018ad3d
AQ
134 * found, NULL otherwise.
135 */
56303d34 136static struct batadv_tt_common_entry *
6b5e971a 137batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
c018ad3d 138 unsigned short vid)
7aadf889 139{
7aadf889 140 struct hlist_head *head;
c018ad3d 141 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
6b5e971a 142 u32 index;
7aadf889
ML
143
144 if (!hash)
145 return NULL;
146
8fdd0153 147 ether_addr_copy(to_search.addr, addr);
c018ad3d
AQ
148 to_search.vid = vid;
149
150 index = batadv_choose_tt(&to_search, hash->size);
7aadf889
ML
151 head = &hash->table[index];
152
153 rcu_read_lock();
c018ad3d
AQ
154 hlist_for_each_entry_rcu(tt, head, hash_entry) {
155 if (!batadv_compare_eth(tt, addr))
156 continue;
157
158 if (tt->vid != vid)
7aadf889
ML
159 continue;
160
92dcdf09 161 if (!kref_get_unless_zero(&tt->refcount))
7683fdc1
AQ
162 continue;
163
c018ad3d 164 tt_tmp = tt;
7aadf889
ML
165 break;
166 }
167 rcu_read_unlock();
168
c018ad3d 169 return tt_tmp;
7aadf889
ML
170}
171
c018ad3d
AQ
172/**
173 * batadv_tt_local_hash_find - search the local table for a given client
174 * @bat_priv: the bat priv with all the soft interface information
175 * @addr: the mac address of the client to look for
176 * @vid: VLAN identifier
177 *
62fe710f 178 * Return: a pointer to the corresponding tt_local_entry struct if the client is
c018ad3d
AQ
179 * found, NULL otherwise.
180 */
56303d34 181static struct batadv_tt_local_entry *
6b5e971a 182batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
c018ad3d 183 unsigned short vid)
7aadf889 184{
56303d34
SE
185 struct batadv_tt_common_entry *tt_common_entry;
186 struct batadv_tt_local_entry *tt_local_entry = NULL;
7aadf889 187
c018ad3d
AQ
188 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
189 vid);
48100bac
AQ
190 if (tt_common_entry)
191 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
192 struct batadv_tt_local_entry,
193 common);
48100bac
AQ
194 return tt_local_entry;
195}
7aadf889 196
c018ad3d
AQ
197/**
198 * batadv_tt_global_hash_find - search the global table for a given client
199 * @bat_priv: the bat priv with all the soft interface information
200 * @addr: the mac address of the client to look for
201 * @vid: VLAN identifier
202 *
62fe710f 203 * Return: a pointer to the corresponding tt_global_entry struct if the client
c018ad3d
AQ
204 * is found, NULL otherwise.
205 */
56303d34 206static struct batadv_tt_global_entry *
6b5e971a 207batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
c018ad3d 208 unsigned short vid)
48100bac 209{
56303d34
SE
210 struct batadv_tt_common_entry *tt_common_entry;
211 struct batadv_tt_global_entry *tt_global_entry = NULL;
7683fdc1 212
c018ad3d
AQ
213 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
214 vid);
48100bac
AQ
215 if (tt_common_entry)
216 tt_global_entry = container_of(tt_common_entry,
56303d34
SE
217 struct batadv_tt_global_entry,
218 common);
48100bac 219 return tt_global_entry;
7aadf889
ML
220}
221
86452f81
SE
222/**
223 * batadv_tt_local_entry_free_rcu - free the tt_local_entry
224 * @rcu: rcu pointer of the tt_local_entry
225 */
226static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
227{
228 struct batadv_tt_local_entry *tt_local_entry;
229
230 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
231 common.rcu);
232
233 kmem_cache_free(batadv_tl_cache, tt_local_entry);
234}
235
92dcdf09
SE
236/**
237 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
238 * for free after rcu grace period
239 * @ref: kref pointer of the nc_node
240 */
241static void batadv_tt_local_entry_release(struct kref *ref)
242{
243 struct batadv_tt_local_entry *tt_local_entry;
244
245 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
246 common.refcount);
247
a33d970d
SE
248 batadv_softif_vlan_put(tt_local_entry->vlan);
249
86452f81 250 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
92dcdf09
SE
251}
252
253/**
95c0db90 254 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
92dcdf09
SE
255 * possibly release it
256 * @tt_local_entry: tt_local_entry to be free'd
257 */
a513088d 258static void
95c0db90 259batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
7683fdc1 260{
92dcdf09
SE
261 kref_put(&tt_local_entry->common.refcount,
262 batadv_tt_local_entry_release);
7683fdc1
AQ
263}
264
86452f81
SE
265/**
266 * batadv_tt_global_entry_free_rcu - free the tt_global_entry
267 * @rcu: rcu pointer of the tt_global_entry
268 */
269static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
270{
271 struct batadv_tt_global_entry *tt_global_entry;
272
273 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
274 common.rcu);
275
276 kmem_cache_free(batadv_tg_cache, tt_global_entry);
277}
278
21026059 279/**
92dcdf09
SE
280 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
281 * for free after rcu grace period
282 * @ref: kref pointer of the nc_node
283 */
284static void batadv_tt_global_entry_release(struct kref *ref)
285{
286 struct batadv_tt_global_entry *tt_global_entry;
287
288 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
289 common.refcount);
290
291 batadv_tt_global_del_orig_list(tt_global_entry);
86452f81
SE
292
293 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
92dcdf09
SE
294}
295
296/**
5dafd8a6
SE
297 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
298 * possibly release it
92dcdf09 299 * @tt_global_entry: tt_global_entry to be free'd
21026059 300 */
a513088d 301static void
5dafd8a6 302batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
7683fdc1 303{
92dcdf09
SE
304 kref_put(&tt_global_entry->common.refcount,
305 batadv_tt_global_entry_release);
db08e6e5
SW
306}
307
1d8ab8d3
LL
308/**
309 * batadv_tt_global_hash_count - count the number of orig entries
d15cd622 310 * @bat_priv: the bat priv with all the soft interface information
1d8ab8d3
LL
311 * @addr: the mac address of the client to count entries for
312 * @vid: VLAN identifier
313 *
62fe710f 314 * Return: the number of originators advertising the given address/data
1d8ab8d3
LL
315 * (excluding ourself).
316 */
317int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
6b5e971a 318 const u8 *addr, unsigned short vid)
1d8ab8d3
LL
319{
320 struct batadv_tt_global_entry *tt_global_entry;
321 int count;
322
323 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
324 if (!tt_global_entry)
325 return 0;
326
327 count = atomic_read(&tt_global_entry->orig_list_count);
5dafd8a6 328 batadv_tt_global_entry_put(tt_global_entry);
1d8ab8d3
LL
329
330 return count;
331}
332
7ea7b4a1
AQ
333/**
334 * batadv_tt_local_size_mod - change the size by v of the local table identified
335 * by vid
336 * @bat_priv: the bat priv with all the soft interface information
337 * @vid: the VLAN identifier of the sub-table to change
338 * @v: the amount to sum to the local table size
339 */
340static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
341 unsigned short vid, int v)
342{
343 struct batadv_softif_vlan *vlan;
344
345 vlan = batadv_softif_vlan_get(bat_priv, vid);
346 if (!vlan)
347 return;
348
349 atomic_add(v, &vlan->tt.num_entries);
350
9c3bf081 351 batadv_softif_vlan_put(vlan);
7ea7b4a1
AQ
352}
353
354/**
355 * batadv_tt_local_size_inc - increase by one the local table size for the given
356 * vid
357 * @bat_priv: the bat priv with all the soft interface information
358 * @vid: the VLAN identifier
359 */
360static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
361 unsigned short vid)
362{
363 batadv_tt_local_size_mod(bat_priv, vid, 1);
364}
365
366/**
367 * batadv_tt_local_size_dec - decrease by one the local table size for the given
368 * vid
369 * @bat_priv: the bat priv with all the soft interface information
370 * @vid: the VLAN identifier
371 */
372static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
373 unsigned short vid)
374{
375 batadv_tt_local_size_mod(bat_priv, vid, -1);
376}
377
378/**
d15cd622
AQ
379 * batadv_tt_global_size_mod - change the size by v of the global table
380 * for orig_node identified by vid
381 * @orig_node: the originator for which the table has to be modified
7ea7b4a1
AQ
382 * @vid: the VLAN identifier
383 * @v: the amount to sum to the global table size
384 */
385static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
386 unsigned short vid, int v)
387{
388 struct batadv_orig_node_vlan *vlan;
389
390 vlan = batadv_orig_node_vlan_new(orig_node, vid);
391 if (!vlan)
392 return;
393
394 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
395 spin_lock_bh(&orig_node->vlan_list_lock);
3db15209
SE
396 if (!hlist_unhashed(&vlan->list)) {
397 hlist_del_init_rcu(&vlan->list);
21754e25 398 batadv_orig_node_vlan_put(vlan);
3db15209 399 }
7ea7b4a1 400 spin_unlock_bh(&orig_node->vlan_list_lock);
7ea7b4a1
AQ
401 }
402
21754e25 403 batadv_orig_node_vlan_put(vlan);
7ea7b4a1
AQ
404}
405
406/**
407 * batadv_tt_global_size_inc - increase by one the global table size for the
408 * given vid
409 * @orig_node: the originator which global table size has to be decreased
410 * @vid: the vlan identifier
411 */
412static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
413 unsigned short vid)
414{
415 batadv_tt_global_size_mod(orig_node, vid, 1);
416}
417
418/**
419 * batadv_tt_global_size_dec - decrease by one the global table size for the
420 * given vid
421 * @orig_node: the originator which global table size has to be decreased
422 * @vid: the vlan identifier
423 */
424static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
425 unsigned short vid)
426{
427 batadv_tt_global_size_mod(orig_node, vid, -1);
428}
429
86452f81
SE
430/**
431 * batadv_tt_orig_list_entry_free_rcu - free the orig_entry
432 * @rcu: rcu pointer of the orig_entry
433 */
434static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
435{
436 struct batadv_tt_orig_list_entry *orig_entry;
437
438 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
439
440 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
441}
442
42eff6a6
SE
443/**
444 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
445 * queue for free after rcu grace period
6e8ef69d 446 * @ref: kref pointer of the tt orig entry
42eff6a6 447 */
6e8ef69d 448static void batadv_tt_orig_list_entry_release(struct kref *ref)
42eff6a6 449{
6e8ef69d
SE
450 struct batadv_tt_orig_list_entry *orig_entry;
451
452 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
453 refcount);
454
5d967310 455 batadv_orig_node_put(orig_entry->orig_node);
86452f81 456 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
42eff6a6
SE
457}
458
6e8ef69d 459/**
7e2366c6
SE
460 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
461 * possibly release it
6e8ef69d
SE
462 * @orig_entry: tt orig entry to be free'd
463 */
a513088d 464static void
7e2366c6 465batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
db08e6e5 466{
6e8ef69d 467 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
7683fdc1
AQ
468}
469
3abe4adb
AQ
470/**
471 * batadv_tt_local_event - store a local TT event (ADD/DEL)
472 * @bat_priv: the bat priv with all the soft interface information
473 * @tt_local_entry: the TT entry involved in the event
474 * @event_flags: flags to store in the event structure
475 */
56303d34 476static void batadv_tt_local_event(struct batadv_priv *bat_priv,
3abe4adb 477 struct batadv_tt_local_entry *tt_local_entry,
6b5e971a 478 u8 event_flags)
a73105b8 479{
56303d34 480 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
3abe4adb 481 struct batadv_tt_common_entry *common = &tt_local_entry->common;
6b5e971a 482 u8 flags = common->flags | event_flags;
3b643de5
AQ
483 bool event_removed = false;
484 bool del_op_requested, del_op_entry;
a73105b8 485
86452f81 486 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
a73105b8
AQ
487 if (!tt_change_node)
488 return;
489
ff66c975 490 tt_change_node->change.flags = flags;
ca663046
AQ
491 memset(tt_change_node->change.reserved, 0,
492 sizeof(tt_change_node->change.reserved));
8fdd0153 493 ether_addr_copy(tt_change_node->change.addr, common->addr);
c018ad3d 494 tt_change_node->change.vid = htons(common->vid);
a73105b8 495
acd34afa 496 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
497
498 /* check for ADD+DEL or DEL+ADD events */
807736f6
SE
499 spin_lock_bh(&bat_priv->tt.changes_list_lock);
500 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
3b643de5 501 list) {
3abe4adb 502 if (!batadv_compare_eth(entry->change.addr, common->addr))
3b643de5
AQ
503 continue;
504
505 /* DEL+ADD in the same orig interval have no effect and can be
506 * removed to avoid silly behaviour on the receiver side. The
507 * other way around (ADD+DEL) can happen in case of roaming of
508 * a client still in the NEW state. Roaming of NEW clients is
509 * now possible due to automatically recognition of "temporary"
510 * clients
511 */
acd34afa 512 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
513 if (!del_op_requested && del_op_entry)
514 goto del;
515 if (del_op_requested && !del_op_entry)
516 goto del;
3c4f7ab6
AQ
517
518 /* this is a second add in the same originator interval. It
519 * means that flags have been changed: update them!
520 */
521 if (!del_op_requested && !del_op_entry)
522 entry->change.flags = flags;
523
3b643de5
AQ
524 continue;
525del:
526 list_del(&entry->list);
86452f81
SE
527 kmem_cache_free(batadv_tt_change_cache, entry);
528 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
3b643de5
AQ
529 event_removed = true;
530 goto unlock;
531 }
532
a73105b8 533 /* track the change in the OGMinterval list */
807736f6 534 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
3b643de5
AQ
535
536unlock:
807736f6 537 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 538
3b643de5 539 if (event_removed)
807736f6 540 atomic_dec(&bat_priv->tt.local_changes);
3b643de5 541 else
807736f6 542 atomic_inc(&bat_priv->tt.local_changes);
a73105b8
AQ
543}
544
335fbe0f
ML
545/**
546 * batadv_tt_len - compute length in bytes of given number of tt changes
547 * @changes_num: number of tt changes
548 *
62fe710f 549 * Return: computed length in bytes.
335fbe0f
ML
550 */
551static int batadv_tt_len(int changes_num)
a73105b8 552{
335fbe0f 553 return changes_num * sizeof(struct batadv_tvlv_tt_change);
a73105b8
AQ
554}
555
298e6e68
AQ
556/**
557 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
558 * @tt_len: available space
559 *
62fe710f 560 * Return: the number of entries.
298e6e68 561 */
6b5e971a 562static u16 batadv_tt_entries(u16 tt_len)
298e6e68
AQ
563{
564 return tt_len / batadv_tt_len(1);
565}
566
a19d3d85
ML
567/**
568 * batadv_tt_local_table_transmit_size - calculates the local translation table
569 * size when transmitted over the air
570 * @bat_priv: the bat priv with all the soft interface information
571 *
62fe710f 572 * Return: local translation table size in bytes.
a19d3d85
ML
573 */
574static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
575{
4f248cff
SE
576 u16 num_vlan = 0;
577 u16 tt_local_entries = 0;
a19d3d85
ML
578 struct batadv_softif_vlan *vlan;
579 int hdr_size;
580
581 rcu_read_lock();
582 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
583 num_vlan++;
584 tt_local_entries += atomic_read(&vlan->tt.num_entries);
585 }
586 rcu_read_unlock();
587
588 /* header size of tvlv encapsulated tt response payload */
589 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
590 hdr_size += sizeof(struct batadv_tvlv_hdr);
591 hdr_size += sizeof(struct batadv_tvlv_tt_data);
592 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
593
594 return hdr_size + batadv_tt_len(tt_local_entries);
595}
596
56303d34 597static int batadv_tt_local_init(struct batadv_priv *bat_priv)
c6c8fea2 598{
807736f6 599 if (bat_priv->tt.local_hash)
5346c35e 600 return 0;
c6c8fea2 601
807736f6 602 bat_priv->tt.local_hash = batadv_hash_new(1024);
c6c8fea2 603
807736f6 604 if (!bat_priv->tt.local_hash)
5346c35e 605 return -ENOMEM;
c6c8fea2 606
dec05074
AQ
607 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
608 &batadv_tt_local_hash_lock_class_key);
609
5346c35e 610 return 0;
c6c8fea2
SE
611}
612
068ee6e2
AQ
613static void batadv_tt_global_free(struct batadv_priv *bat_priv,
614 struct batadv_tt_global_entry *tt_global,
615 const char *message)
616{
617 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
618 "Deleting global tt entry %pM (vid: %d): %s\n",
619 tt_global->common.addr,
f7a2bd65 620 batadv_print_vid(tt_global->common.vid), message);
068ee6e2
AQ
621
622 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
c018ad3d 623 batadv_choose_tt, &tt_global->common);
5dafd8a6 624 batadv_tt_global_entry_put(tt_global);
068ee6e2
AQ
625}
626
c018ad3d
AQ
627/**
628 * batadv_tt_local_add - add a new client to the local table or update an
629 * existing client
630 * @soft_iface: netdev struct of the mesh interface
631 * @addr: the mac address of the client to add
632 * @vid: VLAN identifier
633 * @ifindex: index of the interface where the client is connected to (useful to
634 * identify wireless clients)
9464d071
AQ
635 * @mark: the value contained in the skb->mark field of the received packet (if
636 * any)
a19d3d85 637 *
62fe710f 638 * Return: true if the client was successfully added, false otherwise.
c018ad3d 639 */
6b5e971a
SE
640bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
641 unsigned short vid, int ifindex, u32 mark)
c6c8fea2 642{
56303d34 643 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
170173bf 644 struct batadv_tt_local_entry *tt_local;
c5caf4ef 645 struct batadv_tt_global_entry *tt_global = NULL;
2cd45a06 646 struct net *net = dev_net(soft_iface);
35df3b29 647 struct batadv_softif_vlan *vlan;
0c69aecc 648 struct net_device *in_dev = NULL;
10b1bbb4 649 struct batadv_hard_iface *in_hardif = NULL;
db08e6e5 650 struct hlist_head *head;
56303d34 651 struct batadv_tt_orig_list_entry *orig_entry;
a19d3d85 652 int hash_added, table_size, packet_size_max;
4f248cff
SE
653 bool ret = false;
654 bool roamed_back = false;
6b5e971a
SE
655 u8 remote_flags;
656 u32 match_mark;
c6c8fea2 657
0c69aecc 658 if (ifindex != BATADV_NULL_IFINDEX)
2cd45a06 659 in_dev = dev_get_by_index(net, ifindex);
0c69aecc 660
10b1bbb4
SE
661 if (in_dev)
662 in_hardif = batadv_hardif_get_by_netdev(in_dev);
663
c018ad3d 664 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
c5caf4ef
LL
665
666 if (!is_multicast_ether_addr(addr))
667 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
c6c8fea2 668
47c94655
AQ
669 if (tt_local) {
670 tt_local->last_seen = jiffies;
068ee6e2
AQ
671 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
672 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 673 "Re-adding pending client %pM (vid: %d)\n",
f7a2bd65 674 addr, batadv_print_vid(vid));
068ee6e2
AQ
675 /* whatever the reason why the PENDING flag was set,
676 * this is a client which was enqueued to be removed in
677 * this orig_interval. Since it popped up again, the
678 * flag can be reset like it was never enqueued
679 */
680 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
681 goto add_event;
682 }
683
684 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
685 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 686 "Roaming client %pM (vid: %d) came back to its original location\n",
f7a2bd65 687 addr, batadv_print_vid(vid));
068ee6e2
AQ
688 /* the ROAM flag is set because this client roamed away
689 * and the node got a roaming_advertisement message. Now
690 * that the client popped up again at its original
691 * location such flag can be unset
692 */
693 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
694 roamed_back = true;
695 }
696 goto check_roaming;
c6c8fea2
SE
697 }
698
a19d3d85
ML
699 /* Ignore the client if we cannot send it in a full table response. */
700 table_size = batadv_tt_local_table_transmit_size(bat_priv);
701 table_size += batadv_tt_len(1);
702 packet_size_max = atomic_read(&bat_priv->packet_size_max);
703 if (table_size > packet_size_max) {
704 net_ratelimited_function(batadv_info, soft_iface,
705 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
706 table_size, packet_size_max, addr);
707 goto out;
708 }
709
86452f81 710 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
47c94655 711 if (!tt_local)
7683fdc1 712 goto out;
a73105b8 713
35df3b29
AQ
714 /* increase the refcounter of the related vlan */
715 vlan = batadv_softif_vlan_get(bat_priv, vid);
0b3dd7df
SW
716 if (!vlan) {
717 net_ratelimited_function(batadv_info, soft_iface,
718 "adding TT local entry %pM to non-existent VLAN %d\n",
f7a2bd65 719 addr, batadv_print_vid(vid));
86452f81 720 kmem_cache_free(batadv_tl_cache, tt_local);
fd7dec25 721 tt_local = NULL;
354136bc 722 goto out;
fd7dec25 723 }
35df3b29 724
39c75a51 725 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 726 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
f7a2bd65 727 addr, batadv_print_vid(vid),
6b5e971a 728 (u8)atomic_read(&bat_priv->tt.vn));
c6c8fea2 729
8fdd0153 730 ether_addr_copy(tt_local->common.addr, addr);
8425ec6a
AQ
731 /* The local entry has to be marked as NEW to avoid to send it in
732 * a full table response going out before the next ttvn increment
733 * (consistency check)
734 */
735 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
c018ad3d 736 tt_local->common.vid = vid;
10b1bbb4 737 if (batadv_is_wifi_hardif(in_hardif))
47c94655 738 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
92dcdf09 739 kref_init(&tt_local->common.refcount);
47c94655
AQ
740 tt_local->last_seen = jiffies;
741 tt_local->common.added_at = tt_local->last_seen;
a33d970d 742 tt_local->vlan = vlan;
c6c8fea2 743
c5caf4ef
LL
744 /* the batman interface mac and multicast addresses should never be
745 * purged
746 */
747 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
748 is_multicast_ether_addr(addr))
47c94655 749 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 750
e3387b26 751 kref_get(&tt_local->common.refcount);
807736f6 752 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
c018ad3d 753 batadv_choose_tt, &tt_local->common,
47c94655 754 &tt_local->common.hash_entry);
80b3f58c
SW
755
756 if (unlikely(hash_added != 0)) {
757 /* remove the reference for the hash */
95c0db90 758 batadv_tt_local_entry_put(tt_local);
80b3f58c
SW
759 goto out;
760 }
761
068ee6e2 762add_event:
3abe4adb 763 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
ff66c975 764
068ee6e2
AQ
765check_roaming:
766 /* Check whether it is a roaming, but don't do anything if the roaming
767 * process has already been handled
768 */
769 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
db08e6e5 770 /* These node are probably going to update their tt table */
47c94655 771 head = &tt_global->orig_list;
db08e6e5 772 rcu_read_lock();
b67bfe0d 773 hlist_for_each_entry_rcu(orig_entry, head, list) {
47c94655 774 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
c018ad3d 775 tt_global->common.vid,
a513088d 776 orig_entry->orig_node);
db08e6e5
SW
777 }
778 rcu_read_unlock();
068ee6e2
AQ
779 if (roamed_back) {
780 batadv_tt_global_free(bat_priv, tt_global,
781 "Roaming canceled");
782 tt_global = NULL;
783 } else {
784 /* The global entry has to be marked as ROAMING and
785 * has to be kept for consistency purpose
786 */
787 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
788 tt_global->roam_at = jiffies;
789 }
7683fdc1 790 }
068ee6e2 791
3c4f7ab6
AQ
792 /* store the current remote flags before altering them. This helps
793 * understanding is flags are changing or not
794 */
795 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
796
10b1bbb4 797 if (batadv_is_wifi_hardif(in_hardif))
3c4f7ab6
AQ
798 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
799 else
800 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
a19d3d85 801
9464d071
AQ
802 /* check the mark in the skb: if it's equal to the configured
803 * isolation_mark, it means the packet is coming from an isolated
804 * non-mesh client
805 */
806 match_mark = (mark & bat_priv->isolation_mark_mask);
807 if (bat_priv->isolation_mark_mask &&
808 match_mark == bat_priv->isolation_mark)
809 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
810 else
811 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
812
3c4f7ab6
AQ
813 /* if any "dynamic" flag has been modified, resend an ADD event for this
814 * entry so that all the nodes can get the new flags
815 */
816 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
817 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
818
819 ret = true;
7683fdc1 820out:
10b1bbb4
SE
821 if (in_hardif)
822 batadv_hardif_put(in_hardif);
0c69aecc
AQ
823 if (in_dev)
824 dev_put(in_dev);
47c94655 825 if (tt_local)
95c0db90 826 batadv_tt_local_entry_put(tt_local);
47c94655 827 if (tt_global)
5dafd8a6 828 batadv_tt_global_entry_put(tt_global);
a19d3d85 829 return ret;
c6c8fea2
SE
830}
831
7ea7b4a1
AQ
832/**
833 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
834 * within a TT Response directed to another node
835 * @orig_node: originator for which the TT data has to be prepared
836 * @tt_data: uninitialised pointer to the address of the TVLV buffer
837 * @tt_change: uninitialised pointer to the address of the area where the TT
838 * changed can be stored
839 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
840 * function reserves the amount of space needed to send the entire global TT
841 * table. In case of success the value is updated with the real amount of
842 * reserved bytes
7ea7b4a1
AQ
843 * Allocate the needed amount of memory for the entire TT TVLV and write its
844 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
845 * objects, one per active VLAN served by the originator node.
846 *
62fe710f 847 * Return: the size of the allocated buffer or 0 in case of failure.
7ea7b4a1 848 */
6b5e971a 849static u16
7ea7b4a1
AQ
850batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
851 struct batadv_tvlv_tt_data **tt_data,
852 struct batadv_tvlv_tt_change **tt_change,
6b5e971a 853 s32 *tt_len)
7ea7b4a1 854{
4f248cff
SE
855 u16 num_vlan = 0;
856 u16 num_entries = 0;
857 u16 change_offset;
858 u16 tvlv_len;
7ea7b4a1
AQ
859 struct batadv_tvlv_tt_vlan_data *tt_vlan;
860 struct batadv_orig_node_vlan *vlan;
6b5e971a 861 u8 *tt_change_ptr;
7ea7b4a1
AQ
862
863 rcu_read_lock();
d0fa4f3f 864 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
865 num_vlan++;
866 num_entries += atomic_read(&vlan->tt.num_entries);
867 }
868
869 change_offset = sizeof(**tt_data);
870 change_offset += num_vlan * sizeof(*tt_vlan);
871
872 /* if tt_len is negative, allocate the space needed by the full table */
873 if (*tt_len < 0)
874 *tt_len = batadv_tt_len(num_entries);
875
876 tvlv_len = *tt_len;
877 tvlv_len += change_offset;
878
879 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
880 if (!*tt_data) {
881 *tt_len = 0;
882 goto out;
883 }
884
885 (*tt_data)->flags = BATADV_NO_FLAGS;
886 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
887 (*tt_data)->num_vlan = htons(num_vlan);
888
889 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
d0fa4f3f 890 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
891 tt_vlan->vid = htons(vlan->vid);
892 tt_vlan->crc = htonl(vlan->tt.crc);
893
894 tt_vlan++;
895 }
896
6b5e971a 897 tt_change_ptr = (u8 *)*tt_data + change_offset;
7ea7b4a1
AQ
898 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
899
900out:
901 rcu_read_unlock();
902 return tvlv_len;
903}
904
905/**
906 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
907 * node
908 * @bat_priv: the bat priv with all the soft interface information
909 * @tt_data: uninitialised pointer to the address of the TVLV buffer
910 * @tt_change: uninitialised pointer to the address of the area where the TT
911 * changes can be stored
912 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
913 * function reserves the amount of space needed to send the entire local TT
914 * table. In case of success the value is updated with the real amount of
915 * reserved bytes
916 *
917 * Allocate the needed amount of memory for the entire TT TVLV and write its
918 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
919 * objects, one per active VLAN.
920 *
62fe710f 921 * Return: the size of the allocated buffer or 0 in case of failure.
7ea7b4a1 922 */
6b5e971a 923static u16
7ea7b4a1
AQ
924batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
925 struct batadv_tvlv_tt_data **tt_data,
926 struct batadv_tvlv_tt_change **tt_change,
6b5e971a 927 s32 *tt_len)
7ea7b4a1
AQ
928{
929 struct batadv_tvlv_tt_vlan_data *tt_vlan;
930 struct batadv_softif_vlan *vlan;
4f248cff
SE
931 u16 num_vlan = 0;
932 u16 num_entries = 0;
933 u16 tvlv_len;
6b5e971a 934 u8 *tt_change_ptr;
7ea7b4a1
AQ
935 int change_offset;
936
937 rcu_read_lock();
938 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
939 num_vlan++;
940 num_entries += atomic_read(&vlan->tt.num_entries);
941 }
942
943 change_offset = sizeof(**tt_data);
944 change_offset += num_vlan * sizeof(*tt_vlan);
945
946 /* if tt_len is negative, allocate the space needed by the full table */
947 if (*tt_len < 0)
948 *tt_len = batadv_tt_len(num_entries);
949
950 tvlv_len = *tt_len;
951 tvlv_len += change_offset;
952
953 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
954 if (!*tt_data) {
955 tvlv_len = 0;
956 goto out;
957 }
958
959 (*tt_data)->flags = BATADV_NO_FLAGS;
960 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
961 (*tt_data)->num_vlan = htons(num_vlan);
962
963 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
964 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
965 tt_vlan->vid = htons(vlan->vid);
966 tt_vlan->crc = htonl(vlan->tt.crc);
967
968 tt_vlan++;
969 }
970
6b5e971a 971 tt_change_ptr = (u8 *)*tt_data + change_offset;
7ea7b4a1
AQ
972 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
973
974out:
975 rcu_read_unlock();
976 return tvlv_len;
977}
978
e1bf0c14
ML
979/**
980 * batadv_tt_tvlv_container_update - update the translation table tvlv container
981 * after local tt changes have been committed
982 * @bat_priv: the bat priv with all the soft interface information
983 */
984static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
be9aa4c1 985{
e1bf0c14
ML
986 struct batadv_tt_change_node *entry, *safe;
987 struct batadv_tvlv_tt_data *tt_data;
988 struct batadv_tvlv_tt_change *tt_change;
7ea7b4a1 989 int tt_diff_len, tt_change_len = 0;
4f248cff
SE
990 int tt_diff_entries_num = 0;
991 int tt_diff_entries_count = 0;
6b5e971a 992 u16 tvlv_len;
be9aa4c1 993
7ea7b4a1
AQ
994 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
995 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
be9aa4c1
ML
996
997 /* if we have too many changes for one packet don't send any
998 * and wait for the tt table request which will be fragmented
999 */
e1bf0c14
ML
1000 if (tt_diff_len > bat_priv->soft_iface->mtu)
1001 tt_diff_len = 0;
be9aa4c1 1002
7ea7b4a1
AQ
1003 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1004 &tt_change, &tt_diff_len);
1005 if (!tvlv_len)
e1bf0c14 1006 return;
be9aa4c1 1007
e1bf0c14 1008 tt_data->flags = BATADV_TT_OGM_DIFF;
c6c8fea2 1009
e1bf0c14
ML
1010 if (tt_diff_len == 0)
1011 goto container_register;
be9aa4c1 1012
807736f6
SE
1013 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1014 atomic_set(&bat_priv->tt.local_changes, 0);
c6c8fea2 1015
807736f6 1016 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
7c64fd98 1017 list) {
e1bf0c14
ML
1018 if (tt_diff_entries_count < tt_diff_entries_num) {
1019 memcpy(tt_change + tt_diff_entries_count,
1020 &entry->change,
1021 sizeof(struct batadv_tvlv_tt_change));
1022 tt_diff_entries_count++;
c6c8fea2 1023 }
a73105b8 1024 list_del(&entry->list);
86452f81 1025 kmem_cache_free(batadv_tt_change_cache, entry);
c6c8fea2 1026 }
807736f6 1027 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8
AQ
1028
1029 /* Keep the buffer for possible tt_request */
807736f6
SE
1030 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1031 kfree(bat_priv->tt.last_changeset);
1032 bat_priv->tt.last_changeset_len = 0;
1033 bat_priv->tt.last_changeset = NULL;
e1bf0c14 1034 tt_change_len = batadv_tt_len(tt_diff_entries_count);
be9aa4c1 1035 /* check whether this new OGM has no changes due to size problems */
e1bf0c14 1036 if (tt_diff_entries_count > 0) {
be9aa4c1 1037 /* if kmalloc() fails we will reply with the full table
a73105b8
AQ
1038 * instead of providing the diff
1039 */
e1bf0c14 1040 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
807736f6 1041 if (bat_priv->tt.last_changeset) {
e1bf0c14
ML
1042 memcpy(bat_priv->tt.last_changeset,
1043 tt_change, tt_change_len);
1044 bat_priv->tt.last_changeset_len = tt_diff_len;
a73105b8
AQ
1045 }
1046 }
807736f6 1047 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
c6c8fea2 1048
e1bf0c14
ML
1049container_register:
1050 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
7ea7b4a1 1051 tvlv_len);
e1bf0c14 1052 kfree(tt_data);
c6c8fea2
SE
1053}
1054
dc1cbd14 1055#ifdef CONFIG_BATMAN_ADV_DEBUGFS
08c36d3e 1056int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1057{
1058 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1059 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1060 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 1061 struct batadv_tt_common_entry *tt_common_entry;
85766a82 1062 struct batadv_tt_local_entry *tt_local;
56303d34 1063 struct batadv_hard_iface *primary_if;
c6c8fea2 1064 struct hlist_head *head;
6b5e971a 1065 u32 i;
85766a82
AQ
1066 int last_seen_secs;
1067 int last_seen_msecs;
1068 unsigned long last_seen_jiffies;
1069 bool no_purge;
6b5e971a 1070 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 1071
30da63a6
ML
1072 primary_if = batadv_seq_print_text_primary_if_get(seq);
1073 if (!primary_if)
32ae9b22 1074 goto out;
c6c8fea2 1075
86ceb360 1076 seq_printf(seq,
7ea7b4a1 1077 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
6b5e971a 1078 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
925a6f37
AQ
1079 seq_puts(seq,
1080 " Client VID Flags Last seen (CRC )\n");
c6c8fea2 1081
c6c8fea2
SE
1082 for (i = 0; i < hash->size; i++) {
1083 head = &hash->table[i];
1084
7aadf889 1085 rcu_read_lock();
b67bfe0d 1086 hlist_for_each_entry_rcu(tt_common_entry,
7aadf889 1087 head, hash_entry) {
85766a82
AQ
1088 tt_local = container_of(tt_common_entry,
1089 struct batadv_tt_local_entry,
1090 common);
1091 last_seen_jiffies = jiffies - tt_local->last_seen;
1092 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1093 last_seen_secs = last_seen_msecs / 1000;
1094 last_seen_msecs = last_seen_msecs % 1000;
1095
1096 no_purge = tt_common_entry->flags & np_flag;
7ea7b4a1 1097 seq_printf(seq,
dd24ddb2 1098 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
7c64fd98 1099 tt_common_entry->addr,
f7a2bd65 1100 batadv_print_vid(tt_common_entry->vid),
a2f2b6cd
SE
1101 ((tt_common_entry->flags &
1102 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
85766a82 1103 no_purge ? 'P' : '.',
a2f2b6cd
SE
1104 ((tt_common_entry->flags &
1105 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1106 ((tt_common_entry->flags &
1107 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1108 ((tt_common_entry->flags &
1109 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1110 ((tt_common_entry->flags &
1111 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
a7966d90 1112 no_purge ? 0 : last_seen_secs,
7ea7b4a1 1113 no_purge ? 0 : last_seen_msecs,
a33d970d 1114 tt_local->vlan->tt.crc);
c6c8fea2 1115 }
7aadf889 1116 rcu_read_unlock();
c6c8fea2 1117 }
32ae9b22
ML
1118out:
1119 if (primary_if)
82047ad7 1120 batadv_hardif_put(primary_if);
30da63a6 1121 return 0;
c6c8fea2 1122}
dc1cbd14 1123#endif
c6c8fea2 1124
d34f0550
MS
1125/**
1126 * batadv_tt_local_dump_entry - Dump one TT local entry into a message
1127 * @msg :Netlink message to dump into
1128 * @portid: Port making netlink request
1129 * @seq: Sequence number of netlink message
1130 * @bat_priv: The bat priv with all the soft interface information
1131 * @common: tt local & tt global common data
1132 *
1133 * Return: Error code, or 0 on success
1134 */
1135static int
1136batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1137 struct batadv_priv *bat_priv,
1138 struct batadv_tt_common_entry *common)
1139{
1140 void *hdr;
1141 struct batadv_softif_vlan *vlan;
1142 struct batadv_tt_local_entry *local;
1143 unsigned int last_seen_msecs;
1144 u32 crc;
1145
1146 local = container_of(common, struct batadv_tt_local_entry, common);
1147 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1148
1149 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1150 if (!vlan)
1151 return 0;
1152
1153 crc = vlan->tt.crc;
1154
1155 batadv_softif_vlan_put(vlan);
1156
1157 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1158 NLM_F_MULTI,
1159 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1160 if (!hdr)
1161 return -ENOBUFS;
1162
1163 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1164 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1165 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1166 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1167 goto nla_put_failure;
1168
1169 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1170 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1171 goto nla_put_failure;
1172
1173 genlmsg_end(msg, hdr);
1174 return 0;
1175
1176 nla_put_failure:
1177 genlmsg_cancel(msg, hdr);
1178 return -EMSGSIZE;
1179}
1180
1181/**
1182 * batadv_tt_local_dump_bucket - Dump one TT local bucket into a message
1183 * @msg: Netlink message to dump into
1184 * @portid: Port making netlink request
1185 * @seq: Sequence number of netlink message
1186 * @bat_priv: The bat priv with all the soft interface information
1187 * @head: Pointer to the list containing the local tt entries
1188 * @idx_s: Number of entries to skip
1189 *
1190 * Return: Error code, or 0 on success
1191 */
1192static int
1193batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1194 struct batadv_priv *bat_priv,
1195 struct hlist_head *head, int *idx_s)
1196{
1197 struct batadv_tt_common_entry *common;
1198 int idx = 0;
1199
1200 rcu_read_lock();
1201 hlist_for_each_entry_rcu(common, head, hash_entry) {
1202 if (idx++ < *idx_s)
1203 continue;
1204
1205 if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
1206 common)) {
1207 rcu_read_unlock();
1208 *idx_s = idx - 1;
1209 return -EMSGSIZE;
1210 }
1211 }
1212 rcu_read_unlock();
1213
1214 *idx_s = 0;
1215 return 0;
1216}
1217
1218/**
1219 * batadv_tt_local_dump - Dump TT local entries into a message
1220 * @msg: Netlink message to dump into
1221 * @cb: Parameters from query
1222 *
1223 * Return: Error code, or 0 on success
1224 */
1225int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1226{
1227 struct net *net = sock_net(cb->skb->sk);
1228 struct net_device *soft_iface;
1229 struct batadv_priv *bat_priv;
1230 struct batadv_hard_iface *primary_if = NULL;
1231 struct batadv_hashtable *hash;
1232 struct hlist_head *head;
1233 int ret;
1234 int ifindex;
1235 int bucket = cb->args[0];
1236 int idx = cb->args[1];
1237 int portid = NETLINK_CB(cb->skb).portid;
1238
1239 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1240 if (!ifindex)
1241 return -EINVAL;
1242
1243 soft_iface = dev_get_by_index(net, ifindex);
1244 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1245 ret = -ENODEV;
1246 goto out;
1247 }
1248
1249 bat_priv = netdev_priv(soft_iface);
1250
1251 primary_if = batadv_primary_if_get_selected(bat_priv);
1252 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1253 ret = -ENOENT;
1254 goto out;
1255 }
1256
1257 hash = bat_priv->tt.local_hash;
1258
1259 while (bucket < hash->size) {
1260 head = &hash->table[bucket];
1261
1262 if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
1263 bat_priv, head, &idx))
1264 break;
1265
1266 bucket++;
1267 }
1268
1269 ret = msg->len;
1270
1271 out:
1272 if (primary_if)
1273 batadv_hardif_put(primary_if);
1274 if (soft_iface)
1275 dev_put(soft_iface);
1276
1277 cb->args[0] = bucket;
1278 cb->args[1] = idx;
1279
1280 return ret;
1281}
1282
56303d34
SE
1283static void
1284batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1285 struct batadv_tt_local_entry *tt_local_entry,
6b5e971a 1286 u16 flags, const char *message)
c6c8fea2 1287{
3abe4adb 1288 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
a73105b8 1289
015758d0
AQ
1290 /* The local client has to be marked as "pending to be removed" but has
1291 * to be kept in the table in order to send it in a full table
9cfc7bd6
SE
1292 * response issued before the net ttvn increment (consistency check)
1293 */
acd34afa 1294 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
c566dbbe 1295
39c75a51 1296 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1297 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1298 tt_local_entry->common.addr,
f7a2bd65 1299 batadv_print_vid(tt_local_entry->common.vid), message);
c6c8fea2
SE
1300}
1301
7f91d06c
AQ
1302/**
1303 * batadv_tt_local_remove - logically remove an entry from the local table
1304 * @bat_priv: the bat priv with all the soft interface information
1305 * @addr: the MAC address of the client to remove
c018ad3d 1306 * @vid: VLAN identifier
7f91d06c
AQ
1307 * @message: message to append to the log on deletion
1308 * @roaming: true if the deletion is due to a roaming event
1309 *
62fe710f 1310 * Return: the flags assigned to the local entry before being deleted
7f91d06c 1311 */
6b5e971a
SE
1312u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1313 unsigned short vid, const char *message,
1314 bool roaming)
c6c8fea2 1315{
170173bf 1316 struct batadv_tt_local_entry *tt_local_entry;
6b5e971a 1317 u16 flags, curr_flags = BATADV_NO_FLAGS;
ef72706a 1318 void *tt_entry_exists;
c6c8fea2 1319
c018ad3d 1320 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7683fdc1
AQ
1321 if (!tt_local_entry)
1322 goto out;
1323
7f91d06c
AQ
1324 curr_flags = tt_local_entry->common.flags;
1325
acd34afa 1326 flags = BATADV_TT_CLIENT_DEL;
068ee6e2
AQ
1327 /* if this global entry addition is due to a roaming, the node has to
1328 * mark the local entry as "roamed" in order to correctly reroute
1329 * packets later
1330 */
7c1fd91d 1331 if (roaming) {
acd34afa 1332 flags |= BATADV_TT_CLIENT_ROAM;
7c1fd91d
AQ
1333 /* mark the local client as ROAMed */
1334 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1335 }
42d0b044 1336
068ee6e2
AQ
1337 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1338 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1339 message);
1340 goto out;
1341 }
1342 /* if this client has been added right now, it is possible to
1343 * immediately purge it
1344 */
3abe4adb 1345 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
ef72706a
ML
1346
1347 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1348 batadv_compare_tt,
1349 batadv_choose_tt,
1350 &tt_local_entry->common);
1351 if (!tt_entry_exists)
1352 goto out;
1353
1354 /* extra call to free the local tt entry */
95c0db90 1355 batadv_tt_local_entry_put(tt_local_entry);
7f91d06c 1356
7683fdc1
AQ
1357out:
1358 if (tt_local_entry)
95c0db90 1359 batadv_tt_local_entry_put(tt_local_entry);
7f91d06c
AQ
1360
1361 return curr_flags;
c6c8fea2
SE
1362}
1363
a19d3d85
ML
1364/**
1365 * batadv_tt_local_purge_list - purge inactive tt local entries
1366 * @bat_priv: the bat priv with all the soft interface information
1367 * @head: pointer to the list containing the local tt entries
1368 * @timeout: parameter deciding whether a given tt local entry is considered
1369 * inactive or not
1370 */
56303d34 1371static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
a19d3d85
ML
1372 struct hlist_head *head,
1373 int timeout)
c6c8fea2 1374{
56303d34
SE
1375 struct batadv_tt_local_entry *tt_local_entry;
1376 struct batadv_tt_common_entry *tt_common_entry;
b67bfe0d 1377 struct hlist_node *node_tmp;
acd34afa 1378
b67bfe0d 1379 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
acd34afa
SE
1380 hash_entry) {
1381 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
1382 struct batadv_tt_local_entry,
1383 common);
acd34afa
SE
1384 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1385 continue;
1386
1387 /* entry already marked for deletion */
1388 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1389 continue;
1390
a19d3d85 1391 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
acd34afa
SE
1392 continue;
1393
1394 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1395 BATADV_TT_CLIENT_DEL, "timed out");
1396 }
1397}
1398
a19d3d85
ML
1399/**
1400 * batadv_tt_local_purge - purge inactive tt local entries
1401 * @bat_priv: the bat priv with all the soft interface information
1402 * @timeout: parameter deciding whether a given tt local entry is considered
1403 * inactive or not
1404 */
1405static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1406 int timeout)
acd34afa 1407{
807736f6 1408 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
c6c8fea2 1409 struct hlist_head *head;
7683fdc1 1410 spinlock_t *list_lock; /* protects write access to the hash lists */
6b5e971a 1411 u32 i;
c6c8fea2 1412
c6c8fea2
SE
1413 for (i = 0; i < hash->size; i++) {
1414 head = &hash->table[i];
7683fdc1 1415 list_lock = &hash->list_locks[i];
c6c8fea2 1416
7683fdc1 1417 spin_lock_bh(list_lock);
a19d3d85 1418 batadv_tt_local_purge_list(bat_priv, head, timeout);
7683fdc1 1419 spin_unlock_bh(list_lock);
c6c8fea2 1420 }
c6c8fea2
SE
1421}
1422
56303d34 1423static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
c6c8fea2 1424{
5bf74e9c 1425 struct batadv_hashtable *hash;
a73105b8 1426 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
1427 struct batadv_tt_common_entry *tt_common_entry;
1428 struct batadv_tt_local_entry *tt_local;
b67bfe0d 1429 struct hlist_node *node_tmp;
7683fdc1 1430 struct hlist_head *head;
6b5e971a 1431 u32 i;
a73105b8 1432
807736f6 1433 if (!bat_priv->tt.local_hash)
c6c8fea2
SE
1434 return;
1435
807736f6 1436 hash = bat_priv->tt.local_hash;
a73105b8
AQ
1437
1438 for (i = 0; i < hash->size; i++) {
1439 head = &hash->table[i];
1440 list_lock = &hash->list_locks[i];
1441
1442 spin_lock_bh(list_lock);
b67bfe0d 1443 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
a73105b8 1444 head, hash_entry) {
b67bfe0d 1445 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34
SE
1446 tt_local = container_of(tt_common_entry,
1447 struct batadv_tt_local_entry,
1448 common);
35df3b29 1449
95c0db90 1450 batadv_tt_local_entry_put(tt_local);
a73105b8
AQ
1451 }
1452 spin_unlock_bh(list_lock);
1453 }
1454
1a8eaf07 1455 batadv_hash_destroy(hash);
a73105b8 1456
807736f6 1457 bat_priv->tt.local_hash = NULL;
c6c8fea2
SE
1458}
1459
56303d34 1460static int batadv_tt_global_init(struct batadv_priv *bat_priv)
c6c8fea2 1461{
807736f6 1462 if (bat_priv->tt.global_hash)
5346c35e 1463 return 0;
c6c8fea2 1464
807736f6 1465 bat_priv->tt.global_hash = batadv_hash_new(1024);
c6c8fea2 1466
807736f6 1467 if (!bat_priv->tt.global_hash)
5346c35e 1468 return -ENOMEM;
c6c8fea2 1469
dec05074
AQ
1470 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1471 &batadv_tt_global_hash_lock_class_key);
1472
5346c35e 1473 return 0;
c6c8fea2
SE
1474}
1475
56303d34 1476static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
c6c8fea2 1477{
56303d34 1478 struct batadv_tt_change_node *entry, *safe;
c6c8fea2 1479
807736f6 1480 spin_lock_bh(&bat_priv->tt.changes_list_lock);
c6c8fea2 1481
807736f6 1482 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
a73105b8
AQ
1483 list) {
1484 list_del(&entry->list);
86452f81 1485 kmem_cache_free(batadv_tt_change_cache, entry);
a73105b8 1486 }
c6c8fea2 1487
807736f6
SE
1488 atomic_set(&bat_priv->tt.local_changes, 0);
1489 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 1490}
c6c8fea2 1491
62fe710f 1492/**
d15cd622
AQ
1493 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1494 * @entry: the TT global entry where the orig_list_entry has to be
1495 * extracted from
1496 * @orig_node: the originator for which the orig_list_entry has to be found
62fe710f 1497 *
d15cd622 1498 * retrieve the orig_tt_list_entry belonging to orig_node from the
d657e621
AQ
1499 * batadv_tt_global_entry list
1500 *
62fe710f 1501 * Return: it with an increased refcounter, NULL if not found
db08e6e5 1502 */
d657e621
AQ
1503static struct batadv_tt_orig_list_entry *
1504batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1505 const struct batadv_orig_node *orig_node)
db08e6e5 1506{
d657e621 1507 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
db08e6e5 1508 const struct hlist_head *head;
db08e6e5
SW
1509
1510 rcu_read_lock();
1511 head = &entry->orig_list;
b67bfe0d 1512 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
d657e621
AQ
1513 if (tmp_orig_entry->orig_node != orig_node)
1514 continue;
6e8ef69d 1515 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
d657e621
AQ
1516 continue;
1517
1518 orig_entry = tmp_orig_entry;
1519 break;
db08e6e5
SW
1520 }
1521 rcu_read_unlock();
d657e621
AQ
1522
1523 return orig_entry;
1524}
1525
62fe710f 1526/**
d15cd622
AQ
1527 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1528 * by a given originator
1529 * @entry: the TT global entry to check
1530 * @orig_node: the originator to search in the list
62fe710f
SE
1531 *
1532 * find out if an orig_node is already in the list of a tt_global_entry.
1533 *
1534 * Return: true if found, false otherwise
d657e621
AQ
1535 */
1536static bool
1537batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1538 const struct batadv_orig_node *orig_node)
1539{
1540 struct batadv_tt_orig_list_entry *orig_entry;
1541 bool found = false;
1542
1543 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1544 if (orig_entry) {
1545 found = true;
7e2366c6 1546 batadv_tt_orig_list_entry_put(orig_entry);
d657e621
AQ
1547 }
1548
db08e6e5
SW
1549 return found;
1550}
1551
54e22f26
LL
1552/**
1553 * batadv_tt_global_sync_flags - update TT sync flags
1554 * @tt_global: the TT global entry to update sync flags in
1555 *
1556 * Updates the sync flag bits in the tt_global flag attribute with a logical
1557 * OR of all sync flags from any of its TT orig entries.
1558 */
1559static void
1560batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1561{
1562 struct batadv_tt_orig_list_entry *orig_entry;
1563 const struct hlist_head *head;
1564 u16 flags = BATADV_NO_FLAGS;
1565
1566 rcu_read_lock();
1567 head = &tt_global->orig_list;
1568 hlist_for_each_entry_rcu(orig_entry, head, list)
1569 flags |= orig_entry->flags;
1570 rcu_read_unlock();
1571
1572 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1573 tt_global->common.flags = flags;
1574}
1575
1576/**
1577 * batadv_tt_global_orig_entry_add - add or update a TT orig entry
1578 * @tt_global: the TT global entry to add an orig entry in
1579 * @orig_node: the originator to add an orig entry for
1580 * @ttvn: translation table version number of this changeset
1581 * @flags: TT sync flags
1582 */
a513088d 1583static void
d657e621 1584batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
54e22f26
LL
1585 struct batadv_orig_node *orig_node, int ttvn,
1586 u8 flags)
db08e6e5 1587{
56303d34 1588 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5 1589
d657e621 1590 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
30cfd02b
AQ
1591 if (orig_entry) {
1592 /* refresh the ttvn: the current value could be a bogus one that
1593 * was added during a "temporary client detection"
1594 */
1595 orig_entry->ttvn = ttvn;
54e22f26
LL
1596 orig_entry->flags = flags;
1597 goto sync_flags;
30cfd02b 1598 }
d657e621 1599
86452f81 1600 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
db08e6e5 1601 if (!orig_entry)
d657e621 1602 goto out;
db08e6e5
SW
1603
1604 INIT_HLIST_NODE(&orig_entry->list);
7c124391 1605 kref_get(&orig_node->refcount);
7ea7b4a1 1606 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
db08e6e5
SW
1607 orig_entry->orig_node = orig_node;
1608 orig_entry->ttvn = ttvn;
54e22f26 1609 orig_entry->flags = flags;
6e8ef69d 1610 kref_init(&orig_entry->refcount);
db08e6e5 1611
d657e621 1612 spin_lock_bh(&tt_global->list_lock);
23f55485 1613 kref_get(&orig_entry->refcount);
db08e6e5 1614 hlist_add_head_rcu(&orig_entry->list,
d657e621
AQ
1615 &tt_global->orig_list);
1616 spin_unlock_bh(&tt_global->list_lock);
1d8ab8d3
LL
1617 atomic_inc(&tt_global->orig_list_count);
1618
54e22f26
LL
1619sync_flags:
1620 batadv_tt_global_sync_flags(tt_global);
d657e621
AQ
1621out:
1622 if (orig_entry)
7e2366c6 1623 batadv_tt_orig_list_entry_put(orig_entry);
db08e6e5
SW
1624}
1625
d4ff40f6
AQ
1626/**
1627 * batadv_tt_global_add - add a new TT global entry or update an existing one
1628 * @bat_priv: the bat priv with all the soft interface information
1629 * @orig_node: the originator announcing the client
1630 * @tt_addr: the mac address of the non-mesh client
c018ad3d 1631 * @vid: VLAN identifier
d4ff40f6
AQ
1632 * @flags: TT flags that have to be set for this non-mesh client
1633 * @ttvn: the tt version number ever announcing this non-mesh client
1634 *
1635 * Add a new TT global entry for the given originator. If the entry already
1636 * exists add a new reference to the given originator (a global entry can have
1637 * references to multiple originators) and adjust the flags attribute to reflect
1638 * the function argument.
1639 * If a TT local entry exists for this non-mesh client remove it.
1640 *
1641 * The caller must hold orig_node refcount.
1e5d49fc 1642 *
62fe710f 1643 * Return: true if the new entry has been added, false otherwise
d4ff40f6 1644 */
1e5d49fc
AQ
1645static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1646 struct batadv_orig_node *orig_node,
c018ad3d 1647 const unsigned char *tt_addr,
6b5e971a 1648 unsigned short vid, u16 flags, u8 ttvn)
a73105b8 1649{
170173bf
SE
1650 struct batadv_tt_global_entry *tt_global_entry;
1651 struct batadv_tt_local_entry *tt_local_entry;
1e5d49fc 1652 bool ret = false;
80b3f58c 1653 int hash_added;
56303d34 1654 struct batadv_tt_common_entry *common;
6b5e971a 1655 u16 local_flags;
c6c8fea2 1656
cfd4f757
AQ
1657 /* ignore global entries from backbone nodes */
1658 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1659 return true;
1660
c018ad3d
AQ
1661 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1662 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
068ee6e2
AQ
1663
1664 /* if the node already has a local client for this entry, it has to wait
1665 * for a roaming advertisement instead of manually messing up the global
1666 * table
1667 */
1668 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1669 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1670 goto out;
a73105b8
AQ
1671
1672 if (!tt_global_entry) {
86452f81
SE
1673 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1674 GFP_ATOMIC);
a73105b8 1675 if (!tt_global_entry)
7683fdc1
AQ
1676 goto out;
1677
c0a55929 1678 common = &tt_global_entry->common;
8fdd0153 1679 ether_addr_copy(common->addr, tt_addr);
c018ad3d 1680 common->vid = vid;
db08e6e5 1681
d4f44692 1682 common->flags = flags;
cc47f66e 1683 tt_global_entry->roam_at = 0;
fdf79320
AQ
1684 /* node must store current time in case of roaming. This is
1685 * needed to purge this entry out on timeout (if nobody claims
1686 * it)
1687 */
1688 if (flags & BATADV_TT_CLIENT_ROAM)
1689 tt_global_entry->roam_at = jiffies;
92dcdf09 1690 kref_init(&common->refcount);
30cfd02b 1691 common->added_at = jiffies;
db08e6e5
SW
1692
1693 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1d8ab8d3 1694 atomic_set(&tt_global_entry->orig_list_count, 0);
db08e6e5 1695 spin_lock_init(&tt_global_entry->list_lock);
7683fdc1 1696
15d5ffde 1697 kref_get(&common->refcount);
807736f6 1698 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
a513088d 1699 batadv_compare_tt,
c018ad3d 1700 batadv_choose_tt, common,
a513088d 1701 &common->hash_entry);
80b3f58c
SW
1702
1703 if (unlikely(hash_added != 0)) {
1704 /* remove the reference for the hash */
5dafd8a6 1705 batadv_tt_global_entry_put(tt_global_entry);
80b3f58c
SW
1706 goto out_remove;
1707 }
a73105b8 1708 } else {
068ee6e2 1709 common = &tt_global_entry->common;
30cfd02b
AQ
1710 /* If there is already a global entry, we can use this one for
1711 * our processing.
068ee6e2
AQ
1712 * But if we are trying to add a temporary client then here are
1713 * two options at this point:
1714 * 1) the global client is not a temporary client: the global
1715 * client has to be left as it is, temporary information
1716 * should never override any already known client state
1717 * 2) the global client is a temporary client: purge the
1718 * originator list and add the new one orig_entry
30cfd02b 1719 */
068ee6e2
AQ
1720 if (flags & BATADV_TT_CLIENT_TEMP) {
1721 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1722 goto out;
1723 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1724 orig_node))
1725 goto out_remove;
1726 batadv_tt_global_del_orig_list(tt_global_entry);
1727 goto add_orig_entry;
1728 }
30cfd02b
AQ
1729
1730 /* if the client was temporary added before receiving the first
a6cb3909
SW
1731 * OGM announcing it, we have to clear the TEMP flag. Also,
1732 * remove the previous temporary orig node and re-add it
1733 * if required. If the orig entry changed, the new one which
1734 * is a non-temporary entry is preferred.
30cfd02b 1735 */
a6cb3909
SW
1736 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1737 batadv_tt_global_del_orig_list(tt_global_entry);
1738 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1739 }
db08e6e5 1740
e9c00136 1741 /* the change can carry possible "attribute" flags like the
54e22f26 1742 * TT_CLIENT_TEMP, therefore they have to be copied in the
e9c00136
AQ
1743 * client entry
1744 */
54e22f26 1745 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
e9c00136 1746
acd34afa
SE
1747 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1748 * one originator left in the list and we previously received a
db08e6e5
SW
1749 * delete + roaming change for this originator.
1750 *
1751 * We should first delete the old originator before adding the
1752 * new one.
1753 */
068ee6e2 1754 if (common->flags & BATADV_TT_CLIENT_ROAM) {
a513088d 1755 batadv_tt_global_del_orig_list(tt_global_entry);
068ee6e2 1756 common->flags &= ~BATADV_TT_CLIENT_ROAM;
db08e6e5 1757 tt_global_entry->roam_at = 0;
a73105b8
AQ
1758 }
1759 }
068ee6e2 1760add_orig_entry:
30cfd02b 1761 /* add the new orig_entry (if needed) or update it */
54e22f26
LL
1762 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1763 flags & BATADV_TT_SYNC_MASK);
c6c8fea2 1764
39c75a51 1765 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 1766 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
f7a2bd65 1767 common->addr, batadv_print_vid(common->vid),
16052789 1768 orig_node->orig);
1e5d49fc 1769 ret = true;
c6c8fea2 1770
80b3f58c 1771out_remove:
c5caf4ef
LL
1772 /* Do not remove multicast addresses from the local hash on
1773 * global additions
1774 */
1775 if (is_multicast_ether_addr(tt_addr))
1776 goto out;
7f91d06c 1777
a73105b8 1778 /* remove address from local hash if present */
c018ad3d 1779 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
7f91d06c 1780 "global tt received",
c1d07431 1781 flags & BATADV_TT_CLIENT_ROAM);
7f91d06c
AQ
1782 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1783
068ee6e2
AQ
1784 if (!(flags & BATADV_TT_CLIENT_ROAM))
1785 /* this is a normal global add. Therefore the client is not in a
1786 * roaming state anymore.
1787 */
1788 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1789
7683fdc1
AQ
1790out:
1791 if (tt_global_entry)
5dafd8a6 1792 batadv_tt_global_entry_put(tt_global_entry);
068ee6e2 1793 if (tt_local_entry)
95c0db90 1794 batadv_tt_local_entry_put(tt_local_entry);
7683fdc1 1795 return ret;
c6c8fea2
SE
1796}
1797
1b371d13
SW
1798/**
1799 * batadv_transtable_best_orig - Get best originator list entry from tt entry
4627456a 1800 * @bat_priv: the bat priv with all the soft interface information
981d8900
SE
1801 * @tt_global_entry: global translation table entry to be analyzed
1802 *
1803 * This functon assumes the caller holds rcu_read_lock().
62fe710f 1804 * Return: best originator list entry or NULL on errors.
981d8900
SE
1805 */
1806static struct batadv_tt_orig_list_entry *
4627456a
AQ
1807batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1808 struct batadv_tt_global_entry *tt_global_entry)
981d8900 1809{
4627456a 1810 struct batadv_neigh_node *router, *best_router = NULL;
29824a55 1811 struct batadv_algo_ops *bao = bat_priv->algo_ops;
981d8900 1812 struct hlist_head *head;
981d8900 1813 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
981d8900
SE
1814
1815 head = &tt_global_entry->orig_list;
b67bfe0d 1816 hlist_for_each_entry_rcu(orig_entry, head, list) {
7351a482
SW
1817 router = batadv_orig_router_get(orig_entry->orig_node,
1818 BATADV_IF_DEFAULT);
981d8900
SE
1819 if (!router)
1820 continue;
1821
4627456a 1822 if (best_router &&
29824a55
AQ
1823 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1824 BATADV_IF_DEFAULT) <= 0) {
25bb2509 1825 batadv_neigh_node_put(router);
4627456a 1826 continue;
981d8900
SE
1827 }
1828
4627456a
AQ
1829 /* release the refcount for the "old" best */
1830 if (best_router)
25bb2509 1831 batadv_neigh_node_put(best_router);
4627456a
AQ
1832
1833 best_entry = orig_entry;
1834 best_router = router;
981d8900
SE
1835 }
1836
4627456a 1837 if (best_router)
25bb2509 1838 batadv_neigh_node_put(best_router);
4627456a 1839
981d8900
SE
1840 return best_entry;
1841}
1842
dc1cbd14 1843#ifdef CONFIG_BATMAN_ADV_DEBUGFS
1b371d13
SW
1844/**
1845 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1846 * for this global entry
4627456a 1847 * @bat_priv: the bat priv with all the soft interface information
981d8900
SE
1848 * @tt_global_entry: global translation table entry to be printed
1849 * @seq: debugfs table seq_file struct
1850 *
1851 * This functon assumes the caller holds rcu_read_lock().
db08e6e5 1852 */
a513088d 1853static void
4627456a
AQ
1854batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1855 struct batadv_tt_global_entry *tt_global_entry,
a513088d 1856 struct seq_file *seq)
db08e6e5 1857{
981d8900 1858 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
56303d34 1859 struct batadv_tt_common_entry *tt_common_entry;
7ea7b4a1
AQ
1860 struct batadv_orig_node_vlan *vlan;
1861 struct hlist_head *head;
6b5e971a
SE
1862 u8 last_ttvn;
1863 u16 flags;
db08e6e5
SW
1864
1865 tt_common_entry = &tt_global_entry->common;
981d8900
SE
1866 flags = tt_common_entry->flags;
1867
4627456a 1868 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
981d8900 1869 if (best_entry) {
7ea7b4a1
AQ
1870 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1871 tt_common_entry->vid);
1872 if (!vlan) {
1873 seq_printf(seq,
1874 " * Cannot retrieve VLAN %d for originator %pM\n",
f7a2bd65 1875 batadv_print_vid(tt_common_entry->vid),
7ea7b4a1
AQ
1876 best_entry->orig_node->orig);
1877 goto print_list;
1878 }
1879
981d8900 1880 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
f9d8a537 1881 seq_printf(seq,
dd24ddb2 1882 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
981d8900 1883 '*', tt_global_entry->common.addr,
f7a2bd65 1884 batadv_print_vid(tt_global_entry->common.vid),
981d8900 1885 best_entry->ttvn, best_entry->orig_node->orig,
7ea7b4a1 1886 last_ttvn, vlan->tt.crc,
a2f2b6cd
SE
1887 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1888 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1889 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1890 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
7ea7b4a1 1891
21754e25 1892 batadv_orig_node_vlan_put(vlan);
981d8900 1893 }
db08e6e5 1894
7ea7b4a1 1895print_list:
db08e6e5
SW
1896 head = &tt_global_entry->orig_list;
1897
b67bfe0d 1898 hlist_for_each_entry_rcu(orig_entry, head, list) {
981d8900
SE
1899 if (best_entry == orig_entry)
1900 continue;
1901
7ea7b4a1
AQ
1902 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1903 tt_common_entry->vid);
1904 if (!vlan) {
1905 seq_printf(seq,
1906 " + Cannot retrieve VLAN %d for originator %pM\n",
f7a2bd65 1907 batadv_print_vid(tt_common_entry->vid),
7ea7b4a1
AQ
1908 orig_entry->orig_node->orig);
1909 continue;
1910 }
1911
db08e6e5 1912 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
16052789 1913 seq_printf(seq,
dd24ddb2 1914 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
981d8900 1915 '+', tt_global_entry->common.addr,
f7a2bd65 1916 batadv_print_vid(tt_global_entry->common.vid),
981d8900 1917 orig_entry->ttvn, orig_entry->orig_node->orig,
7ea7b4a1 1918 last_ttvn, vlan->tt.crc,
a2f2b6cd
SE
1919 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1920 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1921 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1922 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
7ea7b4a1 1923
21754e25 1924 batadv_orig_node_vlan_put(vlan);
db08e6e5
SW
1925 }
1926}
1927
08c36d3e 1928int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1929{
1930 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1931 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1932 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
1933 struct batadv_tt_common_entry *tt_common_entry;
1934 struct batadv_tt_global_entry *tt_global;
1935 struct batadv_hard_iface *primary_if;
c6c8fea2 1936 struct hlist_head *head;
6b5e971a 1937 u32 i;
c6c8fea2 1938
30da63a6
ML
1939 primary_if = batadv_seq_print_text_primary_if_get(seq);
1940 if (!primary_if)
32ae9b22 1941 goto out;
c6c8fea2 1942
2dafb49d
AQ
1943 seq_printf(seq,
1944 "Globally announced TT entries received via the mesh %s\n",
c6c8fea2 1945 net_dev->name);
925a6f37
AQ
1946 seq_puts(seq,
1947 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
c6c8fea2 1948
c6c8fea2
SE
1949 for (i = 0; i < hash->size; i++) {
1950 head = &hash->table[i];
1951
7aadf889 1952 rcu_read_lock();
b67bfe0d 1953 hlist_for_each_entry_rcu(tt_common_entry,
7aadf889 1954 head, hash_entry) {
56303d34
SE
1955 tt_global = container_of(tt_common_entry,
1956 struct batadv_tt_global_entry,
1957 common);
4627456a 1958 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
c6c8fea2 1959 }
7aadf889 1960 rcu_read_unlock();
c6c8fea2 1961 }
32ae9b22
ML
1962out:
1963 if (primary_if)
82047ad7 1964 batadv_hardif_put(primary_if);
30da63a6 1965 return 0;
c6c8fea2 1966}
dc1cbd14 1967#endif
c6c8fea2 1968
d34f0550
MS
1969/**
1970 * batadv_tt_global_dump_subentry - Dump all TT local entries into a message
1971 * @msg: Netlink message to dump into
1972 * @portid: Port making netlink request
1973 * @seq: Sequence number of netlink message
1974 * @common: tt local & tt global common data
1975 * @orig: Originator node announcing a non-mesh client
1976 * @best: Is the best originator for the TT entry
1977 *
1978 * Return: Error code, or 0 on success
1979 */
1980static int
1981batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1982 struct batadv_tt_common_entry *common,
1983 struct batadv_tt_orig_list_entry *orig,
1984 bool best)
1985{
54e22f26 1986 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
d34f0550
MS
1987 void *hdr;
1988 struct batadv_orig_node_vlan *vlan;
1989 u8 last_ttvn;
1990 u32 crc;
1991
1992 vlan = batadv_orig_node_vlan_get(orig->orig_node,
1993 common->vid);
1994 if (!vlan)
1995 return 0;
1996
1997 crc = vlan->tt.crc;
1998
1999 batadv_orig_node_vlan_put(vlan);
2000
2001 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2002 NLM_F_MULTI,
2003 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2004 if (!hdr)
2005 return -ENOBUFS;
2006
2007 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2008
2009 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2010 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2011 orig->orig_node->orig) ||
2012 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2013 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2014 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2015 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
54e22f26 2016 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
d34f0550
MS
2017 goto nla_put_failure;
2018
2019 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2020 goto nla_put_failure;
2021
2022 genlmsg_end(msg, hdr);
2023 return 0;
2024
2025 nla_put_failure:
2026 genlmsg_cancel(msg, hdr);
2027 return -EMSGSIZE;
2028}
2029
2030/**
2031 * batadv_tt_global_dump_entry - Dump one TT global entry into a message
2032 * @msg: Netlink message to dump into
2033 * @portid: Port making netlink request
2034 * @seq: Sequence number of netlink message
2035 * @bat_priv: The bat priv with all the soft interface information
2036 * @common: tt local & tt global common data
2037 * @sub_s: Number of entries to skip
2038 *
2039 * This function assumes the caller holds rcu_read_lock().
2040 *
2041 * Return: Error code, or 0 on success
2042 */
2043static int
2044batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2045 struct batadv_priv *bat_priv,
2046 struct batadv_tt_common_entry *common, int *sub_s)
2047{
2048 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2049 struct batadv_tt_global_entry *global;
2050 struct hlist_head *head;
2051 int sub = 0;
2052 bool best;
2053
2054 global = container_of(common, struct batadv_tt_global_entry, common);
2055 best_entry = batadv_transtable_best_orig(bat_priv, global);
2056 head = &global->orig_list;
2057
2058 hlist_for_each_entry_rcu(orig_entry, head, list) {
2059 if (sub++ < *sub_s)
2060 continue;
2061
2062 best = (orig_entry == best_entry);
2063
2064 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2065 orig_entry, best)) {
2066 *sub_s = sub - 1;
2067 return -EMSGSIZE;
2068 }
2069 }
2070
2071 *sub_s = 0;
2072 return 0;
2073}
2074
2075/**
2076 * batadv_tt_global_dump_bucket - Dump one TT local bucket into a message
2077 * @msg: Netlink message to dump into
2078 * @portid: Port making netlink request
2079 * @seq: Sequence number of netlink message
2080 * @bat_priv: The bat priv with all the soft interface information
2081 * @head: Pointer to the list containing the global tt entries
2082 * @idx_s: Number of entries to skip
2083 * @sub: Number of entries to skip
2084 *
2085 * Return: Error code, or 0 on success
2086 */
2087static int
2088batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2089 struct batadv_priv *bat_priv,
2090 struct hlist_head *head, int *idx_s, int *sub)
2091{
2092 struct batadv_tt_common_entry *common;
2093 int idx = 0;
2094
2095 rcu_read_lock();
2096 hlist_for_each_entry_rcu(common, head, hash_entry) {
2097 if (idx++ < *idx_s)
2098 continue;
2099
2100 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2101 common, sub)) {
2102 rcu_read_unlock();
2103 *idx_s = idx - 1;
2104 return -EMSGSIZE;
2105 }
2106 }
2107 rcu_read_unlock();
2108
2109 *idx_s = 0;
2110 *sub = 0;
2111 return 0;
2112}
2113
2114/**
2115 * batadv_tt_global_dump - Dump TT global entries into a message
2116 * @msg: Netlink message to dump into
2117 * @cb: Parameters from query
2118 *
2119 * Return: Error code, or length of message on success
2120 */
2121int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2122{
2123 struct net *net = sock_net(cb->skb->sk);
2124 struct net_device *soft_iface;
2125 struct batadv_priv *bat_priv;
2126 struct batadv_hard_iface *primary_if = NULL;
2127 struct batadv_hashtable *hash;
2128 struct hlist_head *head;
2129 int ret;
2130 int ifindex;
2131 int bucket = cb->args[0];
2132 int idx = cb->args[1];
2133 int sub = cb->args[2];
2134 int portid = NETLINK_CB(cb->skb).portid;
2135
2136 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2137 if (!ifindex)
2138 return -EINVAL;
2139
2140 soft_iface = dev_get_by_index(net, ifindex);
2141 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2142 ret = -ENODEV;
2143 goto out;
2144 }
2145
2146 bat_priv = netdev_priv(soft_iface);
2147
2148 primary_if = batadv_primary_if_get_selected(bat_priv);
2149 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2150 ret = -ENOENT;
2151 goto out;
2152 }
2153
2154 hash = bat_priv->tt.global_hash;
2155
2156 while (bucket < hash->size) {
2157 head = &hash->table[bucket];
2158
2159 if (batadv_tt_global_dump_bucket(msg, portid,
2160 cb->nlh->nlmsg_seq, bat_priv,
2161 head, &idx, &sub))
2162 break;
2163
2164 bucket++;
2165 }
2166
2167 ret = msg->len;
2168
2169 out:
2170 if (primary_if)
2171 batadv_hardif_put(primary_if);
2172 if (soft_iface)
2173 dev_put(soft_iface);
2174
2175 cb->args[0] = bucket;
2176 cb->args[1] = idx;
2177 cb->args[2] = sub;
2178
2179 return ret;
2180}
2181
1d8ab8d3 2182/**
433ff98f 2183 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1d8ab8d3
LL
2184 * @tt_global_entry: the global entry to remove the orig_entry from
2185 * @orig_entry: the orig entry to remove and free
2186 *
2187 * Remove an orig_entry from its list in the given tt_global_entry and
2188 * free this orig_entry afterwards.
433ff98f
ML
2189 *
2190 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2191 * part of a list.
1d8ab8d3
LL
2192 */
2193static void
433ff98f
ML
2194_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2195 struct batadv_tt_orig_list_entry *orig_entry)
1d8ab8d3 2196{
2c72d655
SE
2197 lockdep_assert_held(&tt_global_entry->list_lock);
2198
1d8ab8d3
LL
2199 batadv_tt_global_size_dec(orig_entry->orig_node,
2200 tt_global_entry->common.vid);
2201 atomic_dec(&tt_global_entry->orig_list_count);
433ff98f
ML
2202 /* requires holding tt_global_entry->list_lock and orig_entry->list
2203 * being part of a list
2204 */
1d8ab8d3 2205 hlist_del_rcu(&orig_entry->list);
7e2366c6 2206 batadv_tt_orig_list_entry_put(orig_entry);
1d8ab8d3
LL
2207}
2208
db08e6e5 2209/* deletes the orig list of a tt_global_entry */
a513088d 2210static void
56303d34 2211batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
c6c8fea2 2212{
db08e6e5 2213 struct hlist_head *head;
b67bfe0d 2214 struct hlist_node *safe;
56303d34 2215 struct batadv_tt_orig_list_entry *orig_entry;
a73105b8 2216
db08e6e5
SW
2217 spin_lock_bh(&tt_global_entry->list_lock);
2218 head = &tt_global_entry->orig_list;
1d8ab8d3 2219 hlist_for_each_entry_safe(orig_entry, safe, head, list)
433ff98f 2220 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
db08e6e5 2221 spin_unlock_bh(&tt_global_entry->list_lock);
db08e6e5
SW
2222}
2223
1d8ab8d3
LL
2224/**
2225 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
2226 * @bat_priv: the bat priv with all the soft interface information
2227 * @tt_global_entry: the global entry to remove the orig_node from
2228 * @orig_node: the originator announcing the client
2229 * @message: message to append to the log on deletion
2230 *
2231 * Remove the given orig_node and its according orig_entry from the given
2232 * global tt entry.
2233 */
a513088d 2234static void
1d8ab8d3
LL
2235batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2236 struct batadv_tt_global_entry *tt_global_entry,
2237 struct batadv_orig_node *orig_node,
2238 const char *message)
db08e6e5
SW
2239{
2240 struct hlist_head *head;
b67bfe0d 2241 struct hlist_node *safe;
56303d34 2242 struct batadv_tt_orig_list_entry *orig_entry;
16052789 2243 unsigned short vid;
db08e6e5
SW
2244
2245 spin_lock_bh(&tt_global_entry->list_lock);
2246 head = &tt_global_entry->orig_list;
b67bfe0d 2247 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
db08e6e5 2248 if (orig_entry->orig_node == orig_node) {
16052789 2249 vid = tt_global_entry->common.vid;
39c75a51 2250 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 2251 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1eda58bf 2252 orig_node->orig,
16052789 2253 tt_global_entry->common.addr,
f7a2bd65 2254 batadv_print_vid(vid), message);
433ff98f
ML
2255 _batadv_tt_global_del_orig_entry(tt_global_entry,
2256 orig_entry);
db08e6e5
SW
2257 }
2258 }
2259 spin_unlock_bh(&tt_global_entry->list_lock);
2260}
2261
db08e6e5 2262/* If the client is to be deleted, we check if it is the last origantor entry
acd34afa
SE
2263 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2264 * timer, otherwise we simply remove the originator scheduled for deletion.
db08e6e5 2265 */
a513088d 2266static void
56303d34
SE
2267batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2268 struct batadv_tt_global_entry *tt_global_entry,
2269 struct batadv_orig_node *orig_node,
2270 const char *message)
db08e6e5
SW
2271{
2272 bool last_entry = true;
2273 struct hlist_head *head;
56303d34 2274 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5
SW
2275
2276 /* no local entry exists, case 1:
2277 * Check if this is the last one or if other entries exist.
2278 */
2279
2280 rcu_read_lock();
2281 head = &tt_global_entry->orig_list;
b67bfe0d 2282 hlist_for_each_entry_rcu(orig_entry, head, list) {
db08e6e5
SW
2283 if (orig_entry->orig_node != orig_node) {
2284 last_entry = false;
2285 break;
2286 }
2287 }
2288 rcu_read_unlock();
2289
2290 if (last_entry) {
2291 /* its the last one, mark for roaming. */
acd34afa 2292 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
db08e6e5 2293 tt_global_entry->roam_at = jiffies;
1fda4c0a 2294 } else {
db08e6e5
SW
2295 /* there is another entry, we can simply delete this
2296 * one and can still use the other one.
2297 */
1d8ab8d3
LL
2298 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2299 orig_node, message);
1fda4c0a 2300 }
db08e6e5
SW
2301}
2302
c018ad3d
AQ
2303/**
2304 * batadv_tt_global_del - remove a client from the global table
2305 * @bat_priv: the bat priv with all the soft interface information
2306 * @orig_node: an originator serving this client
2307 * @addr: the mac address of the client
2308 * @vid: VLAN identifier
2309 * @message: a message explaining the reason for deleting the client to print
2310 * for debugging purpose
2311 * @roaming: true if the deletion has been triggered by a roaming event
2312 */
56303d34
SE
2313static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2314 struct batadv_orig_node *orig_node,
c018ad3d 2315 const unsigned char *addr, unsigned short vid,
a513088d 2316 const char *message, bool roaming)
a73105b8 2317{
170173bf 2318 struct batadv_tt_global_entry *tt_global_entry;
56303d34 2319 struct batadv_tt_local_entry *local_entry = NULL;
a73105b8 2320
c018ad3d 2321 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
db08e6e5 2322 if (!tt_global_entry)
7683fdc1 2323 goto out;
a73105b8 2324
db08e6e5 2325 if (!roaming) {
1d8ab8d3
LL
2326 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2327 orig_node, message);
db08e6e5
SW
2328
2329 if (hlist_empty(&tt_global_entry->orig_list))
be73b488
AQ
2330 batadv_tt_global_free(bat_priv, tt_global_entry,
2331 message);
db08e6e5
SW
2332
2333 goto out;
2334 }
92f90f56
SE
2335
2336 /* if we are deleting a global entry due to a roam
2337 * event, there are two possibilities:
db08e6e5
SW
2338 * 1) the client roamed from node A to node B => if there
2339 * is only one originator left for this client, we mark
acd34afa 2340 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
92f90f56
SE
2341 * wait for node B to claim it. In case of timeout
2342 * the entry is purged.
db08e6e5
SW
2343 *
2344 * If there are other originators left, we directly delete
2345 * the originator.
92f90f56 2346 * 2) the client roamed to us => we can directly delete
9cfc7bd6
SE
2347 * the global entry, since it is useless now.
2348 */
a513088d 2349 local_entry = batadv_tt_local_hash_find(bat_priv,
c018ad3d
AQ
2350 tt_global_entry->common.addr,
2351 vid);
a513088d 2352 if (local_entry) {
db08e6e5 2353 /* local entry exists, case 2: client roamed to us. */
a513088d 2354 batadv_tt_global_del_orig_list(tt_global_entry);
be73b488 2355 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1fda4c0a 2356 } else {
db08e6e5 2357 /* no local entry exists, case 1: check for roaming */
a513088d
SE
2358 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2359 orig_node, message);
1fda4c0a 2360 }
92f90f56 2361
cc47f66e 2362out:
7683fdc1 2363 if (tt_global_entry)
5dafd8a6 2364 batadv_tt_global_entry_put(tt_global_entry);
a513088d 2365 if (local_entry)
95c0db90 2366 batadv_tt_local_entry_put(local_entry);
a73105b8
AQ
2367}
2368
95fb130d
AQ
2369/**
2370 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
2371 * given originator matching the provided vid
2372 * @bat_priv: the bat priv with all the soft interface information
2373 * @orig_node: the originator owning the entries to remove
2374 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2375 * removed
2376 * @message: debug message to print as "reason"
2377 */
56303d34
SE
2378void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2379 struct batadv_orig_node *orig_node,
6b5e971a 2380 s32 match_vid,
56303d34 2381 const char *message)
c6c8fea2 2382{
56303d34
SE
2383 struct batadv_tt_global_entry *tt_global;
2384 struct batadv_tt_common_entry *tt_common_entry;
6b5e971a 2385 u32 i;
807736f6 2386 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
b67bfe0d 2387 struct hlist_node *safe;
a73105b8 2388 struct hlist_head *head;
7683fdc1 2389 spinlock_t *list_lock; /* protects write access to the hash lists */
16052789 2390 unsigned short vid;
c6c8fea2 2391
6e801494
SW
2392 if (!hash)
2393 return;
2394
a73105b8
AQ
2395 for (i = 0; i < hash->size; i++) {
2396 head = &hash->table[i];
7683fdc1 2397 list_lock = &hash->list_locks[i];
c6c8fea2 2398
7683fdc1 2399 spin_lock_bh(list_lock);
b67bfe0d 2400 hlist_for_each_entry_safe(tt_common_entry, safe,
7c64fd98 2401 head, hash_entry) {
95fb130d
AQ
2402 /* remove only matching entries */
2403 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2404 continue;
2405
56303d34
SE
2406 tt_global = container_of(tt_common_entry,
2407 struct batadv_tt_global_entry,
2408 common);
db08e6e5 2409
1d8ab8d3
LL
2410 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2411 orig_node, message);
db08e6e5 2412
56303d34 2413 if (hlist_empty(&tt_global->orig_list)) {
16052789 2414 vid = tt_global->common.vid;
39c75a51 2415 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
2416 "Deleting global tt entry %pM (vid: %d): %s\n",
2417 tt_global->common.addr,
f7a2bd65 2418 batadv_print_vid(vid), message);
b67bfe0d 2419 hlist_del_rcu(&tt_common_entry->hash_entry);
5dafd8a6 2420 batadv_tt_global_entry_put(tt_global);
7683fdc1 2421 }
a73105b8 2422 }
7683fdc1 2423 spin_unlock_bh(list_lock);
c6c8fea2 2424 }
ac4eebd4 2425 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
c6c8fea2
SE
2426}
2427
30cfd02b
AQ
2428static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2429 char **msg)
cc47f66e 2430{
30cfd02b
AQ
2431 bool purge = false;
2432 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2433 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
42d0b044 2434
30cfd02b
AQ
2435 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2436 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2437 purge = true;
2438 *msg = "Roaming timeout\n";
2439 }
42d0b044 2440
30cfd02b
AQ
2441 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2442 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2443 purge = true;
2444 *msg = "Temporary client timeout\n";
42d0b044 2445 }
30cfd02b
AQ
2446
2447 return purge;
42d0b044
SE
2448}
2449
30cfd02b 2450static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
42d0b044 2451{
807736f6 2452 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
cc47f66e 2453 struct hlist_head *head;
b67bfe0d 2454 struct hlist_node *node_tmp;
7683fdc1 2455 spinlock_t *list_lock; /* protects write access to the hash lists */
6b5e971a 2456 u32 i;
30cfd02b
AQ
2457 char *msg = NULL;
2458 struct batadv_tt_common_entry *tt_common;
2459 struct batadv_tt_global_entry *tt_global;
cc47f66e 2460
cc47f66e
AQ
2461 for (i = 0; i < hash->size; i++) {
2462 head = &hash->table[i];
7683fdc1 2463 list_lock = &hash->list_locks[i];
cc47f66e 2464
7683fdc1 2465 spin_lock_bh(list_lock);
b67bfe0d 2466 hlist_for_each_entry_safe(tt_common, node_tmp, head,
30cfd02b
AQ
2467 hash_entry) {
2468 tt_global = container_of(tt_common,
2469 struct batadv_tt_global_entry,
2470 common);
2471
2472 if (!batadv_tt_global_to_purge(tt_global, &msg))
2473 continue;
2474
2475 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
2476 "Deleting global tt entry %pM (vid: %d): %s\n",
2477 tt_global->common.addr,
f7a2bd65 2478 batadv_print_vid(tt_global->common.vid),
16052789 2479 msg);
30cfd02b 2480
b67bfe0d 2481 hlist_del_rcu(&tt_common->hash_entry);
30cfd02b 2482
5dafd8a6 2483 batadv_tt_global_entry_put(tt_global);
30cfd02b 2484 }
7683fdc1 2485 spin_unlock_bh(list_lock);
cc47f66e 2486 }
cc47f66e
AQ
2487}
2488
56303d34 2489static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
c6c8fea2 2490{
5bf74e9c 2491 struct batadv_hashtable *hash;
7683fdc1 2492 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
2493 struct batadv_tt_common_entry *tt_common_entry;
2494 struct batadv_tt_global_entry *tt_global;
b67bfe0d 2495 struct hlist_node *node_tmp;
7683fdc1 2496 struct hlist_head *head;
6b5e971a 2497 u32 i;
7683fdc1 2498
807736f6 2499 if (!bat_priv->tt.global_hash)
c6c8fea2
SE
2500 return;
2501
807736f6 2502 hash = bat_priv->tt.global_hash;
7683fdc1
AQ
2503
2504 for (i = 0; i < hash->size; i++) {
2505 head = &hash->table[i];
2506 list_lock = &hash->list_locks[i];
2507
2508 spin_lock_bh(list_lock);
b67bfe0d 2509 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
7683fdc1 2510 head, hash_entry) {
b67bfe0d 2511 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34
SE
2512 tt_global = container_of(tt_common_entry,
2513 struct batadv_tt_global_entry,
2514 common);
5dafd8a6 2515 batadv_tt_global_entry_put(tt_global);
7683fdc1
AQ
2516 }
2517 spin_unlock_bh(list_lock);
2518 }
2519
1a8eaf07 2520 batadv_hash_destroy(hash);
7683fdc1 2521
807736f6 2522 bat_priv->tt.global_hash = NULL;
c6c8fea2
SE
2523}
2524
56303d34
SE
2525static bool
2526_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2527 struct batadv_tt_global_entry *tt_global_entry)
59b699cd 2528{
acd34afa
SE
2529 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2530 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
75ae84a4 2531 return true;
59b699cd 2532
2d2fcc2a
AQ
2533 /* check if the two clients are marked as isolated */
2534 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2535 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
75ae84a4 2536 return true;
2d2fcc2a 2537
75ae84a4 2538 return false;
59b699cd
AQ
2539}
2540
c018ad3d
AQ
2541/**
2542 * batadv_transtable_search - get the mesh destination for a given client
2543 * @bat_priv: the bat priv with all the soft interface information
2544 * @src: mac address of the source client
2545 * @addr: mac address of the destination client
2546 * @vid: VLAN identifier
2547 *
62fe710f 2548 * Return: a pointer to the originator that was selected as destination in the
c018ad3d
AQ
2549 * mesh for contacting the client 'addr', NULL otherwise.
2550 * In case of multiple originators serving the same client, the function returns
2551 * the best one (best in terms of metric towards the destination node).
2552 *
2553 * If the two clients are AP isolated the function returns NULL.
2554 */
56303d34 2555struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
6b5e971a
SE
2556 const u8 *src,
2557 const u8 *addr,
c018ad3d 2558 unsigned short vid)
c6c8fea2 2559{
56303d34
SE
2560 struct batadv_tt_local_entry *tt_local_entry = NULL;
2561 struct batadv_tt_global_entry *tt_global_entry = NULL;
2562 struct batadv_orig_node *orig_node = NULL;
981d8900 2563 struct batadv_tt_orig_list_entry *best_entry;
b8cbd81d 2564
eceb22ae 2565 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
c018ad3d 2566 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
068ee6e2
AQ
2567 if (!tt_local_entry ||
2568 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
3d393e47
AQ
2569 goto out;
2570 }
7aadf889 2571
c018ad3d 2572 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2dafb49d 2573 if (!tt_global_entry)
7b36e8ee 2574 goto out;
7aadf889 2575
3d393e47 2576 /* check whether the clients should not communicate due to AP
9cfc7bd6
SE
2577 * isolation
2578 */
a513088d
SE
2579 if (tt_local_entry &&
2580 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3d393e47
AQ
2581 goto out;
2582
db08e6e5 2583 rcu_read_lock();
4627456a 2584 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
db08e6e5 2585 /* found anything? */
981d8900
SE
2586 if (best_entry)
2587 orig_node = best_entry->orig_node;
7c124391 2588 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
db08e6e5
SW
2589 orig_node = NULL;
2590 rcu_read_unlock();
981d8900 2591
7b36e8ee 2592out:
3d393e47 2593 if (tt_global_entry)
5dafd8a6 2594 batadv_tt_global_entry_put(tt_global_entry);
3d393e47 2595 if (tt_local_entry)
95c0db90 2596 batadv_tt_local_entry_put(tt_local_entry);
3d393e47 2597
7b36e8ee 2598 return orig_node;
c6c8fea2 2599}
a73105b8 2600
ced72933
AQ
2601/**
2602 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2603 * to the given orig_node
2604 * @bat_priv: the bat priv with all the soft interface information
0ffa9e8d 2605 * @orig_node: originator for which the CRC should be computed
7ea7b4a1 2606 * @vid: VLAN identifier for which the CRC32 has to be computed
0ffa9e8d
AQ
2607 *
2608 * This function computes the checksum for the global table corresponding to a
2609 * specific originator. In particular, the checksum is computed as follows: For
2610 * each client connected to the originator the CRC32C of the MAC address and the
2611 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2612 * together.
2613 *
2614 * The idea behind is that CRC32C should be used as much as possible in order to
2615 * produce a unique hash of the table, but since the order which is used to feed
2616 * the CRC32C function affects the result and since every node in the network
2617 * probably sorts the clients differently, the hash function cannot be directly
2618 * computed over the entire table. Hence the CRC32C is used only on
2619 * the single client entry, while all the results are then xor'ed together
2620 * because the XOR operation can combine them all while trying to reduce the
2621 * noise as much as possible.
2622 *
62fe710f 2623 * Return: the checksum of the global table of a given originator.
ced72933 2624 */
6b5e971a
SE
2625static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2626 struct batadv_orig_node *orig_node,
2627 unsigned short vid)
a73105b8 2628{
807736f6 2629 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
54e22f26 2630 struct batadv_tt_orig_list_entry *tt_orig;
56303d34
SE
2631 struct batadv_tt_common_entry *tt_common;
2632 struct batadv_tt_global_entry *tt_global;
a73105b8 2633 struct hlist_head *head;
6b5e971a
SE
2634 u32 i, crc_tmp, crc = 0;
2635 u8 flags;
a30e22ca 2636 __be16 tmp_vid;
a73105b8
AQ
2637
2638 for (i = 0; i < hash->size; i++) {
2639 head = &hash->table[i];
2640
2641 rcu_read_lock();
b67bfe0d 2642 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
56303d34
SE
2643 tt_global = container_of(tt_common,
2644 struct batadv_tt_global_entry,
2645 common);
7ea7b4a1
AQ
2646 /* compute the CRC only for entries belonging to the
2647 * VLAN identified by the vid passed as parameter
2648 */
2649 if (tt_common->vid != vid)
2650 continue;
2651
db08e6e5
SW
2652 /* Roaming clients are in the global table for
2653 * consistency only. They don't have to be
2654 * taken into account while computing the
2655 * global crc
2656 */
acd34afa 2657 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
db08e6e5 2658 continue;
30cfd02b
AQ
2659 /* Temporary clients have not been announced yet, so
2660 * they have to be skipped while computing the global
2661 * crc
2662 */
2663 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2664 continue;
db08e6e5
SW
2665
2666 /* find out if this global entry is announced by this
2667 * originator
2668 */
54e22f26
LL
2669 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2670 orig_node);
2671 if (!tt_orig)
db08e6e5
SW
2672 continue;
2673
a30e22ca
AQ
2674 /* use network order to read the VID: this ensures that
2675 * every node reads the bytes in the same order.
2676 */
2677 tmp_vid = htons(tt_common->vid);
2678 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
0eb01568
AQ
2679
2680 /* compute the CRC on flags that have to be kept in sync
2681 * among nodes
2682 */
54e22f26 2683 flags = tt_orig->flags;
0eb01568
AQ
2684 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2685
0ffa9e8d 2686 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
54e22f26
LL
2687
2688 batadv_tt_orig_list_entry_put(tt_orig);
a73105b8
AQ
2689 }
2690 rcu_read_unlock();
2691 }
2692
ced72933 2693 return crc;
a73105b8
AQ
2694}
2695
ced72933
AQ
2696/**
2697 * batadv_tt_local_crc - calculates the checksum of the local table
2698 * @bat_priv: the bat priv with all the soft interface information
7ea7b4a1 2699 * @vid: VLAN identifier for which the CRC32 has to be computed
0ffa9e8d
AQ
2700 *
2701 * For details about the computation, please refer to the documentation for
2702 * batadv_tt_global_crc().
2703 *
62fe710f 2704 * Return: the checksum of the local table
ced72933 2705 */
6b5e971a
SE
2706static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2707 unsigned short vid)
a73105b8 2708{
807736f6 2709 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 2710 struct batadv_tt_common_entry *tt_common;
a73105b8 2711 struct hlist_head *head;
6b5e971a
SE
2712 u32 i, crc_tmp, crc = 0;
2713 u8 flags;
a30e22ca 2714 __be16 tmp_vid;
a73105b8
AQ
2715
2716 for (i = 0; i < hash->size; i++) {
2717 head = &hash->table[i];
2718
2719 rcu_read_lock();
b67bfe0d 2720 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
7ea7b4a1
AQ
2721 /* compute the CRC only for entries belonging to the
2722 * VLAN identified by vid
2723 */
2724 if (tt_common->vid != vid)
2725 continue;
2726
058d0e26 2727 /* not yet committed clients have not to be taken into
9cfc7bd6
SE
2728 * account while computing the CRC
2729 */
acd34afa 2730 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
058d0e26 2731 continue;
ced72933 2732
a30e22ca
AQ
2733 /* use network order to read the VID: this ensures that
2734 * every node reads the bytes in the same order.
2735 */
2736 tmp_vid = htons(tt_common->vid);
2737 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
0eb01568
AQ
2738
2739 /* compute the CRC on flags that have to be kept in sync
2740 * among nodes
2741 */
2742 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2743 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2744
0ffa9e8d 2745 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
a73105b8 2746 }
a73105b8
AQ
2747 rcu_read_unlock();
2748 }
2749
ced72933 2750 return crc;
a73105b8
AQ
2751}
2752
9c4604a2
SE
2753/**
2754 * batadv_tt_req_node_release - free tt_req node entry
2755 * @ref: kref pointer of the tt req_node entry
2756 */
2757static void batadv_tt_req_node_release(struct kref *ref)
2758{
2759 struct batadv_tt_req_node *tt_req_node;
2760
2761 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2762
86452f81 2763 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
9c4604a2
SE
2764}
2765
2766/**
2767 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2768 * possibly release it
2769 * @tt_req_node: tt_req_node to be free'd
2770 */
2771static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2772{
2773 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2774}
2775
56303d34 2776static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
a73105b8 2777{
7c26a53b
ML
2778 struct batadv_tt_req_node *node;
2779 struct hlist_node *safe;
a73105b8 2780
807736f6 2781 spin_lock_bh(&bat_priv->tt.req_list_lock);
a73105b8 2782
7c26a53b
ML
2783 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2784 hlist_del_init(&node->list);
9c4604a2 2785 batadv_tt_req_node_put(node);
a73105b8
AQ
2786 }
2787
807736f6 2788 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2789}
2790
56303d34
SE
2791static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2792 struct batadv_orig_node *orig_node,
e8cf234a 2793 const void *tt_buff,
6b5e971a 2794 u16 tt_buff_len)
a73105b8 2795{
a73105b8 2796 /* Replace the old buffer only if I received something in the
9cfc7bd6
SE
2797 * last OGM (the OGM could carry no changes)
2798 */
a73105b8
AQ
2799 spin_lock_bh(&orig_node->tt_buff_lock);
2800 if (tt_buff_len > 0) {
2801 kfree(orig_node->tt_buff);
2802 orig_node->tt_buff_len = 0;
2803 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2804 if (orig_node->tt_buff) {
2805 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2806 orig_node->tt_buff_len = tt_buff_len;
2807 }
2808 }
2809 spin_unlock_bh(&orig_node->tt_buff_lock);
2810}
2811
56303d34 2812static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
a73105b8 2813{
7c26a53b
ML
2814 struct batadv_tt_req_node *node;
2815 struct hlist_node *safe;
a73105b8 2816
807736f6 2817 spin_lock_bh(&bat_priv->tt.req_list_lock);
7c26a53b 2818 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
42d0b044
SE
2819 if (batadv_has_timed_out(node->issued_at,
2820 BATADV_TT_REQUEST_TIMEOUT)) {
7c26a53b 2821 hlist_del_init(&node->list);
9c4604a2 2822 batadv_tt_req_node_put(node);
a73105b8
AQ
2823 }
2824 }
807736f6 2825 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2826}
2827
383b8636
ML
2828/**
2829 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2830 * @bat_priv: the bat priv with all the soft interface information
2831 * @orig_node: orig node this request is being issued for
2832 *
62fe710f 2833 * Return: the pointer to the new tt_req_node struct if no request
383b8636 2834 * has already been issued for this orig_node, NULL otherwise.
9cfc7bd6 2835 */
56303d34 2836static struct batadv_tt_req_node *
383b8636 2837batadv_tt_req_node_new(struct batadv_priv *bat_priv,
56303d34 2838 struct batadv_orig_node *orig_node)
a73105b8 2839{
56303d34 2840 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
a73105b8 2841
807736f6 2842 spin_lock_bh(&bat_priv->tt.req_list_lock);
7c26a53b 2843 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1eda58bf
SE
2844 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2845 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
42d0b044 2846 BATADV_TT_REQUEST_TIMEOUT))
a73105b8
AQ
2847 goto unlock;
2848 }
2849
86452f81 2850 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
a73105b8
AQ
2851 if (!tt_req_node)
2852 goto unlock;
2853
9c4604a2 2854 kref_init(&tt_req_node->refcount);
8fdd0153 2855 ether_addr_copy(tt_req_node->addr, orig_node->orig);
a73105b8
AQ
2856 tt_req_node->issued_at = jiffies;
2857
9c4604a2 2858 kref_get(&tt_req_node->refcount);
7c26a53b 2859 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
a73105b8 2860unlock:
807736f6 2861 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2862 return tt_req_node;
2863}
2864
335fbe0f
ML
2865/**
2866 * batadv_tt_local_valid - verify that given tt entry is a valid one
2867 * @entry_ptr: to be checked local tt entry
2868 * @data_ptr: not used but definition required to satisfy the callback prototype
2869 *
4b426b10 2870 * Return: true if the entry is a valid, false otherwise.
335fbe0f 2871 */
4b426b10 2872static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
058d0e26 2873{
56303d34 2874 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
058d0e26 2875
acd34afa 2876 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
4b426b10
SE
2877 return false;
2878 return true;
058d0e26
AQ
2879}
2880
4b426b10
SE
2881static bool batadv_tt_global_valid(const void *entry_ptr,
2882 const void *data_ptr)
a73105b8 2883{
56303d34
SE
2884 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2885 const struct batadv_tt_global_entry *tt_global_entry;
2886 const struct batadv_orig_node *orig_node = data_ptr;
a73105b8 2887
30cfd02b
AQ
2888 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2889 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
4b426b10 2890 return false;
cc47f66e 2891
56303d34
SE
2892 tt_global_entry = container_of(tt_common_entry,
2893 struct batadv_tt_global_entry,
48100bac
AQ
2894 common);
2895
a513088d 2896 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
a73105b8
AQ
2897}
2898
335fbe0f 2899/**
7ea7b4a1
AQ
2900 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2901 * specified tt hash
335fbe0f
ML
2902 * @bat_priv: the bat priv with all the soft interface information
2903 * @hash: hash table containing the tt entries
2904 * @tt_len: expected tvlv tt data buffer length in number of bytes
7ea7b4a1 2905 * @tvlv_buff: pointer to the buffer to fill with the TT data
335fbe0f
ML
2906 * @valid_cb: function to filter tt change entries
2907 * @cb_data: data passed to the filter function as argument
335fbe0f 2908 */
7ea7b4a1
AQ
2909static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2910 struct batadv_hashtable *hash,
6b5e971a 2911 void *tvlv_buff, u16 tt_len,
4b426b10
SE
2912 bool (*valid_cb)(const void *,
2913 const void *),
7ea7b4a1 2914 void *cb_data)
a73105b8 2915{
56303d34 2916 struct batadv_tt_common_entry *tt_common_entry;
335fbe0f 2917 struct batadv_tvlv_tt_change *tt_change;
a73105b8 2918 struct hlist_head *head;
6b5e971a
SE
2919 u16 tt_tot, tt_num_entries = 0;
2920 u32 i;
a73105b8 2921
298e6e68 2922 tt_tot = batadv_tt_entries(tt_len);
7ea7b4a1 2923 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
a73105b8
AQ
2924
2925 rcu_read_lock();
2926 for (i = 0; i < hash->size; i++) {
2927 head = &hash->table[i];
2928
b67bfe0d 2929 hlist_for_each_entry_rcu(tt_common_entry,
a73105b8 2930 head, hash_entry) {
335fbe0f 2931 if (tt_tot == tt_num_entries)
a73105b8
AQ
2932 break;
2933
48100bac 2934 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
a73105b8
AQ
2935 continue;
2936
8fdd0153 2937 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
27b37ebf 2938 tt_change->flags = tt_common_entry->flags;
c018ad3d 2939 tt_change->vid = htons(tt_common_entry->vid);
ca663046
AQ
2940 memset(tt_change->reserved, 0,
2941 sizeof(tt_change->reserved));
a73105b8 2942
335fbe0f 2943 tt_num_entries++;
a73105b8
AQ
2944 tt_change++;
2945 }
2946 }
2947 rcu_read_unlock();
7ea7b4a1 2948}
a73105b8 2949
7ea7b4a1
AQ
2950/**
2951 * batadv_tt_global_check_crc - check if all the CRCs are correct
2952 * @orig_node: originator for which the CRCs have to be checked
2953 * @tt_vlan: pointer to the first tvlv VLAN entry
2954 * @num_vlan: number of tvlv VLAN entries
7ea7b4a1 2955 *
62fe710f 2956 * Return: true if all the received CRCs match the locally stored ones, false
7ea7b4a1
AQ
2957 * otherwise
2958 */
2959static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2960 struct batadv_tvlv_tt_vlan_data *tt_vlan,
6b5e971a 2961 u16 num_vlan)
7ea7b4a1
AQ
2962{
2963 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2964 struct batadv_orig_node_vlan *vlan;
c169c59d 2965 int i, orig_num_vlan;
6b5e971a 2966 u32 crc;
7ea7b4a1
AQ
2967
2968 /* check if each received CRC matches the locally stored one */
2969 for (i = 0; i < num_vlan; i++) {
2970 tt_vlan_tmp = tt_vlan + i;
2971
2972 /* if orig_node is a backbone node for this VLAN, don't check
2973 * the CRC as we ignore all the global entries over it
2974 */
2975 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
cfd4f757
AQ
2976 orig_node->orig,
2977 ntohs(tt_vlan_tmp->vid)))
7ea7b4a1
AQ
2978 continue;
2979
2980 vlan = batadv_orig_node_vlan_get(orig_node,
2981 ntohs(tt_vlan_tmp->vid));
2982 if (!vlan)
2983 return false;
2984
91c2b1a9 2985 crc = vlan->tt.crc;
21754e25 2986 batadv_orig_node_vlan_put(vlan);
91c2b1a9
AQ
2987
2988 if (crc != ntohl(tt_vlan_tmp->crc))
7ea7b4a1
AQ
2989 return false;
2990 }
2991
c169c59d
SW
2992 /* check if any excess VLANs exist locally for the originator
2993 * which are not mentioned in the TVLV from the originator.
2994 */
2995 rcu_read_lock();
2996 orig_num_vlan = 0;
2997 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2998 orig_num_vlan++;
2999 rcu_read_unlock();
3000
3001 if (orig_num_vlan > num_vlan)
3002 return false;
3003
7ea7b4a1
AQ
3004 return true;
3005}
3006
3007/**
3008 * batadv_tt_local_update_crc - update all the local CRCs
3009 * @bat_priv: the bat priv with all the soft interface information
3010 */
3011static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3012{
3013 struct batadv_softif_vlan *vlan;
3014
3015 /* recompute the global CRC for each VLAN */
3016 rcu_read_lock();
3017 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3018 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3019 }
3020 rcu_read_unlock();
3021}
3022
3023/**
3024 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
3025 * @bat_priv: the bat priv with all the soft interface information
3026 * @orig_node: the orig_node for which the CRCs have to be updated
3027 */
3028static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3029 struct batadv_orig_node *orig_node)
3030{
3031 struct batadv_orig_node_vlan *vlan;
6b5e971a 3032 u32 crc;
7ea7b4a1
AQ
3033
3034 /* recompute the global CRC for each VLAN */
3035 rcu_read_lock();
d0fa4f3f 3036 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
3037 /* if orig_node is a backbone node for this VLAN, don't compute
3038 * the CRC as we ignore all the global entries over it
3039 */
cfd4f757
AQ
3040 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3041 vlan->vid))
7ea7b4a1
AQ
3042 continue;
3043
3044 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3045 vlan->tt.crc = crc;
3046 }
3047 rcu_read_unlock();
a73105b8
AQ
3048}
3049
ced72933
AQ
3050/**
3051 * batadv_send_tt_request - send a TT Request message to a given node
3052 * @bat_priv: the bat priv with all the soft interface information
3053 * @dst_orig_node: the destination of the message
3054 * @ttvn: the version number that the source of the message is looking for
7ea7b4a1
AQ
3055 * @tt_vlan: pointer to the first tvlv VLAN object to request
3056 * @num_vlan: number of tvlv VLAN entries
ced72933
AQ
3057 * @full_table: ask for the entire translation table if true, while only for the
3058 * last TT diff otherwise
d15cd622
AQ
3059 *
3060 * Return: true if the TT Request was sent, false otherwise
ced72933 3061 */
4b426b10
SE
3062static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3063 struct batadv_orig_node *dst_orig_node,
3064 u8 ttvn,
3065 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3066 u16 num_vlan, bool full_table)
a73105b8 3067{
335fbe0f 3068 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
56303d34 3069 struct batadv_tt_req_node *tt_req_node = NULL;
7ea7b4a1
AQ
3070 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3071 struct batadv_hard_iface *primary_if;
335fbe0f 3072 bool ret = false;
7ea7b4a1 3073 int i, size;
a73105b8 3074
e5d89254 3075 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
3076 if (!primary_if)
3077 goto out;
3078
3079 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
3080 * reply from the same orig_node yet
3081 */
383b8636 3082 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
a73105b8
AQ
3083 if (!tt_req_node)
3084 goto out;
3085
7ea7b4a1
AQ
3086 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3087 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
335fbe0f 3088 if (!tvlv_tt_data)
a73105b8
AQ
3089 goto out;
3090
335fbe0f
ML
3091 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3092 tvlv_tt_data->ttvn = ttvn;
7ea7b4a1
AQ
3093 tvlv_tt_data->num_vlan = htons(num_vlan);
3094
3095 /* send all the CRCs within the request. This is needed by intermediate
3096 * nodes to ensure they have the correct table before replying
3097 */
3098 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3099 for (i = 0; i < num_vlan; i++) {
3100 tt_vlan_req->vid = tt_vlan->vid;
3101 tt_vlan_req->crc = tt_vlan->crc;
3102
3103 tt_vlan_req++;
3104 tt_vlan++;
3105 }
a73105b8
AQ
3106
3107 if (full_table)
335fbe0f 3108 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 3109
bb351ba0 3110 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
335fbe0f 3111 dst_orig_node->orig, full_table ? 'F' : '.');
a73105b8 3112
d69909d2 3113 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
335fbe0f
ML
3114 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3115 dst_orig_node->orig, BATADV_TVLV_TT, 1,
7ea7b4a1 3116 tvlv_tt_data, size);
335fbe0f 3117 ret = true;
a73105b8
AQ
3118
3119out:
a73105b8 3120 if (primary_if)
82047ad7 3121 batadv_hardif_put(primary_if);
9c4604a2 3122
a73105b8 3123 if (ret && tt_req_node) {
807736f6 3124 spin_lock_bh(&bat_priv->tt.req_list_lock);
9c4604a2
SE
3125 if (!hlist_unhashed(&tt_req_node->list)) {
3126 hlist_del_init(&tt_req_node->list);
3127 batadv_tt_req_node_put(tt_req_node);
3128 }
807736f6 3129 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8 3130 }
9c4604a2
SE
3131
3132 if (tt_req_node)
3133 batadv_tt_req_node_put(tt_req_node);
3134
335fbe0f 3135 kfree(tvlv_tt_data);
a73105b8
AQ
3136 return ret;
3137}
3138
335fbe0f
ML
3139/**
3140 * batadv_send_other_tt_response - send reply to tt request concerning another
3141 * node's translation table
3142 * @bat_priv: the bat priv with all the soft interface information
3143 * @tt_data: tt data containing the tt request information
3144 * @req_src: mac address of tt request sender
3145 * @req_dst: mac address of tt request recipient
3146 *
62fe710f 3147 * Return: true if tt request reply was sent, false otherwise.
335fbe0f
ML
3148 */
3149static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3150 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 3151 u8 *req_src, u8 *req_dst)
a73105b8 3152{
170173bf 3153 struct batadv_orig_node *req_dst_orig_node;
56303d34 3154 struct batadv_orig_node *res_dst_orig_node = NULL;
7ea7b4a1 3155 struct batadv_tvlv_tt_change *tt_change;
335fbe0f 3156 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
7ea7b4a1 3157 struct batadv_tvlv_tt_vlan_data *tt_vlan;
335fbe0f 3158 bool ret = false, full_table;
6b5e971a
SE
3159 u8 orig_ttvn, req_ttvn;
3160 u16 tvlv_len;
3161 s32 tt_len;
a73105b8 3162
39c75a51 3163 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3164 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
335fbe0f 3165 req_src, tt_data->ttvn, req_dst,
a2f2b6cd 3166 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8
AQ
3167
3168 /* Let's get the orig node of the REAL destination */
335fbe0f 3169 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
a73105b8
AQ
3170 if (!req_dst_orig_node)
3171 goto out;
3172
335fbe0f 3173 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
a73105b8
AQ
3174 if (!res_dst_orig_node)
3175 goto out;
3176
6b5e971a 3177 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
335fbe0f 3178 req_ttvn = tt_data->ttvn;
a73105b8 3179
7ea7b4a1 3180 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
335fbe0f 3181 /* this node doesn't have the requested data */
a73105b8 3182 if (orig_ttvn != req_ttvn ||
7ea7b4a1
AQ
3183 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3184 ntohs(tt_data->num_vlan)))
a73105b8
AQ
3185 goto out;
3186
015758d0 3187 /* If the full table has been explicitly requested */
335fbe0f 3188 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
a73105b8
AQ
3189 !req_dst_orig_node->tt_buff)
3190 full_table = true;
3191 else
3192 full_table = false;
3193
335fbe0f
ML
3194 /* TT fragmentation hasn't been implemented yet, so send as many
3195 * TT entries fit a single packet as possible only
9cfc7bd6 3196 */
a73105b8
AQ
3197 if (!full_table) {
3198 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3199 tt_len = req_dst_orig_node->tt_buff_len;
a73105b8 3200
7ea7b4a1
AQ
3201 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3202 &tvlv_tt_data,
3203 &tt_change,
3204 &tt_len);
3205 if (!tt_len)
a73105b8
AQ
3206 goto unlock;
3207
a73105b8 3208 /* Copy the last orig_node's OGM buffer */
7ea7b4a1 3209 memcpy(tt_change, req_dst_orig_node->tt_buff,
a73105b8 3210 req_dst_orig_node->tt_buff_len);
a73105b8
AQ
3211 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3212 } else {
7ea7b4a1
AQ
3213 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3214 * in the initial part
3215 */
3216 tt_len = -1;
3217 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3218 &tvlv_tt_data,
3219 &tt_change,
3220 &tt_len);
3221 if (!tt_len)
a73105b8 3222 goto out;
7ea7b4a1
AQ
3223
3224 /* fill the rest of the tvlv with the real TT entries */
3225 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3226 tt_change, tt_len,
3227 batadv_tt_global_valid,
3228 req_dst_orig_node);
a73105b8
AQ
3229 }
3230
a19d3d85
ML
3231 /* Don't send the response, if larger than fragmented packet. */
3232 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3233 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3234 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3235 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3236 res_dst_orig_node->orig);
3237 goto out;
3238 }
3239
335fbe0f
ML
3240 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3241 tvlv_tt_data->ttvn = req_ttvn;
a73105b8
AQ
3242
3243 if (full_table)
335fbe0f 3244 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 3245
39c75a51 3246 batadv_dbg(BATADV_DBG_TT, bat_priv,
335fbe0f
ML
3247 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3248 res_dst_orig_node->orig, req_dst_orig_node->orig,
3249 full_table ? 'F' : '.', req_ttvn);
a73105b8 3250
d69909d2 3251 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 3252
335fbe0f 3253 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
7ea7b4a1
AQ
3254 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3255 tvlv_len);
e91ecfc6 3256
335fbe0f 3257 ret = true;
a73105b8
AQ
3258 goto out;
3259
3260unlock:
3261 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3262
3263out:
3264 if (res_dst_orig_node)
5d967310 3265 batadv_orig_node_put(res_dst_orig_node);
a73105b8 3266 if (req_dst_orig_node)
5d967310 3267 batadv_orig_node_put(req_dst_orig_node);
335fbe0f 3268 kfree(tvlv_tt_data);
a73105b8 3269 return ret;
a73105b8 3270}
96412690 3271
335fbe0f
ML
3272/**
3273 * batadv_send_my_tt_response - send reply to tt request concerning this node's
3274 * translation table
3275 * @bat_priv: the bat priv with all the soft interface information
3276 * @tt_data: tt data containing the tt request information
3277 * @req_src: mac address of tt request sender
3278 *
62fe710f 3279 * Return: true if tt request reply was sent, false otherwise.
335fbe0f
ML
3280 */
3281static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3282 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 3283 u8 *req_src)
a73105b8 3284{
335fbe0f 3285 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
56303d34 3286 struct batadv_hard_iface *primary_if = NULL;
7ea7b4a1
AQ
3287 struct batadv_tvlv_tt_change *tt_change;
3288 struct batadv_orig_node *orig_node;
6b5e971a
SE
3289 u8 my_ttvn, req_ttvn;
3290 u16 tvlv_len;
a73105b8 3291 bool full_table;
6b5e971a 3292 s32 tt_len;
a73105b8 3293
39c75a51 3294 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3295 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
335fbe0f 3296 req_src, tt_data->ttvn,
a2f2b6cd 3297 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8 3298
a70a9aa9 3299 spin_lock_bh(&bat_priv->tt.commit_lock);
a73105b8 3300
6b5e971a 3301 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
335fbe0f 3302 req_ttvn = tt_data->ttvn;
a73105b8 3303
335fbe0f 3304 orig_node = batadv_orig_hash_find(bat_priv, req_src);
a73105b8
AQ
3305 if (!orig_node)
3306 goto out;
3307
e5d89254 3308 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
3309 if (!primary_if)
3310 goto out;
3311
3312 /* If the full table has been explicitly requested or the gap
9cfc7bd6
SE
3313 * is too big send the whole local translation table
3314 */
335fbe0f 3315 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
807736f6 3316 !bat_priv->tt.last_changeset)
a73105b8
AQ
3317 full_table = true;
3318 else
3319 full_table = false;
3320
335fbe0f
ML
3321 /* TT fragmentation hasn't been implemented yet, so send as many
3322 * TT entries fit a single packet as possible only
9cfc7bd6 3323 */
a73105b8 3324 if (!full_table) {
807736f6 3325 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 3326
7ea7b4a1
AQ
3327 tt_len = bat_priv->tt.last_changeset_len;
3328 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3329 &tvlv_tt_data,
3330 &tt_change,
3331 &tt_len);
c2d0f48a 3332 if (!tt_len || !tvlv_len)
a73105b8
AQ
3333 goto unlock;
3334
335fbe0f 3335 /* Copy the last orig_node's OGM buffer */
7ea7b4a1 3336 memcpy(tt_change, bat_priv->tt.last_changeset,
807736f6
SE
3337 bat_priv->tt.last_changeset_len);
3338 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 3339 } else {
6b5e971a 3340 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
335fbe0f 3341
7ea7b4a1
AQ
3342 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3343 * in the initial part
3344 */
3345 tt_len = -1;
3346 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3347 &tvlv_tt_data,
3348 &tt_change,
3349 &tt_len);
c2d0f48a 3350 if (!tt_len || !tvlv_len)
a73105b8 3351 goto out;
7ea7b4a1
AQ
3352
3353 /* fill the rest of the tvlv with the real TT entries */
3354 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3355 tt_change, tt_len,
3356 batadv_tt_local_valid, NULL);
a73105b8
AQ
3357 }
3358
335fbe0f
ML
3359 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3360 tvlv_tt_data->ttvn = req_ttvn;
a73105b8
AQ
3361
3362 if (full_table)
335fbe0f 3363 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 3364
39c75a51 3365 batadv_dbg(BATADV_DBG_TT, bat_priv,
335fbe0f
ML
3366 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3367 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
a73105b8 3368
d69909d2 3369 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 3370
335fbe0f 3371 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
7ea7b4a1
AQ
3372 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3373 tvlv_len);
335fbe0f 3374
a73105b8
AQ
3375 goto out;
3376
3377unlock:
807736f6 3378 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 3379out:
a70a9aa9 3380 spin_unlock_bh(&bat_priv->tt.commit_lock);
a73105b8 3381 if (orig_node)
5d967310 3382 batadv_orig_node_put(orig_node);
a73105b8 3383 if (primary_if)
82047ad7 3384 batadv_hardif_put(primary_if);
335fbe0f
ML
3385 kfree(tvlv_tt_data);
3386 /* The packet was for this host, so it doesn't need to be re-routed */
a73105b8
AQ
3387 return true;
3388}
3389
335fbe0f
ML
3390/**
3391 * batadv_send_tt_response - send reply to tt request
3392 * @bat_priv: the bat priv with all the soft interface information
3393 * @tt_data: tt data containing the tt request information
3394 * @req_src: mac address of tt request sender
3395 * @req_dst: mac address of tt request recipient
3396 *
62fe710f 3397 * Return: true if tt request reply was sent, false otherwise.
335fbe0f
ML
3398 */
3399static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3400 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 3401 u8 *req_src, u8 *req_dst)
a73105b8 3402{
cfd4f757 3403 if (batadv_is_my_mac(bat_priv, req_dst))
335fbe0f 3404 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
24820df1
AQ
3405 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3406 req_dst);
a73105b8
AQ
3407}
3408
56303d34
SE
3409static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3410 struct batadv_orig_node *orig_node,
335fbe0f 3411 struct batadv_tvlv_tt_change *tt_change,
6b5e971a 3412 u16 tt_num_changes, u8 ttvn)
a73105b8
AQ
3413{
3414 int i;
a513088d 3415 int roams;
a73105b8
AQ
3416
3417 for (i = 0; i < tt_num_changes; i++) {
acd34afa
SE
3418 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3419 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
a513088d
SE
3420 batadv_tt_global_del(bat_priv, orig_node,
3421 (tt_change + i)->addr,
c018ad3d 3422 ntohs((tt_change + i)->vid),
d4f44692
AQ
3423 "tt removed by changes",
3424 roams);
08c36d3e 3425 } else {
08c36d3e 3426 if (!batadv_tt_global_add(bat_priv, orig_node,
d4f44692 3427 (tt_change + i)->addr,
c018ad3d 3428 ntohs((tt_change + i)->vid),
d4f44692 3429 (tt_change + i)->flags, ttvn))
a73105b8
AQ
3430 /* In case of problem while storing a
3431 * global_entry, we stop the updating
3432 * procedure without committing the
3433 * ttvn change. This will avoid to send
3434 * corrupted data on tt_request
3435 */
3436 return;
08c36d3e 3437 }
a73105b8 3438 }
ac4eebd4 3439 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
a73105b8
AQ
3440}
3441
56303d34 3442static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
7ea7b4a1 3443 struct batadv_tvlv_tt_change *tt_change,
6b5e971a
SE
3444 u8 ttvn, u8 *resp_src,
3445 u16 num_entries)
a73105b8 3446{
170173bf 3447 struct batadv_orig_node *orig_node;
a73105b8 3448
335fbe0f 3449 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
a73105b8
AQ
3450 if (!orig_node)
3451 goto out;
3452
3453 /* Purge the old table first.. */
95fb130d
AQ
3454 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3455 "Received full table");
a73105b8 3456
7ea7b4a1
AQ
3457 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3458 ttvn);
a73105b8
AQ
3459
3460 spin_lock_bh(&orig_node->tt_buff_lock);
3461 kfree(orig_node->tt_buff);
3462 orig_node->tt_buff_len = 0;
3463 orig_node->tt_buff = NULL;
3464 spin_unlock_bh(&orig_node->tt_buff_lock);
3465
7ea7b4a1 3466 atomic_set(&orig_node->last_ttvn, ttvn);
a73105b8
AQ
3467
3468out:
3469 if (orig_node)
5d967310 3470 batadv_orig_node_put(orig_node);
a73105b8
AQ
3471}
3472
56303d34
SE
3473static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3474 struct batadv_orig_node *orig_node,
6b5e971a 3475 u16 tt_num_changes, u8 ttvn,
335fbe0f 3476 struct batadv_tvlv_tt_change *tt_change)
a73105b8 3477{
a513088d
SE
3478 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3479 tt_num_changes, ttvn);
a73105b8 3480
e8cf234a
AQ
3481 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3482 batadv_tt_len(tt_num_changes));
a73105b8
AQ
3483 atomic_set(&orig_node->last_ttvn, ttvn);
3484}
3485
c018ad3d
AQ
3486/**
3487 * batadv_is_my_client - check if a client is served by the local node
3488 * @bat_priv: the bat priv with all the soft interface information
3f68785e 3489 * @addr: the mac address of the client to check
c018ad3d
AQ
3490 * @vid: VLAN identifier
3491 *
62fe710f 3492 * Return: true if the client is served by this node, false otherwise.
c018ad3d 3493 */
6b5e971a 3494bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
c018ad3d 3495 unsigned short vid)
a73105b8 3496{
170173bf 3497 struct batadv_tt_local_entry *tt_local_entry;
7683fdc1 3498 bool ret = false;
a73105b8 3499
c018ad3d 3500 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7683fdc1
AQ
3501 if (!tt_local_entry)
3502 goto out;
058d0e26 3503 /* Check if the client has been logically deleted (but is kept for
9cfc7bd6
SE
3504 * consistency purpose)
3505 */
7c1fd91d
AQ
3506 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3507 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
058d0e26 3508 goto out;
7683fdc1
AQ
3509 ret = true;
3510out:
a73105b8 3511 if (tt_local_entry)
95c0db90 3512 batadv_tt_local_entry_put(tt_local_entry);
7683fdc1 3513 return ret;
a73105b8
AQ
3514}
3515
335fbe0f
ML
3516/**
3517 * batadv_handle_tt_response - process incoming tt reply
3518 * @bat_priv: the bat priv with all the soft interface information
3519 * @tt_data: tt data containing the tt request information
3520 * @resp_src: mac address of tt reply sender
3521 * @num_entries: number of tt change entries appended to the tt data
3522 */
3523static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3524 struct batadv_tvlv_tt_data *tt_data,
6b5e971a 3525 u8 *resp_src, u16 num_entries)
a73105b8 3526{
7c26a53b
ML
3527 struct batadv_tt_req_node *node;
3528 struct hlist_node *safe;
56303d34 3529 struct batadv_orig_node *orig_node = NULL;
335fbe0f 3530 struct batadv_tvlv_tt_change *tt_change;
6b5e971a
SE
3531 u8 *tvlv_ptr = (u8 *)tt_data;
3532 u16 change_offset;
a73105b8 3533
39c75a51 3534 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3535 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
335fbe0f 3536 resp_src, tt_data->ttvn, num_entries,
a2f2b6cd 3537 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8 3538
335fbe0f 3539 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
a73105b8
AQ
3540 if (!orig_node)
3541 goto out;
3542
a70a9aa9
AQ
3543 spin_lock_bh(&orig_node->tt_lock);
3544
7ea7b4a1
AQ
3545 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3546 change_offset *= ntohs(tt_data->num_vlan);
3547 change_offset += sizeof(*tt_data);
3548 tvlv_ptr += change_offset;
3549
3550 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
335fbe0f 3551 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
7ea7b4a1
AQ
3552 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3553 resp_src, num_entries);
96412690 3554 } else {
335fbe0f
ML
3555 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3556 tt_data->ttvn, tt_change);
96412690 3557 }
a73105b8 3558
a70a9aa9 3559 /* Recalculate the CRC for this orig_node and store it */
7ea7b4a1 3560 batadv_tt_global_update_crc(bat_priv, orig_node);
a70a9aa9
AQ
3561
3562 spin_unlock_bh(&orig_node->tt_lock);
3563
a73105b8 3564 /* Delete the tt_req_node from pending tt_requests list */
807736f6 3565 spin_lock_bh(&bat_priv->tt.req_list_lock);
7c26a53b 3566 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
335fbe0f 3567 if (!batadv_compare_eth(node->addr, resp_src))
a73105b8 3568 continue;
7c26a53b 3569 hlist_del_init(&node->list);
9c4604a2 3570 batadv_tt_req_node_put(node);
a73105b8 3571 }
7ea7b4a1 3572
807736f6 3573 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
3574out:
3575 if (orig_node)
5d967310 3576 batadv_orig_node_put(orig_node);
a73105b8
AQ
3577}
3578
56303d34 3579static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
a73105b8 3580{
56303d34 3581 struct batadv_tt_roam_node *node, *safe;
a73105b8 3582
807736f6 3583 spin_lock_bh(&bat_priv->tt.roam_list_lock);
a73105b8 3584
807736f6 3585 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
cc47f66e 3586 list_del(&node->list);
86452f81 3587 kmem_cache_free(batadv_tt_roam_cache, node);
cc47f66e
AQ
3588 }
3589
807736f6 3590 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3591}
3592
56303d34 3593static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
cc47f66e 3594{
56303d34 3595 struct batadv_tt_roam_node *node, *safe;
cc47f66e 3596
807736f6
SE
3597 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3598 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
42d0b044
SE
3599 if (!batadv_has_timed_out(node->first_time,
3600 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
3601 continue;
3602
3603 list_del(&node->list);
86452f81 3604 kmem_cache_free(batadv_tt_roam_cache, node);
cc47f66e 3605 }
807736f6 3606 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3607}
3608
62fe710f 3609/**
d15cd622
AQ
3610 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3611 * @bat_priv: the bat priv with all the soft interface information
3612 * @client: mac address of the roaming client
62fe710f
SE
3613 *
3614 * This function checks whether the client already reached the
cc47f66e
AQ
3615 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3616 * will not be sent.
3617 *
62fe710f 3618 * Return: true if the ROAMING_ADV can be sent, false otherwise
9cfc7bd6 3619 */
6b5e971a 3620static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
cc47f66e 3621{
56303d34 3622 struct batadv_tt_roam_node *tt_roam_node;
cc47f66e
AQ
3623 bool ret = false;
3624
807736f6 3625 spin_lock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e 3626 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
3627 * reply from the same orig_node yet
3628 */
807736f6 3629 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
1eda58bf 3630 if (!batadv_compare_eth(tt_roam_node->addr, client))
cc47f66e
AQ
3631 continue;
3632
1eda58bf 3633 if (batadv_has_timed_out(tt_roam_node->first_time,
42d0b044 3634 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
3635 continue;
3636
3e34819e 3637 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
cc47f66e
AQ
3638 /* Sorry, you roamed too many times! */
3639 goto unlock;
3640 ret = true;
3641 break;
3642 }
3643
3644 if (!ret) {
86452f81
SE
3645 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3646 GFP_ATOMIC);
cc47f66e
AQ
3647 if (!tt_roam_node)
3648 goto unlock;
3649
3650 tt_roam_node->first_time = jiffies;
42d0b044
SE
3651 atomic_set(&tt_roam_node->counter,
3652 BATADV_ROAMING_MAX_COUNT - 1);
8fdd0153 3653 ether_addr_copy(tt_roam_node->addr, client);
cc47f66e 3654
807736f6 3655 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
cc47f66e
AQ
3656 ret = true;
3657 }
3658
3659unlock:
807736f6 3660 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3661 return ret;
3662}
3663
c018ad3d
AQ
3664/**
3665 * batadv_send_roam_adv - send a roaming advertisement message
3666 * @bat_priv: the bat priv with all the soft interface information
3667 * @client: mac address of the roaming client
3668 * @vid: VLAN identifier
3669 * @orig_node: message destination
3670 *
3671 * Send a ROAMING_ADV message to the node which was previously serving this
3672 * client. This is done to inform the node that from now on all traffic destined
3673 * for this particular roamed client has to be forwarded to the sender of the
3674 * roaming message.
3675 */
6b5e971a 3676static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
c018ad3d 3677 unsigned short vid,
56303d34 3678 struct batadv_orig_node *orig_node)
cc47f66e 3679{
56303d34 3680 struct batadv_hard_iface *primary_if;
122edaa0
ML
3681 struct batadv_tvlv_roam_adv tvlv_roam;
3682
3683 primary_if = batadv_primary_if_get_selected(bat_priv);
3684 if (!primary_if)
3685 goto out;
cc47f66e
AQ
3686
3687 /* before going on we have to check whether the client has
9cfc7bd6
SE
3688 * already roamed to us too many times
3689 */
a513088d 3690 if (!batadv_tt_check_roam_count(bat_priv, client))
cc47f66e
AQ
3691 goto out;
3692
39c75a51 3693 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 3694 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
f7a2bd65 3695 orig_node->orig, client, batadv_print_vid(vid));
cc47f66e 3696
d69909d2 3697 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
f8214865 3698
122edaa0 3699 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
c018ad3d 3700 tvlv_roam.vid = htons(vid);
122edaa0
ML
3701
3702 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3703 orig_node->orig, BATADV_TVLV_ROAM, 1,
3704 &tvlv_roam, sizeof(tvlv_roam));
cc47f66e
AQ
3705
3706out:
122edaa0 3707 if (primary_if)
82047ad7 3708 batadv_hardif_put(primary_if);
a73105b8
AQ
3709}
3710
a513088d 3711static void batadv_tt_purge(struct work_struct *work)
a73105b8 3712{
56303d34 3713 struct delayed_work *delayed_work;
807736f6 3714 struct batadv_priv_tt *priv_tt;
56303d34
SE
3715 struct batadv_priv *bat_priv;
3716
4ba4bc0f 3717 delayed_work = to_delayed_work(work);
807736f6
SE
3718 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3719 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
a73105b8 3720
a19d3d85 3721 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
30cfd02b 3722 batadv_tt_global_purge(bat_priv);
a513088d
SE
3723 batadv_tt_req_purge(bat_priv);
3724 batadv_tt_roam_purge(bat_priv);
a73105b8 3725
72414442
AQ
3726 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3727 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
a73105b8 3728}
cc47f66e 3729
56303d34 3730void batadv_tt_free(struct batadv_priv *bat_priv)
cc47f66e 3731{
e1bf0c14
ML
3732 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3733 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3734
807736f6 3735 cancel_delayed_work_sync(&bat_priv->tt.work);
cc47f66e 3736
a513088d
SE
3737 batadv_tt_local_table_free(bat_priv);
3738 batadv_tt_global_table_free(bat_priv);
3739 batadv_tt_req_list_free(bat_priv);
3740 batadv_tt_changes_list_free(bat_priv);
3741 batadv_tt_roam_list_free(bat_priv);
cc47f66e 3742
807736f6 3743 kfree(bat_priv->tt.last_changeset);
cc47f66e 3744}
058d0e26 3745
7ea7b4a1
AQ
3746/**
3747 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3748 * table and possibly count them in the TT size
3749 * @bat_priv: the bat priv with all the soft interface information
3750 * @flags: the flag to switch
3751 * @enable: whether to set or unset the flag
3752 * @count: whether to increase the TT size by the number of changed entries
9cfc7bd6 3753 */
6b5e971a
SE
3754static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3755 bool enable, bool count)
058d0e26 3756{
7ea7b4a1
AQ
3757 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3758 struct batadv_tt_common_entry *tt_common_entry;
058d0e26 3759 struct hlist_head *head;
6b5e971a 3760 u32 i;
058d0e26
AQ
3761
3762 if (!hash)
7ea7b4a1 3763 return;
058d0e26
AQ
3764
3765 for (i = 0; i < hash->size; i++) {
3766 head = &hash->table[i];
3767
3768 rcu_read_lock();
b67bfe0d 3769 hlist_for_each_entry_rcu(tt_common_entry,
058d0e26 3770 head, hash_entry) {
697f2531
AQ
3771 if (enable) {
3772 if ((tt_common_entry->flags & flags) == flags)
3773 continue;
3774 tt_common_entry->flags |= flags;
3775 } else {
3776 if (!(tt_common_entry->flags & flags))
3777 continue;
3778 tt_common_entry->flags &= ~flags;
3779 }
7ea7b4a1
AQ
3780
3781 if (!count)
3782 continue;
3783
3784 batadv_tt_local_size_inc(bat_priv,
3785 tt_common_entry->vid);
058d0e26
AQ
3786 }
3787 rcu_read_unlock();
3788 }
058d0e26
AQ
3789}
3790
acd34afa 3791/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
56303d34 3792static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
058d0e26 3793{
807736f6 3794 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34
SE
3795 struct batadv_tt_common_entry *tt_common;
3796 struct batadv_tt_local_entry *tt_local;
b67bfe0d 3797 struct hlist_node *node_tmp;
058d0e26
AQ
3798 struct hlist_head *head;
3799 spinlock_t *list_lock; /* protects write access to the hash lists */
6b5e971a 3800 u32 i;
058d0e26
AQ
3801
3802 if (!hash)
3803 return;
3804
3805 for (i = 0; i < hash->size; i++) {
3806 head = &hash->table[i];
3807 list_lock = &hash->list_locks[i];
3808
3809 spin_lock_bh(list_lock);
b67bfe0d 3810 hlist_for_each_entry_safe(tt_common, node_tmp, head,
acd34afa
SE
3811 hash_entry) {
3812 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
058d0e26
AQ
3813 continue;
3814
39c75a51 3815 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3816 "Deleting local tt entry (%pM, vid: %d): pending\n",
3817 tt_common->addr,
f7a2bd65 3818 batadv_print_vid(tt_common->vid));
058d0e26 3819
7ea7b4a1 3820 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
b67bfe0d 3821 hlist_del_rcu(&tt_common->hash_entry);
56303d34
SE
3822 tt_local = container_of(tt_common,
3823 struct batadv_tt_local_entry,
3824 common);
35df3b29 3825
95c0db90 3826 batadv_tt_local_entry_put(tt_local);
058d0e26
AQ
3827 }
3828 spin_unlock_bh(list_lock);
3829 }
058d0e26
AQ
3830}
3831
e1bf0c14 3832/**
a19d3d85
ML
3833 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3834 * which have been queued in the time since the last commit
e1bf0c14 3835 * @bat_priv: the bat priv with all the soft interface information
a19d3d85
ML
3836 *
3837 * Caller must hold tt->commit_lock.
e1bf0c14 3838 */
a19d3d85 3839static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
058d0e26 3840{
5274cd68
SE
3841 lockdep_assert_held(&bat_priv->tt.commit_lock);
3842
e1bf0c14
ML
3843 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3844 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3845 batadv_tt_tvlv_container_update(bat_priv);
a19d3d85 3846 return;
e1bf0c14 3847 }
be9aa4c1 3848
7ea7b4a1 3849 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
be9aa4c1 3850
a513088d 3851 batadv_tt_local_purge_pending_clients(bat_priv);
7ea7b4a1 3852 batadv_tt_local_update_crc(bat_priv);
058d0e26
AQ
3853
3854 /* Increment the TTVN only once per OGM interval */
807736f6 3855 atomic_inc(&bat_priv->tt.vn);
39c75a51 3856 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3857 "Local changes committed, updating to ttvn %u\n",
6b5e971a 3858 (u8)atomic_read(&bat_priv->tt.vn));
be9aa4c1
ML
3859
3860 /* reset the sending counter */
807736f6 3861 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
e1bf0c14 3862 batadv_tt_tvlv_container_update(bat_priv);
a19d3d85 3863}
a70a9aa9 3864
a19d3d85
ML
3865/**
3866 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3867 * have been queued in the time since the last commit
3868 * @bat_priv: the bat priv with all the soft interface information
3869 */
3870void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3871{
3872 spin_lock_bh(&bat_priv->tt.commit_lock);
3873 batadv_tt_local_commit_changes_nolock(bat_priv);
a70a9aa9 3874 spin_unlock_bh(&bat_priv->tt.commit_lock);
058d0e26 3875}
59b699cd 3876
6b5e971a
SE
3877bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3878 unsigned short vid)
59b699cd 3879{
393b299d
ME
3880 struct batadv_tt_local_entry *tt_local_entry;
3881 struct batadv_tt_global_entry *tt_global_entry;
b8cbd81d 3882 struct batadv_softif_vlan *vlan;
5870adc6 3883 bool ret = false;
59b699cd 3884
b8cbd81d 3885 vlan = batadv_softif_vlan_get(bat_priv, vid);
e087f34f
ME
3886 if (!vlan)
3887 return false;
3888
3889 if (!atomic_read(&vlan->ap_isolation))
393b299d 3890 goto vlan_put;
59b699cd 3891
b8cbd81d 3892 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
59b699cd 3893 if (!tt_local_entry)
393b299d 3894 goto vlan_put;
59b699cd 3895
b8cbd81d 3896 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
59b699cd 3897 if (!tt_global_entry)
393b299d 3898 goto local_entry_put;
59b699cd 3899
393b299d
ME
3900 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3901 ret = true;
59b699cd 3902
393b299d
ME
3903 batadv_tt_global_entry_put(tt_global_entry);
3904local_entry_put:
3905 batadv_tt_local_entry_put(tt_local_entry);
3906vlan_put:
9c3bf081 3907 batadv_softif_vlan_put(vlan);
59b699cd
AQ
3908 return ret;
3909}
a943cac1 3910
e1bf0c14
ML
3911/**
3912 * batadv_tt_update_orig - update global translation table with new tt
3913 * information received via ogms
3914 * @bat_priv: the bat priv with all the soft interface information
e51f0397
SE
3915 * @orig_node: the orig_node of the ogm
3916 * @tt_buff: pointer to the first tvlv VLAN entry
7ea7b4a1
AQ
3917 * @tt_num_vlan: number of tvlv VLAN entries
3918 * @tt_change: pointer to the first entry in the TT buffer
e1bf0c14
ML
3919 * @tt_num_changes: number of tt changes inside the tt buffer
3920 * @ttvn: translation table version number of this changeset
e1bf0c14
ML
3921 */
3922static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3923 struct batadv_orig_node *orig_node,
6b5e971a 3924 const void *tt_buff, u16 tt_num_vlan,
7ea7b4a1 3925 struct batadv_tvlv_tt_change *tt_change,
6b5e971a 3926 u16 tt_num_changes, u8 ttvn)
a943cac1 3927{
6b5e971a 3928 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
7ea7b4a1 3929 struct batadv_tvlv_tt_vlan_data *tt_vlan;
a943cac1 3930 bool full_table = true;
e17931d1 3931 bool has_tt_init;
a943cac1 3932
7ea7b4a1 3933 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
ac4eebd4
LL
3934 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3935 &orig_node->capa_initialized);
e17931d1 3936
17071578 3937 /* orig table not initialised AND first diff is in the OGM OR the ttvn
9cfc7bd6
SE
3938 * increased by one -> we can apply the attached changes
3939 */
e17931d1 3940 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
a943cac1 3941 /* the OGM could not contain the changes due to their size or
42d0b044
SE
3942 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3943 * times.
9cfc7bd6
SE
3944 * In this case send a tt request
3945 */
a943cac1
ML
3946 if (!tt_num_changes) {
3947 full_table = false;
3948 goto request_table;
3949 }
3950
a70a9aa9
AQ
3951 spin_lock_bh(&orig_node->tt_lock);
3952
a513088d 3953 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
96412690 3954 ttvn, tt_change);
a943cac1
ML
3955
3956 /* Even if we received the precomputed crc with the OGM, we
3957 * prefer to recompute it to spot any possible inconsistency
9cfc7bd6
SE
3958 * in the global table
3959 */
7ea7b4a1 3960 batadv_tt_global_update_crc(bat_priv, orig_node);
a943cac1 3961
a70a9aa9
AQ
3962 spin_unlock_bh(&orig_node->tt_lock);
3963
a943cac1
ML
3964 /* The ttvn alone is not enough to guarantee consistency
3965 * because a single value could represent different states
3966 * (due to the wrap around). Thus a node has to check whether
3967 * the resulting table (after applying the changes) is still
3968 * consistent or not. E.g. a node could disconnect while its
3969 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3970 * checking the CRC value is mandatory to detect the
9cfc7bd6
SE
3971 * inconsistency
3972 */
7ea7b4a1
AQ
3973 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3974 tt_num_vlan))
a943cac1 3975 goto request_table;
a943cac1
ML
3976 } else {
3977 /* if we missed more than one change or our tables are not
9cfc7bd6
SE
3978 * in sync anymore -> request fresh tt data
3979 */
e17931d1 3980 if (!has_tt_init || ttvn != orig_ttvn ||
7ea7b4a1
AQ
3981 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3982 tt_num_vlan)) {
a943cac1 3983request_table:
39c75a51 3984 batadv_dbg(BATADV_DBG_TT, bat_priv,
7ea7b4a1
AQ
3985 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3986 orig_node->orig, ttvn, orig_ttvn,
3987 tt_num_changes);
a513088d 3988 batadv_send_tt_request(bat_priv, orig_node, ttvn,
7ea7b4a1
AQ
3989 tt_vlan, tt_num_vlan,
3990 full_table);
a943cac1
ML
3991 return;
3992 }
3993 }
3994}
3275e7cc 3995
c018ad3d
AQ
3996/**
3997 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3998 * @bat_priv: the bat priv with all the soft interface information
3999 * @addr: the mac address of the client to check
4000 * @vid: VLAN identifier
4001 *
62fe710f 4002 * Return: true if we know that the client has moved from its old originator
c018ad3d
AQ
4003 * to another one. This entry is still kept for consistency purposes and will be
4004 * deleted later by a DEL or because of timeout
3275e7cc 4005 */
56303d34 4006bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
6b5e971a 4007 u8 *addr, unsigned short vid)
3275e7cc 4008{
56303d34 4009 struct batadv_tt_global_entry *tt_global_entry;
3275e7cc
AQ
4010 bool ret = false;
4011
c018ad3d 4012 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3275e7cc
AQ
4013 if (!tt_global_entry)
4014 goto out;
4015
c1d07431 4016 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
5dafd8a6 4017 batadv_tt_global_entry_put(tt_global_entry);
3275e7cc
AQ
4018out:
4019 return ret;
4020}
30cfd02b 4021
7c1fd91d
AQ
4022/**
4023 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
4024 * @bat_priv: the bat priv with all the soft interface information
c018ad3d
AQ
4025 * @addr: the mac address of the local client to query
4026 * @vid: VLAN identifier
7c1fd91d 4027 *
62fe710f 4028 * Return: true if the local client is known to be roaming (it is not served by
7c1fd91d
AQ
4029 * this node anymore) or not. If yes, the client is still present in the table
4030 * to keep the latter consistent with the node TTVN
4031 */
4032bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
6b5e971a 4033 u8 *addr, unsigned short vid)
7c1fd91d
AQ
4034{
4035 struct batadv_tt_local_entry *tt_local_entry;
4036 bool ret = false;
4037
c018ad3d 4038 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7c1fd91d
AQ
4039 if (!tt_local_entry)
4040 goto out;
4041
4042 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
95c0db90 4043 batadv_tt_local_entry_put(tt_local_entry);
7c1fd91d
AQ
4044out:
4045 return ret;
7c1fd91d
AQ
4046}
4047
30cfd02b
AQ
4048bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4049 struct batadv_orig_node *orig_node,
c018ad3d 4050 const unsigned char *addr,
16052789 4051 unsigned short vid)
30cfd02b 4052{
1227c9ae
SW
4053 /* ignore loop detect macs, they are not supposed to be in the tt local
4054 * data as well.
4055 */
4056 if (batadv_bla_is_loopdetect_mac(addr))
4057 return false;
4058
16052789 4059 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
30cfd02b
AQ
4060 BATADV_TT_CLIENT_TEMP,
4061 atomic_read(&orig_node->last_ttvn)))
75ae84a4 4062 return false;
30cfd02b
AQ
4063
4064 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 4065 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
f7a2bd65 4066 addr, batadv_print_vid(vid), orig_node->orig);
75ae84a4
SW
4067
4068 return true;
30cfd02b 4069}
e1bf0c14 4070
a19d3d85
ML
4071/**
4072 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
4073 * maximum packet size that can be transported through the mesh
4074 * @soft_iface: netdev struct of the mesh interface
4075 *
4076 * Remove entries older than 'timeout' and half timeout if more entries need
4077 * to be removed.
4078 */
4079void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4080{
4081 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4082 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4083 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4084 bool reduced = false;
4085
4086 spin_lock_bh(&bat_priv->tt.commit_lock);
4087
4088 while (true) {
4089 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4090 if (packet_size_max >= table_size)
4091 break;
4092
4093 batadv_tt_local_purge(bat_priv, timeout);
4094 batadv_tt_local_purge_pending_clients(bat_priv);
4095
4096 timeout /= 2;
4097 reduced = true;
4098 net_ratelimited_function(batadv_info, soft_iface,
4099 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4100 packet_size_max);
4101 }
4102
4103 /* commit these changes immediately, to avoid synchronization problem
4104 * with the TTVN
4105 */
4106 if (reduced)
4107 batadv_tt_local_commit_changes_nolock(bat_priv);
4108
4109 spin_unlock_bh(&bat_priv->tt.commit_lock);
4110}
4111
e1bf0c14
ML
4112/**
4113 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
4114 * @bat_priv: the bat priv with all the soft interface information
4115 * @orig: the orig_node of the ogm
4116 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4117 * @tvlv_value: tvlv buffer containing the gateway data
4118 * @tvlv_value_len: tvlv buffer length
4119 */
4120static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4121 struct batadv_orig_node *orig,
6b5e971a
SE
4122 u8 flags, void *tvlv_value,
4123 u16 tvlv_value_len)
e1bf0c14 4124{
7ea7b4a1
AQ
4125 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4126 struct batadv_tvlv_tt_change *tt_change;
e1bf0c14 4127 struct batadv_tvlv_tt_data *tt_data;
6b5e971a 4128 u16 num_entries, num_vlan;
e1bf0c14
ML
4129
4130 if (tvlv_value_len < sizeof(*tt_data))
4131 return;
4132
4133 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4134 tvlv_value_len -= sizeof(*tt_data);
4135
7ea7b4a1
AQ
4136 num_vlan = ntohs(tt_data->num_vlan);
4137
4138 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4139 return;
4140
4141 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4142 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4143 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4144
298e6e68 4145 num_entries = batadv_tt_entries(tvlv_value_len);
e1bf0c14 4146
7ea7b4a1
AQ
4147 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4148 num_entries, tt_data->ttvn);
e1bf0c14
ML
4149}
4150
335fbe0f
ML
4151/**
4152 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
4153 * container
4154 * @bat_priv: the bat priv with all the soft interface information
4155 * @src: mac address of tt tvlv sender
4156 * @dst: mac address of tt tvlv recipient
4157 * @tvlv_value: tvlv buffer containing the tt data
4158 * @tvlv_value_len: tvlv buffer length
4159 *
62fe710f 4160 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
335fbe0f
ML
4161 * otherwise.
4162 */
4163static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
6b5e971a 4164 u8 *src, u8 *dst,
335fbe0f 4165 void *tvlv_value,
6b5e971a 4166 u16 tvlv_value_len)
335fbe0f
ML
4167{
4168 struct batadv_tvlv_tt_data *tt_data;
6b5e971a 4169 u16 tt_vlan_len, tt_num_entries;
335fbe0f
ML
4170 char tt_flag;
4171 bool ret;
4172
4173 if (tvlv_value_len < sizeof(*tt_data))
4174 return NET_RX_SUCCESS;
4175
4176 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4177 tvlv_value_len -= sizeof(*tt_data);
4178
7ea7b4a1
AQ
4179 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4180 tt_vlan_len *= ntohs(tt_data->num_vlan);
4181
4182 if (tvlv_value_len < tt_vlan_len)
4183 return NET_RX_SUCCESS;
4184
4185 tvlv_value_len -= tt_vlan_len;
4186 tt_num_entries = batadv_tt_entries(tvlv_value_len);
335fbe0f
ML
4187
4188 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4189 case BATADV_TT_REQUEST:
4190 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4191
4192 /* If this node cannot provide a TT response the tt_request is
4193 * forwarded
4194 */
4195 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4196 if (!ret) {
4197 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4198 tt_flag = 'F';
4199 else
4200 tt_flag = '.';
4201
4202 batadv_dbg(BATADV_DBG_TT, bat_priv,
4203 "Routing TT_REQUEST to %pM [%c]\n",
4204 dst, tt_flag);
4205 /* tvlv API will re-route the packet */
4206 return NET_RX_DROP;
4207 }
4208 break;
4209 case BATADV_TT_RESPONSE:
4210 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4211
4212 if (batadv_is_my_mac(bat_priv, dst)) {
4213 batadv_handle_tt_response(bat_priv, tt_data,
7ea7b4a1 4214 src, tt_num_entries);
335fbe0f
ML
4215 return NET_RX_SUCCESS;
4216 }
4217
4218 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4219 tt_flag = 'F';
4220 else
4221 tt_flag = '.';
4222
4223 batadv_dbg(BATADV_DBG_TT, bat_priv,
4224 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4225
4226 /* tvlv API will re-route the packet */
4227 return NET_RX_DROP;
4228 }
4229
4230 return NET_RX_SUCCESS;
4231}
4232
122edaa0
ML
4233/**
4234 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
4235 * @bat_priv: the bat priv with all the soft interface information
4236 * @src: mac address of tt tvlv sender
4237 * @dst: mac address of tt tvlv recipient
4238 * @tvlv_value: tvlv buffer containing the tt data
4239 * @tvlv_value_len: tvlv buffer length
4240 *
62fe710f 4241 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
122edaa0
ML
4242 * otherwise.
4243 */
4244static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
6b5e971a 4245 u8 *src, u8 *dst,
122edaa0 4246 void *tvlv_value,
6b5e971a 4247 u16 tvlv_value_len)
122edaa0
ML
4248{
4249 struct batadv_tvlv_roam_adv *roaming_adv;
4250 struct batadv_orig_node *orig_node = NULL;
4251
4252 /* If this node is not the intended recipient of the
4253 * roaming advertisement the packet is forwarded
4254 * (the tvlv API will re-route the packet).
4255 */
4256 if (!batadv_is_my_mac(bat_priv, dst))
4257 return NET_RX_DROP;
4258
122edaa0
ML
4259 if (tvlv_value_len < sizeof(*roaming_adv))
4260 goto out;
4261
4262 orig_node = batadv_orig_hash_find(bat_priv, src);
4263 if (!orig_node)
4264 goto out;
4265
4266 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4267 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4268
4269 batadv_dbg(BATADV_DBG_TT, bat_priv,
4270 "Received ROAMING_ADV from %pM (client %pM)\n",
4271 src, roaming_adv->client);
4272
4273 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
c018ad3d 4274 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
122edaa0
ML
4275 atomic_read(&orig_node->last_ttvn) + 1);
4276
4277out:
4278 if (orig_node)
5d967310 4279 batadv_orig_node_put(orig_node);
122edaa0
ML
4280 return NET_RX_SUCCESS;
4281}
4282
e1bf0c14
ML
4283/**
4284 * batadv_tt_init - initialise the translation table internals
4285 * @bat_priv: the bat priv with all the soft interface information
4286 *
62fe710f 4287 * Return: 0 on success or negative error number in case of failure.
e1bf0c14
ML
4288 */
4289int batadv_tt_init(struct batadv_priv *bat_priv)
4290{
4291 int ret;
4292
0eb01568
AQ
4293 /* synchronized flags must be remote */
4294 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4295
e1bf0c14
ML
4296 ret = batadv_tt_local_init(bat_priv);
4297 if (ret < 0)
4298 return ret;
4299
4300 ret = batadv_tt_global_init(bat_priv);
4301 if (ret < 0)
4302 return ret;
4303
4304 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
335fbe0f
ML
4305 batadv_tt_tvlv_unicast_handler_v1,
4306 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
e1bf0c14 4307
122edaa0
ML
4308 batadv_tvlv_handler_register(bat_priv, NULL,
4309 batadv_roam_tvlv_unicast_handler_v1,
4310 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4311
e1bf0c14
ML
4312 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4313 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4314 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4315
4316 return 1;
4317}
42cb0bef
AQ
4318
4319/**
4320 * batadv_tt_global_is_isolated - check if a client is marked as isolated
4321 * @bat_priv: the bat priv with all the soft interface information
4322 * @addr: the mac address of the client
4323 * @vid: the identifier of the VLAN where this client is connected
4324 *
62fe710f 4325 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
42cb0bef
AQ
4326 * otherwise
4327 */
4328bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
6b5e971a 4329 const u8 *addr, unsigned short vid)
42cb0bef
AQ
4330{
4331 struct batadv_tt_global_entry *tt;
4332 bool ret;
4333
4334 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4335 if (!tt)
4336 return false;
4337
4338 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4339
5dafd8a6 4340 batadv_tt_global_entry_put(tt);
42cb0bef
AQ
4341
4342 return ret;
4343}
86452f81
SE
4344
4345/**
4346 * batadv_tt_cache_init - Initialize tt memory object cache
4347 *
4348 * Return: 0 on success or negative error number in case of failure.
4349 */
4350int __init batadv_tt_cache_init(void)
4351{
4352 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4353 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4354 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4355 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4356 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4357 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4358
4359 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4360 SLAB_HWCACHE_ALIGN, NULL);
4361 if (!batadv_tl_cache)
4362 return -ENOMEM;
4363
4364 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4365 SLAB_HWCACHE_ALIGN, NULL);
4366 if (!batadv_tg_cache)
4367 goto err_tt_tl_destroy;
4368
4369 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4370 tt_orig_size, 0,
4371 SLAB_HWCACHE_ALIGN, NULL);
4372 if (!batadv_tt_orig_cache)
4373 goto err_tt_tg_destroy;
4374
4375 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4376 tt_change_size, 0,
4377 SLAB_HWCACHE_ALIGN, NULL);
4378 if (!batadv_tt_change_cache)
4379 goto err_tt_orig_destroy;
4380
4381 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4382 tt_req_size, 0,
4383 SLAB_HWCACHE_ALIGN, NULL);
4384 if (!batadv_tt_req_cache)
4385 goto err_tt_change_destroy;
4386
4387 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4388 tt_roam_size, 0,
4389 SLAB_HWCACHE_ALIGN, NULL);
4390 if (!batadv_tt_roam_cache)
4391 goto err_tt_req_destroy;
4392
4393 return 0;
4394
4395err_tt_req_destroy:
4396 kmem_cache_destroy(batadv_tt_req_cache);
4397 batadv_tt_req_cache = NULL;
4398err_tt_change_destroy:
4399 kmem_cache_destroy(batadv_tt_change_cache);
4400 batadv_tt_change_cache = NULL;
4401err_tt_orig_destroy:
4402 kmem_cache_destroy(batadv_tt_orig_cache);
4403 batadv_tt_orig_cache = NULL;
4404err_tt_tg_destroy:
4405 kmem_cache_destroy(batadv_tg_cache);
4406 batadv_tg_cache = NULL;
4407err_tt_tl_destroy:
4408 kmem_cache_destroy(batadv_tl_cache);
4409 batadv_tl_cache = NULL;
4410
4411 return -ENOMEM;
4412}
4413
4414/**
4415 * batadv_tt_cache_destroy - Destroy tt memory object cache
4416 */
4417void batadv_tt_cache_destroy(void)
4418{
4419 kmem_cache_destroy(batadv_tl_cache);
4420 kmem_cache_destroy(batadv_tg_cache);
4421 kmem_cache_destroy(batadv_tt_orig_cache);
4422 kmem_cache_destroy(batadv_tt_change_cache);
4423 kmem_cache_destroy(batadv_tt_req_cache);
4424 kmem_cache_destroy(batadv_tt_roam_cache);
4425}