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