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_hard_iface *primary_if;
476 struct hlist_node *node;
477 struct hlist_head *head;
480 primary_if = batadv_seq_print_text_primary_if_get(seq);
485 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
486 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
488 for (i = 0; i < hash->size; i++) {
489 head = &hash->table[i];
492 hlist_for_each_entry_rcu(tt_common_entry, node,
494 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
495 tt_common_entry->addr,
496 (tt_common_entry->flags &
497 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
498 (tt_common_entry->flags &
499 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
500 (tt_common_entry->flags &
501 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
502 (tt_common_entry->flags &
503 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
504 (tt_common_entry->flags &
505 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
511 batadv_hardif_free_ref(primary_if);
516 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
517 struct batadv_tt_local_entry *tt_local_entry,
518 uint16_t flags, const char *message)
520 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
521 tt_local_entry->common.flags | flags);
523 /* The local client has to be marked as "pending to be removed" but has
524 * to be kept in the table in order to send it in a full table
525 * response issued before the net ttvn increment (consistency check)
527 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
529 batadv_dbg(BATADV_DBG_TT, bat_priv,
530 "Local tt entry (%pM) pending to be removed: %s\n",
531 tt_local_entry->common.addr, message);
535 * batadv_tt_local_remove - logically remove an entry from the local table
536 * @bat_priv: the bat priv with all the soft interface information
537 * @addr: the MAC address of the client to remove
538 * @message: message to append to the log on deletion
539 * @roaming: true if the deletion is due to a roaming event
541 * Returns the flags assigned to the local entry before being deleted
543 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
544 const uint8_t *addr, const char *message,
547 struct batadv_tt_local_entry *tt_local_entry;
548 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
550 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
554 curr_flags = tt_local_entry->common.flags;
556 flags = BATADV_TT_CLIENT_DEL;
557 /* if this global entry addition is due to a roaming, the node has to
558 * mark the local entry as "roamed" in order to correctly reroute
562 flags |= BATADV_TT_CLIENT_ROAM;
563 /* mark the local client as ROAMed */
564 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
567 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
568 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
572 /* if this client has been added right now, it is possible to
573 * immediately purge it
575 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
576 curr_flags | BATADV_TT_CLIENT_DEL);
577 hlist_del_rcu(&tt_local_entry->common.hash_entry);
578 batadv_tt_local_entry_free_ref(tt_local_entry);
582 batadv_tt_local_entry_free_ref(tt_local_entry);
587 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
588 struct hlist_head *head)
590 struct batadv_tt_local_entry *tt_local_entry;
591 struct batadv_tt_common_entry *tt_common_entry;
592 struct hlist_node *node, *node_tmp;
594 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
596 tt_local_entry = container_of(tt_common_entry,
597 struct batadv_tt_local_entry,
599 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
602 /* entry already marked for deletion */
603 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
606 if (!batadv_has_timed_out(tt_local_entry->last_seen,
607 BATADV_TT_LOCAL_TIMEOUT))
610 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
611 BATADV_TT_CLIENT_DEL, "timed out");
615 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
617 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
618 struct hlist_head *head;
619 spinlock_t *list_lock; /* protects write access to the hash lists */
622 for (i = 0; i < hash->size; i++) {
623 head = &hash->table[i];
624 list_lock = &hash->list_locks[i];
626 spin_lock_bh(list_lock);
627 batadv_tt_local_purge_list(bat_priv, head);
628 spin_unlock_bh(list_lock);
633 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
635 struct batadv_hashtable *hash;
636 spinlock_t *list_lock; /* protects write access to the hash lists */
637 struct batadv_tt_common_entry *tt_common_entry;
638 struct batadv_tt_local_entry *tt_local;
639 struct hlist_node *node, *node_tmp;
640 struct hlist_head *head;
643 if (!bat_priv->tt.local_hash)
646 hash = bat_priv->tt.local_hash;
648 for (i = 0; i < hash->size; i++) {
649 head = &hash->table[i];
650 list_lock = &hash->list_locks[i];
652 spin_lock_bh(list_lock);
653 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
656 tt_local = container_of(tt_common_entry,
657 struct batadv_tt_local_entry,
659 batadv_tt_local_entry_free_ref(tt_local);
661 spin_unlock_bh(list_lock);
664 batadv_hash_destroy(hash);
666 bat_priv->tt.local_hash = NULL;
669 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
671 if (bat_priv->tt.global_hash)
674 bat_priv->tt.global_hash = batadv_hash_new(1024);
676 if (!bat_priv->tt.global_hash)
682 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
684 struct batadv_tt_change_node *entry, *safe;
686 spin_lock_bh(&bat_priv->tt.changes_list_lock);
688 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
690 list_del(&entry->list);
694 atomic_set(&bat_priv->tt.local_changes, 0);
695 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
698 /* retrieves the orig_tt_list_entry belonging to orig_node from the
699 * batadv_tt_global_entry list
701 * returns it with an increased refcounter, NULL if not found
703 static struct batadv_tt_orig_list_entry *
704 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
705 const struct batadv_orig_node *orig_node)
707 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
708 const struct hlist_head *head;
709 struct hlist_node *node;
712 head = &entry->orig_list;
713 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
714 if (tmp_orig_entry->orig_node != orig_node)
716 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
719 orig_entry = tmp_orig_entry;
727 /* find out if an orig_node is already in the list of a tt_global_entry.
728 * returns true if found, false otherwise
731 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
732 const struct batadv_orig_node *orig_node)
734 struct batadv_tt_orig_list_entry *orig_entry;
737 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
740 batadv_tt_orig_list_entry_free_ref(orig_entry);
747 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
748 struct batadv_orig_node *orig_node, int ttvn)
750 struct batadv_tt_orig_list_entry *orig_entry;
752 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
754 /* refresh the ttvn: the current value could be a bogus one that
755 * was added during a "temporary client detection"
757 orig_entry->ttvn = ttvn;
761 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
765 INIT_HLIST_NODE(&orig_entry->list);
766 atomic_inc(&orig_node->refcount);
767 atomic_inc(&orig_node->tt_size);
768 orig_entry->orig_node = orig_node;
769 orig_entry->ttvn = ttvn;
770 atomic_set(&orig_entry->refcount, 2);
772 spin_lock_bh(&tt_global->list_lock);
773 hlist_add_head_rcu(&orig_entry->list,
774 &tt_global->orig_list);
775 spin_unlock_bh(&tt_global->list_lock);
778 batadv_tt_orig_list_entry_free_ref(orig_entry);
781 /* caller must hold orig_node refcount */
782 int batadv_tt_global_add(struct batadv_priv *bat_priv,
783 struct batadv_orig_node *orig_node,
784 const unsigned char *tt_addr, uint8_t flags,
787 struct batadv_tt_global_entry *tt_global_entry;
788 struct batadv_tt_local_entry *tt_local_entry;
791 struct batadv_tt_common_entry *common;
792 uint16_t local_flags;
794 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
795 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
797 /* if the node already has a local client for this entry, it has to wait
798 * for a roaming advertisement instead of manually messing up the global
801 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
802 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
805 if (!tt_global_entry) {
806 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
807 if (!tt_global_entry)
810 common = &tt_global_entry->common;
811 memcpy(common->addr, tt_addr, ETH_ALEN);
813 common->flags = flags;
814 tt_global_entry->roam_at = 0;
815 /* node must store current time in case of roaming. This is
816 * needed to purge this entry out on timeout (if nobody claims
819 if (flags & BATADV_TT_CLIENT_ROAM)
820 tt_global_entry->roam_at = jiffies;
821 atomic_set(&common->refcount, 2);
822 common->added_at = jiffies;
824 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
825 spin_lock_init(&tt_global_entry->list_lock);
827 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
829 batadv_choose_orig, common,
830 &common->hash_entry);
832 if (unlikely(hash_added != 0)) {
833 /* remove the reference for the hash */
834 batadv_tt_global_entry_free_ref(tt_global_entry);
838 common = &tt_global_entry->common;
839 /* If there is already a global entry, we can use this one for
841 * But if we are trying to add a temporary client then here are
842 * two options at this point:
843 * 1) the global client is not a temporary client: the global
844 * client has to be left as it is, temporary information
845 * should never override any already known client state
846 * 2) the global client is a temporary client: purge the
847 * originator list and add the new one orig_entry
849 if (flags & BATADV_TT_CLIENT_TEMP) {
850 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
852 if (batadv_tt_global_entry_has_orig(tt_global_entry,
855 batadv_tt_global_del_orig_list(tt_global_entry);
859 /* if the client was temporary added before receiving the first
860 * OGM announcing it, we have to clear the TEMP flag
862 common->flags &= ~BATADV_TT_CLIENT_TEMP;
864 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
865 * one originator left in the list and we previously received a
866 * delete + roaming change for this originator.
868 * We should first delete the old originator before adding the
871 if (common->flags & BATADV_TT_CLIENT_ROAM) {
872 batadv_tt_global_del_orig_list(tt_global_entry);
873 common->flags &= ~BATADV_TT_CLIENT_ROAM;
874 tt_global_entry->roam_at = 0;
878 /* add the new orig_entry (if needed) or update it */
879 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
881 batadv_dbg(BATADV_DBG_TT, bat_priv,
882 "Creating new global tt entry: %pM (via %pM)\n",
883 common->addr, orig_node->orig);
888 /* remove address from local hash if present */
889 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
890 "global tt received",
891 !!(flags & BATADV_TT_CLIENT_ROAM));
892 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
894 if (!(flags & BATADV_TT_CLIENT_ROAM))
895 /* this is a normal global add. Therefore the client is not in a
896 * roaming state anymore.
898 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
902 batadv_tt_global_entry_free_ref(tt_global_entry);
904 batadv_tt_local_entry_free_ref(tt_local_entry);
908 /* print all orig nodes who announce the address for this global entry.
909 * it is assumed that the caller holds rcu_read_lock();
912 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
913 struct seq_file *seq)
915 struct hlist_head *head;
916 struct hlist_node *node;
917 struct batadv_tt_orig_list_entry *orig_entry;
918 struct batadv_tt_common_entry *tt_common_entry;
922 tt_common_entry = &tt_global_entry->common;
924 head = &tt_global_entry->orig_list;
926 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
927 flags = tt_common_entry->flags;
928 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
929 seq_printf(seq, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
930 tt_global_entry->common.addr, orig_entry->ttvn,
931 orig_entry->orig_node->orig, last_ttvn,
932 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
933 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
934 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
938 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
940 struct net_device *net_dev = (struct net_device *)seq->private;
941 struct batadv_priv *bat_priv = netdev_priv(net_dev);
942 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
943 struct batadv_tt_common_entry *tt_common_entry;
944 struct batadv_tt_global_entry *tt_global;
945 struct batadv_hard_iface *primary_if;
946 struct hlist_node *node;
947 struct hlist_head *head;
950 primary_if = batadv_seq_print_text_primary_if_get(seq);
955 "Globally announced TT entries received via the mesh %s\n",
957 seq_printf(seq, " %-13s %s %-15s %s %s\n",
958 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
960 for (i = 0; i < hash->size; i++) {
961 head = &hash->table[i];
964 hlist_for_each_entry_rcu(tt_common_entry, node,
966 tt_global = container_of(tt_common_entry,
967 struct batadv_tt_global_entry,
969 batadv_tt_global_print_entry(tt_global, seq);
975 batadv_hardif_free_ref(primary_if);
979 /* deletes the orig list of a tt_global_entry */
981 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
983 struct hlist_head *head;
984 struct hlist_node *node, *safe;
985 struct batadv_tt_orig_list_entry *orig_entry;
987 spin_lock_bh(&tt_global_entry->list_lock);
988 head = &tt_global_entry->orig_list;
989 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
991 batadv_tt_orig_list_entry_free_ref(orig_entry);
993 spin_unlock_bh(&tt_global_entry->list_lock);
998 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
999 struct batadv_tt_global_entry *tt_global_entry,
1000 struct batadv_orig_node *orig_node,
1001 const char *message)
1003 struct hlist_head *head;
1004 struct hlist_node *node, *safe;
1005 struct batadv_tt_orig_list_entry *orig_entry;
1007 spin_lock_bh(&tt_global_entry->list_lock);
1008 head = &tt_global_entry->orig_list;
1009 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1010 if (orig_entry->orig_node == orig_node) {
1011 batadv_dbg(BATADV_DBG_TT, bat_priv,
1012 "Deleting %pM from global tt entry %pM: %s\n",
1014 tt_global_entry->common.addr, message);
1015 hlist_del_rcu(node);
1016 batadv_tt_orig_list_entry_free_ref(orig_entry);
1019 spin_unlock_bh(&tt_global_entry->list_lock);
1022 /* If the client is to be deleted, we check if it is the last origantor entry
1023 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1024 * timer, otherwise we simply remove the originator scheduled for deletion.
1027 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1028 struct batadv_tt_global_entry *tt_global_entry,
1029 struct batadv_orig_node *orig_node,
1030 const char *message)
1032 bool last_entry = true;
1033 struct hlist_head *head;
1034 struct hlist_node *node;
1035 struct batadv_tt_orig_list_entry *orig_entry;
1037 /* no local entry exists, case 1:
1038 * Check if this is the last one or if other entries exist.
1042 head = &tt_global_entry->orig_list;
1043 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1044 if (orig_entry->orig_node != orig_node) {
1052 /* its the last one, mark for roaming. */
1053 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1054 tt_global_entry->roam_at = jiffies;
1056 /* there is another entry, we can simply delete this
1057 * one and can still use the other one.
1059 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1060 orig_node, message);
1065 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1066 struct batadv_orig_node *orig_node,
1067 const unsigned char *addr,
1068 const char *message, bool roaming)
1070 struct batadv_tt_global_entry *tt_global_entry;
1071 struct batadv_tt_local_entry *local_entry = NULL;
1073 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1074 if (!tt_global_entry)
1078 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1079 orig_node, message);
1081 if (hlist_empty(&tt_global_entry->orig_list))
1082 batadv_tt_global_free(bat_priv, tt_global_entry,
1088 /* if we are deleting a global entry due to a roam
1089 * event, there are two possibilities:
1090 * 1) the client roamed from node A to node B => if there
1091 * is only one originator left for this client, we mark
1092 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1093 * wait for node B to claim it. In case of timeout
1094 * the entry is purged.
1096 * If there are other originators left, we directly delete
1098 * 2) the client roamed to us => we can directly delete
1099 * the global entry, since it is useless now.
1101 local_entry = batadv_tt_local_hash_find(bat_priv,
1102 tt_global_entry->common.addr);
1104 /* local entry exists, case 2: client roamed to us. */
1105 batadv_tt_global_del_orig_list(tt_global_entry);
1106 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1108 /* no local entry exists, case 1: check for roaming */
1109 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1110 orig_node, message);
1114 if (tt_global_entry)
1115 batadv_tt_global_entry_free_ref(tt_global_entry);
1117 batadv_tt_local_entry_free_ref(local_entry);
1120 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1121 struct batadv_orig_node *orig_node,
1122 const char *message)
1124 struct batadv_tt_global_entry *tt_global;
1125 struct batadv_tt_common_entry *tt_common_entry;
1127 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1128 struct hlist_node *node, *safe;
1129 struct hlist_head *head;
1130 spinlock_t *list_lock; /* protects write access to the hash lists */
1135 for (i = 0; i < hash->size; i++) {
1136 head = &hash->table[i];
1137 list_lock = &hash->list_locks[i];
1139 spin_lock_bh(list_lock);
1140 hlist_for_each_entry_safe(tt_common_entry, node, safe,
1142 tt_global = container_of(tt_common_entry,
1143 struct batadv_tt_global_entry,
1146 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1147 orig_node, message);
1149 if (hlist_empty(&tt_global->orig_list)) {
1150 batadv_dbg(BATADV_DBG_TT, bat_priv,
1151 "Deleting global tt entry %pM: %s\n",
1152 tt_global->common.addr, message);
1153 hlist_del_rcu(node);
1154 batadv_tt_global_entry_free_ref(tt_global);
1157 spin_unlock_bh(list_lock);
1159 orig_node->tt_initialised = false;
1162 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1166 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1167 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1169 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1170 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1172 *msg = "Roaming timeout\n";
1175 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1176 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1178 *msg = "Temporary client timeout\n";
1184 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1186 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1187 struct hlist_head *head;
1188 struct hlist_node *node, *node_tmp;
1189 spinlock_t *list_lock; /* protects write access to the hash lists */
1192 struct batadv_tt_common_entry *tt_common;
1193 struct batadv_tt_global_entry *tt_global;
1195 for (i = 0; i < hash->size; i++) {
1196 head = &hash->table[i];
1197 list_lock = &hash->list_locks[i];
1199 spin_lock_bh(list_lock);
1200 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1202 tt_global = container_of(tt_common,
1203 struct batadv_tt_global_entry,
1206 if (!batadv_tt_global_to_purge(tt_global, &msg))
1209 batadv_dbg(BATADV_DBG_TT, bat_priv,
1210 "Deleting global tt entry (%pM): %s\n",
1211 tt_global->common.addr, msg);
1213 hlist_del_rcu(node);
1215 batadv_tt_global_entry_free_ref(tt_global);
1217 spin_unlock_bh(list_lock);
1221 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1223 struct batadv_hashtable *hash;
1224 spinlock_t *list_lock; /* protects write access to the hash lists */
1225 struct batadv_tt_common_entry *tt_common_entry;
1226 struct batadv_tt_global_entry *tt_global;
1227 struct hlist_node *node, *node_tmp;
1228 struct hlist_head *head;
1231 if (!bat_priv->tt.global_hash)
1234 hash = bat_priv->tt.global_hash;
1236 for (i = 0; i < hash->size; i++) {
1237 head = &hash->table[i];
1238 list_lock = &hash->list_locks[i];
1240 spin_lock_bh(list_lock);
1241 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1243 hlist_del_rcu(node);
1244 tt_global = container_of(tt_common_entry,
1245 struct batadv_tt_global_entry,
1247 batadv_tt_global_entry_free_ref(tt_global);
1249 spin_unlock_bh(list_lock);
1252 batadv_hash_destroy(hash);
1254 bat_priv->tt.global_hash = NULL;
1258 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1259 struct batadv_tt_global_entry *tt_global_entry)
1263 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1264 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1270 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1272 const uint8_t *addr)
1274 struct batadv_tt_local_entry *tt_local_entry = NULL;
1275 struct batadv_tt_global_entry *tt_global_entry = NULL;
1276 struct batadv_orig_node *orig_node = NULL;
1277 struct batadv_neigh_node *router = NULL;
1278 struct hlist_head *head;
1279 struct hlist_node *node;
1280 struct batadv_tt_orig_list_entry *orig_entry;
1283 if (src && atomic_read(&bat_priv->ap_isolation)) {
1284 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1285 if (!tt_local_entry ||
1286 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1290 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1291 if (!tt_global_entry)
1294 /* check whether the clients should not communicate due to AP
1297 if (tt_local_entry &&
1298 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1304 head = &tt_global_entry->orig_list;
1305 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1306 router = batadv_orig_node_get_router(orig_entry->orig_node);
1310 if (router->tq_avg > best_tq) {
1311 orig_node = orig_entry->orig_node;
1312 best_tq = router->tq_avg;
1314 batadv_neigh_node_free_ref(router);
1316 /* found anything? */
1317 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1321 if (tt_global_entry)
1322 batadv_tt_global_entry_free_ref(tt_global_entry);
1324 batadv_tt_local_entry_free_ref(tt_local_entry);
1329 /* Calculates the checksum of the local table of a given orig_node */
1330 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1331 struct batadv_orig_node *orig_node)
1333 uint16_t total = 0, total_one;
1334 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1335 struct batadv_tt_common_entry *tt_common;
1336 struct batadv_tt_global_entry *tt_global;
1337 struct hlist_node *node;
1338 struct hlist_head *head;
1342 for (i = 0; i < hash->size; i++) {
1343 head = &hash->table[i];
1346 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1347 tt_global = container_of(tt_common,
1348 struct batadv_tt_global_entry,
1350 /* Roaming clients are in the global table for
1351 * consistency only. They don't have to be
1352 * taken into account while computing the
1355 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1357 /* Temporary clients have not been announced yet, so
1358 * they have to be skipped while computing the global
1361 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1364 /* find out if this global entry is announced by this
1367 if (!batadv_tt_global_entry_has_orig(tt_global,
1372 for (j = 0; j < ETH_ALEN; j++)
1373 total_one = crc16_byte(total_one,
1374 tt_common->addr[j]);
1383 /* Calculates the checksum of the local table */
1384 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1386 uint16_t total = 0, total_one;
1387 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1388 struct batadv_tt_common_entry *tt_common;
1389 struct hlist_node *node;
1390 struct hlist_head *head;
1394 for (i = 0; i < hash->size; i++) {
1395 head = &hash->table[i];
1398 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1399 /* not yet committed clients have not to be taken into
1400 * account while computing the CRC
1402 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1405 for (j = 0; j < ETH_ALEN; j++)
1406 total_one = crc16_byte(total_one,
1407 tt_common->addr[j]);
1416 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1418 struct batadv_tt_req_node *node, *safe;
1420 spin_lock_bh(&bat_priv->tt.req_list_lock);
1422 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1423 list_del(&node->list);
1427 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1430 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1431 struct batadv_orig_node *orig_node,
1432 const unsigned char *tt_buff,
1433 uint8_t tt_num_changes)
1435 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1437 /* Replace the old buffer only if I received something in the
1438 * last OGM (the OGM could carry no changes)
1440 spin_lock_bh(&orig_node->tt_buff_lock);
1441 if (tt_buff_len > 0) {
1442 kfree(orig_node->tt_buff);
1443 orig_node->tt_buff_len = 0;
1444 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1445 if (orig_node->tt_buff) {
1446 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1447 orig_node->tt_buff_len = tt_buff_len;
1450 spin_unlock_bh(&orig_node->tt_buff_lock);
1453 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1455 struct batadv_tt_req_node *node, *safe;
1457 spin_lock_bh(&bat_priv->tt.req_list_lock);
1458 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1459 if (batadv_has_timed_out(node->issued_at,
1460 BATADV_TT_REQUEST_TIMEOUT)) {
1461 list_del(&node->list);
1465 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1468 /* returns the pointer to the new tt_req_node struct if no request
1469 * has already been issued for this orig_node, NULL otherwise
1471 static struct batadv_tt_req_node *
1472 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1473 struct batadv_orig_node *orig_node)
1475 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1477 spin_lock_bh(&bat_priv->tt.req_list_lock);
1478 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1479 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1480 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1481 BATADV_TT_REQUEST_TIMEOUT))
1485 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1489 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1490 tt_req_node->issued_at = jiffies;
1492 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1494 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1498 /* data_ptr is useless here, but has to be kept to respect the prototype */
1499 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1500 const void *data_ptr)
1502 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1504 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1509 static int batadv_tt_global_valid(const void *entry_ptr,
1510 const void *data_ptr)
1512 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1513 const struct batadv_tt_global_entry *tt_global_entry;
1514 const struct batadv_orig_node *orig_node = data_ptr;
1516 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1517 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1520 tt_global_entry = container_of(tt_common_entry,
1521 struct batadv_tt_global_entry,
1524 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1527 static struct sk_buff *
1528 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1529 struct batadv_hashtable *hash,
1530 struct batadv_hard_iface *primary_if,
1531 int (*valid_cb)(const void *, const void *),
1534 struct batadv_tt_common_entry *tt_common_entry;
1535 struct batadv_tt_query_packet *tt_response;
1536 struct batadv_tt_change *tt_change;
1537 struct hlist_node *node;
1538 struct hlist_head *head;
1539 struct sk_buff *skb = NULL;
1540 uint16_t tt_tot, tt_count;
1541 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1545 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1546 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1547 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1549 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1551 len = tt_query_size + tt_len;
1552 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1556 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1557 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1558 tt_response->ttvn = ttvn;
1560 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1564 for (i = 0; i < hash->size; i++) {
1565 head = &hash->table[i];
1567 hlist_for_each_entry_rcu(tt_common_entry, node,
1569 if (tt_count == tt_tot)
1572 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1575 memcpy(tt_change->addr, tt_common_entry->addr,
1577 tt_change->flags = BATADV_NO_FLAGS;
1585 /* store in the message the number of entries we have successfully
1588 tt_response->tt_data = htons(tt_count);
1594 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1595 struct batadv_orig_node *dst_orig_node,
1596 uint8_t ttvn, uint16_t tt_crc,
1599 struct sk_buff *skb = NULL;
1600 struct batadv_tt_query_packet *tt_request;
1601 struct batadv_neigh_node *neigh_node = NULL;
1602 struct batadv_hard_iface *primary_if;
1603 struct batadv_tt_req_node *tt_req_node = NULL;
1607 primary_if = batadv_primary_if_get_selected(bat_priv);
1611 /* The new tt_req will be issued only if I'm not waiting for a
1612 * reply from the same orig_node yet
1614 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1618 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1622 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1624 tt_req_len = sizeof(*tt_request);
1625 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1627 tt_request->header.packet_type = BATADV_TT_QUERY;
1628 tt_request->header.version = BATADV_COMPAT_VERSION;
1629 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1630 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1631 tt_request->header.ttl = BATADV_TTL;
1632 tt_request->ttvn = ttvn;
1633 tt_request->tt_data = htons(tt_crc);
1634 tt_request->flags = BATADV_TT_REQUEST;
1637 tt_request->flags |= BATADV_TT_FULL_TABLE;
1639 neigh_node = batadv_orig_node_get_router(dst_orig_node);
1643 batadv_dbg(BATADV_DBG_TT, bat_priv,
1644 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1645 dst_orig_node->orig, neigh_node->addr,
1646 (full_table ? 'F' : '.'));
1648 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1650 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1655 batadv_neigh_node_free_ref(neigh_node);
1657 batadv_hardif_free_ref(primary_if);
1660 if (ret && tt_req_node) {
1661 spin_lock_bh(&bat_priv->tt.req_list_lock);
1662 list_del(&tt_req_node->list);
1663 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1670 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1671 struct batadv_tt_query_packet *tt_request)
1673 struct batadv_orig_node *req_dst_orig_node;
1674 struct batadv_orig_node *res_dst_orig_node = NULL;
1675 struct batadv_neigh_node *neigh_node = NULL;
1676 struct batadv_hard_iface *primary_if = NULL;
1677 uint8_t orig_ttvn, req_ttvn, ttvn;
1679 unsigned char *tt_buff;
1681 uint16_t tt_len, tt_tot;
1682 struct sk_buff *skb = NULL;
1683 struct batadv_tt_query_packet *tt_response;
1684 uint8_t *packet_pos;
1687 batadv_dbg(BATADV_DBG_TT, bat_priv,
1688 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1689 tt_request->src, tt_request->ttvn, tt_request->dst,
1690 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1692 /* Let's get the orig node of the REAL destination */
1693 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1694 if (!req_dst_orig_node)
1697 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1698 if (!res_dst_orig_node)
1701 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
1705 primary_if = batadv_primary_if_get_selected(bat_priv);
1709 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1710 req_ttvn = tt_request->ttvn;
1712 /* I don't have the requested data */
1713 if (orig_ttvn != req_ttvn ||
1714 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1717 /* If the full table has been explicitly requested */
1718 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1719 !req_dst_orig_node->tt_buff)
1724 /* In this version, fragmentation is not implemented, then
1725 * I'll send only one packet with as much TT entries as I can
1728 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1729 tt_len = req_dst_orig_node->tt_buff_len;
1730 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1732 len = sizeof(*tt_response) + tt_len;
1733 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1737 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1738 packet_pos = skb_put(skb, len);
1739 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1740 tt_response->ttvn = req_ttvn;
1741 tt_response->tt_data = htons(tt_tot);
1743 tt_buff = skb->data + sizeof(*tt_response);
1744 /* Copy the last orig_node's OGM buffer */
1745 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1746 req_dst_orig_node->tt_buff_len);
1748 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1750 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1751 tt_len *= sizeof(struct batadv_tt_change);
1752 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1754 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1755 bat_priv->tt.global_hash,
1757 batadv_tt_global_valid,
1762 tt_response = (struct batadv_tt_query_packet *)skb->data;
1765 tt_response->header.packet_type = BATADV_TT_QUERY;
1766 tt_response->header.version = BATADV_COMPAT_VERSION;
1767 tt_response->header.ttl = BATADV_TTL;
1768 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1769 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1770 tt_response->flags = BATADV_TT_RESPONSE;
1773 tt_response->flags |= BATADV_TT_FULL_TABLE;
1775 batadv_dbg(BATADV_DBG_TT, bat_priv,
1776 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1777 res_dst_orig_node->orig, neigh_node->addr,
1778 req_dst_orig_node->orig, req_ttvn);
1780 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1782 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1787 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1790 if (res_dst_orig_node)
1791 batadv_orig_node_free_ref(res_dst_orig_node);
1792 if (req_dst_orig_node)
1793 batadv_orig_node_free_ref(req_dst_orig_node);
1795 batadv_neigh_node_free_ref(neigh_node);
1797 batadv_hardif_free_ref(primary_if);
1805 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1806 struct batadv_tt_query_packet *tt_request)
1808 struct batadv_orig_node *orig_node;
1809 struct batadv_neigh_node *neigh_node = NULL;
1810 struct batadv_hard_iface *primary_if = NULL;
1811 uint8_t my_ttvn, req_ttvn, ttvn;
1813 unsigned char *tt_buff;
1815 uint16_t tt_len, tt_tot;
1816 struct sk_buff *skb = NULL;
1817 struct batadv_tt_query_packet *tt_response;
1818 uint8_t *packet_pos;
1821 batadv_dbg(BATADV_DBG_TT, bat_priv,
1822 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1823 tt_request->src, tt_request->ttvn,
1824 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1827 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1828 req_ttvn = tt_request->ttvn;
1830 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1834 neigh_node = batadv_orig_node_get_router(orig_node);
1838 primary_if = batadv_primary_if_get_selected(bat_priv);
1842 /* If the full table has been explicitly requested or the gap
1843 * is too big send the whole local translation table
1845 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1846 !bat_priv->tt.last_changeset)
1851 /* In this version, fragmentation is not implemented, then
1852 * I'll send only one packet with as much TT entries as I can
1855 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1856 tt_len = bat_priv->tt.last_changeset_len;
1857 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1859 len = sizeof(*tt_response) + tt_len;
1860 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1864 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1865 packet_pos = skb_put(skb, len);
1866 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1867 tt_response->ttvn = req_ttvn;
1868 tt_response->tt_data = htons(tt_tot);
1870 tt_buff = skb->data + sizeof(*tt_response);
1871 memcpy(tt_buff, bat_priv->tt.last_changeset,
1872 bat_priv->tt.last_changeset_len);
1873 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1875 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1876 tt_len *= sizeof(struct batadv_tt_change);
1877 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1879 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1880 bat_priv->tt.local_hash,
1882 batadv_tt_local_valid_entry,
1887 tt_response = (struct batadv_tt_query_packet *)skb->data;
1890 tt_response->header.packet_type = BATADV_TT_QUERY;
1891 tt_response->header.version = BATADV_COMPAT_VERSION;
1892 tt_response->header.ttl = BATADV_TTL;
1893 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1894 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1895 tt_response->flags = BATADV_TT_RESPONSE;
1898 tt_response->flags |= BATADV_TT_FULL_TABLE;
1900 batadv_dbg(BATADV_DBG_TT, bat_priv,
1901 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1902 orig_node->orig, neigh_node->addr,
1903 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1905 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1907 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1912 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1915 batadv_orig_node_free_ref(orig_node);
1917 batadv_neigh_node_free_ref(neigh_node);
1919 batadv_hardif_free_ref(primary_if);
1922 /* This packet was for me, so it doesn't need to be re-routed */
1926 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1927 struct batadv_tt_query_packet *tt_request)
1929 if (batadv_is_my_mac(tt_request->dst)) {
1930 /* don't answer backbone gws! */
1931 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1934 return batadv_send_my_tt_response(bat_priv, tt_request);
1936 return batadv_send_other_tt_response(bat_priv, tt_request);
1940 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1941 struct batadv_orig_node *orig_node,
1942 struct batadv_tt_change *tt_change,
1943 uint16_t tt_num_changes, uint8_t ttvn)
1948 for (i = 0; i < tt_num_changes; i++) {
1949 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1950 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1951 batadv_tt_global_del(bat_priv, orig_node,
1952 (tt_change + i)->addr,
1953 "tt removed by changes",
1956 if (!batadv_tt_global_add(bat_priv, orig_node,
1957 (tt_change + i)->addr,
1958 (tt_change + i)->flags, ttvn))
1959 /* In case of problem while storing a
1960 * global_entry, we stop the updating
1961 * procedure without committing the
1962 * ttvn change. This will avoid to send
1963 * corrupted data on tt_request
1968 orig_node->tt_initialised = true;
1971 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1972 struct batadv_tt_query_packet *tt_response)
1974 struct batadv_orig_node *orig_node;
1976 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1980 /* Purge the old table first.. */
1981 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
1983 _batadv_tt_update_changes(bat_priv, orig_node,
1984 (struct batadv_tt_change *)(tt_response + 1),
1985 ntohs(tt_response->tt_data),
1988 spin_lock_bh(&orig_node->tt_buff_lock);
1989 kfree(orig_node->tt_buff);
1990 orig_node->tt_buff_len = 0;
1991 orig_node->tt_buff = NULL;
1992 spin_unlock_bh(&orig_node->tt_buff_lock);
1994 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1998 batadv_orig_node_free_ref(orig_node);
2001 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2002 struct batadv_orig_node *orig_node,
2003 uint16_t tt_num_changes, uint8_t ttvn,
2004 struct batadv_tt_change *tt_change)
2006 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2007 tt_num_changes, ttvn);
2009 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2010 (unsigned char *)tt_change, tt_num_changes);
2011 atomic_set(&orig_node->last_ttvn, ttvn);
2014 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2016 struct batadv_tt_local_entry *tt_local_entry;
2019 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2020 if (!tt_local_entry)
2022 /* Check if the client has been logically deleted (but is kept for
2023 * consistency purpose)
2025 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2026 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2031 batadv_tt_local_entry_free_ref(tt_local_entry);
2035 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2036 struct batadv_tt_query_packet *tt_response)
2038 struct batadv_tt_req_node *node, *safe;
2039 struct batadv_orig_node *orig_node = NULL;
2040 struct batadv_tt_change *tt_change;
2042 batadv_dbg(BATADV_DBG_TT, bat_priv,
2043 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2044 tt_response->src, tt_response->ttvn,
2045 ntohs(tt_response->tt_data),
2046 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2048 /* we should have never asked a backbone gw */
2049 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2052 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2056 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2057 batadv_tt_fill_gtable(bat_priv, tt_response);
2059 tt_change = (struct batadv_tt_change *)(tt_response + 1);
2060 batadv_tt_update_changes(bat_priv, orig_node,
2061 ntohs(tt_response->tt_data),
2062 tt_response->ttvn, tt_change);
2065 /* Delete the tt_req_node from pending tt_requests list */
2066 spin_lock_bh(&bat_priv->tt.req_list_lock);
2067 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2068 if (!batadv_compare_eth(node->addr, tt_response->src))
2070 list_del(&node->list);
2073 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2075 /* Recalculate the CRC for this orig_node and store it */
2076 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2079 batadv_orig_node_free_ref(orig_node);
2082 int batadv_tt_init(struct batadv_priv *bat_priv)
2086 ret = batadv_tt_local_init(bat_priv);
2090 ret = batadv_tt_global_init(bat_priv);
2094 batadv_tt_start_timer(bat_priv);
2099 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2101 struct batadv_tt_roam_node *node, *safe;
2103 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2105 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2106 list_del(&node->list);
2110 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2113 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2115 struct batadv_tt_roam_node *node, *safe;
2117 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2118 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2119 if (!batadv_has_timed_out(node->first_time,
2120 BATADV_ROAMING_MAX_TIME))
2123 list_del(&node->list);
2126 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2129 /* This function checks whether the client already reached the
2130 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2133 * returns true if the ROAMING_ADV can be sent, false otherwise
2135 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2138 struct batadv_tt_roam_node *tt_roam_node;
2141 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2142 /* The new tt_req will be issued only if I'm not waiting for a
2143 * reply from the same orig_node yet
2145 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2146 if (!batadv_compare_eth(tt_roam_node->addr, client))
2149 if (batadv_has_timed_out(tt_roam_node->first_time,
2150 BATADV_ROAMING_MAX_TIME))
2153 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2154 /* Sorry, you roamed too many times! */
2161 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2165 tt_roam_node->first_time = jiffies;
2166 atomic_set(&tt_roam_node->counter,
2167 BATADV_ROAMING_MAX_COUNT - 1);
2168 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2170 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2175 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2179 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2180 struct batadv_orig_node *orig_node)
2182 struct batadv_neigh_node *neigh_node = NULL;
2183 struct sk_buff *skb = NULL;
2184 struct batadv_roam_adv_packet *roam_adv_packet;
2186 struct batadv_hard_iface *primary_if;
2187 size_t len = sizeof(*roam_adv_packet);
2189 /* before going on we have to check whether the client has
2190 * already roamed to us too many times
2192 if (!batadv_tt_check_roam_count(bat_priv, client))
2195 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2199 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2201 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2203 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2204 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2205 roam_adv_packet->header.ttl = BATADV_TTL;
2206 roam_adv_packet->reserved = 0;
2207 primary_if = batadv_primary_if_get_selected(bat_priv);
2210 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2211 batadv_hardif_free_ref(primary_if);
2212 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2213 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2215 neigh_node = batadv_orig_node_get_router(orig_node);
2219 batadv_dbg(BATADV_DBG_TT, bat_priv,
2220 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2221 orig_node->orig, client, neigh_node->addr);
2223 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2225 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
2230 batadv_neigh_node_free_ref(neigh_node);
2236 static void batadv_tt_purge(struct work_struct *work)
2238 struct delayed_work *delayed_work;
2239 struct batadv_priv_tt *priv_tt;
2240 struct batadv_priv *bat_priv;
2242 delayed_work = container_of(work, struct delayed_work, work);
2243 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2244 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2246 batadv_tt_local_purge(bat_priv);
2247 batadv_tt_global_purge(bat_priv);
2248 batadv_tt_req_purge(bat_priv);
2249 batadv_tt_roam_purge(bat_priv);
2251 batadv_tt_start_timer(bat_priv);
2254 void batadv_tt_free(struct batadv_priv *bat_priv)
2256 cancel_delayed_work_sync(&bat_priv->tt.work);
2258 batadv_tt_local_table_free(bat_priv);
2259 batadv_tt_global_table_free(bat_priv);
2260 batadv_tt_req_list_free(bat_priv);
2261 batadv_tt_changes_list_free(bat_priv);
2262 batadv_tt_roam_list_free(bat_priv);
2264 kfree(bat_priv->tt.last_changeset);
2267 /* This function will enable or disable the specified flags for all the entries
2268 * in the given hash table and returns the number of modified entries
2270 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2271 uint16_t flags, bool enable)
2274 uint16_t changed_num = 0;
2275 struct hlist_head *head;
2276 struct hlist_node *node;
2277 struct batadv_tt_common_entry *tt_common_entry;
2282 for (i = 0; i < hash->size; i++) {
2283 head = &hash->table[i];
2286 hlist_for_each_entry_rcu(tt_common_entry, node,
2289 if ((tt_common_entry->flags & flags) == flags)
2291 tt_common_entry->flags |= flags;
2293 if (!(tt_common_entry->flags & flags))
2295 tt_common_entry->flags &= ~flags;
2305 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2306 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2308 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2309 struct batadv_tt_common_entry *tt_common;
2310 struct batadv_tt_local_entry *tt_local;
2311 struct hlist_node *node, *node_tmp;
2312 struct hlist_head *head;
2313 spinlock_t *list_lock; /* protects write access to the hash lists */
2319 for (i = 0; i < hash->size; i++) {
2320 head = &hash->table[i];
2321 list_lock = &hash->list_locks[i];
2323 spin_lock_bh(list_lock);
2324 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2326 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2329 batadv_dbg(BATADV_DBG_TT, bat_priv,
2330 "Deleting local tt entry (%pM): pending\n",
2333 atomic_dec(&bat_priv->tt.local_entry_num);
2334 hlist_del_rcu(node);
2335 tt_local = container_of(tt_common,
2336 struct batadv_tt_local_entry,
2338 batadv_tt_local_entry_free_ref(tt_local);
2340 spin_unlock_bh(list_lock);
2345 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2346 unsigned char **packet_buff,
2347 int *packet_buff_len, int packet_min_len)
2349 uint16_t changed_num = 0;
2351 if (atomic_read(&bat_priv->tt.local_changes) < 1)
2354 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2355 BATADV_TT_CLIENT_NEW, false);
2357 /* all reset entries have to be counted as local entries */
2358 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2359 batadv_tt_local_purge_pending_clients(bat_priv);
2360 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2362 /* Increment the TTVN only once per OGM interval */
2363 atomic_inc(&bat_priv->tt.vn);
2364 batadv_dbg(BATADV_DBG_TT, bat_priv,
2365 "Local changes committed, updating to ttvn %u\n",
2366 (uint8_t)atomic_read(&bat_priv->tt.vn));
2368 /* reset the sending counter */
2369 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2371 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2372 packet_buff_len, packet_min_len);
2375 /* when calling this function (hard_iface == primary_if) has to be true */
2376 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2377 unsigned char **packet_buff, int *packet_buff_len,
2382 /* if at least one change happened */
2383 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2387 /* if the changes have been sent often enough */
2388 if ((tt_num_changes < 0) &&
2389 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2390 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2391 packet_min_len, packet_min_len);
2395 return tt_num_changes;
2398 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2401 struct batadv_tt_local_entry *tt_local_entry = NULL;
2402 struct batadv_tt_global_entry *tt_global_entry = NULL;
2405 if (!atomic_read(&bat_priv->ap_isolation))
2408 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2409 if (!tt_local_entry)
2412 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2413 if (!tt_global_entry)
2416 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2422 if (tt_global_entry)
2423 batadv_tt_global_entry_free_ref(tt_global_entry);
2425 batadv_tt_local_entry_free_ref(tt_local_entry);
2429 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2430 struct batadv_orig_node *orig_node,
2431 const unsigned char *tt_buff, uint8_t tt_num_changes,
2432 uint8_t ttvn, uint16_t tt_crc)
2434 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2435 bool full_table = true;
2436 struct batadv_tt_change *tt_change;
2438 /* don't care about a backbone gateways updates. */
2439 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2442 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2443 * increased by one -> we can apply the attached changes
2445 if ((!orig_node->tt_initialised && ttvn == 1) ||
2446 ttvn - orig_ttvn == 1) {
2447 /* the OGM could not contain the changes due to their size or
2448 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2450 * In this case send a tt request
2452 if (!tt_num_changes) {
2457 tt_change = (struct batadv_tt_change *)tt_buff;
2458 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2461 /* Even if we received the precomputed crc with the OGM, we
2462 * prefer to recompute it to spot any possible inconsistency
2463 * in the global table
2465 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2467 /* The ttvn alone is not enough to guarantee consistency
2468 * because a single value could represent different states
2469 * (due to the wrap around). Thus a node has to check whether
2470 * the resulting table (after applying the changes) is still
2471 * consistent or not. E.g. a node could disconnect while its
2472 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2473 * checking the CRC value is mandatory to detect the
2476 if (orig_node->tt_crc != tt_crc)
2479 /* if we missed more than one change or our tables are not
2480 * in sync anymore -> request fresh tt data
2482 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2483 orig_node->tt_crc != tt_crc) {
2485 batadv_dbg(BATADV_DBG_TT, bat_priv,
2486 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2487 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2488 orig_node->tt_crc, tt_num_changes);
2489 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2490 tt_crc, full_table);
2496 /* returns true whether we know that the client has moved from its old
2497 * originator to another one. This entry is kept is still kept for consistency
2500 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2503 struct batadv_tt_global_entry *tt_global_entry;
2506 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2507 if (!tt_global_entry)
2510 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2511 batadv_tt_global_entry_free_ref(tt_global_entry);
2517 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2518 * @bat_priv: the bat priv with all the soft interface information
2519 * @addr: the MAC address of the local client to query
2521 * Returns true if the local client is known to be roaming (it is not served by
2522 * this node anymore) or not. If yes, the client is still present in the table
2523 * to keep the latter consistent with the node TTVN
2525 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2528 struct batadv_tt_local_entry *tt_local_entry;
2531 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2532 if (!tt_local_entry)
2535 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2536 batadv_tt_local_entry_free_ref(tt_local_entry);
2542 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2543 struct batadv_orig_node *orig_node,
2544 const unsigned char *addr)
2548 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2549 BATADV_TT_CLIENT_TEMP,
2550 atomic_read(&orig_node->last_ttvn)))
2553 batadv_dbg(BATADV_DBG_TT, bat_priv,
2554 "Added temporary global client (addr: %pM orig: %pM)\n",
2555 addr, orig_node->orig);