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