1 /* Copyright (C) 2007-2013 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 struct batadv_tt_common_entry *
56 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
58 struct hlist_head *head;
59 struct batadv_tt_common_entry *tt_common_entry;
60 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
66 index = batadv_choose_orig(data, hash->size);
67 head = &hash->table[index];
70 hlist_for_each_entry_rcu(tt_common_entry, head, hash_entry) {
71 if (!batadv_compare_eth(tt_common_entry, data))
74 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
77 tt_common_entry_tmp = tt_common_entry;
82 return tt_common_entry_tmp;
85 static struct batadv_tt_local_entry *
86 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
88 struct batadv_tt_common_entry *tt_common_entry;
89 struct batadv_tt_local_entry *tt_local_entry = NULL;
91 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
93 tt_local_entry = container_of(tt_common_entry,
94 struct batadv_tt_local_entry,
96 return tt_local_entry;
99 static struct batadv_tt_global_entry *
100 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
102 struct batadv_tt_common_entry *tt_common_entry;
103 struct batadv_tt_global_entry *tt_global_entry = NULL;
105 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
107 tt_global_entry = container_of(tt_common_entry,
108 struct batadv_tt_global_entry,
110 return tt_global_entry;
114 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
116 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
117 kfree_rcu(tt_local_entry, common.rcu);
120 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
122 struct batadv_tt_common_entry *tt_common_entry;
123 struct batadv_tt_global_entry *tt_global_entry;
125 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
126 tt_global_entry = container_of(tt_common_entry,
127 struct batadv_tt_global_entry, common);
129 kfree(tt_global_entry);
133 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
135 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
136 batadv_tt_global_del_orig_list(tt_global_entry);
137 call_rcu(&tt_global_entry->common.rcu,
138 batadv_tt_global_entry_free_rcu);
142 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
144 struct batadv_tt_orig_list_entry *orig_entry;
146 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
147 batadv_orig_node_free_ref(orig_entry->orig_node);
152 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
154 if (!atomic_dec_and_test(&orig_entry->refcount))
156 /* to avoid race conditions, immediately decrease the tt counter */
157 atomic_dec(&orig_entry->orig_node->tt_size);
158 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
161 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
162 const uint8_t *addr, uint8_t flags)
164 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
165 bool event_removed = false;
166 bool del_op_requested, del_op_entry;
168 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
173 tt_change_node->change.flags = flags;
174 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
176 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
178 /* check for ADD+DEL or DEL+ADD events */
179 spin_lock_bh(&bat_priv->tt.changes_list_lock);
180 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
182 if (!batadv_compare_eth(entry->change.addr, addr))
185 /* DEL+ADD in the same orig interval have no effect and can be
186 * removed to avoid silly behaviour on the receiver side. The
187 * other way around (ADD+DEL) can happen in case of roaming of
188 * a client still in the NEW state. Roaming of NEW clients is
189 * now possible due to automatically recognition of "temporary"
192 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
193 if (!del_op_requested && del_op_entry)
195 if (del_op_requested && !del_op_entry)
199 list_del(&entry->list);
201 kfree(tt_change_node);
202 event_removed = true;
206 /* track the change in the OGMinterval list */
207 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
210 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
213 atomic_dec(&bat_priv->tt.local_changes);
215 atomic_inc(&bat_priv->tt.local_changes);
218 int batadv_tt_len(int changes_num)
220 return changes_num * sizeof(struct batadv_tt_change);
223 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
225 if (bat_priv->tt.local_hash)
228 bat_priv->tt.local_hash = batadv_hash_new(1024);
230 if (!bat_priv->tt.local_hash)
233 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
234 &batadv_tt_local_hash_lock_class_key);
239 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
240 struct batadv_tt_global_entry *tt_global,
243 batadv_dbg(BATADV_DBG_TT, bat_priv,
244 "Deleting global tt entry %pM: %s\n",
245 tt_global->common.addr, message);
247 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
248 batadv_choose_orig, tt_global->common.addr);
249 batadv_tt_global_entry_free_ref(tt_global);
252 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
255 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
256 struct batadv_tt_local_entry *tt_local;
257 struct batadv_tt_global_entry *tt_global;
258 struct hlist_head *head;
259 struct batadv_tt_orig_list_entry *orig_entry;
261 bool roamed_back = false;
263 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
264 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
267 tt_local->last_seen = jiffies;
268 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
269 batadv_dbg(BATADV_DBG_TT, bat_priv,
270 "Re-adding pending client %pM\n", addr);
271 /* whatever the reason why the PENDING flag was set,
272 * this is a client which was enqueued to be removed in
273 * this orig_interval. Since it popped up again, the
274 * flag can be reset like it was never enqueued
276 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
280 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
281 batadv_dbg(BATADV_DBG_TT, bat_priv,
282 "Roaming client %pM came back to its original location\n",
284 /* the ROAM flag is set because this client roamed away
285 * and the node got a roaming_advertisement message. Now
286 * that the client popped up again at its original
287 * location such flag can be unset
289 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
295 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
299 batadv_dbg(BATADV_DBG_TT, bat_priv,
300 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
301 (uint8_t)atomic_read(&bat_priv->tt.vn));
303 memcpy(tt_local->common.addr, addr, ETH_ALEN);
304 /* The local entry has to be marked as NEW to avoid to send it in
305 * a full table response going out before the next ttvn increment
306 * (consistency check)
308 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
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 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
320 batadv_choose_orig, &tt_local->common,
321 &tt_local->common.hash_entry);
323 if (unlikely(hash_added != 0)) {
324 /* remove the reference for the hash */
325 batadv_tt_local_entry_free_ref(tt_local);
330 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
333 /* Check whether it is a roaming, but don't do anything if the roaming
334 * process has already been handled
336 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
337 /* These node are probably going to update their tt table */
338 head = &tt_global->orig_list;
340 hlist_for_each_entry_rcu(orig_entry, head, list) {
341 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
342 orig_entry->orig_node);
346 batadv_tt_global_free(bat_priv, tt_global,
350 /* The global entry has to be marked as ROAMING and
351 * has to be kept for consistency purpose
353 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
354 tt_global->roam_at = jiffies;
360 batadv_tt_local_entry_free_ref(tt_local);
362 batadv_tt_global_entry_free_ref(tt_global);
365 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
366 int *packet_buff_len,
370 unsigned char *new_buff;
372 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
374 /* keep old buffer if kmalloc should fail */
376 memcpy(new_buff, *packet_buff, min_packet_len);
378 *packet_buff = new_buff;
379 *packet_buff_len = new_packet_len;
383 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
384 unsigned char **packet_buff,
385 int *packet_buff_len,
390 req_len = min_packet_len;
391 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
393 /* if we have too many changes for one packet don't send any
394 * and wait for the tt table request which will be fragmented
396 if (req_len > bat_priv->soft_iface->mtu)
397 req_len = min_packet_len;
399 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
400 min_packet_len, req_len);
403 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
404 unsigned char **packet_buff,
405 int *packet_buff_len,
408 struct batadv_tt_change_node *entry, *safe;
409 int count = 0, tot_changes = 0, new_len;
410 unsigned char *tt_buff;
412 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
413 packet_buff_len, min_packet_len);
415 new_len = *packet_buff_len - min_packet_len;
416 tt_buff = *packet_buff + min_packet_len;
419 tot_changes = new_len / batadv_tt_len(1);
421 spin_lock_bh(&bat_priv->tt.changes_list_lock);
422 atomic_set(&bat_priv->tt.local_changes, 0);
424 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
426 if (count < tot_changes) {
427 memcpy(tt_buff + batadv_tt_len(count),
428 &entry->change, sizeof(struct batadv_tt_change));
431 list_del(&entry->list);
434 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
436 /* Keep the buffer for possible tt_request */
437 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
438 kfree(bat_priv->tt.last_changeset);
439 bat_priv->tt.last_changeset_len = 0;
440 bat_priv->tt.last_changeset = NULL;
441 /* check whether this new OGM has no changes due to size problems */
443 /* if kmalloc() fails we will reply with the full table
444 * instead of providing the diff
446 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
447 if (bat_priv->tt.last_changeset) {
448 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
449 bat_priv->tt.last_changeset_len = new_len;
452 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
457 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
459 struct net_device *net_dev = (struct net_device *)seq->private;
460 struct batadv_priv *bat_priv = netdev_priv(net_dev);
461 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
462 struct batadv_tt_common_entry *tt_common_entry;
463 struct batadv_tt_local_entry *tt_local;
464 struct batadv_hard_iface *primary_if;
465 struct hlist_head *head;
469 unsigned long last_seen_jiffies;
471 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
473 primary_if = batadv_seq_print_text_primary_if_get(seq);
478 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
479 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
480 bat_priv->tt.local_crc);
481 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
484 for (i = 0; i < hash->size; i++) {
485 head = &hash->table[i];
488 hlist_for_each_entry_rcu(tt_common_entry,
490 tt_local = container_of(tt_common_entry,
491 struct batadv_tt_local_entry,
493 last_seen_jiffies = jiffies - tt_local->last_seen;
494 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
495 last_seen_secs = last_seen_msecs / 1000;
496 last_seen_msecs = last_seen_msecs % 1000;
498 no_purge = tt_common_entry->flags & np_flag;
500 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
501 tt_common_entry->addr,
502 (tt_common_entry->flags &
503 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
504 no_purge ? 'P' : '.',
505 (tt_common_entry->flags &
506 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
507 (tt_common_entry->flags &
508 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
509 (tt_common_entry->flags &
510 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
511 no_purge ? 0 : last_seen_secs,
512 no_purge ? 0 : last_seen_msecs);
518 batadv_hardif_free_ref(primary_if);
523 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
524 struct batadv_tt_local_entry *tt_local_entry,
525 uint16_t flags, const char *message)
527 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
528 tt_local_entry->common.flags | flags);
530 /* The local client has to be marked as "pending to be removed" but has
531 * to be kept in the table in order to send it in a full table
532 * response issued before the net ttvn increment (consistency check)
534 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
536 batadv_dbg(BATADV_DBG_TT, bat_priv,
537 "Local tt entry (%pM) pending to be removed: %s\n",
538 tt_local_entry->common.addr, message);
542 * batadv_tt_local_remove - logically remove an entry from the local table
543 * @bat_priv: the bat priv with all the soft interface information
544 * @addr: the MAC address of the client to remove
545 * @message: message to append to the log on deletion
546 * @roaming: true if the deletion is due to a roaming event
548 * Returns the flags assigned to the local entry before being deleted
550 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
551 const uint8_t *addr, const char *message,
554 struct batadv_tt_local_entry *tt_local_entry;
555 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
557 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
561 curr_flags = tt_local_entry->common.flags;
563 flags = BATADV_TT_CLIENT_DEL;
564 /* if this global entry addition is due to a roaming, the node has to
565 * mark the local entry as "roamed" in order to correctly reroute
569 flags |= BATADV_TT_CLIENT_ROAM;
570 /* mark the local client as ROAMed */
571 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
574 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
575 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
579 /* if this client has been added right now, it is possible to
580 * immediately purge it
582 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
583 curr_flags | BATADV_TT_CLIENT_DEL);
584 hlist_del_rcu(&tt_local_entry->common.hash_entry);
585 batadv_tt_local_entry_free_ref(tt_local_entry);
589 batadv_tt_local_entry_free_ref(tt_local_entry);
594 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
595 struct hlist_head *head)
597 struct batadv_tt_local_entry *tt_local_entry;
598 struct batadv_tt_common_entry *tt_common_entry;
599 struct hlist_node *node_tmp;
601 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
603 tt_local_entry = container_of(tt_common_entry,
604 struct batadv_tt_local_entry,
606 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
609 /* entry already marked for deletion */
610 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
613 if (!batadv_has_timed_out(tt_local_entry->last_seen,
614 BATADV_TT_LOCAL_TIMEOUT))
617 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
618 BATADV_TT_CLIENT_DEL, "timed out");
622 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
624 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
625 struct hlist_head *head;
626 spinlock_t *list_lock; /* protects write access to the hash lists */
629 for (i = 0; i < hash->size; i++) {
630 head = &hash->table[i];
631 list_lock = &hash->list_locks[i];
633 spin_lock_bh(list_lock);
634 batadv_tt_local_purge_list(bat_priv, head);
635 spin_unlock_bh(list_lock);
639 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
641 struct batadv_hashtable *hash;
642 spinlock_t *list_lock; /* protects write access to the hash lists */
643 struct batadv_tt_common_entry *tt_common_entry;
644 struct batadv_tt_local_entry *tt_local;
645 struct hlist_node *node_tmp;
646 struct hlist_head *head;
649 if (!bat_priv->tt.local_hash)
652 hash = bat_priv->tt.local_hash;
654 for (i = 0; i < hash->size; i++) {
655 head = &hash->table[i];
656 list_lock = &hash->list_locks[i];
658 spin_lock_bh(list_lock);
659 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
661 hlist_del_rcu(&tt_common_entry->hash_entry);
662 tt_local = container_of(tt_common_entry,
663 struct batadv_tt_local_entry,
665 batadv_tt_local_entry_free_ref(tt_local);
667 spin_unlock_bh(list_lock);
670 batadv_hash_destroy(hash);
672 bat_priv->tt.local_hash = NULL;
675 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
677 if (bat_priv->tt.global_hash)
680 bat_priv->tt.global_hash = batadv_hash_new(1024);
682 if (!bat_priv->tt.global_hash)
685 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
686 &batadv_tt_global_hash_lock_class_key);
691 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
693 struct batadv_tt_change_node *entry, *safe;
695 spin_lock_bh(&bat_priv->tt.changes_list_lock);
697 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
699 list_del(&entry->list);
703 atomic_set(&bat_priv->tt.local_changes, 0);
704 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
707 /* retrieves the orig_tt_list_entry belonging to orig_node from the
708 * batadv_tt_global_entry list
710 * returns it with an increased refcounter, NULL if not found
712 static struct batadv_tt_orig_list_entry *
713 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
714 const struct batadv_orig_node *orig_node)
716 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
717 const struct hlist_head *head;
720 head = &entry->orig_list;
721 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
722 if (tmp_orig_entry->orig_node != orig_node)
724 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
727 orig_entry = tmp_orig_entry;
735 /* find out if an orig_node is already in the list of a tt_global_entry.
736 * returns true if found, false otherwise
739 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
740 const struct batadv_orig_node *orig_node)
742 struct batadv_tt_orig_list_entry *orig_entry;
745 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
748 batadv_tt_orig_list_entry_free_ref(orig_entry);
755 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
756 struct batadv_orig_node *orig_node, int ttvn)
758 struct batadv_tt_orig_list_entry *orig_entry;
760 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
762 /* refresh the ttvn: the current value could be a bogus one that
763 * was added during a "temporary client detection"
765 orig_entry->ttvn = ttvn;
769 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
773 INIT_HLIST_NODE(&orig_entry->list);
774 atomic_inc(&orig_node->refcount);
775 atomic_inc(&orig_node->tt_size);
776 orig_entry->orig_node = orig_node;
777 orig_entry->ttvn = ttvn;
778 atomic_set(&orig_entry->refcount, 2);
780 spin_lock_bh(&tt_global->list_lock);
781 hlist_add_head_rcu(&orig_entry->list,
782 &tt_global->orig_list);
783 spin_unlock_bh(&tt_global->list_lock);
786 batadv_tt_orig_list_entry_free_ref(orig_entry);
789 /* caller must hold orig_node refcount */
790 int batadv_tt_global_add(struct batadv_priv *bat_priv,
791 struct batadv_orig_node *orig_node,
792 const unsigned char *tt_addr, uint8_t flags,
795 struct batadv_tt_global_entry *tt_global_entry;
796 struct batadv_tt_local_entry *tt_local_entry;
799 struct batadv_tt_common_entry *common;
800 uint16_t local_flags;
802 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
803 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
805 /* if the node already has a local client for this entry, it has to wait
806 * for a roaming advertisement instead of manually messing up the global
809 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
810 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
813 if (!tt_global_entry) {
814 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
815 if (!tt_global_entry)
818 common = &tt_global_entry->common;
819 memcpy(common->addr, tt_addr, ETH_ALEN);
821 common->flags = flags;
822 tt_global_entry->roam_at = 0;
823 /* node must store current time in case of roaming. This is
824 * needed to purge this entry out on timeout (if nobody claims
827 if (flags & BATADV_TT_CLIENT_ROAM)
828 tt_global_entry->roam_at = jiffies;
829 atomic_set(&common->refcount, 2);
830 common->added_at = jiffies;
832 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
833 spin_lock_init(&tt_global_entry->list_lock);
835 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
837 batadv_choose_orig, common,
838 &common->hash_entry);
840 if (unlikely(hash_added != 0)) {
841 /* remove the reference for the hash */
842 batadv_tt_global_entry_free_ref(tt_global_entry);
846 common = &tt_global_entry->common;
847 /* If there is already a global entry, we can use this one for
849 * But if we are trying to add a temporary client then here are
850 * two options at this point:
851 * 1) the global client is not a temporary client: the global
852 * client has to be left as it is, temporary information
853 * should never override any already known client state
854 * 2) the global client is a temporary client: purge the
855 * originator list and add the new one orig_entry
857 if (flags & BATADV_TT_CLIENT_TEMP) {
858 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
860 if (batadv_tt_global_entry_has_orig(tt_global_entry,
863 batadv_tt_global_del_orig_list(tt_global_entry);
867 /* if the client was temporary added before receiving the first
868 * OGM announcing it, we have to clear the TEMP flag
870 common->flags &= ~BATADV_TT_CLIENT_TEMP;
872 /* the change can carry possible "attribute" flags like the
873 * TT_CLIENT_WIFI, therefore they have to be copied in the
876 tt_global_entry->common.flags |= flags;
878 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
879 * one originator left in the list and we previously received a
880 * delete + roaming change for this originator.
882 * We should first delete the old originator before adding the
885 if (common->flags & BATADV_TT_CLIENT_ROAM) {
886 batadv_tt_global_del_orig_list(tt_global_entry);
887 common->flags &= ~BATADV_TT_CLIENT_ROAM;
888 tt_global_entry->roam_at = 0;
892 /* add the new orig_entry (if needed) or update it */
893 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
895 batadv_dbg(BATADV_DBG_TT, bat_priv,
896 "Creating new global tt entry: %pM (via %pM)\n",
897 common->addr, orig_node->orig);
902 /* remove address from local hash if present */
903 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
904 "global tt received",
905 flags & BATADV_TT_CLIENT_ROAM);
906 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
908 if (!(flags & BATADV_TT_CLIENT_ROAM))
909 /* this is a normal global add. Therefore the client is not in a
910 * roaming state anymore.
912 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
916 batadv_tt_global_entry_free_ref(tt_global_entry);
918 batadv_tt_local_entry_free_ref(tt_local_entry);
922 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
923 * @tt_global_entry: global translation table entry to be analyzed
925 * This functon assumes the caller holds rcu_read_lock().
926 * Returns best originator list entry or NULL on errors.
928 static struct batadv_tt_orig_list_entry *
929 batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
931 struct batadv_neigh_node *router = NULL;
932 struct hlist_head *head;
933 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
936 head = &tt_global_entry->orig_list;
937 hlist_for_each_entry_rcu(orig_entry, head, list) {
938 router = batadv_orig_node_get_router(orig_entry->orig_node);
942 if (router->tq_avg > best_tq) {
943 best_entry = orig_entry;
944 best_tq = router->tq_avg;
947 batadv_neigh_node_free_ref(router);
953 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
954 * for this global entry
955 * @tt_global_entry: global translation table entry to be printed
956 * @seq: debugfs table seq_file struct
958 * This functon assumes the caller holds rcu_read_lock().
961 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
962 struct seq_file *seq)
964 struct hlist_head *head;
965 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
966 struct batadv_tt_common_entry *tt_common_entry;
970 tt_common_entry = &tt_global_entry->common;
971 flags = tt_common_entry->flags;
973 best_entry = batadv_transtable_best_orig(tt_global_entry);
975 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
977 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n",
978 '*', tt_global_entry->common.addr,
979 best_entry->ttvn, best_entry->orig_node->orig,
980 last_ttvn, best_entry->orig_node->tt_crc,
981 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
982 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
983 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
986 head = &tt_global_entry->orig_list;
988 hlist_for_each_entry_rcu(orig_entry, head, list) {
989 if (best_entry == orig_entry)
992 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
993 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
994 '+', tt_global_entry->common.addr,
995 orig_entry->ttvn, orig_entry->orig_node->orig,
997 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
998 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
999 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1003 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1005 struct net_device *net_dev = (struct net_device *)seq->private;
1006 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1007 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1008 struct batadv_tt_common_entry *tt_common_entry;
1009 struct batadv_tt_global_entry *tt_global;
1010 struct batadv_hard_iface *primary_if;
1011 struct hlist_head *head;
1014 primary_if = batadv_seq_print_text_primary_if_get(seq);
1019 "Globally announced TT entries received via the mesh %s\n",
1021 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1022 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1025 for (i = 0; i < hash->size; i++) {
1026 head = &hash->table[i];
1029 hlist_for_each_entry_rcu(tt_common_entry,
1031 tt_global = container_of(tt_common_entry,
1032 struct batadv_tt_global_entry,
1034 batadv_tt_global_print_entry(tt_global, seq);
1040 batadv_hardif_free_ref(primary_if);
1044 /* deletes the orig list of a tt_global_entry */
1046 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1048 struct hlist_head *head;
1049 struct hlist_node *safe;
1050 struct batadv_tt_orig_list_entry *orig_entry;
1052 spin_lock_bh(&tt_global_entry->list_lock);
1053 head = &tt_global_entry->orig_list;
1054 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1055 hlist_del_rcu(&orig_entry->list);
1056 batadv_tt_orig_list_entry_free_ref(orig_entry);
1058 spin_unlock_bh(&tt_global_entry->list_lock);
1062 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1063 struct batadv_tt_global_entry *tt_global_entry,
1064 struct batadv_orig_node *orig_node,
1065 const char *message)
1067 struct hlist_head *head;
1068 struct hlist_node *safe;
1069 struct batadv_tt_orig_list_entry *orig_entry;
1071 spin_lock_bh(&tt_global_entry->list_lock);
1072 head = &tt_global_entry->orig_list;
1073 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1074 if (orig_entry->orig_node == orig_node) {
1075 batadv_dbg(BATADV_DBG_TT, bat_priv,
1076 "Deleting %pM from global tt entry %pM: %s\n",
1078 tt_global_entry->common.addr, message);
1079 hlist_del_rcu(&orig_entry->list);
1080 batadv_tt_orig_list_entry_free_ref(orig_entry);
1083 spin_unlock_bh(&tt_global_entry->list_lock);
1086 /* If the client is to be deleted, we check if it is the last origantor entry
1087 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1088 * timer, otherwise we simply remove the originator scheduled for deletion.
1091 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1092 struct batadv_tt_global_entry *tt_global_entry,
1093 struct batadv_orig_node *orig_node,
1094 const char *message)
1096 bool last_entry = true;
1097 struct hlist_head *head;
1098 struct batadv_tt_orig_list_entry *orig_entry;
1100 /* no local entry exists, case 1:
1101 * Check if this is the last one or if other entries exist.
1105 head = &tt_global_entry->orig_list;
1106 hlist_for_each_entry_rcu(orig_entry, head, list) {
1107 if (orig_entry->orig_node != orig_node) {
1115 /* its the last one, mark for roaming. */
1116 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1117 tt_global_entry->roam_at = jiffies;
1119 /* there is another entry, we can simply delete this
1120 * one and can still use the other one.
1122 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1123 orig_node, message);
1128 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1129 struct batadv_orig_node *orig_node,
1130 const unsigned char *addr,
1131 const char *message, bool roaming)
1133 struct batadv_tt_global_entry *tt_global_entry;
1134 struct batadv_tt_local_entry *local_entry = NULL;
1136 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1137 if (!tt_global_entry)
1141 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1142 orig_node, message);
1144 if (hlist_empty(&tt_global_entry->orig_list))
1145 batadv_tt_global_free(bat_priv, tt_global_entry,
1151 /* if we are deleting a global entry due to a roam
1152 * event, there are two possibilities:
1153 * 1) the client roamed from node A to node B => if there
1154 * is only one originator left for this client, we mark
1155 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1156 * wait for node B to claim it. In case of timeout
1157 * the entry is purged.
1159 * If there are other originators left, we directly delete
1161 * 2) the client roamed to us => we can directly delete
1162 * the global entry, since it is useless now.
1164 local_entry = batadv_tt_local_hash_find(bat_priv,
1165 tt_global_entry->common.addr);
1167 /* local entry exists, case 2: client roamed to us. */
1168 batadv_tt_global_del_orig_list(tt_global_entry);
1169 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1171 /* no local entry exists, case 1: check for roaming */
1172 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1173 orig_node, message);
1177 if (tt_global_entry)
1178 batadv_tt_global_entry_free_ref(tt_global_entry);
1180 batadv_tt_local_entry_free_ref(local_entry);
1183 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1184 struct batadv_orig_node *orig_node,
1185 const char *message)
1187 struct batadv_tt_global_entry *tt_global;
1188 struct batadv_tt_common_entry *tt_common_entry;
1190 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1191 struct hlist_node *safe;
1192 struct hlist_head *head;
1193 spinlock_t *list_lock; /* protects write access to the hash lists */
1198 for (i = 0; i < hash->size; i++) {
1199 head = &hash->table[i];
1200 list_lock = &hash->list_locks[i];
1202 spin_lock_bh(list_lock);
1203 hlist_for_each_entry_safe(tt_common_entry, safe,
1205 tt_global = container_of(tt_common_entry,
1206 struct batadv_tt_global_entry,
1209 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1210 orig_node, message);
1212 if (hlist_empty(&tt_global->orig_list)) {
1213 batadv_dbg(BATADV_DBG_TT, bat_priv,
1214 "Deleting global tt entry %pM: %s\n",
1215 tt_global->common.addr, message);
1216 hlist_del_rcu(&tt_common_entry->hash_entry);
1217 batadv_tt_global_entry_free_ref(tt_global);
1220 spin_unlock_bh(list_lock);
1222 orig_node->tt_initialised = false;
1225 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1229 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1230 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1232 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1233 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1235 *msg = "Roaming timeout\n";
1238 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1239 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1241 *msg = "Temporary client timeout\n";
1247 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1249 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1250 struct hlist_head *head;
1251 struct hlist_node *node_tmp;
1252 spinlock_t *list_lock; /* protects write access to the hash lists */
1255 struct batadv_tt_common_entry *tt_common;
1256 struct batadv_tt_global_entry *tt_global;
1258 for (i = 0; i < hash->size; i++) {
1259 head = &hash->table[i];
1260 list_lock = &hash->list_locks[i];
1262 spin_lock_bh(list_lock);
1263 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1265 tt_global = container_of(tt_common,
1266 struct batadv_tt_global_entry,
1269 if (!batadv_tt_global_to_purge(tt_global, &msg))
1272 batadv_dbg(BATADV_DBG_TT, bat_priv,
1273 "Deleting global tt entry (%pM): %s\n",
1274 tt_global->common.addr, msg);
1276 hlist_del_rcu(&tt_common->hash_entry);
1278 batadv_tt_global_entry_free_ref(tt_global);
1280 spin_unlock_bh(list_lock);
1284 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1286 struct batadv_hashtable *hash;
1287 spinlock_t *list_lock; /* protects write access to the hash lists */
1288 struct batadv_tt_common_entry *tt_common_entry;
1289 struct batadv_tt_global_entry *tt_global;
1290 struct hlist_node *node_tmp;
1291 struct hlist_head *head;
1294 if (!bat_priv->tt.global_hash)
1297 hash = bat_priv->tt.global_hash;
1299 for (i = 0; i < hash->size; i++) {
1300 head = &hash->table[i];
1301 list_lock = &hash->list_locks[i];
1303 spin_lock_bh(list_lock);
1304 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1306 hlist_del_rcu(&tt_common_entry->hash_entry);
1307 tt_global = container_of(tt_common_entry,
1308 struct batadv_tt_global_entry,
1310 batadv_tt_global_entry_free_ref(tt_global);
1312 spin_unlock_bh(list_lock);
1315 batadv_hash_destroy(hash);
1317 bat_priv->tt.global_hash = NULL;
1321 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1322 struct batadv_tt_global_entry *tt_global_entry)
1326 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1327 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1333 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1335 const uint8_t *addr)
1337 struct batadv_tt_local_entry *tt_local_entry = NULL;
1338 struct batadv_tt_global_entry *tt_global_entry = NULL;
1339 struct batadv_orig_node *orig_node = NULL;
1340 struct batadv_tt_orig_list_entry *best_entry;
1342 if (src && atomic_read(&bat_priv->ap_isolation)) {
1343 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1344 if (!tt_local_entry ||
1345 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1349 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1350 if (!tt_global_entry)
1353 /* check whether the clients should not communicate due to AP
1356 if (tt_local_entry &&
1357 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1361 best_entry = batadv_transtable_best_orig(tt_global_entry);
1362 /* found anything? */
1364 orig_node = best_entry->orig_node;
1365 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1370 if (tt_global_entry)
1371 batadv_tt_global_entry_free_ref(tt_global_entry);
1373 batadv_tt_local_entry_free_ref(tt_local_entry);
1378 /* Calculates the checksum of the local table of a given orig_node */
1379 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1380 struct batadv_orig_node *orig_node)
1382 uint16_t total = 0, total_one;
1383 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1384 struct batadv_tt_common_entry *tt_common;
1385 struct batadv_tt_global_entry *tt_global;
1386 struct hlist_head *head;
1390 for (i = 0; i < hash->size; i++) {
1391 head = &hash->table[i];
1394 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1395 tt_global = container_of(tt_common,
1396 struct batadv_tt_global_entry,
1398 /* Roaming clients are in the global table for
1399 * consistency only. They don't have to be
1400 * taken into account while computing the
1403 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1405 /* Temporary clients have not been announced yet, so
1406 * they have to be skipped while computing the global
1409 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1412 /* find out if this global entry is announced by this
1415 if (!batadv_tt_global_entry_has_orig(tt_global,
1420 for (j = 0; j < ETH_ALEN; j++)
1421 total_one = crc16_byte(total_one,
1422 tt_common->addr[j]);
1431 /* Calculates the checksum of the local table */
1432 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1434 uint16_t total = 0, total_one;
1435 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1436 struct batadv_tt_common_entry *tt_common;
1437 struct hlist_head *head;
1441 for (i = 0; i < hash->size; i++) {
1442 head = &hash->table[i];
1445 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1446 /* not yet committed clients have not to be taken into
1447 * account while computing the CRC
1449 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1452 for (j = 0; j < ETH_ALEN; j++)
1453 total_one = crc16_byte(total_one,
1454 tt_common->addr[j]);
1463 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1465 struct batadv_tt_req_node *node, *safe;
1467 spin_lock_bh(&bat_priv->tt.req_list_lock);
1469 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1470 list_del(&node->list);
1474 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1477 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1478 struct batadv_orig_node *orig_node,
1479 const unsigned char *tt_buff,
1480 uint8_t tt_num_changes)
1482 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1484 /* Replace the old buffer only if I received something in the
1485 * last OGM (the OGM could carry no changes)
1487 spin_lock_bh(&orig_node->tt_buff_lock);
1488 if (tt_buff_len > 0) {
1489 kfree(orig_node->tt_buff);
1490 orig_node->tt_buff_len = 0;
1491 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1492 if (orig_node->tt_buff) {
1493 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1494 orig_node->tt_buff_len = tt_buff_len;
1497 spin_unlock_bh(&orig_node->tt_buff_lock);
1500 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1502 struct batadv_tt_req_node *node, *safe;
1504 spin_lock_bh(&bat_priv->tt.req_list_lock);
1505 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1506 if (batadv_has_timed_out(node->issued_at,
1507 BATADV_TT_REQUEST_TIMEOUT)) {
1508 list_del(&node->list);
1512 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1515 /* returns the pointer to the new tt_req_node struct if no request
1516 * has already been issued for this orig_node, NULL otherwise
1518 static struct batadv_tt_req_node *
1519 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1520 struct batadv_orig_node *orig_node)
1522 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1524 spin_lock_bh(&bat_priv->tt.req_list_lock);
1525 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1526 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1527 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1528 BATADV_TT_REQUEST_TIMEOUT))
1532 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1536 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1537 tt_req_node->issued_at = jiffies;
1539 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1541 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1545 /* data_ptr is useless here, but has to be kept to respect the prototype */
1546 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1547 const void *data_ptr)
1549 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1551 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1556 static int batadv_tt_global_valid(const void *entry_ptr,
1557 const void *data_ptr)
1559 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1560 const struct batadv_tt_global_entry *tt_global_entry;
1561 const struct batadv_orig_node *orig_node = data_ptr;
1563 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1564 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1567 tt_global_entry = container_of(tt_common_entry,
1568 struct batadv_tt_global_entry,
1571 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1574 static struct sk_buff *
1575 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1576 struct batadv_hashtable *hash,
1577 struct batadv_priv *bat_priv,
1578 int (*valid_cb)(const void *, const void *),
1581 struct batadv_tt_common_entry *tt_common_entry;
1582 struct batadv_tt_query_packet *tt_response;
1583 struct batadv_tt_change *tt_change;
1584 struct hlist_head *head;
1585 struct sk_buff *skb = NULL;
1586 uint16_t tt_tot, tt_count;
1587 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1591 if (tt_query_size + tt_len > bat_priv->soft_iface->mtu) {
1592 tt_len = bat_priv->soft_iface->mtu - tt_query_size;
1593 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1595 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1597 len = tt_query_size + tt_len;
1598 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1602 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1603 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1604 tt_response->ttvn = ttvn;
1606 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1610 for (i = 0; i < hash->size; i++) {
1611 head = &hash->table[i];
1613 hlist_for_each_entry_rcu(tt_common_entry,
1615 if (tt_count == tt_tot)
1618 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1621 memcpy(tt_change->addr, tt_common_entry->addr,
1623 tt_change->flags = tt_common_entry->flags;
1631 /* store in the message the number of entries we have successfully
1634 tt_response->tt_data = htons(tt_count);
1640 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1641 struct batadv_orig_node *dst_orig_node,
1642 uint8_t ttvn, uint16_t tt_crc,
1645 struct sk_buff *skb = NULL;
1646 struct batadv_tt_query_packet *tt_request;
1647 struct batadv_hard_iface *primary_if;
1648 struct batadv_tt_req_node *tt_req_node = NULL;
1652 primary_if = batadv_primary_if_get_selected(bat_priv);
1656 /* The new tt_req will be issued only if I'm not waiting for a
1657 * reply from the same orig_node yet
1659 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1663 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1667 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1669 tt_req_len = sizeof(*tt_request);
1670 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1672 tt_request->header.packet_type = BATADV_TT_QUERY;
1673 tt_request->header.version = BATADV_COMPAT_VERSION;
1674 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1675 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1676 tt_request->header.ttl = BATADV_TTL;
1677 tt_request->ttvn = ttvn;
1678 tt_request->tt_data = htons(tt_crc);
1679 tt_request->flags = BATADV_TT_REQUEST;
1682 tt_request->flags |= BATADV_TT_FULL_TABLE;
1684 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1685 dst_orig_node->orig, (full_table ? 'F' : '.'));
1687 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1689 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1694 batadv_hardif_free_ref(primary_if);
1697 if (ret && tt_req_node) {
1698 spin_lock_bh(&bat_priv->tt.req_list_lock);
1699 list_del(&tt_req_node->list);
1700 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1707 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1708 struct batadv_tt_query_packet *tt_request)
1710 struct batadv_orig_node *req_dst_orig_node;
1711 struct batadv_orig_node *res_dst_orig_node = NULL;
1712 uint8_t orig_ttvn, req_ttvn, ttvn;
1714 unsigned char *tt_buff;
1716 uint16_t tt_len, tt_tot;
1717 struct sk_buff *skb = NULL;
1718 struct batadv_tt_query_packet *tt_response;
1719 uint8_t *packet_pos;
1722 batadv_dbg(BATADV_DBG_TT, bat_priv,
1723 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1724 tt_request->src, tt_request->ttvn, tt_request->dst,
1725 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1727 /* Let's get the orig node of the REAL destination */
1728 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1729 if (!req_dst_orig_node)
1732 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1733 if (!res_dst_orig_node)
1736 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1737 req_ttvn = tt_request->ttvn;
1739 /* I don't have the requested data */
1740 if (orig_ttvn != req_ttvn ||
1741 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1744 /* If the full table has been explicitly requested */
1745 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1746 !req_dst_orig_node->tt_buff)
1751 /* In this version, fragmentation is not implemented, then
1752 * I'll send only one packet with as much TT entries as I can
1755 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1756 tt_len = req_dst_orig_node->tt_buff_len;
1757 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1759 len = sizeof(*tt_response) + tt_len;
1760 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1764 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1765 packet_pos = skb_put(skb, len);
1766 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1767 tt_response->ttvn = req_ttvn;
1768 tt_response->tt_data = htons(tt_tot);
1770 tt_buff = skb->data + sizeof(*tt_response);
1771 /* Copy the last orig_node's OGM buffer */
1772 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1773 req_dst_orig_node->tt_buff_len);
1775 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1777 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1778 tt_len *= sizeof(struct batadv_tt_change);
1779 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1781 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1782 bat_priv->tt.global_hash,
1784 batadv_tt_global_valid,
1789 tt_response = (struct batadv_tt_query_packet *)skb->data;
1792 tt_response->header.packet_type = BATADV_TT_QUERY;
1793 tt_response->header.version = BATADV_COMPAT_VERSION;
1794 tt_response->header.ttl = BATADV_TTL;
1795 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1796 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1797 tt_response->flags = BATADV_TT_RESPONSE;
1800 tt_response->flags |= BATADV_TT_FULL_TABLE;
1802 batadv_dbg(BATADV_DBG_TT, bat_priv,
1803 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1804 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
1806 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1808 if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1813 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1816 if (res_dst_orig_node)
1817 batadv_orig_node_free_ref(res_dst_orig_node);
1818 if (req_dst_orig_node)
1819 batadv_orig_node_free_ref(req_dst_orig_node);
1826 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1827 struct batadv_tt_query_packet *tt_request)
1829 struct batadv_orig_node *orig_node;
1830 struct batadv_hard_iface *primary_if = NULL;
1831 uint8_t my_ttvn, req_ttvn, ttvn;
1833 unsigned char *tt_buff;
1835 uint16_t tt_len, tt_tot;
1836 struct sk_buff *skb = NULL;
1837 struct batadv_tt_query_packet *tt_response;
1838 uint8_t *packet_pos;
1841 batadv_dbg(BATADV_DBG_TT, bat_priv,
1842 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1843 tt_request->src, tt_request->ttvn,
1844 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1847 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1848 req_ttvn = tt_request->ttvn;
1850 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1854 primary_if = batadv_primary_if_get_selected(bat_priv);
1858 /* If the full table has been explicitly requested or the gap
1859 * is too big send the whole local translation table
1861 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1862 !bat_priv->tt.last_changeset)
1867 /* In this version, fragmentation is not implemented, then
1868 * I'll send only one packet with as much TT entries as I can
1871 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1872 tt_len = bat_priv->tt.last_changeset_len;
1873 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1875 len = sizeof(*tt_response) + tt_len;
1876 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1880 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1881 packet_pos = skb_put(skb, len);
1882 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1883 tt_response->ttvn = req_ttvn;
1884 tt_response->tt_data = htons(tt_tot);
1886 tt_buff = skb->data + sizeof(*tt_response);
1887 memcpy(tt_buff, bat_priv->tt.last_changeset,
1888 bat_priv->tt.last_changeset_len);
1889 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1891 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1892 tt_len *= sizeof(struct batadv_tt_change);
1893 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1895 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1896 bat_priv->tt.local_hash,
1898 batadv_tt_local_valid_entry,
1903 tt_response = (struct batadv_tt_query_packet *)skb->data;
1906 tt_response->header.packet_type = BATADV_TT_QUERY;
1907 tt_response->header.version = BATADV_COMPAT_VERSION;
1908 tt_response->header.ttl = BATADV_TTL;
1909 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1910 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1911 tt_response->flags = BATADV_TT_RESPONSE;
1914 tt_response->flags |= BATADV_TT_FULL_TABLE;
1916 batadv_dbg(BATADV_DBG_TT, bat_priv,
1917 "Sending TT_RESPONSE to %pM [%c]\n",
1919 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1921 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1923 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1928 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1931 batadv_orig_node_free_ref(orig_node);
1933 batadv_hardif_free_ref(primary_if);
1936 /* This packet was for me, so it doesn't need to be re-routed */
1940 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1941 struct batadv_tt_query_packet *tt_request)
1943 if (batadv_is_my_mac(bat_priv, tt_request->dst)) {
1944 /* don't answer backbone gws! */
1945 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1948 return batadv_send_my_tt_response(bat_priv, tt_request);
1950 return batadv_send_other_tt_response(bat_priv, tt_request);
1954 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1955 struct batadv_orig_node *orig_node,
1956 struct batadv_tt_change *tt_change,
1957 uint16_t tt_num_changes, uint8_t ttvn)
1962 for (i = 0; i < tt_num_changes; i++) {
1963 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1964 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1965 batadv_tt_global_del(bat_priv, orig_node,
1966 (tt_change + i)->addr,
1967 "tt removed by changes",
1970 if (!batadv_tt_global_add(bat_priv, orig_node,
1971 (tt_change + i)->addr,
1972 (tt_change + i)->flags, ttvn))
1973 /* In case of problem while storing a
1974 * global_entry, we stop the updating
1975 * procedure without committing the
1976 * ttvn change. This will avoid to send
1977 * corrupted data on tt_request
1982 orig_node->tt_initialised = true;
1985 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1986 struct batadv_tt_query_packet *tt_response)
1988 struct batadv_orig_node *orig_node;
1990 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1994 /* Purge the old table first.. */
1995 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
1997 _batadv_tt_update_changes(bat_priv, orig_node,
1998 (struct batadv_tt_change *)(tt_response + 1),
1999 ntohs(tt_response->tt_data),
2002 spin_lock_bh(&orig_node->tt_buff_lock);
2003 kfree(orig_node->tt_buff);
2004 orig_node->tt_buff_len = 0;
2005 orig_node->tt_buff = NULL;
2006 spin_unlock_bh(&orig_node->tt_buff_lock);
2008 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2012 batadv_orig_node_free_ref(orig_node);
2015 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2016 struct batadv_orig_node *orig_node,
2017 uint16_t tt_num_changes, uint8_t ttvn,
2018 struct batadv_tt_change *tt_change)
2020 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2021 tt_num_changes, ttvn);
2023 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2024 (unsigned char *)tt_change, tt_num_changes);
2025 atomic_set(&orig_node->last_ttvn, ttvn);
2028 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2030 struct batadv_tt_local_entry *tt_local_entry;
2033 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2034 if (!tt_local_entry)
2036 /* Check if the client has been logically deleted (but is kept for
2037 * consistency purpose)
2039 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2040 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2045 batadv_tt_local_entry_free_ref(tt_local_entry);
2049 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2050 struct batadv_tt_query_packet *tt_response)
2052 struct batadv_tt_req_node *node, *safe;
2053 struct batadv_orig_node *orig_node = NULL;
2054 struct batadv_tt_change *tt_change;
2056 batadv_dbg(BATADV_DBG_TT, bat_priv,
2057 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2058 tt_response->src, tt_response->ttvn,
2059 ntohs(tt_response->tt_data),
2060 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2062 /* we should have never asked a backbone gw */
2063 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2066 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2070 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2071 batadv_tt_fill_gtable(bat_priv, tt_response);
2073 tt_change = (struct batadv_tt_change *)(tt_response + 1);
2074 batadv_tt_update_changes(bat_priv, orig_node,
2075 ntohs(tt_response->tt_data),
2076 tt_response->ttvn, tt_change);
2079 /* Delete the tt_req_node from pending tt_requests list */
2080 spin_lock_bh(&bat_priv->tt.req_list_lock);
2081 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2082 if (!batadv_compare_eth(node->addr, tt_response->src))
2084 list_del(&node->list);
2087 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2089 /* Recalculate the CRC for this orig_node and store it */
2090 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2093 batadv_orig_node_free_ref(orig_node);
2096 int batadv_tt_init(struct batadv_priv *bat_priv)
2100 ret = batadv_tt_local_init(bat_priv);
2104 ret = batadv_tt_global_init(bat_priv);
2108 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2109 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2110 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2115 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2117 struct batadv_tt_roam_node *node, *safe;
2119 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2121 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2122 list_del(&node->list);
2126 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2129 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2131 struct batadv_tt_roam_node *node, *safe;
2133 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2134 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2135 if (!batadv_has_timed_out(node->first_time,
2136 BATADV_ROAMING_MAX_TIME))
2139 list_del(&node->list);
2142 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2145 /* This function checks whether the client already reached the
2146 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2149 * returns true if the ROAMING_ADV can be sent, false otherwise
2151 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2154 struct batadv_tt_roam_node *tt_roam_node;
2157 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2158 /* The new tt_req will be issued only if I'm not waiting for a
2159 * reply from the same orig_node yet
2161 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2162 if (!batadv_compare_eth(tt_roam_node->addr, client))
2165 if (batadv_has_timed_out(tt_roam_node->first_time,
2166 BATADV_ROAMING_MAX_TIME))
2169 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2170 /* Sorry, you roamed too many times! */
2177 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2181 tt_roam_node->first_time = jiffies;
2182 atomic_set(&tt_roam_node->counter,
2183 BATADV_ROAMING_MAX_COUNT - 1);
2184 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2186 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2191 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2195 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2196 struct batadv_orig_node *orig_node)
2198 struct sk_buff *skb = NULL;
2199 struct batadv_roam_adv_packet *roam_adv_packet;
2201 struct batadv_hard_iface *primary_if;
2202 size_t len = sizeof(*roam_adv_packet);
2204 /* before going on we have to check whether the client has
2205 * already roamed to us too many times
2207 if (!batadv_tt_check_roam_count(bat_priv, client))
2210 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2214 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2216 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2218 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2219 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2220 roam_adv_packet->header.ttl = BATADV_TTL;
2221 roam_adv_packet->reserved = 0;
2222 primary_if = batadv_primary_if_get_selected(bat_priv);
2225 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2226 batadv_hardif_free_ref(primary_if);
2227 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2228 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2230 batadv_dbg(BATADV_DBG_TT, bat_priv,
2231 "Sending ROAMING_ADV to %pM (client %pM)\n",
2232 orig_node->orig, client);
2234 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2236 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2245 static void batadv_tt_purge(struct work_struct *work)
2247 struct delayed_work *delayed_work;
2248 struct batadv_priv_tt *priv_tt;
2249 struct batadv_priv *bat_priv;
2251 delayed_work = container_of(work, struct delayed_work, work);
2252 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2253 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2255 batadv_tt_local_purge(bat_priv);
2256 batadv_tt_global_purge(bat_priv);
2257 batadv_tt_req_purge(bat_priv);
2258 batadv_tt_roam_purge(bat_priv);
2260 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2261 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2264 void batadv_tt_free(struct batadv_priv *bat_priv)
2266 cancel_delayed_work_sync(&bat_priv->tt.work);
2268 batadv_tt_local_table_free(bat_priv);
2269 batadv_tt_global_table_free(bat_priv);
2270 batadv_tt_req_list_free(bat_priv);
2271 batadv_tt_changes_list_free(bat_priv);
2272 batadv_tt_roam_list_free(bat_priv);
2274 kfree(bat_priv->tt.last_changeset);
2277 /* This function will enable or disable the specified flags for all the entries
2278 * in the given hash table and returns the number of modified entries
2280 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2281 uint16_t flags, bool enable)
2284 uint16_t changed_num = 0;
2285 struct hlist_head *head;
2286 struct batadv_tt_common_entry *tt_common_entry;
2291 for (i = 0; i < hash->size; i++) {
2292 head = &hash->table[i];
2295 hlist_for_each_entry_rcu(tt_common_entry,
2298 if ((tt_common_entry->flags & flags) == flags)
2300 tt_common_entry->flags |= flags;
2302 if (!(tt_common_entry->flags & flags))
2304 tt_common_entry->flags &= ~flags;
2314 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2315 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2317 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2318 struct batadv_tt_common_entry *tt_common;
2319 struct batadv_tt_local_entry *tt_local;
2320 struct hlist_node *node_tmp;
2321 struct hlist_head *head;
2322 spinlock_t *list_lock; /* protects write access to the hash lists */
2328 for (i = 0; i < hash->size; i++) {
2329 head = &hash->table[i];
2330 list_lock = &hash->list_locks[i];
2332 spin_lock_bh(list_lock);
2333 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2335 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2338 batadv_dbg(BATADV_DBG_TT, bat_priv,
2339 "Deleting local tt entry (%pM): pending\n",
2342 atomic_dec(&bat_priv->tt.local_entry_num);
2343 hlist_del_rcu(&tt_common->hash_entry);
2344 tt_local = container_of(tt_common,
2345 struct batadv_tt_local_entry,
2347 batadv_tt_local_entry_free_ref(tt_local);
2349 spin_unlock_bh(list_lock);
2353 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2354 unsigned char **packet_buff,
2355 int *packet_buff_len, int packet_min_len)
2357 uint16_t changed_num = 0;
2359 if (atomic_read(&bat_priv->tt.local_changes) < 1)
2362 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2363 BATADV_TT_CLIENT_NEW, false);
2365 /* all reset entries have to be counted as local entries */
2366 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2367 batadv_tt_local_purge_pending_clients(bat_priv);
2368 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2370 /* Increment the TTVN only once per OGM interval */
2371 atomic_inc(&bat_priv->tt.vn);
2372 batadv_dbg(BATADV_DBG_TT, bat_priv,
2373 "Local changes committed, updating to ttvn %u\n",
2374 (uint8_t)atomic_read(&bat_priv->tt.vn));
2376 /* reset the sending counter */
2377 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2379 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2380 packet_buff_len, packet_min_len);
2383 /* when calling this function (hard_iface == primary_if) has to be true */
2384 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2385 unsigned char **packet_buff, int *packet_buff_len,
2390 /* if at least one change happened */
2391 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2395 /* if the changes have been sent often enough */
2396 if ((tt_num_changes < 0) &&
2397 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2398 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2399 packet_min_len, packet_min_len);
2403 return tt_num_changes;
2406 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2409 struct batadv_tt_local_entry *tt_local_entry = NULL;
2410 struct batadv_tt_global_entry *tt_global_entry = NULL;
2413 if (!atomic_read(&bat_priv->ap_isolation))
2416 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2417 if (!tt_local_entry)
2420 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2421 if (!tt_global_entry)
2424 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2430 if (tt_global_entry)
2431 batadv_tt_global_entry_free_ref(tt_global_entry);
2433 batadv_tt_local_entry_free_ref(tt_local_entry);
2437 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2438 struct batadv_orig_node *orig_node,
2439 const unsigned char *tt_buff, uint8_t tt_num_changes,
2440 uint8_t ttvn, uint16_t tt_crc)
2442 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2443 bool full_table = true;
2444 struct batadv_tt_change *tt_change;
2446 /* don't care about a backbone gateways updates. */
2447 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2450 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2451 * increased by one -> we can apply the attached changes
2453 if ((!orig_node->tt_initialised && ttvn == 1) ||
2454 ttvn - orig_ttvn == 1) {
2455 /* the OGM could not contain the changes due to their size or
2456 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2458 * In this case send a tt request
2460 if (!tt_num_changes) {
2465 tt_change = (struct batadv_tt_change *)tt_buff;
2466 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2469 /* Even if we received the precomputed crc with the OGM, we
2470 * prefer to recompute it to spot any possible inconsistency
2471 * in the global table
2473 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2475 /* The ttvn alone is not enough to guarantee consistency
2476 * because a single value could represent different states
2477 * (due to the wrap around). Thus a node has to check whether
2478 * the resulting table (after applying the changes) is still
2479 * consistent or not. E.g. a node could disconnect while its
2480 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2481 * checking the CRC value is mandatory to detect the
2484 if (orig_node->tt_crc != tt_crc)
2487 /* if we missed more than one change or our tables are not
2488 * in sync anymore -> request fresh tt data
2490 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2491 orig_node->tt_crc != tt_crc) {
2493 batadv_dbg(BATADV_DBG_TT, bat_priv,
2494 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2495 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2496 orig_node->tt_crc, tt_num_changes);
2497 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2498 tt_crc, full_table);
2504 /* returns true whether we know that the client has moved from its old
2505 * originator to another one. This entry is kept is still kept for consistency
2508 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2511 struct batadv_tt_global_entry *tt_global_entry;
2514 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2515 if (!tt_global_entry)
2518 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2519 batadv_tt_global_entry_free_ref(tt_global_entry);
2525 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2526 * @bat_priv: the bat priv with all the soft interface information
2527 * @addr: the MAC address of the local client to query
2529 * Returns true if the local client is known to be roaming (it is not served by
2530 * this node anymore) or not. If yes, the client is still present in the table
2531 * to keep the latter consistent with the node TTVN
2533 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2536 struct batadv_tt_local_entry *tt_local_entry;
2539 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2540 if (!tt_local_entry)
2543 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2544 batadv_tt_local_entry_free_ref(tt_local_entry);
2549 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2550 struct batadv_orig_node *orig_node,
2551 const unsigned char *addr)
2555 /* if the originator is a backbone node (meaning it belongs to the same
2556 * LAN of this node) the temporary client must not be added because to
2557 * reach such destination the node must use the LAN instead of the mesh
2559 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2562 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2563 BATADV_TT_CLIENT_TEMP,
2564 atomic_read(&orig_node->last_ttvn)))
2567 batadv_dbg(BATADV_DBG_TT, bat_priv,
2568 "Added temporary global client (addr: %pM orig: %pM)\n",
2569 addr, orig_node->orig);