1 /* Copyright (C) 2007-2015 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, see <http://www.gnu.org/licenses/>.
18 #include "translation-table.h"
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/compiler.h>
26 #include <linux/crc32c.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/lockdep.h>
36 #include <linux/netdevice.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42 #include <linux/stddef.h>
43 #include <linux/string.h>
44 #include <linux/workqueue.h>
45 #include <net/net_namespace.h>
47 #include "bridge_loop_avoidance.h"
48 #include "hard-interface.h"
50 #include "multicast.h"
51 #include "originator.h"
53 #include "soft-interface.h"
56 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
57 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
59 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
61 struct batadv_orig_node *orig_node);
62 static void batadv_tt_purge(struct work_struct *work);
64 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
65 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
66 struct batadv_orig_node *orig_node,
67 const unsigned char *addr,
68 unsigned short vid, const char *message,
71 /* returns 1 if they are the same mac addr and vid */
72 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
74 const void *data1 = container_of(node, struct batadv_tt_common_entry,
76 const struct batadv_tt_common_entry *tt1 = data1;
77 const struct batadv_tt_common_entry *tt2 = data2;
79 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
83 * batadv_choose_tt - return the index of the tt entry in the hash table
84 * @data: pointer to the tt_common_entry object to map
85 * @size: the size of the hash table
87 * Returns the hash index where the object represented by 'data' should be
90 static inline u32 batadv_choose_tt(const void *data, u32 size)
92 struct batadv_tt_common_entry *tt;
95 tt = (struct batadv_tt_common_entry *)data;
96 hash = jhash(&tt->addr, ETH_ALEN, hash);
97 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
103 * batadv_tt_hash_find - look for a client in the given hash table
104 * @hash: the hash table to search
105 * @addr: the mac address of the client to look for
106 * @vid: VLAN identifier
108 * Returns a pointer to the tt_common struct belonging to the searched client if
109 * found, NULL otherwise.
111 static struct batadv_tt_common_entry *
112 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
115 struct hlist_head *head;
116 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
122 ether_addr_copy(to_search.addr, addr);
125 index = batadv_choose_tt(&to_search, hash->size);
126 head = &hash->table[index];
129 hlist_for_each_entry_rcu(tt, head, hash_entry) {
130 if (!batadv_compare_eth(tt, addr))
136 if (!atomic_inc_not_zero(&tt->refcount))
148 * batadv_tt_local_hash_find - search the local table for a given client
149 * @bat_priv: the bat priv with all the soft interface information
150 * @addr: the mac address of the client to look for
151 * @vid: VLAN identifier
153 * Returns a pointer to the corresponding tt_local_entry struct if the client is
154 * found, NULL otherwise.
156 static struct batadv_tt_local_entry *
157 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
160 struct batadv_tt_common_entry *tt_common_entry;
161 struct batadv_tt_local_entry *tt_local_entry = NULL;
163 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
166 tt_local_entry = container_of(tt_common_entry,
167 struct batadv_tt_local_entry,
169 return tt_local_entry;
173 * batadv_tt_global_hash_find - search the global table for a given client
174 * @bat_priv: the bat priv with all the soft interface information
175 * @addr: the mac address of the client to look for
176 * @vid: VLAN identifier
178 * Returns a pointer to the corresponding tt_global_entry struct if the client
179 * is found, NULL otherwise.
181 static struct batadv_tt_global_entry *
182 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
185 struct batadv_tt_common_entry *tt_common_entry;
186 struct batadv_tt_global_entry *tt_global_entry = NULL;
188 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
191 tt_global_entry = container_of(tt_common_entry,
192 struct batadv_tt_global_entry,
194 return tt_global_entry;
198 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
200 if (atomic_dec_and_test(&tt_local_entry->common.refcount)) {
201 batadv_softif_vlan_free_ref(tt_local_entry->vlan);
203 kfree_rcu(tt_local_entry, common.rcu);
208 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
209 * tt_global_entry and possibly free it
210 * @tt_global_entry: the object to free
213 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
215 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
216 batadv_tt_global_del_orig_list(tt_global_entry);
217 kfree_rcu(tt_global_entry, common.rcu);
222 * batadv_tt_global_hash_count - count the number of orig entries
223 * @hash: hash table containing the tt entries
224 * @addr: the mac address of the client to count entries for
225 * @vid: VLAN identifier
227 * Return the number of originators advertising the given address/data
228 * (excluding ourself).
230 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
231 const u8 *addr, unsigned short vid)
233 struct batadv_tt_global_entry *tt_global_entry;
236 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
237 if (!tt_global_entry)
240 count = atomic_read(&tt_global_entry->orig_list_count);
241 batadv_tt_global_entry_free_ref(tt_global_entry);
247 * batadv_tt_local_size_mod - change the size by v of the local table identified
249 * @bat_priv: the bat priv with all the soft interface information
250 * @vid: the VLAN identifier of the sub-table to change
251 * @v: the amount to sum to the local table size
253 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
254 unsigned short vid, int v)
256 struct batadv_softif_vlan *vlan;
258 vlan = batadv_softif_vlan_get(bat_priv, vid);
262 atomic_add(v, &vlan->tt.num_entries);
264 batadv_softif_vlan_free_ref(vlan);
268 * batadv_tt_local_size_inc - increase by one the local table size for the given
270 * @bat_priv: the bat priv with all the soft interface information
271 * @vid: the VLAN identifier
273 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
276 batadv_tt_local_size_mod(bat_priv, vid, 1);
280 * batadv_tt_local_size_dec - decrease by one the local table size for the given
282 * @bat_priv: the bat priv with all the soft interface information
283 * @vid: the VLAN identifier
285 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
288 batadv_tt_local_size_mod(bat_priv, vid, -1);
292 * batadv_tt_global_size_mod - change the size by v of the local table
294 * @bat_priv: the bat priv with all the soft interface information
295 * @vid: the VLAN identifier
296 * @v: the amount to sum to the global table size
298 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
299 unsigned short vid, int v)
301 struct batadv_orig_node_vlan *vlan;
303 vlan = batadv_orig_node_vlan_new(orig_node, vid);
307 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
308 spin_lock_bh(&orig_node->vlan_list_lock);
309 if (!hlist_unhashed(&vlan->list)) {
310 hlist_del_init_rcu(&vlan->list);
311 batadv_orig_node_vlan_free_ref(vlan);
313 spin_unlock_bh(&orig_node->vlan_list_lock);
316 batadv_orig_node_vlan_free_ref(vlan);
320 * batadv_tt_global_size_inc - increase by one the global table size for the
322 * @orig_node: the originator which global table size has to be decreased
323 * @vid: the vlan identifier
325 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
328 batadv_tt_global_size_mod(orig_node, vid, 1);
332 * batadv_tt_global_size_dec - decrease by one the global table size for the
334 * @orig_node: the originator which global table size has to be decreased
335 * @vid: the vlan identifier
337 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
340 batadv_tt_global_size_mod(orig_node, vid, -1);
344 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
345 * queue for free after rcu grace period
346 * @orig_entry: tt orig entry to be free'd
349 batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
351 batadv_orig_node_free_ref(orig_entry->orig_node);
352 kfree_rcu(orig_entry, rcu);
356 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
358 if (!atomic_dec_and_test(&orig_entry->refcount))
361 batadv_tt_orig_list_entry_release(orig_entry);
365 * batadv_tt_local_event - store a local TT event (ADD/DEL)
366 * @bat_priv: the bat priv with all the soft interface information
367 * @tt_local_entry: the TT entry involved in the event
368 * @event_flags: flags to store in the event structure
370 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
371 struct batadv_tt_local_entry *tt_local_entry,
374 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
375 struct batadv_tt_common_entry *common = &tt_local_entry->common;
376 u8 flags = common->flags | event_flags;
377 bool event_removed = false;
378 bool del_op_requested, del_op_entry;
380 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
384 tt_change_node->change.flags = flags;
385 memset(tt_change_node->change.reserved, 0,
386 sizeof(tt_change_node->change.reserved));
387 ether_addr_copy(tt_change_node->change.addr, common->addr);
388 tt_change_node->change.vid = htons(common->vid);
390 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
392 /* check for ADD+DEL or DEL+ADD events */
393 spin_lock_bh(&bat_priv->tt.changes_list_lock);
394 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
396 if (!batadv_compare_eth(entry->change.addr, common->addr))
399 /* DEL+ADD in the same orig interval have no effect and can be
400 * removed to avoid silly behaviour on the receiver side. The
401 * other way around (ADD+DEL) can happen in case of roaming of
402 * a client still in the NEW state. Roaming of NEW clients is
403 * now possible due to automatically recognition of "temporary"
406 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
407 if (!del_op_requested && del_op_entry)
409 if (del_op_requested && !del_op_entry)
412 /* this is a second add in the same originator interval. It
413 * means that flags have been changed: update them!
415 if (!del_op_requested && !del_op_entry)
416 entry->change.flags = flags;
420 list_del(&entry->list);
422 kfree(tt_change_node);
423 event_removed = true;
427 /* track the change in the OGMinterval list */
428 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
431 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
434 atomic_dec(&bat_priv->tt.local_changes);
436 atomic_inc(&bat_priv->tt.local_changes);
440 * batadv_tt_len - compute length in bytes of given number of tt changes
441 * @changes_num: number of tt changes
443 * Returns computed length in bytes.
445 static int batadv_tt_len(int changes_num)
447 return changes_num * sizeof(struct batadv_tvlv_tt_change);
451 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
452 * @tt_len: available space
454 * Returns the number of entries.
456 static u16 batadv_tt_entries(u16 tt_len)
458 return tt_len / batadv_tt_len(1);
462 * batadv_tt_local_table_transmit_size - calculates the local translation table
463 * size when transmitted over the air
464 * @bat_priv: the bat priv with all the soft interface information
466 * Returns local translation table size in bytes.
468 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
471 u16 tt_local_entries = 0;
472 struct batadv_softif_vlan *vlan;
476 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
478 tt_local_entries += atomic_read(&vlan->tt.num_entries);
482 /* header size of tvlv encapsulated tt response payload */
483 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
484 hdr_size += sizeof(struct batadv_tvlv_hdr);
485 hdr_size += sizeof(struct batadv_tvlv_tt_data);
486 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
488 return hdr_size + batadv_tt_len(tt_local_entries);
491 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
493 if (bat_priv->tt.local_hash)
496 bat_priv->tt.local_hash = batadv_hash_new(1024);
498 if (!bat_priv->tt.local_hash)
501 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
502 &batadv_tt_local_hash_lock_class_key);
507 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
508 struct batadv_tt_global_entry *tt_global,
511 struct batadv_tt_global_entry *tt_removed_entry;
512 struct hlist_node *tt_removed_node;
514 batadv_dbg(BATADV_DBG_TT, bat_priv,
515 "Deleting global tt entry %pM (vid: %d): %s\n",
516 tt_global->common.addr,
517 BATADV_PRINT_VID(tt_global->common.vid), message);
519 tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
523 if (!tt_removed_node)
526 /* drop reference of remove hash entry */
527 tt_removed_entry = hlist_entry(tt_removed_node,
528 struct batadv_tt_global_entry,
530 batadv_tt_global_entry_free_ref(tt_removed_entry);
534 * batadv_tt_local_add - add a new client to the local table or update an
536 * @soft_iface: netdev struct of the mesh interface
537 * @addr: the mac address of the client to add
538 * @vid: VLAN identifier
539 * @ifindex: index of the interface where the client is connected to (useful to
540 * identify wireless clients)
541 * @mark: the value contained in the skb->mark field of the received packet (if
544 * Returns true if the client was successfully added, false otherwise.
546 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
547 unsigned short vid, int ifindex, u32 mark)
549 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
550 struct batadv_tt_local_entry *tt_local;
551 struct batadv_tt_global_entry *tt_global = NULL;
552 struct batadv_softif_vlan *vlan;
553 struct net_device *in_dev = NULL;
554 struct hlist_head *head;
555 struct batadv_tt_orig_list_entry *orig_entry;
556 int hash_added, table_size, packet_size_max;
558 bool roamed_back = false;
562 if (ifindex != BATADV_NULL_IFINDEX)
563 in_dev = dev_get_by_index(&init_net, ifindex);
565 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
567 if (!is_multicast_ether_addr(addr))
568 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
571 tt_local->last_seen = jiffies;
572 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
573 batadv_dbg(BATADV_DBG_TT, bat_priv,
574 "Re-adding pending client %pM (vid: %d)\n",
575 addr, BATADV_PRINT_VID(vid));
576 /* whatever the reason why the PENDING flag was set,
577 * this is a client which was enqueued to be removed in
578 * this orig_interval. Since it popped up again, the
579 * flag can be reset like it was never enqueued
581 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
585 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
586 batadv_dbg(BATADV_DBG_TT, bat_priv,
587 "Roaming client %pM (vid: %d) came back to its original location\n",
588 addr, BATADV_PRINT_VID(vid));
589 /* the ROAM flag is set because this client roamed away
590 * and the node got a roaming_advertisement message. Now
591 * that the client popped up again at its original
592 * location such flag can be unset
594 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
600 /* Ignore the client if we cannot send it in a full table response. */
601 table_size = batadv_tt_local_table_transmit_size(bat_priv);
602 table_size += batadv_tt_len(1);
603 packet_size_max = atomic_read(&bat_priv->packet_size_max);
604 if (table_size > packet_size_max) {
605 net_ratelimited_function(batadv_info, soft_iface,
606 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
607 table_size, packet_size_max, addr);
611 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
615 /* increase the refcounter of the related vlan */
616 vlan = batadv_softif_vlan_get(bat_priv, vid);
618 net_ratelimited_function(batadv_info, soft_iface,
619 "adding TT local entry %pM to non-existent VLAN %d\n",
620 addr, BATADV_PRINT_VID(vid));
626 batadv_dbg(BATADV_DBG_TT, bat_priv,
627 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
628 addr, BATADV_PRINT_VID(vid),
629 (u8)atomic_read(&bat_priv->tt.vn));
631 ether_addr_copy(tt_local->common.addr, addr);
632 /* The local entry has to be marked as NEW to avoid to send it in
633 * a full table response going out before the next ttvn increment
634 * (consistency check)
636 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
637 tt_local->common.vid = vid;
638 if (batadv_is_wifi_netdev(in_dev))
639 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
640 atomic_set(&tt_local->common.refcount, 2);
641 tt_local->last_seen = jiffies;
642 tt_local->common.added_at = tt_local->last_seen;
644 /* the batman interface mac and multicast addresses should never be
647 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
648 is_multicast_ether_addr(addr))
649 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
651 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
652 batadv_choose_tt, &tt_local->common,
653 &tt_local->common.hash_entry);
655 if (unlikely(hash_added != 0)) {
656 /* remove the reference for the hash */
657 batadv_tt_local_entry_free_ref(tt_local);
662 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
665 /* Check whether it is a roaming, but don't do anything if the roaming
666 * process has already been handled
668 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
669 /* These node are probably going to update their tt table */
670 head = &tt_global->orig_list;
672 hlist_for_each_entry_rcu(orig_entry, head, list) {
673 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
674 tt_global->common.vid,
675 orig_entry->orig_node);
679 batadv_tt_global_free(bat_priv, tt_global,
683 /* The global entry has to be marked as ROAMING and
684 * has to be kept for consistency purpose
686 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
687 tt_global->roam_at = jiffies;
691 /* store the current remote flags before altering them. This helps
692 * understanding is flags are changing or not
694 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
696 if (batadv_is_wifi_netdev(in_dev))
697 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
699 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
701 /* check the mark in the skb: if it's equal to the configured
702 * isolation_mark, it means the packet is coming from an isolated
705 match_mark = (mark & bat_priv->isolation_mark_mask);
706 if (bat_priv->isolation_mark_mask &&
707 match_mark == bat_priv->isolation_mark)
708 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
710 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
712 /* if any "dynamic" flag has been modified, resend an ADD event for this
713 * entry so that all the nodes can get the new flags
715 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
716 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
723 batadv_tt_local_entry_free_ref(tt_local);
725 batadv_tt_global_entry_free_ref(tt_global);
730 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
731 * within a TT Response directed to another node
732 * @orig_node: originator for which the TT data has to be prepared
733 * @tt_data: uninitialised pointer to the address of the TVLV buffer
734 * @tt_change: uninitialised pointer to the address of the area where the TT
735 * changed can be stored
736 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
737 * function reserves the amount of space needed to send the entire global TT
738 * table. In case of success the value is updated with the real amount of
741 * Allocate the needed amount of memory for the entire TT TVLV and write its
742 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
743 * objects, one per active VLAN served by the originator node.
745 * Return the size of the allocated buffer or 0 in case of failure.
748 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
749 struct batadv_tvlv_tt_data **tt_data,
750 struct batadv_tvlv_tt_change **tt_change,
757 struct batadv_tvlv_tt_vlan_data *tt_vlan;
758 struct batadv_orig_node_vlan *vlan;
761 spin_lock_bh(&orig_node->vlan_list_lock);
762 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
764 num_entries += atomic_read(&vlan->tt.num_entries);
767 change_offset = sizeof(**tt_data);
768 change_offset += num_vlan * sizeof(*tt_vlan);
770 /* if tt_len is negative, allocate the space needed by the full table */
772 *tt_len = batadv_tt_len(num_entries);
775 tvlv_len += change_offset;
777 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
783 (*tt_data)->flags = BATADV_NO_FLAGS;
784 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
785 (*tt_data)->num_vlan = htons(num_vlan);
787 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
788 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
789 tt_vlan->vid = htons(vlan->vid);
790 tt_vlan->crc = htonl(vlan->tt.crc);
795 tt_change_ptr = (u8 *)*tt_data + change_offset;
796 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
799 spin_unlock_bh(&orig_node->vlan_list_lock);
804 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
806 * @bat_priv: the bat priv with all the soft interface information
807 * @tt_data: uninitialised pointer to the address of the TVLV buffer
808 * @tt_change: uninitialised pointer to the address of the area where the TT
809 * changes can be stored
810 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
811 * function reserves the amount of space needed to send the entire local TT
812 * table. In case of success the value is updated with the real amount of
815 * Allocate the needed amount of memory for the entire TT TVLV and write its
816 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
817 * objects, one per active VLAN.
819 * Return the size of the allocated buffer or 0 in case of failure.
822 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
823 struct batadv_tvlv_tt_data **tt_data,
824 struct batadv_tvlv_tt_change **tt_change,
827 struct batadv_tvlv_tt_vlan_data *tt_vlan;
828 struct batadv_softif_vlan *vlan;
830 u16 vlan_entries = 0;
831 u16 total_entries = 0;
836 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
837 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
838 vlan_entries = atomic_read(&vlan->tt.num_entries);
839 if (vlan_entries < 1)
843 total_entries += vlan_entries;
846 change_offset = sizeof(**tt_data);
847 change_offset += num_vlan * sizeof(*tt_vlan);
849 /* if tt_len is negative, allocate the space needed by the full table */
851 *tt_len = batadv_tt_len(total_entries);
854 tvlv_len += change_offset;
856 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
862 (*tt_data)->flags = BATADV_NO_FLAGS;
863 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
864 (*tt_data)->num_vlan = htons(num_vlan);
866 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
867 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
868 vlan_entries = atomic_read(&vlan->tt.num_entries);
869 if (vlan_entries < 1)
872 tt_vlan->vid = htons(vlan->vid);
873 tt_vlan->crc = htonl(vlan->tt.crc);
878 tt_change_ptr = (u8 *)*tt_data + change_offset;
879 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
882 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
887 * batadv_tt_tvlv_container_update - update the translation table tvlv container
888 * after local tt changes have been committed
889 * @bat_priv: the bat priv with all the soft interface information
891 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
893 struct batadv_tt_change_node *entry, *safe;
894 struct batadv_tvlv_tt_data *tt_data;
895 struct batadv_tvlv_tt_change *tt_change;
896 int tt_diff_len, tt_change_len = 0;
897 int tt_diff_entries_num = 0;
898 int tt_diff_entries_count = 0;
901 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
902 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
904 /* if we have too many changes for one packet don't send any
905 * and wait for the tt table request which will be fragmented
907 if (tt_diff_len > bat_priv->soft_iface->mtu)
910 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
911 &tt_change, &tt_diff_len);
915 tt_data->flags = BATADV_TT_OGM_DIFF;
917 if (tt_diff_len == 0)
918 goto container_register;
920 spin_lock_bh(&bat_priv->tt.changes_list_lock);
921 atomic_set(&bat_priv->tt.local_changes, 0);
923 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
925 if (tt_diff_entries_count < tt_diff_entries_num) {
926 memcpy(tt_change + tt_diff_entries_count,
928 sizeof(struct batadv_tvlv_tt_change));
929 tt_diff_entries_count++;
931 list_del(&entry->list);
934 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
936 /* Keep the buffer for possible tt_request */
937 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
938 kfree(bat_priv->tt.last_changeset);
939 bat_priv->tt.last_changeset_len = 0;
940 bat_priv->tt.last_changeset = NULL;
941 tt_change_len = batadv_tt_len(tt_diff_entries_count);
942 /* check whether this new OGM has no changes due to size problems */
943 if (tt_diff_entries_count > 0) {
944 /* if kmalloc() fails we will reply with the full table
945 * instead of providing the diff
947 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
948 if (bat_priv->tt.last_changeset) {
949 memcpy(bat_priv->tt.last_changeset,
950 tt_change, tt_change_len);
951 bat_priv->tt.last_changeset_len = tt_diff_len;
954 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
957 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
962 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
964 struct net_device *net_dev = (struct net_device *)seq->private;
965 struct batadv_priv *bat_priv = netdev_priv(net_dev);
966 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
967 struct batadv_tt_common_entry *tt_common_entry;
968 struct batadv_tt_local_entry *tt_local;
969 struct batadv_hard_iface *primary_if;
970 struct hlist_head *head;
975 unsigned long last_seen_jiffies;
977 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
979 primary_if = batadv_seq_print_text_primary_if_get(seq);
984 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
985 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
986 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
987 "Flags", "Last seen", "CRC");
989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
993 hlist_for_each_entry_rcu(tt_common_entry,
995 tt_local = container_of(tt_common_entry,
996 struct batadv_tt_local_entry,
998 vid = tt_common_entry->vid;
999 last_seen_jiffies = jiffies - tt_local->last_seen;
1000 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1001 last_seen_secs = last_seen_msecs / 1000;
1002 last_seen_msecs = last_seen_msecs % 1000;
1004 no_purge = tt_common_entry->flags & np_flag;
1007 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1008 tt_common_entry->addr,
1009 BATADV_PRINT_VID(tt_common_entry->vid),
1010 ((tt_common_entry->flags &
1011 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1012 no_purge ? 'P' : '.',
1013 ((tt_common_entry->flags &
1014 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1015 ((tt_common_entry->flags &
1016 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1017 ((tt_common_entry->flags &
1018 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1019 ((tt_common_entry->flags &
1020 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1021 no_purge ? 0 : last_seen_secs,
1022 no_purge ? 0 : last_seen_msecs,
1023 tt_local->vlan->tt.crc);
1029 batadv_hardif_free_ref(primary_if);
1034 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1035 struct batadv_tt_local_entry *tt_local_entry,
1036 u16 flags, const char *message)
1038 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1040 /* The local client has to be marked as "pending to be removed" but has
1041 * to be kept in the table in order to send it in a full table
1042 * response issued before the net ttvn increment (consistency check)
1044 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1046 batadv_dbg(BATADV_DBG_TT, bat_priv,
1047 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1048 tt_local_entry->common.addr,
1049 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1053 * batadv_tt_local_remove - logically remove an entry from the local table
1054 * @bat_priv: the bat priv with all the soft interface information
1055 * @addr: the MAC address of the client to remove
1056 * @vid: VLAN identifier
1057 * @message: message to append to the log on deletion
1058 * @roaming: true if the deletion is due to a roaming event
1060 * Returns the flags assigned to the local entry before being deleted
1062 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1063 unsigned short vid, const char *message,
1066 struct batadv_tt_local_entry *tt_removed_entry;
1067 struct batadv_tt_local_entry *tt_local_entry;
1068 u16 flags, curr_flags = BATADV_NO_FLAGS;
1069 struct hlist_node *tt_removed_node;
1071 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1072 if (!tt_local_entry)
1075 curr_flags = tt_local_entry->common.flags;
1077 flags = BATADV_TT_CLIENT_DEL;
1078 /* if this global entry addition is due to a roaming, the node has to
1079 * mark the local entry as "roamed" in order to correctly reroute
1083 flags |= BATADV_TT_CLIENT_ROAM;
1084 /* mark the local client as ROAMed */
1085 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1088 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1089 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1093 /* if this client has been added right now, it is possible to
1094 * immediately purge it
1096 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1098 tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1101 &tt_local_entry->common);
1102 if (!tt_removed_node)
1105 /* drop reference of remove hash entry */
1106 tt_removed_entry = hlist_entry(tt_removed_node,
1107 struct batadv_tt_local_entry,
1109 batadv_tt_local_entry_free_ref(tt_removed_entry);
1113 batadv_tt_local_entry_free_ref(tt_local_entry);
1119 * batadv_tt_local_purge_list - purge inactive tt local entries
1120 * @bat_priv: the bat priv with all the soft interface information
1121 * @head: pointer to the list containing the local tt entries
1122 * @timeout: parameter deciding whether a given tt local entry is considered
1125 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1126 struct hlist_head *head,
1129 struct batadv_tt_local_entry *tt_local_entry;
1130 struct batadv_tt_common_entry *tt_common_entry;
1131 struct hlist_node *node_tmp;
1133 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1135 tt_local_entry = container_of(tt_common_entry,
1136 struct batadv_tt_local_entry,
1138 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1141 /* entry already marked for deletion */
1142 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1145 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1148 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1149 BATADV_TT_CLIENT_DEL, "timed out");
1154 * batadv_tt_local_purge - purge inactive tt local entries
1155 * @bat_priv: the bat priv with all the soft interface information
1156 * @timeout: parameter deciding whether a given tt local entry is considered
1159 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1162 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1163 struct hlist_head *head;
1164 spinlock_t *list_lock; /* protects write access to the hash lists */
1167 for (i = 0; i < hash->size; i++) {
1168 head = &hash->table[i];
1169 list_lock = &hash->list_locks[i];
1171 spin_lock_bh(list_lock);
1172 batadv_tt_local_purge_list(bat_priv, head, timeout);
1173 spin_unlock_bh(list_lock);
1177 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1179 struct batadv_hashtable *hash;
1180 spinlock_t *list_lock; /* protects write access to the hash lists */
1181 struct batadv_tt_common_entry *tt_common_entry;
1182 struct batadv_tt_local_entry *tt_local;
1183 struct hlist_node *node_tmp;
1184 struct hlist_head *head;
1187 if (!bat_priv->tt.local_hash)
1190 hash = bat_priv->tt.local_hash;
1192 for (i = 0; i < hash->size; i++) {
1193 head = &hash->table[i];
1194 list_lock = &hash->list_locks[i];
1196 spin_lock_bh(list_lock);
1197 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1199 hlist_del_rcu(&tt_common_entry->hash_entry);
1200 tt_local = container_of(tt_common_entry,
1201 struct batadv_tt_local_entry,
1204 batadv_tt_local_entry_free_ref(tt_local);
1206 spin_unlock_bh(list_lock);
1209 batadv_hash_destroy(hash);
1211 bat_priv->tt.local_hash = NULL;
1214 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1216 if (bat_priv->tt.global_hash)
1219 bat_priv->tt.global_hash = batadv_hash_new(1024);
1221 if (!bat_priv->tt.global_hash)
1224 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1225 &batadv_tt_global_hash_lock_class_key);
1230 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1232 struct batadv_tt_change_node *entry, *safe;
1234 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1236 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1238 list_del(&entry->list);
1242 atomic_set(&bat_priv->tt.local_changes, 0);
1243 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1246 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1247 * batadv_tt_global_entry list
1249 * returns it with an increased refcounter, NULL if not found
1251 static struct batadv_tt_orig_list_entry *
1252 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1253 const struct batadv_orig_node *orig_node)
1255 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1256 const struct hlist_head *head;
1259 head = &entry->orig_list;
1260 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1261 if (tmp_orig_entry->orig_node != orig_node)
1263 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1266 orig_entry = tmp_orig_entry;
1274 /* find out if an orig_node is already in the list of a tt_global_entry.
1275 * returns true if found, false otherwise
1278 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1279 const struct batadv_orig_node *orig_node,
1282 struct batadv_tt_orig_list_entry *orig_entry;
1285 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1290 *flags = orig_entry->flags;
1292 batadv_tt_orig_list_entry_free_ref(orig_entry);
1299 * batadv_tt_global_sync_flags - update TT sync flags
1300 * @tt_global: the TT global entry to update sync flags in
1302 * Updates the sync flag bits in the tt_global flag attribute with a logical
1303 * OR of all sync flags from any of its TT orig entries.
1306 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1308 struct batadv_tt_orig_list_entry *orig_entry;
1309 const struct hlist_head *head;
1310 u16 flags = BATADV_NO_FLAGS;
1313 head = &tt_global->orig_list;
1314 hlist_for_each_entry_rcu(orig_entry, head, list)
1315 flags |= orig_entry->flags;
1318 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1319 tt_global->common.flags = flags;
1323 * batadv_tt_global_orig_entry_add - add or update a TT orig entry
1324 * @tt_global: the TT global entry to add an orig entry in
1325 * @orig_node: the originator to add an orig entry for
1326 * @ttvn: translation table version number of this changeset
1327 * @flags: TT sync flags
1330 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1331 struct batadv_orig_node *orig_node, int ttvn,
1334 struct batadv_tt_orig_list_entry *orig_entry;
1336 spin_lock_bh(&tt_global->list_lock);
1338 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1340 /* refresh the ttvn: the current value could be a bogus one that
1341 * was added during a "temporary client detection"
1343 orig_entry->ttvn = ttvn;
1344 orig_entry->flags = flags;
1348 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1352 INIT_HLIST_NODE(&orig_entry->list);
1353 atomic_inc(&orig_node->refcount);
1354 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1355 orig_entry->orig_node = orig_node;
1356 orig_entry->ttvn = ttvn;
1357 orig_entry->flags = flags;
1358 atomic_set(&orig_entry->refcount, 2);
1360 hlist_add_head_rcu(&orig_entry->list,
1361 &tt_global->orig_list);
1362 atomic_inc(&tt_global->orig_list_count);
1365 batadv_tt_global_sync_flags(tt_global);
1368 batadv_tt_orig_list_entry_free_ref(orig_entry);
1370 spin_unlock_bh(&tt_global->list_lock);
1374 * batadv_tt_global_add - add a new TT global entry or update an existing one
1375 * @bat_priv: the bat priv with all the soft interface information
1376 * @orig_node: the originator announcing the client
1377 * @tt_addr: the mac address of the non-mesh client
1378 * @vid: VLAN identifier
1379 * @flags: TT flags that have to be set for this non-mesh client
1380 * @ttvn: the tt version number ever announcing this non-mesh client
1382 * Add a new TT global entry for the given originator. If the entry already
1383 * exists add a new reference to the given originator (a global entry can have
1384 * references to multiple originators) and adjust the flags attribute to reflect
1385 * the function argument.
1386 * If a TT local entry exists for this non-mesh client remove it.
1388 * The caller must hold orig_node refcount.
1390 * Return true if the new entry has been added, false otherwise
1392 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1393 struct batadv_orig_node *orig_node,
1394 const unsigned char *tt_addr,
1395 unsigned short vid, u16 flags, u8 ttvn)
1397 struct batadv_tt_global_entry *tt_global_entry;
1398 struct batadv_tt_local_entry *tt_local_entry;
1401 struct batadv_tt_common_entry *common;
1404 /* ignore global entries from backbone nodes */
1405 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1408 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1409 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1411 /* if the node already has a local client for this entry, it has to wait
1412 * for a roaming advertisement instead of manually messing up the global
1415 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1416 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1419 if (!tt_global_entry) {
1420 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1421 if (!tt_global_entry)
1424 common = &tt_global_entry->common;
1425 ether_addr_copy(common->addr, tt_addr);
1428 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1430 tt_global_entry->roam_at = 0;
1431 /* node must store current time in case of roaming. This is
1432 * needed to purge this entry out on timeout (if nobody claims
1435 if (flags & BATADV_TT_CLIENT_ROAM)
1436 tt_global_entry->roam_at = jiffies;
1437 atomic_set(&common->refcount, 2);
1438 common->added_at = jiffies;
1440 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1441 atomic_set(&tt_global_entry->orig_list_count, 0);
1442 spin_lock_init(&tt_global_entry->list_lock);
1444 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1446 batadv_choose_tt, common,
1447 &common->hash_entry);
1449 if (unlikely(hash_added != 0)) {
1450 /* remove the reference for the hash */
1451 batadv_tt_global_entry_free_ref(tt_global_entry);
1455 common = &tt_global_entry->common;
1456 /* If there is already a global entry, we can use this one for
1458 * But if we are trying to add a temporary client then here are
1459 * two options at this point:
1460 * 1) the global client is not a temporary client: the global
1461 * client has to be left as it is, temporary information
1462 * should never override any already known client state
1463 * 2) the global client is a temporary client: purge the
1464 * originator list and add the new one orig_entry
1466 if (flags & BATADV_TT_CLIENT_TEMP) {
1467 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1469 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1472 batadv_tt_global_del_orig_list(tt_global_entry);
1473 goto add_orig_entry;
1476 /* if the client was temporary added before receiving the first
1477 * OGM announcing it, we have to clear the TEMP flag. Also,
1478 * remove the previous temporary orig node and re-add it
1479 * if required. If the orig entry changed, the new one which
1480 * is a non-temporary entry is preferred.
1482 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1483 batadv_tt_global_del_orig_list(tt_global_entry);
1484 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1487 /* the change can carry possible "attribute" flags like the
1488 * TT_CLIENT_WIFI, therefore they have to be copied in the
1491 tt_global_entry->common.flags |= flags & (~BATADV_TT_SYNC_MASK);
1493 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1494 * one originator left in the list and we previously received a
1495 * delete + roaming change for this originator.
1497 * We should first delete the old originator before adding the
1500 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1501 batadv_tt_global_del_orig_list(tt_global_entry);
1502 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1503 tt_global_entry->roam_at = 0;
1507 /* add the new orig_entry (if needed) or update it */
1508 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1509 flags & BATADV_TT_SYNC_MASK);
1511 batadv_dbg(BATADV_DBG_TT, bat_priv,
1512 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1513 common->addr, BATADV_PRINT_VID(common->vid),
1518 /* Do not remove multicast addresses from the local hash on
1521 if (is_multicast_ether_addr(tt_addr))
1524 /* remove address from local hash if present */
1525 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1526 "global tt received",
1527 flags & BATADV_TT_CLIENT_ROAM);
1528 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1530 if (!(flags & BATADV_TT_CLIENT_ROAM))
1531 /* this is a normal global add. Therefore the client is not in a
1532 * roaming state anymore.
1534 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1537 if (tt_global_entry)
1538 batadv_tt_global_entry_free_ref(tt_global_entry);
1540 batadv_tt_local_entry_free_ref(tt_local_entry);
1545 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1546 * @bat_priv: the bat priv with all the soft interface information
1547 * @tt_global_entry: global translation table entry to be analyzed
1549 * This functon assumes the caller holds rcu_read_lock().
1550 * Returns best originator list entry or NULL on errors.
1552 static struct batadv_tt_orig_list_entry *
1553 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1554 struct batadv_tt_global_entry *tt_global_entry)
1556 struct batadv_neigh_node *router, *best_router = NULL;
1557 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1558 struct hlist_head *head;
1559 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1561 head = &tt_global_entry->orig_list;
1562 hlist_for_each_entry_rcu(orig_entry, head, list) {
1563 router = batadv_orig_router_get(orig_entry->orig_node,
1569 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1570 best_router, BATADV_IF_DEFAULT) <= 0) {
1571 batadv_neigh_node_free_ref(router);
1575 /* release the refcount for the "old" best */
1577 batadv_neigh_node_free_ref(best_router);
1579 best_entry = orig_entry;
1580 best_router = router;
1584 batadv_neigh_node_free_ref(best_router);
1590 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1591 * for this global entry
1592 * @bat_priv: the bat priv with all the soft interface information
1593 * @tt_global_entry: global translation table entry to be printed
1594 * @seq: debugfs table seq_file struct
1596 * This functon assumes the caller holds rcu_read_lock().
1599 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1600 struct batadv_tt_global_entry *tt_global_entry,
1601 struct seq_file *seq)
1603 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1604 struct batadv_tt_common_entry *tt_common_entry;
1605 struct batadv_orig_node_vlan *vlan;
1606 struct hlist_head *head;
1610 tt_common_entry = &tt_global_entry->common;
1611 flags = tt_common_entry->flags;
1613 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1615 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1616 tt_common_entry->vid);
1619 " * Cannot retrieve VLAN %d for originator %pM\n",
1620 BATADV_PRINT_VID(tt_common_entry->vid),
1621 best_entry->orig_node->orig);
1625 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1627 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1628 '*', tt_global_entry->common.addr,
1629 BATADV_PRINT_VID(tt_global_entry->common.vid),
1630 best_entry->ttvn, best_entry->orig_node->orig,
1631 last_ttvn, vlan->tt.crc,
1632 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1633 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1634 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1635 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1637 batadv_orig_node_vlan_free_ref(vlan);
1641 head = &tt_global_entry->orig_list;
1643 hlist_for_each_entry_rcu(orig_entry, head, list) {
1644 if (best_entry == orig_entry)
1647 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1648 tt_common_entry->vid);
1651 " + Cannot retrieve VLAN %d for originator %pM\n",
1652 BATADV_PRINT_VID(tt_common_entry->vid),
1653 orig_entry->orig_node->orig);
1657 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1659 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1660 '+', tt_global_entry->common.addr,
1661 BATADV_PRINT_VID(tt_global_entry->common.vid),
1662 orig_entry->ttvn, orig_entry->orig_node->orig,
1663 last_ttvn, vlan->tt.crc,
1664 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1665 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1666 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1667 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1669 batadv_orig_node_vlan_free_ref(vlan);
1673 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1675 struct net_device *net_dev = (struct net_device *)seq->private;
1676 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1677 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1678 struct batadv_tt_common_entry *tt_common_entry;
1679 struct batadv_tt_global_entry *tt_global;
1680 struct batadv_hard_iface *primary_if;
1681 struct hlist_head *head;
1684 primary_if = batadv_seq_print_text_primary_if_get(seq);
1689 "Globally announced TT entries received via the mesh %s\n",
1691 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1692 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1695 for (i = 0; i < hash->size; i++) {
1696 head = &hash->table[i];
1699 hlist_for_each_entry_rcu(tt_common_entry,
1701 tt_global = container_of(tt_common_entry,
1702 struct batadv_tt_global_entry,
1704 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1710 batadv_hardif_free_ref(primary_if);
1715 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1716 * @tt_global_entry: the global entry to remove the orig_entry from
1717 * @orig_entry: the orig entry to remove and free
1719 * Remove an orig_entry from its list in the given tt_global_entry and
1720 * free this orig_entry afterwards.
1722 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1726 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1727 struct batadv_tt_orig_list_entry *orig_entry)
1729 lockdep_assert_held(&tt_global_entry->list_lock);
1731 batadv_tt_global_size_dec(orig_entry->orig_node,
1732 tt_global_entry->common.vid);
1733 atomic_dec(&tt_global_entry->orig_list_count);
1734 /* requires holding tt_global_entry->list_lock and orig_entry->list
1735 * being part of a list
1737 hlist_del_rcu(&orig_entry->list);
1738 batadv_tt_orig_list_entry_free_ref(orig_entry);
1741 /* deletes the orig list of a tt_global_entry */
1743 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1745 struct hlist_head *head;
1746 struct hlist_node *safe;
1747 struct batadv_tt_orig_list_entry *orig_entry;
1749 spin_lock_bh(&tt_global_entry->list_lock);
1750 head = &tt_global_entry->orig_list;
1751 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1752 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1753 spin_unlock_bh(&tt_global_entry->list_lock);
1757 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1758 * @bat_priv: the bat priv with all the soft interface information
1759 * @tt_global_entry: the global entry to remove the orig_node from
1760 * @orig_node: the originator announcing the client
1761 * @message: message to append to the log on deletion
1763 * Remove the given orig_node and its according orig_entry from the given
1767 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1768 struct batadv_tt_global_entry *tt_global_entry,
1769 struct batadv_orig_node *orig_node,
1770 const char *message)
1772 struct hlist_head *head;
1773 struct hlist_node *safe;
1774 struct batadv_tt_orig_list_entry *orig_entry;
1777 spin_lock_bh(&tt_global_entry->list_lock);
1778 head = &tt_global_entry->orig_list;
1779 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1780 if (orig_entry->orig_node == orig_node) {
1781 vid = tt_global_entry->common.vid;
1782 batadv_dbg(BATADV_DBG_TT, bat_priv,
1783 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1785 tt_global_entry->common.addr,
1786 BATADV_PRINT_VID(vid), message);
1787 _batadv_tt_global_del_orig_entry(tt_global_entry,
1791 spin_unlock_bh(&tt_global_entry->list_lock);
1794 /* If the client is to be deleted, we check if it is the last origantor entry
1795 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1796 * timer, otherwise we simply remove the originator scheduled for deletion.
1799 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1800 struct batadv_tt_global_entry *tt_global_entry,
1801 struct batadv_orig_node *orig_node,
1802 const char *message)
1804 bool last_entry = true;
1805 struct hlist_head *head;
1806 struct batadv_tt_orig_list_entry *orig_entry;
1808 /* no local entry exists, case 1:
1809 * Check if this is the last one or if other entries exist.
1813 head = &tt_global_entry->orig_list;
1814 hlist_for_each_entry_rcu(orig_entry, head, list) {
1815 if (orig_entry->orig_node != orig_node) {
1823 /* its the last one, mark for roaming. */
1824 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1825 tt_global_entry->roam_at = jiffies;
1827 /* there is another entry, we can simply delete this
1828 * one and can still use the other one.
1830 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1831 orig_node, message);
1835 * batadv_tt_global_del - remove a client from the global table
1836 * @bat_priv: the bat priv with all the soft interface information
1837 * @orig_node: an originator serving this client
1838 * @addr: the mac address of the client
1839 * @vid: VLAN identifier
1840 * @message: a message explaining the reason for deleting the client to print
1841 * for debugging purpose
1842 * @roaming: true if the deletion has been triggered by a roaming event
1844 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1845 struct batadv_orig_node *orig_node,
1846 const unsigned char *addr, unsigned short vid,
1847 const char *message, bool roaming)
1849 struct batadv_tt_global_entry *tt_global_entry;
1850 struct batadv_tt_local_entry *local_entry = NULL;
1852 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1853 if (!tt_global_entry)
1857 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1858 orig_node, message);
1860 if (hlist_empty(&tt_global_entry->orig_list))
1861 batadv_tt_global_free(bat_priv, tt_global_entry,
1867 /* if we are deleting a global entry due to a roam
1868 * event, there are two possibilities:
1869 * 1) the client roamed from node A to node B => if there
1870 * is only one originator left for this client, we mark
1871 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1872 * wait for node B to claim it. In case of timeout
1873 * the entry is purged.
1875 * If there are other originators left, we directly delete
1877 * 2) the client roamed to us => we can directly delete
1878 * the global entry, since it is useless now.
1880 local_entry = batadv_tt_local_hash_find(bat_priv,
1881 tt_global_entry->common.addr,
1884 /* local entry exists, case 2: client roamed to us. */
1885 batadv_tt_global_del_orig_list(tt_global_entry);
1886 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1888 /* no local entry exists, case 1: check for roaming */
1889 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1890 orig_node, message);
1893 if (tt_global_entry)
1894 batadv_tt_global_entry_free_ref(tt_global_entry);
1896 batadv_tt_local_entry_free_ref(local_entry);
1900 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1901 * given originator matching the provided vid
1902 * @bat_priv: the bat priv with all the soft interface information
1903 * @orig_node: the originator owning the entries to remove
1904 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1906 * @message: debug message to print as "reason"
1908 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1909 struct batadv_orig_node *orig_node,
1911 const char *message)
1913 struct batadv_tt_global_entry *tt_global;
1914 struct batadv_tt_common_entry *tt_common_entry;
1916 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1917 struct hlist_node *safe;
1918 struct hlist_head *head;
1919 spinlock_t *list_lock; /* protects write access to the hash lists */
1925 for (i = 0; i < hash->size; i++) {
1926 head = &hash->table[i];
1927 list_lock = &hash->list_locks[i];
1929 spin_lock_bh(list_lock);
1930 hlist_for_each_entry_safe(tt_common_entry, safe,
1932 /* remove only matching entries */
1933 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1936 tt_global = container_of(tt_common_entry,
1937 struct batadv_tt_global_entry,
1940 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1941 orig_node, message);
1943 if (hlist_empty(&tt_global->orig_list)) {
1944 vid = tt_global->common.vid;
1945 batadv_dbg(BATADV_DBG_TT, bat_priv,
1946 "Deleting global tt entry %pM (vid: %d): %s\n",
1947 tt_global->common.addr,
1948 BATADV_PRINT_VID(vid), message);
1949 hlist_del_rcu(&tt_common_entry->hash_entry);
1950 batadv_tt_global_entry_free_ref(tt_global);
1953 spin_unlock_bh(list_lock);
1955 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1958 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1962 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1963 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1965 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1966 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1968 *msg = "Roaming timeout\n";
1971 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1972 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1974 *msg = "Temporary client timeout\n";
1980 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1982 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1983 struct hlist_head *head;
1984 struct hlist_node *node_tmp;
1985 spinlock_t *list_lock; /* protects write access to the hash lists */
1988 struct batadv_tt_common_entry *tt_common;
1989 struct batadv_tt_global_entry *tt_global;
1991 for (i = 0; i < hash->size; i++) {
1992 head = &hash->table[i];
1993 list_lock = &hash->list_locks[i];
1995 spin_lock_bh(list_lock);
1996 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1998 tt_global = container_of(tt_common,
1999 struct batadv_tt_global_entry,
2002 if (!batadv_tt_global_to_purge(tt_global, &msg))
2005 batadv_dbg(BATADV_DBG_TT, bat_priv,
2006 "Deleting global tt entry %pM (vid: %d): %s\n",
2007 tt_global->common.addr,
2008 BATADV_PRINT_VID(tt_global->common.vid),
2011 hlist_del_rcu(&tt_common->hash_entry);
2013 batadv_tt_global_entry_free_ref(tt_global);
2015 spin_unlock_bh(list_lock);
2019 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2021 struct batadv_hashtable *hash;
2022 spinlock_t *list_lock; /* protects write access to the hash lists */
2023 struct batadv_tt_common_entry *tt_common_entry;
2024 struct batadv_tt_global_entry *tt_global;
2025 struct hlist_node *node_tmp;
2026 struct hlist_head *head;
2029 if (!bat_priv->tt.global_hash)
2032 hash = bat_priv->tt.global_hash;
2034 for (i = 0; i < hash->size; i++) {
2035 head = &hash->table[i];
2036 list_lock = &hash->list_locks[i];
2038 spin_lock_bh(list_lock);
2039 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2041 hlist_del_rcu(&tt_common_entry->hash_entry);
2042 tt_global = container_of(tt_common_entry,
2043 struct batadv_tt_global_entry,
2045 batadv_tt_global_entry_free_ref(tt_global);
2047 spin_unlock_bh(list_lock);
2050 batadv_hash_destroy(hash);
2052 bat_priv->tt.global_hash = NULL;
2056 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2057 struct batadv_tt_global_entry *tt_global_entry)
2061 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2062 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2065 /* check if the two clients are marked as isolated */
2066 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2067 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2074 * batadv_transtable_search - get the mesh destination for a given client
2075 * @bat_priv: the bat priv with all the soft interface information
2076 * @src: mac address of the source client
2077 * @addr: mac address of the destination client
2078 * @vid: VLAN identifier
2080 * Returns a pointer to the originator that was selected as destination in the
2081 * mesh for contacting the client 'addr', NULL otherwise.
2082 * In case of multiple originators serving the same client, the function returns
2083 * the best one (best in terms of metric towards the destination node).
2085 * If the two clients are AP isolated the function returns NULL.
2087 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2092 struct batadv_tt_local_entry *tt_local_entry = NULL;
2093 struct batadv_tt_global_entry *tt_global_entry = NULL;
2094 struct batadv_orig_node *orig_node = NULL;
2095 struct batadv_tt_orig_list_entry *best_entry;
2097 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2098 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2099 if (!tt_local_entry ||
2100 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2104 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2105 if (!tt_global_entry)
2108 /* check whether the clients should not communicate due to AP
2111 if (tt_local_entry &&
2112 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2116 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2117 /* found anything? */
2119 orig_node = best_entry->orig_node;
2120 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2125 if (tt_global_entry)
2126 batadv_tt_global_entry_free_ref(tt_global_entry);
2128 batadv_tt_local_entry_free_ref(tt_local_entry);
2134 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2135 * to the given orig_node
2136 * @bat_priv: the bat priv with all the soft interface information
2137 * @orig_node: originator for which the CRC should be computed
2138 * @vid: VLAN identifier for which the CRC32 has to be computed
2140 * This function computes the checksum for the global table corresponding to a
2141 * specific originator. In particular, the checksum is computed as follows: For
2142 * each client connected to the originator the CRC32C of the MAC address and the
2143 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2146 * The idea behind is that CRC32C should be used as much as possible in order to
2147 * produce a unique hash of the table, but since the order which is used to feed
2148 * the CRC32C function affects the result and since every node in the network
2149 * probably sorts the clients differently, the hash function cannot be directly
2150 * computed over the entire table. Hence the CRC32C is used only on
2151 * the single client entry, while all the results are then xor'ed together
2152 * because the XOR operation can combine them all while trying to reduce the
2153 * noise as much as possible.
2155 * Returns the checksum of the global table of a given originator.
2157 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2158 struct batadv_orig_node *orig_node,
2161 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2162 struct batadv_tt_orig_list_entry *tt_orig;
2163 struct batadv_tt_common_entry *tt_common;
2164 struct batadv_tt_global_entry *tt_global;
2165 struct hlist_head *head;
2166 u32 i, crc_tmp, crc = 0;
2170 for (i = 0; i < hash->size; i++) {
2171 head = &hash->table[i];
2174 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2175 tt_global = container_of(tt_common,
2176 struct batadv_tt_global_entry,
2178 /* compute the CRC only for entries belonging to the
2179 * VLAN identified by the vid passed as parameter
2181 if (tt_common->vid != vid)
2184 /* Roaming clients are in the global table for
2185 * consistency only. They don't have to be
2186 * taken into account while computing the
2189 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2191 /* Temporary clients have not been announced yet, so
2192 * they have to be skipped while computing the global
2195 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2198 /* find out if this global entry is announced by this
2201 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2206 /* use network order to read the VID: this ensures that
2207 * every node reads the bytes in the same order.
2209 tmp_vid = htons(tt_common->vid);
2210 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2212 /* compute the CRC on flags that have to be kept in sync
2215 flags = tt_orig->flags;
2216 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2218 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2220 batadv_tt_orig_list_entry_free_ref(tt_orig);
2229 * batadv_tt_local_crc - calculates the checksum of the local table
2230 * @bat_priv: the bat priv with all the soft interface information
2231 * @vid: VLAN identifier for which the CRC32 has to be computed
2233 * For details about the computation, please refer to the documentation for
2234 * batadv_tt_global_crc().
2236 * Returns the checksum of the local table
2238 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2241 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2242 struct batadv_tt_common_entry *tt_common;
2243 struct hlist_head *head;
2244 u32 i, crc_tmp, crc = 0;
2248 for (i = 0; i < hash->size; i++) {
2249 head = &hash->table[i];
2252 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2253 /* compute the CRC only for entries belonging to the
2254 * VLAN identified by vid
2256 if (tt_common->vid != vid)
2259 /* not yet committed clients have not to be taken into
2260 * account while computing the CRC
2262 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2265 /* use network order to read the VID: this ensures that
2266 * every node reads the bytes in the same order.
2268 tmp_vid = htons(tt_common->vid);
2269 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2271 /* compute the CRC on flags that have to be kept in sync
2274 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2275 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2277 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2286 * batadv_tt_req_node_release - free tt_req node entry
2287 * @ref: kref pointer of the tt req_node entry
2289 static void batadv_tt_req_node_release(struct kref *ref)
2291 struct batadv_tt_req_node *tt_req_node;
2293 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2299 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2300 * possibly release it
2301 * @tt_req_node: tt_req_node to be free'd
2303 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2305 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2308 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2310 struct batadv_tt_req_node *node;
2311 struct hlist_node *safe;
2313 spin_lock_bh(&bat_priv->tt.req_list_lock);
2315 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2316 hlist_del_init(&node->list);
2317 batadv_tt_req_node_put(node);
2320 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2323 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2324 struct batadv_orig_node *orig_node,
2325 const void *tt_buff,
2328 /* Replace the old buffer only if I received something in the
2329 * last OGM (the OGM could carry no changes)
2331 spin_lock_bh(&orig_node->tt_buff_lock);
2332 if (tt_buff_len > 0) {
2333 kfree(orig_node->tt_buff);
2334 orig_node->tt_buff_len = 0;
2335 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2336 if (orig_node->tt_buff) {
2337 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2338 orig_node->tt_buff_len = tt_buff_len;
2341 spin_unlock_bh(&orig_node->tt_buff_lock);
2344 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2346 struct batadv_tt_req_node *node;
2347 struct hlist_node *safe;
2349 spin_lock_bh(&bat_priv->tt.req_list_lock);
2350 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2351 if (batadv_has_timed_out(node->issued_at,
2352 BATADV_TT_REQUEST_TIMEOUT)) {
2353 hlist_del_init(&node->list);
2354 batadv_tt_req_node_put(node);
2357 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2361 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2362 * @bat_priv: the bat priv with all the soft interface information
2363 * @orig_node: orig node this request is being issued for
2365 * Returns the pointer to the new tt_req_node struct if no request
2366 * has already been issued for this orig_node, NULL otherwise.
2368 static struct batadv_tt_req_node *
2369 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2370 struct batadv_orig_node *orig_node)
2372 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2374 spin_lock_bh(&bat_priv->tt.req_list_lock);
2375 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2376 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2377 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2378 BATADV_TT_REQUEST_TIMEOUT))
2382 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2386 kref_init(&tt_req_node->refcount);
2387 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2388 tt_req_node->issued_at = jiffies;
2390 kref_get(&tt_req_node->refcount);
2391 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2393 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2398 * batadv_tt_local_valid - verify that given tt entry is a valid one
2399 * @entry_ptr: to be checked local tt entry
2400 * @data_ptr: not used but definition required to satisfy the callback prototype
2402 * Returns 1 if the entry is a valid, 0 otherwise.
2404 static int batadv_tt_local_valid(const void *entry_ptr,
2405 const void *data_ptr,
2408 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2410 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2414 *flags = tt_common_entry->flags;
2419 static int batadv_tt_global_valid(const void *entry_ptr,
2420 const void *data_ptr,
2423 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2424 const struct batadv_tt_global_entry *tt_global_entry;
2425 const struct batadv_orig_node *orig_node = data_ptr;
2427 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2428 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2431 tt_global_entry = container_of(tt_common_entry,
2432 struct batadv_tt_global_entry,
2435 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2440 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2442 * @bat_priv: the bat priv with all the soft interface information
2443 * @hash: hash table containing the tt entries
2444 * @tt_len: expected tvlv tt data buffer length in number of bytes
2445 * @tvlv_buff: pointer to the buffer to fill with the TT data
2446 * @valid_cb: function to filter tt change entries
2447 * @cb_data: data passed to the filter function as argument
2449 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2450 struct batadv_hashtable *hash,
2451 void *tvlv_buff, u16 tt_len,
2452 int (*valid_cb)(const void *,
2457 struct batadv_tt_common_entry *tt_common_entry;
2458 struct batadv_tvlv_tt_change *tt_change;
2459 struct hlist_head *head;
2460 u16 tt_tot, tt_num_entries = 0;
2465 tt_tot = batadv_tt_entries(tt_len);
2466 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2472 for (i = 0; i < hash->size; i++) {
2473 head = &hash->table[i];
2475 hlist_for_each_entry_rcu(tt_common_entry,
2477 if (tt_tot == tt_num_entries)
2480 ret = valid_cb(tt_common_entry, cb_data, &flags);
2484 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2485 tt_change->flags = flags;
2486 tt_change->vid = htons(tt_common_entry->vid);
2487 memset(tt_change->reserved, 0,
2488 sizeof(tt_change->reserved));
2498 * batadv_tt_global_check_crc - check if all the CRCs are correct
2499 * @orig_node: originator for which the CRCs have to be checked
2500 * @tt_vlan: pointer to the first tvlv VLAN entry
2501 * @num_vlan: number of tvlv VLAN entries
2502 * @create: if true, create VLAN objects if not found
2504 * Return true if all the received CRCs match the locally stored ones, false
2507 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2508 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2511 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2512 struct batadv_orig_node_vlan *vlan;
2516 /* check if each received CRC matches the locally stored one */
2517 for (i = 0; i < num_vlan; i++) {
2518 tt_vlan_tmp = tt_vlan + i;
2520 /* if orig_node is a backbone node for this VLAN, don't check
2521 * the CRC as we ignore all the global entries over it
2523 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2525 ntohs(tt_vlan_tmp->vid)))
2528 vlan = batadv_orig_node_vlan_get(orig_node,
2529 ntohs(tt_vlan_tmp->vid));
2534 batadv_orig_node_vlan_free_ref(vlan);
2536 if (crc != ntohl(tt_vlan_tmp->crc))
2544 * batadv_tt_local_update_crc - update all the local CRCs
2545 * @bat_priv: the bat priv with all the soft interface information
2547 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2549 struct batadv_softif_vlan *vlan;
2551 /* recompute the global CRC for each VLAN */
2553 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2554 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2560 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2561 * @bat_priv: the bat priv with all the soft interface information
2562 * @orig_node: the orig_node for which the CRCs have to be updated
2564 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2565 struct batadv_orig_node *orig_node)
2567 struct batadv_orig_node_vlan *vlan;
2570 /* recompute the global CRC for each VLAN */
2572 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2573 /* if orig_node is a backbone node for this VLAN, don't compute
2574 * the CRC as we ignore all the global entries over it
2576 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2580 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2587 * batadv_send_tt_request - send a TT Request message to a given node
2588 * @bat_priv: the bat priv with all the soft interface information
2589 * @dst_orig_node: the destination of the message
2590 * @ttvn: the version number that the source of the message is looking for
2591 * @tt_vlan: pointer to the first tvlv VLAN object to request
2592 * @num_vlan: number of tvlv VLAN entries
2593 * @full_table: ask for the entire translation table if true, while only for the
2594 * last TT diff otherwise
2596 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2597 struct batadv_orig_node *dst_orig_node,
2599 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2600 u16 num_vlan, bool full_table)
2602 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2603 struct batadv_tt_req_node *tt_req_node = NULL;
2604 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2605 struct batadv_hard_iface *primary_if;
2609 primary_if = batadv_primary_if_get_selected(bat_priv);
2613 /* The new tt_req will be issued only if I'm not waiting for a
2614 * reply from the same orig_node yet
2616 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2620 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2621 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2625 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2626 tvlv_tt_data->ttvn = ttvn;
2627 tvlv_tt_data->num_vlan = htons(num_vlan);
2629 /* send all the CRCs within the request. This is needed by intermediate
2630 * nodes to ensure they have the correct table before replying
2632 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2633 for (i = 0; i < num_vlan; i++) {
2634 tt_vlan_req->vid = tt_vlan->vid;
2635 tt_vlan_req->crc = tt_vlan->crc;
2642 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2644 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2645 dst_orig_node->orig, full_table ? 'F' : '.');
2647 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2648 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2649 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2650 tvlv_tt_data, size);
2655 batadv_hardif_free_ref(primary_if);
2657 if (ret && tt_req_node) {
2658 spin_lock_bh(&bat_priv->tt.req_list_lock);
2659 if (!hlist_unhashed(&tt_req_node->list)) {
2660 hlist_del_init(&tt_req_node->list);
2661 batadv_tt_req_node_put(tt_req_node);
2663 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2667 batadv_tt_req_node_put(tt_req_node);
2669 kfree(tvlv_tt_data);
2674 * batadv_send_other_tt_response - send reply to tt request concerning another
2675 * node's translation table
2676 * @bat_priv: the bat priv with all the soft interface information
2677 * @tt_data: tt data containing the tt request information
2678 * @req_src: mac address of tt request sender
2679 * @req_dst: mac address of tt request recipient
2681 * Returns true if tt request reply was sent, false otherwise.
2683 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2684 struct batadv_tvlv_tt_data *tt_data,
2685 u8 *req_src, u8 *req_dst)
2687 struct batadv_orig_node *req_dst_orig_node;
2688 struct batadv_orig_node *res_dst_orig_node = NULL;
2689 struct batadv_tvlv_tt_change *tt_change;
2690 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2691 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2692 bool ret = false, full_table;
2693 u8 orig_ttvn, req_ttvn;
2697 batadv_dbg(BATADV_DBG_TT, bat_priv,
2698 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2699 req_src, tt_data->ttvn, req_dst,
2700 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2702 /* Let's get the orig node of the REAL destination */
2703 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2704 if (!req_dst_orig_node)
2707 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2708 if (!res_dst_orig_node)
2711 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2712 req_ttvn = tt_data->ttvn;
2714 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2715 /* this node doesn't have the requested data */
2716 if (orig_ttvn != req_ttvn ||
2717 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2718 ntohs(tt_data->num_vlan)))
2721 /* If the full table has been explicitly requested */
2722 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2723 !req_dst_orig_node->tt_buff)
2728 /* TT fragmentation hasn't been implemented yet, so send as many
2729 * TT entries fit a single packet as possible only
2732 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2733 tt_len = req_dst_orig_node->tt_buff_len;
2735 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2742 /* Copy the last orig_node's OGM buffer */
2743 memcpy(tt_change, req_dst_orig_node->tt_buff,
2744 req_dst_orig_node->tt_buff_len);
2745 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2747 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2748 * in the initial part
2751 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2758 /* fill the rest of the tvlv with the real TT entries */
2759 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2761 batadv_tt_global_valid,
2765 /* Don't send the response, if larger than fragmented packet. */
2766 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2767 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2768 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2769 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2770 res_dst_orig_node->orig);
2774 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2775 tvlv_tt_data->ttvn = req_ttvn;
2778 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2780 batadv_dbg(BATADV_DBG_TT, bat_priv,
2781 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2782 res_dst_orig_node->orig, req_dst_orig_node->orig,
2783 full_table ? 'F' : '.', req_ttvn);
2785 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2787 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2788 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2795 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2798 if (res_dst_orig_node)
2799 batadv_orig_node_free_ref(res_dst_orig_node);
2800 if (req_dst_orig_node)
2801 batadv_orig_node_free_ref(req_dst_orig_node);
2802 kfree(tvlv_tt_data);
2807 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2809 * @bat_priv: the bat priv with all the soft interface information
2810 * @tt_data: tt data containing the tt request information
2811 * @req_src: mac address of tt request sender
2813 * Returns true if tt request reply was sent, false otherwise.
2815 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2816 struct batadv_tvlv_tt_data *tt_data,
2819 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2820 struct batadv_hard_iface *primary_if = NULL;
2821 struct batadv_tvlv_tt_change *tt_change;
2822 struct batadv_orig_node *orig_node;
2823 u8 my_ttvn, req_ttvn;
2828 batadv_dbg(BATADV_DBG_TT, bat_priv,
2829 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2830 req_src, tt_data->ttvn,
2831 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2833 spin_lock_bh(&bat_priv->tt.commit_lock);
2835 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2836 req_ttvn = tt_data->ttvn;
2838 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2842 primary_if = batadv_primary_if_get_selected(bat_priv);
2846 /* If the full table has been explicitly requested or the gap
2847 * is too big send the whole local translation table
2849 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2850 !bat_priv->tt.last_changeset)
2855 /* TT fragmentation hasn't been implemented yet, so send as many
2856 * TT entries fit a single packet as possible only
2859 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2861 tt_len = bat_priv->tt.last_changeset_len;
2862 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2866 if (!tt_len || !tvlv_len)
2869 /* Copy the last orig_node's OGM buffer */
2870 memcpy(tt_change, bat_priv->tt.last_changeset,
2871 bat_priv->tt.last_changeset_len);
2872 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2874 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2876 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2877 * in the initial part
2880 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2884 if (!tt_len || !tvlv_len)
2887 /* fill the rest of the tvlv with the real TT entries */
2888 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2890 batadv_tt_local_valid, NULL);
2893 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2894 tvlv_tt_data->ttvn = req_ttvn;
2897 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2899 batadv_dbg(BATADV_DBG_TT, bat_priv,
2900 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2901 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2903 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2905 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2906 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2912 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2914 spin_unlock_bh(&bat_priv->tt.commit_lock);
2916 batadv_orig_node_free_ref(orig_node);
2918 batadv_hardif_free_ref(primary_if);
2919 kfree(tvlv_tt_data);
2920 /* The packet was for this host, so it doesn't need to be re-routed */
2925 * batadv_send_tt_response - send reply to tt request
2926 * @bat_priv: the bat priv with all the soft interface information
2927 * @tt_data: tt data containing the tt request information
2928 * @req_src: mac address of tt request sender
2929 * @req_dst: mac address of tt request recipient
2931 * Returns true if tt request reply was sent, false otherwise.
2933 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2934 struct batadv_tvlv_tt_data *tt_data,
2935 u8 *req_src, u8 *req_dst)
2937 if (batadv_is_my_mac(bat_priv, req_dst))
2938 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2939 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2943 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2944 struct batadv_orig_node *orig_node,
2945 struct batadv_tvlv_tt_change *tt_change,
2946 u16 tt_num_changes, u8 ttvn)
2951 for (i = 0; i < tt_num_changes; i++) {
2952 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2953 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2954 batadv_tt_global_del(bat_priv, orig_node,
2955 (tt_change + i)->addr,
2956 ntohs((tt_change + i)->vid),
2957 "tt removed by changes",
2960 if (!batadv_tt_global_add(bat_priv, orig_node,
2961 (tt_change + i)->addr,
2962 ntohs((tt_change + i)->vid),
2963 (tt_change + i)->flags, ttvn))
2964 /* In case of problem while storing a
2965 * global_entry, we stop the updating
2966 * procedure without committing the
2967 * ttvn change. This will avoid to send
2968 * corrupted data on tt_request
2973 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2976 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2977 struct batadv_tvlv_tt_change *tt_change,
2978 u8 ttvn, u8 *resp_src,
2981 struct batadv_orig_node *orig_node;
2983 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2987 /* Purge the old table first.. */
2988 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2989 "Received full table");
2991 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2994 spin_lock_bh(&orig_node->tt_buff_lock);
2995 kfree(orig_node->tt_buff);
2996 orig_node->tt_buff_len = 0;
2997 orig_node->tt_buff = NULL;
2998 spin_unlock_bh(&orig_node->tt_buff_lock);
3000 atomic_set(&orig_node->last_ttvn, ttvn);
3004 batadv_orig_node_free_ref(orig_node);
3007 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3008 struct batadv_orig_node *orig_node,
3009 u16 tt_num_changes, u8 ttvn,
3010 struct batadv_tvlv_tt_change *tt_change)
3012 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3013 tt_num_changes, ttvn);
3015 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3016 batadv_tt_len(tt_num_changes));
3017 atomic_set(&orig_node->last_ttvn, ttvn);
3021 * batadv_is_my_client - check if a client is served by the local node
3022 * @bat_priv: the bat priv with all the soft interface information
3023 * @addr: the mac address of the client to check
3024 * @vid: VLAN identifier
3026 * Returns true if the client is served by this node, false otherwise.
3028 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3031 struct batadv_tt_local_entry *tt_local_entry;
3034 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3035 if (!tt_local_entry)
3037 /* Check if the client has been logically deleted (but is kept for
3038 * consistency purpose)
3040 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3041 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3046 batadv_tt_local_entry_free_ref(tt_local_entry);
3051 * batadv_handle_tt_response - process incoming tt reply
3052 * @bat_priv: the bat priv with all the soft interface information
3053 * @tt_data: tt data containing the tt request information
3054 * @resp_src: mac address of tt reply sender
3055 * @num_entries: number of tt change entries appended to the tt data
3057 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3058 struct batadv_tvlv_tt_data *tt_data,
3059 u8 *resp_src, u16 num_entries)
3061 struct batadv_tt_req_node *node;
3062 struct hlist_node *safe;
3063 struct batadv_orig_node *orig_node = NULL;
3064 struct batadv_tvlv_tt_change *tt_change;
3065 u8 *tvlv_ptr = (u8 *)tt_data;
3068 batadv_dbg(BATADV_DBG_TT, bat_priv,
3069 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3070 resp_src, tt_data->ttvn, num_entries,
3071 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3073 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3077 spin_lock_bh(&orig_node->tt_lock);
3079 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3080 change_offset *= ntohs(tt_data->num_vlan);
3081 change_offset += sizeof(*tt_data);
3082 tvlv_ptr += change_offset;
3084 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3085 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3086 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3087 resp_src, num_entries);
3089 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3090 tt_data->ttvn, tt_change);
3093 /* Recalculate the CRC for this orig_node and store it */
3094 batadv_tt_global_update_crc(bat_priv, orig_node);
3096 spin_unlock_bh(&orig_node->tt_lock);
3098 /* Delete the tt_req_node from pending tt_requests list */
3099 spin_lock_bh(&bat_priv->tt.req_list_lock);
3100 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3101 if (!batadv_compare_eth(node->addr, resp_src))
3103 hlist_del_init(&node->list);
3104 batadv_tt_req_node_put(node);
3107 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3110 batadv_orig_node_free_ref(orig_node);
3113 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3115 struct batadv_tt_roam_node *node, *safe;
3117 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3119 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3120 list_del(&node->list);
3124 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3127 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3129 struct batadv_tt_roam_node *node, *safe;
3131 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3132 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3133 if (!batadv_has_timed_out(node->first_time,
3134 BATADV_ROAMING_MAX_TIME))
3137 list_del(&node->list);
3140 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3143 /* This function checks whether the client already reached the
3144 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3147 * returns true if the ROAMING_ADV can be sent, false otherwise
3149 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3151 struct batadv_tt_roam_node *tt_roam_node;
3154 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3155 /* The new tt_req will be issued only if I'm not waiting for a
3156 * reply from the same orig_node yet
3158 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3159 if (!batadv_compare_eth(tt_roam_node->addr, client))
3162 if (batadv_has_timed_out(tt_roam_node->first_time,
3163 BATADV_ROAMING_MAX_TIME))
3166 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3167 /* Sorry, you roamed too many times! */
3174 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3178 tt_roam_node->first_time = jiffies;
3179 atomic_set(&tt_roam_node->counter,
3180 BATADV_ROAMING_MAX_COUNT - 1);
3181 ether_addr_copy(tt_roam_node->addr, client);
3183 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3188 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3193 * batadv_send_roam_adv - send a roaming advertisement message
3194 * @bat_priv: the bat priv with all the soft interface information
3195 * @client: mac address of the roaming client
3196 * @vid: VLAN identifier
3197 * @orig_node: message destination
3199 * Send a ROAMING_ADV message to the node which was previously serving this
3200 * client. This is done to inform the node that from now on all traffic destined
3201 * for this particular roamed client has to be forwarded to the sender of the
3204 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3206 struct batadv_orig_node *orig_node)
3208 struct batadv_hard_iface *primary_if;
3209 struct batadv_tvlv_roam_adv tvlv_roam;
3211 primary_if = batadv_primary_if_get_selected(bat_priv);
3215 /* before going on we have to check whether the client has
3216 * already roamed to us too many times
3218 if (!batadv_tt_check_roam_count(bat_priv, client))
3221 batadv_dbg(BATADV_DBG_TT, bat_priv,
3222 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3223 orig_node->orig, client, BATADV_PRINT_VID(vid));
3225 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3227 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3228 tvlv_roam.vid = htons(vid);
3230 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3231 orig_node->orig, BATADV_TVLV_ROAM, 1,
3232 &tvlv_roam, sizeof(tvlv_roam));
3236 batadv_hardif_free_ref(primary_if);
3239 static void batadv_tt_purge(struct work_struct *work)
3241 struct delayed_work *delayed_work;
3242 struct batadv_priv_tt *priv_tt;
3243 struct batadv_priv *bat_priv;
3245 delayed_work = container_of(work, struct delayed_work, work);
3246 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3247 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3249 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3250 batadv_tt_global_purge(bat_priv);
3251 batadv_tt_req_purge(bat_priv);
3252 batadv_tt_roam_purge(bat_priv);
3254 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3255 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3258 void batadv_tt_free(struct batadv_priv *bat_priv)
3260 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3262 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3263 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3265 cancel_delayed_work_sync(&bat_priv->tt.work);
3267 batadv_tt_local_table_free(bat_priv);
3268 batadv_tt_global_table_free(bat_priv);
3269 batadv_tt_req_list_free(bat_priv);
3270 batadv_tt_changes_list_free(bat_priv);
3271 batadv_tt_roam_list_free(bat_priv);
3273 kfree(bat_priv->tt.last_changeset);
3277 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3278 * table and possibly count them in the TT size
3279 * @bat_priv: the bat priv with all the soft interface information
3280 * @flags: the flag to switch
3281 * @enable: whether to set or unset the flag
3282 * @count: whether to increase the TT size by the number of changed entries
3284 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3285 bool enable, bool count)
3287 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3288 struct batadv_tt_common_entry *tt_common_entry;
3289 u16 changed_num = 0;
3290 struct hlist_head *head;
3296 for (i = 0; i < hash->size; i++) {
3297 head = &hash->table[i];
3300 hlist_for_each_entry_rcu(tt_common_entry,
3303 if ((tt_common_entry->flags & flags) == flags)
3305 tt_common_entry->flags |= flags;
3307 if (!(tt_common_entry->flags & flags))
3309 tt_common_entry->flags &= ~flags;
3316 batadv_tt_local_size_inc(bat_priv,
3317 tt_common_entry->vid);
3323 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3324 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3326 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3327 struct batadv_tt_common_entry *tt_common;
3328 struct batadv_tt_local_entry *tt_local;
3329 struct hlist_node *node_tmp;
3330 struct hlist_head *head;
3331 spinlock_t *list_lock; /* protects write access to the hash lists */
3337 for (i = 0; i < hash->size; i++) {
3338 head = &hash->table[i];
3339 list_lock = &hash->list_locks[i];
3341 spin_lock_bh(list_lock);
3342 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3344 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3347 batadv_dbg(BATADV_DBG_TT, bat_priv,
3348 "Deleting local tt entry (%pM, vid: %d): pending\n",
3350 BATADV_PRINT_VID(tt_common->vid));
3352 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3353 hlist_del_rcu(&tt_common->hash_entry);
3354 tt_local = container_of(tt_common,
3355 struct batadv_tt_local_entry,
3358 batadv_tt_local_entry_free_ref(tt_local);
3360 spin_unlock_bh(list_lock);
3365 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3366 * which have been queued in the time since the last commit
3367 * @bat_priv: the bat priv with all the soft interface information
3369 * Caller must hold tt->commit_lock.
3371 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3373 lockdep_assert_held(&bat_priv->tt.commit_lock);
3375 /* Update multicast addresses in local translation table */
3376 batadv_mcast_mla_update(bat_priv);
3378 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3379 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3380 batadv_tt_tvlv_container_update(bat_priv);
3384 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3386 batadv_tt_local_purge_pending_clients(bat_priv);
3387 batadv_tt_local_update_crc(bat_priv);
3389 /* Increment the TTVN only once per OGM interval */
3390 atomic_inc(&bat_priv->tt.vn);
3391 batadv_dbg(BATADV_DBG_TT, bat_priv,
3392 "Local changes committed, updating to ttvn %u\n",
3393 (u8)atomic_read(&bat_priv->tt.vn));
3395 /* reset the sending counter */
3396 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3397 batadv_tt_tvlv_container_update(bat_priv);
3401 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3402 * have been queued in the time since the last commit
3403 * @bat_priv: the bat priv with all the soft interface information
3405 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3407 spin_lock_bh(&bat_priv->tt.commit_lock);
3408 batadv_tt_local_commit_changes_nolock(bat_priv);
3409 spin_unlock_bh(&bat_priv->tt.commit_lock);
3412 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3415 struct batadv_tt_local_entry *tt_local_entry = NULL;
3416 struct batadv_tt_global_entry *tt_global_entry = NULL;
3417 struct batadv_softif_vlan *vlan;
3420 vlan = batadv_softif_vlan_get(bat_priv, vid);
3421 if (!vlan || !atomic_read(&vlan->ap_isolation))
3424 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3425 if (!tt_local_entry)
3428 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3429 if (!tt_global_entry)
3432 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3439 batadv_softif_vlan_free_ref(vlan);
3440 if (tt_global_entry)
3441 batadv_tt_global_entry_free_ref(tt_global_entry);
3443 batadv_tt_local_entry_free_ref(tt_local_entry);
3448 * batadv_tt_update_orig - update global translation table with new tt
3449 * information received via ogms
3450 * @bat_priv: the bat priv with all the soft interface information
3451 * @orig: the orig_node of the ogm
3452 * @tt_vlan: pointer to the first tvlv VLAN entry
3453 * @tt_num_vlan: number of tvlv VLAN entries
3454 * @tt_change: pointer to the first entry in the TT buffer
3455 * @tt_num_changes: number of tt changes inside the tt buffer
3456 * @ttvn: translation table version number of this changeset
3457 * @tt_crc: crc32 checksum of orig node's translation table
3459 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3460 struct batadv_orig_node *orig_node,
3461 const void *tt_buff, u16 tt_num_vlan,
3462 struct batadv_tvlv_tt_change *tt_change,
3463 u16 tt_num_changes, u8 ttvn)
3465 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3466 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3467 bool full_table = true;
3470 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3471 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3472 &orig_node->capa_initialized);
3474 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3475 * increased by one -> we can apply the attached changes
3477 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3478 /* the OGM could not contain the changes due to their size or
3479 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3481 * In this case send a tt request
3483 if (!tt_num_changes) {
3488 spin_lock_bh(&orig_node->tt_lock);
3490 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3493 /* Even if we received the precomputed crc with the OGM, we
3494 * prefer to recompute it to spot any possible inconsistency
3495 * in the global table
3497 batadv_tt_global_update_crc(bat_priv, orig_node);
3499 spin_unlock_bh(&orig_node->tt_lock);
3501 /* The ttvn alone is not enough to guarantee consistency
3502 * because a single value could represent different states
3503 * (due to the wrap around). Thus a node has to check whether
3504 * the resulting table (after applying the changes) is still
3505 * consistent or not. E.g. a node could disconnect while its
3506 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3507 * checking the CRC value is mandatory to detect the
3510 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3514 /* if we missed more than one change or our tables are not
3515 * in sync anymore -> request fresh tt data
3517 if (!has_tt_init || ttvn != orig_ttvn ||
3518 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3521 batadv_dbg(BATADV_DBG_TT, bat_priv,
3522 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3523 orig_node->orig, ttvn, orig_ttvn,
3525 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3526 tt_vlan, tt_num_vlan,
3534 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3535 * @bat_priv: the bat priv with all the soft interface information
3536 * @addr: the mac address of the client to check
3537 * @vid: VLAN identifier
3539 * Returns true if we know that the client has moved from its old originator
3540 * to another one. This entry is still kept for consistency purposes and will be
3541 * deleted later by a DEL or because of timeout
3543 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3544 u8 *addr, unsigned short vid)
3546 struct batadv_tt_global_entry *tt_global_entry;
3549 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3550 if (!tt_global_entry)
3553 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3554 batadv_tt_global_entry_free_ref(tt_global_entry);
3560 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3561 * @bat_priv: the bat priv with all the soft interface information
3562 * @addr: the mac address of the local client to query
3563 * @vid: VLAN identifier
3565 * Returns true if the local client is known to be roaming (it is not served by
3566 * this node anymore) or not. If yes, the client is still present in the table
3567 * to keep the latter consistent with the node TTVN
3569 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3570 u8 *addr, unsigned short vid)
3572 struct batadv_tt_local_entry *tt_local_entry;
3575 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3576 if (!tt_local_entry)
3579 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3580 batadv_tt_local_entry_free_ref(tt_local_entry);
3585 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3586 struct batadv_orig_node *orig_node,
3587 const unsigned char *addr,
3592 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3593 BATADV_TT_CLIENT_TEMP,
3594 atomic_read(&orig_node->last_ttvn)))
3597 batadv_dbg(BATADV_DBG_TT, bat_priv,
3598 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3599 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3606 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3607 * maximum packet size that can be transported through the mesh
3608 * @soft_iface: netdev struct of the mesh interface
3610 * Remove entries older than 'timeout' and half timeout if more entries need
3613 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3615 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3616 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3617 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3618 bool reduced = false;
3620 spin_lock_bh(&bat_priv->tt.commit_lock);
3623 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3624 if (packet_size_max >= table_size)
3627 batadv_tt_local_purge(bat_priv, timeout);
3628 batadv_tt_local_purge_pending_clients(bat_priv);
3632 net_ratelimited_function(batadv_info, soft_iface,
3633 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3637 /* commit these changes immediately, to avoid synchronization problem
3641 batadv_tt_local_commit_changes_nolock(bat_priv);
3643 spin_unlock_bh(&bat_priv->tt.commit_lock);
3647 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3648 * @bat_priv: the bat priv with all the soft interface information
3649 * @orig: the orig_node of the ogm
3650 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3651 * @tvlv_value: tvlv buffer containing the gateway data
3652 * @tvlv_value_len: tvlv buffer length
3654 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3655 struct batadv_orig_node *orig,
3656 u8 flags, void *tvlv_value,
3659 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3660 struct batadv_tvlv_tt_change *tt_change;
3661 struct batadv_tvlv_tt_data *tt_data;
3662 u16 num_entries, num_vlan;
3664 if (tvlv_value_len < sizeof(*tt_data))
3667 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3668 tvlv_value_len -= sizeof(*tt_data);
3670 num_vlan = ntohs(tt_data->num_vlan);
3672 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3675 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3676 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3677 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3679 num_entries = batadv_tt_entries(tvlv_value_len);
3681 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3682 num_entries, tt_data->ttvn);
3686 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3688 * @bat_priv: the bat priv with all the soft interface information
3689 * @src: mac address of tt tvlv sender
3690 * @dst: mac address of tt tvlv recipient
3691 * @tvlv_value: tvlv buffer containing the tt data
3692 * @tvlv_value_len: tvlv buffer length
3694 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3697 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3702 struct batadv_tvlv_tt_data *tt_data;
3703 u16 tt_vlan_len, tt_num_entries;
3707 if (tvlv_value_len < sizeof(*tt_data))
3708 return NET_RX_SUCCESS;
3710 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3711 tvlv_value_len -= sizeof(*tt_data);
3713 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3714 tt_vlan_len *= ntohs(tt_data->num_vlan);
3716 if (tvlv_value_len < tt_vlan_len)
3717 return NET_RX_SUCCESS;
3719 tvlv_value_len -= tt_vlan_len;
3720 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3722 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3723 case BATADV_TT_REQUEST:
3724 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3726 /* If this node cannot provide a TT response the tt_request is
3729 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3731 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3736 batadv_dbg(BATADV_DBG_TT, bat_priv,
3737 "Routing TT_REQUEST to %pM [%c]\n",
3739 /* tvlv API will re-route the packet */
3743 case BATADV_TT_RESPONSE:
3744 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3746 if (batadv_is_my_mac(bat_priv, dst)) {
3747 batadv_handle_tt_response(bat_priv, tt_data,
3748 src, tt_num_entries);
3749 return NET_RX_SUCCESS;
3752 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3757 batadv_dbg(BATADV_DBG_TT, bat_priv,
3758 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3760 /* tvlv API will re-route the packet */
3764 return NET_RX_SUCCESS;
3768 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3769 * @bat_priv: the bat priv with all the soft interface information
3770 * @src: mac address of tt tvlv sender
3771 * @dst: mac address of tt tvlv recipient
3772 * @tvlv_value: tvlv buffer containing the tt data
3773 * @tvlv_value_len: tvlv buffer length
3775 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3778 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3783 struct batadv_tvlv_roam_adv *roaming_adv;
3784 struct batadv_orig_node *orig_node = NULL;
3786 /* If this node is not the intended recipient of the
3787 * roaming advertisement the packet is forwarded
3788 * (the tvlv API will re-route the packet).
3790 if (!batadv_is_my_mac(bat_priv, dst))
3793 if (tvlv_value_len < sizeof(*roaming_adv))
3796 orig_node = batadv_orig_hash_find(bat_priv, src);
3800 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3801 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3803 batadv_dbg(BATADV_DBG_TT, bat_priv,
3804 "Received ROAMING_ADV from %pM (client %pM)\n",
3805 src, roaming_adv->client);
3807 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3808 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3809 atomic_read(&orig_node->last_ttvn) + 1);
3813 batadv_orig_node_free_ref(orig_node);
3814 return NET_RX_SUCCESS;
3818 * batadv_tt_init - initialise the translation table internals
3819 * @bat_priv: the bat priv with all the soft interface information
3821 * Return 0 on success or negative error number in case of failure.
3823 int batadv_tt_init(struct batadv_priv *bat_priv)
3827 /* synchronized flags must be remote */
3828 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3830 ret = batadv_tt_local_init(bat_priv);
3834 ret = batadv_tt_global_init(bat_priv);
3838 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3839 batadv_tt_tvlv_unicast_handler_v1,
3840 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3842 batadv_tvlv_handler_register(bat_priv, NULL,
3843 batadv_roam_tvlv_unicast_handler_v1,
3844 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3846 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3847 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3848 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3854 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3855 * @bat_priv: the bat priv with all the soft interface information
3856 * @addr: the mac address of the client
3857 * @vid: the identifier of the VLAN where this client is connected
3859 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3862 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3863 const u8 *addr, unsigned short vid)
3865 struct batadv_tt_global_entry *tt;
3868 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3872 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3874 batadv_tt_global_entry_free_ref(tt);