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 batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
34 static void batadv_tt_purge(struct work_struct *work);
36 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
37 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 const unsigned char *addr,
40 const char *message, bool roaming);
42 /* returns 1 if they are the same mac addr */
43 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
45 const void *data1 = container_of(node, struct batadv_tt_common_entry,
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
51 static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
53 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
55 msecs_to_jiffies(5000));
58 static struct batadv_tt_common_entry *
59 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
61 struct hlist_head *head;
62 struct hlist_node *node;
63 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
70 index = batadv_choose_orig(data, hash->size);
71 head = &hash->table[index];
74 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
75 if (!batadv_compare_eth(tt_common_entry, data))
78 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
81 tt_common_entry_tmp = tt_common_entry;
86 return tt_common_entry_tmp;
89 static struct batadv_tt_local_entry *
90 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
92 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
95 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
97 tt_local_entry = container_of(tt_common_entry,
98 struct batadv_tt_local_entry,
100 return tt_local_entry;
103 static struct batadv_tt_global_entry *
104 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
111 tt_global_entry = container_of(tt_common_entry,
112 struct batadv_tt_global_entry,
114 return tt_global_entry;
119 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
125 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
130 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
131 tt_global_entry = container_of(tt_common_entry,
132 struct batadv_tt_global_entry, common);
134 kfree(tt_global_entry);
138 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
141 batadv_tt_global_del_orig_list(tt_global_entry);
142 call_rcu(&tt_global_entry->common.rcu,
143 batadv_tt_global_entry_free_rcu);
147 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
149 struct batadv_tt_orig_list_entry *orig_entry;
151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
152 batadv_orig_node_free_ref(orig_entry->orig_node);
157 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
159 if (!atomic_dec_and_test(&orig_entry->refcount))
161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
166 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
167 const uint8_t *addr, uint8_t flags)
169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
178 tt_change_node->change.flags = flags;
179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
183 /* check for ADD+DEL or DEL+ADD events */
184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
187 if (!batadv_compare_eth(entry->change.addr, addr))
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
198 if (!del_op_requested && del_op_entry)
200 if (del_op_requested && !del_op_entry)
204 list_del(&entry->list);
206 kfree(tt_change_node);
207 event_removed = true;
211 /* track the change in the OGMinterval list */
212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
218 atomic_dec(&bat_priv->tt.local_changes);
220 atomic_inc(&bat_priv->tt.local_changes);
223 int batadv_tt_len(int changes_num)
225 return changes_num * sizeof(struct batadv_tt_change);
228 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
230 if (bat_priv->tt.local_hash)
233 bat_priv->tt.local_hash = batadv_hash_new(1024);
235 if (!bat_priv->tt.local_hash)
241 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
242 struct batadv_tt_global_entry *tt_global,
245 batadv_dbg(BATADV_DBG_TT, bat_priv,
246 "Deleting global tt entry %pM: %s\n",
247 tt_global->common.addr, message);
249 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
250 batadv_choose_orig, tt_global->common.addr);
251 batadv_tt_global_entry_free_ref(tt_global);
255 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
258 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
259 struct batadv_tt_local_entry *tt_local;
260 struct batadv_tt_global_entry *tt_global;
261 struct hlist_head *head;
262 struct hlist_node *node;
263 struct batadv_tt_orig_list_entry *orig_entry;
265 bool roamed_back = false;
267 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
268 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
271 tt_local->last_seen = jiffies;
272 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
273 batadv_dbg(BATADV_DBG_TT, bat_priv,
274 "Re-adding pending client %pM\n", addr);
275 /* whatever the reason why the PENDING flag was set,
276 * this is a client which was enqueued to be removed in
277 * this orig_interval. Since it popped up again, the
278 * flag can be reset like it was never enqueued
280 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
284 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
285 batadv_dbg(BATADV_DBG_TT, bat_priv,
286 "Roaming client %pM came back to its original location\n",
288 /* the ROAM flag is set because this client roamed away
289 * and the node got a roaming_advertisement message. Now
290 * that the client popped up again at its original
291 * location such flag can be unset
293 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
299 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
303 batadv_dbg(BATADV_DBG_TT, bat_priv,
304 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
305 (uint8_t)atomic_read(&bat_priv->tt.vn));
307 memcpy(tt_local->common.addr, addr, ETH_ALEN);
308 tt_local->common.flags = BATADV_NO_FLAGS;
309 if (batadv_is_wifi_iface(ifindex))
310 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
311 atomic_set(&tt_local->common.refcount, 2);
312 tt_local->last_seen = jiffies;
313 tt_local->common.added_at = tt_local->last_seen;
315 /* the batman interface mac address should never be purged */
316 if (batadv_compare_eth(addr, soft_iface->dev_addr))
317 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
319 /* The local entry has to be marked as NEW to avoid to send it in
320 * a full table response going out before the next ttvn increment
321 * (consistency check)
323 tt_local->common.flags |= BATADV_TT_CLIENT_NEW;
325 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
326 batadv_choose_orig, &tt_local->common,
327 &tt_local->common.hash_entry);
329 if (unlikely(hash_added != 0)) {
330 /* remove the reference for the hash */
331 batadv_tt_local_entry_free_ref(tt_local);
336 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
339 /* Check whether it is a roaming, but don't do anything if the roaming
340 * process has already been handled
342 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
343 /* These node are probably going to update their tt table */
344 head = &tt_global->orig_list;
346 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
347 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
348 orig_entry->orig_node);
352 batadv_tt_global_free(bat_priv, tt_global,
356 /* The global entry has to be marked as ROAMING and
357 * has to be kept for consistency purpose
359 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
360 tt_global->roam_at = jiffies;
366 batadv_tt_local_entry_free_ref(tt_local);
368 batadv_tt_global_entry_free_ref(tt_global);
371 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
372 int *packet_buff_len,
376 unsigned char *new_buff;
378 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
380 /* keep old buffer if kmalloc should fail */
382 memcpy(new_buff, *packet_buff, min_packet_len);
384 *packet_buff = new_buff;
385 *packet_buff_len = new_packet_len;
389 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
390 unsigned char **packet_buff,
391 int *packet_buff_len,
394 struct batadv_hard_iface *primary_if;
397 primary_if = batadv_primary_if_get_selected(bat_priv);
399 req_len = min_packet_len;
400 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
402 /* if we have too many changes for one packet don't send any
403 * and wait for the tt table request which will be fragmented
405 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
406 req_len = min_packet_len;
408 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
409 min_packet_len, req_len);
412 batadv_hardif_free_ref(primary_if);
415 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
416 unsigned char **packet_buff,
417 int *packet_buff_len,
420 struct batadv_tt_change_node *entry, *safe;
421 int count = 0, tot_changes = 0, new_len;
422 unsigned char *tt_buff;
424 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
425 packet_buff_len, min_packet_len);
427 new_len = *packet_buff_len - min_packet_len;
428 tt_buff = *packet_buff + min_packet_len;
431 tot_changes = new_len / batadv_tt_len(1);
433 spin_lock_bh(&bat_priv->tt.changes_list_lock);
434 atomic_set(&bat_priv->tt.local_changes, 0);
436 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
438 if (count < tot_changes) {
439 memcpy(tt_buff + batadv_tt_len(count),
440 &entry->change, sizeof(struct batadv_tt_change));
443 list_del(&entry->list);
446 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
448 /* Keep the buffer for possible tt_request */
449 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
450 kfree(bat_priv->tt.last_changeset);
451 bat_priv->tt.last_changeset_len = 0;
452 bat_priv->tt.last_changeset = NULL;
453 /* check whether this new OGM has no changes due to size problems */
455 /* if kmalloc() fails we will reply with the full table
456 * instead of providing the diff
458 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
459 if (bat_priv->tt.last_changeset) {
460 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
461 bat_priv->tt.last_changeset_len = new_len;
464 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
469 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
471 struct net_device *net_dev = (struct net_device *)seq->private;
472 struct batadv_priv *bat_priv = netdev_priv(net_dev);
473 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
474 struct batadv_tt_common_entry *tt_common_entry;
475 struct batadv_tt_local_entry *tt_local;
476 struct batadv_hard_iface *primary_if;
477 struct hlist_node *node;
478 struct hlist_head *head;
482 unsigned long last_seen_jiffies;
484 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
486 primary_if = batadv_seq_print_text_primary_if_get(seq);
491 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
492 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
493 bat_priv->tt.local_crc);
494 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
497 for (i = 0; i < hash->size; i++) {
498 head = &hash->table[i];
501 hlist_for_each_entry_rcu(tt_common_entry, node,
503 tt_local = container_of(tt_common_entry,
504 struct batadv_tt_local_entry,
506 last_seen_jiffies = jiffies - tt_local->last_seen;
507 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
508 last_seen_secs = last_seen_msecs / 1000;
509 last_seen_msecs = last_seen_msecs % 1000;
511 no_purge = tt_common_entry->flags & np_flag;
513 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
514 tt_common_entry->addr,
515 (tt_common_entry->flags &
516 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
517 no_purge ? 'P' : '.',
518 (tt_common_entry->flags &
519 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
520 (tt_common_entry->flags &
521 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
522 (tt_common_entry->flags &
523 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
524 no_purge ? last_seen_secs : 0,
525 no_purge ? last_seen_msecs : 0);
531 batadv_hardif_free_ref(primary_if);
536 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
537 struct batadv_tt_local_entry *tt_local_entry,
538 uint16_t flags, const char *message)
540 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
541 tt_local_entry->common.flags | flags);
543 /* The local client has to be marked as "pending to be removed" but has
544 * to be kept in the table in order to send it in a full table
545 * response issued before the net ttvn increment (consistency check)
547 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
549 batadv_dbg(BATADV_DBG_TT, bat_priv,
550 "Local tt entry (%pM) pending to be removed: %s\n",
551 tt_local_entry->common.addr, message);
555 * batadv_tt_local_remove - logically remove an entry from the local table
556 * @bat_priv: the bat priv with all the soft interface information
557 * @addr: the MAC address of the client to remove
558 * @message: message to append to the log on deletion
559 * @roaming: true if the deletion is due to a roaming event
561 * Returns the flags assigned to the local entry before being deleted
563 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
564 const uint8_t *addr, const char *message,
567 struct batadv_tt_local_entry *tt_local_entry;
568 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
570 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
574 curr_flags = tt_local_entry->common.flags;
576 flags = BATADV_TT_CLIENT_DEL;
577 /* if this global entry addition is due to a roaming, the node has to
578 * mark the local entry as "roamed" in order to correctly reroute
582 flags |= BATADV_TT_CLIENT_ROAM;
583 /* mark the local client as ROAMed */
584 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
587 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
588 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
592 /* if this client has been added right now, it is possible to
593 * immediately purge it
595 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
596 curr_flags | BATADV_TT_CLIENT_DEL);
597 hlist_del_rcu(&tt_local_entry->common.hash_entry);
598 batadv_tt_local_entry_free_ref(tt_local_entry);
602 batadv_tt_local_entry_free_ref(tt_local_entry);
607 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
608 struct hlist_head *head)
610 struct batadv_tt_local_entry *tt_local_entry;
611 struct batadv_tt_common_entry *tt_common_entry;
612 struct hlist_node *node, *node_tmp;
614 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
616 tt_local_entry = container_of(tt_common_entry,
617 struct batadv_tt_local_entry,
619 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
622 /* entry already marked for deletion */
623 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
626 if (!batadv_has_timed_out(tt_local_entry->last_seen,
627 BATADV_TT_LOCAL_TIMEOUT))
630 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
631 BATADV_TT_CLIENT_DEL, "timed out");
635 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
637 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
638 struct hlist_head *head;
639 spinlock_t *list_lock; /* protects write access to the hash lists */
642 for (i = 0; i < hash->size; i++) {
643 head = &hash->table[i];
644 list_lock = &hash->list_locks[i];
646 spin_lock_bh(list_lock);
647 batadv_tt_local_purge_list(bat_priv, head);
648 spin_unlock_bh(list_lock);
653 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
655 struct batadv_hashtable *hash;
656 spinlock_t *list_lock; /* protects write access to the hash lists */
657 struct batadv_tt_common_entry *tt_common_entry;
658 struct batadv_tt_local_entry *tt_local;
659 struct hlist_node *node, *node_tmp;
660 struct hlist_head *head;
663 if (!bat_priv->tt.local_hash)
666 hash = bat_priv->tt.local_hash;
668 for (i = 0; i < hash->size; i++) {
669 head = &hash->table[i];
670 list_lock = &hash->list_locks[i];
672 spin_lock_bh(list_lock);
673 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
676 tt_local = container_of(tt_common_entry,
677 struct batadv_tt_local_entry,
679 batadv_tt_local_entry_free_ref(tt_local);
681 spin_unlock_bh(list_lock);
684 batadv_hash_destroy(hash);
686 bat_priv->tt.local_hash = NULL;
689 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
691 if (bat_priv->tt.global_hash)
694 bat_priv->tt.global_hash = batadv_hash_new(1024);
696 if (!bat_priv->tt.global_hash)
702 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
704 struct batadv_tt_change_node *entry, *safe;
706 spin_lock_bh(&bat_priv->tt.changes_list_lock);
708 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
710 list_del(&entry->list);
714 atomic_set(&bat_priv->tt.local_changes, 0);
715 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
718 /* retrieves the orig_tt_list_entry belonging to orig_node from the
719 * batadv_tt_global_entry list
721 * returns it with an increased refcounter, NULL if not found
723 static struct batadv_tt_orig_list_entry *
724 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
725 const struct batadv_orig_node *orig_node)
727 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
728 const struct hlist_head *head;
729 struct hlist_node *node;
732 head = &entry->orig_list;
733 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
734 if (tmp_orig_entry->orig_node != orig_node)
736 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
739 orig_entry = tmp_orig_entry;
747 /* find out if an orig_node is already in the list of a tt_global_entry.
748 * returns true if found, false otherwise
751 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
752 const struct batadv_orig_node *orig_node)
754 struct batadv_tt_orig_list_entry *orig_entry;
757 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
760 batadv_tt_orig_list_entry_free_ref(orig_entry);
767 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
768 struct batadv_orig_node *orig_node, int ttvn)
770 struct batadv_tt_orig_list_entry *orig_entry;
772 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
774 /* refresh the ttvn: the current value could be a bogus one that
775 * was added during a "temporary client detection"
777 orig_entry->ttvn = ttvn;
781 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
785 INIT_HLIST_NODE(&orig_entry->list);
786 atomic_inc(&orig_node->refcount);
787 atomic_inc(&orig_node->tt_size);
788 orig_entry->orig_node = orig_node;
789 orig_entry->ttvn = ttvn;
790 atomic_set(&orig_entry->refcount, 2);
792 spin_lock_bh(&tt_global->list_lock);
793 hlist_add_head_rcu(&orig_entry->list,
794 &tt_global->orig_list);
795 spin_unlock_bh(&tt_global->list_lock);
798 batadv_tt_orig_list_entry_free_ref(orig_entry);
801 /* caller must hold orig_node refcount */
802 int batadv_tt_global_add(struct batadv_priv *bat_priv,
803 struct batadv_orig_node *orig_node,
804 const unsigned char *tt_addr, uint8_t flags,
807 struct batadv_tt_global_entry *tt_global_entry;
808 struct batadv_tt_local_entry *tt_local_entry;
811 struct batadv_tt_common_entry *common;
812 uint16_t local_flags;
814 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
815 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
817 /* if the node already has a local client for this entry, it has to wait
818 * for a roaming advertisement instead of manually messing up the global
821 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
822 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
825 if (!tt_global_entry) {
826 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
827 if (!tt_global_entry)
830 common = &tt_global_entry->common;
831 memcpy(common->addr, tt_addr, ETH_ALEN);
833 common->flags = flags;
834 tt_global_entry->roam_at = 0;
835 /* node must store current time in case of roaming. This is
836 * needed to purge this entry out on timeout (if nobody claims
839 if (flags & BATADV_TT_CLIENT_ROAM)
840 tt_global_entry->roam_at = jiffies;
841 atomic_set(&common->refcount, 2);
842 common->added_at = jiffies;
844 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
845 spin_lock_init(&tt_global_entry->list_lock);
847 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
849 batadv_choose_orig, common,
850 &common->hash_entry);
852 if (unlikely(hash_added != 0)) {
853 /* remove the reference for the hash */
854 batadv_tt_global_entry_free_ref(tt_global_entry);
858 common = &tt_global_entry->common;
859 /* If there is already a global entry, we can use this one for
861 * But if we are trying to add a temporary client then here are
862 * two options at this point:
863 * 1) the global client is not a temporary client: the global
864 * client has to be left as it is, temporary information
865 * should never override any already known client state
866 * 2) the global client is a temporary client: purge the
867 * originator list and add the new one orig_entry
869 if (flags & BATADV_TT_CLIENT_TEMP) {
870 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
872 if (batadv_tt_global_entry_has_orig(tt_global_entry,
875 batadv_tt_global_del_orig_list(tt_global_entry);
879 /* if the client was temporary added before receiving the first
880 * OGM announcing it, we have to clear the TEMP flag
882 common->flags &= ~BATADV_TT_CLIENT_TEMP;
884 /* the change can carry possible "attribute" flags like the
885 * TT_CLIENT_WIFI, therefore they have to be copied in the
888 tt_global_entry->common.flags |= flags;
890 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
891 * one originator left in the list and we previously received a
892 * delete + roaming change for this originator.
894 * We should first delete the old originator before adding the
897 if (common->flags & BATADV_TT_CLIENT_ROAM) {
898 batadv_tt_global_del_orig_list(tt_global_entry);
899 common->flags &= ~BATADV_TT_CLIENT_ROAM;
900 tt_global_entry->roam_at = 0;
904 /* add the new orig_entry (if needed) or update it */
905 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
907 batadv_dbg(BATADV_DBG_TT, bat_priv,
908 "Creating new global tt entry: %pM (via %pM)\n",
909 common->addr, orig_node->orig);
914 /* remove address from local hash if present */
915 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
916 "global tt received",
917 !!(flags & BATADV_TT_CLIENT_ROAM));
918 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
920 if (!(flags & BATADV_TT_CLIENT_ROAM))
921 /* this is a normal global add. Therefore the client is not in a
922 * roaming state anymore.
924 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
928 batadv_tt_global_entry_free_ref(tt_global_entry);
930 batadv_tt_local_entry_free_ref(tt_local_entry);
934 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
935 * @tt_global_entry: global translation table entry to be analyzed
937 * This functon assumes the caller holds rcu_read_lock().
938 * Returns best originator list entry or NULL on errors.
940 static struct batadv_tt_orig_list_entry *
941 batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
943 struct batadv_neigh_node *router = NULL;
944 struct hlist_head *head;
945 struct hlist_node *node;
946 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
949 head = &tt_global_entry->orig_list;
950 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
951 router = batadv_orig_node_get_router(orig_entry->orig_node);
955 if (router->tq_avg > best_tq) {
956 best_entry = orig_entry;
957 best_tq = router->tq_avg;
960 batadv_neigh_node_free_ref(router);
966 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
967 * for this global entry
968 * @tt_global_entry: global translation table entry to be printed
969 * @seq: debugfs table seq_file struct
971 * This functon assumes the caller holds rcu_read_lock().
974 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
975 struct seq_file *seq)
977 struct hlist_head *head;
978 struct hlist_node *node;
979 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
980 struct batadv_tt_common_entry *tt_common_entry;
984 tt_common_entry = &tt_global_entry->common;
985 flags = tt_common_entry->flags;
987 best_entry = batadv_transtable_best_orig(tt_global_entry);
989 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
991 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
992 '*', tt_global_entry->common.addr,
993 best_entry->ttvn, best_entry->orig_node->orig,
994 last_ttvn, best_entry->orig_node->tt_crc,
995 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
996 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
997 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1000 head = &tt_global_entry->orig_list;
1002 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1003 if (best_entry == orig_entry)
1006 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1007 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1008 '+', tt_global_entry->common.addr,
1009 orig_entry->ttvn, orig_entry->orig_node->orig,
1011 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1012 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1013 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1017 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1019 struct net_device *net_dev = (struct net_device *)seq->private;
1020 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1021 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1022 struct batadv_tt_common_entry *tt_common_entry;
1023 struct batadv_tt_global_entry *tt_global;
1024 struct batadv_hard_iface *primary_if;
1025 struct hlist_node *node;
1026 struct hlist_head *head;
1029 primary_if = batadv_seq_print_text_primary_if_get(seq);
1034 "Globally announced TT entries received via the mesh %s\n",
1036 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1037 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1040 for (i = 0; i < hash->size; i++) {
1041 head = &hash->table[i];
1044 hlist_for_each_entry_rcu(tt_common_entry, node,
1046 tt_global = container_of(tt_common_entry,
1047 struct batadv_tt_global_entry,
1049 batadv_tt_global_print_entry(tt_global, seq);
1055 batadv_hardif_free_ref(primary_if);
1059 /* deletes the orig list of a tt_global_entry */
1061 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1063 struct hlist_head *head;
1064 struct hlist_node *node, *safe;
1065 struct batadv_tt_orig_list_entry *orig_entry;
1067 spin_lock_bh(&tt_global_entry->list_lock);
1068 head = &tt_global_entry->orig_list;
1069 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1070 hlist_del_rcu(node);
1071 batadv_tt_orig_list_entry_free_ref(orig_entry);
1073 spin_unlock_bh(&tt_global_entry->list_lock);
1078 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1079 struct batadv_tt_global_entry *tt_global_entry,
1080 struct batadv_orig_node *orig_node,
1081 const char *message)
1083 struct hlist_head *head;
1084 struct hlist_node *node, *safe;
1085 struct batadv_tt_orig_list_entry *orig_entry;
1087 spin_lock_bh(&tt_global_entry->list_lock);
1088 head = &tt_global_entry->orig_list;
1089 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1090 if (orig_entry->orig_node == orig_node) {
1091 batadv_dbg(BATADV_DBG_TT, bat_priv,
1092 "Deleting %pM from global tt entry %pM: %s\n",
1094 tt_global_entry->common.addr, message);
1095 hlist_del_rcu(node);
1096 batadv_tt_orig_list_entry_free_ref(orig_entry);
1099 spin_unlock_bh(&tt_global_entry->list_lock);
1102 /* If the client is to be deleted, we check if it is the last origantor entry
1103 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1104 * timer, otherwise we simply remove the originator scheduled for deletion.
1107 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1108 struct batadv_tt_global_entry *tt_global_entry,
1109 struct batadv_orig_node *orig_node,
1110 const char *message)
1112 bool last_entry = true;
1113 struct hlist_head *head;
1114 struct hlist_node *node;
1115 struct batadv_tt_orig_list_entry *orig_entry;
1117 /* no local entry exists, case 1:
1118 * Check if this is the last one or if other entries exist.
1122 head = &tt_global_entry->orig_list;
1123 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1124 if (orig_entry->orig_node != orig_node) {
1132 /* its the last one, mark for roaming. */
1133 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1134 tt_global_entry->roam_at = jiffies;
1136 /* there is another entry, we can simply delete this
1137 * one and can still use the other one.
1139 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1140 orig_node, message);
1145 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1146 struct batadv_orig_node *orig_node,
1147 const unsigned char *addr,
1148 const char *message, bool roaming)
1150 struct batadv_tt_global_entry *tt_global_entry;
1151 struct batadv_tt_local_entry *local_entry = NULL;
1153 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1154 if (!tt_global_entry)
1158 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1159 orig_node, message);
1161 if (hlist_empty(&tt_global_entry->orig_list))
1162 batadv_tt_global_free(bat_priv, tt_global_entry,
1168 /* if we are deleting a global entry due to a roam
1169 * event, there are two possibilities:
1170 * 1) the client roamed from node A to node B => if there
1171 * is only one originator left for this client, we mark
1172 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1173 * wait for node B to claim it. In case of timeout
1174 * the entry is purged.
1176 * If there are other originators left, we directly delete
1178 * 2) the client roamed to us => we can directly delete
1179 * the global entry, since it is useless now.
1181 local_entry = batadv_tt_local_hash_find(bat_priv,
1182 tt_global_entry->common.addr);
1184 /* local entry exists, case 2: client roamed to us. */
1185 batadv_tt_global_del_orig_list(tt_global_entry);
1186 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1188 /* no local entry exists, case 1: check for roaming */
1189 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1190 orig_node, message);
1194 if (tt_global_entry)
1195 batadv_tt_global_entry_free_ref(tt_global_entry);
1197 batadv_tt_local_entry_free_ref(local_entry);
1200 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1201 struct batadv_orig_node *orig_node,
1202 const char *message)
1204 struct batadv_tt_global_entry *tt_global;
1205 struct batadv_tt_common_entry *tt_common_entry;
1207 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1208 struct hlist_node *node, *safe;
1209 struct hlist_head *head;
1210 spinlock_t *list_lock; /* protects write access to the hash lists */
1215 for (i = 0; i < hash->size; i++) {
1216 head = &hash->table[i];
1217 list_lock = &hash->list_locks[i];
1219 spin_lock_bh(list_lock);
1220 hlist_for_each_entry_safe(tt_common_entry, node, safe,
1222 tt_global = container_of(tt_common_entry,
1223 struct batadv_tt_global_entry,
1226 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1227 orig_node, message);
1229 if (hlist_empty(&tt_global->orig_list)) {
1230 batadv_dbg(BATADV_DBG_TT, bat_priv,
1231 "Deleting global tt entry %pM: %s\n",
1232 tt_global->common.addr, message);
1233 hlist_del_rcu(node);
1234 batadv_tt_global_entry_free_ref(tt_global);
1237 spin_unlock_bh(list_lock);
1239 orig_node->tt_initialised = false;
1242 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1246 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1247 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1249 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1250 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1252 *msg = "Roaming timeout\n";
1255 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1256 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1258 *msg = "Temporary client timeout\n";
1264 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1266 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1267 struct hlist_head *head;
1268 struct hlist_node *node, *node_tmp;
1269 spinlock_t *list_lock; /* protects write access to the hash lists */
1272 struct batadv_tt_common_entry *tt_common;
1273 struct batadv_tt_global_entry *tt_global;
1275 for (i = 0; i < hash->size; i++) {
1276 head = &hash->table[i];
1277 list_lock = &hash->list_locks[i];
1279 spin_lock_bh(list_lock);
1280 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1282 tt_global = container_of(tt_common,
1283 struct batadv_tt_global_entry,
1286 if (!batadv_tt_global_to_purge(tt_global, &msg))
1289 batadv_dbg(BATADV_DBG_TT, bat_priv,
1290 "Deleting global tt entry (%pM): %s\n",
1291 tt_global->common.addr, msg);
1293 hlist_del_rcu(node);
1295 batadv_tt_global_entry_free_ref(tt_global);
1297 spin_unlock_bh(list_lock);
1301 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1303 struct batadv_hashtable *hash;
1304 spinlock_t *list_lock; /* protects write access to the hash lists */
1305 struct batadv_tt_common_entry *tt_common_entry;
1306 struct batadv_tt_global_entry *tt_global;
1307 struct hlist_node *node, *node_tmp;
1308 struct hlist_head *head;
1311 if (!bat_priv->tt.global_hash)
1314 hash = bat_priv->tt.global_hash;
1316 for (i = 0; i < hash->size; i++) {
1317 head = &hash->table[i];
1318 list_lock = &hash->list_locks[i];
1320 spin_lock_bh(list_lock);
1321 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1323 hlist_del_rcu(node);
1324 tt_global = container_of(tt_common_entry,
1325 struct batadv_tt_global_entry,
1327 batadv_tt_global_entry_free_ref(tt_global);
1329 spin_unlock_bh(list_lock);
1332 batadv_hash_destroy(hash);
1334 bat_priv->tt.global_hash = NULL;
1338 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1339 struct batadv_tt_global_entry *tt_global_entry)
1343 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1344 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1350 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1352 const uint8_t *addr)
1354 struct batadv_tt_local_entry *tt_local_entry = NULL;
1355 struct batadv_tt_global_entry *tt_global_entry = NULL;
1356 struct batadv_orig_node *orig_node = NULL;
1357 struct batadv_tt_orig_list_entry *best_entry;
1359 if (src && atomic_read(&bat_priv->ap_isolation)) {
1360 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1361 if (!tt_local_entry ||
1362 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1366 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1367 if (!tt_global_entry)
1370 /* check whether the clients should not communicate due to AP
1373 if (tt_local_entry &&
1374 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1378 best_entry = batadv_transtable_best_orig(tt_global_entry);
1379 /* found anything? */
1381 orig_node = best_entry->orig_node;
1382 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1387 if (tt_global_entry)
1388 batadv_tt_global_entry_free_ref(tt_global_entry);
1390 batadv_tt_local_entry_free_ref(tt_local_entry);
1395 /* Calculates the checksum of the local table of a given orig_node */
1396 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1397 struct batadv_orig_node *orig_node)
1399 uint16_t total = 0, total_one;
1400 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1401 struct batadv_tt_common_entry *tt_common;
1402 struct batadv_tt_global_entry *tt_global;
1403 struct hlist_node *node;
1404 struct hlist_head *head;
1408 for (i = 0; i < hash->size; i++) {
1409 head = &hash->table[i];
1412 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1413 tt_global = container_of(tt_common,
1414 struct batadv_tt_global_entry,
1416 /* Roaming clients are in the global table for
1417 * consistency only. They don't have to be
1418 * taken into account while computing the
1421 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1423 /* Temporary clients have not been announced yet, so
1424 * they have to be skipped while computing the global
1427 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1430 /* find out if this global entry is announced by this
1433 if (!batadv_tt_global_entry_has_orig(tt_global,
1438 for (j = 0; j < ETH_ALEN; j++)
1439 total_one = crc16_byte(total_one,
1440 tt_common->addr[j]);
1449 /* Calculates the checksum of the local table */
1450 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1452 uint16_t total = 0, total_one;
1453 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1454 struct batadv_tt_common_entry *tt_common;
1455 struct hlist_node *node;
1456 struct hlist_head *head;
1460 for (i = 0; i < hash->size; i++) {
1461 head = &hash->table[i];
1464 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1465 /* not yet committed clients have not to be taken into
1466 * account while computing the CRC
1468 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1471 for (j = 0; j < ETH_ALEN; j++)
1472 total_one = crc16_byte(total_one,
1473 tt_common->addr[j]);
1482 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1484 struct batadv_tt_req_node *node, *safe;
1486 spin_lock_bh(&bat_priv->tt.req_list_lock);
1488 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1489 list_del(&node->list);
1493 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1496 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1497 struct batadv_orig_node *orig_node,
1498 const unsigned char *tt_buff,
1499 uint8_t tt_num_changes)
1501 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1503 /* Replace the old buffer only if I received something in the
1504 * last OGM (the OGM could carry no changes)
1506 spin_lock_bh(&orig_node->tt_buff_lock);
1507 if (tt_buff_len > 0) {
1508 kfree(orig_node->tt_buff);
1509 orig_node->tt_buff_len = 0;
1510 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1511 if (orig_node->tt_buff) {
1512 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1513 orig_node->tt_buff_len = tt_buff_len;
1516 spin_unlock_bh(&orig_node->tt_buff_lock);
1519 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1521 struct batadv_tt_req_node *node, *safe;
1523 spin_lock_bh(&bat_priv->tt.req_list_lock);
1524 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1525 if (batadv_has_timed_out(node->issued_at,
1526 BATADV_TT_REQUEST_TIMEOUT)) {
1527 list_del(&node->list);
1531 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1534 /* returns the pointer to the new tt_req_node struct if no request
1535 * has already been issued for this orig_node, NULL otherwise
1537 static struct batadv_tt_req_node *
1538 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1539 struct batadv_orig_node *orig_node)
1541 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1543 spin_lock_bh(&bat_priv->tt.req_list_lock);
1544 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1545 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1546 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1547 BATADV_TT_REQUEST_TIMEOUT))
1551 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1555 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1556 tt_req_node->issued_at = jiffies;
1558 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1560 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1564 /* data_ptr is useless here, but has to be kept to respect the prototype */
1565 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1566 const void *data_ptr)
1568 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1570 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1575 static int batadv_tt_global_valid(const void *entry_ptr,
1576 const void *data_ptr)
1578 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1579 const struct batadv_tt_global_entry *tt_global_entry;
1580 const struct batadv_orig_node *orig_node = data_ptr;
1582 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1583 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1586 tt_global_entry = container_of(tt_common_entry,
1587 struct batadv_tt_global_entry,
1590 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1593 static struct sk_buff *
1594 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1595 struct batadv_hashtable *hash,
1596 struct batadv_hard_iface *primary_if,
1597 int (*valid_cb)(const void *, const void *),
1600 struct batadv_tt_common_entry *tt_common_entry;
1601 struct batadv_tt_query_packet *tt_response;
1602 struct batadv_tt_change *tt_change;
1603 struct hlist_node *node;
1604 struct hlist_head *head;
1605 struct sk_buff *skb = NULL;
1606 uint16_t tt_tot, tt_count;
1607 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1611 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1612 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1613 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1615 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1617 len = tt_query_size + tt_len;
1618 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1622 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1623 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1624 tt_response->ttvn = ttvn;
1626 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1630 for (i = 0; i < hash->size; i++) {
1631 head = &hash->table[i];
1633 hlist_for_each_entry_rcu(tt_common_entry, node,
1635 if (tt_count == tt_tot)
1638 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1641 memcpy(tt_change->addr, tt_common_entry->addr,
1643 tt_change->flags = tt_common_entry->flags;
1651 /* store in the message the number of entries we have successfully
1654 tt_response->tt_data = htons(tt_count);
1660 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1661 struct batadv_orig_node *dst_orig_node,
1662 uint8_t ttvn, uint16_t tt_crc,
1665 struct sk_buff *skb = NULL;
1666 struct batadv_tt_query_packet *tt_request;
1667 struct batadv_hard_iface *primary_if;
1668 struct batadv_tt_req_node *tt_req_node = NULL;
1672 primary_if = batadv_primary_if_get_selected(bat_priv);
1676 /* The new tt_req will be issued only if I'm not waiting for a
1677 * reply from the same orig_node yet
1679 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1683 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1687 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1689 tt_req_len = sizeof(*tt_request);
1690 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1692 tt_request->header.packet_type = BATADV_TT_QUERY;
1693 tt_request->header.version = BATADV_COMPAT_VERSION;
1694 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1695 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1696 tt_request->header.ttl = BATADV_TTL;
1697 tt_request->ttvn = ttvn;
1698 tt_request->tt_data = htons(tt_crc);
1699 tt_request->flags = BATADV_TT_REQUEST;
1702 tt_request->flags |= BATADV_TT_FULL_TABLE;
1704 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1705 dst_orig_node->orig, (full_table ? 'F' : '.'));
1707 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1709 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1714 batadv_hardif_free_ref(primary_if);
1717 if (ret && tt_req_node) {
1718 spin_lock_bh(&bat_priv->tt.req_list_lock);
1719 list_del(&tt_req_node->list);
1720 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1727 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1728 struct batadv_tt_query_packet *tt_request)
1730 struct batadv_orig_node *req_dst_orig_node;
1731 struct batadv_orig_node *res_dst_orig_node = NULL;
1732 struct batadv_hard_iface *primary_if = NULL;
1733 uint8_t orig_ttvn, req_ttvn, ttvn;
1735 unsigned char *tt_buff;
1737 uint16_t tt_len, tt_tot;
1738 struct sk_buff *skb = NULL;
1739 struct batadv_tt_query_packet *tt_response;
1740 uint8_t *packet_pos;
1743 batadv_dbg(BATADV_DBG_TT, bat_priv,
1744 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1745 tt_request->src, tt_request->ttvn, tt_request->dst,
1746 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1748 /* Let's get the orig node of the REAL destination */
1749 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1750 if (!req_dst_orig_node)
1753 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1754 if (!res_dst_orig_node)
1757 primary_if = batadv_primary_if_get_selected(bat_priv);
1761 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1762 req_ttvn = tt_request->ttvn;
1764 /* I don't have the requested data */
1765 if (orig_ttvn != req_ttvn ||
1766 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1769 /* If the full table has been explicitly requested */
1770 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1771 !req_dst_orig_node->tt_buff)
1776 /* In this version, fragmentation is not implemented, then
1777 * I'll send only one packet with as much TT entries as I can
1780 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1781 tt_len = req_dst_orig_node->tt_buff_len;
1782 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1784 len = sizeof(*tt_response) + tt_len;
1785 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1789 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1790 packet_pos = skb_put(skb, len);
1791 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1792 tt_response->ttvn = req_ttvn;
1793 tt_response->tt_data = htons(tt_tot);
1795 tt_buff = skb->data + sizeof(*tt_response);
1796 /* Copy the last orig_node's OGM buffer */
1797 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1798 req_dst_orig_node->tt_buff_len);
1800 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1802 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1803 tt_len *= sizeof(struct batadv_tt_change);
1804 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1806 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1807 bat_priv->tt.global_hash,
1809 batadv_tt_global_valid,
1814 tt_response = (struct batadv_tt_query_packet *)skb->data;
1817 tt_response->header.packet_type = BATADV_TT_QUERY;
1818 tt_response->header.version = BATADV_COMPAT_VERSION;
1819 tt_response->header.ttl = BATADV_TTL;
1820 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1821 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1822 tt_response->flags = BATADV_TT_RESPONSE;
1825 tt_response->flags |= BATADV_TT_FULL_TABLE;
1827 batadv_dbg(BATADV_DBG_TT, bat_priv,
1828 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1829 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
1831 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1833 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1838 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1841 if (res_dst_orig_node)
1842 batadv_orig_node_free_ref(res_dst_orig_node);
1843 if (req_dst_orig_node)
1844 batadv_orig_node_free_ref(req_dst_orig_node);
1846 batadv_hardif_free_ref(primary_if);
1854 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1855 struct batadv_tt_query_packet *tt_request)
1857 struct batadv_orig_node *orig_node;
1858 struct batadv_hard_iface *primary_if = NULL;
1859 uint8_t my_ttvn, req_ttvn, ttvn;
1861 unsigned char *tt_buff;
1863 uint16_t tt_len, tt_tot;
1864 struct sk_buff *skb = NULL;
1865 struct batadv_tt_query_packet *tt_response;
1866 uint8_t *packet_pos;
1869 batadv_dbg(BATADV_DBG_TT, bat_priv,
1870 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1871 tt_request->src, tt_request->ttvn,
1872 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1875 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1876 req_ttvn = tt_request->ttvn;
1878 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1882 primary_if = batadv_primary_if_get_selected(bat_priv);
1886 /* If the full table has been explicitly requested or the gap
1887 * is too big send the whole local translation table
1889 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1890 !bat_priv->tt.last_changeset)
1895 /* In this version, fragmentation is not implemented, then
1896 * I'll send only one packet with as much TT entries as I can
1899 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1900 tt_len = bat_priv->tt.last_changeset_len;
1901 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1903 len = sizeof(*tt_response) + tt_len;
1904 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1908 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1909 packet_pos = skb_put(skb, len);
1910 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1911 tt_response->ttvn = req_ttvn;
1912 tt_response->tt_data = htons(tt_tot);
1914 tt_buff = skb->data + sizeof(*tt_response);
1915 memcpy(tt_buff, bat_priv->tt.last_changeset,
1916 bat_priv->tt.last_changeset_len);
1917 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1919 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1920 tt_len *= sizeof(struct batadv_tt_change);
1921 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1923 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1924 bat_priv->tt.local_hash,
1926 batadv_tt_local_valid_entry,
1931 tt_response = (struct batadv_tt_query_packet *)skb->data;
1934 tt_response->header.packet_type = BATADV_TT_QUERY;
1935 tt_response->header.version = BATADV_COMPAT_VERSION;
1936 tt_response->header.ttl = BATADV_TTL;
1937 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1938 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1939 tt_response->flags = BATADV_TT_RESPONSE;
1942 tt_response->flags |= BATADV_TT_FULL_TABLE;
1944 batadv_dbg(BATADV_DBG_TT, bat_priv,
1945 "Sending TT_RESPONSE to %pM [%c]\n",
1947 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1949 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1951 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1956 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1959 batadv_orig_node_free_ref(orig_node);
1961 batadv_hardif_free_ref(primary_if);
1964 /* This packet was for me, so it doesn't need to be re-routed */
1968 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1969 struct batadv_tt_query_packet *tt_request)
1971 if (batadv_is_my_mac(tt_request->dst)) {
1972 /* don't answer backbone gws! */
1973 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1976 return batadv_send_my_tt_response(bat_priv, tt_request);
1978 return batadv_send_other_tt_response(bat_priv, tt_request);
1982 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1983 struct batadv_orig_node *orig_node,
1984 struct batadv_tt_change *tt_change,
1985 uint16_t tt_num_changes, uint8_t ttvn)
1990 for (i = 0; i < tt_num_changes; i++) {
1991 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1992 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1993 batadv_tt_global_del(bat_priv, orig_node,
1994 (tt_change + i)->addr,
1995 "tt removed by changes",
1998 if (!batadv_tt_global_add(bat_priv, orig_node,
1999 (tt_change + i)->addr,
2000 (tt_change + i)->flags, ttvn))
2001 /* In case of problem while storing a
2002 * global_entry, we stop the updating
2003 * procedure without committing the
2004 * ttvn change. This will avoid to send
2005 * corrupted data on tt_request
2010 orig_node->tt_initialised = true;
2013 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2014 struct batadv_tt_query_packet *tt_response)
2016 struct batadv_orig_node *orig_node;
2018 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2022 /* Purge the old table first.. */
2023 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
2025 _batadv_tt_update_changes(bat_priv, orig_node,
2026 (struct batadv_tt_change *)(tt_response + 1),
2027 ntohs(tt_response->tt_data),
2030 spin_lock_bh(&orig_node->tt_buff_lock);
2031 kfree(orig_node->tt_buff);
2032 orig_node->tt_buff_len = 0;
2033 orig_node->tt_buff = NULL;
2034 spin_unlock_bh(&orig_node->tt_buff_lock);
2036 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2040 batadv_orig_node_free_ref(orig_node);
2043 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2044 struct batadv_orig_node *orig_node,
2045 uint16_t tt_num_changes, uint8_t ttvn,
2046 struct batadv_tt_change *tt_change)
2048 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2049 tt_num_changes, ttvn);
2051 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2052 (unsigned char *)tt_change, tt_num_changes);
2053 atomic_set(&orig_node->last_ttvn, ttvn);
2056 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2058 struct batadv_tt_local_entry *tt_local_entry;
2061 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2062 if (!tt_local_entry)
2064 /* Check if the client has been logically deleted (but is kept for
2065 * consistency purpose)
2067 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2068 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2073 batadv_tt_local_entry_free_ref(tt_local_entry);
2077 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2078 struct batadv_tt_query_packet *tt_response)
2080 struct batadv_tt_req_node *node, *safe;
2081 struct batadv_orig_node *orig_node = NULL;
2082 struct batadv_tt_change *tt_change;
2084 batadv_dbg(BATADV_DBG_TT, bat_priv,
2085 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2086 tt_response->src, tt_response->ttvn,
2087 ntohs(tt_response->tt_data),
2088 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2090 /* we should have never asked a backbone gw */
2091 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2094 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2098 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2099 batadv_tt_fill_gtable(bat_priv, tt_response);
2101 tt_change = (struct batadv_tt_change *)(tt_response + 1);
2102 batadv_tt_update_changes(bat_priv, orig_node,
2103 ntohs(tt_response->tt_data),
2104 tt_response->ttvn, tt_change);
2107 /* Delete the tt_req_node from pending tt_requests list */
2108 spin_lock_bh(&bat_priv->tt.req_list_lock);
2109 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2110 if (!batadv_compare_eth(node->addr, tt_response->src))
2112 list_del(&node->list);
2115 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2117 /* Recalculate the CRC for this orig_node and store it */
2118 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2121 batadv_orig_node_free_ref(orig_node);
2124 int batadv_tt_init(struct batadv_priv *bat_priv)
2128 ret = batadv_tt_local_init(bat_priv);
2132 ret = batadv_tt_global_init(bat_priv);
2136 batadv_tt_start_timer(bat_priv);
2141 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2143 struct batadv_tt_roam_node *node, *safe;
2145 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2147 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2148 list_del(&node->list);
2152 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2155 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2157 struct batadv_tt_roam_node *node, *safe;
2159 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2160 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2161 if (!batadv_has_timed_out(node->first_time,
2162 BATADV_ROAMING_MAX_TIME))
2165 list_del(&node->list);
2168 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2171 /* This function checks whether the client already reached the
2172 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2175 * returns true if the ROAMING_ADV can be sent, false otherwise
2177 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2180 struct batadv_tt_roam_node *tt_roam_node;
2183 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2184 /* The new tt_req will be issued only if I'm not waiting for a
2185 * reply from the same orig_node yet
2187 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2188 if (!batadv_compare_eth(tt_roam_node->addr, client))
2191 if (batadv_has_timed_out(tt_roam_node->first_time,
2192 BATADV_ROAMING_MAX_TIME))
2195 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2196 /* Sorry, you roamed too many times! */
2203 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2207 tt_roam_node->first_time = jiffies;
2208 atomic_set(&tt_roam_node->counter,
2209 BATADV_ROAMING_MAX_COUNT - 1);
2210 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2212 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2217 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2221 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2222 struct batadv_orig_node *orig_node)
2224 struct sk_buff *skb = NULL;
2225 struct batadv_roam_adv_packet *roam_adv_packet;
2227 struct batadv_hard_iface *primary_if;
2228 size_t len = sizeof(*roam_adv_packet);
2230 /* before going on we have to check whether the client has
2231 * already roamed to us too many times
2233 if (!batadv_tt_check_roam_count(bat_priv, client))
2236 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2240 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2242 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2244 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2245 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2246 roam_adv_packet->header.ttl = BATADV_TTL;
2247 roam_adv_packet->reserved = 0;
2248 primary_if = batadv_primary_if_get_selected(bat_priv);
2251 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2252 batadv_hardif_free_ref(primary_if);
2253 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2254 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2256 batadv_dbg(BATADV_DBG_TT, bat_priv,
2257 "Sending ROAMING_ADV to %pM (client %pM)\n",
2258 orig_node->orig, client);
2260 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2262 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2271 static void batadv_tt_purge(struct work_struct *work)
2273 struct delayed_work *delayed_work;
2274 struct batadv_priv_tt *priv_tt;
2275 struct batadv_priv *bat_priv;
2277 delayed_work = container_of(work, struct delayed_work, work);
2278 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2279 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2281 batadv_tt_local_purge(bat_priv);
2282 batadv_tt_global_purge(bat_priv);
2283 batadv_tt_req_purge(bat_priv);
2284 batadv_tt_roam_purge(bat_priv);
2286 batadv_tt_start_timer(bat_priv);
2289 void batadv_tt_free(struct batadv_priv *bat_priv)
2291 cancel_delayed_work_sync(&bat_priv->tt.work);
2293 batadv_tt_local_table_free(bat_priv);
2294 batadv_tt_global_table_free(bat_priv);
2295 batadv_tt_req_list_free(bat_priv);
2296 batadv_tt_changes_list_free(bat_priv);
2297 batadv_tt_roam_list_free(bat_priv);
2299 kfree(bat_priv->tt.last_changeset);
2302 /* This function will enable or disable the specified flags for all the entries
2303 * in the given hash table and returns the number of modified entries
2305 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2306 uint16_t flags, bool enable)
2309 uint16_t changed_num = 0;
2310 struct hlist_head *head;
2311 struct hlist_node *node;
2312 struct batadv_tt_common_entry *tt_common_entry;
2317 for (i = 0; i < hash->size; i++) {
2318 head = &hash->table[i];
2321 hlist_for_each_entry_rcu(tt_common_entry, node,
2324 if ((tt_common_entry->flags & flags) == flags)
2326 tt_common_entry->flags |= flags;
2328 if (!(tt_common_entry->flags & flags))
2330 tt_common_entry->flags &= ~flags;
2340 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2341 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2343 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2344 struct batadv_tt_common_entry *tt_common;
2345 struct batadv_tt_local_entry *tt_local;
2346 struct hlist_node *node, *node_tmp;
2347 struct hlist_head *head;
2348 spinlock_t *list_lock; /* protects write access to the hash lists */
2354 for (i = 0; i < hash->size; i++) {
2355 head = &hash->table[i];
2356 list_lock = &hash->list_locks[i];
2358 spin_lock_bh(list_lock);
2359 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2361 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2364 batadv_dbg(BATADV_DBG_TT, bat_priv,
2365 "Deleting local tt entry (%pM): pending\n",
2368 atomic_dec(&bat_priv->tt.local_entry_num);
2369 hlist_del_rcu(node);
2370 tt_local = container_of(tt_common,
2371 struct batadv_tt_local_entry,
2373 batadv_tt_local_entry_free_ref(tt_local);
2375 spin_unlock_bh(list_lock);
2380 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2381 unsigned char **packet_buff,
2382 int *packet_buff_len, int packet_min_len)
2384 uint16_t changed_num = 0;
2386 if (atomic_read(&bat_priv->tt.local_changes) < 1)
2389 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2390 BATADV_TT_CLIENT_NEW, false);
2392 /* all reset entries have to be counted as local entries */
2393 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2394 batadv_tt_local_purge_pending_clients(bat_priv);
2395 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2397 /* Increment the TTVN only once per OGM interval */
2398 atomic_inc(&bat_priv->tt.vn);
2399 batadv_dbg(BATADV_DBG_TT, bat_priv,
2400 "Local changes committed, updating to ttvn %u\n",
2401 (uint8_t)atomic_read(&bat_priv->tt.vn));
2403 /* reset the sending counter */
2404 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2406 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2407 packet_buff_len, packet_min_len);
2410 /* when calling this function (hard_iface == primary_if) has to be true */
2411 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2412 unsigned char **packet_buff, int *packet_buff_len,
2417 /* if at least one change happened */
2418 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2422 /* if the changes have been sent often enough */
2423 if ((tt_num_changes < 0) &&
2424 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2425 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2426 packet_min_len, packet_min_len);
2430 return tt_num_changes;
2433 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2436 struct batadv_tt_local_entry *tt_local_entry = NULL;
2437 struct batadv_tt_global_entry *tt_global_entry = NULL;
2440 if (!atomic_read(&bat_priv->ap_isolation))
2443 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2444 if (!tt_local_entry)
2447 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2448 if (!tt_global_entry)
2451 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2457 if (tt_global_entry)
2458 batadv_tt_global_entry_free_ref(tt_global_entry);
2460 batadv_tt_local_entry_free_ref(tt_local_entry);
2464 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2465 struct batadv_orig_node *orig_node,
2466 const unsigned char *tt_buff, uint8_t tt_num_changes,
2467 uint8_t ttvn, uint16_t tt_crc)
2469 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2470 bool full_table = true;
2471 struct batadv_tt_change *tt_change;
2473 /* don't care about a backbone gateways updates. */
2474 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2477 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2478 * increased by one -> we can apply the attached changes
2480 if ((!orig_node->tt_initialised && ttvn == 1) ||
2481 ttvn - orig_ttvn == 1) {
2482 /* the OGM could not contain the changes due to their size or
2483 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2485 * In this case send a tt request
2487 if (!tt_num_changes) {
2492 tt_change = (struct batadv_tt_change *)tt_buff;
2493 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2496 /* Even if we received the precomputed crc with the OGM, we
2497 * prefer to recompute it to spot any possible inconsistency
2498 * in the global table
2500 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2502 /* The ttvn alone is not enough to guarantee consistency
2503 * because a single value could represent different states
2504 * (due to the wrap around). Thus a node has to check whether
2505 * the resulting table (after applying the changes) is still
2506 * consistent or not. E.g. a node could disconnect while its
2507 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2508 * checking the CRC value is mandatory to detect the
2511 if (orig_node->tt_crc != tt_crc)
2514 /* if we missed more than one change or our tables are not
2515 * in sync anymore -> request fresh tt data
2517 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2518 orig_node->tt_crc != tt_crc) {
2520 batadv_dbg(BATADV_DBG_TT, bat_priv,
2521 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2522 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2523 orig_node->tt_crc, tt_num_changes);
2524 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2525 tt_crc, full_table);
2531 /* returns true whether we know that the client has moved from its old
2532 * originator to another one. This entry is kept is still kept for consistency
2535 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2538 struct batadv_tt_global_entry *tt_global_entry;
2541 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2542 if (!tt_global_entry)
2545 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2546 batadv_tt_global_entry_free_ref(tt_global_entry);
2552 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2553 * @bat_priv: the bat priv with all the soft interface information
2554 * @addr: the MAC address of the local client to query
2556 * Returns true if the local client is known to be roaming (it is not served by
2557 * this node anymore) or not. If yes, the client is still present in the table
2558 * to keep the latter consistent with the node TTVN
2560 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2563 struct batadv_tt_local_entry *tt_local_entry;
2566 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2567 if (!tt_local_entry)
2570 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2571 batadv_tt_local_entry_free_ref(tt_local_entry);
2577 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2578 struct batadv_orig_node *orig_node,
2579 const unsigned char *addr)
2583 /* if the originator is a backbone node (meaning it belongs to the same
2584 * LAN of this node) the temporary client must not be added because to
2585 * reach such destination the node must use the LAN instead of the mesh
2587 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2590 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2591 BATADV_TT_CLIENT_TEMP,
2592 atomic_read(&orig_node->last_ttvn)))
2595 batadv_dbg(BATADV_DBG_TT, bat_priv,
2596 "Added temporary global client (addr: %pM orig: %pM)\n",
2597 addr, orig_node->orig);