2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
23 #include "translation-table.h"
24 #include "soft-interface.h"
25 #include "hard-interface.h"
28 #include "originator.h"
31 #include <linux/crc16.h>
33 static void _tt_global_del(struct bat_priv *bat_priv,
34 struct tt_global_entry *tt_global_entry,
36 static void tt_purge(struct work_struct *work);
38 /* returns 1 if they are the same mac addr */
39 static int compare_ltt(const struct hlist_node *node, const void *data2)
41 const void *data1 = container_of(node, struct tt_local_entry,
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
47 /* returns 1 if they are the same mac addr */
48 static int compare_gtt(const struct hlist_node *node, const void *data2)
50 const void *data1 = container_of(node, struct tt_global_entry,
53 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
56 static void tt_start_timer(struct bat_priv *bat_priv)
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));
63 static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
66 struct hashtable_t *hash = bat_priv->tt_local_hash;
67 struct hlist_head *head;
68 struct hlist_node *node;
69 struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
75 index = choose_orig(data, hash->size);
76 head = &hash->table[index];
79 hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
80 if (!compare_eth(tt_local_entry, data))
83 if (!atomic_inc_not_zero(&tt_local_entry->refcount))
86 tt_local_entry_tmp = tt_local_entry;
91 return tt_local_entry_tmp;
94 static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
97 struct hashtable_t *hash = bat_priv->tt_global_hash;
98 struct hlist_head *head;
99 struct hlist_node *node;
100 struct tt_global_entry *tt_global_entry;
101 struct tt_global_entry *tt_global_entry_tmp = NULL;
107 index = choose_orig(data, hash->size);
108 head = &hash->table[index];
111 hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
112 if (!compare_eth(tt_global_entry, data))
115 if (!atomic_inc_not_zero(&tt_global_entry->refcount))
118 tt_global_entry_tmp = tt_global_entry;
123 return tt_global_entry_tmp;
126 static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
128 unsigned long deadline;
129 deadline = starting_time + msecs_to_jiffies(timeout);
131 return time_after(jiffies, deadline);
134 static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
136 if (atomic_dec_and_test(&tt_local_entry->refcount))
137 kfree_rcu(tt_local_entry, rcu);
140 static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
142 if (atomic_dec_and_test(&tt_global_entry->refcount))
143 kfree_rcu(tt_global_entry, rcu);
146 static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
149 struct tt_change_node *tt_change_node;
151 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
156 tt_change_node->change.flags = flags;
157 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
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);
165 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
168 int tt_len(int changes_num)
170 return changes_num * sizeof(struct tt_change);
173 static int tt_local_init(struct bat_priv *bat_priv)
175 if (bat_priv->tt_local_hash)
178 bat_priv->tt_local_hash = hash_new(1024);
180 if (!bat_priv->tt_local_hash)
186 void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
188 struct bat_priv *bat_priv = netdev_priv(soft_iface);
189 struct tt_local_entry *tt_local_entry = NULL;
190 struct tt_global_entry *tt_global_entry = NULL;
192 tt_local_entry = tt_local_hash_find(bat_priv, addr);
194 if (tt_local_entry) {
195 tt_local_entry->last_seen = jiffies;
199 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
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));
207 memcpy(tt_local_entry->addr, addr, ETH_ALEN);
208 tt_local_entry->last_seen = jiffies;
209 tt_local_entry->flags = NO_FLAGS;
210 atomic_set(&tt_local_entry->refcount, 2);
212 /* the batman interface mac address should never be purged */
213 if (compare_eth(addr, soft_iface->dev_addr))
214 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
216 tt_local_event(bat_priv, addr, tt_local_entry->flags);
218 hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
219 tt_local_entry, &tt_local_entry->hash_entry);
221 atomic_inc(&bat_priv->num_local_tt);
223 /* remove address from global hash if present */
224 tt_global_entry = tt_global_hash_find(bat_priv, addr);
226 /* Check whether it is a roaming! */
227 if (tt_global_entry) {
228 /* This node is probably going to update its tt table */
229 tt_global_entry->orig_node->tt_poss_change = true;
230 _tt_global_del(bat_priv, tt_global_entry,
231 "local tt received");
232 send_roam_adv(bat_priv, tt_global_entry->addr,
233 tt_global_entry->orig_node);
237 tt_local_entry_free_ref(tt_local_entry);
239 tt_global_entry_free_ref(tt_global_entry);
242 int tt_changes_fill_buffer(struct bat_priv *bat_priv,
243 unsigned char *buff, int buff_len)
245 int count = 0, tot_changes = 0;
246 struct tt_change_node *entry, *safe;
249 tot_changes = buff_len / tt_len(1);
251 spin_lock_bh(&bat_priv->tt_changes_list_lock);
252 atomic_set(&bat_priv->tt_local_changes, 0);
254 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
256 if (count < tot_changes) {
257 memcpy(buff + tt_len(count),
258 &entry->change, sizeof(struct tt_change));
261 list_del(&entry->list);
264 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
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
275 * if kmalloc() fails we will reply with the full table
276 * instead of providing the diff
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;
284 spin_unlock_bh(&bat_priv->tt_buff_lock);
289 int tt_local_seq_print_text(struct seq_file *seq, void *offset)
291 struct net_device *net_dev = (struct net_device *)seq->private;
292 struct bat_priv *bat_priv = netdev_priv(net_dev);
293 struct hashtable_t *hash = bat_priv->tt_local_hash;
294 struct tt_local_entry *tt_local_entry;
295 struct hard_iface *primary_if;
296 struct hlist_node *node;
297 struct hlist_head *head;
298 size_t buf_size, pos;
302 primary_if = primary_if_get_selected(bat_priv);
304 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
305 "please specify interfaces to enable it\n",
310 if (primary_if->if_status != IF_ACTIVE) {
311 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
312 "primary interface not active\n",
317 seq_printf(seq, "Locally retrieved addresses (from %s) "
318 "announced via TT (TTVN: %u):\n",
319 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
322 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
323 for (i = 0; i < hash->size; i++) {
324 head = &hash->table[i];
327 __hlist_for_each_rcu(node, head)
332 buff = kmalloc(buf_size, GFP_ATOMIC);
341 for (i = 0; i < hash->size; i++) {
342 head = &hash->table[i];
345 hlist_for_each_entry_rcu(tt_local_entry, node,
347 pos += snprintf(buff + pos, 22, " * %pM\n",
348 tt_local_entry->addr);
353 seq_printf(seq, "%s", buff);
357 hardif_free_ref(primary_if);
361 static void tt_local_del(struct bat_priv *bat_priv,
362 struct tt_local_entry *tt_local_entry,
365 bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry (%pM): %s\n",
366 tt_local_entry->addr, message);
368 atomic_dec(&bat_priv->num_local_tt);
370 hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
371 tt_local_entry->addr);
373 tt_local_entry_free_ref(tt_local_entry);
376 void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
377 const char *message, bool roaming)
379 struct tt_local_entry *tt_local_entry = NULL;
381 tt_local_entry = tt_local_hash_find(bat_priv, addr);
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));
389 tt_local_del(bat_priv, tt_local_entry, message);
392 tt_local_entry_free_ref(tt_local_entry);
395 static void tt_local_purge(struct bat_priv *bat_priv)
397 struct hashtable_t *hash = bat_priv->tt_local_hash;
398 struct tt_local_entry *tt_local_entry;
399 struct hlist_node *node, *node_tmp;
400 struct hlist_head *head;
401 spinlock_t *list_lock; /* protects write access to the hash lists */
404 for (i = 0; i < hash->size; i++) {
405 head = &hash->table[i];
406 list_lock = &hash->list_locks[i];
408 spin_lock_bh(list_lock);
409 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
411 if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
414 if (!is_out_of_time(tt_local_entry->last_seen,
415 TT_LOCAL_TIMEOUT * 1000))
418 tt_local_event(bat_priv, tt_local_entry->addr,
419 tt_local_entry->flags | TT_CLIENT_DEL);
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);
425 tt_local_entry_free_ref(tt_local_entry);
427 spin_unlock_bh(list_lock);
432 static void tt_local_table_free(struct bat_priv *bat_priv)
434 struct hashtable_t *hash;
435 spinlock_t *list_lock; /* protects write access to the hash lists */
436 struct tt_local_entry *tt_local_entry;
437 struct hlist_node *node, *node_tmp;
438 struct hlist_head *head;
441 if (!bat_priv->tt_local_hash)
444 hash = bat_priv->tt_local_hash;
446 for (i = 0; i < hash->size; i++) {
447 head = &hash->table[i];
448 list_lock = &hash->list_locks[i];
450 spin_lock_bh(list_lock);
451 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
454 tt_local_entry_free_ref(tt_local_entry);
456 spin_unlock_bh(list_lock);
461 bat_priv->tt_local_hash = NULL;
464 static int tt_global_init(struct bat_priv *bat_priv)
466 if (bat_priv->tt_global_hash)
469 bat_priv->tt_global_hash = hash_new(1024);
471 if (!bat_priv->tt_global_hash)
477 static void tt_changes_list_free(struct bat_priv *bat_priv)
479 struct tt_change_node *entry, *safe;
481 spin_lock_bh(&bat_priv->tt_changes_list_lock);
483 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
485 list_del(&entry->list);
489 atomic_set(&bat_priv->tt_local_changes, 0);
490 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
493 /* caller must hold orig_node refcount */
494 int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
495 const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
497 struct tt_global_entry *tt_global_entry;
498 struct orig_node *orig_node_tmp;
501 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
503 if (!tt_global_entry) {
505 kmalloc(sizeof(*tt_global_entry),
507 if (!tt_global_entry)
510 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
511 /* Assign the new orig_node */
512 atomic_inc(&orig_node->refcount);
513 tt_global_entry->orig_node = orig_node;
514 tt_global_entry->ttvn = ttvn;
515 tt_global_entry->flags = NO_FLAGS;
516 tt_global_entry->roam_at = 0;
517 atomic_set(&tt_global_entry->refcount, 2);
519 hash_add(bat_priv->tt_global_hash, compare_gtt,
520 choose_orig, tt_global_entry,
521 &tt_global_entry->hash_entry);
522 atomic_inc(&orig_node->tt_size);
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;
529 orig_node_free_ref(orig_node_tmp);
530 atomic_inc(&orig_node->tt_size);
532 tt_global_entry->ttvn = ttvn;
533 tt_global_entry->flags = NO_FLAGS;
534 tt_global_entry->roam_at = 0;
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);
541 /* remove address from local hash if present */
542 tt_local_remove(bat_priv, tt_global_entry->addr,
543 "global tt received", roaming);
547 tt_global_entry_free_ref(tt_global_entry);
551 int tt_global_seq_print_text(struct seq_file *seq, void *offset)
553 struct net_device *net_dev = (struct net_device *)seq->private;
554 struct bat_priv *bat_priv = netdev_priv(net_dev);
555 struct hashtable_t *hash = bat_priv->tt_global_hash;
556 struct tt_global_entry *tt_global_entry;
557 struct hard_iface *primary_if;
558 struct hlist_node *node;
559 struct hlist_head *head;
560 size_t buf_size, pos;
564 primary_if = primary_if_get_selected(bat_priv);
566 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
567 "specify interfaces to enable it\n",
572 if (primary_if->if_status != IF_ACTIVE) {
573 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
574 "primary interface not active\n",
580 "Globally announced TT entries received via the mesh %s\n",
582 seq_printf(seq, " %-13s %s %-15s %s\n",
583 "Client", "(TTVN)", "Originator", "(Curr TTVN)");
586 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
587 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
588 for (i = 0; i < hash->size; i++) {
589 head = &hash->table[i];
592 __hlist_for_each_rcu(node, head)
597 buff = kmalloc(buf_size, GFP_ATOMIC);
606 for (i = 0; i < hash->size; i++) {
607 head = &hash->table[i];
610 hlist_for_each_entry_rcu(tt_global_entry, node,
612 pos += snprintf(buff + pos, 61,
613 " * %pM (%3u) via %pM (%3u)\n",
614 tt_global_entry->addr,
615 tt_global_entry->ttvn,
616 tt_global_entry->orig_node->orig,
617 (uint8_t) atomic_read(
618 &tt_global_entry->orig_node->
624 seq_printf(seq, "%s", buff);
628 hardif_free_ref(primary_if);
632 static void _tt_global_del(struct bat_priv *bat_priv,
633 struct tt_global_entry *tt_global_entry,
636 if (!tt_global_entry)
639 bat_dbg(DBG_TT, bat_priv,
640 "Deleting global tt entry %pM (via %pM): %s\n",
641 tt_global_entry->addr, tt_global_entry->orig_node->orig,
644 atomic_dec(&tt_global_entry->orig_node->tt_size);
646 hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
647 tt_global_entry->addr);
650 tt_global_entry_free_ref(tt_global_entry);
653 void tt_global_del(struct bat_priv *bat_priv,
654 struct orig_node *orig_node, const unsigned char *addr,
655 const char *message, bool roaming)
657 struct tt_global_entry *tt_global_entry = NULL;
659 tt_global_entry = tt_global_hash_find(bat_priv, addr);
660 if (!tt_global_entry)
663 if (tt_global_entry->orig_node == orig_node) {
665 tt_global_entry->flags |= TT_CLIENT_ROAM;
666 tt_global_entry->roam_at = jiffies;
669 _tt_global_del(bat_priv, tt_global_entry, message);
673 tt_global_entry_free_ref(tt_global_entry);
676 void tt_global_del_orig(struct bat_priv *bat_priv,
677 struct orig_node *orig_node, const char *message)
679 struct tt_global_entry *tt_global_entry;
681 struct hashtable_t *hash = bat_priv->tt_global_hash;
682 struct hlist_node *node, *safe;
683 struct hlist_head *head;
684 spinlock_t *list_lock; /* protects write access to the hash lists */
686 for (i = 0; i < hash->size; i++) {
687 head = &hash->table[i];
688 list_lock = &hash->list_locks[i];
690 spin_lock_bh(list_lock);
691 hlist_for_each_entry_safe(tt_global_entry, node, safe,
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);
700 tt_global_entry_free_ref(tt_global_entry);
703 spin_unlock_bh(list_lock);
705 atomic_set(&orig_node->tt_size, 0);
708 static void tt_global_roam_purge(struct bat_priv *bat_priv)
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;
714 spinlock_t *list_lock; /* protects write access to the hash lists */
717 for (i = 0; i < hash->size; i++) {
718 head = &hash->table[i];
719 list_lock = &hash->list_locks[i];
721 spin_lock_bh(list_lock);
722 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
724 if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
726 if (!is_out_of_time(tt_global_entry->roam_at,
727 TT_CLIENT_ROAM_TIMEOUT * 1000))
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);
735 tt_global_entry_free_ref(tt_global_entry);
737 spin_unlock_bh(list_lock);
742 static void tt_global_table_free(struct bat_priv *bat_priv)
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;
751 if (!bat_priv->tt_global_hash)
754 hash = bat_priv->tt_global_hash;
756 for (i = 0; i < hash->size; i++) {
757 head = &hash->table[i];
758 list_lock = &hash->list_locks[i];
760 spin_lock_bh(list_lock);
761 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
764 tt_global_entry_free_ref(tt_global_entry);
766 spin_unlock_bh(list_lock);
771 bat_priv->tt_global_hash = NULL;
774 struct orig_node *transtable_search(struct bat_priv *bat_priv,
777 struct tt_global_entry *tt_global_entry;
778 struct orig_node *orig_node = NULL;
780 tt_global_entry = tt_global_hash_find(bat_priv, addr);
782 if (!tt_global_entry)
785 if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
788 orig_node = tt_global_entry->orig_node;
791 tt_global_entry_free_ref(tt_global_entry);
796 /* Calculates the checksum of the local table of a given orig_node */
797 uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
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;
806 for (i = 0; i < hash->size; i++) {
807 head = &hash->table[i];
810 hlist_for_each_entry_rcu(tt_global_entry, node,
812 if (compare_eth(tt_global_entry->orig_node,
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
818 if (tt_global_entry->flags & TT_CLIENT_ROAM)
821 for (j = 0; j < ETH_ALEN; j++)
822 total_one = crc16_byte(total_one,
823 tt_global_entry->addr[j]);
833 /* Calculates the checksum of the local table */
834 uint16_t tt_local_crc(struct bat_priv *bat_priv)
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;
843 for (i = 0; i < hash->size; i++) {
844 head = &hash->table[i];
847 hlist_for_each_entry_rcu(tt_local_entry, node,
850 for (j = 0; j < ETH_ALEN; j++)
851 total_one = crc16_byte(total_one,
852 tt_local_entry->addr[j]);
861 static void tt_req_list_free(struct bat_priv *bat_priv)
863 struct tt_req_node *node, *safe;
865 spin_lock_bh(&bat_priv->tt_req_list_lock);
867 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
868 list_del(&node->list);
872 spin_unlock_bh(&bat_priv->tt_req_list_lock);
875 void 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)
878 uint16_t tt_buff_len = tt_len(tt_num_changes);
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;
892 spin_unlock_bh(&orig_node->tt_buff_lock);
895 static void tt_req_purge(struct bat_priv *bat_priv)
897 struct tt_req_node *node, *safe;
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);
907 spin_unlock_bh(&bat_priv->tt_req_list_lock);
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 */
912 static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
913 struct orig_node *orig_node)
915 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
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))
925 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
929 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
930 tt_req_node->issued_at = jiffies;
932 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
934 spin_unlock_bh(&bat_priv->tt_req_list_lock);
938 static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
940 const struct tt_global_entry *tt_global_entry = entry_ptr;
941 const struct orig_node *orig_node = data_ptr;
943 if (tt_global_entry->flags & TT_CLIENT_ROAM)
946 return (tt_global_entry->orig_node == orig_node);
949 static 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 *,
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);
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);
970 tt_tot = tt_len / sizeof(struct tt_change);
972 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
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);
982 tt_change = (struct tt_change *)(skb->data + tt_query_size);
986 for (i = 0; i < hash->size; i++) {
987 head = &hash->table[i];
989 hlist_for_each_entry_rcu(tt_local_entry, node,
991 if (tt_count == tt_tot)
994 if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
997 memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
998 tt_change->flags = NO_FLAGS;
1010 int 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)
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;
1020 primary_if = primary_if_get_selected(bat_priv);
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);
1030 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1034 skb_reserve(skb, ETH_HLEN);
1036 tt_request = (struct tt_query_packet *)skb_put(skb,
1037 sizeof(struct tt_query_packet));
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;
1049 tt_request->flags |= TT_FULL_TABLE;
1051 neigh_node = orig_node_get_router(dst_orig_node);
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' : '.'));
1059 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1064 neigh_node_free_ref(neigh_node);
1066 hardif_free_ref(primary_if);
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);
1078 static bool send_other_tt_response(struct bat_priv *bat_priv,
1079 struct tt_query_packet *tt_request)
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;
1086 unsigned char *tt_buff;
1088 uint16_t tt_len, tt_tot;
1089 struct sk_buff *skb = NULL;
1090 struct tt_query_packet *tt_response;
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' : '.'));
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)
1103 res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1104 if (!res_dst_orig_node)
1107 neigh_node = orig_node_get_router(res_dst_orig_node);
1111 primary_if = primary_if_get_selected(bat_priv);
1115 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1116 req_ttvn = tt_request->ttvn;
1118 /* I have not the requested data */
1119 if (orig_ttvn != req_ttvn ||
1120 tt_request->tt_data != req_dst_orig_node->tt_crc)
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)
1130 /* In this version, fragmentation is not implemented, then
1131 * I'll send only one packet with as much TT entries as I can */
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);
1137 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
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);
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);
1153 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
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);
1159 skb = tt_response_fill_table(tt_len, ttvn,
1160 bat_priv->tt_global_hash,
1161 primary_if, tt_global_valid_entry,
1166 tt_response = (struct tt_query_packet *)skb->data;
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;
1177 tt_response->flags |= TT_FULL_TABLE;
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);
1184 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1189 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
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);
1197 neigh_node_free_ref(neigh_node);
1199 hardif_free_ref(primary_if);
1205 static bool send_my_tt_response(struct bat_priv *bat_priv,
1206 struct tt_query_packet *tt_request)
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;
1213 unsigned char *tt_buff;
1215 uint16_t tt_len, tt_tot;
1216 struct sk_buff *skb = NULL;
1217 struct tt_query_packet *tt_response;
1219 bat_dbg(DBG_TT, bat_priv,
1220 "Received TT_REQUEST from %pM for "
1221 "ttvn: %u (me) [%c]\n", tt_request->src,
1223 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1226 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1227 req_ttvn = tt_request->ttvn;
1229 orig_node = get_orig_node(bat_priv, tt_request->src);
1233 neigh_node = orig_node_get_router(orig_node);
1237 primary_if = primary_if_get_selected(bat_priv);
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 ||
1249 /* In this version, fragmentation is not implemented, then
1250 * I'll send only one packet with as much TT entries as I can */
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);
1256 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
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);
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);
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);
1276 skb = tt_response_fill_table(tt_len, ttvn,
1277 bat_priv->tt_local_hash,
1278 primary_if, NULL, NULL);
1282 tt_response = (struct tt_query_packet *)skb->data;
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;
1293 tt_response->flags |= TT_FULL_TABLE;
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' : '.'));
1300 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1305 spin_unlock_bh(&bat_priv->tt_buff_lock);
1308 orig_node_free_ref(orig_node);
1310 neigh_node_free_ref(neigh_node);
1312 hardif_free_ref(primary_if);
1315 /* This packet was for me, so it doesn't need to be re-routed */
1319 bool send_tt_response(struct bat_priv *bat_priv,
1320 struct tt_query_packet *tt_request)
1322 if (is_my_mac(tt_request->dst))
1323 return send_my_tt_response(bat_priv, tt_request);
1325 return send_other_tt_response(bat_priv, tt_request);
1328 static 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)
1335 for (i = 0; i < tt_num_changes; i++) {
1336 if ((tt_change + i)->flags & TT_CLIENT_DEL)
1337 tt_global_del(bat_priv, orig_node,
1338 (tt_change + i)->addr,
1339 "tt removed by changes",
1340 (tt_change + i)->flags & TT_CLIENT_ROAM);
1342 if (!tt_global_add(bat_priv, orig_node,
1343 (tt_change + i)->addr, ttvn, false))
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
1354 static void tt_fill_gtable(struct bat_priv *bat_priv,
1355 struct tt_query_packet *tt_response)
1357 struct orig_node *orig_node = NULL;
1359 orig_node = orig_hash_find(bat_priv, tt_response->src);
1363 /* Purge the old table first.. */
1364 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1366 _tt_update_changes(bat_priv, orig_node,
1367 (struct tt_change *)(tt_response + 1),
1368 tt_response->tt_data, tt_response->ttvn);
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);
1376 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1380 orig_node_free_ref(orig_node);
1383 void 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)
1387 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1390 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1392 atomic_set(&orig_node->last_ttvn, ttvn);
1395 bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1397 struct tt_local_entry *tt_local_entry = NULL;
1400 tt_local_entry = tt_local_hash_find(bat_priv, addr);
1401 if (!tt_local_entry)
1406 tt_local_entry_free_ref(tt_local_entry);
1410 void handle_tt_response(struct bat_priv *bat_priv,
1411 struct tt_query_packet *tt_response)
1413 struct tt_req_node *node, *safe;
1414 struct orig_node *orig_node = NULL;
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' : '.'));
1422 orig_node = orig_hash_find(bat_priv, tt_response->src);
1426 if (tt_response->flags & TT_FULL_TABLE)
1427 tt_fill_gtable(bat_priv, tt_response);
1429 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1431 (struct tt_change *)(tt_response + 1));
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))
1438 list_del(&node->list);
1441 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1443 /* Recalculate the CRC for this orig_node and store it */
1444 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1445 /* Roaming phase is over: tables are in sync again. I can
1447 orig_node->tt_poss_change = false;
1450 orig_node_free_ref(orig_node);
1453 int tt_init(struct bat_priv *bat_priv)
1455 if (!tt_local_init(bat_priv))
1458 if (!tt_global_init(bat_priv))
1461 tt_start_timer(bat_priv);
1466 static void tt_roam_list_free(struct bat_priv *bat_priv)
1468 struct tt_roam_node *node, *safe;
1470 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1472 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1473 list_del(&node->list);
1477 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1480 static void tt_roam_purge(struct bat_priv *bat_priv)
1482 struct tt_roam_node *node, *safe;
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))
1490 list_del(&node->list);
1493 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1496 /* This function checks whether the client already reached the
1497 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1500 * returns true if the ROAMING_ADV can be sent, false otherwise */
1501 static bool tt_check_roam_count(struct bat_priv *bat_priv,
1504 struct tt_roam_node *tt_roam_node;
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))
1514 if (is_out_of_time(tt_roam_node->first_time,
1515 ROAMING_MAX_TIME * 1000))
1518 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1519 /* Sorry, you roamed too many times! */
1526 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
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);
1534 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1539 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1543 void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1544 struct orig_node *orig_node)
1546 struct neigh_node *neigh_node = NULL;
1547 struct sk_buff *skb = NULL;
1548 struct roam_adv_packet *roam_adv_packet;
1550 struct hard_iface *primary_if;
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))
1557 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1561 skb_reserve(skb, ETH_HLEN);
1563 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1564 sizeof(struct roam_adv_packet));
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);
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);
1577 neigh_node = orig_node_get_router(orig_node);
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);
1585 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1590 neigh_node_free_ref(neigh_node);
1596 static void tt_purge(struct work_struct *work)
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);
1603 tt_local_purge(bat_priv);
1604 tt_global_roam_purge(bat_priv);
1605 tt_req_purge(bat_priv);
1606 tt_roam_purge(bat_priv);
1608 tt_start_timer(bat_priv);
1611 void tt_free(struct bat_priv *bat_priv)
1613 cancel_delayed_work_sync(&bat_priv->tt_work);
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);
1621 kfree(bat_priv->tt_buff);