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