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