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