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