2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
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"
30 #include "bridge_loop_avoidance.h"
32 #include <linux/crc16.h>
34 static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
35 struct orig_node *orig_node);
36 static void tt_purge(struct work_struct *work);
37 static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
39 /* returns 1 if they are the same mac addr */
40 static int compare_tt(const struct hlist_node *node, const void *data2)
42 const void *data1 = container_of(node, struct tt_common_entry,
45 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
48 static void tt_start_timer(struct bat_priv *bat_priv)
50 INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
51 queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
52 msecs_to_jiffies(5000));
55 static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
58 struct hlist_head *head;
59 struct hlist_node *node;
60 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
66 index = choose_orig(data, hash->size);
67 head = &hash->table[index];
70 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
71 if (!compare_eth(tt_common_entry, data))
74 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
77 tt_common_entry_tmp = tt_common_entry;
82 return tt_common_entry_tmp;
85 static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
88 struct tt_common_entry *tt_common_entry;
89 struct tt_local_entry *tt_local_entry = NULL;
91 tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
93 tt_local_entry = container_of(tt_common_entry,
94 struct tt_local_entry, common);
95 return tt_local_entry;
98 static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
101 struct tt_common_entry *tt_common_entry;
102 struct tt_global_entry *tt_global_entry = NULL;
104 tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
106 tt_global_entry = container_of(tt_common_entry,
107 struct tt_global_entry, common);
108 return tt_global_entry;
112 static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
114 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
115 kfree_rcu(tt_local_entry, common.rcu);
118 static void tt_global_entry_free_rcu(struct rcu_head *rcu)
120 struct tt_common_entry *tt_common_entry;
121 struct tt_global_entry *tt_global_entry;
123 tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
124 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
127 kfree(tt_global_entry);
130 static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
132 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
133 tt_global_del_orig_list(tt_global_entry);
134 call_rcu(&tt_global_entry->common.rcu,
135 tt_global_entry_free_rcu);
139 static void tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
141 struct tt_orig_list_entry *orig_entry;
143 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
144 atomic_dec(&orig_entry->orig_node->tt_size);
145 orig_node_free_ref(orig_entry->orig_node);
149 static void tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
151 call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu);
154 static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
157 struct tt_change_node *tt_change_node;
159 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
164 tt_change_node->change.flags = flags;
165 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
167 spin_lock_bh(&bat_priv->tt_changes_list_lock);
168 /* track the change in the OGMinterval list */
169 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
170 atomic_inc(&bat_priv->tt_local_changes);
171 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
173 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
176 int tt_len(int changes_num)
178 return changes_num * sizeof(struct tt_change);
181 static int tt_local_init(struct bat_priv *bat_priv)
183 if (bat_priv->tt_local_hash)
186 bat_priv->tt_local_hash = hash_new(1024);
188 if (!bat_priv->tt_local_hash)
194 void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
197 struct bat_priv *bat_priv = netdev_priv(soft_iface);
198 struct tt_local_entry *tt_local_entry = NULL;
199 struct tt_global_entry *tt_global_entry = NULL;
200 struct hlist_head *head;
201 struct hlist_node *node;
202 struct tt_orig_list_entry *orig_entry;
205 tt_local_entry = tt_local_hash_find(bat_priv, addr);
207 if (tt_local_entry) {
208 tt_local_entry->last_seen = jiffies;
209 /* possibly unset the TT_CLIENT_PENDING flag */
210 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
214 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
218 bat_dbg(DBG_TT, bat_priv,
219 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
220 (uint8_t)atomic_read(&bat_priv->ttvn));
222 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
223 tt_local_entry->common.flags = NO_FLAGS;
224 if (is_wifi_iface(ifindex))
225 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
226 atomic_set(&tt_local_entry->common.refcount, 2);
227 tt_local_entry->last_seen = jiffies;
229 /* the batman interface mac address should never be purged */
230 if (compare_eth(addr, soft_iface->dev_addr))
231 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
233 /* The local entry has to be marked as NEW to avoid to send it in
234 * a full table response going out before the next ttvn increment
235 * (consistency check) */
236 tt_local_entry->common.flags |= TT_CLIENT_NEW;
238 hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
239 &tt_local_entry->common,
240 &tt_local_entry->common.hash_entry);
242 if (unlikely(hash_added != 0)) {
243 /* remove the reference for the hash */
244 tt_local_entry_free_ref(tt_local_entry);
248 tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
250 /* remove address from global hash if present */
251 tt_global_entry = tt_global_hash_find(bat_priv, addr);
253 /* Check whether it is a roaming! */
254 if (tt_global_entry) {
255 /* These node are probably going to update their tt table */
256 head = &tt_global_entry->orig_list;
258 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
259 orig_entry->orig_node->tt_poss_change = true;
261 send_roam_adv(bat_priv, tt_global_entry->common.addr,
262 orig_entry->orig_node);
265 /* The global entry has to be marked as ROAMING and
266 * has to be kept for consistency purpose
268 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
269 tt_global_entry->roam_at = jiffies;
273 tt_local_entry_free_ref(tt_local_entry);
275 tt_global_entry_free_ref(tt_global_entry);
278 int tt_changes_fill_buffer(struct bat_priv *bat_priv,
279 unsigned char *buff, int buff_len)
281 int count = 0, tot_changes = 0;
282 struct tt_change_node *entry, *safe;
285 tot_changes = buff_len / tt_len(1);
287 spin_lock_bh(&bat_priv->tt_changes_list_lock);
288 atomic_set(&bat_priv->tt_local_changes, 0);
290 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
292 if (count < tot_changes) {
293 memcpy(buff + tt_len(count),
294 &entry->change, sizeof(struct tt_change));
297 list_del(&entry->list);
300 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
302 /* Keep the buffer for possible tt_request */
303 spin_lock_bh(&bat_priv->tt_buff_lock);
304 kfree(bat_priv->tt_buff);
305 bat_priv->tt_buff_len = 0;
306 bat_priv->tt_buff = NULL;
307 /* We check whether this new OGM has no changes due to size
311 * if kmalloc() fails we will reply with the full table
312 * instead of providing the diff
314 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
315 if (bat_priv->tt_buff) {
316 memcpy(bat_priv->tt_buff, buff, buff_len);
317 bat_priv->tt_buff_len = buff_len;
320 spin_unlock_bh(&bat_priv->tt_buff_lock);
325 int tt_local_seq_print_text(struct seq_file *seq, void *offset)
327 struct net_device *net_dev = (struct net_device *)seq->private;
328 struct bat_priv *bat_priv = netdev_priv(net_dev);
329 struct hashtable_t *hash = bat_priv->tt_local_hash;
330 struct tt_common_entry *tt_common_entry;
331 struct hard_iface *primary_if;
332 struct hlist_node *node;
333 struct hlist_head *head;
337 primary_if = primary_if_get_selected(bat_priv);
339 ret = seq_printf(seq,
340 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
345 if (primary_if->if_status != IF_ACTIVE) {
346 ret = seq_printf(seq,
347 "BATMAN mesh %s disabled - primary interface not active\n",
353 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
354 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
356 for (i = 0; i < hash->size; i++) {
357 head = &hash->table[i];
360 hlist_for_each_entry_rcu(tt_common_entry, node,
362 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
363 tt_common_entry->addr,
364 (tt_common_entry->flags &
365 TT_CLIENT_ROAM ? 'R' : '.'),
366 (tt_common_entry->flags &
367 TT_CLIENT_NOPURGE ? 'P' : '.'),
368 (tt_common_entry->flags &
369 TT_CLIENT_NEW ? 'N' : '.'),
370 (tt_common_entry->flags &
371 TT_CLIENT_PENDING ? 'X' : '.'),
372 (tt_common_entry->flags &
373 TT_CLIENT_WIFI ? 'W' : '.'));
379 hardif_free_ref(primary_if);
383 static void tt_local_set_pending(struct bat_priv *bat_priv,
384 struct tt_local_entry *tt_local_entry,
385 uint16_t flags, const char *message)
387 tt_local_event(bat_priv, tt_local_entry->common.addr,
388 tt_local_entry->common.flags | flags);
390 /* The local client has to be marked as "pending to be removed" but has
391 * to be kept in the table in order to send it in a full table
392 * response issued before the net ttvn increment (consistency check) */
393 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
395 bat_dbg(DBG_TT, bat_priv,
396 "Local tt entry (%pM) pending to be removed: %s\n",
397 tt_local_entry->common.addr, message);
400 void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
401 const char *message, bool roaming)
403 struct tt_local_entry *tt_local_entry = NULL;
405 tt_local_entry = tt_local_hash_find(bat_priv, addr);
409 tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
410 (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
413 tt_local_entry_free_ref(tt_local_entry);
416 static void tt_local_purge(struct bat_priv *bat_priv)
418 struct hashtable_t *hash = bat_priv->tt_local_hash;
419 struct tt_local_entry *tt_local_entry;
420 struct tt_common_entry *tt_common_entry;
421 struct hlist_node *node, *node_tmp;
422 struct hlist_head *head;
423 spinlock_t *list_lock; /* protects write access to the hash lists */
426 for (i = 0; i < hash->size; i++) {
427 head = &hash->table[i];
428 list_lock = &hash->list_locks[i];
430 spin_lock_bh(list_lock);
431 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
433 tt_local_entry = container_of(tt_common_entry,
434 struct tt_local_entry,
436 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
439 /* entry already marked for deletion */
440 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
443 if (!has_timed_out(tt_local_entry->last_seen,
447 tt_local_set_pending(bat_priv, tt_local_entry,
448 TT_CLIENT_DEL, "timed out");
450 spin_unlock_bh(list_lock);
455 static void tt_local_table_free(struct bat_priv *bat_priv)
457 struct hashtable_t *hash;
458 spinlock_t *list_lock; /* protects write access to the hash lists */
459 struct tt_common_entry *tt_common_entry;
460 struct tt_local_entry *tt_local_entry;
461 struct hlist_node *node, *node_tmp;
462 struct hlist_head *head;
465 if (!bat_priv->tt_local_hash)
468 hash = bat_priv->tt_local_hash;
470 for (i = 0; i < hash->size; i++) {
471 head = &hash->table[i];
472 list_lock = &hash->list_locks[i];
474 spin_lock_bh(list_lock);
475 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
478 tt_local_entry = container_of(tt_common_entry,
479 struct tt_local_entry,
481 tt_local_entry_free_ref(tt_local_entry);
483 spin_unlock_bh(list_lock);
488 bat_priv->tt_local_hash = NULL;
491 static int tt_global_init(struct bat_priv *bat_priv)
493 if (bat_priv->tt_global_hash)
496 bat_priv->tt_global_hash = hash_new(1024);
498 if (!bat_priv->tt_global_hash)
504 static void tt_changes_list_free(struct bat_priv *bat_priv)
506 struct tt_change_node *entry, *safe;
508 spin_lock_bh(&bat_priv->tt_changes_list_lock);
510 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
512 list_del(&entry->list);
516 atomic_set(&bat_priv->tt_local_changes, 0);
517 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
520 /* find out if an orig_node is already in the list of a tt_global_entry.
521 * returns 1 if found, 0 otherwise
523 static bool tt_global_entry_has_orig(const struct tt_global_entry *entry,
524 const struct orig_node *orig_node)
526 struct tt_orig_list_entry *tmp_orig_entry;
527 const struct hlist_head *head;
528 struct hlist_node *node;
532 head = &entry->orig_list;
533 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
534 if (tmp_orig_entry->orig_node == orig_node) {
543 static void tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
544 struct orig_node *orig_node,
547 struct tt_orig_list_entry *orig_entry;
549 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
553 INIT_HLIST_NODE(&orig_entry->list);
554 atomic_inc(&orig_node->refcount);
555 atomic_inc(&orig_node->tt_size);
556 orig_entry->orig_node = orig_node;
557 orig_entry->ttvn = ttvn;
559 spin_lock_bh(&tt_global_entry->list_lock);
560 hlist_add_head_rcu(&orig_entry->list,
561 &tt_global_entry->orig_list);
562 spin_unlock_bh(&tt_global_entry->list_lock);
565 /* caller must hold orig_node refcount */
566 int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
567 const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
570 struct tt_global_entry *tt_global_entry = NULL;
574 tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
576 if (!tt_global_entry) {
577 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
579 if (!tt_global_entry)
582 memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
584 tt_global_entry->common.flags = NO_FLAGS;
585 tt_global_entry->roam_at = 0;
586 atomic_set(&tt_global_entry->common.refcount, 2);
588 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
589 spin_lock_init(&tt_global_entry->list_lock);
591 hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
592 choose_orig, &tt_global_entry->common,
593 &tt_global_entry->common.hash_entry);
595 if (unlikely(hash_added != 0)) {
596 /* remove the reference for the hash */
597 tt_global_entry_free_ref(tt_global_entry);
601 tt_global_add_orig_entry(tt_global_entry, orig_node, ttvn);
603 /* there is already a global entry, use this one. */
605 /* If there is the TT_CLIENT_ROAM flag set, there is only one
606 * originator left in the list and we previously received a
607 * delete + roaming change for this originator.
609 * We should first delete the old originator before adding the
612 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
613 tt_global_del_orig_list(tt_global_entry);
614 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
615 tt_global_entry->roam_at = 0;
618 if (!tt_global_entry_has_orig(tt_global_entry, orig_node))
619 tt_global_add_orig_entry(tt_global_entry, orig_node,
624 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
626 bat_dbg(DBG_TT, bat_priv,
627 "Creating new global tt entry: %pM (via %pM)\n",
628 tt_global_entry->common.addr, orig_node->orig);
631 /* remove address from local hash if present */
632 tt_local_remove(bat_priv, tt_global_entry->common.addr,
633 "global tt received", roaming);
637 tt_global_entry_free_ref(tt_global_entry);
641 /* print all orig nodes who announce the address for this global entry.
642 * it is assumed that the caller holds rcu_read_lock();
644 static void tt_global_print_entry(struct tt_global_entry *tt_global_entry,
645 struct seq_file *seq)
647 struct hlist_head *head;
648 struct hlist_node *node;
649 struct tt_orig_list_entry *orig_entry;
650 struct tt_common_entry *tt_common_entry;
654 tt_common_entry = &tt_global_entry->common;
656 head = &tt_global_entry->orig_list;
658 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
659 flags = tt_common_entry->flags;
660 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
661 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
662 tt_global_entry->common.addr, orig_entry->ttvn,
663 orig_entry->orig_node->orig, last_ttvn,
664 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
665 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
669 int tt_global_seq_print_text(struct seq_file *seq, void *offset)
671 struct net_device *net_dev = (struct net_device *)seq->private;
672 struct bat_priv *bat_priv = netdev_priv(net_dev);
673 struct hashtable_t *hash = bat_priv->tt_global_hash;
674 struct tt_common_entry *tt_common_entry;
675 struct tt_global_entry *tt_global_entry;
676 struct hard_iface *primary_if;
677 struct hlist_node *node;
678 struct hlist_head *head;
682 primary_if = primary_if_get_selected(bat_priv);
684 ret = seq_printf(seq,
685 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
690 if (primary_if->if_status != IF_ACTIVE) {
691 ret = seq_printf(seq,
692 "BATMAN mesh %s disabled - primary interface not active\n",
698 "Globally announced TT entries received via the mesh %s\n",
700 seq_printf(seq, " %-13s %s %-15s %s %s\n",
701 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
703 for (i = 0; i < hash->size; i++) {
704 head = &hash->table[i];
707 hlist_for_each_entry_rcu(tt_common_entry, node,
709 tt_global_entry = container_of(tt_common_entry,
710 struct tt_global_entry,
712 tt_global_print_entry(tt_global_entry, seq);
718 hardif_free_ref(primary_if);
722 /* deletes the orig list of a tt_global_entry */
723 static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
725 struct hlist_head *head;
726 struct hlist_node *node, *safe;
727 struct tt_orig_list_entry *orig_entry;
729 spin_lock_bh(&tt_global_entry->list_lock);
730 head = &tt_global_entry->orig_list;
731 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
733 tt_orig_list_entry_free_ref(orig_entry);
735 spin_unlock_bh(&tt_global_entry->list_lock);
739 static void tt_global_del_orig_entry(struct bat_priv *bat_priv,
740 struct tt_global_entry *tt_global_entry,
741 struct orig_node *orig_node,
744 struct hlist_head *head;
745 struct hlist_node *node, *safe;
746 struct tt_orig_list_entry *orig_entry;
748 spin_lock_bh(&tt_global_entry->list_lock);
749 head = &tt_global_entry->orig_list;
750 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
751 if (orig_entry->orig_node == orig_node) {
752 bat_dbg(DBG_TT, bat_priv,
753 "Deleting %pM from global tt entry %pM: %s\n",
754 orig_node->orig, tt_global_entry->common.addr,
757 tt_orig_list_entry_free_ref(orig_entry);
760 spin_unlock_bh(&tt_global_entry->list_lock);
763 static void tt_global_del_struct(struct bat_priv *bat_priv,
764 struct tt_global_entry *tt_global_entry,
767 bat_dbg(DBG_TT, bat_priv,
768 "Deleting global tt entry %pM: %s\n",
769 tt_global_entry->common.addr, message);
771 hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
772 tt_global_entry->common.addr);
773 tt_global_entry_free_ref(tt_global_entry);
777 /* If the client is to be deleted, we check if it is the last origantor entry
778 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
779 * otherwise we simply remove the originator scheduled for deletion.
781 static void tt_global_del_roaming(struct bat_priv *bat_priv,
782 struct tt_global_entry *tt_global_entry,
783 struct orig_node *orig_node,
786 bool last_entry = true;
787 struct hlist_head *head;
788 struct hlist_node *node;
789 struct tt_orig_list_entry *orig_entry;
791 /* no local entry exists, case 1:
792 * Check if this is the last one or if other entries exist.
796 head = &tt_global_entry->orig_list;
797 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
798 if (orig_entry->orig_node != orig_node) {
806 /* its the last one, mark for roaming. */
807 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
808 tt_global_entry->roam_at = jiffies;
810 /* there is another entry, we can simply delete this
811 * one and can still use the other one.
813 tt_global_del_orig_entry(bat_priv, tt_global_entry,
819 static void tt_global_del(struct bat_priv *bat_priv,
820 struct orig_node *orig_node,
821 const unsigned char *addr,
822 const char *message, bool roaming)
824 struct tt_global_entry *tt_global_entry = NULL;
825 struct tt_local_entry *tt_local_entry = NULL;
827 tt_global_entry = tt_global_hash_find(bat_priv, addr);
828 if (!tt_global_entry)
832 tt_global_del_orig_entry(bat_priv, tt_global_entry, orig_node,
835 if (hlist_empty(&tt_global_entry->orig_list))
836 tt_global_del_struct(bat_priv, tt_global_entry,
842 /* if we are deleting a global entry due to a roam
843 * event, there are two possibilities:
844 * 1) the client roamed from node A to node B => if there
845 * is only one originator left for this client, we mark
846 * it with TT_CLIENT_ROAM, we start a timer and we
847 * wait for node B to claim it. In case of timeout
848 * the entry is purged.
850 * If there are other originators left, we directly delete
852 * 2) the client roamed to us => we can directly delete
853 * the global entry, since it is useless now. */
855 tt_local_entry = tt_local_hash_find(bat_priv,
856 tt_global_entry->common.addr);
857 if (tt_local_entry) {
858 /* local entry exists, case 2: client roamed to us. */
859 tt_global_del_orig_list(tt_global_entry);
860 tt_global_del_struct(bat_priv, tt_global_entry, message);
862 /* no local entry exists, case 1: check for roaming */
863 tt_global_del_roaming(bat_priv, tt_global_entry, orig_node,
869 tt_global_entry_free_ref(tt_global_entry);
871 tt_local_entry_free_ref(tt_local_entry);
874 void tt_global_del_orig(struct bat_priv *bat_priv,
875 struct orig_node *orig_node, const char *message)
877 struct tt_global_entry *tt_global_entry;
878 struct tt_common_entry *tt_common_entry;
880 struct hashtable_t *hash = bat_priv->tt_global_hash;
881 struct hlist_node *node, *safe;
882 struct hlist_head *head;
883 spinlock_t *list_lock; /* protects write access to the hash lists */
888 for (i = 0; i < hash->size; i++) {
889 head = &hash->table[i];
890 list_lock = &hash->list_locks[i];
892 spin_lock_bh(list_lock);
893 hlist_for_each_entry_safe(tt_common_entry, node, safe,
895 tt_global_entry = container_of(tt_common_entry,
896 struct tt_global_entry,
899 tt_global_del_orig_entry(bat_priv, tt_global_entry,
902 if (hlist_empty(&tt_global_entry->orig_list)) {
903 bat_dbg(DBG_TT, bat_priv,
904 "Deleting global tt entry %pM: %s\n",
905 tt_global_entry->common.addr,
908 tt_global_entry_free_ref(tt_global_entry);
911 spin_unlock_bh(list_lock);
913 atomic_set(&orig_node->tt_size, 0);
914 orig_node->tt_initialised = false;
917 static void tt_global_roam_purge(struct bat_priv *bat_priv)
919 struct hashtable_t *hash = bat_priv->tt_global_hash;
920 struct tt_common_entry *tt_common_entry;
921 struct tt_global_entry *tt_global_entry;
922 struct hlist_node *node, *node_tmp;
923 struct hlist_head *head;
924 spinlock_t *list_lock; /* protects write access to the hash lists */
927 for (i = 0; i < hash->size; i++) {
928 head = &hash->table[i];
929 list_lock = &hash->list_locks[i];
931 spin_lock_bh(list_lock);
932 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
934 tt_global_entry = container_of(tt_common_entry,
935 struct tt_global_entry,
937 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
939 if (!has_timed_out(tt_global_entry->roam_at,
940 TT_CLIENT_ROAM_TIMEOUT))
943 bat_dbg(DBG_TT, bat_priv,
944 "Deleting global tt entry (%pM): Roaming timeout\n",
945 tt_global_entry->common.addr);
948 tt_global_entry_free_ref(tt_global_entry);
950 spin_unlock_bh(list_lock);
955 static void tt_global_table_free(struct bat_priv *bat_priv)
957 struct hashtable_t *hash;
958 spinlock_t *list_lock; /* protects write access to the hash lists */
959 struct tt_common_entry *tt_common_entry;
960 struct tt_global_entry *tt_global_entry;
961 struct hlist_node *node, *node_tmp;
962 struct hlist_head *head;
965 if (!bat_priv->tt_global_hash)
968 hash = bat_priv->tt_global_hash;
970 for (i = 0; i < hash->size; i++) {
971 head = &hash->table[i];
972 list_lock = &hash->list_locks[i];
974 spin_lock_bh(list_lock);
975 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
978 tt_global_entry = container_of(tt_common_entry,
979 struct tt_global_entry,
981 tt_global_entry_free_ref(tt_global_entry);
983 spin_unlock_bh(list_lock);
988 bat_priv->tt_global_hash = NULL;
991 static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
992 struct tt_global_entry *tt_global_entry)
996 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
997 tt_global_entry->common.flags & TT_CLIENT_WIFI)
1003 struct orig_node *transtable_search(struct bat_priv *bat_priv,
1004 const uint8_t *src, const uint8_t *addr)
1006 struct tt_local_entry *tt_local_entry = NULL;
1007 struct tt_global_entry *tt_global_entry = NULL;
1008 struct orig_node *orig_node = NULL;
1009 struct neigh_node *router = NULL;
1010 struct hlist_head *head;
1011 struct hlist_node *node;
1012 struct tt_orig_list_entry *orig_entry;
1015 if (src && atomic_read(&bat_priv->ap_isolation)) {
1016 tt_local_entry = tt_local_hash_find(bat_priv, src);
1017 if (!tt_local_entry)
1021 tt_global_entry = tt_global_hash_find(bat_priv, addr);
1022 if (!tt_global_entry)
1025 /* check whether the clients should not communicate due to AP
1027 if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
1033 head = &tt_global_entry->orig_list;
1034 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1035 router = orig_node_get_router(orig_entry->orig_node);
1039 if (router->tq_avg > best_tq) {
1040 orig_node = orig_entry->orig_node;
1041 best_tq = router->tq_avg;
1043 neigh_node_free_ref(router);
1045 /* found anything? */
1046 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1050 if (tt_global_entry)
1051 tt_global_entry_free_ref(tt_global_entry);
1053 tt_local_entry_free_ref(tt_local_entry);
1058 /* Calculates the checksum of the local table of a given orig_node */
1059 static uint16_t tt_global_crc(struct bat_priv *bat_priv,
1060 struct orig_node *orig_node)
1062 uint16_t total = 0, total_one;
1063 struct hashtable_t *hash = bat_priv->tt_global_hash;
1064 struct tt_common_entry *tt_common_entry;
1065 struct tt_global_entry *tt_global_entry;
1066 struct hlist_node *node;
1067 struct hlist_head *head;
1071 for (i = 0; i < hash->size; i++) {
1072 head = &hash->table[i];
1075 hlist_for_each_entry_rcu(tt_common_entry, node,
1077 tt_global_entry = container_of(tt_common_entry,
1078 struct tt_global_entry,
1080 /* Roaming clients are in the global table for
1081 * consistency only. They don't have to be
1082 * taken into account while computing the
1085 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1088 /* find out if this global entry is announced by this
1091 if (!tt_global_entry_has_orig(tt_global_entry,
1096 for (j = 0; j < ETH_ALEN; j++)
1097 total_one = crc16_byte(total_one,
1098 tt_global_entry->common.addr[j]);
1107 /* Calculates the checksum of the local table */
1108 uint16_t tt_local_crc(struct bat_priv *bat_priv)
1110 uint16_t total = 0, total_one;
1111 struct hashtable_t *hash = bat_priv->tt_local_hash;
1112 struct tt_common_entry *tt_common_entry;
1113 struct hlist_node *node;
1114 struct hlist_head *head;
1118 for (i = 0; i < hash->size; i++) {
1119 head = &hash->table[i];
1122 hlist_for_each_entry_rcu(tt_common_entry, node,
1124 /* not yet committed clients have not to be taken into
1125 * account while computing the CRC */
1126 if (tt_common_entry->flags & TT_CLIENT_NEW)
1129 for (j = 0; j < ETH_ALEN; j++)
1130 total_one = crc16_byte(total_one,
1131 tt_common_entry->addr[j]);
1140 static void tt_req_list_free(struct bat_priv *bat_priv)
1142 struct tt_req_node *node, *safe;
1144 spin_lock_bh(&bat_priv->tt_req_list_lock);
1146 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1147 list_del(&node->list);
1151 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1154 static void tt_save_orig_buffer(struct bat_priv *bat_priv,
1155 struct orig_node *orig_node,
1156 const unsigned char *tt_buff,
1157 uint8_t tt_num_changes)
1159 uint16_t tt_buff_len = tt_len(tt_num_changes);
1161 /* Replace the old buffer only if I received something in the
1162 * last OGM (the OGM could carry no changes) */
1163 spin_lock_bh(&orig_node->tt_buff_lock);
1164 if (tt_buff_len > 0) {
1165 kfree(orig_node->tt_buff);
1166 orig_node->tt_buff_len = 0;
1167 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1168 if (orig_node->tt_buff) {
1169 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1170 orig_node->tt_buff_len = tt_buff_len;
1173 spin_unlock_bh(&orig_node->tt_buff_lock);
1176 static void tt_req_purge(struct bat_priv *bat_priv)
1178 struct tt_req_node *node, *safe;
1180 spin_lock_bh(&bat_priv->tt_req_list_lock);
1181 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1182 if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
1183 list_del(&node->list);
1187 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1190 /* returns the pointer to the new tt_req_node struct if no request
1191 * has already been issued for this orig_node, NULL otherwise */
1192 static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
1193 struct orig_node *orig_node)
1195 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1197 spin_lock_bh(&bat_priv->tt_req_list_lock);
1198 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1199 if (compare_eth(tt_req_node_tmp, orig_node) &&
1200 !has_timed_out(tt_req_node_tmp->issued_at,
1201 TT_REQUEST_TIMEOUT))
1205 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1209 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1210 tt_req_node->issued_at = jiffies;
1212 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1214 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1218 /* data_ptr is useless here, but has to be kept to respect the prototype */
1219 static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
1221 const struct tt_common_entry *tt_common_entry = entry_ptr;
1223 if (tt_common_entry->flags & TT_CLIENT_NEW)
1228 static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
1230 const struct tt_common_entry *tt_common_entry = entry_ptr;
1231 const struct tt_global_entry *tt_global_entry;
1232 const struct orig_node *orig_node = data_ptr;
1234 if (tt_common_entry->flags & TT_CLIENT_ROAM)
1237 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1240 return tt_global_entry_has_orig(tt_global_entry, orig_node);
1243 static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1244 struct hashtable_t *hash,
1245 struct hard_iface *primary_if,
1246 int (*valid_cb)(const void *,
1250 struct tt_common_entry *tt_common_entry;
1251 struct tt_query_packet *tt_response;
1252 struct tt_change *tt_change;
1253 struct hlist_node *node;
1254 struct hlist_head *head;
1255 struct sk_buff *skb = NULL;
1256 uint16_t tt_tot, tt_count;
1257 ssize_t tt_query_size = sizeof(struct tt_query_packet);
1260 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1261 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1262 tt_len -= tt_len % sizeof(struct tt_change);
1264 tt_tot = tt_len / sizeof(struct tt_change);
1266 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1270 skb_reserve(skb, ETH_HLEN);
1271 tt_response = (struct tt_query_packet *)skb_put(skb,
1272 tt_query_size + tt_len);
1273 tt_response->ttvn = ttvn;
1275 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1279 for (i = 0; i < hash->size; i++) {
1280 head = &hash->table[i];
1282 hlist_for_each_entry_rcu(tt_common_entry, node,
1284 if (tt_count == tt_tot)
1287 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1290 memcpy(tt_change->addr, tt_common_entry->addr,
1292 tt_change->flags = NO_FLAGS;
1300 /* store in the message the number of entries we have successfully
1302 tt_response->tt_data = htons(tt_count);
1308 static int send_tt_request(struct bat_priv *bat_priv,
1309 struct orig_node *dst_orig_node,
1310 uint8_t ttvn, uint16_t tt_crc, bool full_table)
1312 struct sk_buff *skb = NULL;
1313 struct tt_query_packet *tt_request;
1314 struct neigh_node *neigh_node = NULL;
1315 struct hard_iface *primary_if;
1316 struct tt_req_node *tt_req_node = NULL;
1319 primary_if = primary_if_get_selected(bat_priv);
1323 /* The new tt_req will be issued only if I'm not waiting for a
1324 * reply from the same orig_node yet */
1325 tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1329 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1333 skb_reserve(skb, ETH_HLEN);
1335 tt_request = (struct tt_query_packet *)skb_put(skb,
1336 sizeof(struct tt_query_packet));
1338 tt_request->header.packet_type = BAT_TT_QUERY;
1339 tt_request->header.version = COMPAT_VERSION;
1340 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1341 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1342 tt_request->header.ttl = TTL;
1343 tt_request->ttvn = ttvn;
1344 tt_request->tt_data = htons(tt_crc);
1345 tt_request->flags = TT_REQUEST;
1348 tt_request->flags |= TT_FULL_TABLE;
1350 neigh_node = orig_node_get_router(dst_orig_node);
1354 bat_dbg(DBG_TT, bat_priv,
1355 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1356 dst_orig_node->orig, neigh_node->addr,
1357 (full_table ? 'F' : '.'));
1359 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1364 neigh_node_free_ref(neigh_node);
1366 hardif_free_ref(primary_if);
1369 if (ret && tt_req_node) {
1370 spin_lock_bh(&bat_priv->tt_req_list_lock);
1371 list_del(&tt_req_node->list);
1372 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1378 static bool send_other_tt_response(struct bat_priv *bat_priv,
1379 struct tt_query_packet *tt_request)
1381 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1382 struct neigh_node *neigh_node = NULL;
1383 struct hard_iface *primary_if = NULL;
1384 uint8_t orig_ttvn, req_ttvn, ttvn;
1386 unsigned char *tt_buff;
1388 uint16_t tt_len, tt_tot;
1389 struct sk_buff *skb = NULL;
1390 struct tt_query_packet *tt_response;
1392 bat_dbg(DBG_TT, bat_priv,
1393 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1394 tt_request->src, tt_request->ttvn, tt_request->dst,
1395 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1397 /* Let's get the orig node of the REAL destination */
1398 req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
1399 if (!req_dst_orig_node)
1402 res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
1403 if (!res_dst_orig_node)
1406 neigh_node = orig_node_get_router(res_dst_orig_node);
1410 primary_if = primary_if_get_selected(bat_priv);
1414 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1415 req_ttvn = tt_request->ttvn;
1417 /* I don't have the requested data */
1418 if (orig_ttvn != req_ttvn ||
1419 tt_request->tt_data != req_dst_orig_node->tt_crc)
1422 /* If the full table has been explicitly requested */
1423 if (tt_request->flags & TT_FULL_TABLE ||
1424 !req_dst_orig_node->tt_buff)
1429 /* In this version, fragmentation is not implemented, then
1430 * I'll send only one packet with as much TT entries as I can */
1432 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1433 tt_len = req_dst_orig_node->tt_buff_len;
1434 tt_tot = tt_len / sizeof(struct tt_change);
1436 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1441 skb_reserve(skb, ETH_HLEN);
1442 tt_response = (struct tt_query_packet *)skb_put(skb,
1443 sizeof(struct tt_query_packet) + tt_len);
1444 tt_response->ttvn = req_ttvn;
1445 tt_response->tt_data = htons(tt_tot);
1447 tt_buff = skb->data + sizeof(struct tt_query_packet);
1448 /* Copy the last orig_node's OGM buffer */
1449 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1450 req_dst_orig_node->tt_buff_len);
1452 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1454 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1455 sizeof(struct tt_change);
1456 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1458 skb = tt_response_fill_table(tt_len, ttvn,
1459 bat_priv->tt_global_hash,
1460 primary_if, tt_global_valid_entry,
1465 tt_response = (struct tt_query_packet *)skb->data;
1468 tt_response->header.packet_type = BAT_TT_QUERY;
1469 tt_response->header.version = COMPAT_VERSION;
1470 tt_response->header.ttl = TTL;
1471 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1472 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1473 tt_response->flags = TT_RESPONSE;
1476 tt_response->flags |= TT_FULL_TABLE;
1478 bat_dbg(DBG_TT, bat_priv,
1479 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1480 res_dst_orig_node->orig, neigh_node->addr,
1481 req_dst_orig_node->orig, req_ttvn);
1483 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1488 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1491 if (res_dst_orig_node)
1492 orig_node_free_ref(res_dst_orig_node);
1493 if (req_dst_orig_node)
1494 orig_node_free_ref(req_dst_orig_node);
1496 neigh_node_free_ref(neigh_node);
1498 hardif_free_ref(primary_if);
1504 static bool send_my_tt_response(struct bat_priv *bat_priv,
1505 struct tt_query_packet *tt_request)
1507 struct orig_node *orig_node = NULL;
1508 struct neigh_node *neigh_node = NULL;
1509 struct hard_iface *primary_if = NULL;
1510 uint8_t my_ttvn, req_ttvn, ttvn;
1512 unsigned char *tt_buff;
1514 uint16_t tt_len, tt_tot;
1515 struct sk_buff *skb = NULL;
1516 struct tt_query_packet *tt_response;
1518 bat_dbg(DBG_TT, bat_priv,
1519 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1520 tt_request->src, tt_request->ttvn,
1521 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1524 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1525 req_ttvn = tt_request->ttvn;
1527 orig_node = orig_hash_find(bat_priv, tt_request->src);
1531 neigh_node = orig_node_get_router(orig_node);
1535 primary_if = primary_if_get_selected(bat_priv);
1539 /* If the full table has been explicitly requested or the gap
1540 * is too big send the whole local translation table */
1541 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1547 /* In this version, fragmentation is not implemented, then
1548 * I'll send only one packet with as much TT entries as I can */
1550 spin_lock_bh(&bat_priv->tt_buff_lock);
1551 tt_len = bat_priv->tt_buff_len;
1552 tt_tot = tt_len / sizeof(struct tt_change);
1554 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1559 skb_reserve(skb, ETH_HLEN);
1560 tt_response = (struct tt_query_packet *)skb_put(skb,
1561 sizeof(struct tt_query_packet) + tt_len);
1562 tt_response->ttvn = req_ttvn;
1563 tt_response->tt_data = htons(tt_tot);
1565 tt_buff = skb->data + sizeof(struct tt_query_packet);
1566 memcpy(tt_buff, bat_priv->tt_buff,
1567 bat_priv->tt_buff_len);
1568 spin_unlock_bh(&bat_priv->tt_buff_lock);
1570 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1571 sizeof(struct tt_change);
1572 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1574 skb = tt_response_fill_table(tt_len, ttvn,
1575 bat_priv->tt_local_hash,
1576 primary_if, tt_local_valid_entry,
1581 tt_response = (struct tt_query_packet *)skb->data;
1584 tt_response->header.packet_type = BAT_TT_QUERY;
1585 tt_response->header.version = COMPAT_VERSION;
1586 tt_response->header.ttl = TTL;
1587 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1588 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1589 tt_response->flags = TT_RESPONSE;
1592 tt_response->flags |= TT_FULL_TABLE;
1594 bat_dbg(DBG_TT, bat_priv,
1595 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1596 orig_node->orig, neigh_node->addr,
1597 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1599 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1604 spin_unlock_bh(&bat_priv->tt_buff_lock);
1607 orig_node_free_ref(orig_node);
1609 neigh_node_free_ref(neigh_node);
1611 hardif_free_ref(primary_if);
1614 /* This packet was for me, so it doesn't need to be re-routed */
1618 bool send_tt_response(struct bat_priv *bat_priv,
1619 struct tt_query_packet *tt_request)
1621 if (is_my_mac(tt_request->dst)) {
1622 /* don't answer backbone gws! */
1623 if (bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1626 return send_my_tt_response(bat_priv, tt_request);
1628 return send_other_tt_response(bat_priv, tt_request);
1632 static void _tt_update_changes(struct bat_priv *bat_priv,
1633 struct orig_node *orig_node,
1634 struct tt_change *tt_change,
1635 uint16_t tt_num_changes, uint8_t ttvn)
1639 for (i = 0; i < tt_num_changes; i++) {
1640 if ((tt_change + i)->flags & TT_CLIENT_DEL)
1641 tt_global_del(bat_priv, orig_node,
1642 (tt_change + i)->addr,
1643 "tt removed by changes",
1644 (tt_change + i)->flags & TT_CLIENT_ROAM);
1646 if (!tt_global_add(bat_priv, orig_node,
1647 (tt_change + i)->addr, ttvn, false,
1648 (tt_change + i)->flags &
1650 /* In case of problem while storing a
1651 * global_entry, we stop the updating
1652 * procedure without committing the
1653 * ttvn change. This will avoid to send
1654 * corrupted data on tt_request
1658 orig_node->tt_initialised = true;
1661 static void tt_fill_gtable(struct bat_priv *bat_priv,
1662 struct tt_query_packet *tt_response)
1664 struct orig_node *orig_node = NULL;
1666 orig_node = orig_hash_find(bat_priv, tt_response->src);
1670 /* Purge the old table first.. */
1671 tt_global_del_orig(bat_priv, orig_node, "Received full table");
1673 _tt_update_changes(bat_priv, orig_node,
1674 (struct tt_change *)(tt_response + 1),
1675 tt_response->tt_data, tt_response->ttvn);
1677 spin_lock_bh(&orig_node->tt_buff_lock);
1678 kfree(orig_node->tt_buff);
1679 orig_node->tt_buff_len = 0;
1680 orig_node->tt_buff = NULL;
1681 spin_unlock_bh(&orig_node->tt_buff_lock);
1683 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1687 orig_node_free_ref(orig_node);
1690 static void tt_update_changes(struct bat_priv *bat_priv,
1691 struct orig_node *orig_node,
1692 uint16_t tt_num_changes, uint8_t ttvn,
1693 struct tt_change *tt_change)
1695 _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1698 tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1700 atomic_set(&orig_node->last_ttvn, ttvn);
1703 bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1705 struct tt_local_entry *tt_local_entry = NULL;
1708 tt_local_entry = tt_local_hash_find(bat_priv, addr);
1709 if (!tt_local_entry)
1711 /* Check if the client has been logically deleted (but is kept for
1712 * consistency purpose) */
1713 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
1718 tt_local_entry_free_ref(tt_local_entry);
1722 void handle_tt_response(struct bat_priv *bat_priv,
1723 struct tt_query_packet *tt_response)
1725 struct tt_req_node *node, *safe;
1726 struct orig_node *orig_node = NULL;
1728 bat_dbg(DBG_TT, bat_priv,
1729 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1730 tt_response->src, tt_response->ttvn, tt_response->tt_data,
1731 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1733 /* we should have never asked a backbone gw */
1734 if (bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1737 orig_node = orig_hash_find(bat_priv, tt_response->src);
1741 if (tt_response->flags & TT_FULL_TABLE)
1742 tt_fill_gtable(bat_priv, tt_response);
1744 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1746 (struct tt_change *)(tt_response + 1));
1748 /* Delete the tt_req_node from pending tt_requests list */
1749 spin_lock_bh(&bat_priv->tt_req_list_lock);
1750 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1751 if (!compare_eth(node->addr, tt_response->src))
1753 list_del(&node->list);
1756 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1758 /* Recalculate the CRC for this orig_node and store it */
1759 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1760 /* Roaming phase is over: tables are in sync again. I can
1762 orig_node->tt_poss_change = false;
1765 orig_node_free_ref(orig_node);
1768 int tt_init(struct bat_priv *bat_priv)
1770 if (!tt_local_init(bat_priv))
1773 if (!tt_global_init(bat_priv))
1776 tt_start_timer(bat_priv);
1781 static void tt_roam_list_free(struct bat_priv *bat_priv)
1783 struct tt_roam_node *node, *safe;
1785 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1787 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1788 list_del(&node->list);
1792 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1795 static void tt_roam_purge(struct bat_priv *bat_priv)
1797 struct tt_roam_node *node, *safe;
1799 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1800 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1801 if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
1804 list_del(&node->list);
1807 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1810 /* This function checks whether the client already reached the
1811 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1814 * returns true if the ROAMING_ADV can be sent, false otherwise */
1815 static bool tt_check_roam_count(struct bat_priv *bat_priv,
1818 struct tt_roam_node *tt_roam_node;
1821 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1822 /* The new tt_req will be issued only if I'm not waiting for a
1823 * reply from the same orig_node yet */
1824 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1825 if (!compare_eth(tt_roam_node->addr, client))
1828 if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
1831 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1832 /* Sorry, you roamed too many times! */
1839 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1843 tt_roam_node->first_time = jiffies;
1844 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1845 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1847 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1852 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1856 static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1857 struct orig_node *orig_node)
1859 struct neigh_node *neigh_node = NULL;
1860 struct sk_buff *skb = NULL;
1861 struct roam_adv_packet *roam_adv_packet;
1863 struct hard_iface *primary_if;
1865 /* before going on we have to check whether the client has
1866 * already roamed to us too many times */
1867 if (!tt_check_roam_count(bat_priv, client))
1870 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1874 skb_reserve(skb, ETH_HLEN);
1876 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1877 sizeof(struct roam_adv_packet));
1879 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1880 roam_adv_packet->header.version = COMPAT_VERSION;
1881 roam_adv_packet->header.ttl = TTL;
1882 primary_if = primary_if_get_selected(bat_priv);
1885 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1886 hardif_free_ref(primary_if);
1887 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1888 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1890 neigh_node = orig_node_get_router(orig_node);
1894 bat_dbg(DBG_TT, bat_priv,
1895 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1896 orig_node->orig, client, neigh_node->addr);
1898 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1903 neigh_node_free_ref(neigh_node);
1909 static void tt_purge(struct work_struct *work)
1911 struct delayed_work *delayed_work =
1912 container_of(work, struct delayed_work, work);
1913 struct bat_priv *bat_priv =
1914 container_of(delayed_work, struct bat_priv, tt_work);
1916 tt_local_purge(bat_priv);
1917 tt_global_roam_purge(bat_priv);
1918 tt_req_purge(bat_priv);
1919 tt_roam_purge(bat_priv);
1921 tt_start_timer(bat_priv);
1924 void tt_free(struct bat_priv *bat_priv)
1926 cancel_delayed_work_sync(&bat_priv->tt_work);
1928 tt_local_table_free(bat_priv);
1929 tt_global_table_free(bat_priv);
1930 tt_req_list_free(bat_priv);
1931 tt_changes_list_free(bat_priv);
1932 tt_roam_list_free(bat_priv);
1934 kfree(bat_priv->tt_buff);
1937 /* This function will enable or disable the specified flags for all the entries
1938 * in the given hash table and returns the number of modified entries */
1939 static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
1943 uint16_t changed_num = 0;
1944 struct hlist_head *head;
1945 struct hlist_node *node;
1946 struct tt_common_entry *tt_common_entry;
1951 for (i = 0; i < hash->size; i++) {
1952 head = &hash->table[i];
1955 hlist_for_each_entry_rcu(tt_common_entry, node,
1958 if ((tt_common_entry->flags & flags) == flags)
1960 tt_common_entry->flags |= flags;
1962 if (!(tt_common_entry->flags & flags))
1964 tt_common_entry->flags &= ~flags;
1974 /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1975 static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
1977 struct hashtable_t *hash = bat_priv->tt_local_hash;
1978 struct tt_common_entry *tt_common_entry;
1979 struct tt_local_entry *tt_local_entry;
1980 struct hlist_node *node, *node_tmp;
1981 struct hlist_head *head;
1982 spinlock_t *list_lock; /* protects write access to the hash lists */
1988 for (i = 0; i < hash->size; i++) {
1989 head = &hash->table[i];
1990 list_lock = &hash->list_locks[i];
1992 spin_lock_bh(list_lock);
1993 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1995 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
1998 bat_dbg(DBG_TT, bat_priv,
1999 "Deleting local tt entry (%pM): pending\n",
2000 tt_common_entry->addr);
2002 atomic_dec(&bat_priv->num_local_tt);
2003 hlist_del_rcu(node);
2004 tt_local_entry = container_of(tt_common_entry,
2005 struct tt_local_entry,
2007 tt_local_entry_free_ref(tt_local_entry);
2009 spin_unlock_bh(list_lock);
2014 void tt_commit_changes(struct bat_priv *bat_priv)
2016 uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
2017 TT_CLIENT_NEW, false);
2018 /* all the reset entries have now to be effectively counted as local
2020 atomic_add(changed_num, &bat_priv->num_local_tt);
2021 tt_local_purge_pending_clients(bat_priv);
2023 /* Increment the TTVN only once per OGM interval */
2024 atomic_inc(&bat_priv->ttvn);
2025 bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n",
2026 (uint8_t)atomic_read(&bat_priv->ttvn));
2027 bat_priv->tt_poss_change = false;
2030 bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
2032 struct tt_local_entry *tt_local_entry = NULL;
2033 struct tt_global_entry *tt_global_entry = NULL;
2036 if (!atomic_read(&bat_priv->ap_isolation))
2039 tt_local_entry = tt_local_hash_find(bat_priv, dst);
2040 if (!tt_local_entry)
2043 tt_global_entry = tt_global_hash_find(bat_priv, src);
2044 if (!tt_global_entry)
2047 if (_is_ap_isolated(tt_local_entry, tt_global_entry))
2053 if (tt_global_entry)
2054 tt_global_entry_free_ref(tt_global_entry);
2056 tt_local_entry_free_ref(tt_local_entry);
2060 void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
2061 const unsigned char *tt_buff, uint8_t tt_num_changes,
2062 uint8_t ttvn, uint16_t tt_crc)
2064 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2065 bool full_table = true;
2067 /* don't care about a backbone gateways updates. */
2068 if (bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2071 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2072 * increased by one -> we can apply the attached changes */
2073 if ((!orig_node->tt_initialised && ttvn == 1) ||
2074 ttvn - orig_ttvn == 1) {
2075 /* the OGM could not contain the changes due to their size or
2076 * because they have already been sent TT_OGM_APPEND_MAX times.
2077 * In this case send a tt request */
2078 if (!tt_num_changes) {
2083 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
2084 (struct tt_change *)tt_buff);
2086 /* Even if we received the precomputed crc with the OGM, we
2087 * prefer to recompute it to spot any possible inconsistency
2088 * in the global table */
2089 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
2091 /* The ttvn alone is not enough to guarantee consistency
2092 * because a single value could represent different states
2093 * (due to the wrap around). Thus a node has to check whether
2094 * the resulting table (after applying the changes) is still
2095 * consistent or not. E.g. a node could disconnect while its
2096 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2097 * checking the CRC value is mandatory to detect the
2099 if (orig_node->tt_crc != tt_crc)
2102 /* Roaming phase is over: tables are in sync again. I can
2104 orig_node->tt_poss_change = false;
2106 /* if we missed more than one change or our tables are not
2107 * in sync anymore -> request fresh tt data */
2109 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2110 orig_node->tt_crc != tt_crc) {
2112 bat_dbg(DBG_TT, bat_priv,
2113 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2114 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2115 orig_node->tt_crc, tt_num_changes);
2116 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
2123 /* returns true whether we know that the client has moved from its old
2124 * originator to another one. This entry is kept is still kept for consistency
2127 bool tt_global_client_is_roaming(struct bat_priv *bat_priv, uint8_t *addr)
2129 struct tt_global_entry *tt_global_entry;
2132 tt_global_entry = tt_global_hash_find(bat_priv, addr);
2133 if (!tt_global_entry)
2136 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
2137 tt_global_entry_free_ref(tt_global_entry);