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