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