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