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