1 /* Copyright (C) 2007-2016 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,
72 * batadv_compare_tt - check if two TT entries are the same
73 * @node: the list element pointer of the first TT entry
74 * @data2: pointer to the tt_common_entry of the second TT entry
76 * Compare the MAC address and the VLAN ID of the two TT entries and check if
77 * they are the same TT client.
78 * Return: 1 if the two TT clients are the same, 0 otherwise
80 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
82 const void *data1 = container_of(node, struct batadv_tt_common_entry,
84 const struct batadv_tt_common_entry *tt1 = data1;
85 const struct batadv_tt_common_entry *tt2 = data2;
87 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
91 * batadv_choose_tt - return the index of the tt entry in the hash table
92 * @data: pointer to the tt_common_entry object to map
93 * @size: the size of the hash table
95 * Return: the hash index where the object represented by 'data' should be
98 static inline u32 batadv_choose_tt(const void *data, u32 size)
100 struct batadv_tt_common_entry *tt;
103 tt = (struct batadv_tt_common_entry *)data;
104 hash = jhash(&tt->addr, ETH_ALEN, hash);
105 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
111 * batadv_tt_hash_find - look for a client in the given hash table
112 * @hash: the hash table to search
113 * @addr: the mac address of the client to look for
114 * @vid: VLAN identifier
116 * Return: a pointer to the tt_common struct belonging to the searched client if
117 * found, NULL otherwise.
119 static struct batadv_tt_common_entry *
120 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
123 struct hlist_head *head;
124 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
130 ether_addr_copy(to_search.addr, addr);
133 index = batadv_choose_tt(&to_search, hash->size);
134 head = &hash->table[index];
137 hlist_for_each_entry_rcu(tt, head, hash_entry) {
138 if (!batadv_compare_eth(tt, addr))
144 if (!atomic_inc_not_zero(&tt->refcount))
156 * batadv_tt_local_hash_find - search the local table for a given client
157 * @bat_priv: the bat priv with all the soft interface information
158 * @addr: the mac address of the client to look for
159 * @vid: VLAN identifier
161 * Return: a pointer to the corresponding tt_local_entry struct if the client is
162 * found, NULL otherwise.
164 static struct batadv_tt_local_entry *
165 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
168 struct batadv_tt_common_entry *tt_common_entry;
169 struct batadv_tt_local_entry *tt_local_entry = NULL;
171 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
174 tt_local_entry = container_of(tt_common_entry,
175 struct batadv_tt_local_entry,
177 return tt_local_entry;
181 * batadv_tt_global_hash_find - search the global table for a given client
182 * @bat_priv: the bat priv with all the soft interface information
183 * @addr: the mac address of the client to look for
184 * @vid: VLAN identifier
186 * Return: a pointer to the corresponding tt_global_entry struct if the client
187 * is found, NULL otherwise.
189 static struct batadv_tt_global_entry *
190 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
193 struct batadv_tt_common_entry *tt_common_entry;
194 struct batadv_tt_global_entry *tt_global_entry = NULL;
196 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
199 tt_global_entry = container_of(tt_common_entry,
200 struct batadv_tt_global_entry,
202 return tt_global_entry;
206 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
208 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
209 kfree_rcu(tt_local_entry, common.rcu);
213 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
214 * tt_global_entry and possibly free it
215 * @tt_global_entry: the object to free
218 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
220 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
221 batadv_tt_global_del_orig_list(tt_global_entry);
222 kfree_rcu(tt_global_entry, common.rcu);
227 * batadv_tt_global_hash_count - count the number of orig entries
228 * @bat_priv: the bat priv with all the soft interface information
229 * @addr: the mac address of the client to count entries for
230 * @vid: VLAN identifier
232 * Return: the number of originators advertising the given address/data
233 * (excluding ourself).
235 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
236 const u8 *addr, unsigned short vid)
238 struct batadv_tt_global_entry *tt_global_entry;
241 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
242 if (!tt_global_entry)
245 count = atomic_read(&tt_global_entry->orig_list_count);
246 batadv_tt_global_entry_free_ref(tt_global_entry);
252 * batadv_tt_local_size_mod - change the size by v of the local table identified
254 * @bat_priv: the bat priv with all the soft interface information
255 * @vid: the VLAN identifier of the sub-table to change
256 * @v: the amount to sum to the local table size
258 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
259 unsigned short vid, int v)
261 struct batadv_softif_vlan *vlan;
263 vlan = batadv_softif_vlan_get(bat_priv, vid);
267 atomic_add(v, &vlan->tt.num_entries);
269 batadv_softif_vlan_free_ref(vlan);
273 * batadv_tt_local_size_inc - increase by one the local table size for the given
275 * @bat_priv: the bat priv with all the soft interface information
276 * @vid: the VLAN identifier
278 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
281 batadv_tt_local_size_mod(bat_priv, vid, 1);
285 * batadv_tt_local_size_dec - decrease by one the local table size for the given
287 * @bat_priv: the bat priv with all the soft interface information
288 * @vid: the VLAN identifier
290 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
293 batadv_tt_local_size_mod(bat_priv, vid, -1);
297 * batadv_tt_global_size_mod - change the size by v of the global table
298 * for orig_node identified by vid
299 * @orig_node: the originator for which the table has to be modified
300 * @vid: the VLAN identifier
301 * @v: the amount to sum to the global table size
303 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
304 unsigned short vid, int v)
306 struct batadv_orig_node_vlan *vlan;
308 vlan = batadv_orig_node_vlan_new(orig_node, vid);
312 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
313 spin_lock_bh(&orig_node->vlan_list_lock);
314 hlist_del_init_rcu(&vlan->list);
315 spin_unlock_bh(&orig_node->vlan_list_lock);
316 batadv_orig_node_vlan_free_ref(vlan);
319 batadv_orig_node_vlan_free_ref(vlan);
323 * batadv_tt_global_size_inc - increase by one the global table size for the
325 * @orig_node: the originator which global table size has to be decreased
326 * @vid: the vlan identifier
328 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
331 batadv_tt_global_size_mod(orig_node, vid, 1);
335 * batadv_tt_global_size_dec - decrease by one the global table size for the
337 * @orig_node: the originator which global table size has to be decreased
338 * @vid: the vlan identifier
340 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
343 batadv_tt_global_size_mod(orig_node, vid, -1);
347 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
348 * queue for free after rcu grace period
349 * @orig_entry: tt orig entry to be free'd
352 batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
354 batadv_orig_node_free_ref(orig_entry->orig_node);
355 kfree_rcu(orig_entry, rcu);
359 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
361 if (!atomic_dec_and_test(&orig_entry->refcount))
364 batadv_tt_orig_list_entry_release(orig_entry);
368 * batadv_tt_local_event - store a local TT event (ADD/DEL)
369 * @bat_priv: the bat priv with all the soft interface information
370 * @tt_local_entry: the TT entry involved in the event
371 * @event_flags: flags to store in the event structure
373 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
374 struct batadv_tt_local_entry *tt_local_entry,
377 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
378 struct batadv_tt_common_entry *common = &tt_local_entry->common;
379 u8 flags = common->flags | event_flags;
380 bool event_removed = false;
381 bool del_op_requested, del_op_entry;
383 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
387 tt_change_node->change.flags = flags;
388 memset(tt_change_node->change.reserved, 0,
389 sizeof(tt_change_node->change.reserved));
390 ether_addr_copy(tt_change_node->change.addr, common->addr);
391 tt_change_node->change.vid = htons(common->vid);
393 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
395 /* check for ADD+DEL or DEL+ADD events */
396 spin_lock_bh(&bat_priv->tt.changes_list_lock);
397 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
399 if (!batadv_compare_eth(entry->change.addr, common->addr))
402 /* DEL+ADD in the same orig interval have no effect and can be
403 * removed to avoid silly behaviour on the receiver side. The
404 * other way around (ADD+DEL) can happen in case of roaming of
405 * a client still in the NEW state. Roaming of NEW clients is
406 * now possible due to automatically recognition of "temporary"
409 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
410 if (!del_op_requested && del_op_entry)
412 if (del_op_requested && !del_op_entry)
415 /* this is a second add in the same originator interval. It
416 * means that flags have been changed: update them!
418 if (!del_op_requested && !del_op_entry)
419 entry->change.flags = flags;
423 list_del(&entry->list);
425 kfree(tt_change_node);
426 event_removed = true;
430 /* track the change in the OGMinterval list */
431 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
434 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
437 atomic_dec(&bat_priv->tt.local_changes);
439 atomic_inc(&bat_priv->tt.local_changes);
443 * batadv_tt_len - compute length in bytes of given number of tt changes
444 * @changes_num: number of tt changes
446 * Return: computed length in bytes.
448 static int batadv_tt_len(int changes_num)
450 return changes_num * sizeof(struct batadv_tvlv_tt_change);
454 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
455 * @tt_len: available space
457 * Return: the number of entries.
459 static u16 batadv_tt_entries(u16 tt_len)
461 return tt_len / batadv_tt_len(1);
465 * batadv_tt_local_table_transmit_size - calculates the local translation table
466 * size when transmitted over the air
467 * @bat_priv: the bat priv with all the soft interface information
469 * Return: local translation table size in bytes.
471 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
474 u16 tt_local_entries = 0;
475 struct batadv_softif_vlan *vlan;
479 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
481 tt_local_entries += atomic_read(&vlan->tt.num_entries);
485 /* header size of tvlv encapsulated tt response payload */
486 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
487 hdr_size += sizeof(struct batadv_tvlv_hdr);
488 hdr_size += sizeof(struct batadv_tvlv_tt_data);
489 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
491 return hdr_size + batadv_tt_len(tt_local_entries);
494 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
496 if (bat_priv->tt.local_hash)
499 bat_priv->tt.local_hash = batadv_hash_new(1024);
501 if (!bat_priv->tt.local_hash)
504 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
505 &batadv_tt_local_hash_lock_class_key);
510 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
511 struct batadv_tt_global_entry *tt_global,
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 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
520 batadv_choose_tt, &tt_global->common);
521 batadv_tt_global_entry_free_ref(tt_global);
525 * batadv_tt_local_add - add a new client to the local table or update an
527 * @soft_iface: netdev struct of the mesh interface
528 * @addr: the mac address of the client to add
529 * @vid: VLAN identifier
530 * @ifindex: index of the interface where the client is connected to (useful to
531 * identify wireless clients)
532 * @mark: the value contained in the skb->mark field of the received packet (if
535 * Return: true if the client was successfully added, false otherwise.
537 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
538 unsigned short vid, int ifindex, u32 mark)
540 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
541 struct batadv_tt_local_entry *tt_local;
542 struct batadv_tt_global_entry *tt_global = NULL;
543 struct batadv_softif_vlan *vlan;
544 struct net_device *in_dev = NULL;
545 struct hlist_head *head;
546 struct batadv_tt_orig_list_entry *orig_entry;
547 int hash_added, table_size, packet_size_max;
549 bool roamed_back = false;
553 if (ifindex != BATADV_NULL_IFINDEX)
554 in_dev = dev_get_by_index(&init_net, ifindex);
556 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
558 if (!is_multicast_ether_addr(addr))
559 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
562 tt_local->last_seen = jiffies;
563 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
564 batadv_dbg(BATADV_DBG_TT, bat_priv,
565 "Re-adding pending client %pM (vid: %d)\n",
566 addr, BATADV_PRINT_VID(vid));
567 /* whatever the reason why the PENDING flag was set,
568 * this is a client which was enqueued to be removed in
569 * this orig_interval. Since it popped up again, the
570 * flag can be reset like it was never enqueued
572 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
576 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
577 batadv_dbg(BATADV_DBG_TT, bat_priv,
578 "Roaming client %pM (vid: %d) came back to its original location\n",
579 addr, BATADV_PRINT_VID(vid));
580 /* the ROAM flag is set because this client roamed away
581 * and the node got a roaming_advertisement message. Now
582 * that the client popped up again at its original
583 * location such flag can be unset
585 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
591 /* Ignore the client if we cannot send it in a full table response. */
592 table_size = batadv_tt_local_table_transmit_size(bat_priv);
593 table_size += batadv_tt_len(1);
594 packet_size_max = atomic_read(&bat_priv->packet_size_max);
595 if (table_size > packet_size_max) {
596 net_ratelimited_function(batadv_info, soft_iface,
597 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
598 table_size, packet_size_max, addr);
602 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
606 /* increase the refcounter of the related vlan */
607 vlan = batadv_softif_vlan_get(bat_priv, vid);
608 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
609 addr, BATADV_PRINT_VID(vid))) {
615 batadv_dbg(BATADV_DBG_TT, bat_priv,
616 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
617 addr, BATADV_PRINT_VID(vid),
618 (u8)atomic_read(&bat_priv->tt.vn));
620 ether_addr_copy(tt_local->common.addr, addr);
621 /* The local entry has to be marked as NEW to avoid to send it in
622 * a full table response going out before the next ttvn increment
623 * (consistency check)
625 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
626 tt_local->common.vid = vid;
627 if (batadv_is_wifi_netdev(in_dev))
628 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
629 atomic_set(&tt_local->common.refcount, 2);
630 tt_local->last_seen = jiffies;
631 tt_local->common.added_at = tt_local->last_seen;
633 /* the batman interface mac and multicast addresses should never be
636 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
637 is_multicast_ether_addr(addr))
638 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
640 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
641 batadv_choose_tt, &tt_local->common,
642 &tt_local->common.hash_entry);
644 if (unlikely(hash_added != 0)) {
645 /* remove the reference for the hash */
646 batadv_tt_local_entry_free_ref(tt_local);
647 batadv_softif_vlan_free_ref(vlan);
652 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
655 /* Check whether it is a roaming, but don't do anything if the roaming
656 * process has already been handled
658 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
659 /* These node are probably going to update their tt table */
660 head = &tt_global->orig_list;
662 hlist_for_each_entry_rcu(orig_entry, head, list) {
663 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
664 tt_global->common.vid,
665 orig_entry->orig_node);
669 batadv_tt_global_free(bat_priv, tt_global,
673 /* The global entry has to be marked as ROAMING and
674 * has to be kept for consistency purpose
676 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
677 tt_global->roam_at = jiffies;
681 /* store the current remote flags before altering them. This helps
682 * understanding is flags are changing or not
684 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
686 if (batadv_is_wifi_netdev(in_dev))
687 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
689 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
691 /* check the mark in the skb: if it's equal to the configured
692 * isolation_mark, it means the packet is coming from an isolated
695 match_mark = (mark & bat_priv->isolation_mark_mask);
696 if (bat_priv->isolation_mark_mask &&
697 match_mark == bat_priv->isolation_mark)
698 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
700 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
702 /* if any "dynamic" flag has been modified, resend an ADD event for this
703 * entry so that all the nodes can get the new flags
705 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
706 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
713 batadv_tt_local_entry_free_ref(tt_local);
715 batadv_tt_global_entry_free_ref(tt_global);
720 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
721 * within a TT Response directed to another node
722 * @orig_node: originator for which the TT data has to be prepared
723 * @tt_data: uninitialised pointer to the address of the TVLV buffer
724 * @tt_change: uninitialised pointer to the address of the area where the TT
725 * changed can be stored
726 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
727 * function reserves the amount of space needed to send the entire global TT
728 * table. In case of success the value is updated with the real amount of
730 * Allocate the needed amount of memory for the entire TT TVLV and write its
731 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
732 * objects, one per active VLAN served by the originator node.
734 * Return: the size of the allocated buffer or 0 in case of failure.
737 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
738 struct batadv_tvlv_tt_data **tt_data,
739 struct batadv_tvlv_tt_change **tt_change,
746 struct batadv_tvlv_tt_vlan_data *tt_vlan;
747 struct batadv_orig_node_vlan *vlan;
751 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
753 num_entries += atomic_read(&vlan->tt.num_entries);
756 change_offset = sizeof(**tt_data);
757 change_offset += num_vlan * sizeof(*tt_vlan);
759 /* if tt_len is negative, allocate the space needed by the full table */
761 *tt_len = batadv_tt_len(num_entries);
764 tvlv_len += change_offset;
766 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
772 (*tt_data)->flags = BATADV_NO_FLAGS;
773 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
774 (*tt_data)->num_vlan = htons(num_vlan);
776 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
777 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
778 tt_vlan->vid = htons(vlan->vid);
779 tt_vlan->crc = htonl(vlan->tt.crc);
784 tt_change_ptr = (u8 *)*tt_data + change_offset;
785 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
793 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
795 * @bat_priv: the bat priv with all the soft interface information
796 * @tt_data: uninitialised pointer to the address of the TVLV buffer
797 * @tt_change: uninitialised pointer to the address of the area where the TT
798 * changes can be stored
799 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
800 * function reserves the amount of space needed to send the entire local TT
801 * table. In case of success the value is updated with the real amount of
804 * Allocate the needed amount of memory for the entire TT TVLV and write its
805 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
806 * objects, one per active VLAN.
808 * Return: the size of the allocated buffer or 0 in case of failure.
811 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
812 struct batadv_tvlv_tt_data **tt_data,
813 struct batadv_tvlv_tt_change **tt_change,
816 struct batadv_tvlv_tt_vlan_data *tt_vlan;
817 struct batadv_softif_vlan *vlan;
825 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
827 num_entries += atomic_read(&vlan->tt.num_entries);
830 change_offset = sizeof(**tt_data);
831 change_offset += num_vlan * sizeof(*tt_vlan);
833 /* if tt_len is negative, allocate the space needed by the full table */
835 *tt_len = batadv_tt_len(num_entries);
838 tvlv_len += change_offset;
840 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
846 (*tt_data)->flags = BATADV_NO_FLAGS;
847 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
848 (*tt_data)->num_vlan = htons(num_vlan);
850 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
851 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
852 tt_vlan->vid = htons(vlan->vid);
853 tt_vlan->crc = htonl(vlan->tt.crc);
858 tt_change_ptr = (u8 *)*tt_data + change_offset;
859 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
867 * batadv_tt_tvlv_container_update - update the translation table tvlv container
868 * after local tt changes have been committed
869 * @bat_priv: the bat priv with all the soft interface information
871 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
873 struct batadv_tt_change_node *entry, *safe;
874 struct batadv_tvlv_tt_data *tt_data;
875 struct batadv_tvlv_tt_change *tt_change;
876 int tt_diff_len, tt_change_len = 0;
877 int tt_diff_entries_num = 0;
878 int tt_diff_entries_count = 0;
881 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
882 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
884 /* if we have too many changes for one packet don't send any
885 * and wait for the tt table request which will be fragmented
887 if (tt_diff_len > bat_priv->soft_iface->mtu)
890 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
891 &tt_change, &tt_diff_len);
895 tt_data->flags = BATADV_TT_OGM_DIFF;
897 if (tt_diff_len == 0)
898 goto container_register;
900 spin_lock_bh(&bat_priv->tt.changes_list_lock);
901 atomic_set(&bat_priv->tt.local_changes, 0);
903 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
905 if (tt_diff_entries_count < tt_diff_entries_num) {
906 memcpy(tt_change + tt_diff_entries_count,
908 sizeof(struct batadv_tvlv_tt_change));
909 tt_diff_entries_count++;
911 list_del(&entry->list);
914 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
916 /* Keep the buffer for possible tt_request */
917 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
918 kfree(bat_priv->tt.last_changeset);
919 bat_priv->tt.last_changeset_len = 0;
920 bat_priv->tt.last_changeset = NULL;
921 tt_change_len = batadv_tt_len(tt_diff_entries_count);
922 /* check whether this new OGM has no changes due to size problems */
923 if (tt_diff_entries_count > 0) {
924 /* if kmalloc() fails we will reply with the full table
925 * instead of providing the diff
927 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
928 if (bat_priv->tt.last_changeset) {
929 memcpy(bat_priv->tt.last_changeset,
930 tt_change, tt_change_len);
931 bat_priv->tt.last_changeset_len = tt_diff_len;
934 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
937 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
942 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
944 struct net_device *net_dev = (struct net_device *)seq->private;
945 struct batadv_priv *bat_priv = netdev_priv(net_dev);
946 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
947 struct batadv_tt_common_entry *tt_common_entry;
948 struct batadv_tt_local_entry *tt_local;
949 struct batadv_hard_iface *primary_if;
950 struct batadv_softif_vlan *vlan;
951 struct hlist_head *head;
956 unsigned long last_seen_jiffies;
958 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
960 primary_if = batadv_seq_print_text_primary_if_get(seq);
965 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
966 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
967 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
968 "Flags", "Last seen", "CRC");
970 for (i = 0; i < hash->size; i++) {
971 head = &hash->table[i];
974 hlist_for_each_entry_rcu(tt_common_entry,
976 tt_local = container_of(tt_common_entry,
977 struct batadv_tt_local_entry,
979 vid = tt_common_entry->vid;
980 last_seen_jiffies = jiffies - tt_local->last_seen;
981 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
982 last_seen_secs = last_seen_msecs / 1000;
983 last_seen_msecs = last_seen_msecs % 1000;
985 no_purge = tt_common_entry->flags & np_flag;
987 vlan = batadv_softif_vlan_get(bat_priv, vid);
989 seq_printf(seq, "Cannot retrieve VLAN %d\n",
990 BATADV_PRINT_VID(vid));
995 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
996 tt_common_entry->addr,
997 BATADV_PRINT_VID(tt_common_entry->vid),
998 ((tt_common_entry->flags &
999 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1000 no_purge ? 'P' : '.',
1001 ((tt_common_entry->flags &
1002 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1003 ((tt_common_entry->flags &
1004 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1005 ((tt_common_entry->flags &
1006 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1007 ((tt_common_entry->flags &
1008 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1009 no_purge ? 0 : last_seen_secs,
1010 no_purge ? 0 : last_seen_msecs,
1013 batadv_softif_vlan_free_ref(vlan);
1019 batadv_hardif_free_ref(primary_if);
1024 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1025 struct batadv_tt_local_entry *tt_local_entry,
1026 u16 flags, const char *message)
1028 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1030 /* The local client has to be marked as "pending to be removed" but has
1031 * to be kept in the table in order to send it in a full table
1032 * response issued before the net ttvn increment (consistency check)
1034 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1036 batadv_dbg(BATADV_DBG_TT, bat_priv,
1037 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1038 tt_local_entry->common.addr,
1039 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1043 * batadv_tt_local_remove - logically remove an entry from the local table
1044 * @bat_priv: the bat priv with all the soft interface information
1045 * @addr: the MAC address of the client to remove
1046 * @vid: VLAN identifier
1047 * @message: message to append to the log on deletion
1048 * @roaming: true if the deletion is due to a roaming event
1050 * Return: the flags assigned to the local entry before being deleted
1052 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1053 unsigned short vid, const char *message,
1056 struct batadv_tt_local_entry *tt_local_entry;
1057 u16 flags, curr_flags = BATADV_NO_FLAGS;
1058 struct batadv_softif_vlan *vlan;
1059 void *tt_entry_exists;
1061 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1062 if (!tt_local_entry)
1065 curr_flags = tt_local_entry->common.flags;
1067 flags = BATADV_TT_CLIENT_DEL;
1068 /* if this global entry addition is due to a roaming, the node has to
1069 * mark the local entry as "roamed" in order to correctly reroute
1073 flags |= BATADV_TT_CLIENT_ROAM;
1074 /* mark the local client as ROAMed */
1075 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1078 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1079 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1083 /* if this client has been added right now, it is possible to
1084 * immediately purge it
1086 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1088 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1091 &tt_local_entry->common);
1092 if (!tt_entry_exists)
1095 /* extra call to free the local tt entry */
1096 batadv_tt_local_entry_free_ref(tt_local_entry);
1098 /* decrease the reference held for this vlan */
1099 vlan = batadv_softif_vlan_get(bat_priv, vid);
1103 batadv_softif_vlan_free_ref(vlan);
1104 batadv_softif_vlan_free_ref(vlan);
1108 batadv_tt_local_entry_free_ref(tt_local_entry);
1114 * batadv_tt_local_purge_list - purge inactive tt local entries
1115 * @bat_priv: the bat priv with all the soft interface information
1116 * @head: pointer to the list containing the local tt entries
1117 * @timeout: parameter deciding whether a given tt local entry is considered
1120 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1121 struct hlist_head *head,
1124 struct batadv_tt_local_entry *tt_local_entry;
1125 struct batadv_tt_common_entry *tt_common_entry;
1126 struct hlist_node *node_tmp;
1128 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1130 tt_local_entry = container_of(tt_common_entry,
1131 struct batadv_tt_local_entry,
1133 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1136 /* entry already marked for deletion */
1137 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1140 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1143 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1144 BATADV_TT_CLIENT_DEL, "timed out");
1149 * batadv_tt_local_purge - purge inactive tt local entries
1150 * @bat_priv: the bat priv with all the soft interface information
1151 * @timeout: parameter deciding whether a given tt local entry is considered
1154 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1157 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1158 struct hlist_head *head;
1159 spinlock_t *list_lock; /* protects write access to the hash lists */
1162 for (i = 0; i < hash->size; i++) {
1163 head = &hash->table[i];
1164 list_lock = &hash->list_locks[i];
1166 spin_lock_bh(list_lock);
1167 batadv_tt_local_purge_list(bat_priv, head, timeout);
1168 spin_unlock_bh(list_lock);
1172 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1174 struct batadv_hashtable *hash;
1175 spinlock_t *list_lock; /* protects write access to the hash lists */
1176 struct batadv_tt_common_entry *tt_common_entry;
1177 struct batadv_tt_local_entry *tt_local;
1178 struct batadv_softif_vlan *vlan;
1179 struct hlist_node *node_tmp;
1180 struct hlist_head *head;
1183 if (!bat_priv->tt.local_hash)
1186 hash = bat_priv->tt.local_hash;
1188 for (i = 0; i < hash->size; i++) {
1189 head = &hash->table[i];
1190 list_lock = &hash->list_locks[i];
1192 spin_lock_bh(list_lock);
1193 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1195 hlist_del_rcu(&tt_common_entry->hash_entry);
1196 tt_local = container_of(tt_common_entry,
1197 struct batadv_tt_local_entry,
1200 /* decrease the reference held for this vlan */
1201 vlan = batadv_softif_vlan_get(bat_priv,
1202 tt_common_entry->vid);
1204 batadv_softif_vlan_free_ref(vlan);
1205 batadv_softif_vlan_free_ref(vlan);
1208 batadv_tt_local_entry_free_ref(tt_local);
1210 spin_unlock_bh(list_lock);
1213 batadv_hash_destroy(hash);
1215 bat_priv->tt.local_hash = NULL;
1218 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1220 if (bat_priv->tt.global_hash)
1223 bat_priv->tt.global_hash = batadv_hash_new(1024);
1225 if (!bat_priv->tt.global_hash)
1228 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1229 &batadv_tt_global_hash_lock_class_key);
1234 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1236 struct batadv_tt_change_node *entry, *safe;
1238 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1240 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1242 list_del(&entry->list);
1246 atomic_set(&bat_priv->tt.local_changes, 0);
1247 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1251 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1252 * @entry: the TT global entry where the orig_list_entry has to be
1254 * @orig_node: the originator for which the orig_list_entry has to be found
1256 * retrieve the orig_tt_list_entry belonging to orig_node from the
1257 * batadv_tt_global_entry list
1259 * Return: it with an increased refcounter, NULL if not found
1261 static struct batadv_tt_orig_list_entry *
1262 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1263 const struct batadv_orig_node *orig_node)
1265 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1266 const struct hlist_head *head;
1269 head = &entry->orig_list;
1270 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1271 if (tmp_orig_entry->orig_node != orig_node)
1273 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1276 orig_entry = tmp_orig_entry;
1285 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1286 * by a given originator
1287 * @entry: the TT global entry to check
1288 * @orig_node: the originator to search in the list
1290 * find out if an orig_node is already in the list of a tt_global_entry.
1292 * Return: true if found, false otherwise
1295 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1296 const struct batadv_orig_node *orig_node)
1298 struct batadv_tt_orig_list_entry *orig_entry;
1301 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1304 batadv_tt_orig_list_entry_free_ref(orig_entry);
1311 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1312 struct batadv_orig_node *orig_node, int ttvn)
1314 struct batadv_tt_orig_list_entry *orig_entry;
1316 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1318 /* refresh the ttvn: the current value could be a bogus one that
1319 * was added during a "temporary client detection"
1321 orig_entry->ttvn = ttvn;
1325 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1329 INIT_HLIST_NODE(&orig_entry->list);
1330 atomic_inc(&orig_node->refcount);
1331 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1332 orig_entry->orig_node = orig_node;
1333 orig_entry->ttvn = ttvn;
1334 atomic_set(&orig_entry->refcount, 2);
1336 spin_lock_bh(&tt_global->list_lock);
1337 hlist_add_head_rcu(&orig_entry->list,
1338 &tt_global->orig_list);
1339 spin_unlock_bh(&tt_global->list_lock);
1340 atomic_inc(&tt_global->orig_list_count);
1344 batadv_tt_orig_list_entry_free_ref(orig_entry);
1348 * batadv_tt_global_add - add a new TT global entry or update an existing one
1349 * @bat_priv: the bat priv with all the soft interface information
1350 * @orig_node: the originator announcing the client
1351 * @tt_addr: the mac address of the non-mesh client
1352 * @vid: VLAN identifier
1353 * @flags: TT flags that have to be set for this non-mesh client
1354 * @ttvn: the tt version number ever announcing this non-mesh client
1356 * Add a new TT global entry for the given originator. If the entry already
1357 * exists add a new reference to the given originator (a global entry can have
1358 * references to multiple originators) and adjust the flags attribute to reflect
1359 * the function argument.
1360 * If a TT local entry exists for this non-mesh client remove it.
1362 * The caller must hold orig_node refcount.
1364 * Return: true if the new entry has been added, false otherwise
1366 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1367 struct batadv_orig_node *orig_node,
1368 const unsigned char *tt_addr,
1369 unsigned short vid, u16 flags, u8 ttvn)
1371 struct batadv_tt_global_entry *tt_global_entry;
1372 struct batadv_tt_local_entry *tt_local_entry;
1375 struct batadv_tt_common_entry *common;
1378 /* ignore global entries from backbone nodes */
1379 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1382 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1383 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1385 /* if the node already has a local client for this entry, it has to wait
1386 * for a roaming advertisement instead of manually messing up the global
1389 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1390 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1393 if (!tt_global_entry) {
1394 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1395 if (!tt_global_entry)
1398 common = &tt_global_entry->common;
1399 ether_addr_copy(common->addr, tt_addr);
1402 common->flags = flags;
1403 tt_global_entry->roam_at = 0;
1404 /* node must store current time in case of roaming. This is
1405 * needed to purge this entry out on timeout (if nobody claims
1408 if (flags & BATADV_TT_CLIENT_ROAM)
1409 tt_global_entry->roam_at = jiffies;
1410 atomic_set(&common->refcount, 2);
1411 common->added_at = jiffies;
1413 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1414 atomic_set(&tt_global_entry->orig_list_count, 0);
1415 spin_lock_init(&tt_global_entry->list_lock);
1417 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1419 batadv_choose_tt, common,
1420 &common->hash_entry);
1422 if (unlikely(hash_added != 0)) {
1423 /* remove the reference for the hash */
1424 batadv_tt_global_entry_free_ref(tt_global_entry);
1428 common = &tt_global_entry->common;
1429 /* If there is already a global entry, we can use this one for
1431 * But if we are trying to add a temporary client then here are
1432 * two options at this point:
1433 * 1) the global client is not a temporary client: the global
1434 * client has to be left as it is, temporary information
1435 * should never override any already known client state
1436 * 2) the global client is a temporary client: purge the
1437 * originator list and add the new one orig_entry
1439 if (flags & BATADV_TT_CLIENT_TEMP) {
1440 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1442 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1445 batadv_tt_global_del_orig_list(tt_global_entry);
1446 goto add_orig_entry;
1449 /* if the client was temporary added before receiving the first
1450 * OGM announcing it, we have to clear the TEMP flag. Also,
1451 * remove the previous temporary orig node and re-add it
1452 * if required. If the orig entry changed, the new one which
1453 * is a non-temporary entry is preferred.
1455 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1456 batadv_tt_global_del_orig_list(tt_global_entry);
1457 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1460 /* the change can carry possible "attribute" flags like the
1461 * TT_CLIENT_WIFI, therefore they have to be copied in the
1464 common->flags |= flags;
1466 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1467 * one originator left in the list and we previously received a
1468 * delete + roaming change for this originator.
1470 * We should first delete the old originator before adding the
1473 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1474 batadv_tt_global_del_orig_list(tt_global_entry);
1475 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1476 tt_global_entry->roam_at = 0;
1480 /* add the new orig_entry (if needed) or update it */
1481 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1483 batadv_dbg(BATADV_DBG_TT, bat_priv,
1484 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1485 common->addr, BATADV_PRINT_VID(common->vid),
1490 /* Do not remove multicast addresses from the local hash on
1493 if (is_multicast_ether_addr(tt_addr))
1496 /* remove address from local hash if present */
1497 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1498 "global tt received",
1499 flags & BATADV_TT_CLIENT_ROAM);
1500 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1502 if (!(flags & BATADV_TT_CLIENT_ROAM))
1503 /* this is a normal global add. Therefore the client is not in a
1504 * roaming state anymore.
1506 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1509 if (tt_global_entry)
1510 batadv_tt_global_entry_free_ref(tt_global_entry);
1512 batadv_tt_local_entry_free_ref(tt_local_entry);
1517 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1518 * @bat_priv: the bat priv with all the soft interface information
1519 * @tt_global_entry: global translation table entry to be analyzed
1521 * This functon assumes the caller holds rcu_read_lock().
1522 * Return: best originator list entry or NULL on errors.
1524 static struct batadv_tt_orig_list_entry *
1525 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1526 struct batadv_tt_global_entry *tt_global_entry)
1528 struct batadv_neigh_node *router, *best_router = NULL;
1529 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1530 struct hlist_head *head;
1531 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1533 head = &tt_global_entry->orig_list;
1534 hlist_for_each_entry_rcu(orig_entry, head, list) {
1535 router = batadv_orig_router_get(orig_entry->orig_node,
1541 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1542 best_router, BATADV_IF_DEFAULT) <= 0) {
1543 batadv_neigh_node_free_ref(router);
1547 /* release the refcount for the "old" best */
1549 batadv_neigh_node_free_ref(best_router);
1551 best_entry = orig_entry;
1552 best_router = router;
1556 batadv_neigh_node_free_ref(best_router);
1562 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1563 * for this global entry
1564 * @bat_priv: the bat priv with all the soft interface information
1565 * @tt_global_entry: global translation table entry to be printed
1566 * @seq: debugfs table seq_file struct
1568 * This functon assumes the caller holds rcu_read_lock().
1571 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1572 struct batadv_tt_global_entry *tt_global_entry,
1573 struct seq_file *seq)
1575 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1576 struct batadv_tt_common_entry *tt_common_entry;
1577 struct batadv_orig_node_vlan *vlan;
1578 struct hlist_head *head;
1582 tt_common_entry = &tt_global_entry->common;
1583 flags = tt_common_entry->flags;
1585 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1587 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1588 tt_common_entry->vid);
1591 " * Cannot retrieve VLAN %d for originator %pM\n",
1592 BATADV_PRINT_VID(tt_common_entry->vid),
1593 best_entry->orig_node->orig);
1597 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1599 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1600 '*', tt_global_entry->common.addr,
1601 BATADV_PRINT_VID(tt_global_entry->common.vid),
1602 best_entry->ttvn, best_entry->orig_node->orig,
1603 last_ttvn, vlan->tt.crc,
1604 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1605 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1606 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1607 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1609 batadv_orig_node_vlan_free_ref(vlan);
1613 head = &tt_global_entry->orig_list;
1615 hlist_for_each_entry_rcu(orig_entry, head, list) {
1616 if (best_entry == orig_entry)
1619 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1620 tt_common_entry->vid);
1623 " + Cannot retrieve VLAN %d for originator %pM\n",
1624 BATADV_PRINT_VID(tt_common_entry->vid),
1625 orig_entry->orig_node->orig);
1629 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1631 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1632 '+', tt_global_entry->common.addr,
1633 BATADV_PRINT_VID(tt_global_entry->common.vid),
1634 orig_entry->ttvn, orig_entry->orig_node->orig,
1635 last_ttvn, vlan->tt.crc,
1636 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1637 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1638 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1639 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1641 batadv_orig_node_vlan_free_ref(vlan);
1645 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1647 struct net_device *net_dev = (struct net_device *)seq->private;
1648 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1649 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1650 struct batadv_tt_common_entry *tt_common_entry;
1651 struct batadv_tt_global_entry *tt_global;
1652 struct batadv_hard_iface *primary_if;
1653 struct hlist_head *head;
1656 primary_if = batadv_seq_print_text_primary_if_get(seq);
1661 "Globally announced TT entries received via the mesh %s\n",
1663 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1664 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1667 for (i = 0; i < hash->size; i++) {
1668 head = &hash->table[i];
1671 hlist_for_each_entry_rcu(tt_common_entry,
1673 tt_global = container_of(tt_common_entry,
1674 struct batadv_tt_global_entry,
1676 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1682 batadv_hardif_free_ref(primary_if);
1687 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1688 * @tt_global_entry: the global entry to remove the orig_entry from
1689 * @orig_entry: the orig entry to remove and free
1691 * Remove an orig_entry from its list in the given tt_global_entry and
1692 * free this orig_entry afterwards.
1694 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1698 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1699 struct batadv_tt_orig_list_entry *orig_entry)
1701 lockdep_assert_held(&tt_global_entry->list_lock);
1703 batadv_tt_global_size_dec(orig_entry->orig_node,
1704 tt_global_entry->common.vid);
1705 atomic_dec(&tt_global_entry->orig_list_count);
1706 /* requires holding tt_global_entry->list_lock and orig_entry->list
1707 * being part of a list
1709 hlist_del_rcu(&orig_entry->list);
1710 batadv_tt_orig_list_entry_free_ref(orig_entry);
1713 /* deletes the orig list of a tt_global_entry */
1715 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1717 struct hlist_head *head;
1718 struct hlist_node *safe;
1719 struct batadv_tt_orig_list_entry *orig_entry;
1721 spin_lock_bh(&tt_global_entry->list_lock);
1722 head = &tt_global_entry->orig_list;
1723 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1724 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1725 spin_unlock_bh(&tt_global_entry->list_lock);
1729 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1730 * @bat_priv: the bat priv with all the soft interface information
1731 * @tt_global_entry: the global entry to remove the orig_node from
1732 * @orig_node: the originator announcing the client
1733 * @message: message to append to the log on deletion
1735 * Remove the given orig_node and its according orig_entry from the given
1739 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1740 struct batadv_tt_global_entry *tt_global_entry,
1741 struct batadv_orig_node *orig_node,
1742 const char *message)
1744 struct hlist_head *head;
1745 struct hlist_node *safe;
1746 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 if (orig_entry->orig_node == orig_node) {
1753 vid = tt_global_entry->common.vid;
1754 batadv_dbg(BATADV_DBG_TT, bat_priv,
1755 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1757 tt_global_entry->common.addr,
1758 BATADV_PRINT_VID(vid), message);
1759 _batadv_tt_global_del_orig_entry(tt_global_entry,
1763 spin_unlock_bh(&tt_global_entry->list_lock);
1766 /* If the client is to be deleted, we check if it is the last origantor entry
1767 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1768 * timer, otherwise we simply remove the originator scheduled for deletion.
1771 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1772 struct batadv_tt_global_entry *tt_global_entry,
1773 struct batadv_orig_node *orig_node,
1774 const char *message)
1776 bool last_entry = true;
1777 struct hlist_head *head;
1778 struct batadv_tt_orig_list_entry *orig_entry;
1780 /* no local entry exists, case 1:
1781 * Check if this is the last one or if other entries exist.
1785 head = &tt_global_entry->orig_list;
1786 hlist_for_each_entry_rcu(orig_entry, head, list) {
1787 if (orig_entry->orig_node != orig_node) {
1795 /* its the last one, mark for roaming. */
1796 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1797 tt_global_entry->roam_at = jiffies;
1799 /* there is another entry, we can simply delete this
1800 * one and can still use the other one.
1802 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1803 orig_node, message);
1807 * batadv_tt_global_del - remove a client from the global table
1808 * @bat_priv: the bat priv with all the soft interface information
1809 * @orig_node: an originator serving this client
1810 * @addr: the mac address of the client
1811 * @vid: VLAN identifier
1812 * @message: a message explaining the reason for deleting the client to print
1813 * for debugging purpose
1814 * @roaming: true if the deletion has been triggered by a roaming event
1816 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1817 struct batadv_orig_node *orig_node,
1818 const unsigned char *addr, unsigned short vid,
1819 const char *message, bool roaming)
1821 struct batadv_tt_global_entry *tt_global_entry;
1822 struct batadv_tt_local_entry *local_entry = NULL;
1824 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1825 if (!tt_global_entry)
1829 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1830 orig_node, message);
1832 if (hlist_empty(&tt_global_entry->orig_list))
1833 batadv_tt_global_free(bat_priv, tt_global_entry,
1839 /* if we are deleting a global entry due to a roam
1840 * event, there are two possibilities:
1841 * 1) the client roamed from node A to node B => if there
1842 * is only one originator left for this client, we mark
1843 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1844 * wait for node B to claim it. In case of timeout
1845 * the entry is purged.
1847 * If there are other originators left, we directly delete
1849 * 2) the client roamed to us => we can directly delete
1850 * the global entry, since it is useless now.
1852 local_entry = batadv_tt_local_hash_find(bat_priv,
1853 tt_global_entry->common.addr,
1856 /* local entry exists, case 2: client roamed to us. */
1857 batadv_tt_global_del_orig_list(tt_global_entry);
1858 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1860 /* no local entry exists, case 1: check for roaming */
1861 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1862 orig_node, message);
1865 if (tt_global_entry)
1866 batadv_tt_global_entry_free_ref(tt_global_entry);
1868 batadv_tt_local_entry_free_ref(local_entry);
1872 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1873 * given originator matching the provided vid
1874 * @bat_priv: the bat priv with all the soft interface information
1875 * @orig_node: the originator owning the entries to remove
1876 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1878 * @message: debug message to print as "reason"
1880 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1881 struct batadv_orig_node *orig_node,
1883 const char *message)
1885 struct batadv_tt_global_entry *tt_global;
1886 struct batadv_tt_common_entry *tt_common_entry;
1888 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1889 struct hlist_node *safe;
1890 struct hlist_head *head;
1891 spinlock_t *list_lock; /* protects write access to the hash lists */
1897 for (i = 0; i < hash->size; i++) {
1898 head = &hash->table[i];
1899 list_lock = &hash->list_locks[i];
1901 spin_lock_bh(list_lock);
1902 hlist_for_each_entry_safe(tt_common_entry, safe,
1904 /* remove only matching entries */
1905 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1908 tt_global = container_of(tt_common_entry,
1909 struct batadv_tt_global_entry,
1912 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1913 orig_node, message);
1915 if (hlist_empty(&tt_global->orig_list)) {
1916 vid = tt_global->common.vid;
1917 batadv_dbg(BATADV_DBG_TT, bat_priv,
1918 "Deleting global tt entry %pM (vid: %d): %s\n",
1919 tt_global->common.addr,
1920 BATADV_PRINT_VID(vid), message);
1921 hlist_del_rcu(&tt_common_entry->hash_entry);
1922 batadv_tt_global_entry_free_ref(tt_global);
1925 spin_unlock_bh(list_lock);
1927 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1930 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1934 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1935 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1937 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1938 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1940 *msg = "Roaming timeout\n";
1943 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1944 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1946 *msg = "Temporary client timeout\n";
1952 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1954 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1955 struct hlist_head *head;
1956 struct hlist_node *node_tmp;
1957 spinlock_t *list_lock; /* protects write access to the hash lists */
1960 struct batadv_tt_common_entry *tt_common;
1961 struct batadv_tt_global_entry *tt_global;
1963 for (i = 0; i < hash->size; i++) {
1964 head = &hash->table[i];
1965 list_lock = &hash->list_locks[i];
1967 spin_lock_bh(list_lock);
1968 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1970 tt_global = container_of(tt_common,
1971 struct batadv_tt_global_entry,
1974 if (!batadv_tt_global_to_purge(tt_global, &msg))
1977 batadv_dbg(BATADV_DBG_TT, bat_priv,
1978 "Deleting global tt entry %pM (vid: %d): %s\n",
1979 tt_global->common.addr,
1980 BATADV_PRINT_VID(tt_global->common.vid),
1983 hlist_del_rcu(&tt_common->hash_entry);
1985 batadv_tt_global_entry_free_ref(tt_global);
1987 spin_unlock_bh(list_lock);
1991 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1993 struct batadv_hashtable *hash;
1994 spinlock_t *list_lock; /* protects write access to the hash lists */
1995 struct batadv_tt_common_entry *tt_common_entry;
1996 struct batadv_tt_global_entry *tt_global;
1997 struct hlist_node *node_tmp;
1998 struct hlist_head *head;
2001 if (!bat_priv->tt.global_hash)
2004 hash = bat_priv->tt.global_hash;
2006 for (i = 0; i < hash->size; i++) {
2007 head = &hash->table[i];
2008 list_lock = &hash->list_locks[i];
2010 spin_lock_bh(list_lock);
2011 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2013 hlist_del_rcu(&tt_common_entry->hash_entry);
2014 tt_global = container_of(tt_common_entry,
2015 struct batadv_tt_global_entry,
2017 batadv_tt_global_entry_free_ref(tt_global);
2019 spin_unlock_bh(list_lock);
2022 batadv_hash_destroy(hash);
2024 bat_priv->tt.global_hash = NULL;
2028 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2029 struct batadv_tt_global_entry *tt_global_entry)
2033 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2034 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2037 /* check if the two clients are marked as isolated */
2038 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2039 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2046 * batadv_transtable_search - get the mesh destination for a given client
2047 * @bat_priv: the bat priv with all the soft interface information
2048 * @src: mac address of the source client
2049 * @addr: mac address of the destination client
2050 * @vid: VLAN identifier
2052 * Return: a pointer to the originator that was selected as destination in the
2053 * mesh for contacting the client 'addr', NULL otherwise.
2054 * In case of multiple originators serving the same client, the function returns
2055 * the best one (best in terms of metric towards the destination node).
2057 * If the two clients are AP isolated the function returns NULL.
2059 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2064 struct batadv_tt_local_entry *tt_local_entry = NULL;
2065 struct batadv_tt_global_entry *tt_global_entry = NULL;
2066 struct batadv_orig_node *orig_node = NULL;
2067 struct batadv_tt_orig_list_entry *best_entry;
2069 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2070 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2071 if (!tt_local_entry ||
2072 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2076 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2077 if (!tt_global_entry)
2080 /* check whether the clients should not communicate due to AP
2083 if (tt_local_entry &&
2084 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2088 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2089 /* found anything? */
2091 orig_node = best_entry->orig_node;
2092 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2097 if (tt_global_entry)
2098 batadv_tt_global_entry_free_ref(tt_global_entry);
2100 batadv_tt_local_entry_free_ref(tt_local_entry);
2106 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2107 * to the given orig_node
2108 * @bat_priv: the bat priv with all the soft interface information
2109 * @orig_node: originator for which the CRC should be computed
2110 * @vid: VLAN identifier for which the CRC32 has to be computed
2112 * This function computes the checksum for the global table corresponding to a
2113 * specific originator. In particular, the checksum is computed as follows: For
2114 * each client connected to the originator the CRC32C of the MAC address and the
2115 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2118 * The idea behind is that CRC32C should be used as much as possible in order to
2119 * produce a unique hash of the table, but since the order which is used to feed
2120 * the CRC32C function affects the result and since every node in the network
2121 * probably sorts the clients differently, the hash function cannot be directly
2122 * computed over the entire table. Hence the CRC32C is used only on
2123 * the single client entry, while all the results are then xor'ed together
2124 * because the XOR operation can combine them all while trying to reduce the
2125 * noise as much as possible.
2127 * Return: the checksum of the global table of a given originator.
2129 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2130 struct batadv_orig_node *orig_node,
2133 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2134 struct batadv_tt_common_entry *tt_common;
2135 struct batadv_tt_global_entry *tt_global;
2136 struct hlist_head *head;
2137 u32 i, crc_tmp, crc = 0;
2141 for (i = 0; i < hash->size; i++) {
2142 head = &hash->table[i];
2145 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2146 tt_global = container_of(tt_common,
2147 struct batadv_tt_global_entry,
2149 /* compute the CRC only for entries belonging to the
2150 * VLAN identified by the vid passed as parameter
2152 if (tt_common->vid != vid)
2155 /* Roaming clients are in the global table for
2156 * consistency only. They don't have to be
2157 * taken into account while computing the
2160 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2162 /* Temporary clients have not been announced yet, so
2163 * they have to be skipped while computing the global
2166 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2169 /* find out if this global entry is announced by this
2172 if (!batadv_tt_global_entry_has_orig(tt_global,
2176 /* use network order to read the VID: this ensures that
2177 * every node reads the bytes in the same order.
2179 tmp_vid = htons(tt_common->vid);
2180 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2182 /* compute the CRC on flags that have to be kept in sync
2185 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2186 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2188 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2197 * batadv_tt_local_crc - calculates the checksum of the local table
2198 * @bat_priv: the bat priv with all the soft interface information
2199 * @vid: VLAN identifier for which the CRC32 has to be computed
2201 * For details about the computation, please refer to the documentation for
2202 * batadv_tt_global_crc().
2204 * Return: the checksum of the local table
2206 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2209 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2210 struct batadv_tt_common_entry *tt_common;
2211 struct hlist_head *head;
2212 u32 i, crc_tmp, crc = 0;
2216 for (i = 0; i < hash->size; i++) {
2217 head = &hash->table[i];
2220 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2221 /* compute the CRC only for entries belonging to the
2222 * VLAN identified by vid
2224 if (tt_common->vid != vid)
2227 /* not yet committed clients have not to be taken into
2228 * account while computing the CRC
2230 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2233 /* use network order to read the VID: this ensures that
2234 * every node reads the bytes in the same order.
2236 tmp_vid = htons(tt_common->vid);
2237 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2239 /* compute the CRC on flags that have to be kept in sync
2242 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2243 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2245 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2253 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2255 struct batadv_tt_req_node *node;
2256 struct hlist_node *safe;
2258 spin_lock_bh(&bat_priv->tt.req_list_lock);
2260 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2261 hlist_del_init(&node->list);
2265 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2268 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2269 struct batadv_orig_node *orig_node,
2270 const void *tt_buff,
2273 /* Replace the old buffer only if I received something in the
2274 * last OGM (the OGM could carry no changes)
2276 spin_lock_bh(&orig_node->tt_buff_lock);
2277 if (tt_buff_len > 0) {
2278 kfree(orig_node->tt_buff);
2279 orig_node->tt_buff_len = 0;
2280 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2281 if (orig_node->tt_buff) {
2282 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2283 orig_node->tt_buff_len = tt_buff_len;
2286 spin_unlock_bh(&orig_node->tt_buff_lock);
2289 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2291 struct batadv_tt_req_node *node;
2292 struct hlist_node *safe;
2294 spin_lock_bh(&bat_priv->tt.req_list_lock);
2295 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2296 if (batadv_has_timed_out(node->issued_at,
2297 BATADV_TT_REQUEST_TIMEOUT)) {
2298 hlist_del_init(&node->list);
2302 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2306 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2307 * @bat_priv: the bat priv with all the soft interface information
2308 * @orig_node: orig node this request is being issued for
2310 * Return: the pointer to the new tt_req_node struct if no request
2311 * has already been issued for this orig_node, NULL otherwise.
2313 static struct batadv_tt_req_node *
2314 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2315 struct batadv_orig_node *orig_node)
2317 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2319 spin_lock_bh(&bat_priv->tt.req_list_lock);
2320 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2321 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2322 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2323 BATADV_TT_REQUEST_TIMEOUT))
2327 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2331 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2332 tt_req_node->issued_at = jiffies;
2334 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2336 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2341 * batadv_tt_local_valid - verify that given tt entry is a valid one
2342 * @entry_ptr: to be checked local tt entry
2343 * @data_ptr: not used but definition required to satisfy the callback prototype
2345 * Return: 1 if the entry is a valid, 0 otherwise.
2347 static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2349 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2351 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2356 static int batadv_tt_global_valid(const void *entry_ptr,
2357 const void *data_ptr)
2359 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2360 const struct batadv_tt_global_entry *tt_global_entry;
2361 const struct batadv_orig_node *orig_node = data_ptr;
2363 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2364 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2367 tt_global_entry = container_of(tt_common_entry,
2368 struct batadv_tt_global_entry,
2371 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2375 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2377 * @bat_priv: the bat priv with all the soft interface information
2378 * @hash: hash table containing the tt entries
2379 * @tt_len: expected tvlv tt data buffer length in number of bytes
2380 * @tvlv_buff: pointer to the buffer to fill with the TT data
2381 * @valid_cb: function to filter tt change entries
2382 * @cb_data: data passed to the filter function as argument
2384 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2385 struct batadv_hashtable *hash,
2386 void *tvlv_buff, u16 tt_len,
2387 int (*valid_cb)(const void *, const void *),
2390 struct batadv_tt_common_entry *tt_common_entry;
2391 struct batadv_tvlv_tt_change *tt_change;
2392 struct hlist_head *head;
2393 u16 tt_tot, tt_num_entries = 0;
2396 tt_tot = batadv_tt_entries(tt_len);
2397 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2400 for (i = 0; i < hash->size; i++) {
2401 head = &hash->table[i];
2403 hlist_for_each_entry_rcu(tt_common_entry,
2405 if (tt_tot == tt_num_entries)
2408 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2411 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2412 tt_change->flags = tt_common_entry->flags;
2413 tt_change->vid = htons(tt_common_entry->vid);
2414 memset(tt_change->reserved, 0,
2415 sizeof(tt_change->reserved));
2425 * batadv_tt_global_check_crc - check if all the CRCs are correct
2426 * @orig_node: originator for which the CRCs have to be checked
2427 * @tt_vlan: pointer to the first tvlv VLAN entry
2428 * @num_vlan: number of tvlv VLAN entries
2430 * Return: true if all the received CRCs match the locally stored ones, false
2433 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2434 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2437 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2438 struct batadv_orig_node_vlan *vlan;
2439 int i, orig_num_vlan;
2442 /* check if each received CRC matches the locally stored one */
2443 for (i = 0; i < num_vlan; i++) {
2444 tt_vlan_tmp = tt_vlan + i;
2446 /* if orig_node is a backbone node for this VLAN, don't check
2447 * the CRC as we ignore all the global entries over it
2449 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2451 ntohs(tt_vlan_tmp->vid)))
2454 vlan = batadv_orig_node_vlan_get(orig_node,
2455 ntohs(tt_vlan_tmp->vid));
2460 batadv_orig_node_vlan_free_ref(vlan);
2462 if (crc != ntohl(tt_vlan_tmp->crc))
2466 /* check if any excess VLANs exist locally for the originator
2467 * which are not mentioned in the TVLV from the originator.
2471 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2475 if (orig_num_vlan > num_vlan)
2482 * batadv_tt_local_update_crc - update all the local CRCs
2483 * @bat_priv: the bat priv with all the soft interface information
2485 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2487 struct batadv_softif_vlan *vlan;
2489 /* recompute the global CRC for each VLAN */
2491 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2492 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2498 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2499 * @bat_priv: the bat priv with all the soft interface information
2500 * @orig_node: the orig_node for which the CRCs have to be updated
2502 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2503 struct batadv_orig_node *orig_node)
2505 struct batadv_orig_node_vlan *vlan;
2508 /* recompute the global CRC for each VLAN */
2510 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2511 /* if orig_node is a backbone node for this VLAN, don't compute
2512 * the CRC as we ignore all the global entries over it
2514 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2518 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2525 * batadv_send_tt_request - send a TT Request message to a given node
2526 * @bat_priv: the bat priv with all the soft interface information
2527 * @dst_orig_node: the destination of the message
2528 * @ttvn: the version number that the source of the message is looking for
2529 * @tt_vlan: pointer to the first tvlv VLAN object to request
2530 * @num_vlan: number of tvlv VLAN entries
2531 * @full_table: ask for the entire translation table if true, while only for the
2532 * last TT diff otherwise
2534 * Return: true if the TT Request was sent, false otherwise
2536 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2537 struct batadv_orig_node *dst_orig_node,
2539 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2540 u16 num_vlan, bool full_table)
2542 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2543 struct batadv_tt_req_node *tt_req_node = NULL;
2544 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2545 struct batadv_hard_iface *primary_if;
2549 primary_if = batadv_primary_if_get_selected(bat_priv);
2553 /* The new tt_req will be issued only if I'm not waiting for a
2554 * reply from the same orig_node yet
2556 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2560 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2561 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2565 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2566 tvlv_tt_data->ttvn = ttvn;
2567 tvlv_tt_data->num_vlan = htons(num_vlan);
2569 /* send all the CRCs within the request. This is needed by intermediate
2570 * nodes to ensure they have the correct table before replying
2572 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2573 for (i = 0; i < num_vlan; i++) {
2574 tt_vlan_req->vid = tt_vlan->vid;
2575 tt_vlan_req->crc = tt_vlan->crc;
2582 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2584 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2585 dst_orig_node->orig, full_table ? 'F' : '.');
2587 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2588 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2589 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2590 tvlv_tt_data, size);
2595 batadv_hardif_free_ref(primary_if);
2596 if (ret && tt_req_node) {
2597 spin_lock_bh(&bat_priv->tt.req_list_lock);
2598 /* hlist_del_init() verifies tt_req_node still is in the list */
2599 hlist_del_init(&tt_req_node->list);
2600 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2603 kfree(tvlv_tt_data);
2608 * batadv_send_other_tt_response - send reply to tt request concerning another
2609 * node's translation table
2610 * @bat_priv: the bat priv with all the soft interface information
2611 * @tt_data: tt data containing the tt request information
2612 * @req_src: mac address of tt request sender
2613 * @req_dst: mac address of tt request recipient
2615 * Return: true if tt request reply was sent, false otherwise.
2617 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2618 struct batadv_tvlv_tt_data *tt_data,
2619 u8 *req_src, u8 *req_dst)
2621 struct batadv_orig_node *req_dst_orig_node;
2622 struct batadv_orig_node *res_dst_orig_node = NULL;
2623 struct batadv_tvlv_tt_change *tt_change;
2624 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2625 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2626 bool ret = false, full_table;
2627 u8 orig_ttvn, req_ttvn;
2631 batadv_dbg(BATADV_DBG_TT, bat_priv,
2632 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2633 req_src, tt_data->ttvn, req_dst,
2634 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2636 /* Let's get the orig node of the REAL destination */
2637 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2638 if (!req_dst_orig_node)
2641 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2642 if (!res_dst_orig_node)
2645 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2646 req_ttvn = tt_data->ttvn;
2648 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2649 /* this node doesn't have the requested data */
2650 if (orig_ttvn != req_ttvn ||
2651 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2652 ntohs(tt_data->num_vlan)))
2655 /* If the full table has been explicitly requested */
2656 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2657 !req_dst_orig_node->tt_buff)
2662 /* TT fragmentation hasn't been implemented yet, so send as many
2663 * TT entries fit a single packet as possible only
2666 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2667 tt_len = req_dst_orig_node->tt_buff_len;
2669 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2676 /* Copy the last orig_node's OGM buffer */
2677 memcpy(tt_change, req_dst_orig_node->tt_buff,
2678 req_dst_orig_node->tt_buff_len);
2679 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2681 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2682 * in the initial part
2685 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2692 /* fill the rest of the tvlv with the real TT entries */
2693 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2695 batadv_tt_global_valid,
2699 /* Don't send the response, if larger than fragmented packet. */
2700 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2701 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2702 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2703 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2704 res_dst_orig_node->orig);
2708 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2709 tvlv_tt_data->ttvn = req_ttvn;
2712 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2714 batadv_dbg(BATADV_DBG_TT, bat_priv,
2715 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2716 res_dst_orig_node->orig, req_dst_orig_node->orig,
2717 full_table ? 'F' : '.', req_ttvn);
2719 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2721 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2722 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2729 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2732 if (res_dst_orig_node)
2733 batadv_orig_node_free_ref(res_dst_orig_node);
2734 if (req_dst_orig_node)
2735 batadv_orig_node_free_ref(req_dst_orig_node);
2736 kfree(tvlv_tt_data);
2741 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2743 * @bat_priv: the bat priv with all the soft interface information
2744 * @tt_data: tt data containing the tt request information
2745 * @req_src: mac address of tt request sender
2747 * Return: true if tt request reply was sent, false otherwise.
2749 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2750 struct batadv_tvlv_tt_data *tt_data,
2753 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2754 struct batadv_hard_iface *primary_if = NULL;
2755 struct batadv_tvlv_tt_change *tt_change;
2756 struct batadv_orig_node *orig_node;
2757 u8 my_ttvn, req_ttvn;
2762 batadv_dbg(BATADV_DBG_TT, bat_priv,
2763 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2764 req_src, tt_data->ttvn,
2765 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2767 spin_lock_bh(&bat_priv->tt.commit_lock);
2769 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2770 req_ttvn = tt_data->ttvn;
2772 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2776 primary_if = batadv_primary_if_get_selected(bat_priv);
2780 /* If the full table has been explicitly requested or the gap
2781 * is too big send the whole local translation table
2783 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2784 !bat_priv->tt.last_changeset)
2789 /* TT fragmentation hasn't been implemented yet, so send as many
2790 * TT entries fit a single packet as possible only
2793 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2795 tt_len = bat_priv->tt.last_changeset_len;
2796 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2803 /* Copy the last orig_node's OGM buffer */
2804 memcpy(tt_change, bat_priv->tt.last_changeset,
2805 bat_priv->tt.last_changeset_len);
2806 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2808 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2810 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2811 * in the initial part
2814 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2821 /* fill the rest of the tvlv with the real TT entries */
2822 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2824 batadv_tt_local_valid, NULL);
2827 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2828 tvlv_tt_data->ttvn = req_ttvn;
2831 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2833 batadv_dbg(BATADV_DBG_TT, bat_priv,
2834 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2835 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2837 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2839 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2840 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2846 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2848 spin_unlock_bh(&bat_priv->tt.commit_lock);
2850 batadv_orig_node_free_ref(orig_node);
2852 batadv_hardif_free_ref(primary_if);
2853 kfree(tvlv_tt_data);
2854 /* The packet was for this host, so it doesn't need to be re-routed */
2859 * batadv_send_tt_response - send reply to tt request
2860 * @bat_priv: the bat priv with all the soft interface information
2861 * @tt_data: tt data containing the tt request information
2862 * @req_src: mac address of tt request sender
2863 * @req_dst: mac address of tt request recipient
2865 * Return: true if tt request reply was sent, false otherwise.
2867 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2868 struct batadv_tvlv_tt_data *tt_data,
2869 u8 *req_src, u8 *req_dst)
2871 if (batadv_is_my_mac(bat_priv, req_dst))
2872 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2873 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2877 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2878 struct batadv_orig_node *orig_node,
2879 struct batadv_tvlv_tt_change *tt_change,
2880 u16 tt_num_changes, u8 ttvn)
2885 for (i = 0; i < tt_num_changes; i++) {
2886 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2887 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2888 batadv_tt_global_del(bat_priv, orig_node,
2889 (tt_change + i)->addr,
2890 ntohs((tt_change + i)->vid),
2891 "tt removed by changes",
2894 if (!batadv_tt_global_add(bat_priv, orig_node,
2895 (tt_change + i)->addr,
2896 ntohs((tt_change + i)->vid),
2897 (tt_change + i)->flags, ttvn))
2898 /* In case of problem while storing a
2899 * global_entry, we stop the updating
2900 * procedure without committing the
2901 * ttvn change. This will avoid to send
2902 * corrupted data on tt_request
2907 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2910 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2911 struct batadv_tvlv_tt_change *tt_change,
2912 u8 ttvn, u8 *resp_src,
2915 struct batadv_orig_node *orig_node;
2917 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2921 /* Purge the old table first.. */
2922 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2923 "Received full table");
2925 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2928 spin_lock_bh(&orig_node->tt_buff_lock);
2929 kfree(orig_node->tt_buff);
2930 orig_node->tt_buff_len = 0;
2931 orig_node->tt_buff = NULL;
2932 spin_unlock_bh(&orig_node->tt_buff_lock);
2934 atomic_set(&orig_node->last_ttvn, ttvn);
2938 batadv_orig_node_free_ref(orig_node);
2941 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2942 struct batadv_orig_node *orig_node,
2943 u16 tt_num_changes, u8 ttvn,
2944 struct batadv_tvlv_tt_change *tt_change)
2946 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2947 tt_num_changes, ttvn);
2949 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2950 batadv_tt_len(tt_num_changes));
2951 atomic_set(&orig_node->last_ttvn, ttvn);
2955 * batadv_is_my_client - check if a client is served by the local node
2956 * @bat_priv: the bat priv with all the soft interface information
2957 * @addr: the mac address of the client to check
2958 * @vid: VLAN identifier
2960 * Return: true if the client is served by this node, false otherwise.
2962 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
2965 struct batadv_tt_local_entry *tt_local_entry;
2968 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2969 if (!tt_local_entry)
2971 /* Check if the client has been logically deleted (but is kept for
2972 * consistency purpose)
2974 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2975 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2980 batadv_tt_local_entry_free_ref(tt_local_entry);
2985 * batadv_handle_tt_response - process incoming tt reply
2986 * @bat_priv: the bat priv with all the soft interface information
2987 * @tt_data: tt data containing the tt request information
2988 * @resp_src: mac address of tt reply sender
2989 * @num_entries: number of tt change entries appended to the tt data
2991 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2992 struct batadv_tvlv_tt_data *tt_data,
2993 u8 *resp_src, u16 num_entries)
2995 struct batadv_tt_req_node *node;
2996 struct hlist_node *safe;
2997 struct batadv_orig_node *orig_node = NULL;
2998 struct batadv_tvlv_tt_change *tt_change;
2999 u8 *tvlv_ptr = (u8 *)tt_data;
3002 batadv_dbg(BATADV_DBG_TT, bat_priv,
3003 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3004 resp_src, tt_data->ttvn, num_entries,
3005 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3007 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3011 spin_lock_bh(&orig_node->tt_lock);
3013 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3014 change_offset *= ntohs(tt_data->num_vlan);
3015 change_offset += sizeof(*tt_data);
3016 tvlv_ptr += change_offset;
3018 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3019 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3020 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3021 resp_src, num_entries);
3023 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3024 tt_data->ttvn, tt_change);
3027 /* Recalculate the CRC for this orig_node and store it */
3028 batadv_tt_global_update_crc(bat_priv, orig_node);
3030 spin_unlock_bh(&orig_node->tt_lock);
3032 /* Delete the tt_req_node from pending tt_requests list */
3033 spin_lock_bh(&bat_priv->tt.req_list_lock);
3034 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3035 if (!batadv_compare_eth(node->addr, resp_src))
3037 hlist_del_init(&node->list);
3041 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3044 batadv_orig_node_free_ref(orig_node);
3047 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3049 struct batadv_tt_roam_node *node, *safe;
3051 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3053 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3054 list_del(&node->list);
3058 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3061 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3063 struct batadv_tt_roam_node *node, *safe;
3065 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3066 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3067 if (!batadv_has_timed_out(node->first_time,
3068 BATADV_ROAMING_MAX_TIME))
3071 list_del(&node->list);
3074 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3078 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3079 * @bat_priv: the bat priv with all the soft interface information
3080 * @client: mac address of the roaming client
3082 * This function checks whether the client already reached the
3083 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3086 * Return: true if the ROAMING_ADV can be sent, false otherwise
3088 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3090 struct batadv_tt_roam_node *tt_roam_node;
3093 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3094 /* The new tt_req will be issued only if I'm not waiting for a
3095 * reply from the same orig_node yet
3097 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3098 if (!batadv_compare_eth(tt_roam_node->addr, client))
3101 if (batadv_has_timed_out(tt_roam_node->first_time,
3102 BATADV_ROAMING_MAX_TIME))
3105 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3106 /* Sorry, you roamed too many times! */
3113 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3117 tt_roam_node->first_time = jiffies;
3118 atomic_set(&tt_roam_node->counter,
3119 BATADV_ROAMING_MAX_COUNT - 1);
3120 ether_addr_copy(tt_roam_node->addr, client);
3122 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3127 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3132 * batadv_send_roam_adv - send a roaming advertisement message
3133 * @bat_priv: the bat priv with all the soft interface information
3134 * @client: mac address of the roaming client
3135 * @vid: VLAN identifier
3136 * @orig_node: message destination
3138 * Send a ROAMING_ADV message to the node which was previously serving this
3139 * client. This is done to inform the node that from now on all traffic destined
3140 * for this particular roamed client has to be forwarded to the sender of the
3143 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3145 struct batadv_orig_node *orig_node)
3147 struct batadv_hard_iface *primary_if;
3148 struct batadv_tvlv_roam_adv tvlv_roam;
3150 primary_if = batadv_primary_if_get_selected(bat_priv);
3154 /* before going on we have to check whether the client has
3155 * already roamed to us too many times
3157 if (!batadv_tt_check_roam_count(bat_priv, client))
3160 batadv_dbg(BATADV_DBG_TT, bat_priv,
3161 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3162 orig_node->orig, client, BATADV_PRINT_VID(vid));
3164 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3166 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3167 tvlv_roam.vid = htons(vid);
3169 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3170 orig_node->orig, BATADV_TVLV_ROAM, 1,
3171 &tvlv_roam, sizeof(tvlv_roam));
3175 batadv_hardif_free_ref(primary_if);
3178 static void batadv_tt_purge(struct work_struct *work)
3180 struct delayed_work *delayed_work;
3181 struct batadv_priv_tt *priv_tt;
3182 struct batadv_priv *bat_priv;
3184 delayed_work = container_of(work, struct delayed_work, work);
3185 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3186 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3188 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3189 batadv_tt_global_purge(bat_priv);
3190 batadv_tt_req_purge(bat_priv);
3191 batadv_tt_roam_purge(bat_priv);
3193 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3194 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3197 void batadv_tt_free(struct batadv_priv *bat_priv)
3199 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3200 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3202 cancel_delayed_work_sync(&bat_priv->tt.work);
3204 batadv_tt_local_table_free(bat_priv);
3205 batadv_tt_global_table_free(bat_priv);
3206 batadv_tt_req_list_free(bat_priv);
3207 batadv_tt_changes_list_free(bat_priv);
3208 batadv_tt_roam_list_free(bat_priv);
3210 kfree(bat_priv->tt.last_changeset);
3214 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3215 * table and possibly count them in the TT size
3216 * @bat_priv: the bat priv with all the soft interface information
3217 * @flags: the flag to switch
3218 * @enable: whether to set or unset the flag
3219 * @count: whether to increase the TT size by the number of changed entries
3221 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3222 bool enable, bool count)
3224 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3225 struct batadv_tt_common_entry *tt_common_entry;
3226 u16 changed_num = 0;
3227 struct hlist_head *head;
3233 for (i = 0; i < hash->size; i++) {
3234 head = &hash->table[i];
3237 hlist_for_each_entry_rcu(tt_common_entry,
3240 if ((tt_common_entry->flags & flags) == flags)
3242 tt_common_entry->flags |= flags;
3244 if (!(tt_common_entry->flags & flags))
3246 tt_common_entry->flags &= ~flags;
3253 batadv_tt_local_size_inc(bat_priv,
3254 tt_common_entry->vid);
3260 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3261 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3263 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3264 struct batadv_tt_common_entry *tt_common;
3265 struct batadv_tt_local_entry *tt_local;
3266 struct batadv_softif_vlan *vlan;
3267 struct hlist_node *node_tmp;
3268 struct hlist_head *head;
3269 spinlock_t *list_lock; /* protects write access to the hash lists */
3275 for (i = 0; i < hash->size; i++) {
3276 head = &hash->table[i];
3277 list_lock = &hash->list_locks[i];
3279 spin_lock_bh(list_lock);
3280 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3282 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3285 batadv_dbg(BATADV_DBG_TT, bat_priv,
3286 "Deleting local tt entry (%pM, vid: %d): pending\n",
3288 BATADV_PRINT_VID(tt_common->vid));
3290 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3291 hlist_del_rcu(&tt_common->hash_entry);
3292 tt_local = container_of(tt_common,
3293 struct batadv_tt_local_entry,
3296 /* decrease the reference held for this vlan */
3297 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3299 batadv_softif_vlan_free_ref(vlan);
3300 batadv_softif_vlan_free_ref(vlan);
3303 batadv_tt_local_entry_free_ref(tt_local);
3305 spin_unlock_bh(list_lock);
3310 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3311 * which have been queued in the time since the last commit
3312 * @bat_priv: the bat priv with all the soft interface information
3314 * Caller must hold tt->commit_lock.
3316 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3318 lockdep_assert_held(&bat_priv->tt.commit_lock);
3320 /* Update multicast addresses in local translation table */
3321 batadv_mcast_mla_update(bat_priv);
3323 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3324 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3325 batadv_tt_tvlv_container_update(bat_priv);
3329 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3331 batadv_tt_local_purge_pending_clients(bat_priv);
3332 batadv_tt_local_update_crc(bat_priv);
3334 /* Increment the TTVN only once per OGM interval */
3335 atomic_inc(&bat_priv->tt.vn);
3336 batadv_dbg(BATADV_DBG_TT, bat_priv,
3337 "Local changes committed, updating to ttvn %u\n",
3338 (u8)atomic_read(&bat_priv->tt.vn));
3340 /* reset the sending counter */
3341 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3342 batadv_tt_tvlv_container_update(bat_priv);
3346 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3347 * have been queued in the time since the last commit
3348 * @bat_priv: the bat priv with all the soft interface information
3350 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3352 spin_lock_bh(&bat_priv->tt.commit_lock);
3353 batadv_tt_local_commit_changes_nolock(bat_priv);
3354 spin_unlock_bh(&bat_priv->tt.commit_lock);
3357 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3360 struct batadv_tt_local_entry *tt_local_entry = NULL;
3361 struct batadv_tt_global_entry *tt_global_entry = NULL;
3362 struct batadv_softif_vlan *vlan;
3365 vlan = batadv_softif_vlan_get(bat_priv, vid);
3369 if (!atomic_read(&vlan->ap_isolation))
3372 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3373 if (!tt_local_entry)
3376 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3377 if (!tt_global_entry)
3380 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3386 batadv_softif_vlan_free_ref(vlan);
3387 if (tt_global_entry)
3388 batadv_tt_global_entry_free_ref(tt_global_entry);
3390 batadv_tt_local_entry_free_ref(tt_local_entry);
3395 * batadv_tt_update_orig - update global translation table with new tt
3396 * information received via ogms
3397 * @bat_priv: the bat priv with all the soft interface information
3398 * @orig_node: the orig_node of the ogm
3399 * @tt_buff: pointer to the first tvlv VLAN entry
3400 * @tt_num_vlan: number of tvlv VLAN entries
3401 * @tt_change: pointer to the first entry in the TT buffer
3402 * @tt_num_changes: number of tt changes inside the tt buffer
3403 * @ttvn: translation table version number of this changeset
3405 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3406 struct batadv_orig_node *orig_node,
3407 const void *tt_buff, u16 tt_num_vlan,
3408 struct batadv_tvlv_tt_change *tt_change,
3409 u16 tt_num_changes, u8 ttvn)
3411 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3412 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3413 bool full_table = true;
3416 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3417 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3418 &orig_node->capa_initialized);
3420 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3421 * increased by one -> we can apply the attached changes
3423 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3424 /* the OGM could not contain the changes due to their size or
3425 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3427 * In this case send a tt request
3429 if (!tt_num_changes) {
3434 spin_lock_bh(&orig_node->tt_lock);
3436 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3439 /* Even if we received the precomputed crc with the OGM, we
3440 * prefer to recompute it to spot any possible inconsistency
3441 * in the global table
3443 batadv_tt_global_update_crc(bat_priv, orig_node);
3445 spin_unlock_bh(&orig_node->tt_lock);
3447 /* The ttvn alone is not enough to guarantee consistency
3448 * because a single value could represent different states
3449 * (due to the wrap around). Thus a node has to check whether
3450 * the resulting table (after applying the changes) is still
3451 * consistent or not. E.g. a node could disconnect while its
3452 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3453 * checking the CRC value is mandatory to detect the
3456 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3460 /* if we missed more than one change or our tables are not
3461 * in sync anymore -> request fresh tt data
3463 if (!has_tt_init || ttvn != orig_ttvn ||
3464 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3467 batadv_dbg(BATADV_DBG_TT, bat_priv,
3468 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3469 orig_node->orig, ttvn, orig_ttvn,
3471 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3472 tt_vlan, tt_num_vlan,
3480 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3481 * @bat_priv: the bat priv with all the soft interface information
3482 * @addr: the mac address of the client to check
3483 * @vid: VLAN identifier
3485 * Return: true if we know that the client has moved from its old originator
3486 * to another one. This entry is still kept for consistency purposes and will be
3487 * deleted later by a DEL or because of timeout
3489 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3490 u8 *addr, unsigned short vid)
3492 struct batadv_tt_global_entry *tt_global_entry;
3495 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3496 if (!tt_global_entry)
3499 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3500 batadv_tt_global_entry_free_ref(tt_global_entry);
3506 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3507 * @bat_priv: the bat priv with all the soft interface information
3508 * @addr: the mac address of the local client to query
3509 * @vid: VLAN identifier
3511 * Return: true if the local client is known to be roaming (it is not served by
3512 * this node anymore) or not. If yes, the client is still present in the table
3513 * to keep the latter consistent with the node TTVN
3515 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3516 u8 *addr, unsigned short vid)
3518 struct batadv_tt_local_entry *tt_local_entry;
3521 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3522 if (!tt_local_entry)
3525 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3526 batadv_tt_local_entry_free_ref(tt_local_entry);
3531 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3532 struct batadv_orig_node *orig_node,
3533 const unsigned char *addr,
3538 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3539 BATADV_TT_CLIENT_TEMP,
3540 atomic_read(&orig_node->last_ttvn)))
3543 batadv_dbg(BATADV_DBG_TT, bat_priv,
3544 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3545 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3552 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3553 * maximum packet size that can be transported through the mesh
3554 * @soft_iface: netdev struct of the mesh interface
3556 * Remove entries older than 'timeout' and half timeout if more entries need
3559 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3561 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3562 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3563 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3564 bool reduced = false;
3566 spin_lock_bh(&bat_priv->tt.commit_lock);
3569 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3570 if (packet_size_max >= table_size)
3573 batadv_tt_local_purge(bat_priv, timeout);
3574 batadv_tt_local_purge_pending_clients(bat_priv);
3578 net_ratelimited_function(batadv_info, soft_iface,
3579 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3583 /* commit these changes immediately, to avoid synchronization problem
3587 batadv_tt_local_commit_changes_nolock(bat_priv);
3589 spin_unlock_bh(&bat_priv->tt.commit_lock);
3593 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3594 * @bat_priv: the bat priv with all the soft interface information
3595 * @orig: the orig_node of the ogm
3596 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3597 * @tvlv_value: tvlv buffer containing the gateway data
3598 * @tvlv_value_len: tvlv buffer length
3600 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3601 struct batadv_orig_node *orig,
3602 u8 flags, void *tvlv_value,
3605 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3606 struct batadv_tvlv_tt_change *tt_change;
3607 struct batadv_tvlv_tt_data *tt_data;
3608 u16 num_entries, num_vlan;
3610 if (tvlv_value_len < sizeof(*tt_data))
3613 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3614 tvlv_value_len -= sizeof(*tt_data);
3616 num_vlan = ntohs(tt_data->num_vlan);
3618 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3621 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3622 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3623 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3625 num_entries = batadv_tt_entries(tvlv_value_len);
3627 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3628 num_entries, tt_data->ttvn);
3632 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3634 * @bat_priv: the bat priv with all the soft interface information
3635 * @src: mac address of tt tvlv sender
3636 * @dst: mac address of tt tvlv recipient
3637 * @tvlv_value: tvlv buffer containing the tt data
3638 * @tvlv_value_len: tvlv buffer length
3640 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3643 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3648 struct batadv_tvlv_tt_data *tt_data;
3649 u16 tt_vlan_len, tt_num_entries;
3653 if (tvlv_value_len < sizeof(*tt_data))
3654 return NET_RX_SUCCESS;
3656 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3657 tvlv_value_len -= sizeof(*tt_data);
3659 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3660 tt_vlan_len *= ntohs(tt_data->num_vlan);
3662 if (tvlv_value_len < tt_vlan_len)
3663 return NET_RX_SUCCESS;
3665 tvlv_value_len -= tt_vlan_len;
3666 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3668 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3669 case BATADV_TT_REQUEST:
3670 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3672 /* If this node cannot provide a TT response the tt_request is
3675 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3677 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3682 batadv_dbg(BATADV_DBG_TT, bat_priv,
3683 "Routing TT_REQUEST to %pM [%c]\n",
3685 /* tvlv API will re-route the packet */
3689 case BATADV_TT_RESPONSE:
3690 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3692 if (batadv_is_my_mac(bat_priv, dst)) {
3693 batadv_handle_tt_response(bat_priv, tt_data,
3694 src, tt_num_entries);
3695 return NET_RX_SUCCESS;
3698 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3703 batadv_dbg(BATADV_DBG_TT, bat_priv,
3704 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3706 /* tvlv API will re-route the packet */
3710 return NET_RX_SUCCESS;
3714 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3715 * @bat_priv: the bat priv with all the soft interface information
3716 * @src: mac address of tt tvlv sender
3717 * @dst: mac address of tt tvlv recipient
3718 * @tvlv_value: tvlv buffer containing the tt data
3719 * @tvlv_value_len: tvlv buffer length
3721 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3724 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3729 struct batadv_tvlv_roam_adv *roaming_adv;
3730 struct batadv_orig_node *orig_node = NULL;
3732 /* If this node is not the intended recipient of the
3733 * roaming advertisement the packet is forwarded
3734 * (the tvlv API will re-route the packet).
3736 if (!batadv_is_my_mac(bat_priv, dst))
3739 if (tvlv_value_len < sizeof(*roaming_adv))
3742 orig_node = batadv_orig_hash_find(bat_priv, src);
3746 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3747 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3749 batadv_dbg(BATADV_DBG_TT, bat_priv,
3750 "Received ROAMING_ADV from %pM (client %pM)\n",
3751 src, roaming_adv->client);
3753 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3754 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3755 atomic_read(&orig_node->last_ttvn) + 1);
3759 batadv_orig_node_free_ref(orig_node);
3760 return NET_RX_SUCCESS;
3764 * batadv_tt_init - initialise the translation table internals
3765 * @bat_priv: the bat priv with all the soft interface information
3767 * Return: 0 on success or negative error number in case of failure.
3769 int batadv_tt_init(struct batadv_priv *bat_priv)
3773 /* synchronized flags must be remote */
3774 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3776 ret = batadv_tt_local_init(bat_priv);
3780 ret = batadv_tt_global_init(bat_priv);
3784 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3785 batadv_tt_tvlv_unicast_handler_v1,
3786 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3788 batadv_tvlv_handler_register(bat_priv, NULL,
3789 batadv_roam_tvlv_unicast_handler_v1,
3790 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3792 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3793 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3794 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3800 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3801 * @bat_priv: the bat priv with all the soft interface information
3802 * @addr: the mac address of the client
3803 * @vid: the identifier of the VLAN where this client is connected
3805 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3808 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3809 const u8 *addr, unsigned short vid)
3811 struct batadv_tt_global_entry *tt;
3814 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3818 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3820 batadv_tt_global_entry_free_ref(tt);