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/crc32c.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,
38 struct batadv_orig_node *orig_node);
39 static void batadv_tt_purge(struct work_struct *work);
41 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
42 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
43 struct batadv_orig_node *orig_node,
44 const unsigned char *addr,
45 unsigned short vid, const char *message,
48 /* returns 1 if they are the same mac addr */
49 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
51 const void *data1 = container_of(node, struct batadv_tt_common_entry,
54 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
58 * batadv_choose_tt - return the index of the tt entry in the hash table
59 * @data: pointer to the tt_common_entry object to map
60 * @size: the size of the hash table
62 * Returns the hash index where the object represented by 'data' should be
65 static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
67 struct batadv_tt_common_entry *tt;
70 tt = (struct batadv_tt_common_entry *)data;
71 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
72 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
82 * batadv_tt_hash_find - look for a client in the given hash table
83 * @hash: the hash table to search
84 * @addr: the mac address of the client to look for
85 * @vid: VLAN identifier
87 * Returns a pointer to the tt_common struct belonging to the searched client if
88 * found, NULL otherwise.
90 static struct batadv_tt_common_entry *
91 batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
94 struct hlist_head *head;
95 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
101 memcpy(to_search.addr, addr, ETH_ALEN);
104 index = batadv_choose_tt(&to_search, hash->size);
105 head = &hash->table[index];
108 hlist_for_each_entry_rcu(tt, head, hash_entry) {
109 if (!batadv_compare_eth(tt, addr))
115 if (!atomic_inc_not_zero(&tt->refcount))
127 * batadv_tt_local_hash_find - search the local table for a given client
128 * @bat_priv: the bat priv with all the soft interface information
129 * @addr: the mac address of the client to look for
130 * @vid: VLAN identifier
132 * Returns a pointer to the corresponding tt_local_entry struct if the client is
133 * found, NULL otherwise.
135 static struct batadv_tt_local_entry *
136 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
139 struct batadv_tt_common_entry *tt_common_entry;
140 struct batadv_tt_local_entry *tt_local_entry = NULL;
142 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
145 tt_local_entry = container_of(tt_common_entry,
146 struct batadv_tt_local_entry,
148 return tt_local_entry;
152 * batadv_tt_global_hash_find - search the global table for a given client
153 * @bat_priv: the bat priv with all the soft interface information
154 * @addr: the mac address of the client to look for
155 * @vid: VLAN identifier
157 * Returns a pointer to the corresponding tt_global_entry struct if the client
158 * is found, NULL otherwise.
160 static struct batadv_tt_global_entry *
161 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
164 struct batadv_tt_common_entry *tt_common_entry;
165 struct batadv_tt_global_entry *tt_global_entry = NULL;
167 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
170 tt_global_entry = container_of(tt_common_entry,
171 struct batadv_tt_global_entry,
173 return tt_global_entry;
177 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
179 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
180 kfree_rcu(tt_local_entry, common.rcu);
184 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
185 * tt_global_entry and possibly free it
186 * @tt_global_entry: the object to free
189 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
191 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
192 batadv_tt_global_del_orig_list(tt_global_entry);
193 kfree_rcu(tt_global_entry, common.rcu);
197 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
199 struct batadv_tt_orig_list_entry *orig_entry;
201 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
203 /* We are in an rcu callback here, therefore we cannot use
204 * batadv_orig_node_free_ref() and its call_rcu():
205 * An rcu_barrier() wouldn't wait for that to finish
207 batadv_orig_node_free_ref_now(orig_entry->orig_node);
212 * batadv_tt_local_size_mod - change the size by v of the local table identified
214 * @bat_priv: the bat priv with all the soft interface information
215 * @vid: the VLAN identifier of the sub-table to change
216 * @v: the amount to sum to the local table size
218 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
219 unsigned short vid, int v)
221 struct batadv_softif_vlan *vlan;
223 vlan = batadv_softif_vlan_get(bat_priv, vid);
227 atomic_add(v, &vlan->tt.num_entries);
229 batadv_softif_vlan_free_ref(vlan);
233 * batadv_tt_local_size_inc - increase by one the local table size for the given
235 * @bat_priv: the bat priv with all the soft interface information
236 * @vid: the VLAN identifier
238 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
241 batadv_tt_local_size_mod(bat_priv, vid, 1);
245 * batadv_tt_local_size_dec - decrease by one the local table size for the given
247 * @bat_priv: the bat priv with all the soft interface information
248 * @vid: the VLAN identifier
250 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
253 batadv_tt_local_size_mod(bat_priv, vid, -1);
257 * batadv_tt_global_size_mod - change the size by v of the local table
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
261 * @v: the amount to sum to the global table size
263 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
264 unsigned short vid, int v)
266 struct batadv_orig_node_vlan *vlan;
268 vlan = batadv_orig_node_vlan_new(orig_node, vid);
272 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
273 spin_lock_bh(&orig_node->vlan_list_lock);
274 list_del_rcu(&vlan->list);
275 spin_unlock_bh(&orig_node->vlan_list_lock);
276 batadv_orig_node_vlan_free_ref(vlan);
279 batadv_orig_node_vlan_free_ref(vlan);
283 * batadv_tt_global_size_inc - increase by one the global table size for the
285 * @orig_node: the originator which global table size has to be decreased
286 * @vid: the vlan identifier
288 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
291 batadv_tt_global_size_mod(orig_node, vid, 1);
295 * batadv_tt_global_size_dec - decrease by one the global table size for the
297 * @orig_node: the originator which global table size has to be decreased
298 * @vid: the vlan identifier
300 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
303 batadv_tt_global_size_mod(orig_node, vid, -1);
307 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
309 if (!atomic_dec_and_test(&orig_entry->refcount))
312 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
316 * batadv_tt_local_event - store a local TT event (ADD/DEL)
317 * @bat_priv: the bat priv with all the soft interface information
318 * @tt_local_entry: the TT entry involved in the event
319 * @event_flags: flags to store in the event structure
321 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
322 struct batadv_tt_local_entry *tt_local_entry,
325 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
326 struct batadv_tt_common_entry *common = &tt_local_entry->common;
327 uint8_t flags = common->flags | event_flags;
328 bool event_removed = false;
329 bool del_op_requested, del_op_entry;
331 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
335 tt_change_node->change.flags = flags;
336 tt_change_node->change.reserved = 0;
337 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
338 tt_change_node->change.vid = htons(common->vid);
340 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
342 /* check for ADD+DEL or DEL+ADD events */
343 spin_lock_bh(&bat_priv->tt.changes_list_lock);
344 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
346 if (!batadv_compare_eth(entry->change.addr, common->addr))
349 /* DEL+ADD in the same orig interval have no effect and can be
350 * removed to avoid silly behaviour on the receiver side. The
351 * other way around (ADD+DEL) can happen in case of roaming of
352 * a client still in the NEW state. Roaming of NEW clients is
353 * now possible due to automatically recognition of "temporary"
356 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
357 if (!del_op_requested && del_op_entry)
359 if (del_op_requested && !del_op_entry)
363 list_del(&entry->list);
365 kfree(tt_change_node);
366 event_removed = true;
370 /* track the change in the OGMinterval list */
371 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
374 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
377 atomic_dec(&bat_priv->tt.local_changes);
379 atomic_inc(&bat_priv->tt.local_changes);
383 * batadv_tt_len - compute length in bytes of given number of tt changes
384 * @changes_num: number of tt changes
386 * Returns computed length in bytes.
388 static int batadv_tt_len(int changes_num)
390 return changes_num * sizeof(struct batadv_tvlv_tt_change);
394 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
395 * @tt_len: available space
397 * Returns the number of entries.
399 static uint16_t batadv_tt_entries(uint16_t tt_len)
401 return tt_len / batadv_tt_len(1);
405 * batadv_tt_local_table_transmit_size - calculates the local translation table
406 * size when transmitted over the air
407 * @bat_priv: the bat priv with all the soft interface information
409 * Returns local translation table size in bytes.
411 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
413 uint16_t num_vlan = 0, tt_local_entries = 0;
414 struct batadv_softif_vlan *vlan;
418 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
420 tt_local_entries += atomic_read(&vlan->tt.num_entries);
424 /* header size of tvlv encapsulated tt response payload */
425 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
426 hdr_size += sizeof(struct batadv_tvlv_hdr);
427 hdr_size += sizeof(struct batadv_tvlv_tt_data);
428 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
430 return hdr_size + batadv_tt_len(tt_local_entries);
433 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
435 if (bat_priv->tt.local_hash)
438 bat_priv->tt.local_hash = batadv_hash_new(1024);
440 if (!bat_priv->tt.local_hash)
443 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
444 &batadv_tt_local_hash_lock_class_key);
449 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
450 struct batadv_tt_global_entry *tt_global,
453 batadv_dbg(BATADV_DBG_TT, bat_priv,
454 "Deleting global tt entry %pM (vid: %d): %s\n",
455 tt_global->common.addr,
456 BATADV_PRINT_VID(tt_global->common.vid), message);
458 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
459 batadv_choose_tt, &tt_global->common);
460 batadv_tt_global_entry_free_ref(tt_global);
464 * batadv_tt_local_add - add a new client to the local table or update an
466 * @soft_iface: netdev struct of the mesh interface
467 * @addr: the mac address of the client to add
468 * @vid: VLAN identifier
469 * @ifindex: index of the interface where the client is connected to (useful to
470 * identify wireless clients)
472 * Returns true if the client was successfully added, false otherwise.
474 bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
475 unsigned short vid, int ifindex)
477 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
478 struct batadv_tt_local_entry *tt_local;
479 struct batadv_tt_global_entry *tt_global;
480 struct hlist_head *head;
481 struct batadv_tt_orig_list_entry *orig_entry;
482 int hash_added, table_size, packet_size_max;
483 bool ret = false, roamed_back = false;
485 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
486 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
489 tt_local->last_seen = jiffies;
490 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
491 batadv_dbg(BATADV_DBG_TT, bat_priv,
492 "Re-adding pending client %pM (vid: %d)\n",
493 addr, BATADV_PRINT_VID(vid));
494 /* whatever the reason why the PENDING flag was set,
495 * this is a client which was enqueued to be removed in
496 * this orig_interval. Since it popped up again, the
497 * flag can be reset like it was never enqueued
499 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
503 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
504 batadv_dbg(BATADV_DBG_TT, bat_priv,
505 "Roaming client %pM (vid: %d) came back to its original location\n",
506 addr, BATADV_PRINT_VID(vid));
507 /* the ROAM flag is set because this client roamed away
508 * and the node got a roaming_advertisement message. Now
509 * that the client popped up again at its original
510 * location such flag can be unset
512 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
518 /* Ignore the client if we cannot send it in a full table response. */
519 table_size = batadv_tt_local_table_transmit_size(bat_priv);
520 table_size += batadv_tt_len(1);
521 packet_size_max = atomic_read(&bat_priv->packet_size_max);
522 if (table_size > packet_size_max) {
523 net_ratelimited_function(batadv_info, soft_iface,
524 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
525 table_size, packet_size_max, addr);
529 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
533 batadv_dbg(BATADV_DBG_TT, bat_priv,
534 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
535 addr, BATADV_PRINT_VID(vid),
536 (uint8_t)atomic_read(&bat_priv->tt.vn));
538 memcpy(tt_local->common.addr, addr, ETH_ALEN);
539 /* The local entry has to be marked as NEW to avoid to send it in
540 * a full table response going out before the next ttvn increment
541 * (consistency check)
543 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
544 tt_local->common.vid = vid;
545 if (batadv_is_wifi_iface(ifindex))
546 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
547 atomic_set(&tt_local->common.refcount, 2);
548 tt_local->last_seen = jiffies;
549 tt_local->common.added_at = tt_local->last_seen;
551 /* the batman interface mac address should never be purged */
552 if (batadv_compare_eth(addr, soft_iface->dev_addr))
553 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
555 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
556 batadv_choose_tt, &tt_local->common,
557 &tt_local->common.hash_entry);
559 if (unlikely(hash_added != 0)) {
560 /* remove the reference for the hash */
561 batadv_tt_local_entry_free_ref(tt_local);
566 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
569 /* Check whether it is a roaming, but don't do anything if the roaming
570 * process has already been handled
572 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
573 /* These node are probably going to update their tt table */
574 head = &tt_global->orig_list;
576 hlist_for_each_entry_rcu(orig_entry, head, list) {
577 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
578 tt_global->common.vid,
579 orig_entry->orig_node);
583 batadv_tt_global_free(bat_priv, tt_global,
587 /* The global entry has to be marked as ROAMING and
588 * has to be kept for consistency purpose
590 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
591 tt_global->roam_at = jiffies;
599 batadv_tt_local_entry_free_ref(tt_local);
601 batadv_tt_global_entry_free_ref(tt_global);
606 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
607 * within a TT Response directed to another node
608 * @orig_node: originator for which the TT data has to be prepared
609 * @tt_data: uninitialised pointer to the address of the TVLV buffer
610 * @tt_change: uninitialised pointer to the address of the area where the TT
611 * changed can be stored
612 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
613 * function reserves the amount of space needed to send the entire global TT
614 * table. In case of success the value is updated with the real amount of
617 * Allocate the needed amount of memory for the entire TT TVLV and write its
618 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
619 * objects, one per active VLAN served by the originator node.
621 * Return the size of the allocated buffer or 0 in case of failure.
624 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
625 struct batadv_tvlv_tt_data **tt_data,
626 struct batadv_tvlv_tt_change **tt_change,
629 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
630 struct batadv_tvlv_tt_vlan_data *tt_vlan;
631 struct batadv_orig_node_vlan *vlan;
632 uint8_t *tt_change_ptr;
635 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
637 num_entries += atomic_read(&vlan->tt.num_entries);
640 change_offset = sizeof(**tt_data);
641 change_offset += num_vlan * sizeof(*tt_vlan);
643 /* if tt_len is negative, allocate the space needed by the full table */
645 *tt_len = batadv_tt_len(num_entries);
648 tvlv_len += change_offset;
650 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
656 (*tt_data)->flags = BATADV_NO_FLAGS;
657 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
658 (*tt_data)->num_vlan = htons(num_vlan);
660 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
661 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
662 tt_vlan->vid = htons(vlan->vid);
663 tt_vlan->crc = htonl(vlan->tt.crc);
668 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
669 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
677 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
679 * @bat_priv: the bat priv with all the soft interface information
680 * @tt_data: uninitialised pointer to the address of the TVLV buffer
681 * @tt_change: uninitialised pointer to the address of the area where the TT
682 * changes can be stored
683 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
684 * function reserves the amount of space needed to send the entire local TT
685 * table. In case of success the value is updated with the real amount of
688 * Allocate the needed amount of memory for the entire TT TVLV and write its
689 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
690 * objects, one per active VLAN.
692 * Return the size of the allocated buffer or 0 in case of failure.
695 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
696 struct batadv_tvlv_tt_data **tt_data,
697 struct batadv_tvlv_tt_change **tt_change,
700 struct batadv_tvlv_tt_vlan_data *tt_vlan;
701 struct batadv_softif_vlan *vlan;
702 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
703 uint8_t *tt_change_ptr;
707 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
709 num_entries += atomic_read(&vlan->tt.num_entries);
712 change_offset = sizeof(**tt_data);
713 change_offset += num_vlan * sizeof(*tt_vlan);
715 /* if tt_len is negative, allocate the space needed by the full table */
717 *tt_len = batadv_tt_len(num_entries);
720 tvlv_len += change_offset;
722 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
728 (*tt_data)->flags = BATADV_NO_FLAGS;
729 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
730 (*tt_data)->num_vlan = htons(num_vlan);
732 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
733 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
734 tt_vlan->vid = htons(vlan->vid);
735 tt_vlan->crc = htonl(vlan->tt.crc);
740 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
741 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
749 * batadv_tt_tvlv_container_update - update the translation table tvlv container
750 * after local tt changes have been committed
751 * @bat_priv: the bat priv with all the soft interface information
753 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
755 struct batadv_tt_change_node *entry, *safe;
756 struct batadv_tvlv_tt_data *tt_data;
757 struct batadv_tvlv_tt_change *tt_change;
758 int tt_diff_len, tt_change_len = 0;
759 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
762 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
763 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
765 /* if we have too many changes for one packet don't send any
766 * and wait for the tt table request which will be fragmented
768 if (tt_diff_len > bat_priv->soft_iface->mtu)
771 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
772 &tt_change, &tt_diff_len);
776 tt_data->flags = BATADV_TT_OGM_DIFF;
778 if (tt_diff_len == 0)
779 goto container_register;
781 spin_lock_bh(&bat_priv->tt.changes_list_lock);
782 atomic_set(&bat_priv->tt.local_changes, 0);
784 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
786 if (tt_diff_entries_count < tt_diff_entries_num) {
787 memcpy(tt_change + tt_diff_entries_count,
789 sizeof(struct batadv_tvlv_tt_change));
790 tt_diff_entries_count++;
792 list_del(&entry->list);
795 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
797 /* Keep the buffer for possible tt_request */
798 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
799 kfree(bat_priv->tt.last_changeset);
800 bat_priv->tt.last_changeset_len = 0;
801 bat_priv->tt.last_changeset = NULL;
802 tt_change_len = batadv_tt_len(tt_diff_entries_count);
803 /* check whether this new OGM has no changes due to size problems */
804 if (tt_diff_entries_count > 0) {
805 /* if kmalloc() fails we will reply with the full table
806 * instead of providing the diff
808 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
809 if (bat_priv->tt.last_changeset) {
810 memcpy(bat_priv->tt.last_changeset,
811 tt_change, tt_change_len);
812 bat_priv->tt.last_changeset_len = tt_diff_len;
815 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
818 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
823 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
825 struct net_device *net_dev = (struct net_device *)seq->private;
826 struct batadv_priv *bat_priv = netdev_priv(net_dev);
827 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
828 struct batadv_tt_common_entry *tt_common_entry;
829 struct batadv_tt_local_entry *tt_local;
830 struct batadv_hard_iface *primary_if;
831 struct batadv_softif_vlan *vlan;
832 struct hlist_head *head;
837 unsigned long last_seen_jiffies;
839 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
841 primary_if = batadv_seq_print_text_primary_if_get(seq);
846 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
847 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
848 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
849 "Flags", "Last seen", "CRC");
851 for (i = 0; i < hash->size; i++) {
852 head = &hash->table[i];
855 hlist_for_each_entry_rcu(tt_common_entry,
857 tt_local = container_of(tt_common_entry,
858 struct batadv_tt_local_entry,
860 vid = tt_common_entry->vid;
861 last_seen_jiffies = jiffies - tt_local->last_seen;
862 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
863 last_seen_secs = last_seen_msecs / 1000;
864 last_seen_msecs = last_seen_msecs % 1000;
866 no_purge = tt_common_entry->flags & np_flag;
868 vlan = batadv_softif_vlan_get(bat_priv, vid);
870 seq_printf(seq, "Cannot retrieve VLAN %d\n",
871 BATADV_PRINT_VID(vid));
876 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
877 tt_common_entry->addr,
878 BATADV_PRINT_VID(tt_common_entry->vid),
879 (tt_common_entry->flags &
880 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
881 no_purge ? 'P' : '.',
882 (tt_common_entry->flags &
883 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
884 (tt_common_entry->flags &
885 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
886 (tt_common_entry->flags &
887 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
888 no_purge ? 0 : last_seen_secs,
889 no_purge ? 0 : last_seen_msecs,
892 batadv_softif_vlan_free_ref(vlan);
898 batadv_hardif_free_ref(primary_if);
903 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
904 struct batadv_tt_local_entry *tt_local_entry,
905 uint16_t flags, const char *message)
907 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
909 /* The local client has to be marked as "pending to be removed" but has
910 * to be kept in the table in order to send it in a full table
911 * response issued before the net ttvn increment (consistency check)
913 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
915 batadv_dbg(BATADV_DBG_TT, bat_priv,
916 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
917 tt_local_entry->common.addr,
918 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
922 * batadv_tt_local_remove - logically remove an entry from the local table
923 * @bat_priv: the bat priv with all the soft interface information
924 * @addr: the MAC address of the client to remove
925 * @vid: VLAN identifier
926 * @message: message to append to the log on deletion
927 * @roaming: true if the deletion is due to a roaming event
929 * Returns the flags assigned to the local entry before being deleted
931 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
932 const uint8_t *addr, unsigned short vid,
933 const char *message, bool roaming)
935 struct batadv_tt_local_entry *tt_local_entry;
936 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
938 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
942 curr_flags = tt_local_entry->common.flags;
944 flags = BATADV_TT_CLIENT_DEL;
945 /* if this global entry addition is due to a roaming, the node has to
946 * mark the local entry as "roamed" in order to correctly reroute
950 flags |= BATADV_TT_CLIENT_ROAM;
951 /* mark the local client as ROAMed */
952 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
955 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
956 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
960 /* if this client has been added right now, it is possible to
961 * immediately purge it
963 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
964 hlist_del_rcu(&tt_local_entry->common.hash_entry);
965 batadv_tt_local_entry_free_ref(tt_local_entry);
969 batadv_tt_local_entry_free_ref(tt_local_entry);
975 * batadv_tt_local_purge_list - purge inactive tt local entries
976 * @bat_priv: the bat priv with all the soft interface information
977 * @head: pointer to the list containing the local tt entries
978 * @timeout: parameter deciding whether a given tt local entry is considered
981 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
982 struct hlist_head *head,
985 struct batadv_tt_local_entry *tt_local_entry;
986 struct batadv_tt_common_entry *tt_common_entry;
987 struct hlist_node *node_tmp;
989 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
991 tt_local_entry = container_of(tt_common_entry,
992 struct batadv_tt_local_entry,
994 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
997 /* entry already marked for deletion */
998 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1001 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1004 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1005 BATADV_TT_CLIENT_DEL, "timed out");
1010 * batadv_tt_local_purge - purge inactive tt local entries
1011 * @bat_priv: the bat priv with all the soft interface information
1012 * @timeout: parameter deciding whether a given tt local entry is considered
1015 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1018 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1019 struct hlist_head *head;
1020 spinlock_t *list_lock; /* protects write access to the hash lists */
1023 for (i = 0; i < hash->size; i++) {
1024 head = &hash->table[i];
1025 list_lock = &hash->list_locks[i];
1027 spin_lock_bh(list_lock);
1028 batadv_tt_local_purge_list(bat_priv, head, timeout);
1029 spin_unlock_bh(list_lock);
1033 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1035 struct batadv_hashtable *hash;
1036 spinlock_t *list_lock; /* protects write access to the hash lists */
1037 struct batadv_tt_common_entry *tt_common_entry;
1038 struct batadv_tt_local_entry *tt_local;
1039 struct hlist_node *node_tmp;
1040 struct hlist_head *head;
1043 if (!bat_priv->tt.local_hash)
1046 hash = bat_priv->tt.local_hash;
1048 for (i = 0; i < hash->size; i++) {
1049 head = &hash->table[i];
1050 list_lock = &hash->list_locks[i];
1052 spin_lock_bh(list_lock);
1053 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1055 hlist_del_rcu(&tt_common_entry->hash_entry);
1056 tt_local = container_of(tt_common_entry,
1057 struct batadv_tt_local_entry,
1059 batadv_tt_local_entry_free_ref(tt_local);
1061 spin_unlock_bh(list_lock);
1064 batadv_hash_destroy(hash);
1066 bat_priv->tt.local_hash = NULL;
1069 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1071 if (bat_priv->tt.global_hash)
1074 bat_priv->tt.global_hash = batadv_hash_new(1024);
1076 if (!bat_priv->tt.global_hash)
1079 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1080 &batadv_tt_global_hash_lock_class_key);
1085 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1087 struct batadv_tt_change_node *entry, *safe;
1089 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1091 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1093 list_del(&entry->list);
1097 atomic_set(&bat_priv->tt.local_changes, 0);
1098 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1101 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1102 * batadv_tt_global_entry list
1104 * returns it with an increased refcounter, NULL if not found
1106 static struct batadv_tt_orig_list_entry *
1107 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1108 const struct batadv_orig_node *orig_node)
1110 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1111 const struct hlist_head *head;
1114 head = &entry->orig_list;
1115 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1116 if (tmp_orig_entry->orig_node != orig_node)
1118 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1121 orig_entry = tmp_orig_entry;
1129 /* find out if an orig_node is already in the list of a tt_global_entry.
1130 * returns true if found, false otherwise
1133 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1134 const struct batadv_orig_node *orig_node)
1136 struct batadv_tt_orig_list_entry *orig_entry;
1139 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1142 batadv_tt_orig_list_entry_free_ref(orig_entry);
1149 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1150 struct batadv_orig_node *orig_node, int ttvn)
1152 struct batadv_tt_orig_list_entry *orig_entry;
1154 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1156 /* refresh the ttvn: the current value could be a bogus one that
1157 * was added during a "temporary client detection"
1159 orig_entry->ttvn = ttvn;
1163 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1167 INIT_HLIST_NODE(&orig_entry->list);
1168 atomic_inc(&orig_node->refcount);
1169 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1170 orig_entry->orig_node = orig_node;
1171 orig_entry->ttvn = ttvn;
1172 atomic_set(&orig_entry->refcount, 2);
1174 spin_lock_bh(&tt_global->list_lock);
1175 hlist_add_head_rcu(&orig_entry->list,
1176 &tt_global->orig_list);
1177 spin_unlock_bh(&tt_global->list_lock);
1180 batadv_tt_orig_list_entry_free_ref(orig_entry);
1184 * batadv_tt_global_add - add a new TT global entry or update an existing one
1185 * @bat_priv: the bat priv with all the soft interface information
1186 * @orig_node: the originator announcing the client
1187 * @tt_addr: the mac address of the non-mesh client
1188 * @vid: VLAN identifier
1189 * @flags: TT flags that have to be set for this non-mesh client
1190 * @ttvn: the tt version number ever announcing this non-mesh client
1192 * Add a new TT global entry for the given originator. If the entry already
1193 * exists add a new reference to the given originator (a global entry can have
1194 * references to multiple originators) and adjust the flags attribute to reflect
1195 * the function argument.
1196 * If a TT local entry exists for this non-mesh client remove it.
1198 * The caller must hold orig_node refcount.
1200 * Return true if the new entry has been added, false otherwise
1202 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1203 struct batadv_orig_node *orig_node,
1204 const unsigned char *tt_addr,
1205 unsigned short vid, uint16_t flags,
1208 struct batadv_tt_global_entry *tt_global_entry;
1209 struct batadv_tt_local_entry *tt_local_entry;
1212 struct batadv_tt_common_entry *common;
1213 uint16_t local_flags;
1215 /* ignore global entries from backbone nodes */
1216 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1219 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1220 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1222 /* if the node already has a local client for this entry, it has to wait
1223 * for a roaming advertisement instead of manually messing up the global
1226 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1227 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1230 if (!tt_global_entry) {
1231 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1232 if (!tt_global_entry)
1235 common = &tt_global_entry->common;
1236 memcpy(common->addr, tt_addr, ETH_ALEN);
1239 common->flags = flags;
1240 tt_global_entry->roam_at = 0;
1241 /* node must store current time in case of roaming. This is
1242 * needed to purge this entry out on timeout (if nobody claims
1245 if (flags & BATADV_TT_CLIENT_ROAM)
1246 tt_global_entry->roam_at = jiffies;
1247 atomic_set(&common->refcount, 2);
1248 common->added_at = jiffies;
1250 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1251 spin_lock_init(&tt_global_entry->list_lock);
1253 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1255 batadv_choose_tt, common,
1256 &common->hash_entry);
1258 if (unlikely(hash_added != 0)) {
1259 /* remove the reference for the hash */
1260 batadv_tt_global_entry_free_ref(tt_global_entry);
1264 common = &tt_global_entry->common;
1265 /* If there is already a global entry, we can use this one for
1267 * But if we are trying to add a temporary client then here are
1268 * two options at this point:
1269 * 1) the global client is not a temporary client: the global
1270 * client has to be left as it is, temporary information
1271 * should never override any already known client state
1272 * 2) the global client is a temporary client: purge the
1273 * originator list and add the new one orig_entry
1275 if (flags & BATADV_TT_CLIENT_TEMP) {
1276 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1278 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1281 batadv_tt_global_del_orig_list(tt_global_entry);
1282 goto add_orig_entry;
1285 /* if the client was temporary added before receiving the first
1286 * OGM announcing it, we have to clear the TEMP flag
1288 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1290 /* the change can carry possible "attribute" flags like the
1291 * TT_CLIENT_WIFI, therefore they have to be copied in the
1294 tt_global_entry->common.flags |= flags;
1296 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1297 * one originator left in the list and we previously received a
1298 * delete + roaming change for this originator.
1300 * We should first delete the old originator before adding the
1303 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1304 batadv_tt_global_del_orig_list(tt_global_entry);
1305 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1306 tt_global_entry->roam_at = 0;
1310 /* add the new orig_entry (if needed) or update it */
1311 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1313 batadv_dbg(BATADV_DBG_TT, bat_priv,
1314 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1315 common->addr, BATADV_PRINT_VID(common->vid),
1321 /* remove address from local hash if present */
1322 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1323 "global tt received",
1324 flags & BATADV_TT_CLIENT_ROAM);
1325 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1327 if (!(flags & BATADV_TT_CLIENT_ROAM))
1328 /* this is a normal global add. Therefore the client is not in a
1329 * roaming state anymore.
1331 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1334 if (tt_global_entry)
1335 batadv_tt_global_entry_free_ref(tt_global_entry);
1337 batadv_tt_local_entry_free_ref(tt_local_entry);
1341 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
1342 * @bat_priv: the bat priv with all the soft interface information
1343 * @tt_global_entry: global translation table entry to be analyzed
1345 * This functon assumes the caller holds rcu_read_lock().
1346 * Returns best originator list entry or NULL on errors.
1348 static struct batadv_tt_orig_list_entry *
1349 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1350 struct batadv_tt_global_entry *tt_global_entry)
1352 struct batadv_neigh_node *router, *best_router = NULL;
1353 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1354 struct hlist_head *head;
1355 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1357 head = &tt_global_entry->orig_list;
1358 hlist_for_each_entry_rcu(orig_entry, head, list) {
1359 router = batadv_orig_node_get_router(orig_entry->orig_node);
1364 bao->bat_neigh_cmp(router, best_router) <= 0) {
1365 batadv_neigh_node_free_ref(router);
1369 /* release the refcount for the "old" best */
1371 batadv_neigh_node_free_ref(best_router);
1373 best_entry = orig_entry;
1374 best_router = router;
1378 batadv_neigh_node_free_ref(best_router);
1383 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
1384 * for this global entry
1385 * @bat_priv: the bat priv with all the soft interface information
1386 * @tt_global_entry: global translation table entry to be printed
1387 * @seq: debugfs table seq_file struct
1389 * This functon assumes the caller holds rcu_read_lock().
1392 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1393 struct batadv_tt_global_entry *tt_global_entry,
1394 struct seq_file *seq)
1396 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1397 struct batadv_tt_common_entry *tt_common_entry;
1398 struct batadv_orig_node_vlan *vlan;
1399 struct hlist_head *head;
1403 tt_common_entry = &tt_global_entry->common;
1404 flags = tt_common_entry->flags;
1406 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1408 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1409 tt_common_entry->vid);
1412 " * Cannot retrieve VLAN %d for originator %pM\n",
1413 BATADV_PRINT_VID(tt_common_entry->vid),
1414 best_entry->orig_node->orig);
1418 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1420 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1421 '*', tt_global_entry->common.addr,
1422 BATADV_PRINT_VID(tt_global_entry->common.vid),
1423 best_entry->ttvn, best_entry->orig_node->orig,
1424 last_ttvn, vlan->tt.crc,
1425 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1426 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1427 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1429 batadv_orig_node_vlan_free_ref(vlan);
1433 head = &tt_global_entry->orig_list;
1435 hlist_for_each_entry_rcu(orig_entry, head, list) {
1436 if (best_entry == orig_entry)
1439 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1440 tt_common_entry->vid);
1443 " + Cannot retrieve VLAN %d for originator %pM\n",
1444 BATADV_PRINT_VID(tt_common_entry->vid),
1445 orig_entry->orig_node->orig);
1449 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1451 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1452 '+', tt_global_entry->common.addr,
1453 BATADV_PRINT_VID(tt_global_entry->common.vid),
1454 orig_entry->ttvn, orig_entry->orig_node->orig,
1455 last_ttvn, vlan->tt.crc,
1456 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1457 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1458 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1460 batadv_orig_node_vlan_free_ref(vlan);
1464 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1466 struct net_device *net_dev = (struct net_device *)seq->private;
1467 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1468 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1469 struct batadv_tt_common_entry *tt_common_entry;
1470 struct batadv_tt_global_entry *tt_global;
1471 struct batadv_hard_iface *primary_if;
1472 struct hlist_head *head;
1475 primary_if = batadv_seq_print_text_primary_if_get(seq);
1480 "Globally announced TT entries received via the mesh %s\n",
1482 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1483 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1486 for (i = 0; i < hash->size; i++) {
1487 head = &hash->table[i];
1490 hlist_for_each_entry_rcu(tt_common_entry,
1492 tt_global = container_of(tt_common_entry,
1493 struct batadv_tt_global_entry,
1495 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1501 batadv_hardif_free_ref(primary_if);
1505 /* deletes the orig list of a tt_global_entry */
1507 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1509 struct hlist_head *head;
1510 struct hlist_node *safe;
1511 struct batadv_tt_orig_list_entry *orig_entry;
1513 spin_lock_bh(&tt_global_entry->list_lock);
1514 head = &tt_global_entry->orig_list;
1515 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1516 hlist_del_rcu(&orig_entry->list);
1517 batadv_tt_global_size_dec(orig_entry->orig_node,
1518 tt_global_entry->common.vid);
1519 batadv_tt_orig_list_entry_free_ref(orig_entry);
1521 spin_unlock_bh(&tt_global_entry->list_lock);
1525 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1526 struct batadv_tt_global_entry *tt_global_entry,
1527 struct batadv_orig_node *orig_node,
1528 const char *message)
1530 struct hlist_head *head;
1531 struct hlist_node *safe;
1532 struct batadv_tt_orig_list_entry *orig_entry;
1535 spin_lock_bh(&tt_global_entry->list_lock);
1536 head = &tt_global_entry->orig_list;
1537 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1538 if (orig_entry->orig_node == orig_node) {
1539 vid = tt_global_entry->common.vid;
1540 batadv_dbg(BATADV_DBG_TT, bat_priv,
1541 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1543 tt_global_entry->common.addr,
1544 BATADV_PRINT_VID(vid), message);
1545 hlist_del_rcu(&orig_entry->list);
1546 batadv_tt_global_size_dec(orig_node,
1547 tt_global_entry->common.vid);
1548 batadv_tt_orig_list_entry_free_ref(orig_entry);
1551 spin_unlock_bh(&tt_global_entry->list_lock);
1554 /* If the client is to be deleted, we check if it is the last origantor entry
1555 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1556 * timer, otherwise we simply remove the originator scheduled for deletion.
1559 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1560 struct batadv_tt_global_entry *tt_global_entry,
1561 struct batadv_orig_node *orig_node,
1562 const char *message)
1564 bool last_entry = true;
1565 struct hlist_head *head;
1566 struct batadv_tt_orig_list_entry *orig_entry;
1568 /* no local entry exists, case 1:
1569 * Check if this is the last one or if other entries exist.
1573 head = &tt_global_entry->orig_list;
1574 hlist_for_each_entry_rcu(orig_entry, head, list) {
1575 if (orig_entry->orig_node != orig_node) {
1583 /* its the last one, mark for roaming. */
1584 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1585 tt_global_entry->roam_at = jiffies;
1587 /* there is another entry, we can simply delete this
1588 * one and can still use the other one.
1590 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1591 orig_node, message);
1595 * batadv_tt_global_del - remove a client from the global table
1596 * @bat_priv: the bat priv with all the soft interface information
1597 * @orig_node: an originator serving this client
1598 * @addr: the mac address of the client
1599 * @vid: VLAN identifier
1600 * @message: a message explaining the reason for deleting the client to print
1601 * for debugging purpose
1602 * @roaming: true if the deletion has been triggered by a roaming event
1604 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1605 struct batadv_orig_node *orig_node,
1606 const unsigned char *addr, unsigned short vid,
1607 const char *message, bool roaming)
1609 struct batadv_tt_global_entry *tt_global_entry;
1610 struct batadv_tt_local_entry *local_entry = NULL;
1612 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1613 if (!tt_global_entry)
1617 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1618 orig_node, message);
1620 if (hlist_empty(&tt_global_entry->orig_list))
1621 batadv_tt_global_free(bat_priv, tt_global_entry,
1627 /* if we are deleting a global entry due to a roam
1628 * event, there are two possibilities:
1629 * 1) the client roamed from node A to node B => if there
1630 * is only one originator left for this client, we mark
1631 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1632 * wait for node B to claim it. In case of timeout
1633 * the entry is purged.
1635 * If there are other originators left, we directly delete
1637 * 2) the client roamed to us => we can directly delete
1638 * the global entry, since it is useless now.
1640 local_entry = batadv_tt_local_hash_find(bat_priv,
1641 tt_global_entry->common.addr,
1644 /* local entry exists, case 2: client roamed to us. */
1645 batadv_tt_global_del_orig_list(tt_global_entry);
1646 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1648 /* no local entry exists, case 1: check for roaming */
1649 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1650 orig_node, message);
1654 if (tt_global_entry)
1655 batadv_tt_global_entry_free_ref(tt_global_entry);
1657 batadv_tt_local_entry_free_ref(local_entry);
1661 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1662 * given originator matching the provided vid
1663 * @bat_priv: the bat priv with all the soft interface information
1664 * @orig_node: the originator owning the entries to remove
1665 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1667 * @message: debug message to print as "reason"
1669 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1670 struct batadv_orig_node *orig_node,
1672 const char *message)
1674 struct batadv_tt_global_entry *tt_global;
1675 struct batadv_tt_common_entry *tt_common_entry;
1677 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1678 struct hlist_node *safe;
1679 struct hlist_head *head;
1680 spinlock_t *list_lock; /* protects write access to the hash lists */
1686 for (i = 0; i < hash->size; i++) {
1687 head = &hash->table[i];
1688 list_lock = &hash->list_locks[i];
1690 spin_lock_bh(list_lock);
1691 hlist_for_each_entry_safe(tt_common_entry, safe,
1693 /* remove only matching entries */
1694 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1697 tt_global = container_of(tt_common_entry,
1698 struct batadv_tt_global_entry,
1701 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1702 orig_node, message);
1704 if (hlist_empty(&tt_global->orig_list)) {
1705 vid = tt_global->common.vid;
1706 batadv_dbg(BATADV_DBG_TT, bat_priv,
1707 "Deleting global tt entry %pM (vid: %d): %s\n",
1708 tt_global->common.addr,
1709 BATADV_PRINT_VID(vid), message);
1710 hlist_del_rcu(&tt_common_entry->hash_entry);
1711 batadv_tt_global_entry_free_ref(tt_global);
1714 spin_unlock_bh(list_lock);
1716 orig_node->tt_initialised = false;
1719 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1723 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1724 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1726 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1727 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1729 *msg = "Roaming timeout\n";
1732 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1733 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1735 *msg = "Temporary client timeout\n";
1741 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1743 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1744 struct hlist_head *head;
1745 struct hlist_node *node_tmp;
1746 spinlock_t *list_lock; /* protects write access to the hash lists */
1749 struct batadv_tt_common_entry *tt_common;
1750 struct batadv_tt_global_entry *tt_global;
1752 for (i = 0; i < hash->size; i++) {
1753 head = &hash->table[i];
1754 list_lock = &hash->list_locks[i];
1756 spin_lock_bh(list_lock);
1757 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1759 tt_global = container_of(tt_common,
1760 struct batadv_tt_global_entry,
1763 if (!batadv_tt_global_to_purge(tt_global, &msg))
1766 batadv_dbg(BATADV_DBG_TT, bat_priv,
1767 "Deleting global tt entry %pM (vid: %d): %s\n",
1768 tt_global->common.addr,
1769 BATADV_PRINT_VID(tt_global->common.vid),
1772 hlist_del_rcu(&tt_common->hash_entry);
1774 batadv_tt_global_entry_free_ref(tt_global);
1776 spin_unlock_bh(list_lock);
1780 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1782 struct batadv_hashtable *hash;
1783 spinlock_t *list_lock; /* protects write access to the hash lists */
1784 struct batadv_tt_common_entry *tt_common_entry;
1785 struct batadv_tt_global_entry *tt_global;
1786 struct hlist_node *node_tmp;
1787 struct hlist_head *head;
1790 if (!bat_priv->tt.global_hash)
1793 hash = bat_priv->tt.global_hash;
1795 for (i = 0; i < hash->size; i++) {
1796 head = &hash->table[i];
1797 list_lock = &hash->list_locks[i];
1799 spin_lock_bh(list_lock);
1800 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1802 hlist_del_rcu(&tt_common_entry->hash_entry);
1803 tt_global = container_of(tt_common_entry,
1804 struct batadv_tt_global_entry,
1806 batadv_tt_global_entry_free_ref(tt_global);
1808 spin_unlock_bh(list_lock);
1811 batadv_hash_destroy(hash);
1813 bat_priv->tt.global_hash = NULL;
1817 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1818 struct batadv_tt_global_entry *tt_global_entry)
1822 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1823 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1830 * batadv_transtable_search - get the mesh destination for a given client
1831 * @bat_priv: the bat priv with all the soft interface information
1832 * @src: mac address of the source client
1833 * @addr: mac address of the destination client
1834 * @vid: VLAN identifier
1836 * Returns a pointer to the originator that was selected as destination in the
1837 * mesh for contacting the client 'addr', NULL otherwise.
1838 * In case of multiple originators serving the same client, the function returns
1839 * the best one (best in terms of metric towards the destination node).
1841 * If the two clients are AP isolated the function returns NULL.
1843 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1845 const uint8_t *addr,
1848 struct batadv_tt_local_entry *tt_local_entry = NULL;
1849 struct batadv_tt_global_entry *tt_global_entry = NULL;
1850 struct batadv_orig_node *orig_node = NULL;
1851 struct batadv_tt_orig_list_entry *best_entry;
1852 bool ap_isolation_enabled = false;
1853 struct batadv_softif_vlan *vlan;
1855 /* if the AP isolation is requested on a VLAN, then check for its
1856 * setting in the proper VLAN private data structure
1858 vlan = batadv_softif_vlan_get(bat_priv, vid);
1860 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1861 batadv_softif_vlan_free_ref(vlan);
1864 if (src && ap_isolation_enabled) {
1865 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
1866 if (!tt_local_entry ||
1867 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1871 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1872 if (!tt_global_entry)
1875 /* check whether the clients should not communicate due to AP
1878 if (tt_local_entry &&
1879 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1883 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1884 /* found anything? */
1886 orig_node = best_entry->orig_node;
1887 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1892 if (tt_global_entry)
1893 batadv_tt_global_entry_free_ref(tt_global_entry);
1895 batadv_tt_local_entry_free_ref(tt_local_entry);
1901 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1902 * to the given orig_node
1903 * @bat_priv: the bat priv with all the soft interface information
1904 * @orig_node: originator for which the CRC should be computed
1905 * @vid: VLAN identifier for which the CRC32 has to be computed
1907 * This function computes the checksum for the global table corresponding to a
1908 * specific originator. In particular, the checksum is computed as follows: For
1909 * each client connected to the originator the CRC32C of the MAC address and the
1910 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1913 * The idea behind is that CRC32C should be used as much as possible in order to
1914 * produce a unique hash of the table, but since the order which is used to feed
1915 * the CRC32C function affects the result and since every node in the network
1916 * probably sorts the clients differently, the hash function cannot be directly
1917 * computed over the entire table. Hence the CRC32C is used only on
1918 * the single client entry, while all the results are then xor'ed together
1919 * because the XOR operation can combine them all while trying to reduce the
1920 * noise as much as possible.
1922 * Returns the checksum of the global table of a given originator.
1924 static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1925 struct batadv_orig_node *orig_node,
1928 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1929 struct batadv_tt_common_entry *tt_common;
1930 struct batadv_tt_global_entry *tt_global;
1931 struct hlist_head *head;
1932 uint32_t i, crc_tmp, crc = 0;
1934 for (i = 0; i < hash->size; i++) {
1935 head = &hash->table[i];
1938 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1939 tt_global = container_of(tt_common,
1940 struct batadv_tt_global_entry,
1942 /* compute the CRC only for entries belonging to the
1943 * VLAN identified by the vid passed as parameter
1945 if (tt_common->vid != vid)
1948 /* Roaming clients are in the global table for
1949 * consistency only. They don't have to be
1950 * taken into account while computing the
1953 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1955 /* Temporary clients have not been announced yet, so
1956 * they have to be skipped while computing the global
1959 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1962 /* find out if this global entry is announced by this
1965 if (!batadv_tt_global_entry_has_orig(tt_global,
1969 crc_tmp = crc32c(0, &tt_common->vid,
1970 sizeof(tt_common->vid));
1971 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
1980 * batadv_tt_local_crc - calculates the checksum of the local table
1981 * @bat_priv: the bat priv with all the soft interface information
1982 * @vid: VLAN identifier for which the CRC32 has to be computed
1984 * For details about the computation, please refer to the documentation for
1985 * batadv_tt_global_crc().
1987 * Returns the checksum of the local table
1989 static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
1992 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1993 struct batadv_tt_common_entry *tt_common;
1994 struct hlist_head *head;
1995 uint32_t i, crc_tmp, crc = 0;
1997 for (i = 0; i < hash->size; i++) {
1998 head = &hash->table[i];
2001 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2002 /* compute the CRC only for entries belonging to the
2003 * VLAN identified by vid
2005 if (tt_common->vid != vid)
2008 /* not yet committed clients have not to be taken into
2009 * account while computing the CRC
2011 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2014 crc_tmp = crc32c(0, &tt_common->vid,
2015 sizeof(tt_common->vid));
2016 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2024 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2026 struct batadv_tt_req_node *node, *safe;
2028 spin_lock_bh(&bat_priv->tt.req_list_lock);
2030 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2031 list_del(&node->list);
2035 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2038 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2039 struct batadv_orig_node *orig_node,
2040 const void *tt_buff,
2041 uint16_t tt_buff_len)
2043 /* Replace the old buffer only if I received something in the
2044 * last OGM (the OGM could carry no changes)
2046 spin_lock_bh(&orig_node->tt_buff_lock);
2047 if (tt_buff_len > 0) {
2048 kfree(orig_node->tt_buff);
2049 orig_node->tt_buff_len = 0;
2050 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2051 if (orig_node->tt_buff) {
2052 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2053 orig_node->tt_buff_len = tt_buff_len;
2056 spin_unlock_bh(&orig_node->tt_buff_lock);
2059 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2061 struct batadv_tt_req_node *node, *safe;
2063 spin_lock_bh(&bat_priv->tt.req_list_lock);
2064 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2065 if (batadv_has_timed_out(node->issued_at,
2066 BATADV_TT_REQUEST_TIMEOUT)) {
2067 list_del(&node->list);
2071 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2074 /* returns the pointer to the new tt_req_node struct if no request
2075 * has already been issued for this orig_node, NULL otherwise
2077 static struct batadv_tt_req_node *
2078 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2079 struct batadv_orig_node *orig_node)
2081 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2083 spin_lock_bh(&bat_priv->tt.req_list_lock);
2084 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2085 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2086 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2087 BATADV_TT_REQUEST_TIMEOUT))
2091 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2095 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2096 tt_req_node->issued_at = jiffies;
2098 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
2100 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2105 * batadv_tt_local_valid - verify that given tt entry is a valid one
2106 * @entry_ptr: to be checked local tt entry
2107 * @data_ptr: not used but definition required to satisfy the callback prototype
2109 * Returns 1 if the entry is a valid, 0 otherwise.
2111 static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2113 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2115 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2120 static int batadv_tt_global_valid(const void *entry_ptr,
2121 const void *data_ptr)
2123 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2124 const struct batadv_tt_global_entry *tt_global_entry;
2125 const struct batadv_orig_node *orig_node = data_ptr;
2127 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2128 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2131 tt_global_entry = container_of(tt_common_entry,
2132 struct batadv_tt_global_entry,
2135 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2139 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2141 * @bat_priv: the bat priv with all the soft interface information
2142 * @hash: hash table containing the tt entries
2143 * @tt_len: expected tvlv tt data buffer length in number of bytes
2144 * @tvlv_buff: pointer to the buffer to fill with the TT data
2145 * @valid_cb: function to filter tt change entries
2146 * @cb_data: data passed to the filter function as argument
2148 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2149 struct batadv_hashtable *hash,
2150 void *tvlv_buff, uint16_t tt_len,
2151 int (*valid_cb)(const void *, const void *),
2154 struct batadv_tt_common_entry *tt_common_entry;
2155 struct batadv_tvlv_tt_change *tt_change;
2156 struct hlist_head *head;
2157 uint16_t tt_tot, tt_num_entries = 0;
2160 tt_tot = batadv_tt_entries(tt_len);
2161 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2164 for (i = 0; i < hash->size; i++) {
2165 head = &hash->table[i];
2167 hlist_for_each_entry_rcu(tt_common_entry,
2169 if (tt_tot == tt_num_entries)
2172 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2175 memcpy(tt_change->addr, tt_common_entry->addr,
2177 tt_change->flags = tt_common_entry->flags;
2178 tt_change->vid = htons(tt_common_entry->vid);
2179 tt_change->reserved = 0;
2189 * batadv_tt_global_check_crc - check if all the CRCs are correct
2190 * @orig_node: originator for which the CRCs have to be checked
2191 * @tt_vlan: pointer to the first tvlv VLAN entry
2192 * @num_vlan: number of tvlv VLAN entries
2193 * @create: if true, create VLAN objects if not found
2195 * Return true if all the received CRCs match the locally stored ones, false
2198 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2199 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2202 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2203 struct batadv_orig_node_vlan *vlan;
2206 /* check if each received CRC matches the locally stored one */
2207 for (i = 0; i < num_vlan; i++) {
2208 tt_vlan_tmp = tt_vlan + i;
2210 /* if orig_node is a backbone node for this VLAN, don't check
2211 * the CRC as we ignore all the global entries over it
2213 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2215 ntohs(tt_vlan_tmp->vid)))
2218 vlan = batadv_orig_node_vlan_get(orig_node,
2219 ntohs(tt_vlan_tmp->vid));
2223 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2231 * batadv_tt_local_update_crc - update all the local CRCs
2232 * @bat_priv: the bat priv with all the soft interface information
2234 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2236 struct batadv_softif_vlan *vlan;
2238 /* recompute the global CRC for each VLAN */
2240 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2241 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2247 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2248 * @bat_priv: the bat priv with all the soft interface information
2249 * @orig_node: the orig_node for which the CRCs have to be updated
2251 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2252 struct batadv_orig_node *orig_node)
2254 struct batadv_orig_node_vlan *vlan;
2257 /* recompute the global CRC for each VLAN */
2259 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2260 /* if orig_node is a backbone node for this VLAN, don't compute
2261 * the CRC as we ignore all the global entries over it
2263 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2267 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2274 * batadv_send_tt_request - send a TT Request message to a given node
2275 * @bat_priv: the bat priv with all the soft interface information
2276 * @dst_orig_node: the destination of the message
2277 * @ttvn: the version number that the source of the message is looking for
2278 * @tt_vlan: pointer to the first tvlv VLAN object to request
2279 * @num_vlan: number of tvlv VLAN entries
2280 * @full_table: ask for the entire translation table if true, while only for the
2281 * last TT diff otherwise
2283 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2284 struct batadv_orig_node *dst_orig_node,
2286 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2287 uint16_t num_vlan, bool full_table)
2289 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2290 struct batadv_tt_req_node *tt_req_node = NULL;
2291 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2292 struct batadv_hard_iface *primary_if;
2296 primary_if = batadv_primary_if_get_selected(bat_priv);
2300 /* The new tt_req will be issued only if I'm not waiting for a
2301 * reply from the same orig_node yet
2303 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
2307 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2308 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2312 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2313 tvlv_tt_data->ttvn = ttvn;
2314 tvlv_tt_data->num_vlan = htons(num_vlan);
2316 /* send all the CRCs within the request. This is needed by intermediate
2317 * nodes to ensure they have the correct table before replying
2319 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2320 for (i = 0; i < num_vlan; i++) {
2321 tt_vlan_req->vid = tt_vlan->vid;
2322 tt_vlan_req->crc = tt_vlan->crc;
2329 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2331 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2332 dst_orig_node->orig, full_table ? 'F' : '.');
2334 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2335 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2336 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2337 tvlv_tt_data, size);
2342 batadv_hardif_free_ref(primary_if);
2343 if (ret && tt_req_node) {
2344 spin_lock_bh(&bat_priv->tt.req_list_lock);
2345 list_del(&tt_req_node->list);
2346 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2349 kfree(tvlv_tt_data);
2354 * batadv_send_other_tt_response - send reply to tt request concerning another
2355 * node's translation table
2356 * @bat_priv: the bat priv with all the soft interface information
2357 * @tt_data: tt data containing the tt request information
2358 * @req_src: mac address of tt request sender
2359 * @req_dst: mac address of tt request recipient
2361 * Returns true if tt request reply was sent, false otherwise.
2363 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2364 struct batadv_tvlv_tt_data *tt_data,
2365 uint8_t *req_src, uint8_t *req_dst)
2367 struct batadv_orig_node *req_dst_orig_node;
2368 struct batadv_orig_node *res_dst_orig_node = NULL;
2369 struct batadv_tvlv_tt_change *tt_change;
2370 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2371 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2372 bool ret = false, full_table;
2373 uint8_t orig_ttvn, req_ttvn;
2377 batadv_dbg(BATADV_DBG_TT, bat_priv,
2378 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2379 req_src, tt_data->ttvn, req_dst,
2380 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2382 /* Let's get the orig node of the REAL destination */
2383 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2384 if (!req_dst_orig_node)
2387 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2388 if (!res_dst_orig_node)
2391 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
2392 req_ttvn = tt_data->ttvn;
2394 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2395 /* this node doesn't have the requested data */
2396 if (orig_ttvn != req_ttvn ||
2397 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2398 ntohs(tt_data->num_vlan)))
2401 /* If the full table has been explicitly requested */
2402 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2403 !req_dst_orig_node->tt_buff)
2408 /* TT fragmentation hasn't been implemented yet, so send as many
2409 * TT entries fit a single packet as possible only
2412 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2413 tt_len = req_dst_orig_node->tt_buff_len;
2415 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2422 /* Copy the last orig_node's OGM buffer */
2423 memcpy(tt_change, req_dst_orig_node->tt_buff,
2424 req_dst_orig_node->tt_buff_len);
2425 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2427 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2428 * in the initial part
2431 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2438 /* fill the rest of the tvlv with the real TT entries */
2439 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2441 batadv_tt_global_valid,
2445 /* Don't send the response, if larger than fragmented packet. */
2446 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2447 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2448 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2449 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2450 res_dst_orig_node->orig);
2454 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2455 tvlv_tt_data->ttvn = req_ttvn;
2458 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2460 batadv_dbg(BATADV_DBG_TT, bat_priv,
2461 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2462 res_dst_orig_node->orig, req_dst_orig_node->orig,
2463 full_table ? 'F' : '.', req_ttvn);
2465 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2467 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2468 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2475 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2478 if (res_dst_orig_node)
2479 batadv_orig_node_free_ref(res_dst_orig_node);
2480 if (req_dst_orig_node)
2481 batadv_orig_node_free_ref(req_dst_orig_node);
2482 kfree(tvlv_tt_data);
2487 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2489 * @bat_priv: the bat priv with all the soft interface information
2490 * @tt_data: tt data containing the tt request information
2491 * @req_src: mac address of tt request sender
2493 * Returns true if tt request reply was sent, false otherwise.
2495 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2496 struct batadv_tvlv_tt_data *tt_data,
2499 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2500 struct batadv_hard_iface *primary_if = NULL;
2501 struct batadv_tvlv_tt_change *tt_change;
2502 struct batadv_orig_node *orig_node;
2503 uint8_t my_ttvn, req_ttvn;
2508 batadv_dbg(BATADV_DBG_TT, bat_priv,
2509 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2510 req_src, tt_data->ttvn,
2511 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2513 spin_lock_bh(&bat_priv->tt.commit_lock);
2515 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2516 req_ttvn = tt_data->ttvn;
2518 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2522 primary_if = batadv_primary_if_get_selected(bat_priv);
2526 /* If the full table has been explicitly requested or the gap
2527 * is too big send the whole local translation table
2529 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2530 !bat_priv->tt.last_changeset)
2535 /* TT fragmentation hasn't been implemented yet, so send as many
2536 * TT entries fit a single packet as possible only
2539 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2541 tt_len = bat_priv->tt.last_changeset_len;
2542 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2549 /* Copy the last orig_node's OGM buffer */
2550 memcpy(tt_change, bat_priv->tt.last_changeset,
2551 bat_priv->tt.last_changeset_len);
2552 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2554 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2556 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2557 * in the initial part
2560 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2567 /* fill the rest of the tvlv with the real TT entries */
2568 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2570 batadv_tt_local_valid, NULL);
2573 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2574 tvlv_tt_data->ttvn = req_ttvn;
2577 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2579 batadv_dbg(BATADV_DBG_TT, bat_priv,
2580 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2581 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2583 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2585 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2586 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2592 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2594 spin_unlock_bh(&bat_priv->tt.commit_lock);
2596 batadv_orig_node_free_ref(orig_node);
2598 batadv_hardif_free_ref(primary_if);
2599 kfree(tvlv_tt_data);
2600 /* The packet was for this host, so it doesn't need to be re-routed */
2605 * batadv_send_tt_response - send reply to tt request
2606 * @bat_priv: the bat priv with all the soft interface information
2607 * @tt_data: tt data containing the tt request information
2608 * @req_src: mac address of tt request sender
2609 * @req_dst: mac address of tt request recipient
2611 * Returns true if tt request reply was sent, false otherwise.
2613 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2614 struct batadv_tvlv_tt_data *tt_data,
2615 uint8_t *req_src, uint8_t *req_dst)
2617 if (batadv_is_my_mac(bat_priv, req_dst))
2618 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2620 return batadv_send_other_tt_response(bat_priv, tt_data,
2624 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2625 struct batadv_orig_node *orig_node,
2626 struct batadv_tvlv_tt_change *tt_change,
2627 uint16_t tt_num_changes, uint8_t ttvn)
2632 for (i = 0; i < tt_num_changes; i++) {
2633 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2634 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2635 batadv_tt_global_del(bat_priv, orig_node,
2636 (tt_change + i)->addr,
2637 ntohs((tt_change + i)->vid),
2638 "tt removed by changes",
2641 if (!batadv_tt_global_add(bat_priv, orig_node,
2642 (tt_change + i)->addr,
2643 ntohs((tt_change + i)->vid),
2644 (tt_change + i)->flags, ttvn))
2645 /* In case of problem while storing a
2646 * global_entry, we stop the updating
2647 * procedure without committing the
2648 * ttvn change. This will avoid to send
2649 * corrupted data on tt_request
2654 orig_node->tt_initialised = true;
2657 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2658 struct batadv_tvlv_tt_change *tt_change,
2659 uint8_t ttvn, uint8_t *resp_src,
2660 uint16_t num_entries)
2662 struct batadv_orig_node *orig_node;
2664 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2668 /* Purge the old table first.. */
2669 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2670 "Received full table");
2672 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2675 spin_lock_bh(&orig_node->tt_buff_lock);
2676 kfree(orig_node->tt_buff);
2677 orig_node->tt_buff_len = 0;
2678 orig_node->tt_buff = NULL;
2679 spin_unlock_bh(&orig_node->tt_buff_lock);
2681 atomic_set(&orig_node->last_ttvn, ttvn);
2685 batadv_orig_node_free_ref(orig_node);
2688 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2689 struct batadv_orig_node *orig_node,
2690 uint16_t tt_num_changes, uint8_t ttvn,
2691 struct batadv_tvlv_tt_change *tt_change)
2693 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2694 tt_num_changes, ttvn);
2696 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2697 batadv_tt_len(tt_num_changes));
2698 atomic_set(&orig_node->last_ttvn, ttvn);
2702 * batadv_is_my_client - check if a client is served by the local node
2703 * @bat_priv: the bat priv with all the soft interface information
2704 * @addr: the mac adress of the client to check
2705 * @vid: VLAN identifier
2707 * Returns true if the client is served by this node, false otherwise.
2709 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2712 struct batadv_tt_local_entry *tt_local_entry;
2715 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2716 if (!tt_local_entry)
2718 /* Check if the client has been logically deleted (but is kept for
2719 * consistency purpose)
2721 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2722 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2727 batadv_tt_local_entry_free_ref(tt_local_entry);
2732 * batadv_handle_tt_response - process incoming tt reply
2733 * @bat_priv: the bat priv with all the soft interface information
2734 * @tt_data: tt data containing the tt request information
2735 * @resp_src: mac address of tt reply sender
2736 * @num_entries: number of tt change entries appended to the tt data
2738 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2739 struct batadv_tvlv_tt_data *tt_data,
2740 uint8_t *resp_src, uint16_t num_entries)
2742 struct batadv_tt_req_node *node, *safe;
2743 struct batadv_orig_node *orig_node = NULL;
2744 struct batadv_tvlv_tt_change *tt_change;
2745 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2746 uint16_t change_offset;
2748 batadv_dbg(BATADV_DBG_TT, bat_priv,
2749 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2750 resp_src, tt_data->ttvn, num_entries,
2751 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2753 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2757 spin_lock_bh(&orig_node->tt_lock);
2759 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2760 change_offset *= ntohs(tt_data->num_vlan);
2761 change_offset += sizeof(*tt_data);
2762 tvlv_ptr += change_offset;
2764 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
2765 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2766 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2767 resp_src, num_entries);
2769 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2770 tt_data->ttvn, tt_change);
2773 /* Recalculate the CRC for this orig_node and store it */
2774 batadv_tt_global_update_crc(bat_priv, orig_node);
2776 spin_unlock_bh(&orig_node->tt_lock);
2778 /* Delete the tt_req_node from pending tt_requests list */
2779 spin_lock_bh(&bat_priv->tt.req_list_lock);
2780 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2781 if (!batadv_compare_eth(node->addr, resp_src))
2783 list_del(&node->list);
2787 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2790 batadv_orig_node_free_ref(orig_node);
2793 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2795 struct batadv_tt_roam_node *node, *safe;
2797 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2799 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2800 list_del(&node->list);
2804 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2807 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2809 struct batadv_tt_roam_node *node, *safe;
2811 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2812 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2813 if (!batadv_has_timed_out(node->first_time,
2814 BATADV_ROAMING_MAX_TIME))
2817 list_del(&node->list);
2820 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2823 /* This function checks whether the client already reached the
2824 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2827 * returns true if the ROAMING_ADV can be sent, false otherwise
2829 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2832 struct batadv_tt_roam_node *tt_roam_node;
2835 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2836 /* The new tt_req will be issued only if I'm not waiting for a
2837 * reply from the same orig_node yet
2839 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2840 if (!batadv_compare_eth(tt_roam_node->addr, client))
2843 if (batadv_has_timed_out(tt_roam_node->first_time,
2844 BATADV_ROAMING_MAX_TIME))
2847 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2848 /* Sorry, you roamed too many times! */
2855 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2859 tt_roam_node->first_time = jiffies;
2860 atomic_set(&tt_roam_node->counter,
2861 BATADV_ROAMING_MAX_COUNT - 1);
2862 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2864 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2869 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2874 * batadv_send_roam_adv - send a roaming advertisement message
2875 * @bat_priv: the bat priv with all the soft interface information
2876 * @client: mac address of the roaming client
2877 * @vid: VLAN identifier
2878 * @orig_node: message destination
2880 * Send a ROAMING_ADV message to the node which was previously serving this
2881 * client. This is done to inform the node that from now on all traffic destined
2882 * for this particular roamed client has to be forwarded to the sender of the
2885 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2887 struct batadv_orig_node *orig_node)
2889 struct batadv_hard_iface *primary_if;
2890 struct batadv_tvlv_roam_adv tvlv_roam;
2892 primary_if = batadv_primary_if_get_selected(bat_priv);
2896 /* before going on we have to check whether the client has
2897 * already roamed to us too many times
2899 if (!batadv_tt_check_roam_count(bat_priv, client))
2902 batadv_dbg(BATADV_DBG_TT, bat_priv,
2903 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2904 orig_node->orig, client, BATADV_PRINT_VID(vid));
2906 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2908 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2909 tvlv_roam.vid = htons(vid);
2911 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2912 orig_node->orig, BATADV_TVLV_ROAM, 1,
2913 &tvlv_roam, sizeof(tvlv_roam));
2917 batadv_hardif_free_ref(primary_if);
2920 static void batadv_tt_purge(struct work_struct *work)
2922 struct delayed_work *delayed_work;
2923 struct batadv_priv_tt *priv_tt;
2924 struct batadv_priv *bat_priv;
2926 delayed_work = container_of(work, struct delayed_work, work);
2927 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2928 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2930 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
2931 batadv_tt_global_purge(bat_priv);
2932 batadv_tt_req_purge(bat_priv);
2933 batadv_tt_roam_purge(bat_priv);
2935 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2936 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2939 void batadv_tt_free(struct batadv_priv *bat_priv)
2941 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2942 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2944 cancel_delayed_work_sync(&bat_priv->tt.work);
2946 batadv_tt_local_table_free(bat_priv);
2947 batadv_tt_global_table_free(bat_priv);
2948 batadv_tt_req_list_free(bat_priv);
2949 batadv_tt_changes_list_free(bat_priv);
2950 batadv_tt_roam_list_free(bat_priv);
2952 kfree(bat_priv->tt.last_changeset);
2956 * batadv_tt_local_set_flags - set or unset the specified flags on the local
2957 * table and possibly count them in the TT size
2958 * @bat_priv: the bat priv with all the soft interface information
2959 * @flags: the flag to switch
2960 * @enable: whether to set or unset the flag
2961 * @count: whether to increase the TT size by the number of changed entries
2963 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2964 uint16_t flags, bool enable, bool count)
2966 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2967 struct batadv_tt_common_entry *tt_common_entry;
2968 uint16_t changed_num = 0;
2969 struct hlist_head *head;
2975 for (i = 0; i < hash->size; i++) {
2976 head = &hash->table[i];
2979 hlist_for_each_entry_rcu(tt_common_entry,
2982 if ((tt_common_entry->flags & flags) == flags)
2984 tt_common_entry->flags |= flags;
2986 if (!(tt_common_entry->flags & flags))
2988 tt_common_entry->flags &= ~flags;
2995 batadv_tt_local_size_inc(bat_priv,
2996 tt_common_entry->vid);
3002 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3003 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3005 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3006 struct batadv_tt_common_entry *tt_common;
3007 struct batadv_tt_local_entry *tt_local;
3008 struct hlist_node *node_tmp;
3009 struct hlist_head *head;
3010 spinlock_t *list_lock; /* protects write access to the hash lists */
3016 for (i = 0; i < hash->size; i++) {
3017 head = &hash->table[i];
3018 list_lock = &hash->list_locks[i];
3020 spin_lock_bh(list_lock);
3021 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3023 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3026 batadv_dbg(BATADV_DBG_TT, bat_priv,
3027 "Deleting local tt entry (%pM, vid: %d): pending\n",
3029 BATADV_PRINT_VID(tt_common->vid));
3031 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3032 hlist_del_rcu(&tt_common->hash_entry);
3033 tt_local = container_of(tt_common,
3034 struct batadv_tt_local_entry,
3036 batadv_tt_local_entry_free_ref(tt_local);
3038 spin_unlock_bh(list_lock);
3043 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3044 * which have been queued in the time since the last commit
3045 * @bat_priv: the bat priv with all the soft interface information
3047 * Caller must hold tt->commit_lock.
3049 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3051 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3052 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3053 batadv_tt_tvlv_container_update(bat_priv);
3057 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3059 batadv_tt_local_purge_pending_clients(bat_priv);
3060 batadv_tt_local_update_crc(bat_priv);
3062 /* Increment the TTVN only once per OGM interval */
3063 atomic_inc(&bat_priv->tt.vn);
3064 batadv_dbg(BATADV_DBG_TT, bat_priv,
3065 "Local changes committed, updating to ttvn %u\n",
3066 (uint8_t)atomic_read(&bat_priv->tt.vn));
3068 /* reset the sending counter */
3069 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3070 batadv_tt_tvlv_container_update(bat_priv);
3074 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3075 * have been queued in the time since the last commit
3076 * @bat_priv: the bat priv with all the soft interface information
3078 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3080 spin_lock_bh(&bat_priv->tt.commit_lock);
3081 batadv_tt_local_commit_changes_nolock(bat_priv);
3082 spin_unlock_bh(&bat_priv->tt.commit_lock);
3085 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
3086 uint8_t *dst, unsigned short vid)
3088 struct batadv_tt_local_entry *tt_local_entry = NULL;
3089 struct batadv_tt_global_entry *tt_global_entry = NULL;
3090 struct batadv_softif_vlan *vlan;
3093 vlan = batadv_softif_vlan_get(bat_priv, vid);
3094 if (!vlan || !atomic_read(&vlan->ap_isolation))
3097 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3098 if (!tt_local_entry)
3101 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3102 if (!tt_global_entry)
3105 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3112 batadv_softif_vlan_free_ref(vlan);
3113 if (tt_global_entry)
3114 batadv_tt_global_entry_free_ref(tt_global_entry);
3116 batadv_tt_local_entry_free_ref(tt_local_entry);
3121 * batadv_tt_update_orig - update global translation table with new tt
3122 * information received via ogms
3123 * @bat_priv: the bat priv with all the soft interface information
3124 * @orig: the orig_node of the ogm
3125 * @tt_vlan: pointer to the first tvlv VLAN entry
3126 * @tt_num_vlan: number of tvlv VLAN entries
3127 * @tt_change: pointer to the first entry in the TT buffer
3128 * @tt_num_changes: number of tt changes inside the tt buffer
3129 * @ttvn: translation table version number of this changeset
3130 * @tt_crc: crc32 checksum of orig node's translation table
3132 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3133 struct batadv_orig_node *orig_node,
3134 const void *tt_buff, uint16_t tt_num_vlan,
3135 struct batadv_tvlv_tt_change *tt_change,
3136 uint16_t tt_num_changes, uint8_t ttvn)
3138 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
3139 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3140 bool full_table = true;
3142 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3143 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3144 * increased by one -> we can apply the attached changes
3146 if ((!orig_node->tt_initialised && ttvn == 1) ||
3147 ttvn - orig_ttvn == 1) {
3148 /* the OGM could not contain the changes due to their size or
3149 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3151 * In this case send a tt request
3153 if (!tt_num_changes) {
3158 spin_lock_bh(&orig_node->tt_lock);
3160 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
3161 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3164 /* Even if we received the precomputed crc with the OGM, we
3165 * prefer to recompute it to spot any possible inconsistency
3166 * in the global table
3168 batadv_tt_global_update_crc(bat_priv, orig_node);
3170 spin_unlock_bh(&orig_node->tt_lock);
3172 /* The ttvn alone is not enough to guarantee consistency
3173 * because a single value could represent different states
3174 * (due to the wrap around). Thus a node has to check whether
3175 * the resulting table (after applying the changes) is still
3176 * consistent or not. E.g. a node could disconnect while its
3177 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3178 * checking the CRC value is mandatory to detect the
3181 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3185 /* if we missed more than one change or our tables are not
3186 * in sync anymore -> request fresh tt data
3188 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
3189 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3192 batadv_dbg(BATADV_DBG_TT, bat_priv,
3193 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3194 orig_node->orig, ttvn, orig_ttvn,
3196 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3197 tt_vlan, tt_num_vlan,
3205 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3206 * @bat_priv: the bat priv with all the soft interface information
3207 * @addr: the mac address of the client to check
3208 * @vid: VLAN identifier
3210 * Returns true if we know that the client has moved from its old originator
3211 * to another one. This entry is still kept for consistency purposes and will be
3212 * deleted later by a DEL or because of timeout
3214 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3215 uint8_t *addr, unsigned short vid)
3217 struct batadv_tt_global_entry *tt_global_entry;
3220 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3221 if (!tt_global_entry)
3224 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3225 batadv_tt_global_entry_free_ref(tt_global_entry);
3231 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3232 * @bat_priv: the bat priv with all the soft interface information
3233 * @addr: the mac address of the local client to query
3234 * @vid: VLAN identifier
3236 * Returns true if the local client is known to be roaming (it is not served by
3237 * this node anymore) or not. If yes, the client is still present in the table
3238 * to keep the latter consistent with the node TTVN
3240 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3241 uint8_t *addr, unsigned short vid)
3243 struct batadv_tt_local_entry *tt_local_entry;
3246 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3247 if (!tt_local_entry)
3250 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3251 batadv_tt_local_entry_free_ref(tt_local_entry);
3256 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3257 struct batadv_orig_node *orig_node,
3258 const unsigned char *addr,
3263 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3264 BATADV_TT_CLIENT_TEMP,
3265 atomic_read(&orig_node->last_ttvn)))
3268 batadv_dbg(BATADV_DBG_TT, bat_priv,
3269 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3270 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3277 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3278 * maximum packet size that can be transported through the mesh
3279 * @soft_iface: netdev struct of the mesh interface
3281 * Remove entries older than 'timeout' and half timeout if more entries need
3284 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3286 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3287 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3288 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3289 bool reduced = false;
3291 spin_lock_bh(&bat_priv->tt.commit_lock);
3294 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3295 if (packet_size_max >= table_size)
3298 batadv_tt_local_purge(bat_priv, timeout);
3299 batadv_tt_local_purge_pending_clients(bat_priv);
3303 net_ratelimited_function(batadv_info, soft_iface,
3304 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3308 /* commit these changes immediately, to avoid synchronization problem
3312 batadv_tt_local_commit_changes_nolock(bat_priv);
3314 spin_unlock_bh(&bat_priv->tt.commit_lock);
3318 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3319 * @bat_priv: the bat priv with all the soft interface information
3320 * @orig: the orig_node of the ogm
3321 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3322 * @tvlv_value: tvlv buffer containing the gateway data
3323 * @tvlv_value_len: tvlv buffer length
3325 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3326 struct batadv_orig_node *orig,
3327 uint8_t flags, void *tvlv_value,
3328 uint16_t tvlv_value_len)
3330 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3331 struct batadv_tvlv_tt_change *tt_change;
3332 struct batadv_tvlv_tt_data *tt_data;
3333 uint16_t num_entries, num_vlan;
3335 if (tvlv_value_len < sizeof(*tt_data))
3338 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3339 tvlv_value_len -= sizeof(*tt_data);
3341 num_vlan = ntohs(tt_data->num_vlan);
3343 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3346 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3347 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3348 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3350 num_entries = batadv_tt_entries(tvlv_value_len);
3352 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3353 num_entries, tt_data->ttvn);
3357 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3359 * @bat_priv: the bat priv with all the soft interface information
3360 * @src: mac address of tt tvlv sender
3361 * @dst: mac address of tt tvlv recipient
3362 * @tvlv_value: tvlv buffer containing the tt data
3363 * @tvlv_value_len: tvlv buffer length
3365 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3368 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3369 uint8_t *src, uint8_t *dst,
3371 uint16_t tvlv_value_len)
3373 struct batadv_tvlv_tt_data *tt_data;
3374 uint16_t tt_vlan_len, tt_num_entries;
3378 if (tvlv_value_len < sizeof(*tt_data))
3379 return NET_RX_SUCCESS;
3381 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3382 tvlv_value_len -= sizeof(*tt_data);
3384 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3385 tt_vlan_len *= ntohs(tt_data->num_vlan);
3387 if (tvlv_value_len < tt_vlan_len)
3388 return NET_RX_SUCCESS;
3390 tvlv_value_len -= tt_vlan_len;
3391 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3393 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3394 case BATADV_TT_REQUEST:
3395 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3397 /* If this node cannot provide a TT response the tt_request is
3400 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3402 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3407 batadv_dbg(BATADV_DBG_TT, bat_priv,
3408 "Routing TT_REQUEST to %pM [%c]\n",
3410 /* tvlv API will re-route the packet */
3414 case BATADV_TT_RESPONSE:
3415 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3417 if (batadv_is_my_mac(bat_priv, dst)) {
3418 batadv_handle_tt_response(bat_priv, tt_data,
3419 src, tt_num_entries);
3420 return NET_RX_SUCCESS;
3423 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3428 batadv_dbg(BATADV_DBG_TT, bat_priv,
3429 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3431 /* tvlv API will re-route the packet */
3435 return NET_RX_SUCCESS;
3439 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3440 * @bat_priv: the bat priv with all the soft interface information
3441 * @src: mac address of tt tvlv sender
3442 * @dst: mac address of tt tvlv recipient
3443 * @tvlv_value: tvlv buffer containing the tt data
3444 * @tvlv_value_len: tvlv buffer length
3446 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3449 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3450 uint8_t *src, uint8_t *dst,
3452 uint16_t tvlv_value_len)
3454 struct batadv_tvlv_roam_adv *roaming_adv;
3455 struct batadv_orig_node *orig_node = NULL;
3457 /* If this node is not the intended recipient of the
3458 * roaming advertisement the packet is forwarded
3459 * (the tvlv API will re-route the packet).
3461 if (!batadv_is_my_mac(bat_priv, dst))
3464 if (tvlv_value_len < sizeof(*roaming_adv))
3467 orig_node = batadv_orig_hash_find(bat_priv, src);
3471 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3472 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3474 batadv_dbg(BATADV_DBG_TT, bat_priv,
3475 "Received ROAMING_ADV from %pM (client %pM)\n",
3476 src, roaming_adv->client);
3478 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3479 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3480 atomic_read(&orig_node->last_ttvn) + 1);
3484 batadv_orig_node_free_ref(orig_node);
3485 return NET_RX_SUCCESS;
3489 * batadv_tt_init - initialise the translation table internals
3490 * @bat_priv: the bat priv with all the soft interface information
3492 * Return 0 on success or negative error number in case of failure.
3494 int batadv_tt_init(struct batadv_priv *bat_priv)
3498 ret = batadv_tt_local_init(bat_priv);
3502 ret = batadv_tt_global_init(bat_priv);
3506 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3507 batadv_tt_tvlv_unicast_handler_v1,
3508 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3510 batadv_tvlv_handler_register(bat_priv, NULL,
3511 batadv_roam_tvlv_unicast_handler_v1,
3512 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3514 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3515 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3516 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));