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