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>
33 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
34 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
36 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
37 struct batadv_orig_node *orig_node);
38 static void batadv_tt_purge(struct work_struct *work);
40 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
41 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
42 struct batadv_orig_node *orig_node,
43 const unsigned char *addr,
44 const char *message, bool roaming);
46 /* returns 1 if they are the same mac addr */
47 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
49 const void *data1 = container_of(node, struct batadv_tt_common_entry,
52 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
55 static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
57 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
58 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
59 msecs_to_jiffies(5000));
62 static struct batadv_tt_common_entry *
63 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
65 struct hlist_head *head;
66 struct hlist_node *node;
67 struct batadv_tt_common_entry *tt_common_entry;
68 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
74 index = batadv_choose_orig(data, hash->size);
75 head = &hash->table[index];
78 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
79 if (!batadv_compare_eth(tt_common_entry, data))
82 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
85 tt_common_entry_tmp = tt_common_entry;
90 return tt_common_entry_tmp;
93 static struct batadv_tt_local_entry *
94 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
96 struct batadv_tt_common_entry *tt_common_entry;
97 struct batadv_tt_local_entry *tt_local_entry = NULL;
99 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
101 tt_local_entry = container_of(tt_common_entry,
102 struct batadv_tt_local_entry,
104 return tt_local_entry;
107 static struct batadv_tt_global_entry *
108 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
110 struct batadv_tt_common_entry *tt_common_entry;
111 struct batadv_tt_global_entry *tt_global_entry = NULL;
113 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
115 tt_global_entry = container_of(tt_common_entry,
116 struct batadv_tt_global_entry,
118 return tt_global_entry;
122 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
124 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
125 kfree_rcu(tt_local_entry, common.rcu);
128 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
130 struct batadv_tt_common_entry *tt_common_entry;
131 struct batadv_tt_global_entry *tt_global_entry;
133 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
134 tt_global_entry = container_of(tt_common_entry,
135 struct batadv_tt_global_entry, common);
137 kfree(tt_global_entry);
141 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
143 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
144 batadv_tt_global_del_orig_list(tt_global_entry);
145 call_rcu(&tt_global_entry->common.rcu,
146 batadv_tt_global_entry_free_rcu);
150 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
152 struct batadv_tt_orig_list_entry *orig_entry;
154 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
155 batadv_orig_node_free_ref(orig_entry->orig_node);
160 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
162 if (!atomic_dec_and_test(&orig_entry->refcount))
164 /* to avoid race conditions, immediately decrease the tt counter */
165 atomic_dec(&orig_entry->orig_node->tt_size);
166 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
169 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
170 const uint8_t *addr, uint8_t flags)
172 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
173 bool event_removed = false;
174 bool del_op_requested, del_op_entry;
176 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
181 tt_change_node->change.flags = flags;
182 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
184 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
186 /* check for ADD+DEL or DEL+ADD events */
187 spin_lock_bh(&bat_priv->tt.changes_list_lock);
188 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
190 if (!batadv_compare_eth(entry->change.addr, addr))
193 /* DEL+ADD in the same orig interval have no effect and can be
194 * removed to avoid silly behaviour on the receiver side. The
195 * other way around (ADD+DEL) can happen in case of roaming of
196 * a client still in the NEW state. Roaming of NEW clients is
197 * now possible due to automatically recognition of "temporary"
200 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
201 if (!del_op_requested && del_op_entry)
203 if (del_op_requested && !del_op_entry)
207 list_del(&entry->list);
209 kfree(tt_change_node);
210 event_removed = true;
214 /* track the change in the OGMinterval list */
215 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
218 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
221 atomic_dec(&bat_priv->tt.local_changes);
223 atomic_inc(&bat_priv->tt.local_changes);
226 int batadv_tt_len(int changes_num)
228 return changes_num * sizeof(struct batadv_tt_change);
231 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
233 if (bat_priv->tt.local_hash)
236 bat_priv->tt.local_hash = batadv_hash_new(1024);
238 if (!bat_priv->tt.local_hash)
241 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
242 &batadv_tt_local_hash_lock_class_key);
247 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
248 struct batadv_tt_global_entry *tt_global,
251 batadv_dbg(BATADV_DBG_TT, bat_priv,
252 "Deleting global tt entry %pM: %s\n",
253 tt_global->common.addr, message);
255 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
256 batadv_choose_orig, tt_global->common.addr);
257 batadv_tt_global_entry_free_ref(tt_global);
260 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
263 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
264 struct batadv_tt_local_entry *tt_local;
265 struct batadv_tt_global_entry *tt_global;
266 struct hlist_head *head;
267 struct hlist_node *node;
268 struct batadv_tt_orig_list_entry *orig_entry;
270 bool roamed_back = false;
272 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
273 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
276 tt_local->last_seen = jiffies;
277 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
278 batadv_dbg(BATADV_DBG_TT, bat_priv,
279 "Re-adding pending client %pM\n", addr);
280 /* whatever the reason why the PENDING flag was set,
281 * this is a client which was enqueued to be removed in
282 * this orig_interval. Since it popped up again, the
283 * flag can be reset like it was never enqueued
285 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
289 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
290 batadv_dbg(BATADV_DBG_TT, bat_priv,
291 "Roaming client %pM came back to its original location\n",
293 /* the ROAM flag is set because this client roamed away
294 * and the node got a roaming_advertisement message. Now
295 * that the client popped up again at its original
296 * location such flag can be unset
298 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
304 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
308 batadv_dbg(BATADV_DBG_TT, bat_priv,
309 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
310 (uint8_t)atomic_read(&bat_priv->tt.vn));
312 memcpy(tt_local->common.addr, addr, ETH_ALEN);
313 /* The local entry has to be marked as NEW to avoid to send it in
314 * a full table response going out before the next ttvn increment
315 * (consistency check)
317 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
318 if (batadv_is_wifi_iface(ifindex))
319 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
320 atomic_set(&tt_local->common.refcount, 2);
321 tt_local->last_seen = jiffies;
322 tt_local->common.added_at = tt_local->last_seen;
324 /* the batman interface mac address should never be purged */
325 if (batadv_compare_eth(addr, soft_iface->dev_addr))
326 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
328 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
329 batadv_choose_orig, &tt_local->common,
330 &tt_local->common.hash_entry);
332 if (unlikely(hash_added != 0)) {
333 /* remove the reference for the hash */
334 batadv_tt_local_entry_free_ref(tt_local);
339 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
342 /* Check whether it is a roaming, but don't do anything if the roaming
343 * process has already been handled
345 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
346 /* These node are probably going to update their tt table */
347 head = &tt_global->orig_list;
349 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
350 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
351 orig_entry->orig_node);
355 batadv_tt_global_free(bat_priv, tt_global,
359 /* The global entry has to be marked as ROAMING and
360 * has to be kept for consistency purpose
362 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
363 tt_global->roam_at = jiffies;
369 batadv_tt_local_entry_free_ref(tt_local);
371 batadv_tt_global_entry_free_ref(tt_global);
374 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
375 int *packet_buff_len,
379 unsigned char *new_buff;
381 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
383 /* keep old buffer if kmalloc should fail */
385 memcpy(new_buff, *packet_buff, min_packet_len);
387 *packet_buff = new_buff;
388 *packet_buff_len = new_packet_len;
392 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
393 unsigned char **packet_buff,
394 int *packet_buff_len,
397 struct batadv_hard_iface *primary_if;
400 primary_if = batadv_primary_if_get_selected(bat_priv);
402 req_len = min_packet_len;
403 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
405 /* if we have too many changes for one packet don't send any
406 * and wait for the tt table request which will be fragmented
408 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
409 req_len = min_packet_len;
411 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
412 min_packet_len, req_len);
415 batadv_hardif_free_ref(primary_if);
418 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
419 unsigned char **packet_buff,
420 int *packet_buff_len,
423 struct batadv_tt_change_node *entry, *safe;
424 int count = 0, tot_changes = 0, new_len;
425 unsigned char *tt_buff;
427 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
428 packet_buff_len, min_packet_len);
430 new_len = *packet_buff_len - min_packet_len;
431 tt_buff = *packet_buff + min_packet_len;
434 tot_changes = new_len / batadv_tt_len(1);
436 spin_lock_bh(&bat_priv->tt.changes_list_lock);
437 atomic_set(&bat_priv->tt.local_changes, 0);
439 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
441 if (count < tot_changes) {
442 memcpy(tt_buff + batadv_tt_len(count),
443 &entry->change, sizeof(struct batadv_tt_change));
446 list_del(&entry->list);
449 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
451 /* Keep the buffer for possible tt_request */
452 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
453 kfree(bat_priv->tt.last_changeset);
454 bat_priv->tt.last_changeset_len = 0;
455 bat_priv->tt.last_changeset = NULL;
456 /* check whether this new OGM has no changes due to size problems */
458 /* if kmalloc() fails we will reply with the full table
459 * instead of providing the diff
461 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
462 if (bat_priv->tt.last_changeset) {
463 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
464 bat_priv->tt.last_changeset_len = new_len;
467 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
472 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
474 struct net_device *net_dev = (struct net_device *)seq->private;
475 struct batadv_priv *bat_priv = netdev_priv(net_dev);
476 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
477 struct batadv_tt_common_entry *tt_common_entry;
478 struct batadv_tt_local_entry *tt_local;
479 struct batadv_hard_iface *primary_if;
480 struct hlist_node *node;
481 struct hlist_head *head;
485 unsigned long last_seen_jiffies;
487 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
489 primary_if = batadv_seq_print_text_primary_if_get(seq);
494 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
495 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
496 bat_priv->tt.local_crc);
497 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
500 for (i = 0; i < hash->size; i++) {
501 head = &hash->table[i];
504 hlist_for_each_entry_rcu(tt_common_entry, node,
506 tt_local = container_of(tt_common_entry,
507 struct batadv_tt_local_entry,
509 last_seen_jiffies = jiffies - tt_local->last_seen;
510 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
511 last_seen_secs = last_seen_msecs / 1000;
512 last_seen_msecs = last_seen_msecs % 1000;
514 no_purge = tt_common_entry->flags & np_flag;
516 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
517 tt_common_entry->addr,
518 (tt_common_entry->flags &
519 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
520 no_purge ? 'P' : '.',
521 (tt_common_entry->flags &
522 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
523 (tt_common_entry->flags &
524 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
525 (tt_common_entry->flags &
526 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
527 no_purge ? last_seen_secs : 0,
528 no_purge ? last_seen_msecs : 0);
534 batadv_hardif_free_ref(primary_if);
539 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
540 struct batadv_tt_local_entry *tt_local_entry,
541 uint16_t flags, const char *message)
543 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
544 tt_local_entry->common.flags | flags);
546 /* The local client has to be marked as "pending to be removed" but has
547 * to be kept in the table in order to send it in a full table
548 * response issued before the net ttvn increment (consistency check)
550 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
552 batadv_dbg(BATADV_DBG_TT, bat_priv,
553 "Local tt entry (%pM) pending to be removed: %s\n",
554 tt_local_entry->common.addr, message);
558 * batadv_tt_local_remove - logically remove an entry from the local table
559 * @bat_priv: the bat priv with all the soft interface information
560 * @addr: the MAC address of the client to remove
561 * @message: message to append to the log on deletion
562 * @roaming: true if the deletion is due to a roaming event
564 * Returns the flags assigned to the local entry before being deleted
566 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
567 const uint8_t *addr, const char *message,
570 struct batadv_tt_local_entry *tt_local_entry;
571 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
573 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
577 curr_flags = tt_local_entry->common.flags;
579 flags = BATADV_TT_CLIENT_DEL;
580 /* if this global entry addition is due to a roaming, the node has to
581 * mark the local entry as "roamed" in order to correctly reroute
585 flags |= BATADV_TT_CLIENT_ROAM;
586 /* mark the local client as ROAMed */
587 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
590 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
591 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
595 /* if this client has been added right now, it is possible to
596 * immediately purge it
598 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
599 curr_flags | BATADV_TT_CLIENT_DEL);
600 hlist_del_rcu(&tt_local_entry->common.hash_entry);
601 batadv_tt_local_entry_free_ref(tt_local_entry);
605 batadv_tt_local_entry_free_ref(tt_local_entry);
610 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
611 struct hlist_head *head)
613 struct batadv_tt_local_entry *tt_local_entry;
614 struct batadv_tt_common_entry *tt_common_entry;
615 struct hlist_node *node, *node_tmp;
617 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
619 tt_local_entry = container_of(tt_common_entry,
620 struct batadv_tt_local_entry,
622 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
625 /* entry already marked for deletion */
626 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
629 if (!batadv_has_timed_out(tt_local_entry->last_seen,
630 BATADV_TT_LOCAL_TIMEOUT))
633 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
634 BATADV_TT_CLIENT_DEL, "timed out");
638 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
640 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
641 struct hlist_head *head;
642 spinlock_t *list_lock; /* protects write access to the hash lists */
645 for (i = 0; i < hash->size; i++) {
646 head = &hash->table[i];
647 list_lock = &hash->list_locks[i];
649 spin_lock_bh(list_lock);
650 batadv_tt_local_purge_list(bat_priv, head);
651 spin_unlock_bh(list_lock);
655 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
657 struct batadv_hashtable *hash;
658 spinlock_t *list_lock; /* protects write access to the hash lists */
659 struct batadv_tt_common_entry *tt_common_entry;
660 struct batadv_tt_local_entry *tt_local;
661 struct hlist_node *node, *node_tmp;
662 struct hlist_head *head;
665 if (!bat_priv->tt.local_hash)
668 hash = bat_priv->tt.local_hash;
670 for (i = 0; i < hash->size; i++) {
671 head = &hash->table[i];
672 list_lock = &hash->list_locks[i];
674 spin_lock_bh(list_lock);
675 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
678 tt_local = container_of(tt_common_entry,
679 struct batadv_tt_local_entry,
681 batadv_tt_local_entry_free_ref(tt_local);
683 spin_unlock_bh(list_lock);
686 batadv_hash_destroy(hash);
688 bat_priv->tt.local_hash = NULL;
691 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
693 if (bat_priv->tt.global_hash)
696 bat_priv->tt.global_hash = batadv_hash_new(1024);
698 if (!bat_priv->tt.global_hash)
701 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
702 &batadv_tt_global_hash_lock_class_key);
707 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
709 struct batadv_tt_change_node *entry, *safe;
711 spin_lock_bh(&bat_priv->tt.changes_list_lock);
713 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
715 list_del(&entry->list);
719 atomic_set(&bat_priv->tt.local_changes, 0);
720 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
723 /* retrieves the orig_tt_list_entry belonging to orig_node from the
724 * batadv_tt_global_entry list
726 * returns it with an increased refcounter, NULL if not found
728 static struct batadv_tt_orig_list_entry *
729 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
730 const struct batadv_orig_node *orig_node)
732 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
733 const struct hlist_head *head;
734 struct hlist_node *node;
737 head = &entry->orig_list;
738 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
739 if (tmp_orig_entry->orig_node != orig_node)
741 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
744 orig_entry = tmp_orig_entry;
752 /* find out if an orig_node is already in the list of a tt_global_entry.
753 * returns true if found, false otherwise
756 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
757 const struct batadv_orig_node *orig_node)
759 struct batadv_tt_orig_list_entry *orig_entry;
762 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
765 batadv_tt_orig_list_entry_free_ref(orig_entry);
772 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
773 struct batadv_orig_node *orig_node, int ttvn)
775 struct batadv_tt_orig_list_entry *orig_entry;
777 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
779 /* refresh the ttvn: the current value could be a bogus one that
780 * was added during a "temporary client detection"
782 orig_entry->ttvn = ttvn;
786 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
790 INIT_HLIST_NODE(&orig_entry->list);
791 atomic_inc(&orig_node->refcount);
792 atomic_inc(&orig_node->tt_size);
793 orig_entry->orig_node = orig_node;
794 orig_entry->ttvn = ttvn;
795 atomic_set(&orig_entry->refcount, 2);
797 spin_lock_bh(&tt_global->list_lock);
798 hlist_add_head_rcu(&orig_entry->list,
799 &tt_global->orig_list);
800 spin_unlock_bh(&tt_global->list_lock);
803 batadv_tt_orig_list_entry_free_ref(orig_entry);
806 /* caller must hold orig_node refcount */
807 int batadv_tt_global_add(struct batadv_priv *bat_priv,
808 struct batadv_orig_node *orig_node,
809 const unsigned char *tt_addr, uint8_t flags,
812 struct batadv_tt_global_entry *tt_global_entry;
813 struct batadv_tt_local_entry *tt_local_entry;
816 struct batadv_tt_common_entry *common;
817 uint16_t local_flags;
819 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
820 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
822 /* if the node already has a local client for this entry, it has to wait
823 * for a roaming advertisement instead of manually messing up the global
826 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
827 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
830 if (!tt_global_entry) {
831 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
832 if (!tt_global_entry)
835 common = &tt_global_entry->common;
836 memcpy(common->addr, tt_addr, ETH_ALEN);
838 common->flags = flags;
839 tt_global_entry->roam_at = 0;
840 /* node must store current time in case of roaming. This is
841 * needed to purge this entry out on timeout (if nobody claims
844 if (flags & BATADV_TT_CLIENT_ROAM)
845 tt_global_entry->roam_at = jiffies;
846 atomic_set(&common->refcount, 2);
847 common->added_at = jiffies;
849 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
850 spin_lock_init(&tt_global_entry->list_lock);
852 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
854 batadv_choose_orig, common,
855 &common->hash_entry);
857 if (unlikely(hash_added != 0)) {
858 /* remove the reference for the hash */
859 batadv_tt_global_entry_free_ref(tt_global_entry);
863 common = &tt_global_entry->common;
864 /* If there is already a global entry, we can use this one for
866 * But if we are trying to add a temporary client then here are
867 * two options at this point:
868 * 1) the global client is not a temporary client: the global
869 * client has to be left as it is, temporary information
870 * should never override any already known client state
871 * 2) the global client is a temporary client: purge the
872 * originator list and add the new one orig_entry
874 if (flags & BATADV_TT_CLIENT_TEMP) {
875 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
877 if (batadv_tt_global_entry_has_orig(tt_global_entry,
880 batadv_tt_global_del_orig_list(tt_global_entry);
884 /* if the client was temporary added before receiving the first
885 * OGM announcing it, we have to clear the TEMP flag
887 common->flags &= ~BATADV_TT_CLIENT_TEMP;
889 /* the change can carry possible "attribute" flags like the
890 * TT_CLIENT_WIFI, therefore they have to be copied in the
893 tt_global_entry->common.flags |= flags;
895 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
896 * one originator left in the list and we previously received a
897 * delete + roaming change for this originator.
899 * We should first delete the old originator before adding the
902 if (common->flags & BATADV_TT_CLIENT_ROAM) {
903 batadv_tt_global_del_orig_list(tt_global_entry);
904 common->flags &= ~BATADV_TT_CLIENT_ROAM;
905 tt_global_entry->roam_at = 0;
909 /* add the new orig_entry (if needed) or update it */
910 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
912 batadv_dbg(BATADV_DBG_TT, bat_priv,
913 "Creating new global tt entry: %pM (via %pM)\n",
914 common->addr, orig_node->orig);
919 /* remove address from local hash if present */
920 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
921 "global tt received",
922 !!(flags & BATADV_TT_CLIENT_ROAM));
923 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
925 if (!(flags & BATADV_TT_CLIENT_ROAM))
926 /* this is a normal global add. Therefore the client is not in a
927 * roaming state anymore.
929 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
933 batadv_tt_global_entry_free_ref(tt_global_entry);
935 batadv_tt_local_entry_free_ref(tt_local_entry);
939 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
940 * @tt_global_entry: global translation table entry to be analyzed
942 * This functon assumes the caller holds rcu_read_lock().
943 * Returns best originator list entry or NULL on errors.
945 static struct batadv_tt_orig_list_entry *
946 batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
948 struct batadv_neigh_node *router = NULL;
949 struct hlist_head *head;
950 struct hlist_node *node;
951 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
954 head = &tt_global_entry->orig_list;
955 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
956 router = batadv_orig_node_get_router(orig_entry->orig_node);
960 if (router->tq_avg > best_tq) {
961 best_entry = orig_entry;
962 best_tq = router->tq_avg;
965 batadv_neigh_node_free_ref(router);
971 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
972 * for this global entry
973 * @tt_global_entry: global translation table entry to be printed
974 * @seq: debugfs table seq_file struct
976 * This functon assumes the caller holds rcu_read_lock().
979 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
980 struct seq_file *seq)
982 struct hlist_head *head;
983 struct hlist_node *node;
984 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
985 struct batadv_tt_common_entry *tt_common_entry;
989 tt_common_entry = &tt_global_entry->common;
990 flags = tt_common_entry->flags;
992 best_entry = batadv_transtable_best_orig(tt_global_entry);
994 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
996 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
997 '*', tt_global_entry->common.addr,
998 best_entry->ttvn, best_entry->orig_node->orig,
999 last_ttvn, best_entry->orig_node->tt_crc,
1000 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1001 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1002 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1005 head = &tt_global_entry->orig_list;
1007 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1008 if (best_entry == orig_entry)
1011 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1012 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
1013 '+', tt_global_entry->common.addr,
1014 orig_entry->ttvn, orig_entry->orig_node->orig,
1016 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1017 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1018 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1022 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1024 struct net_device *net_dev = (struct net_device *)seq->private;
1025 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1026 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1027 struct batadv_tt_common_entry *tt_common_entry;
1028 struct batadv_tt_global_entry *tt_global;
1029 struct batadv_hard_iface *primary_if;
1030 struct hlist_node *node;
1031 struct hlist_head *head;
1034 primary_if = batadv_seq_print_text_primary_if_get(seq);
1039 "Globally announced TT entries received via the mesh %s\n",
1041 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1042 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1045 for (i = 0; i < hash->size; i++) {
1046 head = &hash->table[i];
1049 hlist_for_each_entry_rcu(tt_common_entry, node,
1051 tt_global = container_of(tt_common_entry,
1052 struct batadv_tt_global_entry,
1054 batadv_tt_global_print_entry(tt_global, seq);
1060 batadv_hardif_free_ref(primary_if);
1064 /* deletes the orig list of a tt_global_entry */
1066 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1068 struct hlist_head *head;
1069 struct hlist_node *node, *safe;
1070 struct batadv_tt_orig_list_entry *orig_entry;
1072 spin_lock_bh(&tt_global_entry->list_lock);
1073 head = &tt_global_entry->orig_list;
1074 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1075 hlist_del_rcu(node);
1076 batadv_tt_orig_list_entry_free_ref(orig_entry);
1078 spin_unlock_bh(&tt_global_entry->list_lock);
1082 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1083 struct batadv_tt_global_entry *tt_global_entry,
1084 struct batadv_orig_node *orig_node,
1085 const char *message)
1087 struct hlist_head *head;
1088 struct hlist_node *node, *safe;
1089 struct batadv_tt_orig_list_entry *orig_entry;
1091 spin_lock_bh(&tt_global_entry->list_lock);
1092 head = &tt_global_entry->orig_list;
1093 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1094 if (orig_entry->orig_node == orig_node) {
1095 batadv_dbg(BATADV_DBG_TT, bat_priv,
1096 "Deleting %pM from global tt entry %pM: %s\n",
1098 tt_global_entry->common.addr, message);
1099 hlist_del_rcu(node);
1100 batadv_tt_orig_list_entry_free_ref(orig_entry);
1103 spin_unlock_bh(&tt_global_entry->list_lock);
1106 /* If the client is to be deleted, we check if it is the last origantor entry
1107 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1108 * timer, otherwise we simply remove the originator scheduled for deletion.
1111 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1112 struct batadv_tt_global_entry *tt_global_entry,
1113 struct batadv_orig_node *orig_node,
1114 const char *message)
1116 bool last_entry = true;
1117 struct hlist_head *head;
1118 struct hlist_node *node;
1119 struct batadv_tt_orig_list_entry *orig_entry;
1121 /* no local entry exists, case 1:
1122 * Check if this is the last one or if other entries exist.
1126 head = &tt_global_entry->orig_list;
1127 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1128 if (orig_entry->orig_node != orig_node) {
1136 /* its the last one, mark for roaming. */
1137 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1138 tt_global_entry->roam_at = jiffies;
1140 /* there is another entry, we can simply delete this
1141 * one and can still use the other one.
1143 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1144 orig_node, message);
1149 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1150 struct batadv_orig_node *orig_node,
1151 const unsigned char *addr,
1152 const char *message, bool roaming)
1154 struct batadv_tt_global_entry *tt_global_entry;
1155 struct batadv_tt_local_entry *local_entry = NULL;
1157 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1158 if (!tt_global_entry)
1162 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1163 orig_node, message);
1165 if (hlist_empty(&tt_global_entry->orig_list))
1166 batadv_tt_global_free(bat_priv, tt_global_entry,
1172 /* if we are deleting a global entry due to a roam
1173 * event, there are two possibilities:
1174 * 1) the client roamed from node A to node B => if there
1175 * is only one originator left for this client, we mark
1176 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1177 * wait for node B to claim it. In case of timeout
1178 * the entry is purged.
1180 * If there are other originators left, we directly delete
1182 * 2) the client roamed to us => we can directly delete
1183 * the global entry, since it is useless now.
1185 local_entry = batadv_tt_local_hash_find(bat_priv,
1186 tt_global_entry->common.addr);
1188 /* local entry exists, case 2: client roamed to us. */
1189 batadv_tt_global_del_orig_list(tt_global_entry);
1190 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1192 /* no local entry exists, case 1: check for roaming */
1193 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1194 orig_node, message);
1198 if (tt_global_entry)
1199 batadv_tt_global_entry_free_ref(tt_global_entry);
1201 batadv_tt_local_entry_free_ref(local_entry);
1204 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1205 struct batadv_orig_node *orig_node,
1206 const char *message)
1208 struct batadv_tt_global_entry *tt_global;
1209 struct batadv_tt_common_entry *tt_common_entry;
1211 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1212 struct hlist_node *node, *safe;
1213 struct hlist_head *head;
1214 spinlock_t *list_lock; /* protects write access to the hash lists */
1219 for (i = 0; i < hash->size; i++) {
1220 head = &hash->table[i];
1221 list_lock = &hash->list_locks[i];
1223 spin_lock_bh(list_lock);
1224 hlist_for_each_entry_safe(tt_common_entry, node, safe,
1226 tt_global = container_of(tt_common_entry,
1227 struct batadv_tt_global_entry,
1230 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1231 orig_node, message);
1233 if (hlist_empty(&tt_global->orig_list)) {
1234 batadv_dbg(BATADV_DBG_TT, bat_priv,
1235 "Deleting global tt entry %pM: %s\n",
1236 tt_global->common.addr, message);
1237 hlist_del_rcu(node);
1238 batadv_tt_global_entry_free_ref(tt_global);
1241 spin_unlock_bh(list_lock);
1243 orig_node->tt_initialised = false;
1246 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1250 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1251 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1253 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1254 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1256 *msg = "Roaming timeout\n";
1259 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1260 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1262 *msg = "Temporary client timeout\n";
1268 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1270 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1271 struct hlist_head *head;
1272 struct hlist_node *node, *node_tmp;
1273 spinlock_t *list_lock; /* protects write access to the hash lists */
1276 struct batadv_tt_common_entry *tt_common;
1277 struct batadv_tt_global_entry *tt_global;
1279 for (i = 0; i < hash->size; i++) {
1280 head = &hash->table[i];
1281 list_lock = &hash->list_locks[i];
1283 spin_lock_bh(list_lock);
1284 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1286 tt_global = container_of(tt_common,
1287 struct batadv_tt_global_entry,
1290 if (!batadv_tt_global_to_purge(tt_global, &msg))
1293 batadv_dbg(BATADV_DBG_TT, bat_priv,
1294 "Deleting global tt entry (%pM): %s\n",
1295 tt_global->common.addr, msg);
1297 hlist_del_rcu(node);
1299 batadv_tt_global_entry_free_ref(tt_global);
1301 spin_unlock_bh(list_lock);
1305 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1307 struct batadv_hashtable *hash;
1308 spinlock_t *list_lock; /* protects write access to the hash lists */
1309 struct batadv_tt_common_entry *tt_common_entry;
1310 struct batadv_tt_global_entry *tt_global;
1311 struct hlist_node *node, *node_tmp;
1312 struct hlist_head *head;
1315 if (!bat_priv->tt.global_hash)
1318 hash = bat_priv->tt.global_hash;
1320 for (i = 0; i < hash->size; i++) {
1321 head = &hash->table[i];
1322 list_lock = &hash->list_locks[i];
1324 spin_lock_bh(list_lock);
1325 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1327 hlist_del_rcu(node);
1328 tt_global = container_of(tt_common_entry,
1329 struct batadv_tt_global_entry,
1331 batadv_tt_global_entry_free_ref(tt_global);
1333 spin_unlock_bh(list_lock);
1336 batadv_hash_destroy(hash);
1338 bat_priv->tt.global_hash = NULL;
1342 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1343 struct batadv_tt_global_entry *tt_global_entry)
1347 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1348 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1354 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1356 const uint8_t *addr)
1358 struct batadv_tt_local_entry *tt_local_entry = NULL;
1359 struct batadv_tt_global_entry *tt_global_entry = NULL;
1360 struct batadv_orig_node *orig_node = NULL;
1361 struct batadv_tt_orig_list_entry *best_entry;
1363 if (src && atomic_read(&bat_priv->ap_isolation)) {
1364 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1365 if (!tt_local_entry ||
1366 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1370 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1371 if (!tt_global_entry)
1374 /* check whether the clients should not communicate due to AP
1377 if (tt_local_entry &&
1378 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1382 best_entry = batadv_transtable_best_orig(tt_global_entry);
1383 /* found anything? */
1385 orig_node = best_entry->orig_node;
1386 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1391 if (tt_global_entry)
1392 batadv_tt_global_entry_free_ref(tt_global_entry);
1394 batadv_tt_local_entry_free_ref(tt_local_entry);
1399 /* Calculates the checksum of the local table of a given orig_node */
1400 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1401 struct batadv_orig_node *orig_node)
1403 uint16_t total = 0, total_one;
1404 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1405 struct batadv_tt_common_entry *tt_common;
1406 struct batadv_tt_global_entry *tt_global;
1407 struct hlist_node *node;
1408 struct hlist_head *head;
1412 for (i = 0; i < hash->size; i++) {
1413 head = &hash->table[i];
1416 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1417 tt_global = container_of(tt_common,
1418 struct batadv_tt_global_entry,
1420 /* Roaming clients are in the global table for
1421 * consistency only. They don't have to be
1422 * taken into account while computing the
1425 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1427 /* Temporary clients have not been announced yet, so
1428 * they have to be skipped while computing the global
1431 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1434 /* find out if this global entry is announced by this
1437 if (!batadv_tt_global_entry_has_orig(tt_global,
1442 for (j = 0; j < ETH_ALEN; j++)
1443 total_one = crc16_byte(total_one,
1444 tt_common->addr[j]);
1453 /* Calculates the checksum of the local table */
1454 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1456 uint16_t total = 0, total_one;
1457 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1458 struct batadv_tt_common_entry *tt_common;
1459 struct hlist_node *node;
1460 struct hlist_head *head;
1464 for (i = 0; i < hash->size; i++) {
1465 head = &hash->table[i];
1468 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1469 /* not yet committed clients have not to be taken into
1470 * account while computing the CRC
1472 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1475 for (j = 0; j < ETH_ALEN; j++)
1476 total_one = crc16_byte(total_one,
1477 tt_common->addr[j]);
1486 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1488 struct batadv_tt_req_node *node, *safe;
1490 spin_lock_bh(&bat_priv->tt.req_list_lock);
1492 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1493 list_del(&node->list);
1497 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1500 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1501 struct batadv_orig_node *orig_node,
1502 const unsigned char *tt_buff,
1503 uint8_t tt_num_changes)
1505 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1507 /* Replace the old buffer only if I received something in the
1508 * last OGM (the OGM could carry no changes)
1510 spin_lock_bh(&orig_node->tt_buff_lock);
1511 if (tt_buff_len > 0) {
1512 kfree(orig_node->tt_buff);
1513 orig_node->tt_buff_len = 0;
1514 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1515 if (orig_node->tt_buff) {
1516 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1517 orig_node->tt_buff_len = tt_buff_len;
1520 spin_unlock_bh(&orig_node->tt_buff_lock);
1523 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1525 struct batadv_tt_req_node *node, *safe;
1527 spin_lock_bh(&bat_priv->tt.req_list_lock);
1528 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1529 if (batadv_has_timed_out(node->issued_at,
1530 BATADV_TT_REQUEST_TIMEOUT)) {
1531 list_del(&node->list);
1535 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1538 /* returns the pointer to the new tt_req_node struct if no request
1539 * has already been issued for this orig_node, NULL otherwise
1541 static struct batadv_tt_req_node *
1542 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1543 struct batadv_orig_node *orig_node)
1545 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1547 spin_lock_bh(&bat_priv->tt.req_list_lock);
1548 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1549 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1550 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1551 BATADV_TT_REQUEST_TIMEOUT))
1555 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1559 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1560 tt_req_node->issued_at = jiffies;
1562 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1564 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1568 /* data_ptr is useless here, but has to be kept to respect the prototype */
1569 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1570 const void *data_ptr)
1572 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1574 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1579 static int batadv_tt_global_valid(const void *entry_ptr,
1580 const void *data_ptr)
1582 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1583 const struct batadv_tt_global_entry *tt_global_entry;
1584 const struct batadv_orig_node *orig_node = data_ptr;
1586 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1587 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1590 tt_global_entry = container_of(tt_common_entry,
1591 struct batadv_tt_global_entry,
1594 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1597 static struct sk_buff *
1598 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1599 struct batadv_hashtable *hash,
1600 struct batadv_hard_iface *primary_if,
1601 int (*valid_cb)(const void *, const void *),
1604 struct batadv_tt_common_entry *tt_common_entry;
1605 struct batadv_tt_query_packet *tt_response;
1606 struct batadv_tt_change *tt_change;
1607 struct hlist_node *node;
1608 struct hlist_head *head;
1609 struct sk_buff *skb = NULL;
1610 uint16_t tt_tot, tt_count;
1611 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1615 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1616 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1617 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1619 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1621 len = tt_query_size + tt_len;
1622 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1626 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1627 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1628 tt_response->ttvn = ttvn;
1630 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1634 for (i = 0; i < hash->size; i++) {
1635 head = &hash->table[i];
1637 hlist_for_each_entry_rcu(tt_common_entry, node,
1639 if (tt_count == tt_tot)
1642 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1645 memcpy(tt_change->addr, tt_common_entry->addr,
1647 tt_change->flags = tt_common_entry->flags;
1655 /* store in the message the number of entries we have successfully
1658 tt_response->tt_data = htons(tt_count);
1664 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1665 struct batadv_orig_node *dst_orig_node,
1666 uint8_t ttvn, uint16_t tt_crc,
1669 struct sk_buff *skb = NULL;
1670 struct batadv_tt_query_packet *tt_request;
1671 struct batadv_hard_iface *primary_if;
1672 struct batadv_tt_req_node *tt_req_node = NULL;
1676 primary_if = batadv_primary_if_get_selected(bat_priv);
1680 /* The new tt_req will be issued only if I'm not waiting for a
1681 * reply from the same orig_node yet
1683 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1687 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1691 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1693 tt_req_len = sizeof(*tt_request);
1694 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1696 tt_request->header.packet_type = BATADV_TT_QUERY;
1697 tt_request->header.version = BATADV_COMPAT_VERSION;
1698 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1699 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1700 tt_request->header.ttl = BATADV_TTL;
1701 tt_request->ttvn = ttvn;
1702 tt_request->tt_data = htons(tt_crc);
1703 tt_request->flags = BATADV_TT_REQUEST;
1706 tt_request->flags |= BATADV_TT_FULL_TABLE;
1708 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1709 dst_orig_node->orig, (full_table ? 'F' : '.'));
1711 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1713 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1718 batadv_hardif_free_ref(primary_if);
1721 if (ret && tt_req_node) {
1722 spin_lock_bh(&bat_priv->tt.req_list_lock);
1723 list_del(&tt_req_node->list);
1724 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1731 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1732 struct batadv_tt_query_packet *tt_request)
1734 struct batadv_orig_node *req_dst_orig_node;
1735 struct batadv_orig_node *res_dst_orig_node = NULL;
1736 struct batadv_hard_iface *primary_if = NULL;
1737 uint8_t orig_ttvn, req_ttvn, ttvn;
1739 unsigned char *tt_buff;
1741 uint16_t tt_len, tt_tot;
1742 struct sk_buff *skb = NULL;
1743 struct batadv_tt_query_packet *tt_response;
1744 uint8_t *packet_pos;
1747 batadv_dbg(BATADV_DBG_TT, bat_priv,
1748 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1749 tt_request->src, tt_request->ttvn, tt_request->dst,
1750 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1752 /* Let's get the orig node of the REAL destination */
1753 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1754 if (!req_dst_orig_node)
1757 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1758 if (!res_dst_orig_node)
1761 primary_if = batadv_primary_if_get_selected(bat_priv);
1765 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1766 req_ttvn = tt_request->ttvn;
1768 /* I don't have the requested data */
1769 if (orig_ttvn != req_ttvn ||
1770 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1773 /* If the full table has been explicitly requested */
1774 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1775 !req_dst_orig_node->tt_buff)
1780 /* In this version, fragmentation is not implemented, then
1781 * I'll send only one packet with as much TT entries as I can
1784 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1785 tt_len = req_dst_orig_node->tt_buff_len;
1786 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1788 len = sizeof(*tt_response) + tt_len;
1789 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1793 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1794 packet_pos = skb_put(skb, len);
1795 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1796 tt_response->ttvn = req_ttvn;
1797 tt_response->tt_data = htons(tt_tot);
1799 tt_buff = skb->data + sizeof(*tt_response);
1800 /* Copy the last orig_node's OGM buffer */
1801 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1802 req_dst_orig_node->tt_buff_len);
1804 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1806 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1807 tt_len *= sizeof(struct batadv_tt_change);
1808 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1810 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1811 bat_priv->tt.global_hash,
1813 batadv_tt_global_valid,
1818 tt_response = (struct batadv_tt_query_packet *)skb->data;
1821 tt_response->header.packet_type = BATADV_TT_QUERY;
1822 tt_response->header.version = BATADV_COMPAT_VERSION;
1823 tt_response->header.ttl = BATADV_TTL;
1824 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1825 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1826 tt_response->flags = BATADV_TT_RESPONSE;
1829 tt_response->flags |= BATADV_TT_FULL_TABLE;
1831 batadv_dbg(BATADV_DBG_TT, bat_priv,
1832 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1833 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
1835 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1837 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1842 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1845 if (res_dst_orig_node)
1846 batadv_orig_node_free_ref(res_dst_orig_node);
1847 if (req_dst_orig_node)
1848 batadv_orig_node_free_ref(req_dst_orig_node);
1850 batadv_hardif_free_ref(primary_if);
1857 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1858 struct batadv_tt_query_packet *tt_request)
1860 struct batadv_orig_node *orig_node;
1861 struct batadv_hard_iface *primary_if = NULL;
1862 uint8_t my_ttvn, req_ttvn, ttvn;
1864 unsigned char *tt_buff;
1866 uint16_t tt_len, tt_tot;
1867 struct sk_buff *skb = NULL;
1868 struct batadv_tt_query_packet *tt_response;
1869 uint8_t *packet_pos;
1872 batadv_dbg(BATADV_DBG_TT, bat_priv,
1873 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1874 tt_request->src, tt_request->ttvn,
1875 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1878 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1879 req_ttvn = tt_request->ttvn;
1881 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1885 primary_if = batadv_primary_if_get_selected(bat_priv);
1889 /* If the full table has been explicitly requested or the gap
1890 * is too big send the whole local translation table
1892 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1893 !bat_priv->tt.last_changeset)
1898 /* In this version, fragmentation is not implemented, then
1899 * I'll send only one packet with as much TT entries as I can
1902 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1903 tt_len = bat_priv->tt.last_changeset_len;
1904 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1906 len = sizeof(*tt_response) + tt_len;
1907 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1911 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1912 packet_pos = skb_put(skb, len);
1913 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1914 tt_response->ttvn = req_ttvn;
1915 tt_response->tt_data = htons(tt_tot);
1917 tt_buff = skb->data + sizeof(*tt_response);
1918 memcpy(tt_buff, bat_priv->tt.last_changeset,
1919 bat_priv->tt.last_changeset_len);
1920 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1922 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1923 tt_len *= sizeof(struct batadv_tt_change);
1924 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1926 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1927 bat_priv->tt.local_hash,
1929 batadv_tt_local_valid_entry,
1934 tt_response = (struct batadv_tt_query_packet *)skb->data;
1937 tt_response->header.packet_type = BATADV_TT_QUERY;
1938 tt_response->header.version = BATADV_COMPAT_VERSION;
1939 tt_response->header.ttl = BATADV_TTL;
1940 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1941 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1942 tt_response->flags = BATADV_TT_RESPONSE;
1945 tt_response->flags |= BATADV_TT_FULL_TABLE;
1947 batadv_dbg(BATADV_DBG_TT, bat_priv,
1948 "Sending TT_RESPONSE to %pM [%c]\n",
1950 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1952 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1954 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1959 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1962 batadv_orig_node_free_ref(orig_node);
1964 batadv_hardif_free_ref(primary_if);
1967 /* This packet was for me, so it doesn't need to be re-routed */
1971 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1972 struct batadv_tt_query_packet *tt_request)
1974 if (batadv_is_my_mac(tt_request->dst)) {
1975 /* don't answer backbone gws! */
1976 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1979 return batadv_send_my_tt_response(bat_priv, tt_request);
1981 return batadv_send_other_tt_response(bat_priv, tt_request);
1985 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1986 struct batadv_orig_node *orig_node,
1987 struct batadv_tt_change *tt_change,
1988 uint16_t tt_num_changes, uint8_t ttvn)
1993 for (i = 0; i < tt_num_changes; i++) {
1994 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1995 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1996 batadv_tt_global_del(bat_priv, orig_node,
1997 (tt_change + i)->addr,
1998 "tt removed by changes",
2001 if (!batadv_tt_global_add(bat_priv, orig_node,
2002 (tt_change + i)->addr,
2003 (tt_change + i)->flags, ttvn))
2004 /* In case of problem while storing a
2005 * global_entry, we stop the updating
2006 * procedure without committing the
2007 * ttvn change. This will avoid to send
2008 * corrupted data on tt_request
2013 orig_node->tt_initialised = true;
2016 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2017 struct batadv_tt_query_packet *tt_response)
2019 struct batadv_orig_node *orig_node;
2021 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2025 /* Purge the old table first.. */
2026 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
2028 _batadv_tt_update_changes(bat_priv, orig_node,
2029 (struct batadv_tt_change *)(tt_response + 1),
2030 ntohs(tt_response->tt_data),
2033 spin_lock_bh(&orig_node->tt_buff_lock);
2034 kfree(orig_node->tt_buff);
2035 orig_node->tt_buff_len = 0;
2036 orig_node->tt_buff = NULL;
2037 spin_unlock_bh(&orig_node->tt_buff_lock);
2039 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2043 batadv_orig_node_free_ref(orig_node);
2046 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2047 struct batadv_orig_node *orig_node,
2048 uint16_t tt_num_changes, uint8_t ttvn,
2049 struct batadv_tt_change *tt_change)
2051 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2052 tt_num_changes, ttvn);
2054 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2055 (unsigned char *)tt_change, tt_num_changes);
2056 atomic_set(&orig_node->last_ttvn, ttvn);
2059 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2061 struct batadv_tt_local_entry *tt_local_entry;
2064 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2065 if (!tt_local_entry)
2067 /* Check if the client has been logically deleted (but is kept for
2068 * consistency purpose)
2070 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2071 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2076 batadv_tt_local_entry_free_ref(tt_local_entry);
2080 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2081 struct batadv_tt_query_packet *tt_response)
2083 struct batadv_tt_req_node *node, *safe;
2084 struct batadv_orig_node *orig_node = NULL;
2085 struct batadv_tt_change *tt_change;
2087 batadv_dbg(BATADV_DBG_TT, bat_priv,
2088 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2089 tt_response->src, tt_response->ttvn,
2090 ntohs(tt_response->tt_data),
2091 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2093 /* we should have never asked a backbone gw */
2094 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2097 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2101 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2102 batadv_tt_fill_gtable(bat_priv, tt_response);
2104 tt_change = (struct batadv_tt_change *)(tt_response + 1);
2105 batadv_tt_update_changes(bat_priv, orig_node,
2106 ntohs(tt_response->tt_data),
2107 tt_response->ttvn, tt_change);
2110 /* Delete the tt_req_node from pending tt_requests list */
2111 spin_lock_bh(&bat_priv->tt.req_list_lock);
2112 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2113 if (!batadv_compare_eth(node->addr, tt_response->src))
2115 list_del(&node->list);
2118 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2120 /* Recalculate the CRC for this orig_node and store it */
2121 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2124 batadv_orig_node_free_ref(orig_node);
2127 int batadv_tt_init(struct batadv_priv *bat_priv)
2131 ret = batadv_tt_local_init(bat_priv);
2135 ret = batadv_tt_global_init(bat_priv);
2139 batadv_tt_start_timer(bat_priv);
2144 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2146 struct batadv_tt_roam_node *node, *safe;
2148 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2150 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2151 list_del(&node->list);
2155 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2158 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2160 struct batadv_tt_roam_node *node, *safe;
2162 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2163 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2164 if (!batadv_has_timed_out(node->first_time,
2165 BATADV_ROAMING_MAX_TIME))
2168 list_del(&node->list);
2171 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2174 /* This function checks whether the client already reached the
2175 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2178 * returns true if the ROAMING_ADV can be sent, false otherwise
2180 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2183 struct batadv_tt_roam_node *tt_roam_node;
2186 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2187 /* The new tt_req will be issued only if I'm not waiting for a
2188 * reply from the same orig_node yet
2190 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2191 if (!batadv_compare_eth(tt_roam_node->addr, client))
2194 if (batadv_has_timed_out(tt_roam_node->first_time,
2195 BATADV_ROAMING_MAX_TIME))
2198 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2199 /* Sorry, you roamed too many times! */
2206 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2210 tt_roam_node->first_time = jiffies;
2211 atomic_set(&tt_roam_node->counter,
2212 BATADV_ROAMING_MAX_COUNT - 1);
2213 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2215 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2220 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2224 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2225 struct batadv_orig_node *orig_node)
2227 struct sk_buff *skb = NULL;
2228 struct batadv_roam_adv_packet *roam_adv_packet;
2230 struct batadv_hard_iface *primary_if;
2231 size_t len = sizeof(*roam_adv_packet);
2233 /* before going on we have to check whether the client has
2234 * already roamed to us too many times
2236 if (!batadv_tt_check_roam_count(bat_priv, client))
2239 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2243 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2245 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2247 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2248 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2249 roam_adv_packet->header.ttl = BATADV_TTL;
2250 roam_adv_packet->reserved = 0;
2251 primary_if = batadv_primary_if_get_selected(bat_priv);
2254 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2255 batadv_hardif_free_ref(primary_if);
2256 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2257 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2259 batadv_dbg(BATADV_DBG_TT, bat_priv,
2260 "Sending ROAMING_ADV to %pM (client %pM)\n",
2261 orig_node->orig, client);
2263 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2265 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2274 static void batadv_tt_purge(struct work_struct *work)
2276 struct delayed_work *delayed_work;
2277 struct batadv_priv_tt *priv_tt;
2278 struct batadv_priv *bat_priv;
2280 delayed_work = container_of(work, struct delayed_work, work);
2281 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2282 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2284 batadv_tt_local_purge(bat_priv);
2285 batadv_tt_global_purge(bat_priv);
2286 batadv_tt_req_purge(bat_priv);
2287 batadv_tt_roam_purge(bat_priv);
2289 batadv_tt_start_timer(bat_priv);
2292 void batadv_tt_free(struct batadv_priv *bat_priv)
2294 cancel_delayed_work_sync(&bat_priv->tt.work);
2296 batadv_tt_local_table_free(bat_priv);
2297 batadv_tt_global_table_free(bat_priv);
2298 batadv_tt_req_list_free(bat_priv);
2299 batadv_tt_changes_list_free(bat_priv);
2300 batadv_tt_roam_list_free(bat_priv);
2302 kfree(bat_priv->tt.last_changeset);
2305 /* This function will enable or disable the specified flags for all the entries
2306 * in the given hash table and returns the number of modified entries
2308 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2309 uint16_t flags, bool enable)
2312 uint16_t changed_num = 0;
2313 struct hlist_head *head;
2314 struct hlist_node *node;
2315 struct batadv_tt_common_entry *tt_common_entry;
2320 for (i = 0; i < hash->size; i++) {
2321 head = &hash->table[i];
2324 hlist_for_each_entry_rcu(tt_common_entry, node,
2327 if ((tt_common_entry->flags & flags) == flags)
2329 tt_common_entry->flags |= flags;
2331 if (!(tt_common_entry->flags & flags))
2333 tt_common_entry->flags &= ~flags;
2343 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2344 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2346 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2347 struct batadv_tt_common_entry *tt_common;
2348 struct batadv_tt_local_entry *tt_local;
2349 struct hlist_node *node, *node_tmp;
2350 struct hlist_head *head;
2351 spinlock_t *list_lock; /* protects write access to the hash lists */
2357 for (i = 0; i < hash->size; i++) {
2358 head = &hash->table[i];
2359 list_lock = &hash->list_locks[i];
2361 spin_lock_bh(list_lock);
2362 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2364 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2367 batadv_dbg(BATADV_DBG_TT, bat_priv,
2368 "Deleting local tt entry (%pM): pending\n",
2371 atomic_dec(&bat_priv->tt.local_entry_num);
2372 hlist_del_rcu(node);
2373 tt_local = container_of(tt_common,
2374 struct batadv_tt_local_entry,
2376 batadv_tt_local_entry_free_ref(tt_local);
2378 spin_unlock_bh(list_lock);
2382 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2383 unsigned char **packet_buff,
2384 int *packet_buff_len, int packet_min_len)
2386 uint16_t changed_num = 0;
2388 if (atomic_read(&bat_priv->tt.local_changes) < 1)
2391 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2392 BATADV_TT_CLIENT_NEW, false);
2394 /* all reset entries have to be counted as local entries */
2395 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2396 batadv_tt_local_purge_pending_clients(bat_priv);
2397 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2399 /* Increment the TTVN only once per OGM interval */
2400 atomic_inc(&bat_priv->tt.vn);
2401 batadv_dbg(BATADV_DBG_TT, bat_priv,
2402 "Local changes committed, updating to ttvn %u\n",
2403 (uint8_t)atomic_read(&bat_priv->tt.vn));
2405 /* reset the sending counter */
2406 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2408 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2409 packet_buff_len, packet_min_len);
2412 /* when calling this function (hard_iface == primary_if) has to be true */
2413 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2414 unsigned char **packet_buff, int *packet_buff_len,
2419 /* if at least one change happened */
2420 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2424 /* if the changes have been sent often enough */
2425 if ((tt_num_changes < 0) &&
2426 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2427 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2428 packet_min_len, packet_min_len);
2432 return tt_num_changes;
2435 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2438 struct batadv_tt_local_entry *tt_local_entry = NULL;
2439 struct batadv_tt_global_entry *tt_global_entry = NULL;
2442 if (!atomic_read(&bat_priv->ap_isolation))
2445 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2446 if (!tt_local_entry)
2449 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2450 if (!tt_global_entry)
2453 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2459 if (tt_global_entry)
2460 batadv_tt_global_entry_free_ref(tt_global_entry);
2462 batadv_tt_local_entry_free_ref(tt_local_entry);
2466 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2467 struct batadv_orig_node *orig_node,
2468 const unsigned char *tt_buff, uint8_t tt_num_changes,
2469 uint8_t ttvn, uint16_t tt_crc)
2471 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2472 bool full_table = true;
2473 struct batadv_tt_change *tt_change;
2475 /* don't care about a backbone gateways updates. */
2476 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2479 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2480 * increased by one -> we can apply the attached changes
2482 if ((!orig_node->tt_initialised && ttvn == 1) ||
2483 ttvn - orig_ttvn == 1) {
2484 /* the OGM could not contain the changes due to their size or
2485 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2487 * In this case send a tt request
2489 if (!tt_num_changes) {
2494 tt_change = (struct batadv_tt_change *)tt_buff;
2495 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2498 /* Even if we received the precomputed crc with the OGM, we
2499 * prefer to recompute it to spot any possible inconsistency
2500 * in the global table
2502 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2504 /* The ttvn alone is not enough to guarantee consistency
2505 * because a single value could represent different states
2506 * (due to the wrap around). Thus a node has to check whether
2507 * the resulting table (after applying the changes) is still
2508 * consistent or not. E.g. a node could disconnect while its
2509 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2510 * checking the CRC value is mandatory to detect the
2513 if (orig_node->tt_crc != tt_crc)
2516 /* if we missed more than one change or our tables are not
2517 * in sync anymore -> request fresh tt data
2519 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2520 orig_node->tt_crc != tt_crc) {
2522 batadv_dbg(BATADV_DBG_TT, bat_priv,
2523 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2524 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2525 orig_node->tt_crc, tt_num_changes);
2526 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2527 tt_crc, full_table);
2533 /* returns true whether we know that the client has moved from its old
2534 * originator to another one. This entry is kept is still kept for consistency
2537 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2540 struct batadv_tt_global_entry *tt_global_entry;
2543 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2544 if (!tt_global_entry)
2547 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2548 batadv_tt_global_entry_free_ref(tt_global_entry);
2554 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2555 * @bat_priv: the bat priv with all the soft interface information
2556 * @addr: the MAC address of the local client to query
2558 * Returns true if the local client is known to be roaming (it is not served by
2559 * this node anymore) or not. If yes, the client is still present in the table
2560 * to keep the latter consistent with the node TTVN
2562 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2565 struct batadv_tt_local_entry *tt_local_entry;
2568 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2569 if (!tt_local_entry)
2572 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2573 batadv_tt_local_entry_free_ref(tt_local_entry);
2578 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2579 struct batadv_orig_node *orig_node,
2580 const unsigned char *addr)
2584 /* if the originator is a backbone node (meaning it belongs to the same
2585 * LAN of this node) the temporary client must not be added because to
2586 * reach such destination the node must use the LAN instead of the mesh
2588 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2591 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2592 BATADV_TT_CLIENT_TEMP,
2593 atomic_read(&orig_node->last_ttvn)))
2596 batadv_dbg(BATADV_DBG_TT, bat_priv,
2597 "Added temporary global client (addr: %pM orig: %pM)\n",
2598 addr, orig_node->orig);