1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "translation-table.h"
22 #include "soft-interface.h"
23 #include "hard-interface.h"
26 #include "originator.h"
28 #include "bridge_loop_avoidance.h"
30 #include <linux/crc16.h>
32 static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
33 struct orig_node *orig_node);
34 static void batadv_tt_purge(struct work_struct *work);
36 batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
38 /* returns 1 if they are the same mac addr */
39 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
41 const void *data1 = container_of(node, struct tt_common_entry,
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
47 static void batadv_tt_start_timer(struct bat_priv *bat_priv)
49 INIT_DELAYED_WORK(&bat_priv->tt_work, batadv_tt_purge);
50 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt_work,
51 msecs_to_jiffies(5000));
54 static struct tt_common_entry *batadv_tt_hash_find(struct hashtable_t *hash,
57 struct hlist_head *head;
58 struct hlist_node *node;
59 struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
65 index = batadv_choose_orig(data, hash->size);
66 head = &hash->table[index];
69 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
70 if (!batadv_compare_eth(tt_common_entry, data))
73 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
76 tt_common_entry_tmp = tt_common_entry;
81 return tt_common_entry_tmp;
84 static struct tt_local_entry *
85 batadv_tt_local_hash_find(struct bat_priv *bat_priv, const void *data)
87 struct tt_common_entry *tt_common_entry;
88 struct tt_local_entry *tt_local_entry = NULL;
90 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_local_hash, data);
92 tt_local_entry = container_of(tt_common_entry,
93 struct tt_local_entry, common);
94 return tt_local_entry;
97 static struct tt_global_entry *
98 batadv_tt_global_hash_find(struct bat_priv *bat_priv, const void *data)
100 struct tt_common_entry *tt_common_entry;
101 struct tt_global_entry *tt_global_entry = NULL;
103 tt_common_entry = batadv_tt_hash_find(bat_priv->tt_global_hash, data);
105 tt_global_entry = container_of(tt_common_entry,
106 struct tt_global_entry, common);
107 return tt_global_entry;
112 batadv_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 batadv_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);
131 batadv_tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
133 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
134 batadv_tt_global_del_orig_list(tt_global_entry);
135 call_rcu(&tt_global_entry->common.rcu,
136 batadv_tt_global_entry_free_rcu);
140 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
142 struct tt_orig_list_entry *orig_entry;
144 orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
145 batadv_orig_node_free_ref(orig_entry->orig_node);
150 batadv_tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
152 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
155 static void batadv_tt_local_event(struct bat_priv *bat_priv,
156 const uint8_t *addr, uint8_t flags)
158 struct tt_change_node *tt_change_node;
160 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
165 tt_change_node->change.flags = flags;
166 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
168 spin_lock_bh(&bat_priv->tt_changes_list_lock);
169 /* track the change in the OGMinterval list */
170 list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
171 atomic_inc(&bat_priv->tt_local_changes);
172 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
174 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
177 int batadv_tt_len(int changes_num)
179 return changes_num * sizeof(struct tt_change);
182 static int batadv_tt_local_init(struct bat_priv *bat_priv)
184 if (bat_priv->tt_local_hash)
187 bat_priv->tt_local_hash = batadv_hash_new(1024);
189 if (!bat_priv->tt_local_hash)
195 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
198 struct bat_priv *bat_priv = netdev_priv(soft_iface);
199 struct tt_local_entry *tt_local_entry = NULL;
200 struct tt_global_entry *tt_global_entry = NULL;
201 struct hlist_head *head;
202 struct hlist_node *node;
203 struct tt_orig_list_entry *orig_entry;
206 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
208 if (tt_local_entry) {
209 tt_local_entry->last_seen = jiffies;
210 /* possibly unset the TT_CLIENT_PENDING flag */
211 tt_local_entry->common.flags &= ~TT_CLIENT_PENDING;
215 tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
219 batadv_dbg(DBG_TT, bat_priv,
220 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
221 (uint8_t)atomic_read(&bat_priv->ttvn));
223 memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
224 tt_local_entry->common.flags = NO_FLAGS;
225 if (batadv_is_wifi_iface(ifindex))
226 tt_local_entry->common.flags |= TT_CLIENT_WIFI;
227 atomic_set(&tt_local_entry->common.refcount, 2);
228 tt_local_entry->last_seen = jiffies;
230 /* the batman interface mac address should never be purged */
231 if (batadv_compare_eth(addr, soft_iface->dev_addr))
232 tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
234 /* The local entry has to be marked as NEW to avoid to send it in
235 * a full table response going out before the next ttvn increment
236 * (consistency check)
238 tt_local_entry->common.flags |= TT_CLIENT_NEW;
240 hash_added = batadv_hash_add(bat_priv->tt_local_hash, batadv_compare_tt,
242 &tt_local_entry->common,
243 &tt_local_entry->common.hash_entry);
245 if (unlikely(hash_added != 0)) {
246 /* remove the reference for the hash */
247 batadv_tt_local_entry_free_ref(tt_local_entry);
251 batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
253 /* remove address from global hash if present */
254 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
256 /* Check whether it is a roaming! */
257 if (tt_global_entry) {
258 /* These node are probably going to update their tt table */
259 head = &tt_global_entry->orig_list;
261 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
262 orig_entry->orig_node->tt_poss_change = true;
264 batadv_send_roam_adv(bat_priv,
265 tt_global_entry->common.addr,
266 orig_entry->orig_node);
269 /* The global entry has to be marked as ROAMING and
270 * has to be kept for consistency purpose
272 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
273 tt_global_entry->roam_at = jiffies;
277 batadv_tt_local_entry_free_ref(tt_local_entry);
279 batadv_tt_global_entry_free_ref(tt_global_entry);
282 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
283 int *packet_buff_len,
287 unsigned char *new_buff;
289 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
291 /* keep old buffer if kmalloc should fail */
293 memcpy(new_buff, *packet_buff, min_packet_len);
295 *packet_buff = new_buff;
296 *packet_buff_len = new_packet_len;
300 static void batadv_tt_prepare_packet_buff(struct bat_priv *bat_priv,
301 unsigned char **packet_buff,
302 int *packet_buff_len,
305 struct hard_iface *primary_if;
308 primary_if = batadv_primary_if_get_selected(bat_priv);
310 req_len = min_packet_len;
311 req_len += batadv_tt_len(atomic_read(&bat_priv->tt_local_changes));
313 /* if we have too many changes for one packet don't send any
314 * and wait for the tt table request which will be fragmented
316 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
317 req_len = min_packet_len;
319 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
320 min_packet_len, req_len);
323 batadv_hardif_free_ref(primary_if);
326 static int batadv_tt_changes_fill_buff(struct bat_priv *bat_priv,
327 unsigned char **packet_buff,
328 int *packet_buff_len,
331 struct tt_change_node *entry, *safe;
332 int count = 0, tot_changes = 0, new_len;
333 unsigned char *tt_buff;
335 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
336 packet_buff_len, min_packet_len);
338 new_len = *packet_buff_len - min_packet_len;
339 tt_buff = *packet_buff + min_packet_len;
342 tot_changes = new_len / batadv_tt_len(1);
344 spin_lock_bh(&bat_priv->tt_changes_list_lock);
345 atomic_set(&bat_priv->tt_local_changes, 0);
347 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
349 if (count < tot_changes) {
350 memcpy(tt_buff + batadv_tt_len(count),
351 &entry->change, sizeof(struct tt_change));
354 list_del(&entry->list);
357 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
359 /* Keep the buffer for possible tt_request */
360 spin_lock_bh(&bat_priv->tt_buff_lock);
361 kfree(bat_priv->tt_buff);
362 bat_priv->tt_buff_len = 0;
363 bat_priv->tt_buff = NULL;
364 /* check whether this new OGM has no changes due to size problems */
366 /* if kmalloc() fails we will reply with the full table
367 * instead of providing the diff
369 bat_priv->tt_buff = kmalloc(new_len, GFP_ATOMIC);
370 if (bat_priv->tt_buff) {
371 memcpy(bat_priv->tt_buff, tt_buff, new_len);
372 bat_priv->tt_buff_len = new_len;
375 spin_unlock_bh(&bat_priv->tt_buff_lock);
380 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
382 struct net_device *net_dev = (struct net_device *)seq->private;
383 struct bat_priv *bat_priv = netdev_priv(net_dev);
384 struct hashtable_t *hash = bat_priv->tt_local_hash;
385 struct tt_common_entry *tt_common_entry;
386 struct hard_iface *primary_if;
387 struct hlist_node *node;
388 struct hlist_head *head;
392 primary_if = batadv_primary_if_get_selected(bat_priv);
394 ret = seq_printf(seq,
395 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
400 if (primary_if->if_status != IF_ACTIVE) {
401 ret = seq_printf(seq,
402 "BATMAN mesh %s disabled - primary interface not active\n",
408 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
409 net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
411 for (i = 0; i < hash->size; i++) {
412 head = &hash->table[i];
415 hlist_for_each_entry_rcu(tt_common_entry, node,
417 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
418 tt_common_entry->addr,
419 (tt_common_entry->flags &
420 TT_CLIENT_ROAM ? 'R' : '.'),
421 (tt_common_entry->flags &
422 TT_CLIENT_NOPURGE ? 'P' : '.'),
423 (tt_common_entry->flags &
424 TT_CLIENT_NEW ? 'N' : '.'),
425 (tt_common_entry->flags &
426 TT_CLIENT_PENDING ? 'X' : '.'),
427 (tt_common_entry->flags &
428 TT_CLIENT_WIFI ? 'W' : '.'));
434 batadv_hardif_free_ref(primary_if);
438 static void batadv_tt_local_set_pending(struct bat_priv *bat_priv,
439 struct tt_local_entry *tt_local_entry,
440 uint16_t flags, const char *message)
442 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
443 tt_local_entry->common.flags | flags);
445 /* The local client has to be marked as "pending to be removed" but has
446 * to be kept in the table in order to send it in a full table
447 * response issued before the net ttvn increment (consistency check)
449 tt_local_entry->common.flags |= TT_CLIENT_PENDING;
451 batadv_dbg(DBG_TT, bat_priv,
452 "Local tt entry (%pM) pending to be removed: %s\n",
453 tt_local_entry->common.addr, message);
456 void batadv_tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
457 const char *message, bool roaming)
459 struct tt_local_entry *tt_local_entry = NULL;
461 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
465 batadv_tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
466 (roaming ? TT_CLIENT_ROAM : NO_FLAGS),
470 batadv_tt_local_entry_free_ref(tt_local_entry);
473 static void batadv_tt_local_purge(struct bat_priv *bat_priv)
475 struct hashtable_t *hash = bat_priv->tt_local_hash;
476 struct tt_local_entry *tt_local_entry;
477 struct tt_common_entry *tt_common_entry;
478 struct hlist_node *node, *node_tmp;
479 struct hlist_head *head;
480 spinlock_t *list_lock; /* protects write access to the hash lists */
483 for (i = 0; i < hash->size; i++) {
484 head = &hash->table[i];
485 list_lock = &hash->list_locks[i];
487 spin_lock_bh(list_lock);
488 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
490 tt_local_entry = container_of(tt_common_entry,
491 struct tt_local_entry,
493 if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
496 /* entry already marked for deletion */
497 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
500 if (!batadv_has_timed_out(tt_local_entry->last_seen,
504 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
505 TT_CLIENT_DEL, "timed out");
507 spin_unlock_bh(list_lock);
512 static void batadv_tt_local_table_free(struct bat_priv *bat_priv)
514 struct hashtable_t *hash;
515 spinlock_t *list_lock; /* protects write access to the hash lists */
516 struct tt_common_entry *tt_common_entry;
517 struct tt_local_entry *tt_local_entry;
518 struct hlist_node *node, *node_tmp;
519 struct hlist_head *head;
522 if (!bat_priv->tt_local_hash)
525 hash = bat_priv->tt_local_hash;
527 for (i = 0; i < hash->size; i++) {
528 head = &hash->table[i];
529 list_lock = &hash->list_locks[i];
531 spin_lock_bh(list_lock);
532 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
535 tt_local_entry = container_of(tt_common_entry,
536 struct tt_local_entry,
538 batadv_tt_local_entry_free_ref(tt_local_entry);
540 spin_unlock_bh(list_lock);
543 batadv_hash_destroy(hash);
545 bat_priv->tt_local_hash = NULL;
548 static int batadv_tt_global_init(struct bat_priv *bat_priv)
550 if (bat_priv->tt_global_hash)
553 bat_priv->tt_global_hash = batadv_hash_new(1024);
555 if (!bat_priv->tt_global_hash)
561 static void batadv_tt_changes_list_free(struct bat_priv *bat_priv)
563 struct tt_change_node *entry, *safe;
565 spin_lock_bh(&bat_priv->tt_changes_list_lock);
567 list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
569 list_del(&entry->list);
573 atomic_set(&bat_priv->tt_local_changes, 0);
574 spin_unlock_bh(&bat_priv->tt_changes_list_lock);
577 /* find out if an orig_node is already in the list of a tt_global_entry.
578 * returns 1 if found, 0 otherwise
580 static bool batadv_tt_global_entry_has_orig(const struct tt_global_entry *entry,
581 const struct orig_node *orig_node)
583 struct tt_orig_list_entry *tmp_orig_entry;
584 const struct hlist_head *head;
585 struct hlist_node *node;
589 head = &entry->orig_list;
590 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
591 if (tmp_orig_entry->orig_node == orig_node) {
601 batadv_tt_global_add_orig_entry(struct tt_global_entry *tt_global_entry,
602 struct orig_node *orig_node, int ttvn)
604 struct tt_orig_list_entry *orig_entry;
606 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
610 INIT_HLIST_NODE(&orig_entry->list);
611 atomic_inc(&orig_node->refcount);
612 atomic_inc(&orig_node->tt_size);
613 orig_entry->orig_node = orig_node;
614 orig_entry->ttvn = ttvn;
616 spin_lock_bh(&tt_global_entry->list_lock);
617 hlist_add_head_rcu(&orig_entry->list,
618 &tt_global_entry->orig_list);
619 spin_unlock_bh(&tt_global_entry->list_lock);
622 /* caller must hold orig_node refcount */
623 int batadv_tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
624 const unsigned char *tt_addr, uint8_t ttvn,
625 bool roaming, bool wifi)
627 struct tt_global_entry *tt_global_entry = NULL;
630 struct tt_common_entry *common;
632 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
634 if (!tt_global_entry) {
635 tt_global_entry = kzalloc(sizeof(*tt_global_entry),
637 if (!tt_global_entry)
640 common = &tt_global_entry->common;
641 memcpy(common->addr, tt_addr, ETH_ALEN);
643 common->flags = NO_FLAGS;
644 tt_global_entry->roam_at = 0;
645 atomic_set(&common->refcount, 2);
647 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
648 spin_lock_init(&tt_global_entry->list_lock);
650 hash_added = batadv_hash_add(bat_priv->tt_global_hash,
652 batadv_choose_orig, common,
653 &common->hash_entry);
655 if (unlikely(hash_added != 0)) {
656 /* remove the reference for the hash */
657 batadv_tt_global_entry_free_ref(tt_global_entry);
661 batadv_tt_global_add_orig_entry(tt_global_entry, orig_node,
664 /* there is already a global entry, use this one. */
666 /* If there is the TT_CLIENT_ROAM flag set, there is only one
667 * originator left in the list and we previously received a
668 * delete + roaming change for this originator.
670 * We should first delete the old originator before adding the
673 if (tt_global_entry->common.flags & TT_CLIENT_ROAM) {
674 batadv_tt_global_del_orig_list(tt_global_entry);
675 tt_global_entry->common.flags &= ~TT_CLIENT_ROAM;
676 tt_global_entry->roam_at = 0;
679 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
681 batadv_tt_global_add_orig_entry(tt_global_entry,
686 tt_global_entry->common.flags |= TT_CLIENT_WIFI;
688 batadv_dbg(DBG_TT, bat_priv,
689 "Creating new global tt entry: %pM (via %pM)\n",
690 tt_global_entry->common.addr, orig_node->orig);
693 /* remove address from local hash if present */
694 batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
695 "global tt received", roaming);
699 batadv_tt_global_entry_free_ref(tt_global_entry);
703 /* print all orig nodes who announce the address for this global entry.
704 * it is assumed that the caller holds rcu_read_lock();
707 batadv_tt_global_print_entry(struct tt_global_entry *tt_global_entry,
708 struct seq_file *seq)
710 struct hlist_head *head;
711 struct hlist_node *node;
712 struct tt_orig_list_entry *orig_entry;
713 struct tt_common_entry *tt_common_entry;
717 tt_common_entry = &tt_global_entry->common;
719 head = &tt_global_entry->orig_list;
721 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
722 flags = tt_common_entry->flags;
723 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
724 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
725 tt_global_entry->common.addr, orig_entry->ttvn,
726 orig_entry->orig_node->orig, last_ttvn,
727 (flags & TT_CLIENT_ROAM ? 'R' : '.'),
728 (flags & TT_CLIENT_WIFI ? 'W' : '.'));
732 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
734 struct net_device *net_dev = (struct net_device *)seq->private;
735 struct bat_priv *bat_priv = netdev_priv(net_dev);
736 struct hashtable_t *hash = bat_priv->tt_global_hash;
737 struct tt_common_entry *tt_common_entry;
738 struct tt_global_entry *tt_global_entry;
739 struct hard_iface *primary_if;
740 struct hlist_node *node;
741 struct hlist_head *head;
745 primary_if = batadv_primary_if_get_selected(bat_priv);
747 ret = seq_printf(seq,
748 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
753 if (primary_if->if_status != IF_ACTIVE) {
754 ret = seq_printf(seq,
755 "BATMAN mesh %s disabled - primary interface not active\n",
761 "Globally announced TT entries received via the mesh %s\n",
763 seq_printf(seq, " %-13s %s %-15s %s %s\n",
764 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
766 for (i = 0; i < hash->size; i++) {
767 head = &hash->table[i];
770 hlist_for_each_entry_rcu(tt_common_entry, node,
772 tt_global_entry = container_of(tt_common_entry,
773 struct tt_global_entry,
775 batadv_tt_global_print_entry(tt_global_entry, seq);
781 batadv_hardif_free_ref(primary_if);
785 /* deletes the orig list of a tt_global_entry */
787 batadv_tt_global_del_orig_list(struct tt_global_entry *tt_global_entry)
789 struct hlist_head *head;
790 struct hlist_node *node, *safe;
791 struct tt_orig_list_entry *orig_entry;
793 spin_lock_bh(&tt_global_entry->list_lock);
794 head = &tt_global_entry->orig_list;
795 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
797 batadv_tt_orig_list_entry_free_ref(orig_entry);
799 spin_unlock_bh(&tt_global_entry->list_lock);
804 batadv_tt_global_del_orig_entry(struct bat_priv *bat_priv,
805 struct tt_global_entry *tt_global_entry,
806 struct orig_node *orig_node,
809 struct hlist_head *head;
810 struct hlist_node *node, *safe;
811 struct tt_orig_list_entry *orig_entry;
813 spin_lock_bh(&tt_global_entry->list_lock);
814 head = &tt_global_entry->orig_list;
815 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
816 if (orig_entry->orig_node == orig_node) {
817 batadv_dbg(DBG_TT, bat_priv,
818 "Deleting %pM from global tt entry %pM: %s\n",
820 tt_global_entry->common.addr, message);
822 batadv_tt_orig_list_entry_free_ref(orig_entry);
825 spin_unlock_bh(&tt_global_entry->list_lock);
828 static void batadv_tt_global_del_struct(struct bat_priv *bat_priv,
829 struct tt_global_entry *tt_global_entry,
832 batadv_dbg(DBG_TT, bat_priv, "Deleting global tt entry %pM: %s\n",
833 tt_global_entry->common.addr, message);
835 batadv_hash_remove(bat_priv->tt_global_hash, batadv_compare_tt,
836 batadv_choose_orig, tt_global_entry->common.addr);
837 batadv_tt_global_entry_free_ref(tt_global_entry);
841 /* If the client is to be deleted, we check if it is the last origantor entry
842 * within tt_global entry. If yes, we set the TT_CLIENT_ROAM flag and the timer,
843 * otherwise we simply remove the originator scheduled for deletion.
846 batadv_tt_global_del_roaming(struct bat_priv *bat_priv,
847 struct tt_global_entry *tt_global_entry,
848 struct orig_node *orig_node, const char *message)
850 bool last_entry = true;
851 struct hlist_head *head;
852 struct hlist_node *node;
853 struct tt_orig_list_entry *orig_entry;
855 /* no local entry exists, case 1:
856 * Check if this is the last one or if other entries exist.
860 head = &tt_global_entry->orig_list;
861 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
862 if (orig_entry->orig_node != orig_node) {
870 /* its the last one, mark for roaming. */
871 tt_global_entry->common.flags |= TT_CLIENT_ROAM;
872 tt_global_entry->roam_at = jiffies;
874 /* there is another entry, we can simply delete this
875 * one and can still use the other one.
877 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
883 static void batadv_tt_global_del(struct bat_priv *bat_priv,
884 struct orig_node *orig_node,
885 const unsigned char *addr,
886 const char *message, bool roaming)
888 struct tt_global_entry *tt_global_entry = NULL;
889 struct tt_local_entry *local_entry = NULL;
891 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
892 if (!tt_global_entry)
896 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
899 if (hlist_empty(&tt_global_entry->orig_list))
900 batadv_tt_global_del_struct(bat_priv, tt_global_entry,
906 /* if we are deleting a global entry due to a roam
907 * event, there are two possibilities:
908 * 1) the client roamed from node A to node B => if there
909 * is only one originator left for this client, we mark
910 * it with TT_CLIENT_ROAM, we start a timer and we
911 * wait for node B to claim it. In case of timeout
912 * the entry is purged.
914 * If there are other originators left, we directly delete
916 * 2) the client roamed to us => we can directly delete
917 * the global entry, since it is useless now.
919 local_entry = batadv_tt_local_hash_find(bat_priv,
920 tt_global_entry->common.addr);
922 /* local entry exists, case 2: client roamed to us. */
923 batadv_tt_global_del_orig_list(tt_global_entry);
924 batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
926 /* no local entry exists, case 1: check for roaming */
927 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
933 batadv_tt_global_entry_free_ref(tt_global_entry);
935 batadv_tt_local_entry_free_ref(local_entry);
938 void batadv_tt_global_del_orig(struct bat_priv *bat_priv,
939 struct orig_node *orig_node, const char *message)
941 struct tt_global_entry *global_entry;
942 struct tt_common_entry *tt_common_entry;
944 struct hashtable_t *hash = bat_priv->tt_global_hash;
945 struct hlist_node *node, *safe;
946 struct hlist_head *head;
947 spinlock_t *list_lock; /* protects write access to the hash lists */
952 for (i = 0; i < hash->size; i++) {
953 head = &hash->table[i];
954 list_lock = &hash->list_locks[i];
956 spin_lock_bh(list_lock);
957 hlist_for_each_entry_safe(tt_common_entry, node, safe,
959 global_entry = container_of(tt_common_entry,
960 struct tt_global_entry,
963 batadv_tt_global_del_orig_entry(bat_priv, global_entry,
966 if (hlist_empty(&global_entry->orig_list)) {
967 batadv_dbg(DBG_TT, bat_priv,
968 "Deleting global tt entry %pM: %s\n",
969 global_entry->common.addr, message);
971 batadv_tt_global_entry_free_ref(global_entry);
974 spin_unlock_bh(list_lock);
976 orig_node->tt_initialised = false;
979 static void batadv_tt_global_roam_purge(struct bat_priv *bat_priv)
981 struct hashtable_t *hash = bat_priv->tt_global_hash;
982 struct tt_common_entry *tt_common_entry;
983 struct tt_global_entry *tt_global_entry;
984 struct hlist_node *node, *node_tmp;
985 struct hlist_head *head;
986 spinlock_t *list_lock; /* protects write access to the hash lists */
989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
991 list_lock = &hash->list_locks[i];
993 spin_lock_bh(list_lock);
994 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
996 tt_global_entry = container_of(tt_common_entry,
997 struct tt_global_entry,
999 if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
1001 if (!batadv_has_timed_out(tt_global_entry->roam_at,
1002 TT_CLIENT_ROAM_TIMEOUT))
1005 batadv_dbg(DBG_TT, bat_priv,
1006 "Deleting global tt entry (%pM): Roaming timeout\n",
1007 tt_global_entry->common.addr);
1009 hlist_del_rcu(node);
1010 batadv_tt_global_entry_free_ref(tt_global_entry);
1012 spin_unlock_bh(list_lock);
1017 static void batadv_tt_global_table_free(struct bat_priv *bat_priv)
1019 struct hashtable_t *hash;
1020 spinlock_t *list_lock; /* protects write access to the hash lists */
1021 struct tt_common_entry *tt_common_entry;
1022 struct tt_global_entry *tt_global_entry;
1023 struct hlist_node *node, *node_tmp;
1024 struct hlist_head *head;
1027 if (!bat_priv->tt_global_hash)
1030 hash = bat_priv->tt_global_hash;
1032 for (i = 0; i < hash->size; i++) {
1033 head = &hash->table[i];
1034 list_lock = &hash->list_locks[i];
1036 spin_lock_bh(list_lock);
1037 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1039 hlist_del_rcu(node);
1040 tt_global_entry = container_of(tt_common_entry,
1041 struct tt_global_entry,
1043 batadv_tt_global_entry_free_ref(tt_global_entry);
1045 spin_unlock_bh(list_lock);
1048 batadv_hash_destroy(hash);
1050 bat_priv->tt_global_hash = NULL;
1053 static bool _batadv_is_ap_isolated(struct tt_local_entry *tt_local_entry,
1054 struct tt_global_entry *tt_global_entry)
1058 if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
1059 tt_global_entry->common.flags & TT_CLIENT_WIFI)
1065 struct orig_node *batadv_transtable_search(struct bat_priv *bat_priv,
1067 const uint8_t *addr)
1069 struct tt_local_entry *tt_local_entry = NULL;
1070 struct tt_global_entry *tt_global_entry = NULL;
1071 struct orig_node *orig_node = NULL;
1072 struct neigh_node *router = NULL;
1073 struct hlist_head *head;
1074 struct hlist_node *node;
1075 struct tt_orig_list_entry *orig_entry;
1078 if (src && atomic_read(&bat_priv->ap_isolation)) {
1079 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1080 if (!tt_local_entry)
1084 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1085 if (!tt_global_entry)
1088 /* check whether the clients should not communicate due to AP
1091 if (tt_local_entry &&
1092 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1098 head = &tt_global_entry->orig_list;
1099 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1100 router = batadv_orig_node_get_router(orig_entry->orig_node);
1104 if (router->tq_avg > best_tq) {
1105 orig_node = orig_entry->orig_node;
1106 best_tq = router->tq_avg;
1108 batadv_neigh_node_free_ref(router);
1110 /* found anything? */
1111 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1115 if (tt_global_entry)
1116 batadv_tt_global_entry_free_ref(tt_global_entry);
1118 batadv_tt_local_entry_free_ref(tt_local_entry);
1123 /* Calculates the checksum of the local table of a given orig_node */
1124 static uint16_t batadv_tt_global_crc(struct bat_priv *bat_priv,
1125 struct orig_node *orig_node)
1127 uint16_t total = 0, total_one;
1128 struct hashtable_t *hash = bat_priv->tt_global_hash;
1129 struct tt_common_entry *tt_common_entry;
1130 struct tt_global_entry *tt_global_entry;
1131 struct hlist_node *node;
1132 struct hlist_head *head;
1136 for (i = 0; i < hash->size; i++) {
1137 head = &hash->table[i];
1140 hlist_for_each_entry_rcu(tt_common_entry, node,
1142 tt_global_entry = container_of(tt_common_entry,
1143 struct tt_global_entry,
1145 /* Roaming clients are in the global table for
1146 * consistency only. They don't have to be
1147 * taken into account while computing the
1150 if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
1153 /* find out if this global entry is announced by this
1156 if (!batadv_tt_global_entry_has_orig(tt_global_entry,
1161 for (j = 0; j < ETH_ALEN; j++)
1162 total_one = crc16_byte(total_one,
1163 tt_global_entry->common.addr[j]);
1172 /* Calculates the checksum of the local table */
1173 static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
1175 uint16_t total = 0, total_one;
1176 struct hashtable_t *hash = bat_priv->tt_local_hash;
1177 struct tt_common_entry *tt_common_entry;
1178 struct hlist_node *node;
1179 struct hlist_head *head;
1183 for (i = 0; i < hash->size; i++) {
1184 head = &hash->table[i];
1187 hlist_for_each_entry_rcu(tt_common_entry, node,
1189 /* not yet committed clients have not to be taken into
1190 * account while computing the CRC
1192 if (tt_common_entry->flags & TT_CLIENT_NEW)
1195 for (j = 0; j < ETH_ALEN; j++)
1196 total_one = crc16_byte(total_one,
1197 tt_common_entry->addr[j]);
1206 static void batadv_tt_req_list_free(struct bat_priv *bat_priv)
1208 struct tt_req_node *node, *safe;
1210 spin_lock_bh(&bat_priv->tt_req_list_lock);
1212 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1213 list_del(&node->list);
1217 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1220 static void batadv_tt_save_orig_buffer(struct bat_priv *bat_priv,
1221 struct orig_node *orig_node,
1222 const unsigned char *tt_buff,
1223 uint8_t tt_num_changes)
1225 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1227 /* Replace the old buffer only if I received something in the
1228 * last OGM (the OGM could carry no changes)
1230 spin_lock_bh(&orig_node->tt_buff_lock);
1231 if (tt_buff_len > 0) {
1232 kfree(orig_node->tt_buff);
1233 orig_node->tt_buff_len = 0;
1234 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1235 if (orig_node->tt_buff) {
1236 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1237 orig_node->tt_buff_len = tt_buff_len;
1240 spin_unlock_bh(&orig_node->tt_buff_lock);
1243 static void batadv_tt_req_purge(struct bat_priv *bat_priv)
1245 struct tt_req_node *node, *safe;
1247 spin_lock_bh(&bat_priv->tt_req_list_lock);
1248 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1249 if (batadv_has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
1250 list_del(&node->list);
1254 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1257 /* returns the pointer to the new tt_req_node struct if no request
1258 * has already been issued for this orig_node, NULL otherwise
1260 static struct tt_req_node *batadv_new_tt_req_node(struct bat_priv *bat_priv,
1261 struct orig_node *orig_node)
1263 struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1265 spin_lock_bh(&bat_priv->tt_req_list_lock);
1266 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
1267 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1268 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1269 TT_REQUEST_TIMEOUT))
1273 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1277 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1278 tt_req_node->issued_at = jiffies;
1280 list_add(&tt_req_node->list, &bat_priv->tt_req_list);
1282 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1286 /* data_ptr is useless here, but has to be kept to respect the prototype */
1287 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1288 const void *data_ptr)
1290 const struct tt_common_entry *tt_common_entry = entry_ptr;
1292 if (tt_common_entry->flags & TT_CLIENT_NEW)
1297 static int batadv_tt_global_valid(const void *entry_ptr,
1298 const void *data_ptr)
1300 const struct tt_common_entry *tt_common_entry = entry_ptr;
1301 const struct tt_global_entry *tt_global_entry;
1302 const struct orig_node *orig_node = data_ptr;
1304 if (tt_common_entry->flags & TT_CLIENT_ROAM)
1307 tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
1310 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1313 static struct sk_buff *
1314 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1315 struct hashtable_t *hash,
1316 struct hard_iface *primary_if,
1317 int (*valid_cb)(const void *, const void *),
1320 struct tt_common_entry *tt_common_entry;
1321 struct tt_query_packet *tt_response;
1322 struct tt_change *tt_change;
1323 struct hlist_node *node;
1324 struct hlist_head *head;
1325 struct sk_buff *skb = NULL;
1326 uint16_t tt_tot, tt_count;
1327 ssize_t tt_query_size = sizeof(struct tt_query_packet);
1330 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1331 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1332 tt_len -= tt_len % sizeof(struct tt_change);
1334 tt_tot = tt_len / sizeof(struct tt_change);
1336 skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
1340 skb_reserve(skb, ETH_HLEN);
1341 tt_response = (struct tt_query_packet *)skb_put(skb,
1342 tt_query_size + tt_len);
1343 tt_response->ttvn = ttvn;
1345 tt_change = (struct tt_change *)(skb->data + tt_query_size);
1349 for (i = 0; i < hash->size; i++) {
1350 head = &hash->table[i];
1352 hlist_for_each_entry_rcu(tt_common_entry, node,
1354 if (tt_count == tt_tot)
1357 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1360 memcpy(tt_change->addr, tt_common_entry->addr,
1362 tt_change->flags = NO_FLAGS;
1370 /* store in the message the number of entries we have successfully
1373 tt_response->tt_data = htons(tt_count);
1379 static int batadv_send_tt_request(struct bat_priv *bat_priv,
1380 struct orig_node *dst_orig_node,
1381 uint8_t ttvn, uint16_t tt_crc,
1384 struct sk_buff *skb = NULL;
1385 struct tt_query_packet *tt_request;
1386 struct neigh_node *neigh_node = NULL;
1387 struct hard_iface *primary_if;
1388 struct tt_req_node *tt_req_node = NULL;
1391 primary_if = batadv_primary_if_get_selected(bat_priv);
1395 /* The new tt_req will be issued only if I'm not waiting for a
1396 * reply from the same orig_node yet
1398 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1402 skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1406 skb_reserve(skb, ETH_HLEN);
1408 tt_request = (struct tt_query_packet *)skb_put(skb,
1409 sizeof(struct tt_query_packet));
1411 tt_request->header.packet_type = BAT_TT_QUERY;
1412 tt_request->header.version = COMPAT_VERSION;
1413 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1414 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1415 tt_request->header.ttl = TTL;
1416 tt_request->ttvn = ttvn;
1417 tt_request->tt_data = htons(tt_crc);
1418 tt_request->flags = TT_REQUEST;
1421 tt_request->flags |= TT_FULL_TABLE;
1423 neigh_node = batadv_orig_node_get_router(dst_orig_node);
1427 batadv_dbg(DBG_TT, bat_priv,
1428 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1429 dst_orig_node->orig, neigh_node->addr,
1430 (full_table ? 'F' : '.'));
1432 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
1434 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1439 batadv_neigh_node_free_ref(neigh_node);
1441 batadv_hardif_free_ref(primary_if);
1444 if (ret && tt_req_node) {
1445 spin_lock_bh(&bat_priv->tt_req_list_lock);
1446 list_del(&tt_req_node->list);
1447 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1453 static bool batadv_send_other_tt_response(struct bat_priv *bat_priv,
1454 struct tt_query_packet *tt_request)
1456 struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1457 struct neigh_node *neigh_node = NULL;
1458 struct hard_iface *primary_if = NULL;
1459 uint8_t orig_ttvn, req_ttvn, ttvn;
1461 unsigned char *tt_buff;
1463 uint16_t tt_len, tt_tot;
1464 struct sk_buff *skb = NULL;
1465 struct tt_query_packet *tt_response;
1467 batadv_dbg(DBG_TT, bat_priv,
1468 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1469 tt_request->src, tt_request->ttvn, tt_request->dst,
1470 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1472 /* Let's get the orig node of the REAL destination */
1473 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1474 if (!req_dst_orig_node)
1477 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1478 if (!res_dst_orig_node)
1481 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
1485 primary_if = batadv_primary_if_get_selected(bat_priv);
1489 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1490 req_ttvn = tt_request->ttvn;
1492 /* I don't have the requested data */
1493 if (orig_ttvn != req_ttvn ||
1494 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1497 /* If the full table has been explicitly requested */
1498 if (tt_request->flags & TT_FULL_TABLE ||
1499 !req_dst_orig_node->tt_buff)
1504 /* In this version, fragmentation is not implemented, then
1505 * I'll send only one packet with as much TT entries as I can
1508 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1509 tt_len = req_dst_orig_node->tt_buff_len;
1510 tt_tot = tt_len / sizeof(struct tt_change);
1512 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1517 skb_reserve(skb, ETH_HLEN);
1518 tt_response = (struct tt_query_packet *)skb_put(skb,
1519 sizeof(struct tt_query_packet) + tt_len);
1520 tt_response->ttvn = req_ttvn;
1521 tt_response->tt_data = htons(tt_tot);
1523 tt_buff = skb->data + sizeof(struct tt_query_packet);
1524 /* Copy the last orig_node's OGM buffer */
1525 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1526 req_dst_orig_node->tt_buff_len);
1528 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1530 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1531 sizeof(struct tt_change);
1532 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1534 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1535 bat_priv->tt_global_hash,
1537 batadv_tt_global_valid,
1542 tt_response = (struct tt_query_packet *)skb->data;
1545 tt_response->header.packet_type = BAT_TT_QUERY;
1546 tt_response->header.version = COMPAT_VERSION;
1547 tt_response->header.ttl = TTL;
1548 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1549 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1550 tt_response->flags = TT_RESPONSE;
1553 tt_response->flags |= TT_FULL_TABLE;
1555 batadv_dbg(DBG_TT, bat_priv,
1556 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1557 res_dst_orig_node->orig, neigh_node->addr,
1558 req_dst_orig_node->orig, req_ttvn);
1560 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1562 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1567 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1570 if (res_dst_orig_node)
1571 batadv_orig_node_free_ref(res_dst_orig_node);
1572 if (req_dst_orig_node)
1573 batadv_orig_node_free_ref(req_dst_orig_node);
1575 batadv_neigh_node_free_ref(neigh_node);
1577 batadv_hardif_free_ref(primary_if);
1583 static bool batadv_send_my_tt_response(struct bat_priv *bat_priv,
1584 struct tt_query_packet *tt_request)
1586 struct orig_node *orig_node = NULL;
1587 struct neigh_node *neigh_node = NULL;
1588 struct hard_iface *primary_if = NULL;
1589 uint8_t my_ttvn, req_ttvn, ttvn;
1591 unsigned char *tt_buff;
1593 uint16_t tt_len, tt_tot;
1594 struct sk_buff *skb = NULL;
1595 struct tt_query_packet *tt_response;
1597 batadv_dbg(DBG_TT, bat_priv,
1598 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1599 tt_request->src, tt_request->ttvn,
1600 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1603 my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1604 req_ttvn = tt_request->ttvn;
1606 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1610 neigh_node = batadv_orig_node_get_router(orig_node);
1614 primary_if = batadv_primary_if_get_selected(bat_priv);
1618 /* If the full table has been explicitly requested or the gap
1619 * is too big send the whole local translation table
1621 if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1627 /* In this version, fragmentation is not implemented, then
1628 * I'll send only one packet with as much TT entries as I can
1631 spin_lock_bh(&bat_priv->tt_buff_lock);
1632 tt_len = bat_priv->tt_buff_len;
1633 tt_tot = tt_len / sizeof(struct tt_change);
1635 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1640 skb_reserve(skb, ETH_HLEN);
1641 tt_response = (struct tt_query_packet *)skb_put(skb,
1642 sizeof(struct tt_query_packet) + tt_len);
1643 tt_response->ttvn = req_ttvn;
1644 tt_response->tt_data = htons(tt_tot);
1646 tt_buff = skb->data + sizeof(struct tt_query_packet);
1647 memcpy(tt_buff, bat_priv->tt_buff,
1648 bat_priv->tt_buff_len);
1649 spin_unlock_bh(&bat_priv->tt_buff_lock);
1651 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1652 sizeof(struct tt_change);
1653 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1655 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1656 bat_priv->tt_local_hash,
1658 batadv_tt_local_valid_entry,
1663 tt_response = (struct tt_query_packet *)skb->data;
1666 tt_response->header.packet_type = BAT_TT_QUERY;
1667 tt_response->header.version = COMPAT_VERSION;
1668 tt_response->header.ttl = TTL;
1669 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1670 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1671 tt_response->flags = TT_RESPONSE;
1674 tt_response->flags |= TT_FULL_TABLE;
1676 batadv_dbg(DBG_TT, bat_priv,
1677 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1678 orig_node->orig, neigh_node->addr,
1679 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1681 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
1683 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1688 spin_unlock_bh(&bat_priv->tt_buff_lock);
1691 batadv_orig_node_free_ref(orig_node);
1693 batadv_neigh_node_free_ref(neigh_node);
1695 batadv_hardif_free_ref(primary_if);
1698 /* This packet was for me, so it doesn't need to be re-routed */
1702 bool batadv_send_tt_response(struct bat_priv *bat_priv,
1703 struct tt_query_packet *tt_request)
1705 if (batadv_is_my_mac(tt_request->dst)) {
1706 /* don't answer backbone gws! */
1707 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1710 return batadv_send_my_tt_response(bat_priv, tt_request);
1712 return batadv_send_other_tt_response(bat_priv, tt_request);
1716 static void _batadv_tt_update_changes(struct bat_priv *bat_priv,
1717 struct orig_node *orig_node,
1718 struct tt_change *tt_change,
1719 uint16_t tt_num_changes, uint8_t ttvn)
1725 for (i = 0; i < tt_num_changes; i++) {
1726 if ((tt_change + i)->flags & TT_CLIENT_DEL) {
1727 roams = (tt_change + i)->flags & TT_CLIENT_ROAM;
1728 batadv_tt_global_del(bat_priv, orig_node,
1729 (tt_change + i)->addr,
1730 "tt removed by changes",
1733 is_wifi = (tt_change + i)->flags & TT_CLIENT_WIFI;
1734 if (!batadv_tt_global_add(bat_priv, orig_node,
1735 (tt_change + i)->addr, ttvn,
1737 /* In case of problem while storing a
1738 * global_entry, we stop the updating
1739 * procedure without committing the
1740 * ttvn change. This will avoid to send
1741 * corrupted data on tt_request
1746 orig_node->tt_initialised = true;
1749 static void batadv_tt_fill_gtable(struct bat_priv *bat_priv,
1750 struct tt_query_packet *tt_response)
1752 struct orig_node *orig_node = NULL;
1754 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1758 /* Purge the old table first.. */
1759 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
1761 _batadv_tt_update_changes(bat_priv, orig_node,
1762 (struct tt_change *)(tt_response + 1),
1763 ntohs(tt_response->tt_data),
1766 spin_lock_bh(&orig_node->tt_buff_lock);
1767 kfree(orig_node->tt_buff);
1768 orig_node->tt_buff_len = 0;
1769 orig_node->tt_buff = NULL;
1770 spin_unlock_bh(&orig_node->tt_buff_lock);
1772 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1776 batadv_orig_node_free_ref(orig_node);
1779 static void batadv_tt_update_changes(struct bat_priv *bat_priv,
1780 struct orig_node *orig_node,
1781 uint16_t tt_num_changes, uint8_t ttvn,
1782 struct tt_change *tt_change)
1784 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
1785 tt_num_changes, ttvn);
1787 batadv_tt_save_orig_buffer(bat_priv, orig_node,
1788 (unsigned char *)tt_change, tt_num_changes);
1789 atomic_set(&orig_node->last_ttvn, ttvn);
1792 bool batadv_is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1794 struct tt_local_entry *tt_local_entry = NULL;
1797 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
1798 if (!tt_local_entry)
1800 /* Check if the client has been logically deleted (but is kept for
1801 * consistency purpose)
1803 if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
1808 batadv_tt_local_entry_free_ref(tt_local_entry);
1812 void batadv_handle_tt_response(struct bat_priv *bat_priv,
1813 struct tt_query_packet *tt_response)
1815 struct tt_req_node *node, *safe;
1816 struct orig_node *orig_node = NULL;
1818 batadv_dbg(DBG_TT, bat_priv,
1819 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1820 tt_response->src, tt_response->ttvn,
1821 ntohs(tt_response->tt_data),
1822 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1824 /* we should have never asked a backbone gw */
1825 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1828 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1832 if (tt_response->flags & TT_FULL_TABLE)
1833 batadv_tt_fill_gtable(bat_priv, tt_response);
1835 batadv_tt_update_changes(bat_priv, orig_node,
1836 ntohs(tt_response->tt_data),
1838 (struct tt_change *)(tt_response + 1));
1840 /* Delete the tt_req_node from pending tt_requests list */
1841 spin_lock_bh(&bat_priv->tt_req_list_lock);
1842 list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1843 if (!batadv_compare_eth(node->addr, tt_response->src))
1845 list_del(&node->list);
1848 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1850 /* Recalculate the CRC for this orig_node and store it */
1851 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
1852 /* Roaming phase is over: tables are in sync again. I can
1855 orig_node->tt_poss_change = false;
1858 batadv_orig_node_free_ref(orig_node);
1861 int batadv_tt_init(struct bat_priv *bat_priv)
1865 ret = batadv_tt_local_init(bat_priv);
1869 ret = batadv_tt_global_init(bat_priv);
1873 batadv_tt_start_timer(bat_priv);
1878 static void batadv_tt_roam_list_free(struct bat_priv *bat_priv)
1880 struct tt_roam_node *node, *safe;
1882 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1884 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1885 list_del(&node->list);
1889 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1892 static void batadv_tt_roam_purge(struct bat_priv *bat_priv)
1894 struct tt_roam_node *node, *safe;
1896 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1897 list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1898 if (!batadv_has_timed_out(node->first_time, ROAMING_MAX_TIME))
1901 list_del(&node->list);
1904 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1907 /* This function checks whether the client already reached the
1908 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1911 * returns true if the ROAMING_ADV can be sent, false otherwise
1913 static bool batadv_tt_check_roam_count(struct bat_priv *bat_priv,
1916 struct tt_roam_node *tt_roam_node;
1919 spin_lock_bh(&bat_priv->tt_roam_list_lock);
1920 /* The new tt_req will be issued only if I'm not waiting for a
1921 * reply from the same orig_node yet
1923 list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1924 if (!batadv_compare_eth(tt_roam_node->addr, client))
1927 if (batadv_has_timed_out(tt_roam_node->first_time,
1931 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1932 /* Sorry, you roamed too many times! */
1939 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1943 tt_roam_node->first_time = jiffies;
1944 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1945 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1947 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1952 spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1956 static void batadv_send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1957 struct orig_node *orig_node)
1959 struct neigh_node *neigh_node = NULL;
1960 struct sk_buff *skb = NULL;
1961 struct roam_adv_packet *roam_adv_packet;
1963 struct hard_iface *primary_if;
1965 /* before going on we have to check whether the client has
1966 * already roamed to us too many times
1968 if (!batadv_tt_check_roam_count(bat_priv, client))
1971 skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1975 skb_reserve(skb, ETH_HLEN);
1977 roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1978 sizeof(struct roam_adv_packet));
1980 roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
1981 roam_adv_packet->header.version = COMPAT_VERSION;
1982 roam_adv_packet->header.ttl = TTL;
1983 primary_if = batadv_primary_if_get_selected(bat_priv);
1986 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1987 batadv_hardif_free_ref(primary_if);
1988 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1989 memcpy(roam_adv_packet->client, client, ETH_ALEN);
1991 neigh_node = batadv_orig_node_get_router(orig_node);
1995 batadv_dbg(DBG_TT, bat_priv,
1996 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1997 orig_node->orig, client, neigh_node->addr);
1999 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
2001 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
2006 batadv_neigh_node_free_ref(neigh_node);
2012 static void batadv_tt_purge(struct work_struct *work)
2014 struct delayed_work *delayed_work =
2015 container_of(work, struct delayed_work, work);
2016 struct bat_priv *bat_priv =
2017 container_of(delayed_work, struct bat_priv, tt_work);
2019 batadv_tt_local_purge(bat_priv);
2020 batadv_tt_global_roam_purge(bat_priv);
2021 batadv_tt_req_purge(bat_priv);
2022 batadv_tt_roam_purge(bat_priv);
2024 batadv_tt_start_timer(bat_priv);
2027 void batadv_tt_free(struct bat_priv *bat_priv)
2029 cancel_delayed_work_sync(&bat_priv->tt_work);
2031 batadv_tt_local_table_free(bat_priv);
2032 batadv_tt_global_table_free(bat_priv);
2033 batadv_tt_req_list_free(bat_priv);
2034 batadv_tt_changes_list_free(bat_priv);
2035 batadv_tt_roam_list_free(bat_priv);
2037 kfree(bat_priv->tt_buff);
2040 /* This function will enable or disable the specified flags for all the entries
2041 * in the given hash table and returns the number of modified entries
2043 static uint16_t batadv_tt_set_flags(struct hashtable_t *hash, uint16_t flags,
2047 uint16_t changed_num = 0;
2048 struct hlist_head *head;
2049 struct hlist_node *node;
2050 struct tt_common_entry *tt_common_entry;
2055 for (i = 0; i < hash->size; i++) {
2056 head = &hash->table[i];
2059 hlist_for_each_entry_rcu(tt_common_entry, node,
2062 if ((tt_common_entry->flags & flags) == flags)
2064 tt_common_entry->flags |= flags;
2066 if (!(tt_common_entry->flags & flags))
2068 tt_common_entry->flags &= ~flags;
2078 /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
2079 static void batadv_tt_local_purge_pending_clients(struct bat_priv *bat_priv)
2081 struct hashtable_t *hash = bat_priv->tt_local_hash;
2082 struct tt_common_entry *tt_common_entry;
2083 struct tt_local_entry *tt_local_entry;
2084 struct hlist_node *node, *node_tmp;
2085 struct hlist_head *head;
2086 spinlock_t *list_lock; /* protects write access to the hash lists */
2092 for (i = 0; i < hash->size; i++) {
2093 head = &hash->table[i];
2094 list_lock = &hash->list_locks[i];
2096 spin_lock_bh(list_lock);
2097 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
2099 if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
2102 batadv_dbg(DBG_TT, bat_priv,
2103 "Deleting local tt entry (%pM): pending\n",
2104 tt_common_entry->addr);
2106 atomic_dec(&bat_priv->num_local_tt);
2107 hlist_del_rcu(node);
2108 tt_local_entry = container_of(tt_common_entry,
2109 struct tt_local_entry,
2111 batadv_tt_local_entry_free_ref(tt_local_entry);
2113 spin_unlock_bh(list_lock);
2118 static int batadv_tt_commit_changes(struct bat_priv *bat_priv,
2119 unsigned char **packet_buff,
2120 int *packet_buff_len, int packet_min_len)
2122 uint16_t changed_num = 0;
2124 if (atomic_read(&bat_priv->tt_local_changes) < 1)
2127 changed_num = batadv_tt_set_flags(bat_priv->tt_local_hash,
2128 TT_CLIENT_NEW, false);
2130 /* all reset entries have to be counted as local entries */
2131 atomic_add(changed_num, &bat_priv->num_local_tt);
2132 batadv_tt_local_purge_pending_clients(bat_priv);
2133 bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
2135 /* Increment the TTVN only once per OGM interval */
2136 atomic_inc(&bat_priv->ttvn);
2137 batadv_dbg(DBG_TT, bat_priv,
2138 "Local changes committed, updating to ttvn %u\n",
2139 (uint8_t)atomic_read(&bat_priv->ttvn));
2140 bat_priv->tt_poss_change = false;
2142 /* reset the sending counter */
2143 atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
2145 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2146 packet_buff_len, packet_min_len);
2149 /* when calling this function (hard_iface == primary_if) has to be true */
2150 int batadv_tt_append_diff(struct bat_priv *bat_priv,
2151 unsigned char **packet_buff, int *packet_buff_len,
2156 /* if at least one change happened */
2157 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2161 /* if the changes have been sent often enough */
2162 if ((tt_num_changes < 0) &&
2163 (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
2164 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2165 packet_min_len, packet_min_len);
2169 return tt_num_changes;
2172 bool batadv_is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src,
2175 struct tt_local_entry *tt_local_entry = NULL;
2176 struct tt_global_entry *tt_global_entry = NULL;
2179 if (!atomic_read(&bat_priv->ap_isolation))
2182 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2183 if (!tt_local_entry)
2186 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2187 if (!tt_global_entry)
2190 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2196 if (tt_global_entry)
2197 batadv_tt_global_entry_free_ref(tt_global_entry);
2199 batadv_tt_local_entry_free_ref(tt_local_entry);
2203 void batadv_tt_update_orig(struct bat_priv *bat_priv,
2204 struct orig_node *orig_node,
2205 const unsigned char *tt_buff, uint8_t tt_num_changes,
2206 uint8_t ttvn, uint16_t tt_crc)
2208 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2209 bool full_table = true;
2211 /* don't care about a backbone gateways updates. */
2212 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2215 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2216 * increased by one -> we can apply the attached changes
2218 if ((!orig_node->tt_initialised && ttvn == 1) ||
2219 ttvn - orig_ttvn == 1) {
2220 /* the OGM could not contain the changes due to their size or
2221 * because they have already been sent TT_OGM_APPEND_MAX times.
2222 * In this case send a tt request
2224 if (!tt_num_changes) {
2229 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2230 ttvn, (struct tt_change *)tt_buff);
2232 /* Even if we received the precomputed crc with the OGM, we
2233 * prefer to recompute it to spot any possible inconsistency
2234 * in the global table
2236 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2238 /* The ttvn alone is not enough to guarantee consistency
2239 * because a single value could represent different states
2240 * (due to the wrap around). Thus a node has to check whether
2241 * the resulting table (after applying the changes) is still
2242 * consistent or not. E.g. a node could disconnect while its
2243 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2244 * checking the CRC value is mandatory to detect the
2247 if (orig_node->tt_crc != tt_crc)
2250 /* Roaming phase is over: tables are in sync again. I can
2253 orig_node->tt_poss_change = false;
2255 /* if we missed more than one change or our tables are not
2256 * in sync anymore -> request fresh tt data
2258 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2259 orig_node->tt_crc != tt_crc) {
2261 batadv_dbg(DBG_TT, bat_priv,
2262 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2263 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2264 orig_node->tt_crc, tt_num_changes);
2265 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2266 tt_crc, full_table);
2272 /* returns true whether we know that the client has moved from its old
2273 * originator to another one. This entry is kept is still kept for consistency
2276 bool batadv_tt_global_client_is_roaming(struct bat_priv *bat_priv,
2279 struct tt_global_entry *tt_global_entry;
2282 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2283 if (!tt_global_entry)
2286 ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
2287 batadv_tt_global_entry_free_ref(tt_global_entry);