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);
148 /* We are in an rcu callback here, therefore we cannot use
149 * batadv_orig_node_free_ref() and its call_rcu():
150 * An rcu_barrier() wouldn't wait for that to finish
152 batadv_orig_node_free_ref_now(orig_entry->orig_node);
157 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
159 if (!atomic_dec_and_test(&orig_entry->refcount))
161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
167 * batadv_tt_local_event - store a local TT event (ADD/DEL)
168 * @bat_priv: the bat priv with all the soft interface information
169 * @tt_local_entry: the TT entry involved in the event
170 * @event_flags: flags to store in the event structure
172 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
173 struct batadv_tt_local_entry *tt_local_entry,
176 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
177 struct batadv_tt_common_entry *common = &tt_local_entry->common;
178 uint8_t flags = common->flags | event_flags;
179 bool event_removed = false;
180 bool del_op_requested, del_op_entry;
182 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
186 tt_change_node->change.flags = flags;
187 tt_change_node->change.reserved = 0;
188 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
190 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
192 /* check for ADD+DEL or DEL+ADD events */
193 spin_lock_bh(&bat_priv->tt.changes_list_lock);
194 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
196 if (!batadv_compare_eth(entry->change.addr, common->addr))
199 /* DEL+ADD in the same orig interval have no effect and can be
200 * removed to avoid silly behaviour on the receiver side. The
201 * other way around (ADD+DEL) can happen in case of roaming of
202 * a client still in the NEW state. Roaming of NEW clients is
203 * now possible due to automatically recognition of "temporary"
206 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
207 if (!del_op_requested && del_op_entry)
209 if (del_op_requested && !del_op_entry)
213 list_del(&entry->list);
215 kfree(tt_change_node);
216 event_removed = true;
220 /* track the change in the OGMinterval list */
221 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
224 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
227 atomic_dec(&bat_priv->tt.local_changes);
229 atomic_inc(&bat_priv->tt.local_changes);
233 * batadv_tt_len - compute length in bytes of given number of tt changes
234 * @changes_num: number of tt changes
236 * Returns computed length in bytes.
238 static int batadv_tt_len(int changes_num)
240 return changes_num * sizeof(struct batadv_tvlv_tt_change);
243 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
245 if (bat_priv->tt.local_hash)
248 bat_priv->tt.local_hash = batadv_hash_new(1024);
250 if (!bat_priv->tt.local_hash)
253 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
254 &batadv_tt_local_hash_lock_class_key);
259 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
260 struct batadv_tt_global_entry *tt_global,
263 batadv_dbg(BATADV_DBG_TT, bat_priv,
264 "Deleting global tt entry %pM: %s\n",
265 tt_global->common.addr, message);
267 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
268 batadv_choose_orig, tt_global->common.addr);
269 batadv_tt_global_entry_free_ref(tt_global);
272 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
275 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
276 struct batadv_tt_local_entry *tt_local;
277 struct batadv_tt_global_entry *tt_global;
278 struct hlist_head *head;
279 struct batadv_tt_orig_list_entry *orig_entry;
281 bool roamed_back = false;
283 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
284 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
287 tt_local->last_seen = jiffies;
288 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
289 batadv_dbg(BATADV_DBG_TT, bat_priv,
290 "Re-adding pending client %pM\n", addr);
291 /* whatever the reason why the PENDING flag was set,
292 * this is a client which was enqueued to be removed in
293 * this orig_interval. Since it popped up again, the
294 * flag can be reset like it was never enqueued
296 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
300 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
301 batadv_dbg(BATADV_DBG_TT, bat_priv,
302 "Roaming client %pM came back to its original location\n",
304 /* the ROAM flag is set because this client roamed away
305 * and the node got a roaming_advertisement message. Now
306 * that the client popped up again at its original
307 * location such flag can be unset
309 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
315 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
319 batadv_dbg(BATADV_DBG_TT, bat_priv,
320 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
321 (uint8_t)atomic_read(&bat_priv->tt.vn));
323 memcpy(tt_local->common.addr, addr, ETH_ALEN);
324 /* The local entry has to be marked as NEW to avoid to send it in
325 * a full table response going out before the next ttvn increment
326 * (consistency check)
328 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
329 if (batadv_is_wifi_iface(ifindex))
330 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
331 atomic_set(&tt_local->common.refcount, 2);
332 tt_local->last_seen = jiffies;
333 tt_local->common.added_at = tt_local->last_seen;
335 /* the batman interface mac address should never be purged */
336 if (batadv_compare_eth(addr, soft_iface->dev_addr))
337 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
339 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
340 batadv_choose_orig, &tt_local->common,
341 &tt_local->common.hash_entry);
343 if (unlikely(hash_added != 0)) {
344 /* remove the reference for the hash */
345 batadv_tt_local_entry_free_ref(tt_local);
350 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
353 /* Check whether it is a roaming, but don't do anything if the roaming
354 * process has already been handled
356 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
357 /* These node are probably going to update their tt table */
358 head = &tt_global->orig_list;
360 hlist_for_each_entry_rcu(orig_entry, head, list) {
361 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
362 orig_entry->orig_node);
366 batadv_tt_global_free(bat_priv, tt_global,
370 /* The global entry has to be marked as ROAMING and
371 * has to be kept for consistency purpose
373 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
374 tt_global->roam_at = jiffies;
380 batadv_tt_local_entry_free_ref(tt_local);
382 batadv_tt_global_entry_free_ref(tt_global);
386 * batadv_tt_tvlv_container_update - update the translation table tvlv container
387 * after local tt changes have been committed
388 * @bat_priv: the bat priv with all the soft interface information
390 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
392 struct batadv_tt_change_node *entry, *safe;
393 struct batadv_tvlv_tt_data *tt_data;
394 struct batadv_tvlv_tt_change *tt_change;
395 int tt_diff_len = 0, tt_change_len = 0;
396 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
398 tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
400 /* if we have too many changes for one packet don't send any
401 * and wait for the tt table request which will be fragmented
403 if (tt_diff_len > bat_priv->soft_iface->mtu)
406 tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
410 tt_data->flags = BATADV_TT_OGM_DIFF;
411 tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
412 tt_data->crc = htons(bat_priv->tt.local_crc);
414 if (tt_diff_len == 0)
415 goto container_register;
417 tt_diff_entries_num = tt_diff_len / batadv_tt_len(1);
419 spin_lock_bh(&bat_priv->tt.changes_list_lock);
420 atomic_set(&bat_priv->tt.local_changes, 0);
422 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
424 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
426 if (tt_diff_entries_count < tt_diff_entries_num) {
427 memcpy(tt_change + tt_diff_entries_count,
429 sizeof(struct batadv_tvlv_tt_change));
430 tt_diff_entries_count++;
432 list_del(&entry->list);
435 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
437 /* Keep the buffer for possible tt_request */
438 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
439 kfree(bat_priv->tt.last_changeset);
440 bat_priv->tt.last_changeset_len = 0;
441 bat_priv->tt.last_changeset = NULL;
442 tt_change_len = batadv_tt_len(tt_diff_entries_count);
443 /* check whether this new OGM has no changes due to size problems */
444 if (tt_diff_entries_count > 0) {
445 /* if kmalloc() fails we will reply with the full table
446 * instead of providing the diff
448 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
449 if (bat_priv->tt.last_changeset) {
450 memcpy(bat_priv->tt.last_changeset,
451 tt_change, tt_change_len);
452 bat_priv->tt.last_changeset_len = tt_diff_len;
455 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
458 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
459 sizeof(*tt_data) + tt_change_len);
463 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
465 struct net_device *net_dev = (struct net_device *)seq->private;
466 struct batadv_priv *bat_priv = netdev_priv(net_dev);
467 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
468 struct batadv_tt_common_entry *tt_common_entry;
469 struct batadv_tt_local_entry *tt_local;
470 struct batadv_hard_iface *primary_if;
471 struct hlist_head *head;
475 unsigned long last_seen_jiffies;
477 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
479 primary_if = batadv_seq_print_text_primary_if_get(seq);
484 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
485 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
486 bat_priv->tt.local_crc);
487 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags",
490 for (i = 0; i < hash->size; i++) {
491 head = &hash->table[i];
494 hlist_for_each_entry_rcu(tt_common_entry,
496 tt_local = container_of(tt_common_entry,
497 struct batadv_tt_local_entry,
499 last_seen_jiffies = jiffies - tt_local->last_seen;
500 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
501 last_seen_secs = last_seen_msecs / 1000;
502 last_seen_msecs = last_seen_msecs % 1000;
504 no_purge = tt_common_entry->flags & np_flag;
506 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
507 tt_common_entry->addr,
508 (tt_common_entry->flags &
509 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
510 no_purge ? 'P' : '.',
511 (tt_common_entry->flags &
512 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
513 (tt_common_entry->flags &
514 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
515 (tt_common_entry->flags &
516 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
517 no_purge ? 0 : last_seen_secs,
518 no_purge ? 0 : last_seen_msecs);
524 batadv_hardif_free_ref(primary_if);
529 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
530 struct batadv_tt_local_entry *tt_local_entry,
531 uint16_t flags, const char *message)
533 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
535 /* The local client has to be marked as "pending to be removed" but has
536 * to be kept in the table in order to send it in a full table
537 * response issued before the net ttvn increment (consistency check)
539 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
541 batadv_dbg(BATADV_DBG_TT, bat_priv,
542 "Local tt entry (%pM) pending to be removed: %s\n",
543 tt_local_entry->common.addr, message);
547 * batadv_tt_local_remove - logically remove an entry from the local table
548 * @bat_priv: the bat priv with all the soft interface information
549 * @addr: the MAC address of the client to remove
550 * @message: message to append to the log on deletion
551 * @roaming: true if the deletion is due to a roaming event
553 * Returns the flags assigned to the local entry before being deleted
555 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
556 const uint8_t *addr, const char *message,
559 struct batadv_tt_local_entry *tt_local_entry;
560 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
562 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
566 curr_flags = tt_local_entry->common.flags;
568 flags = BATADV_TT_CLIENT_DEL;
569 /* if this global entry addition is due to a roaming, the node has to
570 * mark the local entry as "roamed" in order to correctly reroute
574 flags |= BATADV_TT_CLIENT_ROAM;
575 /* mark the local client as ROAMed */
576 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
579 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
580 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
584 /* if this client has been added right now, it is possible to
585 * immediately purge it
587 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
588 hlist_del_rcu(&tt_local_entry->common.hash_entry);
589 batadv_tt_local_entry_free_ref(tt_local_entry);
593 batadv_tt_local_entry_free_ref(tt_local_entry);
598 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
599 struct hlist_head *head)
601 struct batadv_tt_local_entry *tt_local_entry;
602 struct batadv_tt_common_entry *tt_common_entry;
603 struct hlist_node *node_tmp;
605 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
607 tt_local_entry = container_of(tt_common_entry,
608 struct batadv_tt_local_entry,
610 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
613 /* entry already marked for deletion */
614 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
617 if (!batadv_has_timed_out(tt_local_entry->last_seen,
618 BATADV_TT_LOCAL_TIMEOUT))
621 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
622 BATADV_TT_CLIENT_DEL, "timed out");
626 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
628 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
629 struct hlist_head *head;
630 spinlock_t *list_lock; /* protects write access to the hash lists */
633 for (i = 0; i < hash->size; i++) {
634 head = &hash->table[i];
635 list_lock = &hash->list_locks[i];
637 spin_lock_bh(list_lock);
638 batadv_tt_local_purge_list(bat_priv, head);
639 spin_unlock_bh(list_lock);
643 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
645 struct batadv_hashtable *hash;
646 spinlock_t *list_lock; /* protects write access to the hash lists */
647 struct batadv_tt_common_entry *tt_common_entry;
648 struct batadv_tt_local_entry *tt_local;
649 struct hlist_node *node_tmp;
650 struct hlist_head *head;
653 if (!bat_priv->tt.local_hash)
656 hash = bat_priv->tt.local_hash;
658 for (i = 0; i < hash->size; i++) {
659 head = &hash->table[i];
660 list_lock = &hash->list_locks[i];
662 spin_lock_bh(list_lock);
663 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
665 hlist_del_rcu(&tt_common_entry->hash_entry);
666 tt_local = container_of(tt_common_entry,
667 struct batadv_tt_local_entry,
669 batadv_tt_local_entry_free_ref(tt_local);
671 spin_unlock_bh(list_lock);
674 batadv_hash_destroy(hash);
676 bat_priv->tt.local_hash = NULL;
679 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
681 if (bat_priv->tt.global_hash)
684 bat_priv->tt.global_hash = batadv_hash_new(1024);
686 if (!bat_priv->tt.global_hash)
689 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
690 &batadv_tt_global_hash_lock_class_key);
695 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
697 struct batadv_tt_change_node *entry, *safe;
699 spin_lock_bh(&bat_priv->tt.changes_list_lock);
701 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
703 list_del(&entry->list);
707 atomic_set(&bat_priv->tt.local_changes, 0);
708 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
711 /* retrieves the orig_tt_list_entry belonging to orig_node from the
712 * batadv_tt_global_entry list
714 * returns it with an increased refcounter, NULL if not found
716 static struct batadv_tt_orig_list_entry *
717 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
718 const struct batadv_orig_node *orig_node)
720 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
721 const struct hlist_head *head;
724 head = &entry->orig_list;
725 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
726 if (tmp_orig_entry->orig_node != orig_node)
728 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
731 orig_entry = tmp_orig_entry;
739 /* find out if an orig_node is already in the list of a tt_global_entry.
740 * returns true if found, false otherwise
743 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
744 const struct batadv_orig_node *orig_node)
746 struct batadv_tt_orig_list_entry *orig_entry;
749 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
752 batadv_tt_orig_list_entry_free_ref(orig_entry);
759 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
760 struct batadv_orig_node *orig_node, int ttvn)
762 struct batadv_tt_orig_list_entry *orig_entry;
764 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
766 /* refresh the ttvn: the current value could be a bogus one that
767 * was added during a "temporary client detection"
769 orig_entry->ttvn = ttvn;
773 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
777 INIT_HLIST_NODE(&orig_entry->list);
778 atomic_inc(&orig_node->refcount);
779 atomic_inc(&orig_node->tt_size);
780 orig_entry->orig_node = orig_node;
781 orig_entry->ttvn = ttvn;
782 atomic_set(&orig_entry->refcount, 2);
784 spin_lock_bh(&tt_global->list_lock);
785 hlist_add_head_rcu(&orig_entry->list,
786 &tt_global->orig_list);
787 spin_unlock_bh(&tt_global->list_lock);
790 batadv_tt_orig_list_entry_free_ref(orig_entry);
794 * batadv_tt_global_add - add a new TT global entry or update an existing one
795 * @bat_priv: the bat priv with all the soft interface information
796 * @orig_node: the originator announcing the client
797 * @tt_addr: the mac address of the non-mesh client
798 * @flags: TT flags that have to be set for this non-mesh client
799 * @ttvn: the tt version number ever announcing this non-mesh client
801 * Add a new TT global entry for the given originator. If the entry already
802 * exists add a new reference to the given originator (a global entry can have
803 * references to multiple originators) and adjust the flags attribute to reflect
804 * the function argument.
805 * If a TT local entry exists for this non-mesh client remove it.
807 * The caller must hold orig_node refcount.
809 int batadv_tt_global_add(struct batadv_priv *bat_priv,
810 struct batadv_orig_node *orig_node,
811 const unsigned char *tt_addr, uint16_t flags,
814 struct batadv_tt_global_entry *tt_global_entry;
815 struct batadv_tt_local_entry *tt_local_entry;
818 struct batadv_tt_common_entry *common;
819 uint16_t local_flags;
821 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
822 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
824 /* if the node already has a local client for this entry, it has to wait
825 * for a roaming advertisement instead of manually messing up the global
828 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
829 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
832 if (!tt_global_entry) {
833 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
834 if (!tt_global_entry)
837 common = &tt_global_entry->common;
838 memcpy(common->addr, tt_addr, ETH_ALEN);
840 common->flags = flags;
841 tt_global_entry->roam_at = 0;
842 /* node must store current time in case of roaming. This is
843 * needed to purge this entry out on timeout (if nobody claims
846 if (flags & BATADV_TT_CLIENT_ROAM)
847 tt_global_entry->roam_at = jiffies;
848 atomic_set(&common->refcount, 2);
849 common->added_at = jiffies;
851 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
852 spin_lock_init(&tt_global_entry->list_lock);
854 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
856 batadv_choose_orig, common,
857 &common->hash_entry);
859 if (unlikely(hash_added != 0)) {
860 /* remove the reference for the hash */
861 batadv_tt_global_entry_free_ref(tt_global_entry);
865 common = &tt_global_entry->common;
866 /* If there is already a global entry, we can use this one for
868 * But if we are trying to add a temporary client then here are
869 * two options at this point:
870 * 1) the global client is not a temporary client: the global
871 * client has to be left as it is, temporary information
872 * should never override any already known client state
873 * 2) the global client is a temporary client: purge the
874 * originator list and add the new one orig_entry
876 if (flags & BATADV_TT_CLIENT_TEMP) {
877 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
879 if (batadv_tt_global_entry_has_orig(tt_global_entry,
882 batadv_tt_global_del_orig_list(tt_global_entry);
886 /* if the client was temporary added before receiving the first
887 * OGM announcing it, we have to clear the TEMP flag
889 common->flags &= ~BATADV_TT_CLIENT_TEMP;
891 /* the change can carry possible "attribute" flags like the
892 * TT_CLIENT_WIFI, therefore they have to be copied in the
895 tt_global_entry->common.flags |= flags;
897 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
898 * one originator left in the list and we previously received a
899 * delete + roaming change for this originator.
901 * We should first delete the old originator before adding the
904 if (common->flags & BATADV_TT_CLIENT_ROAM) {
905 batadv_tt_global_del_orig_list(tt_global_entry);
906 common->flags &= ~BATADV_TT_CLIENT_ROAM;
907 tt_global_entry->roam_at = 0;
911 /* add the new orig_entry (if needed) or update it */
912 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
914 batadv_dbg(BATADV_DBG_TT, bat_priv,
915 "Creating new global tt entry: %pM (via %pM)\n",
916 common->addr, orig_node->orig);
921 /* remove address from local hash if present */
922 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
923 "global tt received",
924 flags & BATADV_TT_CLIENT_ROAM);
925 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
927 if (!(flags & BATADV_TT_CLIENT_ROAM))
928 /* this is a normal global add. Therefore the client is not in a
929 * roaming state anymore.
931 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
935 batadv_tt_global_entry_free_ref(tt_global_entry);
937 batadv_tt_local_entry_free_ref(tt_local_entry);
941 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
942 * @tt_global_entry: global translation table entry to be analyzed
944 * This functon assumes the caller holds rcu_read_lock().
945 * Returns best originator list entry or NULL on errors.
947 static struct batadv_tt_orig_list_entry *
948 batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
950 struct batadv_neigh_node *router = NULL;
951 struct hlist_head *head;
952 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
955 head = &tt_global_entry->orig_list;
956 hlist_for_each_entry_rcu(orig_entry, head, list) {
957 router = batadv_orig_node_get_router(orig_entry->orig_node);
961 if (router->tq_avg > best_tq) {
962 best_entry = orig_entry;
963 best_tq = router->tq_avg;
966 batadv_neigh_node_free_ref(router);
972 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
973 * for this global entry
974 * @tt_global_entry: global translation table entry to be printed
975 * @seq: debugfs table seq_file struct
977 * This functon assumes the caller holds rcu_read_lock().
980 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
981 struct seq_file *seq)
983 struct hlist_head *head;
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, 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_head *head;
1033 primary_if = batadv_seq_print_text_primary_if_get(seq);
1038 "Globally announced TT entries received via the mesh %s\n",
1040 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n",
1041 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1044 for (i = 0; i < hash->size; i++) {
1045 head = &hash->table[i];
1048 hlist_for_each_entry_rcu(tt_common_entry,
1050 tt_global = container_of(tt_common_entry,
1051 struct batadv_tt_global_entry,
1053 batadv_tt_global_print_entry(tt_global, seq);
1059 batadv_hardif_free_ref(primary_if);
1063 /* deletes the orig list of a tt_global_entry */
1065 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
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 hlist_del_rcu(&orig_entry->list);
1075 batadv_tt_orig_list_entry_free_ref(orig_entry);
1077 spin_unlock_bh(&tt_global_entry->list_lock);
1081 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1082 struct batadv_tt_global_entry *tt_global_entry,
1083 struct batadv_orig_node *orig_node,
1084 const char *message)
1086 struct hlist_head *head;
1087 struct hlist_node *safe;
1088 struct batadv_tt_orig_list_entry *orig_entry;
1090 spin_lock_bh(&tt_global_entry->list_lock);
1091 head = &tt_global_entry->orig_list;
1092 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1093 if (orig_entry->orig_node == orig_node) {
1094 batadv_dbg(BATADV_DBG_TT, bat_priv,
1095 "Deleting %pM from global tt entry %pM: %s\n",
1097 tt_global_entry->common.addr, message);
1098 hlist_del_rcu(&orig_entry->list);
1099 batadv_tt_orig_list_entry_free_ref(orig_entry);
1102 spin_unlock_bh(&tt_global_entry->list_lock);
1105 /* If the client is to be deleted, we check if it is the last origantor entry
1106 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1107 * timer, otherwise we simply remove the originator scheduled for deletion.
1110 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1111 struct batadv_tt_global_entry *tt_global_entry,
1112 struct batadv_orig_node *orig_node,
1113 const char *message)
1115 bool last_entry = true;
1116 struct hlist_head *head;
1117 struct batadv_tt_orig_list_entry *orig_entry;
1119 /* no local entry exists, case 1:
1120 * Check if this is the last one or if other entries exist.
1124 head = &tt_global_entry->orig_list;
1125 hlist_for_each_entry_rcu(orig_entry, head, list) {
1126 if (orig_entry->orig_node != orig_node) {
1134 /* its the last one, mark for roaming. */
1135 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1136 tt_global_entry->roam_at = jiffies;
1138 /* there is another entry, we can simply delete this
1139 * one and can still use the other one.
1141 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1142 orig_node, message);
1147 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1148 struct batadv_orig_node *orig_node,
1149 const unsigned char *addr,
1150 const char *message, bool roaming)
1152 struct batadv_tt_global_entry *tt_global_entry;
1153 struct batadv_tt_local_entry *local_entry = NULL;
1155 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1156 if (!tt_global_entry)
1160 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1161 orig_node, message);
1163 if (hlist_empty(&tt_global_entry->orig_list))
1164 batadv_tt_global_free(bat_priv, tt_global_entry,
1170 /* if we are deleting a global entry due to a roam
1171 * event, there are two possibilities:
1172 * 1) the client roamed from node A to node B => if there
1173 * is only one originator left for this client, we mark
1174 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1175 * wait for node B to claim it. In case of timeout
1176 * the entry is purged.
1178 * If there are other originators left, we directly delete
1180 * 2) the client roamed to us => we can directly delete
1181 * the global entry, since it is useless now.
1183 local_entry = batadv_tt_local_hash_find(bat_priv,
1184 tt_global_entry->common.addr);
1186 /* local entry exists, case 2: client roamed to us. */
1187 batadv_tt_global_del_orig_list(tt_global_entry);
1188 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1190 /* no local entry exists, case 1: check for roaming */
1191 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1192 orig_node, message);
1196 if (tt_global_entry)
1197 batadv_tt_global_entry_free_ref(tt_global_entry);
1199 batadv_tt_local_entry_free_ref(local_entry);
1202 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1203 struct batadv_orig_node *orig_node,
1204 const char *message)
1206 struct batadv_tt_global_entry *tt_global;
1207 struct batadv_tt_common_entry *tt_common_entry;
1209 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1210 struct hlist_node *safe;
1211 struct hlist_head *head;
1212 spinlock_t *list_lock; /* protects write access to the hash lists */
1217 for (i = 0; i < hash->size; i++) {
1218 head = &hash->table[i];
1219 list_lock = &hash->list_locks[i];
1221 spin_lock_bh(list_lock);
1222 hlist_for_each_entry_safe(tt_common_entry, safe,
1224 tt_global = container_of(tt_common_entry,
1225 struct batadv_tt_global_entry,
1228 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1229 orig_node, message);
1231 if (hlist_empty(&tt_global->orig_list)) {
1232 batadv_dbg(BATADV_DBG_TT, bat_priv,
1233 "Deleting global tt entry %pM: %s\n",
1234 tt_global->common.addr, message);
1235 hlist_del_rcu(&tt_common_entry->hash_entry);
1236 batadv_tt_global_entry_free_ref(tt_global);
1239 spin_unlock_bh(list_lock);
1241 orig_node->tt_initialised = false;
1244 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1248 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1249 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1251 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1252 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1254 *msg = "Roaming timeout\n";
1257 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1258 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1260 *msg = "Temporary client timeout\n";
1266 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1268 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1269 struct hlist_head *head;
1270 struct hlist_node *node_tmp;
1271 spinlock_t *list_lock; /* protects write access to the hash lists */
1274 struct batadv_tt_common_entry *tt_common;
1275 struct batadv_tt_global_entry *tt_global;
1277 for (i = 0; i < hash->size; i++) {
1278 head = &hash->table[i];
1279 list_lock = &hash->list_locks[i];
1281 spin_lock_bh(list_lock);
1282 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1284 tt_global = container_of(tt_common,
1285 struct batadv_tt_global_entry,
1288 if (!batadv_tt_global_to_purge(tt_global, &msg))
1291 batadv_dbg(BATADV_DBG_TT, bat_priv,
1292 "Deleting global tt entry (%pM): %s\n",
1293 tt_global->common.addr, msg);
1295 hlist_del_rcu(&tt_common->hash_entry);
1297 batadv_tt_global_entry_free_ref(tt_global);
1299 spin_unlock_bh(list_lock);
1303 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1305 struct batadv_hashtable *hash;
1306 spinlock_t *list_lock; /* protects write access to the hash lists */
1307 struct batadv_tt_common_entry *tt_common_entry;
1308 struct batadv_tt_global_entry *tt_global;
1309 struct hlist_node *node_tmp;
1310 struct hlist_head *head;
1313 if (!bat_priv->tt.global_hash)
1316 hash = bat_priv->tt.global_hash;
1318 for (i = 0; i < hash->size; i++) {
1319 head = &hash->table[i];
1320 list_lock = &hash->list_locks[i];
1322 spin_lock_bh(list_lock);
1323 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1325 hlist_del_rcu(&tt_common_entry->hash_entry);
1326 tt_global = container_of(tt_common_entry,
1327 struct batadv_tt_global_entry,
1329 batadv_tt_global_entry_free_ref(tt_global);
1331 spin_unlock_bh(list_lock);
1334 batadv_hash_destroy(hash);
1336 bat_priv->tt.global_hash = NULL;
1340 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1341 struct batadv_tt_global_entry *tt_global_entry)
1345 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1346 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1352 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1354 const uint8_t *addr)
1356 struct batadv_tt_local_entry *tt_local_entry = NULL;
1357 struct batadv_tt_global_entry *tt_global_entry = NULL;
1358 struct batadv_orig_node *orig_node = NULL;
1359 struct batadv_tt_orig_list_entry *best_entry;
1361 if (src && atomic_read(&bat_priv->ap_isolation)) {
1362 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1363 if (!tt_local_entry ||
1364 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1368 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1369 if (!tt_global_entry)
1372 /* check whether the clients should not communicate due to AP
1375 if (tt_local_entry &&
1376 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1380 best_entry = batadv_transtable_best_orig(tt_global_entry);
1381 /* found anything? */
1383 orig_node = best_entry->orig_node;
1384 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1389 if (tt_global_entry)
1390 batadv_tt_global_entry_free_ref(tt_global_entry);
1392 batadv_tt_local_entry_free_ref(tt_local_entry);
1397 /* Calculates the checksum of the local table of a given orig_node */
1398 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1399 struct batadv_orig_node *orig_node)
1401 uint16_t total = 0, total_one;
1402 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1403 struct batadv_tt_common_entry *tt_common;
1404 struct batadv_tt_global_entry *tt_global;
1405 struct hlist_head *head;
1409 for (i = 0; i < hash->size; i++) {
1410 head = &hash->table[i];
1413 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1414 tt_global = container_of(tt_common,
1415 struct batadv_tt_global_entry,
1417 /* Roaming clients are in the global table for
1418 * consistency only. They don't have to be
1419 * taken into account while computing the
1422 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1424 /* Temporary clients have not been announced yet, so
1425 * they have to be skipped while computing the global
1428 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1431 /* find out if this global entry is announced by this
1434 if (!batadv_tt_global_entry_has_orig(tt_global,
1439 for (j = 0; j < ETH_ALEN; j++)
1440 total_one = crc16_byte(total_one,
1441 tt_common->addr[j]);
1450 /* Calculates the checksum of the local table */
1451 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1453 uint16_t total = 0, total_one;
1454 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1455 struct batadv_tt_common_entry *tt_common;
1456 struct hlist_head *head;
1460 for (i = 0; i < hash->size; i++) {
1461 head = &hash->table[i];
1464 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1465 /* not yet committed clients have not to be taken into
1466 * account while computing the CRC
1468 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1471 for (j = 0; j < ETH_ALEN; j++)
1472 total_one = crc16_byte(total_one,
1473 tt_common->addr[j]);
1482 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1484 struct batadv_tt_req_node *node, *safe;
1486 spin_lock_bh(&bat_priv->tt.req_list_lock);
1488 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1489 list_del(&node->list);
1493 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1496 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1497 struct batadv_orig_node *orig_node,
1498 const unsigned char *tt_buff,
1499 uint16_t tt_num_changes)
1501 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1503 /* Replace the old buffer only if I received something in the
1504 * last OGM (the OGM could carry no changes)
1506 spin_lock_bh(&orig_node->tt_buff_lock);
1507 if (tt_buff_len > 0) {
1508 kfree(orig_node->tt_buff);
1509 orig_node->tt_buff_len = 0;
1510 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1511 if (orig_node->tt_buff) {
1512 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1513 orig_node->tt_buff_len = tt_buff_len;
1516 spin_unlock_bh(&orig_node->tt_buff_lock);
1519 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1521 struct batadv_tt_req_node *node, *safe;
1523 spin_lock_bh(&bat_priv->tt.req_list_lock);
1524 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1525 if (batadv_has_timed_out(node->issued_at,
1526 BATADV_TT_REQUEST_TIMEOUT)) {
1527 list_del(&node->list);
1531 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1534 /* returns the pointer to the new tt_req_node struct if no request
1535 * has already been issued for this orig_node, NULL otherwise
1537 static struct batadv_tt_req_node *
1538 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1539 struct batadv_orig_node *orig_node)
1541 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1543 spin_lock_bh(&bat_priv->tt.req_list_lock);
1544 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1545 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1546 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1547 BATADV_TT_REQUEST_TIMEOUT))
1551 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1555 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1556 tt_req_node->issued_at = jiffies;
1558 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1560 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1565 * batadv_tt_local_valid - verify that given tt entry is a valid one
1566 * @entry_ptr: to be checked local tt entry
1567 * @data_ptr: not used but definition required to satisfy the callback prototype
1569 * Returns 1 if the entry is a valid, 0 otherwise.
1571 static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
1573 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1575 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1580 static int batadv_tt_global_valid(const void *entry_ptr,
1581 const void *data_ptr)
1583 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1584 const struct batadv_tt_global_entry *tt_global_entry;
1585 const struct batadv_orig_node *orig_node = data_ptr;
1587 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1588 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1591 tt_global_entry = container_of(tt_common_entry,
1592 struct batadv_tt_global_entry,
1595 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1599 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
1600 * tt entries from the specified tt hash
1601 * @bat_priv: the bat priv with all the soft interface information
1602 * @hash: hash table containing the tt entries
1603 * @tt_len: expected tvlv tt data buffer length in number of bytes
1604 * @valid_cb: function to filter tt change entries
1605 * @cb_data: data passed to the filter function as argument
1607 * Returns pointer to allocated tvlv tt data buffer if operation was
1608 * successful or NULL otherwise.
1610 static struct batadv_tvlv_tt_data *
1611 batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
1612 struct batadv_hashtable *hash, uint16_t tt_len,
1613 int (*valid_cb)(const void *, const void *),
1616 struct batadv_tt_common_entry *tt_common_entry;
1617 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1618 struct batadv_tvlv_tt_change *tt_change;
1619 struct hlist_head *head;
1620 uint16_t tt_tot, tt_num_entries = 0;
1621 ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
1624 if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
1625 tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
1626 tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
1629 tt_tot = tt_len / sizeof(struct batadv_tvlv_tt_change);
1631 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1636 tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
1639 for (i = 0; i < hash->size; i++) {
1640 head = &hash->table[i];
1642 hlist_for_each_entry_rcu(tt_common_entry,
1644 if (tt_tot == tt_num_entries)
1647 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1650 memcpy(tt_change->addr, tt_common_entry->addr,
1652 tt_change->flags = tt_common_entry->flags;
1653 tt_change->reserved = 0;
1662 return tvlv_tt_data;
1665 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1666 struct batadv_orig_node *dst_orig_node,
1667 uint8_t ttvn, uint16_t tt_crc,
1670 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1671 struct batadv_hard_iface *primary_if;
1672 struct batadv_tt_req_node *tt_req_node = NULL;
1675 primary_if = batadv_primary_if_get_selected(bat_priv);
1679 /* The new tt_req will be issued only if I'm not waiting for a
1680 * reply from the same orig_node yet
1682 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1686 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
1690 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1691 tvlv_tt_data->ttvn = ttvn;
1692 tvlv_tt_data->crc = htons(tt_crc);
1695 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1697 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1698 dst_orig_node->orig, full_table ? 'F' : '.');
1700 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1701 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1702 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1703 tvlv_tt_data, sizeof(*tvlv_tt_data));
1708 batadv_hardif_free_ref(primary_if);
1709 if (ret && tt_req_node) {
1710 spin_lock_bh(&bat_priv->tt.req_list_lock);
1711 list_del(&tt_req_node->list);
1712 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1715 kfree(tvlv_tt_data);
1720 * batadv_send_other_tt_response - send reply to tt request concerning another
1721 * node's translation table
1722 * @bat_priv: the bat priv with all the soft interface information
1723 * @tt_data: tt data containing the tt request information
1724 * @req_src: mac address of tt request sender
1725 * @req_dst: mac address of tt request recipient
1727 * Returns true if tt request reply was sent, false otherwise.
1729 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1730 struct batadv_tvlv_tt_data *tt_data,
1731 uint8_t *req_src, uint8_t *req_dst)
1733 struct batadv_orig_node *req_dst_orig_node;
1734 struct batadv_orig_node *res_dst_orig_node = NULL;
1735 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1736 uint8_t orig_ttvn, req_ttvn;
1738 bool ret = false, full_table;
1740 batadv_dbg(BATADV_DBG_TT, bat_priv,
1741 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1742 req_src, tt_data->ttvn, req_dst,
1743 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1745 /* Let's get the orig node of the REAL destination */
1746 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
1747 if (!req_dst_orig_node)
1750 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
1751 if (!res_dst_orig_node)
1754 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1755 req_ttvn = tt_data->ttvn;
1757 /* this node doesn't have the requested data */
1758 if (orig_ttvn != req_ttvn ||
1759 tt_data->crc != htons(req_dst_orig_node->tt_crc))
1762 /* If the full table has been explicitly requested */
1763 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
1764 !req_dst_orig_node->tt_buff)
1769 /* TT fragmentation hasn't been implemented yet, so send as many
1770 * TT entries fit a single packet as possible only
1773 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1774 tt_len = req_dst_orig_node->tt_buff_len;
1776 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1781 /* Copy the last orig_node's OGM buffer */
1782 memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
1783 req_dst_orig_node->tt_buff_len);
1784 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1786 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1787 tt_len = batadv_tt_len(tt_len);
1789 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1790 bat_priv->tt.global_hash,
1792 batadv_tt_global_valid,
1798 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1799 tvlv_tt_data->ttvn = req_ttvn;
1802 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1804 batadv_dbg(BATADV_DBG_TT, bat_priv,
1805 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1806 res_dst_orig_node->orig, req_dst_orig_node->orig,
1807 full_table ? 'F' : '.', req_ttvn);
1809 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1811 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1812 req_src, BATADV_TVLV_TT, 1,
1813 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
1819 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1822 if (res_dst_orig_node)
1823 batadv_orig_node_free_ref(res_dst_orig_node);
1824 if (req_dst_orig_node)
1825 batadv_orig_node_free_ref(req_dst_orig_node);
1826 kfree(tvlv_tt_data);
1831 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1833 * @bat_priv: the bat priv with all the soft interface information
1834 * @tt_data: tt data containing the tt request information
1835 * @req_src: mac address of tt request sender
1837 * Returns true if tt request reply was sent, false otherwise.
1839 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1840 struct batadv_tvlv_tt_data *tt_data,
1843 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1844 struct batadv_orig_node *orig_node;
1845 struct batadv_hard_iface *primary_if = NULL;
1846 uint8_t my_ttvn, req_ttvn;
1850 batadv_dbg(BATADV_DBG_TT, bat_priv,
1851 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1852 req_src, tt_data->ttvn,
1853 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1856 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1857 req_ttvn = tt_data->ttvn;
1859 orig_node = batadv_orig_hash_find(bat_priv, req_src);
1863 primary_if = batadv_primary_if_get_selected(bat_priv);
1867 /* If the full table has been explicitly requested or the gap
1868 * is too big send the whole local translation table
1870 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1871 !bat_priv->tt.last_changeset)
1876 /* TT fragmentation hasn't been implemented yet, so send as many
1877 * TT entries fit a single packet as possible only
1880 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1881 tt_len = bat_priv->tt.last_changeset_len;
1883 tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
1888 /* Copy the last orig_node's OGM buffer */
1889 memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
1890 bat_priv->tt.last_changeset_len);
1891 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1893 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1894 tt_len = batadv_tt_len(tt_len);
1895 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1897 tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
1898 bat_priv->tt.local_hash,
1900 batadv_tt_local_valid,
1906 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1907 tvlv_tt_data->ttvn = req_ttvn;
1910 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1912 batadv_dbg(BATADV_DBG_TT, bat_priv,
1913 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
1914 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
1916 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1918 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1919 req_src, BATADV_TVLV_TT, 1,
1920 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
1925 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1928 batadv_orig_node_free_ref(orig_node);
1930 batadv_hardif_free_ref(primary_if);
1931 kfree(tvlv_tt_data);
1932 /* The packet was for this host, so it doesn't need to be re-routed */
1937 * batadv_send_tt_response - send reply to tt request
1938 * @bat_priv: the bat priv with all the soft interface information
1939 * @tt_data: tt data containing the tt request information
1940 * @req_src: mac address of tt request sender
1941 * @req_dst: mac address of tt request recipient
1943 * Returns true if tt request reply was sent, false otherwise.
1945 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1946 struct batadv_tvlv_tt_data *tt_data,
1947 uint8_t *req_src, uint8_t *req_dst)
1949 if (batadv_is_my_mac(bat_priv, req_dst)) {
1950 /* don't answer backbone gws! */
1951 if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
1954 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
1956 return batadv_send_other_tt_response(bat_priv, tt_data,
1961 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1962 struct batadv_orig_node *orig_node,
1963 struct batadv_tvlv_tt_change *tt_change,
1964 uint16_t tt_num_changes, uint8_t ttvn)
1969 for (i = 0; i < tt_num_changes; i++) {
1970 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1971 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1972 batadv_tt_global_del(bat_priv, orig_node,
1973 (tt_change + i)->addr,
1974 "tt removed by changes",
1977 if (!batadv_tt_global_add(bat_priv, orig_node,
1978 (tt_change + i)->addr,
1979 (tt_change + i)->flags, ttvn))
1980 /* In case of problem while storing a
1981 * global_entry, we stop the updating
1982 * procedure without committing the
1983 * ttvn change. This will avoid to send
1984 * corrupted data on tt_request
1989 orig_node->tt_initialised = true;
1992 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1993 struct batadv_tvlv_tt_data *tt_data,
1994 uint8_t *resp_src, uint16_t num_entries)
1996 struct batadv_orig_node *orig_node;
1998 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2002 /* Purge the old table first.. */
2003 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
2005 _batadv_tt_update_changes(bat_priv, orig_node,
2006 (struct batadv_tvlv_tt_change *)(tt_data + 1),
2007 num_entries, tt_data->ttvn);
2009 spin_lock_bh(&orig_node->tt_buff_lock);
2010 kfree(orig_node->tt_buff);
2011 orig_node->tt_buff_len = 0;
2012 orig_node->tt_buff = NULL;
2013 spin_unlock_bh(&orig_node->tt_buff_lock);
2015 atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
2019 batadv_orig_node_free_ref(orig_node);
2022 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2023 struct batadv_orig_node *orig_node,
2024 uint16_t tt_num_changes, uint8_t ttvn,
2025 struct batadv_tvlv_tt_change *tt_change)
2027 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2028 tt_num_changes, ttvn);
2030 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2031 (unsigned char *)tt_change, tt_num_changes);
2032 atomic_set(&orig_node->last_ttvn, ttvn);
2035 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2037 struct batadv_tt_local_entry *tt_local_entry;
2040 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2041 if (!tt_local_entry)
2043 /* Check if the client has been logically deleted (but is kept for
2044 * consistency purpose)
2046 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2047 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2052 batadv_tt_local_entry_free_ref(tt_local_entry);
2057 * batadv_handle_tt_response - process incoming tt reply
2058 * @bat_priv: the bat priv with all the soft interface information
2059 * @tt_data: tt data containing the tt request information
2060 * @resp_src: mac address of tt reply sender
2061 * @num_entries: number of tt change entries appended to the tt data
2063 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2064 struct batadv_tvlv_tt_data *tt_data,
2065 uint8_t *resp_src, uint16_t num_entries)
2067 struct batadv_tt_req_node *node, *safe;
2068 struct batadv_orig_node *orig_node = NULL;
2069 struct batadv_tvlv_tt_change *tt_change;
2071 batadv_dbg(BATADV_DBG_TT, bat_priv,
2072 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2073 resp_src, tt_data->ttvn, num_entries,
2074 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2076 /* we should have never asked a backbone gw */
2077 if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
2080 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2084 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2085 batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
2087 tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
2088 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2089 tt_data->ttvn, tt_change);
2092 /* Delete the tt_req_node from pending tt_requests list */
2093 spin_lock_bh(&bat_priv->tt.req_list_lock);
2094 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2095 if (!batadv_compare_eth(node->addr, resp_src))
2097 list_del(&node->list);
2100 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2102 /* Recalculate the CRC for this orig_node and store it */
2103 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2106 batadv_orig_node_free_ref(orig_node);
2109 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2111 struct batadv_tt_roam_node *node, *safe;
2113 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2115 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2116 list_del(&node->list);
2120 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2123 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2125 struct batadv_tt_roam_node *node, *safe;
2127 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2128 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2129 if (!batadv_has_timed_out(node->first_time,
2130 BATADV_ROAMING_MAX_TIME))
2133 list_del(&node->list);
2136 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2139 /* This function checks whether the client already reached the
2140 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2143 * returns true if the ROAMING_ADV can be sent, false otherwise
2145 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2148 struct batadv_tt_roam_node *tt_roam_node;
2151 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2152 /* The new tt_req will be issued only if I'm not waiting for a
2153 * reply from the same orig_node yet
2155 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2156 if (!batadv_compare_eth(tt_roam_node->addr, client))
2159 if (batadv_has_timed_out(tt_roam_node->first_time,
2160 BATADV_ROAMING_MAX_TIME))
2163 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2164 /* Sorry, you roamed too many times! */
2171 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2175 tt_roam_node->first_time = jiffies;
2176 atomic_set(&tt_roam_node->counter,
2177 BATADV_ROAMING_MAX_COUNT - 1);
2178 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2180 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2185 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2189 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2190 struct batadv_orig_node *orig_node)
2192 struct sk_buff *skb = NULL;
2193 struct batadv_roam_adv_packet *roam_adv_packet;
2195 struct batadv_hard_iface *primary_if;
2196 size_t len = sizeof(*roam_adv_packet);
2198 /* before going on we have to check whether the client has
2199 * already roamed to us too many times
2201 if (!batadv_tt_check_roam_count(bat_priv, client))
2204 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
2208 skb->priority = TC_PRIO_CONTROL;
2209 skb_reserve(skb, ETH_HLEN);
2211 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2213 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2214 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2215 roam_adv_packet->header.ttl = BATADV_TTL;
2216 roam_adv_packet->reserved = 0;
2217 primary_if = batadv_primary_if_get_selected(bat_priv);
2220 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2221 batadv_hardif_free_ref(primary_if);
2222 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2223 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2225 batadv_dbg(BATADV_DBG_TT, bat_priv,
2226 "Sending ROAMING_ADV to %pM (client %pM)\n",
2227 orig_node->orig, client);
2229 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2231 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
2240 static void batadv_tt_purge(struct work_struct *work)
2242 struct delayed_work *delayed_work;
2243 struct batadv_priv_tt *priv_tt;
2244 struct batadv_priv *bat_priv;
2246 delayed_work = container_of(work, struct delayed_work, work);
2247 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2248 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2250 batadv_tt_local_purge(bat_priv);
2251 batadv_tt_global_purge(bat_priv);
2252 batadv_tt_req_purge(bat_priv);
2253 batadv_tt_roam_purge(bat_priv);
2255 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2256 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2259 void batadv_tt_free(struct batadv_priv *bat_priv)
2261 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2262 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2264 cancel_delayed_work_sync(&bat_priv->tt.work);
2266 batadv_tt_local_table_free(bat_priv);
2267 batadv_tt_global_table_free(bat_priv);
2268 batadv_tt_req_list_free(bat_priv);
2269 batadv_tt_changes_list_free(bat_priv);
2270 batadv_tt_roam_list_free(bat_priv);
2272 kfree(bat_priv->tt.last_changeset);
2275 /* This function will enable or disable the specified flags for all the entries
2276 * in the given hash table and returns the number of modified entries
2278 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2279 uint16_t flags, bool enable)
2282 uint16_t changed_num = 0;
2283 struct hlist_head *head;
2284 struct batadv_tt_common_entry *tt_common_entry;
2289 for (i = 0; i < hash->size; i++) {
2290 head = &hash->table[i];
2293 hlist_for_each_entry_rcu(tt_common_entry,
2296 if ((tt_common_entry->flags & flags) == flags)
2298 tt_common_entry->flags |= flags;
2300 if (!(tt_common_entry->flags & flags))
2302 tt_common_entry->flags &= ~flags;
2312 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2313 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2315 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2316 struct batadv_tt_common_entry *tt_common;
2317 struct batadv_tt_local_entry *tt_local;
2318 struct hlist_node *node_tmp;
2319 struct hlist_head *head;
2320 spinlock_t *list_lock; /* protects write access to the hash lists */
2326 for (i = 0; i < hash->size; i++) {
2327 head = &hash->table[i];
2328 list_lock = &hash->list_locks[i];
2330 spin_lock_bh(list_lock);
2331 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2333 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2336 batadv_dbg(BATADV_DBG_TT, bat_priv,
2337 "Deleting local tt entry (%pM): pending\n",
2340 atomic_dec(&bat_priv->tt.local_entry_num);
2341 hlist_del_rcu(&tt_common->hash_entry);
2342 tt_local = container_of(tt_common,
2343 struct batadv_tt_local_entry,
2345 batadv_tt_local_entry_free_ref(tt_local);
2347 spin_unlock_bh(list_lock);
2352 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2353 * have been queued in the time since the last commit
2354 * @bat_priv: the bat priv with all the soft interface information
2356 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
2358 uint16_t changed_num = 0;
2360 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2361 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2362 batadv_tt_tvlv_container_update(bat_priv);
2366 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2367 BATADV_TT_CLIENT_NEW, false);
2369 /* all reset entries have to be counted as local entries */
2370 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2371 batadv_tt_local_purge_pending_clients(bat_priv);
2372 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2374 /* Increment the TTVN only once per OGM interval */
2375 atomic_inc(&bat_priv->tt.vn);
2376 batadv_dbg(BATADV_DBG_TT, bat_priv,
2377 "Local changes committed, updating to ttvn %u\n",
2378 (uint8_t)atomic_read(&bat_priv->tt.vn));
2380 /* reset the sending counter */
2381 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2382 batadv_tt_tvlv_container_update(bat_priv);
2385 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2388 struct batadv_tt_local_entry *tt_local_entry = NULL;
2389 struct batadv_tt_global_entry *tt_global_entry = NULL;
2392 if (!atomic_read(&bat_priv->ap_isolation))
2395 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2396 if (!tt_local_entry)
2399 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2400 if (!tt_global_entry)
2403 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2409 if (tt_global_entry)
2410 batadv_tt_global_entry_free_ref(tt_global_entry);
2412 batadv_tt_local_entry_free_ref(tt_local_entry);
2417 * batadv_tt_update_orig - update global translation table with new tt
2418 * information received via ogms
2419 * @bat_priv: the bat priv with all the soft interface information
2420 * @orig: the orig_node of the ogm
2421 * @tt_buff: buffer holding the tt information
2422 * @tt_num_changes: number of tt changes inside the tt buffer
2423 * @ttvn: translation table version number of this changeset
2424 * @tt_crc: crc16 checksum of orig node's translation table
2426 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2427 struct batadv_orig_node *orig_node,
2428 const unsigned char *tt_buff,
2429 uint16_t tt_num_changes, uint8_t ttvn,
2432 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2433 bool full_table = true;
2434 struct batadv_tvlv_tt_change *tt_change;
2436 /* don't care about a backbone gateways updates. */
2437 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2440 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2441 * increased by one -> we can apply the attached changes
2443 if ((!orig_node->tt_initialised && ttvn == 1) ||
2444 ttvn - orig_ttvn == 1) {
2445 /* the OGM could not contain the changes due to their size or
2446 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2448 * In this case send a tt request
2450 if (!tt_num_changes) {
2455 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
2456 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2459 /* Even if we received the precomputed crc with the OGM, we
2460 * prefer to recompute it to spot any possible inconsistency
2461 * in the global table
2463 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2465 /* The ttvn alone is not enough to guarantee consistency
2466 * because a single value could represent different states
2467 * (due to the wrap around). Thus a node has to check whether
2468 * the resulting table (after applying the changes) is still
2469 * consistent or not. E.g. a node could disconnect while its
2470 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2471 * checking the CRC value is mandatory to detect the
2474 if (orig_node->tt_crc != tt_crc)
2477 /* if we missed more than one change or our tables are not
2478 * in sync anymore -> request fresh tt data
2480 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2481 orig_node->tt_crc != tt_crc) {
2483 batadv_dbg(BATADV_DBG_TT, bat_priv,
2484 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2485 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2486 orig_node->tt_crc, tt_num_changes);
2487 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2488 tt_crc, full_table);
2494 /* returns true whether we know that the client has moved from its old
2495 * originator to another one. This entry is kept is still kept for consistency
2498 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2501 struct batadv_tt_global_entry *tt_global_entry;
2504 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2505 if (!tt_global_entry)
2508 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2509 batadv_tt_global_entry_free_ref(tt_global_entry);
2515 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2516 * @bat_priv: the bat priv with all the soft interface information
2517 * @addr: the MAC address of the local client to query
2519 * Returns true if the local client is known to be roaming (it is not served by
2520 * this node anymore) or not. If yes, the client is still present in the table
2521 * to keep the latter consistent with the node TTVN
2523 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2526 struct batadv_tt_local_entry *tt_local_entry;
2529 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2530 if (!tt_local_entry)
2533 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2534 batadv_tt_local_entry_free_ref(tt_local_entry);
2539 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2540 struct batadv_orig_node *orig_node,
2541 const unsigned char *addr)
2545 /* if the originator is a backbone node (meaning it belongs to the same
2546 * LAN of this node) the temporary client must not be added because to
2547 * reach such destination the node must use the LAN instead of the mesh
2549 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2552 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2553 BATADV_TT_CLIENT_TEMP,
2554 atomic_read(&orig_node->last_ttvn)))
2557 batadv_dbg(BATADV_DBG_TT, bat_priv,
2558 "Added temporary global client (addr: %pM orig: %pM)\n",
2559 addr, orig_node->orig);
2566 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
2567 * @bat_priv: the bat priv with all the soft interface information
2568 * @orig: the orig_node of the ogm
2569 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
2570 * @tvlv_value: tvlv buffer containing the gateway data
2571 * @tvlv_value_len: tvlv buffer length
2573 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
2574 struct batadv_orig_node *orig,
2577 uint16_t tvlv_value_len)
2579 struct batadv_tvlv_tt_data *tt_data;
2580 uint16_t num_entries;
2582 if (tvlv_value_len < sizeof(*tt_data))
2585 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2586 tvlv_value_len -= sizeof(*tt_data);
2588 num_entries = tvlv_value_len / batadv_tt_len(1);
2590 batadv_tt_update_orig(bat_priv, orig,
2591 (unsigned char *)(tt_data + 1),
2592 num_entries, tt_data->ttvn, ntohs(tt_data->crc));
2596 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
2598 * @bat_priv: the bat priv with all the soft interface information
2599 * @src: mac address of tt tvlv sender
2600 * @dst: mac address of tt tvlv recipient
2601 * @tvlv_value: tvlv buffer containing the tt data
2602 * @tvlv_value_len: tvlv buffer length
2604 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
2607 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
2608 uint8_t *src, uint8_t *dst,
2610 uint16_t tvlv_value_len)
2612 struct batadv_tvlv_tt_data *tt_data;
2613 uint16_t num_entries;
2617 if (tvlv_value_len < sizeof(*tt_data))
2618 return NET_RX_SUCCESS;
2620 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
2621 tvlv_value_len -= sizeof(*tt_data);
2623 num_entries = tvlv_value_len / batadv_tt_len(1);
2625 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
2626 case BATADV_TT_REQUEST:
2627 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
2629 /* If this node cannot provide a TT response the tt_request is
2632 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
2634 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2639 batadv_dbg(BATADV_DBG_TT, bat_priv,
2640 "Routing TT_REQUEST to %pM [%c]\n",
2642 /* tvlv API will re-route the packet */
2646 case BATADV_TT_RESPONSE:
2647 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
2649 if (batadv_is_my_mac(bat_priv, dst)) {
2650 batadv_handle_tt_response(bat_priv, tt_data,
2652 return NET_RX_SUCCESS;
2655 if (tt_data->flags & BATADV_TT_FULL_TABLE)
2660 batadv_dbg(BATADV_DBG_TT, bat_priv,
2661 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
2663 /* tvlv API will re-route the packet */
2667 return NET_RX_SUCCESS;
2671 * batadv_tt_init - initialise the translation table internals
2672 * @bat_priv: the bat priv with all the soft interface information
2674 * Return 0 on success or negative error number in case of failure.
2676 int batadv_tt_init(struct batadv_priv *bat_priv)
2680 ret = batadv_tt_local_init(bat_priv);
2684 ret = batadv_tt_global_init(bat_priv);
2688 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
2689 batadv_tt_tvlv_unicast_handler_v1,
2690 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
2692 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2693 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2694 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));