]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/batman-adv/translation-table.c
batman-adv: Start new development cycle
[mirror_ubuntu-jammy-kernel.git] / net / batman-adv / translation-table.c
CommitLineData
c6c8fea2 1/*
567db7b0 2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "translation-table.h"
24#include "soft-interface.h"
32ae9b22 25#include "hard-interface.h"
a73105b8 26#include "send.h"
c6c8fea2
SE
27#include "hash.h"
28#include "originator.h"
a73105b8 29#include "routing.h"
c6c8fea2 30
a73105b8
AQ
31#include <linux/crc16.h>
32
33static void _tt_global_del(struct bat_priv *bat_priv,
34 struct tt_global_entry *tt_global_entry,
35 const char *message);
36static void tt_purge(struct work_struct *work);
c6c8fea2 37
7aadf889 38/* returns 1 if they are the same mac addr */
48100bac 39static int compare_tt(const struct hlist_node *node, const void *data2)
7aadf889 40{
48100bac 41 const void *data1 = container_of(node, struct tt_common_entry,
747e4221 42 hash_entry);
7aadf889
ML
43
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45}
46
a73105b8 47static void tt_start_timer(struct bat_priv *bat_priv)
c6c8fea2 48{
a73105b8
AQ
49 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
50 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
51 msecs_to_jiffies(5000));
c6c8fea2
SE
52}
53
48100bac
AQ
54static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
55 const void *data)
7aadf889 56{
7aadf889
ML
57 struct hlist_head *head;
58 struct hlist_node *node;
48100bac 59 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
c90681b8 60 uint32_t index;
7aadf889
ML
61
62 if (!hash)
63 return NULL;
64
65 index = choose_orig(data, hash->size);
66 head = &hash->table[index];
67
68 rcu_read_lock();
48100bac
AQ
69 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
70 if (!compare_eth(tt_common_entry, data))
7aadf889
ML
71 continue;
72
48100bac 73 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
7683fdc1
AQ
74 continue;
75
48100bac 76 tt_common_entry_tmp = tt_common_entry;
7aadf889
ML
77 break;
78 }
79 rcu_read_unlock();
80
48100bac 81 return tt_common_entry_tmp;
7aadf889
ML
82}
83
48100bac
AQ
84static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
85 const void *data)
7aadf889 86{
48100bac
AQ
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
7aadf889 89
48100bac
AQ
90 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
91 if (tt_common_entry)
92 tt_local_entry = container_of(tt_common_entry,
93 struct tt_local_entry, common);
94 return tt_local_entry;
95}
7aadf889 96
48100bac
AQ
97static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
98 const void *data)
99{
100 struct tt_common_entry *tt_common_entry;
101 struct tt_global_entry *tt_global_entry = NULL;
7683fdc1 102
48100bac
AQ
103 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
104 if (tt_common_entry)
105 tt_global_entry = container_of(tt_common_entry,
106 struct tt_global_entry, common);
107 return tt_global_entry;
7aadf889 108
7aadf889
ML
109}
110
7683fdc1
AQ
111static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
112{
48100bac
AQ
113 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
114 kfree_rcu(tt_local_entry, common.rcu);
7683fdc1
AQ
115}
116
531027fc
SW
117static void tt_global_entry_free_rcu(struct rcu_head *rcu)
118{
48100bac 119 struct tt_common_entry *tt_common_entry;
531027fc
SW
120 struct tt_global_entry *tt_global_entry;
121
48100bac
AQ
122 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
123 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
124 common);
531027fc
SW
125
126 if (tt_global_entry->orig_node)
127 orig_node_free_ref(tt_global_entry->orig_node);
128
129 kfree(tt_global_entry);
130}
131
7683fdc1
AQ
132static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
133{
48100bac
AQ
134 if (atomic_dec_and_test(&tt_global_entry->common.refcount))
135 call_rcu(&tt_global_entry->common.rcu,
136 tt_global_entry_free_rcu);
7683fdc1
AQ
137}
138
ff66c975
AQ
139static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
140 uint8_t flags)
a73105b8
AQ
141{
142 struct tt_change_node *tt_change_node;
143
144 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
145
146 if (!tt_change_node)
147 return;
148
ff66c975 149 tt_change_node->change.flags = flags;
a73105b8
AQ
150 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
151
152 spin_lock_bh(&bat_priv->tt_changes_list_lock);
153 /* track the change in the OGMinterval list */
154 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
155 atomic_inc(&bat_priv->tt_local_changes);
156 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
157
158 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
159}
160
161int tt_len(int changes_num)
162{
163 return changes_num * sizeof(struct tt_change);
164}
165
166static int tt_local_init(struct bat_priv *bat_priv)
c6c8fea2 167{
2dafb49d 168 if (bat_priv->tt_local_hash)
c6c8fea2
SE
169 return 1;
170
2dafb49d 171 bat_priv->tt_local_hash = hash_new(1024);
c6c8fea2 172
2dafb49d 173 if (!bat_priv->tt_local_hash)
c6c8fea2
SE
174 return 0;
175
c6c8fea2
SE
176 return 1;
177}
178
bc279080
AQ
179void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
180 int ifindex)
c6c8fea2
SE
181{
182 struct bat_priv *bat_priv = netdev_priv(soft_iface);
7683fdc1
AQ
183 struct tt_local_entry *tt_local_entry = NULL;
184 struct tt_global_entry *tt_global_entry = NULL;
80b3f58c 185 int hash_added;
c6c8fea2 186
2dafb49d 187 tt_local_entry = tt_local_hash_find(bat_priv, addr);
c6c8fea2 188
2dafb49d
AQ
189 if (tt_local_entry) {
190 tt_local_entry->last_seen = jiffies;
7683fdc1 191 goto out;
c6c8fea2
SE
192 }
193
704509b8 194 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
2dafb49d 195 if (!tt_local_entry)
7683fdc1 196 goto out;
a73105b8 197
a73105b8
AQ
198 bat_dbg(DBG_TT, bat_priv,
199 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
200 (uint8_t)atomic_read(&bat_priv->ttvn));
c6c8fea2 201
48100bac
AQ
202 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
203 tt_local_entry->common.flags = NO_FLAGS;
bc279080 204 if (is_wifi_iface(ifindex))
48100bac
AQ
205 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
206 atomic_set(&tt_local_entry->common.refcount, 2);
207 tt_local_entry->last_seen = jiffies;
c6c8fea2
SE
208
209 /* the batman interface mac address should never be purged */
39901e71 210 if (compare_eth(addr, soft_iface->dev_addr))
48100bac 211 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
c6c8fea2 212
c40ed2bf
AQ
213 /* The local entry has to be marked as NEW to avoid to send it in
214 * a full table response going out before the next ttvn increment
215 * (consistency check) */
216 tt_local_entry->common.flags |= TT_CLIENT_NEW;
217
80b3f58c
SW
218 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
219 &tt_local_entry->common,
220 &tt_local_entry->common.hash_entry);
221
222 if (unlikely(hash_added != 0)) {
223 /* remove the reference for the hash */
224 tt_local_entry_free_ref(tt_local_entry);
225 goto out;
226 }
227
48100bac 228 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
ff66c975 229
c6c8fea2 230 /* remove address from global hash if present */
2dafb49d 231 tt_global_entry = tt_global_hash_find(bat_priv, addr);
c6c8fea2 232
cc47f66e
AQ
233 /* Check whether it is a roaming! */
234 if (tt_global_entry) {
cc47f66e
AQ
235 /* This node is probably going to update its tt table */
236 tt_global_entry->orig_node->tt_poss_change = true;
03fc3070 237 /* The global entry has to be marked as ROAMING and has to be
980d55b2 238 * kept for consistency purpose */
220b07e9 239 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
03fc3070 240 tt_global_entry->roam_at = jiffies;
48100bac 241 send_roam_adv(bat_priv, tt_global_entry->common.addr,
7683fdc1
AQ
242 tt_global_entry->orig_node);
243 }
244out:
245 if (tt_local_entry)
246 tt_local_entry_free_ref(tt_local_entry);
247 if (tt_global_entry)
248 tt_global_entry_free_ref(tt_global_entry);
c6c8fea2
SE
249}
250
a73105b8
AQ
251int tt_changes_fill_buffer(struct bat_priv *bat_priv,
252 unsigned char *buff, int buff_len)
c6c8fea2 253{
a73105b8
AQ
254 int count = 0, tot_changes = 0;
255 struct tt_change_node *entry, *safe;
c6c8fea2 256
a73105b8
AQ
257 if (buff_len > 0)
258 tot_changes = buff_len / tt_len(1);
c6c8fea2 259
a73105b8
AQ
260 spin_lock_bh(&bat_priv->tt_changes_list_lock);
261 atomic_set(&bat_priv->tt_local_changes, 0);
c6c8fea2 262
a73105b8
AQ
263 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
264 list) {
265 if (count < tot_changes) {
266 memcpy(buff + tt_len(count),
267 &entry->change, sizeof(struct tt_change));
c6c8fea2
SE
268 count++;
269 }
a73105b8
AQ
270 list_del(&entry->list);
271 kfree(entry);
c6c8fea2 272 }
a73105b8
AQ
273 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
274
275 /* Keep the buffer for possible tt_request */
276 spin_lock_bh(&bat_priv->tt_buff_lock);
277 kfree(bat_priv->tt_buff);
278 bat_priv->tt_buff_len = 0;
279 bat_priv->tt_buff = NULL;
280 /* We check whether this new OGM has no changes due to size
281 * problems */
282 if (buff_len > 0) {
283 /**
284 * if kmalloc() fails we will reply with the full table
285 * instead of providing the diff
286 */
287 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
288 if (bat_priv->tt_buff) {
289 memcpy(bat_priv->tt_buff, buff, buff_len);
290 bat_priv->tt_buff_len = buff_len;
291 }
292 }
293 spin_unlock_bh(&bat_priv->tt_buff_lock);
c6c8fea2 294
a73105b8 295 return tot_changes;
c6c8fea2
SE
296}
297
2dafb49d 298int tt_local_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
299{
300 struct net_device *net_dev = (struct net_device *)seq->private;
301 struct bat_priv *bat_priv = netdev_priv(net_dev);
2dafb49d 302 struct hashtable_t *hash = bat_priv->tt_local_hash;
48100bac 303 struct tt_common_entry *tt_common_entry;
32ae9b22 304 struct hard_iface *primary_if;
7aadf889 305 struct hlist_node *node;
c6c8fea2 306 struct hlist_head *head;
c90681b8
AQ
307 uint32_t i;
308 int ret = 0;
32ae9b22
ML
309
310 primary_if = primary_if_get_selected(bat_priv);
311 if (!primary_if) {
312 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
313 "please specify interfaces to enable it\n",
314 net_dev->name);
315 goto out;
316 }
c6c8fea2 317
32ae9b22
ML
318 if (primary_if->if_status != IF_ACTIVE) {
319 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
320 "primary interface not active\n",
321 net_dev->name);
322 goto out;
c6c8fea2
SE
323 }
324
325 seq_printf(seq, "Locally retrieved addresses (from %s) "
a73105b8
AQ
326 "announced via TT (TTVN: %u):\n",
327 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
c6c8fea2 328
c6c8fea2
SE
329 for (i = 0; i < hash->size; i++) {
330 head = &hash->table[i];
331
7aadf889 332 rcu_read_lock();
48100bac 333 hlist_for_each_entry_rcu(tt_common_entry, node,
7aadf889 334 head, hash_entry) {
d099c2c5 335 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
48100bac
AQ
336 tt_common_entry->addr,
337 (tt_common_entry->flags &
df6edb9e 338 TT_CLIENT_ROAM ? 'R' : '.'),
48100bac 339 (tt_common_entry->flags &
df6edb9e 340 TT_CLIENT_NOPURGE ? 'P' : '.'),
48100bac 341 (tt_common_entry->flags &
df6edb9e 342 TT_CLIENT_NEW ? 'N' : '.'),
48100bac 343 (tt_common_entry->flags &
df6edb9e 344 TT_CLIENT_PENDING ? 'X' : '.'),
48100bac 345 (tt_common_entry->flags &
df6edb9e 346 TT_CLIENT_WIFI ? 'W' : '.'));
c6c8fea2 347 }
7aadf889 348 rcu_read_unlock();
c6c8fea2 349 }
32ae9b22
ML
350out:
351 if (primary_if)
352 hardif_free_ref(primary_if);
353 return ret;
c6c8fea2
SE
354}
355
058d0e26
AQ
356static void tt_local_set_pending(struct bat_priv *bat_priv,
357 struct tt_local_entry *tt_local_entry,
c566dbbe 358 uint16_t flags, const char *message)
c6c8fea2 359{
48100bac
AQ
360 tt_local_event(bat_priv, tt_local_entry->common.addr,
361 tt_local_entry->common.flags | flags);
a73105b8 362
015758d0
AQ
363 /* The local client has to be marked as "pending to be removed" but has
364 * to be kept in the table in order to send it in a full table
058d0e26 365 * response issued before the net ttvn increment (consistency check) */
48100bac 366 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
c566dbbe
AQ
367
368 bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
369 "%s\n", tt_local_entry->common.addr, message);
c6c8fea2
SE
370}
371
a73105b8 372void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
cc47f66e 373 const char *message, bool roaming)
c6c8fea2 374{
7683fdc1 375 struct tt_local_entry *tt_local_entry = NULL;
c6c8fea2 376
2dafb49d 377 tt_local_entry = tt_local_hash_find(bat_priv, addr);
7683fdc1
AQ
378 if (!tt_local_entry)
379 goto out;
380
058d0e26 381 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
c566dbbe 382 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
7683fdc1
AQ
383out:
384 if (tt_local_entry)
385 tt_local_entry_free_ref(tt_local_entry);
c6c8fea2
SE
386}
387
a73105b8 388static void tt_local_purge(struct bat_priv *bat_priv)
c6c8fea2 389{
2dafb49d
AQ
390 struct hashtable_t *hash = bat_priv->tt_local_hash;
391 struct tt_local_entry *tt_local_entry;
48100bac 392 struct tt_common_entry *tt_common_entry;
7aadf889 393 struct hlist_node *node, *node_tmp;
c6c8fea2 394 struct hlist_head *head;
7683fdc1 395 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 396 uint32_t i;
c6c8fea2 397
c6c8fea2
SE
398 for (i = 0; i < hash->size; i++) {
399 head = &hash->table[i];
7683fdc1 400 list_lock = &hash->list_locks[i];
c6c8fea2 401
7683fdc1 402 spin_lock_bh(list_lock);
48100bac 403 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
7aadf889 404 head, hash_entry) {
48100bac
AQ
405 tt_local_entry = container_of(tt_common_entry,
406 struct tt_local_entry,
407 common);
408 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
7aadf889 409 continue;
c6c8fea2 410
058d0e26 411 /* entry already marked for deletion */
48100bac 412 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
058d0e26
AQ
413 continue;
414
a04ccd59 415 if (!has_timed_out(tt_local_entry->last_seen,
032b7969 416 TT_LOCAL_TIMEOUT))
7aadf889
ML
417 continue;
418
058d0e26 419 tt_local_set_pending(bat_priv, tt_local_entry,
c566dbbe 420 TT_CLIENT_DEL, "timed out");
c6c8fea2 421 }
7683fdc1 422 spin_unlock_bh(list_lock);
c6c8fea2
SE
423 }
424
c6c8fea2
SE
425}
426
a73105b8 427static void tt_local_table_free(struct bat_priv *bat_priv)
c6c8fea2 428{
a73105b8 429 struct hashtable_t *hash;
a73105b8 430 spinlock_t *list_lock; /* protects write access to the hash lists */
48100bac 431 struct tt_common_entry *tt_common_entry;
a73105b8 432 struct tt_local_entry *tt_local_entry;
7683fdc1
AQ
433 struct hlist_node *node, *node_tmp;
434 struct hlist_head *head;
c90681b8 435 uint32_t i;
a73105b8 436
2dafb49d 437 if (!bat_priv->tt_local_hash)
c6c8fea2
SE
438 return;
439
a73105b8
AQ
440 hash = bat_priv->tt_local_hash;
441
442 for (i = 0; i < hash->size; i++) {
443 head = &hash->table[i];
444 list_lock = &hash->list_locks[i];
445
446 spin_lock_bh(list_lock);
48100bac 447 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
a73105b8
AQ
448 head, hash_entry) {
449 hlist_del_rcu(node);
48100bac
AQ
450 tt_local_entry = container_of(tt_common_entry,
451 struct tt_local_entry,
452 common);
7683fdc1 453 tt_local_entry_free_ref(tt_local_entry);
a73105b8
AQ
454 }
455 spin_unlock_bh(list_lock);
456 }
457
458 hash_destroy(hash);
459
2dafb49d 460 bat_priv->tt_local_hash = NULL;
c6c8fea2
SE
461}
462
a73105b8 463static int tt_global_init(struct bat_priv *bat_priv)
c6c8fea2 464{
2dafb49d 465 if (bat_priv->tt_global_hash)
c6c8fea2
SE
466 return 1;
467
2dafb49d 468 bat_priv->tt_global_hash = hash_new(1024);
c6c8fea2 469
2dafb49d 470 if (!bat_priv->tt_global_hash)
c6c8fea2
SE
471 return 0;
472
473 return 1;
474}
475
a73105b8 476static void tt_changes_list_free(struct bat_priv *bat_priv)
c6c8fea2 477{
a73105b8 478 struct tt_change_node *entry, *safe;
c6c8fea2 479
a73105b8 480 spin_lock_bh(&bat_priv->tt_changes_list_lock);
c6c8fea2 481
a73105b8
AQ
482 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
483 list) {
484 list_del(&entry->list);
485 kfree(entry);
486 }
c6c8fea2 487
a73105b8
AQ
488 atomic_set(&bat_priv->tt_local_changes, 0);
489 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
490}
c6c8fea2 491
a73105b8
AQ
492/* caller must hold orig_node refcount */
493int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
bc279080
AQ
494 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
495 bool wifi)
a73105b8
AQ
496{
497 struct tt_global_entry *tt_global_entry;
a73105b8 498 struct orig_node *orig_node_tmp;
7683fdc1 499 int ret = 0;
80b3f58c 500 int hash_added;
c6c8fea2 501
a73105b8
AQ
502 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
503
504 if (!tt_global_entry) {
505 tt_global_entry =
506 kmalloc(sizeof(*tt_global_entry),
507 GFP_ATOMIC);
508 if (!tt_global_entry)
7683fdc1
AQ
509 goto out;
510
48100bac
AQ
511 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
512 tt_global_entry->common.flags = NO_FLAGS;
513 atomic_set(&tt_global_entry->common.refcount, 2);
a73105b8
AQ
514 /* Assign the new orig_node */
515 atomic_inc(&orig_node->refcount);
2dafb49d 516 tt_global_entry->orig_node = orig_node;
a73105b8 517 tt_global_entry->ttvn = ttvn;
cc47f66e 518 tt_global_entry->roam_at = 0;
7683fdc1 519
80b3f58c
SW
520 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
521 choose_orig, &tt_global_entry->common,
522 &tt_global_entry->common.hash_entry);
523
524 if (unlikely(hash_added != 0)) {
525 /* remove the reference for the hash */
526 tt_global_entry_free_ref(tt_global_entry);
527 goto out_remove;
528 }
7683fdc1 529 atomic_inc(&orig_node->tt_size);
a73105b8
AQ
530 } else {
531 if (tt_global_entry->orig_node != orig_node) {
532 atomic_dec(&tt_global_entry->orig_node->tt_size);
533 orig_node_tmp = tt_global_entry->orig_node;
534 atomic_inc(&orig_node->refcount);
535 tt_global_entry->orig_node = orig_node;
a73105b8
AQ
536 orig_node_free_ref(orig_node_tmp);
537 atomic_inc(&orig_node->tt_size);
538 }
48100bac 539 tt_global_entry->common.flags = NO_FLAGS;
cc47f66e 540 tt_global_entry->ttvn = ttvn;
cc47f66e 541 tt_global_entry->roam_at = 0;
a73105b8 542 }
c6c8fea2 543
bc279080 544 if (wifi)
48100bac 545 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
bc279080 546
a73105b8
AQ
547 bat_dbg(DBG_TT, bat_priv,
548 "Creating new global tt entry: %pM (via %pM)\n",
48100bac 549 tt_global_entry->common.addr, orig_node->orig);
c6c8fea2 550
80b3f58c 551out_remove:
a73105b8 552 /* remove address from local hash if present */
48100bac 553 tt_local_remove(bat_priv, tt_global_entry->common.addr,
7683fdc1
AQ
554 "global tt received", roaming);
555 ret = 1;
556out:
557 if (tt_global_entry)
558 tt_global_entry_free_ref(tt_global_entry);
559 return ret;
c6c8fea2
SE
560}
561
2dafb49d 562int tt_global_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
563{
564 struct net_device *net_dev = (struct net_device *)seq->private;
565 struct bat_priv *bat_priv = netdev_priv(net_dev);
2dafb49d 566 struct hashtable_t *hash = bat_priv->tt_global_hash;
48100bac 567 struct tt_common_entry *tt_common_entry;
2dafb49d 568 struct tt_global_entry *tt_global_entry;
32ae9b22 569 struct hard_iface *primary_if;
7aadf889 570 struct hlist_node *node;
c6c8fea2 571 struct hlist_head *head;
c90681b8
AQ
572 uint32_t i;
573 int ret = 0;
32ae9b22
ML
574
575 primary_if = primary_if_get_selected(bat_priv);
576 if (!primary_if) {
577 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
578 "specify interfaces to enable it\n",
579 net_dev->name);
580 goto out;
581 }
c6c8fea2 582
32ae9b22
ML
583 if (primary_if->if_status != IF_ACTIVE) {
584 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
585 "primary interface not active\n",
586 net_dev->name);
587 goto out;
c6c8fea2
SE
588 }
589
2dafb49d
AQ
590 seq_printf(seq,
591 "Globally announced TT entries received via the mesh %s\n",
c6c8fea2 592 net_dev->name);
df6edb9e
AQ
593 seq_printf(seq, " %-13s %s %-15s %s %s\n",
594 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
c6c8fea2 595
c6c8fea2
SE
596 for (i = 0; i < hash->size; i++) {
597 head = &hash->table[i];
598
7aadf889 599 rcu_read_lock();
48100bac 600 hlist_for_each_entry_rcu(tt_common_entry, node,
7aadf889 601 head, hash_entry) {
48100bac
AQ
602 tt_global_entry = container_of(tt_common_entry,
603 struct tt_global_entry,
604 common);
d099c2c5 605 seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
78d6942a 606 "[%c%c]\n",
48100bac 607 tt_global_entry->common.addr,
a73105b8
AQ
608 tt_global_entry->ttvn,
609 tt_global_entry->orig_node->orig,
610 (uint8_t) atomic_read(
611 &tt_global_entry->orig_node->
df6edb9e 612 last_ttvn),
48100bac 613 (tt_global_entry->common.flags &
df6edb9e 614 TT_CLIENT_ROAM ? 'R' : '.'),
48100bac 615 (tt_global_entry->common.flags &
df6edb9e 616 TT_CLIENT_WIFI ? 'W' : '.'));
c6c8fea2 617 }
7aadf889 618 rcu_read_unlock();
c6c8fea2 619 }
32ae9b22
ML
620out:
621 if (primary_if)
622 hardif_free_ref(primary_if);
623 return ret;
c6c8fea2
SE
624}
625
a73105b8
AQ
626static void _tt_global_del(struct bat_priv *bat_priv,
627 struct tt_global_entry *tt_global_entry,
628 const char *message)
c6c8fea2 629{
a73105b8 630 if (!tt_global_entry)
7683fdc1 631 goto out;
a73105b8
AQ
632
633 bat_dbg(DBG_TT, bat_priv,
2dafb49d 634 "Deleting global tt entry %pM (via %pM): %s\n",
48100bac 635 tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
c6c8fea2
SE
636 message);
637
a73105b8 638 atomic_dec(&tt_global_entry->orig_node->tt_size);
7683fdc1 639
48100bac
AQ
640 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
641 tt_global_entry->common.addr);
7683fdc1
AQ
642out:
643 if (tt_global_entry)
644 tt_global_entry_free_ref(tt_global_entry);
c6c8fea2
SE
645}
646
a73105b8
AQ
647void tt_global_del(struct bat_priv *bat_priv,
648 struct orig_node *orig_node, const unsigned char *addr,
cc47f66e 649 const char *message, bool roaming)
a73105b8 650{
7683fdc1 651 struct tt_global_entry *tt_global_entry = NULL;
797399b4 652 struct tt_local_entry *tt_local_entry = NULL;
a73105b8 653
a73105b8 654 tt_global_entry = tt_global_hash_find(bat_priv, addr);
92f90f56 655 if (!tt_global_entry || tt_global_entry->orig_node != orig_node)
7683fdc1 656 goto out;
a73105b8 657
92f90f56
SE
658 if (!roaming)
659 goto out_del;
660
661 /* if we are deleting a global entry due to a roam
662 * event, there are two possibilities:
663 * 1) the client roamed from node A to node B => we mark
664 * it with TT_CLIENT_ROAM, we start a timer and we
665 * wait for node B to claim it. In case of timeout
666 * the entry is purged.
667 * 2) the client roamed to us => we can directly delete
668 * the global entry, since it is useless now. */
669 tt_local_entry = tt_local_hash_find(bat_priv,
670 tt_global_entry->common.addr);
671 if (!tt_local_entry) {
672 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
673 tt_global_entry->roam_at = jiffies;
674 goto out;
a73105b8 675 }
92f90f56
SE
676
677out_del:
678 _tt_global_del(bat_priv, tt_global_entry, message);
679
cc47f66e 680out:
7683fdc1
AQ
681 if (tt_global_entry)
682 tt_global_entry_free_ref(tt_global_entry);
797399b4
AQ
683 if (tt_local_entry)
684 tt_local_entry_free_ref(tt_local_entry);
a73105b8
AQ
685}
686
2dafb49d 687void tt_global_del_orig(struct bat_priv *bat_priv,
a73105b8 688 struct orig_node *orig_node, const char *message)
c6c8fea2 689{
2dafb49d 690 struct tt_global_entry *tt_global_entry;
48100bac 691 struct tt_common_entry *tt_common_entry;
c90681b8 692 uint32_t i;
a73105b8
AQ
693 struct hashtable_t *hash = bat_priv->tt_global_hash;
694 struct hlist_node *node, *safe;
695 struct hlist_head *head;
7683fdc1 696 spinlock_t *list_lock; /* protects write access to the hash lists */
c6c8fea2 697
6e801494
SW
698 if (!hash)
699 return;
700
a73105b8
AQ
701 for (i = 0; i < hash->size; i++) {
702 head = &hash->table[i];
7683fdc1 703 list_lock = &hash->list_locks[i];
c6c8fea2 704
7683fdc1 705 spin_lock_bh(list_lock);
48100bac 706 hlist_for_each_entry_safe(tt_common_entry, node, safe,
a73105b8 707 head, hash_entry) {
48100bac
AQ
708 tt_global_entry = container_of(tt_common_entry,
709 struct tt_global_entry,
710 common);
7683fdc1
AQ
711 if (tt_global_entry->orig_node == orig_node) {
712 bat_dbg(DBG_TT, bat_priv,
713 "Deleting global tt entry %pM "
87944973 714 "(via %pM): %s\n",
48100bac 715 tt_global_entry->common.addr,
87944973
AQ
716 tt_global_entry->orig_node->orig,
717 message);
7683fdc1
AQ
718 hlist_del_rcu(node);
719 tt_global_entry_free_ref(tt_global_entry);
720 }
a73105b8 721 }
7683fdc1 722 spin_unlock_bh(list_lock);
c6c8fea2 723 }
a73105b8 724 atomic_set(&orig_node->tt_size, 0);
17071578 725 orig_node->tt_initialised = false;
c6c8fea2
SE
726}
727
cc47f66e
AQ
728static void tt_global_roam_purge(struct bat_priv *bat_priv)
729{
730 struct hashtable_t *hash = bat_priv->tt_global_hash;
48100bac 731 struct tt_common_entry *tt_common_entry;
cc47f66e
AQ
732 struct tt_global_entry *tt_global_entry;
733 struct hlist_node *node, *node_tmp;
734 struct hlist_head *head;
7683fdc1 735 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 736 uint32_t i;
cc47f66e 737
cc47f66e
AQ
738 for (i = 0; i < hash->size; i++) {
739 head = &hash->table[i];
7683fdc1 740 list_lock = &hash->list_locks[i];
cc47f66e 741
7683fdc1 742 spin_lock_bh(list_lock);
48100bac 743 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
cc47f66e 744 head, hash_entry) {
48100bac
AQ
745 tt_global_entry = container_of(tt_common_entry,
746 struct tt_global_entry,
747 common);
748 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
cc47f66e 749 continue;
a04ccd59 750 if (!has_timed_out(tt_global_entry->roam_at,
032b7969 751 TT_CLIENT_ROAM_TIMEOUT))
cc47f66e
AQ
752 continue;
753
7683fdc1
AQ
754 bat_dbg(DBG_TT, bat_priv, "Deleting global "
755 "tt entry (%pM): Roaming timeout\n",
48100bac 756 tt_global_entry->common.addr);
7683fdc1
AQ
757 atomic_dec(&tt_global_entry->orig_node->tt_size);
758 hlist_del_rcu(node);
759 tt_global_entry_free_ref(tt_global_entry);
cc47f66e 760 }
7683fdc1 761 spin_unlock_bh(list_lock);
cc47f66e
AQ
762 }
763
cc47f66e
AQ
764}
765
a73105b8 766static void tt_global_table_free(struct bat_priv *bat_priv)
c6c8fea2 767{
7683fdc1
AQ
768 struct hashtable_t *hash;
769 spinlock_t *list_lock; /* protects write access to the hash lists */
48100bac 770 struct tt_common_entry *tt_common_entry;
7683fdc1
AQ
771 struct tt_global_entry *tt_global_entry;
772 struct hlist_node *node, *node_tmp;
773 struct hlist_head *head;
c90681b8 774 uint32_t i;
7683fdc1 775
2dafb49d 776 if (!bat_priv->tt_global_hash)
c6c8fea2
SE
777 return;
778
7683fdc1
AQ
779 hash = bat_priv->tt_global_hash;
780
781 for (i = 0; i < hash->size; i++) {
782 head = &hash->table[i];
783 list_lock = &hash->list_locks[i];
784
785 spin_lock_bh(list_lock);
48100bac 786 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
7683fdc1
AQ
787 head, hash_entry) {
788 hlist_del_rcu(node);
48100bac
AQ
789 tt_global_entry = container_of(tt_common_entry,
790 struct tt_global_entry,
791 common);
7683fdc1
AQ
792 tt_global_entry_free_ref(tt_global_entry);
793 }
794 spin_unlock_bh(list_lock);
795 }
796
797 hash_destroy(hash);
798
2dafb49d 799 bat_priv->tt_global_hash = NULL;
c6c8fea2
SE
800}
801
59b699cd
AQ
802static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
803 struct tt_global_entry *tt_global_entry)
804{
805 bool ret = false;
806
48100bac
AQ
807 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
808 tt_global_entry->common.flags & TT_CLIENT_WIFI)
59b699cd
AQ
809 ret = true;
810
811 return ret;
812}
813
747e4221 814struct orig_node *transtable_search(struct bat_priv *bat_priv,
3d393e47 815 const uint8_t *src, const uint8_t *addr)
c6c8fea2 816{
3d393e47
AQ
817 struct tt_local_entry *tt_local_entry = NULL;
818 struct tt_global_entry *tt_global_entry = NULL;
7b36e8ee 819 struct orig_node *orig_node = NULL;
c6c8fea2 820
3d393e47
AQ
821 if (src && atomic_read(&bat_priv->ap_isolation)) {
822 tt_local_entry = tt_local_hash_find(bat_priv, src);
823 if (!tt_local_entry)
824 goto out;
825 }
7aadf889 826
3d393e47 827 tt_global_entry = tt_global_hash_find(bat_priv, addr);
2dafb49d 828 if (!tt_global_entry)
7b36e8ee 829 goto out;
7aadf889 830
3d393e47
AQ
831 /* check whether the clients should not communicate due to AP
832 * isolation */
833 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
834 goto out;
835
2dafb49d 836 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
3d393e47 837 goto out;
c6c8fea2 838
2dafb49d 839 orig_node = tt_global_entry->orig_node;
c6c8fea2 840
7b36e8ee 841out:
3d393e47
AQ
842 if (tt_global_entry)
843 tt_global_entry_free_ref(tt_global_entry);
844 if (tt_local_entry)
845 tt_local_entry_free_ref(tt_local_entry);
846
7b36e8ee 847 return orig_node;
c6c8fea2 848}
a73105b8
AQ
849
850/* Calculates the checksum of the local table of a given orig_node */
851uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
852{
853 uint16_t total = 0, total_one;
854 struct hashtable_t *hash = bat_priv->tt_global_hash;
48100bac 855 struct tt_common_entry *tt_common_entry;
a73105b8
AQ
856 struct tt_global_entry *tt_global_entry;
857 struct hlist_node *node;
858 struct hlist_head *head;
c90681b8
AQ
859 uint32_t i;
860 int j;
a73105b8
AQ
861
862 for (i = 0; i < hash->size; i++) {
863 head = &hash->table[i];
864
865 rcu_read_lock();
48100bac 866 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8 867 head, hash_entry) {
48100bac
AQ
868 tt_global_entry = container_of(tt_common_entry,
869 struct tt_global_entry,
870 common);
a73105b8
AQ
871 if (compare_eth(tt_global_entry->orig_node,
872 orig_node)) {
cc47f66e
AQ
873 /* Roaming clients are in the global table for
874 * consistency only. They don't have to be
875 * taken into account while computing the
876 * global crc */
48100bac 877 if (tt_common_entry->flags & TT_CLIENT_ROAM)
cc47f66e 878 continue;
a73105b8
AQ
879 total_one = 0;
880 for (j = 0; j < ETH_ALEN; j++)
881 total_one = crc16_byte(total_one,
48100bac 882 tt_common_entry->addr[j]);
a73105b8
AQ
883 total ^= total_one;
884 }
885 }
886 rcu_read_unlock();
887 }
888
889 return total;
890}
891
892/* Calculates the checksum of the local table */
893uint16_t tt_local_crc(struct bat_priv *bat_priv)
894{
895 uint16_t total = 0, total_one;
896 struct hashtable_t *hash = bat_priv->tt_local_hash;
48100bac 897 struct tt_common_entry *tt_common_entry;
a73105b8
AQ
898 struct hlist_node *node;
899 struct hlist_head *head;
c90681b8
AQ
900 uint32_t i;
901 int j;
a73105b8
AQ
902
903 for (i = 0; i < hash->size; i++) {
904 head = &hash->table[i];
905
906 rcu_read_lock();
48100bac 907 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8 908 head, hash_entry) {
058d0e26
AQ
909 /* not yet committed clients have not to be taken into
910 * account while computing the CRC */
48100bac 911 if (tt_common_entry->flags & TT_CLIENT_NEW)
058d0e26 912 continue;
a73105b8
AQ
913 total_one = 0;
914 for (j = 0; j < ETH_ALEN; j++)
915 total_one = crc16_byte(total_one,
48100bac 916 tt_common_entry->addr[j]);
a73105b8
AQ
917 total ^= total_one;
918 }
a73105b8
AQ
919 rcu_read_unlock();
920 }
921
922 return total;
923}
924
925static void tt_req_list_free(struct bat_priv *bat_priv)
926{
927 struct tt_req_node *node, *safe;
928
929 spin_lock_bh(&bat_priv->tt_req_list_lock);
930
931 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
932 list_del(&node->list);
933 kfree(node);
934 }
935
936 spin_unlock_bh(&bat_priv->tt_req_list_lock);
937}
938
939void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
940 const unsigned char *tt_buff, uint8_t tt_num_changes)
941{
942 uint16_t tt_buff_len = tt_len(tt_num_changes);
943
944 /* Replace the old buffer only if I received something in the
945 * last OGM (the OGM could carry no changes) */
946 spin_lock_bh(&orig_node->tt_buff_lock);
947 if (tt_buff_len > 0) {
948 kfree(orig_node->tt_buff);
949 orig_node->tt_buff_len = 0;
950 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
951 if (orig_node->tt_buff) {
952 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
953 orig_node->tt_buff_len = tt_buff_len;
954 }
955 }
956 spin_unlock_bh(&orig_node->tt_buff_lock);
957}
958
959static void tt_req_purge(struct bat_priv *bat_priv)
960{
961 struct tt_req_node *node, *safe;
962
963 spin_lock_bh(&bat_priv->tt_req_list_lock);
964 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
032b7969 965 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
a73105b8
AQ
966 list_del(&node->list);
967 kfree(node);
968 }
969 }
970 spin_unlock_bh(&bat_priv->tt_req_list_lock);
971}
972
973/* returns the pointer to the new tt_req_node struct if no request
974 * has already been issued for this orig_node, NULL otherwise */
975static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
976 struct orig_node *orig_node)
977{
978 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
979
980 spin_lock_bh(&bat_priv->tt_req_list_lock);
981 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
982 if (compare_eth(tt_req_node_tmp, orig_node) &&
a04ccd59 983 !has_timed_out(tt_req_node_tmp->issued_at,
032b7969 984 TT_REQUEST_TIMEOUT))
a73105b8
AQ
985 goto unlock;
986 }
987
988 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
989 if (!tt_req_node)
990 goto unlock;
991
992 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
993 tt_req_node->issued_at = jiffies;
994
995 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
996unlock:
997 spin_unlock_bh(&bat_priv->tt_req_list_lock);
998 return tt_req_node;
999}
1000
058d0e26
AQ
1001/* data_ptr is useless here, but has to be kept to respect the prototype */
1002static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1003{
48100bac 1004 const struct tt_common_entry *tt_common_entry = entry_ptr;
058d0e26 1005
48100bac 1006 if (tt_common_entry->flags & TT_CLIENT_NEW)
058d0e26
AQ
1007 return 0;
1008 return 1;
1009}
1010
a73105b8
AQ
1011static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1012{
48100bac
AQ
1013 const struct tt_common_entry *tt_common_entry = entry_ptr;
1014 const struct tt_global_entry *tt_global_entry;
a73105b8
AQ
1015 const struct orig_node *orig_node = data_ptr;
1016
48100bac 1017 if (tt_common_entry->flags & TT_CLIENT_ROAM)
cc47f66e
AQ
1018 return 0;
1019
48100bac
AQ
1020 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1021 common);
1022
a73105b8
AQ
1023 return (tt_global_entry->orig_node == orig_node);
1024}
1025
1026static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1027 struct hashtable_t *hash,
1028 struct hard_iface *primary_if,
1029 int (*valid_cb)(const void *,
1030 const void *),
1031 void *cb_data)
1032{
48100bac 1033 struct tt_common_entry *tt_common_entry;
a73105b8
AQ
1034 struct tt_query_packet *tt_response;
1035 struct tt_change *tt_change;
1036 struct hlist_node *node;
1037 struct hlist_head *head;
1038 struct sk_buff *skb = NULL;
1039 uint16_t tt_tot, tt_count;
1040 ssize_t tt_query_size = sizeof(struct tt_query_packet);
c90681b8 1041 uint32_t i;
a73105b8
AQ
1042
1043 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1044 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1045 tt_len -= tt_len % sizeof(struct tt_change);
1046 }
1047 tt_tot = tt_len / sizeof(struct tt_change);
1048
1049 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1050 if (!skb)
1051 goto out;
1052
1053 skb_reserve(skb, ETH_HLEN);
1054 tt_response = (struct tt_query_packet *)skb_put(skb,
1055 tt_query_size + tt_len);
1056 tt_response->ttvn = ttvn;
a73105b8
AQ
1057
1058 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1059 tt_count = 0;
1060
1061 rcu_read_lock();
1062 for (i = 0; i < hash->size; i++) {
1063 head = &hash->table[i];
1064
48100bac 1065 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8
AQ
1066 head, hash_entry) {
1067 if (tt_count == tt_tot)
1068 break;
1069
48100bac 1070 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
a73105b8
AQ
1071 continue;
1072
48100bac
AQ
1073 memcpy(tt_change->addr, tt_common_entry->addr,
1074 ETH_ALEN);
a73105b8
AQ
1075 tt_change->flags = NO_FLAGS;
1076
1077 tt_count++;
1078 tt_change++;
1079 }
1080 }
1081 rcu_read_unlock();
1082
9d852393
AQ
1083 /* store in the message the number of entries we have successfully
1084 * copied */
1085 tt_response->tt_data = htons(tt_count);
1086
a73105b8
AQ
1087out:
1088 return skb;
1089}
1090
a943cac1
ML
1091static int send_tt_request(struct bat_priv *bat_priv,
1092 struct orig_node *dst_orig_node,
1093 uint8_t ttvn, uint16_t tt_crc, bool full_table)
a73105b8
AQ
1094{
1095 struct sk_buff *skb = NULL;
1096 struct tt_query_packet *tt_request;
1097 struct neigh_node *neigh_node = NULL;
1098 struct hard_iface *primary_if;
1099 struct tt_req_node *tt_req_node = NULL;
1100 int ret = 1;
1101
1102 primary_if = primary_if_get_selected(bat_priv);
1103 if (!primary_if)
1104 goto out;
1105
1106 /* The new tt_req will be issued only if I'm not waiting for a
1107 * reply from the same orig_node yet */
1108 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1109 if (!tt_req_node)
1110 goto out;
1111
1112 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1113 if (!skb)
1114 goto out;
1115
1116 skb_reserve(skb, ETH_HLEN);
1117
1118 tt_request = (struct tt_query_packet *)skb_put(skb,
1119 sizeof(struct tt_query_packet));
1120
76543d14
SE
1121 tt_request->header.packet_type = BAT_TT_QUERY;
1122 tt_request->header.version = COMPAT_VERSION;
a73105b8
AQ
1123 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1124 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
76543d14 1125 tt_request->header.ttl = TTL;
a73105b8
AQ
1126 tt_request->ttvn = ttvn;
1127 tt_request->tt_data = tt_crc;
1128 tt_request->flags = TT_REQUEST;
1129
1130 if (full_table)
1131 tt_request->flags |= TT_FULL_TABLE;
1132
1133 neigh_node = orig_node_get_router(dst_orig_node);
1134 if (!neigh_node)
1135 goto out;
1136
1137 bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1138 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1139 (full_table ? 'F' : '.'));
1140
1141 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1142 ret = 0;
1143
1144out:
1145 if (neigh_node)
1146 neigh_node_free_ref(neigh_node);
1147 if (primary_if)
1148 hardif_free_ref(primary_if);
1149 if (ret)
1150 kfree_skb(skb);
1151 if (ret && tt_req_node) {
1152 spin_lock_bh(&bat_priv->tt_req_list_lock);
1153 list_del(&tt_req_node->list);
1154 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1155 kfree(tt_req_node);
1156 }
1157 return ret;
1158}
1159
1160static bool send_other_tt_response(struct bat_priv *bat_priv,
1161 struct tt_query_packet *tt_request)
1162{
1163 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1164 struct neigh_node *neigh_node = NULL;
1165 struct hard_iface *primary_if = NULL;
1166 uint8_t orig_ttvn, req_ttvn, ttvn;
1167 int ret = false;
1168 unsigned char *tt_buff;
1169 bool full_table;
1170 uint16_t tt_len, tt_tot;
1171 struct sk_buff *skb = NULL;
1172 struct tt_query_packet *tt_response;
1173
1174 bat_dbg(DBG_TT, bat_priv,
1175 "Received TT_REQUEST from %pM for "
1176 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1177 tt_request->ttvn, tt_request->dst,
1178 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1179
1180 /* Let's get the orig node of the REAL destination */
eb7e2a1e 1181 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
a73105b8
AQ
1182 if (!req_dst_orig_node)
1183 goto out;
1184
eb7e2a1e 1185 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
a73105b8
AQ
1186 if (!res_dst_orig_node)
1187 goto out;
1188
1189 neigh_node = orig_node_get_router(res_dst_orig_node);
1190 if (!neigh_node)
1191 goto out;
1192
1193 primary_if = primary_if_get_selected(bat_priv);
1194 if (!primary_if)
1195 goto out;
1196
1197 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1198 req_ttvn = tt_request->ttvn;
1199
015758d0 1200 /* I don't have the requested data */
a73105b8
AQ
1201 if (orig_ttvn != req_ttvn ||
1202 tt_request->tt_data != req_dst_orig_node->tt_crc)
1203 goto out;
1204
015758d0 1205 /* If the full table has been explicitly requested */
a73105b8
AQ
1206 if (tt_request->flags & TT_FULL_TABLE ||
1207 !req_dst_orig_node->tt_buff)
1208 full_table = true;
1209 else
1210 full_table = false;
1211
1212 /* In this version, fragmentation is not implemented, then
1213 * I'll send only one packet with as much TT entries as I can */
1214 if (!full_table) {
1215 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1216 tt_len = req_dst_orig_node->tt_buff_len;
1217 tt_tot = tt_len / sizeof(struct tt_change);
1218
1219 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1220 tt_len + ETH_HLEN);
1221 if (!skb)
1222 goto unlock;
1223
1224 skb_reserve(skb, ETH_HLEN);
1225 tt_response = (struct tt_query_packet *)skb_put(skb,
1226 sizeof(struct tt_query_packet) + tt_len);
1227 tt_response->ttvn = req_ttvn;
1228 tt_response->tt_data = htons(tt_tot);
1229
1230 tt_buff = skb->data + sizeof(struct tt_query_packet);
1231 /* Copy the last orig_node's OGM buffer */
1232 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1233 req_dst_orig_node->tt_buff_len);
1234
1235 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1236 } else {
1237 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1238 sizeof(struct tt_change);
1239 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1240
1241 skb = tt_response_fill_table(tt_len, ttvn,
1242 bat_priv->tt_global_hash,
1243 primary_if, tt_global_valid_entry,
1244 req_dst_orig_node);
1245 if (!skb)
1246 goto out;
1247
1248 tt_response = (struct tt_query_packet *)skb->data;
1249 }
1250
76543d14
SE
1251 tt_response->header.packet_type = BAT_TT_QUERY;
1252 tt_response->header.version = COMPAT_VERSION;
1253 tt_response->header.ttl = TTL;
a73105b8
AQ
1254 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1255 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1256 tt_response->flags = TT_RESPONSE;
1257
1258 if (full_table)
1259 tt_response->flags |= TT_FULL_TABLE;
1260
1261 bat_dbg(DBG_TT, bat_priv,
1262 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1263 res_dst_orig_node->orig, neigh_node->addr,
1264 req_dst_orig_node->orig, req_ttvn);
1265
1266 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1267 ret = true;
1268 goto out;
1269
1270unlock:
1271 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1272
1273out:
1274 if (res_dst_orig_node)
1275 orig_node_free_ref(res_dst_orig_node);
1276 if (req_dst_orig_node)
1277 orig_node_free_ref(req_dst_orig_node);
1278 if (neigh_node)
1279 neigh_node_free_ref(neigh_node);
1280 if (primary_if)
1281 hardif_free_ref(primary_if);
1282 if (!ret)
1283 kfree_skb(skb);
1284 return ret;
1285
1286}
1287static bool send_my_tt_response(struct bat_priv *bat_priv,
1288 struct tt_query_packet *tt_request)
1289{
1290 struct orig_node *orig_node = NULL;
1291 struct neigh_node *neigh_node = NULL;
1292 struct hard_iface *primary_if = NULL;
1293 uint8_t my_ttvn, req_ttvn, ttvn;
1294 int ret = false;
1295 unsigned char *tt_buff;
1296 bool full_table;
1297 uint16_t tt_len, tt_tot;
1298 struct sk_buff *skb = NULL;
1299 struct tt_query_packet *tt_response;
1300
1301 bat_dbg(DBG_TT, bat_priv,
1302 "Received TT_REQUEST from %pM for "
1303 "ttvn: %u (me) [%c]\n", tt_request->src,
1304 tt_request->ttvn,
1305 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1306
1307
1308 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1309 req_ttvn = tt_request->ttvn;
1310
eb7e2a1e 1311 orig_node = orig_hash_find(bat_priv, tt_request->src);
a73105b8
AQ
1312 if (!orig_node)
1313 goto out;
1314
1315 neigh_node = orig_node_get_router(orig_node);
1316 if (!neigh_node)
1317 goto out;
1318
1319 primary_if = primary_if_get_selected(bat_priv);
1320 if (!primary_if)
1321 goto out;
1322
1323 /* If the full table has been explicitly requested or the gap
1324 * is too big send the whole local translation table */
1325 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1326 !bat_priv->tt_buff)
1327 full_table = true;
1328 else
1329 full_table = false;
1330
1331 /* In this version, fragmentation is not implemented, then
1332 * I'll send only one packet with as much TT entries as I can */
1333 if (!full_table) {
1334 spin_lock_bh(&bat_priv->tt_buff_lock);
1335 tt_len = bat_priv->tt_buff_len;
1336 tt_tot = tt_len / sizeof(struct tt_change);
1337
1338 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1339 tt_len + ETH_HLEN);
1340 if (!skb)
1341 goto unlock;
1342
1343 skb_reserve(skb, ETH_HLEN);
1344 tt_response = (struct tt_query_packet *)skb_put(skb,
1345 sizeof(struct tt_query_packet) + tt_len);
1346 tt_response->ttvn = req_ttvn;
1347 tt_response->tt_data = htons(tt_tot);
1348
1349 tt_buff = skb->data + sizeof(struct tt_query_packet);
1350 memcpy(tt_buff, bat_priv->tt_buff,
1351 bat_priv->tt_buff_len);
1352 spin_unlock_bh(&bat_priv->tt_buff_lock);
1353 } else {
1354 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1355 sizeof(struct tt_change);
1356 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1357
1358 skb = tt_response_fill_table(tt_len, ttvn,
1359 bat_priv->tt_local_hash,
058d0e26
AQ
1360 primary_if, tt_local_valid_entry,
1361 NULL);
a73105b8
AQ
1362 if (!skb)
1363 goto out;
1364
1365 tt_response = (struct tt_query_packet *)skb->data;
1366 }
1367
76543d14
SE
1368 tt_response->header.packet_type = BAT_TT_QUERY;
1369 tt_response->header.version = COMPAT_VERSION;
1370 tt_response->header.ttl = TTL;
a73105b8
AQ
1371 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1372 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1373 tt_response->flags = TT_RESPONSE;
1374
1375 if (full_table)
1376 tt_response->flags |= TT_FULL_TABLE;
1377
1378 bat_dbg(DBG_TT, bat_priv,
1379 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1380 orig_node->orig, neigh_node->addr,
1381 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1382
1383 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1384 ret = true;
1385 goto out;
1386
1387unlock:
1388 spin_unlock_bh(&bat_priv->tt_buff_lock);
1389out:
1390 if (orig_node)
1391 orig_node_free_ref(orig_node);
1392 if (neigh_node)
1393 neigh_node_free_ref(neigh_node);
1394 if (primary_if)
1395 hardif_free_ref(primary_if);
1396 if (!ret)
1397 kfree_skb(skb);
1398 /* This packet was for me, so it doesn't need to be re-routed */
1399 return true;
1400}
1401
1402bool send_tt_response(struct bat_priv *bat_priv,
1403 struct tt_query_packet *tt_request)
1404{
1405 if (is_my_mac(tt_request->dst))
1406 return send_my_tt_response(bat_priv, tt_request);
1407 else
1408 return send_other_tt_response(bat_priv, tt_request);
1409}
1410
1411static void _tt_update_changes(struct bat_priv *bat_priv,
1412 struct orig_node *orig_node,
1413 struct tt_change *tt_change,
1414 uint16_t tt_num_changes, uint8_t ttvn)
1415{
1416 int i;
1417
1418 for (i = 0; i < tt_num_changes; i++) {
5fbc1598 1419 if ((tt_change + i)->flags & TT_CLIENT_DEL)
a73105b8
AQ
1420 tt_global_del(bat_priv, orig_node,
1421 (tt_change + i)->addr,
cc47f66e
AQ
1422 "tt removed by changes",
1423 (tt_change + i)->flags & TT_CLIENT_ROAM);
a73105b8
AQ
1424 else
1425 if (!tt_global_add(bat_priv, orig_node,
bc279080
AQ
1426 (tt_change + i)->addr, ttvn, false,
1427 (tt_change + i)->flags &
1428 TT_CLIENT_WIFI))
a73105b8
AQ
1429 /* In case of problem while storing a
1430 * global_entry, we stop the updating
1431 * procedure without committing the
1432 * ttvn change. This will avoid to send
1433 * corrupted data on tt_request
1434 */
1435 return;
1436 }
17071578 1437 orig_node->tt_initialised = true;
a73105b8
AQ
1438}
1439
1440static void tt_fill_gtable(struct bat_priv *bat_priv,
1441 struct tt_query_packet *tt_response)
1442{
1443 struct orig_node *orig_node = NULL;
1444
1445 orig_node = orig_hash_find(bat_priv, tt_response->src);
1446 if (!orig_node)
1447 goto out;
1448
1449 /* Purge the old table first.. */
1450 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1451
1452 _tt_update_changes(bat_priv, orig_node,
1453 (struct tt_change *)(tt_response + 1),
1454 tt_response->tt_data, tt_response->ttvn);
1455
1456 spin_lock_bh(&orig_node->tt_buff_lock);
1457 kfree(orig_node->tt_buff);
1458 orig_node->tt_buff_len = 0;
1459 orig_node->tt_buff = NULL;
1460 spin_unlock_bh(&orig_node->tt_buff_lock);
1461
1462 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1463
1464out:
1465 if (orig_node)
1466 orig_node_free_ref(orig_node);
1467}
1468
a943cac1
ML
1469static void tt_update_changes(struct bat_priv *bat_priv,
1470 struct orig_node *orig_node,
1471 uint16_t tt_num_changes, uint8_t ttvn,
1472 struct tt_change *tt_change)
a73105b8
AQ
1473{
1474 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1475 ttvn);
1476
1477 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1478 tt_num_changes);
1479 atomic_set(&orig_node->last_ttvn, ttvn);
1480}
1481
1482bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1483{
7683fdc1
AQ
1484 struct tt_local_entry *tt_local_entry = NULL;
1485 bool ret = false;
a73105b8 1486
a73105b8 1487 tt_local_entry = tt_local_hash_find(bat_priv, addr);
7683fdc1
AQ
1488 if (!tt_local_entry)
1489 goto out;
058d0e26
AQ
1490 /* Check if the client has been logically deleted (but is kept for
1491 * consistency purpose) */
48100bac 1492 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
058d0e26 1493 goto out;
7683fdc1
AQ
1494 ret = true;
1495out:
a73105b8 1496 if (tt_local_entry)
7683fdc1
AQ
1497 tt_local_entry_free_ref(tt_local_entry);
1498 return ret;
a73105b8
AQ
1499}
1500
1501void handle_tt_response(struct bat_priv *bat_priv,
1502 struct tt_query_packet *tt_response)
1503{
1504 struct tt_req_node *node, *safe;
1505 struct orig_node *orig_node = NULL;
1506
1507 bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1508 "ttvn %d t_size: %d [%c]\n",
1509 tt_response->src, tt_response->ttvn,
1510 tt_response->tt_data,
1511 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1512
1513 orig_node = orig_hash_find(bat_priv, tt_response->src);
1514 if (!orig_node)
1515 goto out;
1516
1517 if (tt_response->flags & TT_FULL_TABLE)
1518 tt_fill_gtable(bat_priv, tt_response);
1519 else
1520 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1521 tt_response->ttvn,
1522 (struct tt_change *)(tt_response + 1));
1523
1524 /* Delete the tt_req_node from pending tt_requests list */
1525 spin_lock_bh(&bat_priv->tt_req_list_lock);
1526 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1527 if (!compare_eth(node->addr, tt_response->src))
1528 continue;
1529 list_del(&node->list);
1530 kfree(node);
1531 }
1532 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1533
1534 /* Recalculate the CRC for this orig_node and store it */
a73105b8 1535 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
cc47f66e
AQ
1536 /* Roaming phase is over: tables are in sync again. I can
1537 * unset the flag */
1538 orig_node->tt_poss_change = false;
a73105b8
AQ
1539out:
1540 if (orig_node)
1541 orig_node_free_ref(orig_node);
1542}
1543
1544int tt_init(struct bat_priv *bat_priv)
1545{
1546 if (!tt_local_init(bat_priv))
1547 return 0;
1548
1549 if (!tt_global_init(bat_priv))
1550 return 0;
1551
1552 tt_start_timer(bat_priv);
1553
1554 return 1;
1555}
1556
cc47f66e 1557static void tt_roam_list_free(struct bat_priv *bat_priv)
a73105b8 1558{
cc47f66e 1559 struct tt_roam_node *node, *safe;
a73105b8 1560
cc47f66e 1561 spin_lock_bh(&bat_priv->tt_roam_list_lock);
a73105b8 1562
cc47f66e
AQ
1563 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1564 list_del(&node->list);
1565 kfree(node);
1566 }
1567
1568 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1569}
1570
1571static void tt_roam_purge(struct bat_priv *bat_priv)
1572{
1573 struct tt_roam_node *node, *safe;
1574
1575 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1576 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
032b7969 1577 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
cc47f66e
AQ
1578 continue;
1579
1580 list_del(&node->list);
1581 kfree(node);
1582 }
1583 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1584}
1585
1586/* This function checks whether the client already reached the
1587 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1588 * will not be sent.
1589 *
1590 * returns true if the ROAMING_ADV can be sent, false otherwise */
1591static bool tt_check_roam_count(struct bat_priv *bat_priv,
1592 uint8_t *client)
1593{
1594 struct tt_roam_node *tt_roam_node;
1595 bool ret = false;
1596
1597 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1598 /* The new tt_req will be issued only if I'm not waiting for a
1599 * reply from the same orig_node yet */
1600 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1601 if (!compare_eth(tt_roam_node->addr, client))
1602 continue;
1603
032b7969 1604 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
cc47f66e
AQ
1605 continue;
1606
1607 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1608 /* Sorry, you roamed too many times! */
1609 goto unlock;
1610 ret = true;
1611 break;
1612 }
1613
1614 if (!ret) {
1615 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1616 if (!tt_roam_node)
1617 goto unlock;
1618
1619 tt_roam_node->first_time = jiffies;
1620 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1621 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1622
1623 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1624 ret = true;
1625 }
1626
1627unlock:
1628 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1629 return ret;
1630}
1631
1632void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1633 struct orig_node *orig_node)
1634{
1635 struct neigh_node *neigh_node = NULL;
1636 struct sk_buff *skb = NULL;
1637 struct roam_adv_packet *roam_adv_packet;
1638 int ret = 1;
1639 struct hard_iface *primary_if;
1640
1641 /* before going on we have to check whether the client has
1642 * already roamed to us too many times */
1643 if (!tt_check_roam_count(bat_priv, client))
1644 goto out;
1645
1646 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1647 if (!skb)
1648 goto out;
1649
1650 skb_reserve(skb, ETH_HLEN);
1651
1652 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1653 sizeof(struct roam_adv_packet));
1654
76543d14
SE
1655 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1656 roam_adv_packet->header.version = COMPAT_VERSION;
1657 roam_adv_packet->header.ttl = TTL;
cc47f66e
AQ
1658 primary_if = primary_if_get_selected(bat_priv);
1659 if (!primary_if)
1660 goto out;
1661 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1662 hardif_free_ref(primary_if);
1663 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1664 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1665
1666 neigh_node = orig_node_get_router(orig_node);
1667 if (!neigh_node)
1668 goto out;
1669
1670 bat_dbg(DBG_TT, bat_priv,
1671 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1672 orig_node->orig, client, neigh_node->addr);
1673
1674 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1675 ret = 0;
1676
1677out:
1678 if (neigh_node)
1679 neigh_node_free_ref(neigh_node);
1680 if (ret)
1681 kfree_skb(skb);
1682 return;
a73105b8
AQ
1683}
1684
1685static void tt_purge(struct work_struct *work)
1686{
1687 struct delayed_work *delayed_work =
1688 container_of(work, struct delayed_work, work);
1689 struct bat_priv *bat_priv =
1690 container_of(delayed_work, struct bat_priv, tt_work);
1691
1692 tt_local_purge(bat_priv);
cc47f66e 1693 tt_global_roam_purge(bat_priv);
a73105b8 1694 tt_req_purge(bat_priv);
cc47f66e 1695 tt_roam_purge(bat_priv);
a73105b8
AQ
1696
1697 tt_start_timer(bat_priv);
1698}
cc47f66e
AQ
1699
1700void tt_free(struct bat_priv *bat_priv)
1701{
1702 cancel_delayed_work_sync(&bat_priv->tt_work);
1703
1704 tt_local_table_free(bat_priv);
1705 tt_global_table_free(bat_priv);
1706 tt_req_list_free(bat_priv);
1707 tt_changes_list_free(bat_priv);
1708 tt_roam_list_free(bat_priv);
1709
1710 kfree(bat_priv->tt_buff);
1711}
058d0e26 1712
697f2531
AQ
1713/* This function will enable or disable the specified flags for all the entries
1714 * in the given hash table and returns the number of modified entries */
1715static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1716 bool enable)
058d0e26 1717{
c90681b8 1718 uint32_t i;
697f2531 1719 uint16_t changed_num = 0;
058d0e26
AQ
1720 struct hlist_head *head;
1721 struct hlist_node *node;
48100bac 1722 struct tt_common_entry *tt_common_entry;
058d0e26
AQ
1723
1724 if (!hash)
697f2531 1725 goto out;
058d0e26
AQ
1726
1727 for (i = 0; i < hash->size; i++) {
1728 head = &hash->table[i];
1729
1730 rcu_read_lock();
48100bac 1731 hlist_for_each_entry_rcu(tt_common_entry, node,
058d0e26 1732 head, hash_entry) {
697f2531
AQ
1733 if (enable) {
1734 if ((tt_common_entry->flags & flags) == flags)
1735 continue;
1736 tt_common_entry->flags |= flags;
1737 } else {
1738 if (!(tt_common_entry->flags & flags))
1739 continue;
1740 tt_common_entry->flags &= ~flags;
1741 }
1742 changed_num++;
058d0e26
AQ
1743 }
1744 rcu_read_unlock();
1745 }
697f2531
AQ
1746out:
1747 return changed_num;
058d0e26
AQ
1748}
1749
1750/* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1751static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1752{
1753 struct hashtable_t *hash = bat_priv->tt_local_hash;
48100bac 1754 struct tt_common_entry *tt_common_entry;
058d0e26
AQ
1755 struct tt_local_entry *tt_local_entry;
1756 struct hlist_node *node, *node_tmp;
1757 struct hlist_head *head;
1758 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 1759 uint32_t i;
058d0e26
AQ
1760
1761 if (!hash)
1762 return;
1763
1764 for (i = 0; i < hash->size; i++) {
1765 head = &hash->table[i];
1766 list_lock = &hash->list_locks[i];
1767
1768 spin_lock_bh(list_lock);
48100bac 1769 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
058d0e26 1770 head, hash_entry) {
48100bac 1771 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
058d0e26
AQ
1772 continue;
1773
1774 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
48100bac 1775 "(%pM): pending\n", tt_common_entry->addr);
058d0e26
AQ
1776
1777 atomic_dec(&bat_priv->num_local_tt);
1778 hlist_del_rcu(node);
48100bac
AQ
1779 tt_local_entry = container_of(tt_common_entry,
1780 struct tt_local_entry,
1781 common);
058d0e26
AQ
1782 tt_local_entry_free_ref(tt_local_entry);
1783 }
1784 spin_unlock_bh(list_lock);
1785 }
1786
1787}
1788
1789void tt_commit_changes(struct bat_priv *bat_priv)
1790{
697f2531
AQ
1791 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
1792 TT_CLIENT_NEW, false);
1793 /* all the reset entries have now to be effectively counted as local
1794 * entries */
1795 atomic_add(changed_num, &bat_priv->num_local_tt);
058d0e26
AQ
1796 tt_local_purge_pending_clients(bat_priv);
1797
1798 /* Increment the TTVN only once per OGM interval */
1799 atomic_inc(&bat_priv->ttvn);
1800 bat_priv->tt_poss_change = false;
1801}
59b699cd
AQ
1802
1803bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
1804{
1805 struct tt_local_entry *tt_local_entry = NULL;
1806 struct tt_global_entry *tt_global_entry = NULL;
1807 bool ret = true;
1808
1809 if (!atomic_read(&bat_priv->ap_isolation))
1810 return false;
1811
1812 tt_local_entry = tt_local_hash_find(bat_priv, dst);
1813 if (!tt_local_entry)
1814 goto out;
1815
1816 tt_global_entry = tt_global_hash_find(bat_priv, src);
1817 if (!tt_global_entry)
1818 goto out;
1819
1820 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
1821 goto out;
1822
1823 ret = false;
1824
1825out:
1826 if (tt_global_entry)
1827 tt_global_entry_free_ref(tt_global_entry);
1828 if (tt_local_entry)
1829 tt_local_entry_free_ref(tt_local_entry);
1830 return ret;
1831}
a943cac1
ML
1832
1833void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
1834 const unsigned char *tt_buff, uint8_t tt_num_changes,
1835 uint8_t ttvn, uint16_t tt_crc)
1836{
1837 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
1838 bool full_table = true;
1839
17071578
AQ
1840 /* orig table not initialised AND first diff is in the OGM OR the ttvn
1841 * increased by one -> we can apply the attached changes */
1842 if ((!orig_node->tt_initialised && ttvn == 1) ||
1843 ttvn - orig_ttvn == 1) {
a943cac1
ML
1844 /* the OGM could not contain the changes due to their size or
1845 * because they have already been sent TT_OGM_APPEND_MAX times.
1846 * In this case send a tt request */
1847 if (!tt_num_changes) {
1848 full_table = false;
1849 goto request_table;
1850 }
1851
1852 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
1853 (struct tt_change *)tt_buff);
1854
1855 /* Even if we received the precomputed crc with the OGM, we
1856 * prefer to recompute it to spot any possible inconsistency
1857 * in the global table */
1858 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1859
1860 /* The ttvn alone is not enough to guarantee consistency
1861 * because a single value could represent different states
1862 * (due to the wrap around). Thus a node has to check whether
1863 * the resulting table (after applying the changes) is still
1864 * consistent or not. E.g. a node could disconnect while its
1865 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1866 * checking the CRC value is mandatory to detect the
1867 * inconsistency */
1868 if (orig_node->tt_crc != tt_crc)
1869 goto request_table;
1870
1871 /* Roaming phase is over: tables are in sync again. I can
1872 * unset the flag */
1873 orig_node->tt_poss_change = false;
1874 } else {
1875 /* if we missed more than one change or our tables are not
1876 * in sync anymore -> request fresh tt data */
17071578
AQ
1877 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
1878 orig_node->tt_crc != tt_crc) {
a943cac1
ML
1879request_table:
1880 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
1881 "Need to retrieve the correct information "
1882 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1883 "%u num_changes: %u)\n", orig_node->orig, ttvn,
1884 orig_ttvn, tt_crc, orig_node->tt_crc,
1885 tt_num_changes);
1886 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
1887 full_table);
1888 return;
1889 }
1890 }
1891}