1 /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "translation-table.h"
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/compiler.h>
26 #include <linux/crc32c.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/lockdep.h>
36 #include <linux/netdevice.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42 #include <linux/stddef.h>
43 #include <linux/string.h>
44 #include <linux/workqueue.h>
45 #include <net/net_namespace.h>
47 #include "bridge_loop_avoidance.h"
48 #include "hard-interface.h"
50 #include "multicast.h"
51 #include "originator.h"
53 #include "soft-interface.h"
56 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
57 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
59 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
61 struct batadv_orig_node *orig_node);
62 static void batadv_tt_purge(struct work_struct *work);
64 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
65 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
66 struct batadv_orig_node *orig_node,
67 const unsigned char *addr,
68 unsigned short vid, const char *message,
71 /* returns 1 if they are the same mac addr and vid */
72 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
74 const void *data1 = container_of(node, struct batadv_tt_common_entry,
76 const struct batadv_tt_common_entry *tt1 = data1;
77 const struct batadv_tt_common_entry *tt2 = data2;
79 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
83 * batadv_choose_tt - return the index of the tt entry in the hash table
84 * @data: pointer to the tt_common_entry object to map
85 * @size: the size of the hash table
87 * Returns the hash index where the object represented by 'data' should be
90 static inline u32 batadv_choose_tt(const void *data, u32 size)
92 struct batadv_tt_common_entry *tt;
95 tt = (struct batadv_tt_common_entry *)data;
96 hash = jhash(&tt->addr, ETH_ALEN, hash);
97 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
103 * batadv_tt_hash_find - look for a client in the given hash table
104 * @hash: the hash table to search
105 * @addr: the mac address of the client to look for
106 * @vid: VLAN identifier
108 * Returns a pointer to the tt_common struct belonging to the searched client if
109 * found, NULL otherwise.
111 static struct batadv_tt_common_entry *
112 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
115 struct hlist_head *head;
116 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
122 ether_addr_copy(to_search.addr, addr);
125 index = batadv_choose_tt(&to_search, hash->size);
126 head = &hash->table[index];
129 hlist_for_each_entry_rcu(tt, head, hash_entry) {
130 if (!batadv_compare_eth(tt, addr))
136 if (!atomic_inc_not_zero(&tt->refcount))
148 * batadv_tt_local_hash_find - search the local table for a given client
149 * @bat_priv: the bat priv with all the soft interface information
150 * @addr: the mac address of the client to look for
151 * @vid: VLAN identifier
153 * Returns a pointer to the corresponding tt_local_entry struct if the client is
154 * found, NULL otherwise.
156 static struct batadv_tt_local_entry *
157 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
160 struct batadv_tt_common_entry *tt_common_entry;
161 struct batadv_tt_local_entry *tt_local_entry = NULL;
163 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
166 tt_local_entry = container_of(tt_common_entry,
167 struct batadv_tt_local_entry,
169 return tt_local_entry;
173 * batadv_tt_global_hash_find - search the global table for a given client
174 * @bat_priv: the bat priv with all the soft interface information
175 * @addr: the mac address of the client to look for
176 * @vid: VLAN identifier
178 * Returns a pointer to the corresponding tt_global_entry struct if the client
179 * is found, NULL otherwise.
181 static struct batadv_tt_global_entry *
182 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
185 struct batadv_tt_common_entry *tt_common_entry;
186 struct batadv_tt_global_entry *tt_global_entry = NULL;
188 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
191 tt_global_entry = container_of(tt_common_entry,
192 struct batadv_tt_global_entry,
194 return tt_global_entry;
198 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
200 if (atomic_dec_and_test(&tt_local_entry->common.refcount)) {
201 batadv_softif_vlan_free_ref(tt_local_entry->vlan);
203 kfree_rcu(tt_local_entry, common.rcu);
208 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
209 * tt_global_entry and possibly free it
210 * @tt_global_entry: the object to free
213 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
215 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
216 batadv_tt_global_del_orig_list(tt_global_entry);
217 kfree_rcu(tt_global_entry, common.rcu);
222 * batadv_tt_global_hash_count - count the number of orig entries
223 * @hash: hash table containing the tt entries
224 * @addr: the mac address of the client to count entries for
225 * @vid: VLAN identifier
227 * Return the number of originators advertising the given address/data
228 * (excluding ourself).
230 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
231 const u8 *addr, unsigned short vid)
233 struct batadv_tt_global_entry *tt_global_entry;
236 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
237 if (!tt_global_entry)
240 count = atomic_read(&tt_global_entry->orig_list_count);
241 batadv_tt_global_entry_free_ref(tt_global_entry);
247 * batadv_tt_local_size_mod - change the size by v of the local table identified
249 * @bat_priv: the bat priv with all the soft interface information
250 * @vid: the VLAN identifier of the sub-table to change
251 * @v: the amount to sum to the local table size
253 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
254 unsigned short vid, int v)
256 struct batadv_softif_vlan *vlan;
258 vlan = batadv_softif_vlan_get(bat_priv, vid);
262 atomic_add(v, &vlan->tt.num_entries);
264 batadv_softif_vlan_free_ref(vlan);
268 * batadv_tt_local_size_inc - increase by one the local table size for the given
270 * @bat_priv: the bat priv with all the soft interface information
271 * @vid: the VLAN identifier
273 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
276 batadv_tt_local_size_mod(bat_priv, vid, 1);
280 * batadv_tt_local_size_dec - decrease by one the local table size for the given
282 * @bat_priv: the bat priv with all the soft interface information
283 * @vid: the VLAN identifier
285 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
288 batadv_tt_local_size_mod(bat_priv, vid, -1);
292 * batadv_tt_global_size_mod - change the size by v of the local table
294 * @bat_priv: the bat priv with all the soft interface information
295 * @vid: the VLAN identifier
296 * @v: the amount to sum to the global table size
298 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
299 unsigned short vid, int v)
301 struct batadv_orig_node_vlan *vlan;
303 vlan = batadv_orig_node_vlan_new(orig_node, vid);
307 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
308 spin_lock_bh(&orig_node->vlan_list_lock);
309 if (!hlist_unhashed(&vlan->list)) {
310 hlist_del_init_rcu(&vlan->list);
311 batadv_orig_node_vlan_free_ref(vlan);
313 spin_unlock_bh(&orig_node->vlan_list_lock);
316 batadv_orig_node_vlan_free_ref(vlan);
320 * batadv_tt_global_size_inc - increase by one the global table size for the
322 * @orig_node: the originator which global table size has to be decreased
323 * @vid: the vlan identifier
325 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
328 batadv_tt_global_size_mod(orig_node, vid, 1);
332 * batadv_tt_global_size_dec - decrease by one the global table size for the
334 * @orig_node: the originator which global table size has to be decreased
335 * @vid: the vlan identifier
337 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
340 batadv_tt_global_size_mod(orig_node, vid, -1);
344 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
345 * queue for free after rcu grace period
346 * @orig_entry: tt orig entry to be free'd
349 batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
351 batadv_orig_node_free_ref(orig_entry->orig_node);
352 kfree_rcu(orig_entry, rcu);
356 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
358 if (!atomic_dec_and_test(&orig_entry->refcount))
361 batadv_tt_orig_list_entry_release(orig_entry);
365 * batadv_tt_local_event - store a local TT event (ADD/DEL)
366 * @bat_priv: the bat priv with all the soft interface information
367 * @tt_local_entry: the TT entry involved in the event
368 * @event_flags: flags to store in the event structure
370 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
371 struct batadv_tt_local_entry *tt_local_entry,
374 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
375 struct batadv_tt_common_entry *common = &tt_local_entry->common;
376 u8 flags = common->flags | event_flags;
377 bool event_removed = false;
378 bool del_op_requested, del_op_entry;
380 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
384 tt_change_node->change.flags = flags;
385 memset(tt_change_node->change.reserved, 0,
386 sizeof(tt_change_node->change.reserved));
387 ether_addr_copy(tt_change_node->change.addr, common->addr);
388 tt_change_node->change.vid = htons(common->vid);
390 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
392 /* check for ADD+DEL or DEL+ADD events */
393 spin_lock_bh(&bat_priv->tt.changes_list_lock);
394 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
396 if (!batadv_compare_eth(entry->change.addr, common->addr))
399 /* DEL+ADD in the same orig interval have no effect and can be
400 * removed to avoid silly behaviour on the receiver side. The
401 * other way around (ADD+DEL) can happen in case of roaming of
402 * a client still in the NEW state. Roaming of NEW clients is
403 * now possible due to automatically recognition of "temporary"
406 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
407 if (!del_op_requested && del_op_entry)
409 if (del_op_requested && !del_op_entry)
412 /* this is a second add in the same originator interval. It
413 * means that flags have been changed: update them!
415 if (!del_op_requested && !del_op_entry)
416 entry->change.flags = flags;
420 list_del(&entry->list);
422 kfree(tt_change_node);
423 event_removed = true;
427 /* track the change in the OGMinterval list */
428 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
431 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
434 atomic_dec(&bat_priv->tt.local_changes);
436 atomic_inc(&bat_priv->tt.local_changes);
440 * batadv_tt_len - compute length in bytes of given number of tt changes
441 * @changes_num: number of tt changes
443 * Returns computed length in bytes.
445 static int batadv_tt_len(int changes_num)
447 return changes_num * sizeof(struct batadv_tvlv_tt_change);
451 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
452 * @tt_len: available space
454 * Returns the number of entries.
456 static u16 batadv_tt_entries(u16 tt_len)
458 return tt_len / batadv_tt_len(1);
462 * batadv_tt_local_table_transmit_size - calculates the local translation table
463 * size when transmitted over the air
464 * @bat_priv: the bat priv with all the soft interface information
466 * Returns local translation table size in bytes.
468 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
471 u16 tt_local_entries = 0;
472 struct batadv_softif_vlan *vlan;
476 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
478 tt_local_entries += atomic_read(&vlan->tt.num_entries);
482 /* header size of tvlv encapsulated tt response payload */
483 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
484 hdr_size += sizeof(struct batadv_tvlv_hdr);
485 hdr_size += sizeof(struct batadv_tvlv_tt_data);
486 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
488 return hdr_size + batadv_tt_len(tt_local_entries);
491 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
493 if (bat_priv->tt.local_hash)
496 bat_priv->tt.local_hash = batadv_hash_new(1024);
498 if (!bat_priv->tt.local_hash)
501 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
502 &batadv_tt_local_hash_lock_class_key);
507 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
508 struct batadv_tt_global_entry *tt_global,
511 struct batadv_tt_global_entry *tt_removed_entry;
512 struct hlist_node *tt_removed_node;
514 batadv_dbg(BATADV_DBG_TT, bat_priv,
515 "Deleting global tt entry %pM (vid: %d): %s\n",
516 tt_global->common.addr,
517 BATADV_PRINT_VID(tt_global->common.vid), message);
519 tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
523 if (!tt_removed_node)
526 /* drop reference of remove hash entry */
527 tt_removed_entry = hlist_entry(tt_removed_node,
528 struct batadv_tt_global_entry,
530 batadv_tt_global_entry_free_ref(tt_removed_entry);
534 * batadv_tt_local_add - add a new client to the local table or update an
536 * @soft_iface: netdev struct of the mesh interface
537 * @addr: the mac address of the client to add
538 * @vid: VLAN identifier
539 * @ifindex: index of the interface where the client is connected to (useful to
540 * identify wireless clients)
541 * @mark: the value contained in the skb->mark field of the received packet (if
544 * Returns true if the client was successfully added, false otherwise.
546 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
547 unsigned short vid, int ifindex, u32 mark)
549 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
550 struct batadv_tt_local_entry *tt_local;
551 struct batadv_tt_global_entry *tt_global = NULL;
552 struct batadv_softif_vlan *vlan;
553 struct net_device *in_dev = NULL;
554 struct hlist_head *head;
555 struct batadv_tt_orig_list_entry *orig_entry;
556 int hash_added, table_size, packet_size_max;
558 bool roamed_back = false;
562 if (ifindex != BATADV_NULL_IFINDEX)
563 in_dev = dev_get_by_index(&init_net, ifindex);
565 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
567 if (!is_multicast_ether_addr(addr))
568 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
571 tt_local->last_seen = jiffies;
572 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
573 batadv_dbg(BATADV_DBG_TT, bat_priv,
574 "Re-adding pending client %pM (vid: %d)\n",
575 addr, BATADV_PRINT_VID(vid));
576 /* whatever the reason why the PENDING flag was set,
577 * this is a client which was enqueued to be removed in
578 * this orig_interval. Since it popped up again, the
579 * flag can be reset like it was never enqueued
581 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
585 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
586 batadv_dbg(BATADV_DBG_TT, bat_priv,
587 "Roaming client %pM (vid: %d) came back to its original location\n",
588 addr, BATADV_PRINT_VID(vid));
589 /* the ROAM flag is set because this client roamed away
590 * and the node got a roaming_advertisement message. Now
591 * that the client popped up again at its original
592 * location such flag can be unset
594 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
600 /* Ignore the client if we cannot send it in a full table response. */
601 table_size = batadv_tt_local_table_transmit_size(bat_priv);
602 table_size += batadv_tt_len(1);
603 packet_size_max = atomic_read(&bat_priv->packet_size_max);
604 if (table_size > packet_size_max) {
605 net_ratelimited_function(batadv_info, soft_iface,
606 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
607 table_size, packet_size_max, addr);
611 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
615 /* increase the refcounter of the related vlan */
616 vlan = batadv_softif_vlan_get(bat_priv, vid);
618 net_ratelimited_function(batadv_info, soft_iface,
619 "adding TT local entry %pM to non-existent VLAN %d\n",
620 addr, BATADV_PRINT_VID(vid));
626 batadv_dbg(BATADV_DBG_TT, bat_priv,
627 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
628 addr, BATADV_PRINT_VID(vid),
629 (u8)atomic_read(&bat_priv->tt.vn));
631 ether_addr_copy(tt_local->common.addr, addr);
632 /* The local entry has to be marked as NEW to avoid to send it in
633 * a full table response going out before the next ttvn increment
634 * (consistency check)
636 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
637 tt_local->common.vid = vid;
638 if (batadv_is_wifi_netdev(in_dev))
639 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
640 atomic_set(&tt_local->common.refcount, 2);
641 tt_local->last_seen = jiffies;
642 tt_local->common.added_at = tt_local->last_seen;
644 /* the batman interface mac and multicast addresses should never be
647 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
648 is_multicast_ether_addr(addr))
649 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
651 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
652 batadv_choose_tt, &tt_local->common,
653 &tt_local->common.hash_entry);
655 if (unlikely(hash_added != 0)) {
656 /* remove the reference for the hash */
657 batadv_tt_local_entry_free_ref(tt_local);
662 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
665 /* Check whether it is a roaming, but don't do anything if the roaming
666 * process has already been handled
668 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
669 /* These node are probably going to update their tt table */
670 head = &tt_global->orig_list;
672 hlist_for_each_entry_rcu(orig_entry, head, list) {
673 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
674 tt_global->common.vid,
675 orig_entry->orig_node);
679 batadv_tt_global_free(bat_priv, tt_global,
683 /* The global entry has to be marked as ROAMING and
684 * has to be kept for consistency purpose
686 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
687 tt_global->roam_at = jiffies;
691 /* store the current remote flags before altering them. This helps
692 * understanding is flags are changing or not
694 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
696 if (batadv_is_wifi_netdev(in_dev))
697 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
699 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
701 /* check the mark in the skb: if it's equal to the configured
702 * isolation_mark, it means the packet is coming from an isolated
705 match_mark = (mark & bat_priv->isolation_mark_mask);
706 if (bat_priv->isolation_mark_mask &&
707 match_mark == bat_priv->isolation_mark)
708 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
710 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
712 /* if any "dynamic" flag has been modified, resend an ADD event for this
713 * entry so that all the nodes can get the new flags
715 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
716 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
723 batadv_tt_local_entry_free_ref(tt_local);
725 batadv_tt_global_entry_free_ref(tt_global);
730 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
731 * within a TT Response directed to another node
732 * @orig_node: originator for which the TT data has to be prepared
733 * @tt_data: uninitialised pointer to the address of the TVLV buffer
734 * @tt_change: uninitialised pointer to the address of the area where the TT
735 * changed can be stored
736 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
737 * function reserves the amount of space needed to send the entire global TT
738 * table. In case of success the value is updated with the real amount of
741 * Allocate the needed amount of memory for the entire TT TVLV and write its
742 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
743 * objects, one per active VLAN served by the originator node.
745 * Return the size of the allocated buffer or 0 in case of failure.
748 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
749 struct batadv_tvlv_tt_data **tt_data,
750 struct batadv_tvlv_tt_change **tt_change,
757 struct batadv_tvlv_tt_vlan_data *tt_vlan;
758 struct batadv_orig_node_vlan *vlan;
761 spin_lock_bh(&orig_node->vlan_list_lock);
762 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
764 num_entries += atomic_read(&vlan->tt.num_entries);
767 change_offset = sizeof(**tt_data);
768 change_offset += num_vlan * sizeof(*tt_vlan);
770 /* if tt_len is negative, allocate the space needed by the full table */
772 *tt_len = batadv_tt_len(num_entries);
775 tvlv_len += change_offset;
777 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
783 (*tt_data)->flags = BATADV_NO_FLAGS;
784 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
785 (*tt_data)->num_vlan = htons(num_vlan);
787 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
788 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
789 tt_vlan->vid = htons(vlan->vid);
790 tt_vlan->crc = htonl(vlan->tt.crc);
795 tt_change_ptr = (u8 *)*tt_data + change_offset;
796 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
799 spin_unlock_bh(&orig_node->vlan_list_lock);
804 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
806 * @bat_priv: the bat priv with all the soft interface information
807 * @tt_data: uninitialised pointer to the address of the TVLV buffer
808 * @tt_change: uninitialised pointer to the address of the area where the TT
809 * changes can be stored
810 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
811 * function reserves the amount of space needed to send the entire local TT
812 * table. In case of success the value is updated with the real amount of
815 * Allocate the needed amount of memory for the entire TT TVLV and write its
816 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
817 * objects, one per active VLAN.
819 * Return the size of the allocated buffer or 0 in case of failure.
822 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
823 struct batadv_tvlv_tt_data **tt_data,
824 struct batadv_tvlv_tt_change **tt_change,
827 struct batadv_tvlv_tt_vlan_data *tt_vlan;
828 struct batadv_softif_vlan *vlan;
830 u16 vlan_entries = 0;
831 u16 total_entries = 0;
836 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
837 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
838 vlan_entries = atomic_read(&vlan->tt.num_entries);
839 if (vlan_entries < 1)
843 total_entries += vlan_entries;
846 change_offset = sizeof(**tt_data);
847 change_offset += num_vlan * sizeof(*tt_vlan);
849 /* if tt_len is negative, allocate the space needed by the full table */
851 *tt_len = batadv_tt_len(total_entries);
854 tvlv_len += change_offset;
856 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
862 (*tt_data)->flags = BATADV_NO_FLAGS;
863 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
864 (*tt_data)->num_vlan = htons(num_vlan);
866 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
867 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
868 vlan_entries = atomic_read(&vlan->tt.num_entries);
869 if (vlan_entries < 1)
872 tt_vlan->vid = htons(vlan->vid);
873 tt_vlan->crc = htonl(vlan->tt.crc);
874 tt_vlan->reserved = 0;
879 tt_change_ptr = (u8 *)*tt_data + change_offset;
880 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
883 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
888 * batadv_tt_tvlv_container_update - update the translation table tvlv container
889 * after local tt changes have been committed
890 * @bat_priv: the bat priv with all the soft interface information
892 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
894 struct batadv_tt_change_node *entry, *safe;
895 struct batadv_tvlv_tt_data *tt_data;
896 struct batadv_tvlv_tt_change *tt_change;
897 int tt_diff_len, tt_change_len = 0;
898 int tt_diff_entries_num = 0;
899 int tt_diff_entries_count = 0;
902 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
903 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
905 /* if we have too many changes for one packet don't send any
906 * and wait for the tt table request which will be fragmented
908 if (tt_diff_len > bat_priv->soft_iface->mtu)
911 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
912 &tt_change, &tt_diff_len);
916 tt_data->flags = BATADV_TT_OGM_DIFF;
918 if (tt_diff_len == 0)
919 goto container_register;
921 spin_lock_bh(&bat_priv->tt.changes_list_lock);
922 atomic_set(&bat_priv->tt.local_changes, 0);
924 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
926 if (tt_diff_entries_count < tt_diff_entries_num) {
927 memcpy(tt_change + tt_diff_entries_count,
929 sizeof(struct batadv_tvlv_tt_change));
930 tt_diff_entries_count++;
932 list_del(&entry->list);
935 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
937 /* Keep the buffer for possible tt_request */
938 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
939 kfree(bat_priv->tt.last_changeset);
940 bat_priv->tt.last_changeset_len = 0;
941 bat_priv->tt.last_changeset = NULL;
942 tt_change_len = batadv_tt_len(tt_diff_entries_count);
943 /* check whether this new OGM has no changes due to size problems */
944 if (tt_diff_entries_count > 0) {
945 /* if kmalloc() fails we will reply with the full table
946 * instead of providing the diff
948 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
949 if (bat_priv->tt.last_changeset) {
950 memcpy(bat_priv->tt.last_changeset,
951 tt_change, tt_change_len);
952 bat_priv->tt.last_changeset_len = tt_diff_len;
955 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
958 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
963 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
965 struct net_device *net_dev = (struct net_device *)seq->private;
966 struct batadv_priv *bat_priv = netdev_priv(net_dev);
967 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
968 struct batadv_tt_common_entry *tt_common_entry;
969 struct batadv_tt_local_entry *tt_local;
970 struct batadv_hard_iface *primary_if;
971 struct hlist_head *head;
976 unsigned long last_seen_jiffies;
978 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
980 primary_if = batadv_seq_print_text_primary_if_get(seq);
985 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
986 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
987 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
988 "Flags", "Last seen", "CRC");
990 for (i = 0; i < hash->size; i++) {
991 head = &hash->table[i];
994 hlist_for_each_entry_rcu(tt_common_entry,
996 tt_local = container_of(tt_common_entry,
997 struct batadv_tt_local_entry,
999 vid = tt_common_entry->vid;
1000 last_seen_jiffies = jiffies - tt_local->last_seen;
1001 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1002 last_seen_secs = last_seen_msecs / 1000;
1003 last_seen_msecs = last_seen_msecs % 1000;
1005 no_purge = tt_common_entry->flags & np_flag;
1008 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1009 tt_common_entry->addr,
1010 BATADV_PRINT_VID(tt_common_entry->vid),
1011 ((tt_common_entry->flags &
1012 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1013 no_purge ? 'P' : '.',
1014 ((tt_common_entry->flags &
1015 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1016 ((tt_common_entry->flags &
1017 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1018 ((tt_common_entry->flags &
1019 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1020 ((tt_common_entry->flags &
1021 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1022 no_purge ? 0 : last_seen_secs,
1023 no_purge ? 0 : last_seen_msecs,
1024 tt_local->vlan->tt.crc);
1030 batadv_hardif_free_ref(primary_if);
1035 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1036 struct batadv_tt_local_entry *tt_local_entry,
1037 u16 flags, const char *message)
1039 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1041 /* The local client has to be marked as "pending to be removed" but has
1042 * to be kept in the table in order to send it in a full table
1043 * response issued before the net ttvn increment (consistency check)
1045 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1047 batadv_dbg(BATADV_DBG_TT, bat_priv,
1048 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1049 tt_local_entry->common.addr,
1050 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1054 * batadv_tt_local_remove - logically remove an entry from the local table
1055 * @bat_priv: the bat priv with all the soft interface information
1056 * @addr: the MAC address of the client to remove
1057 * @vid: VLAN identifier
1058 * @message: message to append to the log on deletion
1059 * @roaming: true if the deletion is due to a roaming event
1061 * Returns the flags assigned to the local entry before being deleted
1063 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1064 unsigned short vid, const char *message,
1067 struct batadv_tt_local_entry *tt_removed_entry;
1068 struct batadv_tt_local_entry *tt_local_entry;
1069 u16 flags, curr_flags = BATADV_NO_FLAGS;
1070 struct hlist_node *tt_removed_node;
1072 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1073 if (!tt_local_entry)
1076 curr_flags = tt_local_entry->common.flags;
1078 flags = BATADV_TT_CLIENT_DEL;
1079 /* if this global entry addition is due to a roaming, the node has to
1080 * mark the local entry as "roamed" in order to correctly reroute
1084 flags |= BATADV_TT_CLIENT_ROAM;
1085 /* mark the local client as ROAMed */
1086 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1089 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1090 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1094 /* if this client has been added right now, it is possible to
1095 * immediately purge it
1097 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1099 tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1102 &tt_local_entry->common);
1103 if (!tt_removed_node)
1106 /* drop reference of remove hash entry */
1107 tt_removed_entry = hlist_entry(tt_removed_node,
1108 struct batadv_tt_local_entry,
1110 batadv_tt_local_entry_free_ref(tt_removed_entry);
1114 batadv_tt_local_entry_free_ref(tt_local_entry);
1120 * batadv_tt_local_purge_list - purge inactive tt local entries
1121 * @bat_priv: the bat priv with all the soft interface information
1122 * @head: pointer to the list containing the local tt entries
1123 * @timeout: parameter deciding whether a given tt local entry is considered
1126 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1127 struct hlist_head *head,
1130 struct batadv_tt_local_entry *tt_local_entry;
1131 struct batadv_tt_common_entry *tt_common_entry;
1132 struct hlist_node *node_tmp;
1134 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1136 tt_local_entry = container_of(tt_common_entry,
1137 struct batadv_tt_local_entry,
1139 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1142 /* entry already marked for deletion */
1143 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1146 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1149 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1150 BATADV_TT_CLIENT_DEL, "timed out");
1155 * batadv_tt_local_purge - purge inactive tt local entries
1156 * @bat_priv: the bat priv with all the soft interface information
1157 * @timeout: parameter deciding whether a given tt local entry is considered
1160 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1163 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1164 struct hlist_head *head;
1165 spinlock_t *list_lock; /* protects write access to the hash lists */
1168 for (i = 0; i < hash->size; i++) {
1169 head = &hash->table[i];
1170 list_lock = &hash->list_locks[i];
1172 spin_lock_bh(list_lock);
1173 batadv_tt_local_purge_list(bat_priv, head, timeout);
1174 spin_unlock_bh(list_lock);
1178 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1180 struct batadv_hashtable *hash;
1181 spinlock_t *list_lock; /* protects write access to the hash lists */
1182 struct batadv_tt_common_entry *tt_common_entry;
1183 struct batadv_tt_local_entry *tt_local;
1184 struct hlist_node *node_tmp;
1185 struct hlist_head *head;
1188 if (!bat_priv->tt.local_hash)
1191 hash = bat_priv->tt.local_hash;
1193 for (i = 0; i < hash->size; i++) {
1194 head = &hash->table[i];
1195 list_lock = &hash->list_locks[i];
1197 spin_lock_bh(list_lock);
1198 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1200 hlist_del_rcu(&tt_common_entry->hash_entry);
1201 tt_local = container_of(tt_common_entry,
1202 struct batadv_tt_local_entry,
1205 batadv_tt_local_entry_free_ref(tt_local);
1207 spin_unlock_bh(list_lock);
1210 batadv_hash_destroy(hash);
1212 bat_priv->tt.local_hash = NULL;
1215 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1217 if (bat_priv->tt.global_hash)
1220 bat_priv->tt.global_hash = batadv_hash_new(1024);
1222 if (!bat_priv->tt.global_hash)
1225 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1226 &batadv_tt_global_hash_lock_class_key);
1231 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1233 struct batadv_tt_change_node *entry, *safe;
1235 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1237 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1239 list_del(&entry->list);
1243 atomic_set(&bat_priv->tt.local_changes, 0);
1244 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1247 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1248 * batadv_tt_global_entry list
1250 * returns it with an increased refcounter, NULL if not found
1252 static struct batadv_tt_orig_list_entry *
1253 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1254 const struct batadv_orig_node *orig_node)
1256 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1257 const struct hlist_head *head;
1260 head = &entry->orig_list;
1261 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1262 if (tmp_orig_entry->orig_node != orig_node)
1264 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1267 orig_entry = tmp_orig_entry;
1275 /* find out if an orig_node is already in the list of a tt_global_entry.
1276 * returns true if found, false otherwise
1279 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1280 const struct batadv_orig_node *orig_node,
1283 struct batadv_tt_orig_list_entry *orig_entry;
1286 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1291 *flags = orig_entry->flags;
1293 batadv_tt_orig_list_entry_free_ref(orig_entry);
1300 * batadv_tt_global_sync_flags - update TT sync flags
1301 * @tt_global: the TT global entry to update sync flags in
1303 * Updates the sync flag bits in the tt_global flag attribute with a logical
1304 * OR of all sync flags from any of its TT orig entries.
1307 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1309 struct batadv_tt_orig_list_entry *orig_entry;
1310 const struct hlist_head *head;
1311 u16 flags = BATADV_NO_FLAGS;
1314 head = &tt_global->orig_list;
1315 hlist_for_each_entry_rcu(orig_entry, head, list)
1316 flags |= orig_entry->flags;
1319 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1320 tt_global->common.flags = flags;
1324 * batadv_tt_global_orig_entry_add - add or update a TT orig entry
1325 * @tt_global: the TT global entry to add an orig entry in
1326 * @orig_node: the originator to add an orig entry for
1327 * @ttvn: translation table version number of this changeset
1328 * @flags: TT sync flags
1331 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1332 struct batadv_orig_node *orig_node, int ttvn,
1335 struct batadv_tt_orig_list_entry *orig_entry;
1337 spin_lock_bh(&tt_global->list_lock);
1339 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1341 /* refresh the ttvn: the current value could be a bogus one that
1342 * was added during a "temporary client detection"
1344 orig_entry->ttvn = ttvn;
1345 orig_entry->flags = flags;
1349 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1353 INIT_HLIST_NODE(&orig_entry->list);
1354 atomic_inc(&orig_node->refcount);
1355 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1356 orig_entry->orig_node = orig_node;
1357 orig_entry->ttvn = ttvn;
1358 orig_entry->flags = flags;
1359 atomic_set(&orig_entry->refcount, 2);
1361 hlist_add_head_rcu(&orig_entry->list,
1362 &tt_global->orig_list);
1363 atomic_inc(&tt_global->orig_list_count);
1366 batadv_tt_global_sync_flags(tt_global);
1369 batadv_tt_orig_list_entry_free_ref(orig_entry);
1371 spin_unlock_bh(&tt_global->list_lock);
1375 * batadv_tt_global_add - add a new TT global entry or update an existing one
1376 * @bat_priv: the bat priv with all the soft interface information
1377 * @orig_node: the originator announcing the client
1378 * @tt_addr: the mac address of the non-mesh client
1379 * @vid: VLAN identifier
1380 * @flags: TT flags that have to be set for this non-mesh client
1381 * @ttvn: the tt version number ever announcing this non-mesh client
1383 * Add a new TT global entry for the given originator. If the entry already
1384 * exists add a new reference to the given originator (a global entry can have
1385 * references to multiple originators) and adjust the flags attribute to reflect
1386 * the function argument.
1387 * If a TT local entry exists for this non-mesh client remove it.
1389 * The caller must hold orig_node refcount.
1391 * Return true if the new entry has been added, false otherwise
1393 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1394 struct batadv_orig_node *orig_node,
1395 const unsigned char *tt_addr,
1396 unsigned short vid, u16 flags, u8 ttvn)
1398 struct batadv_tt_global_entry *tt_global_entry;
1399 struct batadv_tt_local_entry *tt_local_entry;
1402 struct batadv_tt_common_entry *common;
1405 /* ignore global entries from backbone nodes */
1406 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1409 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1410 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1412 /* if the node already has a local client for this entry, it has to wait
1413 * for a roaming advertisement instead of manually messing up the global
1416 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1417 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1420 if (!tt_global_entry) {
1421 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1422 if (!tt_global_entry)
1425 common = &tt_global_entry->common;
1426 ether_addr_copy(common->addr, tt_addr);
1429 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1431 tt_global_entry->roam_at = 0;
1432 /* node must store current time in case of roaming. This is
1433 * needed to purge this entry out on timeout (if nobody claims
1436 if (flags & BATADV_TT_CLIENT_ROAM)
1437 tt_global_entry->roam_at = jiffies;
1438 atomic_set(&common->refcount, 2);
1439 common->added_at = jiffies;
1441 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1442 atomic_set(&tt_global_entry->orig_list_count, 0);
1443 spin_lock_init(&tt_global_entry->list_lock);
1445 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1447 batadv_choose_tt, common,
1448 &common->hash_entry);
1450 if (unlikely(hash_added != 0)) {
1451 /* remove the reference for the hash */
1452 batadv_tt_global_entry_free_ref(tt_global_entry);
1456 common = &tt_global_entry->common;
1457 /* If there is already a global entry, we can use this one for
1459 * But if we are trying to add a temporary client then here are
1460 * two options at this point:
1461 * 1) the global client is not a temporary client: the global
1462 * client has to be left as it is, temporary information
1463 * should never override any already known client state
1464 * 2) the global client is a temporary client: purge the
1465 * originator list and add the new one orig_entry
1467 if (flags & BATADV_TT_CLIENT_TEMP) {
1468 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1470 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1473 batadv_tt_global_del_orig_list(tt_global_entry);
1474 goto add_orig_entry;
1477 /* if the client was temporary added before receiving the first
1478 * OGM announcing it, we have to clear the TEMP flag. Also,
1479 * remove the previous temporary orig node and re-add it
1480 * if required. If the orig entry changed, the new one which
1481 * is a non-temporary entry is preferred.
1483 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1484 batadv_tt_global_del_orig_list(tt_global_entry);
1485 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1488 /* the change can carry possible "attribute" flags like the
1489 * TT_CLIENT_WIFI, therefore they have to be copied in the
1492 tt_global_entry->common.flags |= flags & (~BATADV_TT_SYNC_MASK);
1494 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1495 * one originator left in the list and we previously received a
1496 * delete + roaming change for this originator.
1498 * We should first delete the old originator before adding the
1501 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1502 batadv_tt_global_del_orig_list(tt_global_entry);
1503 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1504 tt_global_entry->roam_at = 0;
1508 /* add the new orig_entry (if needed) or update it */
1509 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1510 flags & BATADV_TT_SYNC_MASK);
1512 batadv_dbg(BATADV_DBG_TT, bat_priv,
1513 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1514 common->addr, BATADV_PRINT_VID(common->vid),
1519 /* Do not remove multicast addresses from the local hash on
1522 if (is_multicast_ether_addr(tt_addr))
1525 /* remove address from local hash if present */
1526 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1527 "global tt received",
1528 flags & BATADV_TT_CLIENT_ROAM);
1529 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1531 if (!(flags & BATADV_TT_CLIENT_ROAM))
1532 /* this is a normal global add. Therefore the client is not in a
1533 * roaming state anymore.
1535 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1538 if (tt_global_entry)
1539 batadv_tt_global_entry_free_ref(tt_global_entry);
1541 batadv_tt_local_entry_free_ref(tt_local_entry);
1546 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1547 * @bat_priv: the bat priv with all the soft interface information
1548 * @tt_global_entry: global translation table entry to be analyzed
1550 * This functon assumes the caller holds rcu_read_lock().
1551 * Returns best originator list entry or NULL on errors.
1553 static struct batadv_tt_orig_list_entry *
1554 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1555 struct batadv_tt_global_entry *tt_global_entry)
1557 struct batadv_neigh_node *router, *best_router = NULL;
1558 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1559 struct hlist_head *head;
1560 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1562 head = &tt_global_entry->orig_list;
1563 hlist_for_each_entry_rcu(orig_entry, head, list) {
1564 router = batadv_orig_router_get(orig_entry->orig_node,
1570 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1571 best_router, BATADV_IF_DEFAULT) <= 0) {
1572 batadv_neigh_node_free_ref(router);
1576 /* release the refcount for the "old" best */
1578 batadv_neigh_node_free_ref(best_router);
1580 best_entry = orig_entry;
1581 best_router = router;
1585 batadv_neigh_node_free_ref(best_router);
1591 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1592 * for this global entry
1593 * @bat_priv: the bat priv with all the soft interface information
1594 * @tt_global_entry: global translation table entry to be printed
1595 * @seq: debugfs table seq_file struct
1597 * This functon assumes the caller holds rcu_read_lock().
1600 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1601 struct batadv_tt_global_entry *tt_global_entry,
1602 struct seq_file *seq)
1604 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1605 struct batadv_tt_common_entry *tt_common_entry;
1606 struct batadv_orig_node_vlan *vlan;
1607 struct hlist_head *head;
1611 tt_common_entry = &tt_global_entry->common;
1612 flags = tt_common_entry->flags;
1614 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1616 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1617 tt_common_entry->vid);
1620 " * Cannot retrieve VLAN %d for originator %pM\n",
1621 BATADV_PRINT_VID(tt_common_entry->vid),
1622 best_entry->orig_node->orig);
1626 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1628 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1629 '*', tt_global_entry->common.addr,
1630 BATADV_PRINT_VID(tt_global_entry->common.vid),
1631 best_entry->ttvn, best_entry->orig_node->orig,
1632 last_ttvn, vlan->tt.crc,
1633 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1634 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1635 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1636 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1638 batadv_orig_node_vlan_free_ref(vlan);
1642 head = &tt_global_entry->orig_list;
1644 hlist_for_each_entry_rcu(orig_entry, head, list) {
1645 if (best_entry == orig_entry)
1648 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1649 tt_common_entry->vid);
1652 " + Cannot retrieve VLAN %d for originator %pM\n",
1653 BATADV_PRINT_VID(tt_common_entry->vid),
1654 orig_entry->orig_node->orig);
1658 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1660 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1661 '+', tt_global_entry->common.addr,
1662 BATADV_PRINT_VID(tt_global_entry->common.vid),
1663 orig_entry->ttvn, orig_entry->orig_node->orig,
1664 last_ttvn, vlan->tt.crc,
1665 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1666 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1667 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1668 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1670 batadv_orig_node_vlan_free_ref(vlan);
1674 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1676 struct net_device *net_dev = (struct net_device *)seq->private;
1677 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1678 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1679 struct batadv_tt_common_entry *tt_common_entry;
1680 struct batadv_tt_global_entry *tt_global;
1681 struct batadv_hard_iface *primary_if;
1682 struct hlist_head *head;
1685 primary_if = batadv_seq_print_text_primary_if_get(seq);
1690 "Globally announced TT entries received via the mesh %s\n",
1692 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1693 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1696 for (i = 0; i < hash->size; i++) {
1697 head = &hash->table[i];
1700 hlist_for_each_entry_rcu(tt_common_entry,
1702 tt_global = container_of(tt_common_entry,
1703 struct batadv_tt_global_entry,
1705 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1711 batadv_hardif_free_ref(primary_if);
1716 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1717 * @tt_global_entry: the global entry to remove the orig_entry from
1718 * @orig_entry: the orig entry to remove and free
1720 * Remove an orig_entry from its list in the given tt_global_entry and
1721 * free this orig_entry afterwards.
1723 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1727 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1728 struct batadv_tt_orig_list_entry *orig_entry)
1730 lockdep_assert_held(&tt_global_entry->list_lock);
1732 batadv_tt_global_size_dec(orig_entry->orig_node,
1733 tt_global_entry->common.vid);
1734 atomic_dec(&tt_global_entry->orig_list_count);
1735 /* requires holding tt_global_entry->list_lock and orig_entry->list
1736 * being part of a list
1738 hlist_del_rcu(&orig_entry->list);
1739 batadv_tt_orig_list_entry_free_ref(orig_entry);
1742 /* deletes the orig list of a tt_global_entry */
1744 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1746 struct hlist_head *head;
1747 struct hlist_node *safe;
1748 struct batadv_tt_orig_list_entry *orig_entry;
1750 spin_lock_bh(&tt_global_entry->list_lock);
1751 head = &tt_global_entry->orig_list;
1752 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1753 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1754 spin_unlock_bh(&tt_global_entry->list_lock);
1758 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1759 * @bat_priv: the bat priv with all the soft interface information
1760 * @tt_global_entry: the global entry to remove the orig_node from
1761 * @orig_node: the originator announcing the client
1762 * @message: message to append to the log on deletion
1764 * Remove the given orig_node and its according orig_entry from the given
1768 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1769 struct batadv_tt_global_entry *tt_global_entry,
1770 struct batadv_orig_node *orig_node,
1771 const char *message)
1773 struct hlist_head *head;
1774 struct hlist_node *safe;
1775 struct batadv_tt_orig_list_entry *orig_entry;
1778 spin_lock_bh(&tt_global_entry->list_lock);
1779 head = &tt_global_entry->orig_list;
1780 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1781 if (orig_entry->orig_node == orig_node) {
1782 vid = tt_global_entry->common.vid;
1783 batadv_dbg(BATADV_DBG_TT, bat_priv,
1784 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1786 tt_global_entry->common.addr,
1787 BATADV_PRINT_VID(vid), message);
1788 _batadv_tt_global_del_orig_entry(tt_global_entry,
1792 spin_unlock_bh(&tt_global_entry->list_lock);
1795 /* If the client is to be deleted, we check if it is the last origantor entry
1796 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1797 * timer, otherwise we simply remove the originator scheduled for deletion.
1800 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1801 struct batadv_tt_global_entry *tt_global_entry,
1802 struct batadv_orig_node *orig_node,
1803 const char *message)
1805 bool last_entry = true;
1806 struct hlist_head *head;
1807 struct batadv_tt_orig_list_entry *orig_entry;
1809 /* no local entry exists, case 1:
1810 * Check if this is the last one or if other entries exist.
1814 head = &tt_global_entry->orig_list;
1815 hlist_for_each_entry_rcu(orig_entry, head, list) {
1816 if (orig_entry->orig_node != orig_node) {
1824 /* its the last one, mark for roaming. */
1825 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1826 tt_global_entry->roam_at = jiffies;
1828 /* there is another entry, we can simply delete this
1829 * one and can still use the other one.
1831 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1832 orig_node, message);
1836 * batadv_tt_global_del - remove a client from the global table
1837 * @bat_priv: the bat priv with all the soft interface information
1838 * @orig_node: an originator serving this client
1839 * @addr: the mac address of the client
1840 * @vid: VLAN identifier
1841 * @message: a message explaining the reason for deleting the client to print
1842 * for debugging purpose
1843 * @roaming: true if the deletion has been triggered by a roaming event
1845 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1846 struct batadv_orig_node *orig_node,
1847 const unsigned char *addr, unsigned short vid,
1848 const char *message, bool roaming)
1850 struct batadv_tt_global_entry *tt_global_entry;
1851 struct batadv_tt_local_entry *local_entry = NULL;
1853 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1854 if (!tt_global_entry)
1858 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1859 orig_node, message);
1861 if (hlist_empty(&tt_global_entry->orig_list))
1862 batadv_tt_global_free(bat_priv, tt_global_entry,
1868 /* if we are deleting a global entry due to a roam
1869 * event, there are two possibilities:
1870 * 1) the client roamed from node A to node B => if there
1871 * is only one originator left for this client, we mark
1872 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1873 * wait for node B to claim it. In case of timeout
1874 * the entry is purged.
1876 * If there are other originators left, we directly delete
1878 * 2) the client roamed to us => we can directly delete
1879 * the global entry, since it is useless now.
1881 local_entry = batadv_tt_local_hash_find(bat_priv,
1882 tt_global_entry->common.addr,
1885 /* local entry exists, case 2: client roamed to us. */
1886 batadv_tt_global_del_orig_list(tt_global_entry);
1887 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1889 /* no local entry exists, case 1: check for roaming */
1890 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1891 orig_node, message);
1894 if (tt_global_entry)
1895 batadv_tt_global_entry_free_ref(tt_global_entry);
1897 batadv_tt_local_entry_free_ref(local_entry);
1901 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1902 * given originator matching the provided vid
1903 * @bat_priv: the bat priv with all the soft interface information
1904 * @orig_node: the originator owning the entries to remove
1905 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1907 * @message: debug message to print as "reason"
1909 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1910 struct batadv_orig_node *orig_node,
1912 const char *message)
1914 struct batadv_tt_global_entry *tt_global;
1915 struct batadv_tt_common_entry *tt_common_entry;
1917 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1918 struct hlist_node *safe;
1919 struct hlist_head *head;
1920 spinlock_t *list_lock; /* protects write access to the hash lists */
1926 for (i = 0; i < hash->size; i++) {
1927 head = &hash->table[i];
1928 list_lock = &hash->list_locks[i];
1930 spin_lock_bh(list_lock);
1931 hlist_for_each_entry_safe(tt_common_entry, safe,
1933 /* remove only matching entries */
1934 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1937 tt_global = container_of(tt_common_entry,
1938 struct batadv_tt_global_entry,
1941 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1942 orig_node, message);
1944 if (hlist_empty(&tt_global->orig_list)) {
1945 vid = tt_global->common.vid;
1946 batadv_dbg(BATADV_DBG_TT, bat_priv,
1947 "Deleting global tt entry %pM (vid: %d): %s\n",
1948 tt_global->common.addr,
1949 BATADV_PRINT_VID(vid), message);
1950 hlist_del_rcu(&tt_common_entry->hash_entry);
1951 batadv_tt_global_entry_free_ref(tt_global);
1954 spin_unlock_bh(list_lock);
1956 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1959 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1963 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1964 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1966 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1967 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1969 *msg = "Roaming timeout\n";
1972 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1973 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1975 *msg = "Temporary client timeout\n";
1981 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1983 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1984 struct hlist_head *head;
1985 struct hlist_node *node_tmp;
1986 spinlock_t *list_lock; /* protects write access to the hash lists */
1989 struct batadv_tt_common_entry *tt_common;
1990 struct batadv_tt_global_entry *tt_global;
1992 for (i = 0; i < hash->size; i++) {
1993 head = &hash->table[i];
1994 list_lock = &hash->list_locks[i];
1996 spin_lock_bh(list_lock);
1997 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1999 tt_global = container_of(tt_common,
2000 struct batadv_tt_global_entry,
2003 if (!batadv_tt_global_to_purge(tt_global, &msg))
2006 batadv_dbg(BATADV_DBG_TT, bat_priv,
2007 "Deleting global tt entry %pM (vid: %d): %s\n",
2008 tt_global->common.addr,
2009 BATADV_PRINT_VID(tt_global->common.vid),
2012 hlist_del_rcu(&tt_common->hash_entry);
2014 batadv_tt_global_entry_free_ref(tt_global);
2016 spin_unlock_bh(list_lock);
2020 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2022 struct batadv_hashtable *hash;
2023 spinlock_t *list_lock; /* protects write access to the hash lists */
2024 struct batadv_tt_common_entry *tt_common_entry;
2025 struct batadv_tt_global_entry *tt_global;
2026 struct hlist_node *node_tmp;
2027 struct hlist_head *head;
2030 if (!bat_priv->tt.global_hash)
2033 hash = bat_priv->tt.global_hash;
2035 for (i = 0; i < hash->size; i++) {
2036 head = &hash->table[i];
2037 list_lock = &hash->list_locks[i];
2039 spin_lock_bh(list_lock);
2040 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2042 hlist_del_rcu(&tt_common_entry->hash_entry);
2043 tt_global = container_of(tt_common_entry,
2044 struct batadv_tt_global_entry,
2046 batadv_tt_global_entry_free_ref(tt_global);
2048 spin_unlock_bh(list_lock);
2051 batadv_hash_destroy(hash);
2053 bat_priv->tt.global_hash = NULL;
2057 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2058 struct batadv_tt_global_entry *tt_global_entry)
2062 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2063 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2066 /* check if the two clients are marked as isolated */
2067 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2068 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2075 * batadv_transtable_search - get the mesh destination for a given client
2076 * @bat_priv: the bat priv with all the soft interface information
2077 * @src: mac address of the source client
2078 * @addr: mac address of the destination client
2079 * @vid: VLAN identifier
2081 * Returns a pointer to the originator that was selected as destination in the
2082 * mesh for contacting the client 'addr', NULL otherwise.
2083 * In case of multiple originators serving the same client, the function returns
2084 * the best one (best in terms of metric towards the destination node).
2086 * If the two clients are AP isolated the function returns NULL.
2088 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2093 struct batadv_tt_local_entry *tt_local_entry = NULL;
2094 struct batadv_tt_global_entry *tt_global_entry = NULL;
2095 struct batadv_orig_node *orig_node = NULL;
2096 struct batadv_tt_orig_list_entry *best_entry;
2098 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2099 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2100 if (!tt_local_entry ||
2101 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2105 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2106 if (!tt_global_entry)
2109 /* check whether the clients should not communicate due to AP
2112 if (tt_local_entry &&
2113 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2117 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2118 /* found anything? */
2120 orig_node = best_entry->orig_node;
2121 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2126 if (tt_global_entry)
2127 batadv_tt_global_entry_free_ref(tt_global_entry);
2129 batadv_tt_local_entry_free_ref(tt_local_entry);
2135 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2136 * to the given orig_node
2137 * @bat_priv: the bat priv with all the soft interface information
2138 * @orig_node: originator for which the CRC should be computed
2139 * @vid: VLAN identifier for which the CRC32 has to be computed
2141 * This function computes the checksum for the global table corresponding to a
2142 * specific originator. In particular, the checksum is computed as follows: For
2143 * each client connected to the originator the CRC32C of the MAC address and the
2144 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2147 * The idea behind is that CRC32C should be used as much as possible in order to
2148 * produce a unique hash of the table, but since the order which is used to feed
2149 * the CRC32C function affects the result and since every node in the network
2150 * probably sorts the clients differently, the hash function cannot be directly
2151 * computed over the entire table. Hence the CRC32C is used only on
2152 * the single client entry, while all the results are then xor'ed together
2153 * because the XOR operation can combine them all while trying to reduce the
2154 * noise as much as possible.
2156 * Returns the checksum of the global table of a given originator.
2158 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2159 struct batadv_orig_node *orig_node,
2162 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2163 struct batadv_tt_orig_list_entry *tt_orig;
2164 struct batadv_tt_common_entry *tt_common;
2165 struct batadv_tt_global_entry *tt_global;
2166 struct hlist_head *head;
2167 u32 i, crc_tmp, crc = 0;
2171 for (i = 0; i < hash->size; i++) {
2172 head = &hash->table[i];
2175 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2176 tt_global = container_of(tt_common,
2177 struct batadv_tt_global_entry,
2179 /* compute the CRC only for entries belonging to the
2180 * VLAN identified by the vid passed as parameter
2182 if (tt_common->vid != vid)
2185 /* Roaming clients are in the global table for
2186 * consistency only. They don't have to be
2187 * taken into account while computing the
2190 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2192 /* Temporary clients have not been announced yet, so
2193 * they have to be skipped while computing the global
2196 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2199 /* find out if this global entry is announced by this
2202 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2207 /* use network order to read the VID: this ensures that
2208 * every node reads the bytes in the same order.
2210 tmp_vid = htons(tt_common->vid);
2211 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2213 /* compute the CRC on flags that have to be kept in sync
2216 flags = tt_orig->flags;
2217 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2219 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2221 batadv_tt_orig_list_entry_free_ref(tt_orig);
2230 * batadv_tt_local_crc - calculates the checksum of the local table
2231 * @bat_priv: the bat priv with all the soft interface information
2232 * @vid: VLAN identifier for which the CRC32 has to be computed
2234 * For details about the computation, please refer to the documentation for
2235 * batadv_tt_global_crc().
2237 * Returns the checksum of the local table
2239 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2242 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2243 struct batadv_tt_common_entry *tt_common;
2244 struct hlist_head *head;
2245 u32 i, crc_tmp, crc = 0;
2249 for (i = 0; i < hash->size; i++) {
2250 head = &hash->table[i];
2253 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2254 /* compute the CRC only for entries belonging to the
2255 * VLAN identified by vid
2257 if (tt_common->vid != vid)
2260 /* not yet committed clients have not to be taken into
2261 * account while computing the CRC
2263 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2266 /* use network order to read the VID: this ensures that
2267 * every node reads the bytes in the same order.
2269 tmp_vid = htons(tt_common->vid);
2270 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2272 /* compute the CRC on flags that have to be kept in sync
2275 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2276 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2278 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2287 * batadv_tt_req_node_release - free tt_req node entry
2288 * @ref: kref pointer of the tt req_node entry
2290 static void batadv_tt_req_node_release(struct kref *ref)
2292 struct batadv_tt_req_node *tt_req_node;
2294 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2300 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2301 * possibly release it
2302 * @tt_req_node: tt_req_node to be free'd
2304 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2306 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2309 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2311 struct batadv_tt_req_node *node;
2312 struct hlist_node *safe;
2314 spin_lock_bh(&bat_priv->tt.req_list_lock);
2316 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2317 hlist_del_init(&node->list);
2318 batadv_tt_req_node_put(node);
2321 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2324 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2325 struct batadv_orig_node *orig_node,
2326 const void *tt_buff,
2329 /* Replace the old buffer only if I received something in the
2330 * last OGM (the OGM could carry no changes)
2332 spin_lock_bh(&orig_node->tt_buff_lock);
2333 if (tt_buff_len > 0) {
2334 kfree(orig_node->tt_buff);
2335 orig_node->tt_buff_len = 0;
2336 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2337 if (orig_node->tt_buff) {
2338 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2339 orig_node->tt_buff_len = tt_buff_len;
2342 spin_unlock_bh(&orig_node->tt_buff_lock);
2345 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2347 struct batadv_tt_req_node *node;
2348 struct hlist_node *safe;
2350 spin_lock_bh(&bat_priv->tt.req_list_lock);
2351 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2352 if (batadv_has_timed_out(node->issued_at,
2353 BATADV_TT_REQUEST_TIMEOUT)) {
2354 hlist_del_init(&node->list);
2355 batadv_tt_req_node_put(node);
2358 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2362 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2363 * @bat_priv: the bat priv with all the soft interface information
2364 * @orig_node: orig node this request is being issued for
2366 * Returns the pointer to the new tt_req_node struct if no request
2367 * has already been issued for this orig_node, NULL otherwise.
2369 static struct batadv_tt_req_node *
2370 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2371 struct batadv_orig_node *orig_node)
2373 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2375 spin_lock_bh(&bat_priv->tt.req_list_lock);
2376 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2377 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2378 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2379 BATADV_TT_REQUEST_TIMEOUT))
2383 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2387 kref_init(&tt_req_node->refcount);
2388 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2389 tt_req_node->issued_at = jiffies;
2391 kref_get(&tt_req_node->refcount);
2392 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2394 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2399 * batadv_tt_local_valid - verify that given tt entry is a valid one
2400 * @entry_ptr: to be checked local tt entry
2401 * @data_ptr: not used but definition required to satisfy the callback prototype
2403 * Returns 1 if the entry is a valid, 0 otherwise.
2405 static int batadv_tt_local_valid(const void *entry_ptr,
2406 const void *data_ptr,
2409 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2411 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2415 *flags = tt_common_entry->flags;
2420 static int batadv_tt_global_valid(const void *entry_ptr,
2421 const void *data_ptr,
2424 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2425 const struct batadv_tt_global_entry *tt_global_entry;
2426 const struct batadv_orig_node *orig_node = data_ptr;
2428 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2429 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2432 tt_global_entry = container_of(tt_common_entry,
2433 struct batadv_tt_global_entry,
2436 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2441 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2443 * @bat_priv: the bat priv with all the soft interface information
2444 * @hash: hash table containing the tt entries
2445 * @tt_len: expected tvlv tt data buffer length in number of bytes
2446 * @tvlv_buff: pointer to the buffer to fill with the TT data
2447 * @valid_cb: function to filter tt change entries
2448 * @cb_data: data passed to the filter function as argument
2450 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2451 struct batadv_hashtable *hash,
2452 void *tvlv_buff, u16 tt_len,
2453 int (*valid_cb)(const void *,
2458 struct batadv_tt_common_entry *tt_common_entry;
2459 struct batadv_tvlv_tt_change *tt_change;
2460 struct hlist_head *head;
2461 u16 tt_tot, tt_num_entries = 0;
2466 tt_tot = batadv_tt_entries(tt_len);
2467 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2473 for (i = 0; i < hash->size; i++) {
2474 head = &hash->table[i];
2476 hlist_for_each_entry_rcu(tt_common_entry,
2478 if (tt_tot == tt_num_entries)
2481 ret = valid_cb(tt_common_entry, cb_data, &flags);
2485 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2486 tt_change->flags = flags;
2487 tt_change->vid = htons(tt_common_entry->vid);
2488 memset(tt_change->reserved, 0,
2489 sizeof(tt_change->reserved));
2499 * batadv_tt_global_check_crc - check if all the CRCs are correct
2500 * @orig_node: originator for which the CRCs have to be checked
2501 * @tt_vlan: pointer to the first tvlv VLAN entry
2502 * @num_vlan: number of tvlv VLAN entries
2503 * @create: if true, create VLAN objects if not found
2505 * Return true if all the received CRCs match the locally stored ones, false
2508 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2509 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2512 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2513 struct batadv_orig_node_vlan *vlan;
2517 /* check if each received CRC matches the locally stored one */
2518 for (i = 0; i < num_vlan; i++) {
2519 tt_vlan_tmp = tt_vlan + i;
2521 /* if orig_node is a backbone node for this VLAN, don't check
2522 * the CRC as we ignore all the global entries over it
2524 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2526 ntohs(tt_vlan_tmp->vid)))
2529 vlan = batadv_orig_node_vlan_get(orig_node,
2530 ntohs(tt_vlan_tmp->vid));
2535 batadv_orig_node_vlan_free_ref(vlan);
2537 if (crc != ntohl(tt_vlan_tmp->crc))
2545 * batadv_tt_local_update_crc - update all the local CRCs
2546 * @bat_priv: the bat priv with all the soft interface information
2548 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2550 struct batadv_softif_vlan *vlan;
2552 /* recompute the global CRC for each VLAN */
2554 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2555 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2561 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2562 * @bat_priv: the bat priv with all the soft interface information
2563 * @orig_node: the orig_node for which the CRCs have to be updated
2565 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2566 struct batadv_orig_node *orig_node)
2568 struct batadv_orig_node_vlan *vlan;
2571 /* recompute the global CRC for each VLAN */
2573 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2574 /* if orig_node is a backbone node for this VLAN, don't compute
2575 * the CRC as we ignore all the global entries over it
2577 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2581 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2588 * batadv_send_tt_request - send a TT Request message to a given node
2589 * @bat_priv: the bat priv with all the soft interface information
2590 * @dst_orig_node: the destination of the message
2591 * @ttvn: the version number that the source of the message is looking for
2592 * @tt_vlan: pointer to the first tvlv VLAN object to request
2593 * @num_vlan: number of tvlv VLAN entries
2594 * @full_table: ask for the entire translation table if true, while only for the
2595 * last TT diff otherwise
2597 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2598 struct batadv_orig_node *dst_orig_node,
2600 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2601 u16 num_vlan, bool full_table)
2603 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2604 struct batadv_tt_req_node *tt_req_node = NULL;
2605 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2606 struct batadv_hard_iface *primary_if;
2610 primary_if = batadv_primary_if_get_selected(bat_priv);
2614 /* The new tt_req will be issued only if I'm not waiting for a
2615 * reply from the same orig_node yet
2617 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2621 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2622 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2626 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2627 tvlv_tt_data->ttvn = ttvn;
2628 tvlv_tt_data->num_vlan = htons(num_vlan);
2630 /* send all the CRCs within the request. This is needed by intermediate
2631 * nodes to ensure they have the correct table before replying
2633 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2634 for (i = 0; i < num_vlan; i++) {
2635 tt_vlan_req->vid = tt_vlan->vid;
2636 tt_vlan_req->crc = tt_vlan->crc;
2643 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2645 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2646 dst_orig_node->orig, full_table ? 'F' : '.');
2648 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2649 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2650 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2651 tvlv_tt_data, size);
2656 batadv_hardif_free_ref(primary_if);
2658 if (ret && tt_req_node) {
2659 spin_lock_bh(&bat_priv->tt.req_list_lock);
2660 if (!hlist_unhashed(&tt_req_node->list)) {
2661 hlist_del_init(&tt_req_node->list);
2662 batadv_tt_req_node_put(tt_req_node);
2664 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2668 batadv_tt_req_node_put(tt_req_node);
2670 kfree(tvlv_tt_data);
2675 * batadv_send_other_tt_response - send reply to tt request concerning another
2676 * node's translation table
2677 * @bat_priv: the bat priv with all the soft interface information
2678 * @tt_data: tt data containing the tt request information
2679 * @req_src: mac address of tt request sender
2680 * @req_dst: mac address of tt request recipient
2682 * Returns true if tt request reply was sent, false otherwise.
2684 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2685 struct batadv_tvlv_tt_data *tt_data,
2686 u8 *req_src, u8 *req_dst)
2688 struct batadv_orig_node *req_dst_orig_node;
2689 struct batadv_orig_node *res_dst_orig_node = NULL;
2690 struct batadv_tvlv_tt_change *tt_change;
2691 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2692 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2693 bool ret = false, full_table;
2694 u8 orig_ttvn, req_ttvn;
2698 batadv_dbg(BATADV_DBG_TT, bat_priv,
2699 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2700 req_src, tt_data->ttvn, req_dst,
2701 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2703 /* Let's get the orig node of the REAL destination */
2704 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2705 if (!req_dst_orig_node)
2708 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2709 if (!res_dst_orig_node)
2712 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2713 req_ttvn = tt_data->ttvn;
2715 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2716 /* this node doesn't have the requested data */
2717 if (orig_ttvn != req_ttvn ||
2718 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2719 ntohs(tt_data->num_vlan)))
2722 /* If the full table has been explicitly requested */
2723 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2724 !req_dst_orig_node->tt_buff)
2729 /* TT fragmentation hasn't been implemented yet, so send as many
2730 * TT entries fit a single packet as possible only
2733 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2734 tt_len = req_dst_orig_node->tt_buff_len;
2736 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2743 /* Copy the last orig_node's OGM buffer */
2744 memcpy(tt_change, req_dst_orig_node->tt_buff,
2745 req_dst_orig_node->tt_buff_len);
2746 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2748 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2749 * in the initial part
2752 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2759 /* fill the rest of the tvlv with the real TT entries */
2760 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2762 batadv_tt_global_valid,
2766 /* Don't send the response, if larger than fragmented packet. */
2767 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2768 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2769 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2770 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2771 res_dst_orig_node->orig);
2775 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2776 tvlv_tt_data->ttvn = req_ttvn;
2779 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2781 batadv_dbg(BATADV_DBG_TT, bat_priv,
2782 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2783 res_dst_orig_node->orig, req_dst_orig_node->orig,
2784 full_table ? 'F' : '.', req_ttvn);
2786 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2788 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2789 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2796 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2799 if (res_dst_orig_node)
2800 batadv_orig_node_free_ref(res_dst_orig_node);
2801 if (req_dst_orig_node)
2802 batadv_orig_node_free_ref(req_dst_orig_node);
2803 kfree(tvlv_tt_data);
2808 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2810 * @bat_priv: the bat priv with all the soft interface information
2811 * @tt_data: tt data containing the tt request information
2812 * @req_src: mac address of tt request sender
2814 * Returns true if tt request reply was sent, false otherwise.
2816 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2817 struct batadv_tvlv_tt_data *tt_data,
2820 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2821 struct batadv_hard_iface *primary_if = NULL;
2822 struct batadv_tvlv_tt_change *tt_change;
2823 struct batadv_orig_node *orig_node;
2824 u8 my_ttvn, req_ttvn;
2829 batadv_dbg(BATADV_DBG_TT, bat_priv,
2830 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2831 req_src, tt_data->ttvn,
2832 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2834 spin_lock_bh(&bat_priv->tt.commit_lock);
2836 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2837 req_ttvn = tt_data->ttvn;
2839 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2843 primary_if = batadv_primary_if_get_selected(bat_priv);
2847 /* If the full table has been explicitly requested or the gap
2848 * is too big send the whole local translation table
2850 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2851 !bat_priv->tt.last_changeset)
2856 /* TT fragmentation hasn't been implemented yet, so send as many
2857 * TT entries fit a single packet as possible only
2860 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2862 tt_len = bat_priv->tt.last_changeset_len;
2863 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2867 if (!tt_len || !tvlv_len)
2870 /* Copy the last orig_node's OGM buffer */
2871 memcpy(tt_change, bat_priv->tt.last_changeset,
2872 bat_priv->tt.last_changeset_len);
2873 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2875 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2877 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2878 * in the initial part
2881 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2885 if (!tt_len || !tvlv_len)
2888 /* fill the rest of the tvlv with the real TT entries */
2889 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2891 batadv_tt_local_valid, NULL);
2894 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2895 tvlv_tt_data->ttvn = req_ttvn;
2898 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2900 batadv_dbg(BATADV_DBG_TT, bat_priv,
2901 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2902 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2904 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2906 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2907 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2913 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2915 spin_unlock_bh(&bat_priv->tt.commit_lock);
2917 batadv_orig_node_free_ref(orig_node);
2919 batadv_hardif_free_ref(primary_if);
2920 kfree(tvlv_tt_data);
2921 /* The packet was for this host, so it doesn't need to be re-routed */
2926 * batadv_send_tt_response - send reply to tt request
2927 * @bat_priv: the bat priv with all the soft interface information
2928 * @tt_data: tt data containing the tt request information
2929 * @req_src: mac address of tt request sender
2930 * @req_dst: mac address of tt request recipient
2932 * Returns true if tt request reply was sent, false otherwise.
2934 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2935 struct batadv_tvlv_tt_data *tt_data,
2936 u8 *req_src, u8 *req_dst)
2938 if (batadv_is_my_mac(bat_priv, req_dst))
2939 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2940 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2944 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2945 struct batadv_orig_node *orig_node,
2946 struct batadv_tvlv_tt_change *tt_change,
2947 u16 tt_num_changes, u8 ttvn)
2952 for (i = 0; i < tt_num_changes; i++) {
2953 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2954 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2955 batadv_tt_global_del(bat_priv, orig_node,
2956 (tt_change + i)->addr,
2957 ntohs((tt_change + i)->vid),
2958 "tt removed by changes",
2961 if (!batadv_tt_global_add(bat_priv, orig_node,
2962 (tt_change + i)->addr,
2963 ntohs((tt_change + i)->vid),
2964 (tt_change + i)->flags, ttvn))
2965 /* In case of problem while storing a
2966 * global_entry, we stop the updating
2967 * procedure without committing the
2968 * ttvn change. This will avoid to send
2969 * corrupted data on tt_request
2974 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2977 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2978 struct batadv_tvlv_tt_change *tt_change,
2979 u8 ttvn, u8 *resp_src,
2982 struct batadv_orig_node *orig_node;
2984 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2988 /* Purge the old table first.. */
2989 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2990 "Received full table");
2992 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2995 spin_lock_bh(&orig_node->tt_buff_lock);
2996 kfree(orig_node->tt_buff);
2997 orig_node->tt_buff_len = 0;
2998 orig_node->tt_buff = NULL;
2999 spin_unlock_bh(&orig_node->tt_buff_lock);
3001 atomic_set(&orig_node->last_ttvn, ttvn);
3005 batadv_orig_node_free_ref(orig_node);
3008 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3009 struct batadv_orig_node *orig_node,
3010 u16 tt_num_changes, u8 ttvn,
3011 struct batadv_tvlv_tt_change *tt_change)
3013 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3014 tt_num_changes, ttvn);
3016 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3017 batadv_tt_len(tt_num_changes));
3018 atomic_set(&orig_node->last_ttvn, ttvn);
3022 * batadv_is_my_client - check if a client is served by the local node
3023 * @bat_priv: the bat priv with all the soft interface information
3024 * @addr: the mac address of the client to check
3025 * @vid: VLAN identifier
3027 * Returns true if the client is served by this node, false otherwise.
3029 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3032 struct batadv_tt_local_entry *tt_local_entry;
3035 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3036 if (!tt_local_entry)
3038 /* Check if the client has been logically deleted (but is kept for
3039 * consistency purpose)
3041 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3042 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3047 batadv_tt_local_entry_free_ref(tt_local_entry);
3052 * batadv_handle_tt_response - process incoming tt reply
3053 * @bat_priv: the bat priv with all the soft interface information
3054 * @tt_data: tt data containing the tt request information
3055 * @resp_src: mac address of tt reply sender
3056 * @num_entries: number of tt change entries appended to the tt data
3058 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3059 struct batadv_tvlv_tt_data *tt_data,
3060 u8 *resp_src, u16 num_entries)
3062 struct batadv_tt_req_node *node;
3063 struct hlist_node *safe;
3064 struct batadv_orig_node *orig_node = NULL;
3065 struct batadv_tvlv_tt_change *tt_change;
3066 u8 *tvlv_ptr = (u8 *)tt_data;
3069 batadv_dbg(BATADV_DBG_TT, bat_priv,
3070 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3071 resp_src, tt_data->ttvn, num_entries,
3072 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3074 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3078 spin_lock_bh(&orig_node->tt_lock);
3080 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3081 change_offset *= ntohs(tt_data->num_vlan);
3082 change_offset += sizeof(*tt_data);
3083 tvlv_ptr += change_offset;
3085 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3086 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3087 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3088 resp_src, num_entries);
3090 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3091 tt_data->ttvn, tt_change);
3094 /* Recalculate the CRC for this orig_node and store it */
3095 batadv_tt_global_update_crc(bat_priv, orig_node);
3097 spin_unlock_bh(&orig_node->tt_lock);
3099 /* Delete the tt_req_node from pending tt_requests list */
3100 spin_lock_bh(&bat_priv->tt.req_list_lock);
3101 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3102 if (!batadv_compare_eth(node->addr, resp_src))
3104 hlist_del_init(&node->list);
3105 batadv_tt_req_node_put(node);
3108 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3111 batadv_orig_node_free_ref(orig_node);
3114 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3116 struct batadv_tt_roam_node *node, *safe;
3118 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3120 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3121 list_del(&node->list);
3125 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3128 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3130 struct batadv_tt_roam_node *node, *safe;
3132 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3133 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3134 if (!batadv_has_timed_out(node->first_time,
3135 BATADV_ROAMING_MAX_TIME))
3138 list_del(&node->list);
3141 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3144 /* This function checks whether the client already reached the
3145 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3148 * returns true if the ROAMING_ADV can be sent, false otherwise
3150 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3152 struct batadv_tt_roam_node *tt_roam_node;
3155 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3156 /* The new tt_req will be issued only if I'm not waiting for a
3157 * reply from the same orig_node yet
3159 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3160 if (!batadv_compare_eth(tt_roam_node->addr, client))
3163 if (batadv_has_timed_out(tt_roam_node->first_time,
3164 BATADV_ROAMING_MAX_TIME))
3167 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3168 /* Sorry, you roamed too many times! */
3175 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3179 tt_roam_node->first_time = jiffies;
3180 atomic_set(&tt_roam_node->counter,
3181 BATADV_ROAMING_MAX_COUNT - 1);
3182 ether_addr_copy(tt_roam_node->addr, client);
3184 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3189 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3194 * batadv_send_roam_adv - send a roaming advertisement message
3195 * @bat_priv: the bat priv with all the soft interface information
3196 * @client: mac address of the roaming client
3197 * @vid: VLAN identifier
3198 * @orig_node: message destination
3200 * Send a ROAMING_ADV message to the node which was previously serving this
3201 * client. This is done to inform the node that from now on all traffic destined
3202 * for this particular roamed client has to be forwarded to the sender of the
3205 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3207 struct batadv_orig_node *orig_node)
3209 struct batadv_hard_iface *primary_if;
3210 struct batadv_tvlv_roam_adv tvlv_roam;
3212 primary_if = batadv_primary_if_get_selected(bat_priv);
3216 /* before going on we have to check whether the client has
3217 * already roamed to us too many times
3219 if (!batadv_tt_check_roam_count(bat_priv, client))
3222 batadv_dbg(BATADV_DBG_TT, bat_priv,
3223 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3224 orig_node->orig, client, BATADV_PRINT_VID(vid));
3226 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3228 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3229 tvlv_roam.vid = htons(vid);
3231 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3232 orig_node->orig, BATADV_TVLV_ROAM, 1,
3233 &tvlv_roam, sizeof(tvlv_roam));
3237 batadv_hardif_free_ref(primary_if);
3240 static void batadv_tt_purge(struct work_struct *work)
3242 struct delayed_work *delayed_work;
3243 struct batadv_priv_tt *priv_tt;
3244 struct batadv_priv *bat_priv;
3246 delayed_work = container_of(work, struct delayed_work, work);
3247 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3248 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3250 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3251 batadv_tt_global_purge(bat_priv);
3252 batadv_tt_req_purge(bat_priv);
3253 batadv_tt_roam_purge(bat_priv);
3255 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3256 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3259 void batadv_tt_free(struct batadv_priv *bat_priv)
3261 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3263 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3264 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3266 cancel_delayed_work_sync(&bat_priv->tt.work);
3268 batadv_tt_local_table_free(bat_priv);
3269 batadv_tt_global_table_free(bat_priv);
3270 batadv_tt_req_list_free(bat_priv);
3271 batadv_tt_changes_list_free(bat_priv);
3272 batadv_tt_roam_list_free(bat_priv);
3274 kfree(bat_priv->tt.last_changeset);
3278 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3279 * table and possibly count them in the TT size
3280 * @bat_priv: the bat priv with all the soft interface information
3281 * @flags: the flag to switch
3282 * @enable: whether to set or unset the flag
3283 * @count: whether to increase the TT size by the number of changed entries
3285 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3286 bool enable, bool count)
3288 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3289 struct batadv_tt_common_entry *tt_common_entry;
3290 u16 changed_num = 0;
3291 struct hlist_head *head;
3297 for (i = 0; i < hash->size; i++) {
3298 head = &hash->table[i];
3301 hlist_for_each_entry_rcu(tt_common_entry,
3304 if ((tt_common_entry->flags & flags) == flags)
3306 tt_common_entry->flags |= flags;
3308 if (!(tt_common_entry->flags & flags))
3310 tt_common_entry->flags &= ~flags;
3317 batadv_tt_local_size_inc(bat_priv,
3318 tt_common_entry->vid);
3324 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3325 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3327 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3328 struct batadv_tt_common_entry *tt_common;
3329 struct batadv_tt_local_entry *tt_local;
3330 struct hlist_node *node_tmp;
3331 struct hlist_head *head;
3332 spinlock_t *list_lock; /* protects write access to the hash lists */
3338 for (i = 0; i < hash->size; i++) {
3339 head = &hash->table[i];
3340 list_lock = &hash->list_locks[i];
3342 spin_lock_bh(list_lock);
3343 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3345 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3348 batadv_dbg(BATADV_DBG_TT, bat_priv,
3349 "Deleting local tt entry (%pM, vid: %d): pending\n",
3351 BATADV_PRINT_VID(tt_common->vid));
3353 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3354 hlist_del_rcu(&tt_common->hash_entry);
3355 tt_local = container_of(tt_common,
3356 struct batadv_tt_local_entry,
3359 batadv_tt_local_entry_free_ref(tt_local);
3361 spin_unlock_bh(list_lock);
3366 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3367 * which have been queued in the time since the last commit
3368 * @bat_priv: the bat priv with all the soft interface information
3370 * Caller must hold tt->commit_lock.
3372 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3374 lockdep_assert_held(&bat_priv->tt.commit_lock);
3376 /* Update multicast addresses in local translation table */
3377 batadv_mcast_mla_update(bat_priv);
3379 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3380 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3381 batadv_tt_tvlv_container_update(bat_priv);
3385 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3387 batadv_tt_local_purge_pending_clients(bat_priv);
3388 batadv_tt_local_update_crc(bat_priv);
3390 /* Increment the TTVN only once per OGM interval */
3391 atomic_inc(&bat_priv->tt.vn);
3392 batadv_dbg(BATADV_DBG_TT, bat_priv,
3393 "Local changes committed, updating to ttvn %u\n",
3394 (u8)atomic_read(&bat_priv->tt.vn));
3396 /* reset the sending counter */
3397 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3398 batadv_tt_tvlv_container_update(bat_priv);
3402 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3403 * have been queued in the time since the last commit
3404 * @bat_priv: the bat priv with all the soft interface information
3406 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3408 spin_lock_bh(&bat_priv->tt.commit_lock);
3409 batadv_tt_local_commit_changes_nolock(bat_priv);
3410 spin_unlock_bh(&bat_priv->tt.commit_lock);
3413 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3416 struct batadv_tt_local_entry *tt_local_entry = NULL;
3417 struct batadv_tt_global_entry *tt_global_entry = NULL;
3418 struct batadv_softif_vlan *vlan;
3421 vlan = batadv_softif_vlan_get(bat_priv, vid);
3422 if (!vlan || !atomic_read(&vlan->ap_isolation))
3425 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3426 if (!tt_local_entry)
3429 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3430 if (!tt_global_entry)
3433 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3440 batadv_softif_vlan_free_ref(vlan);
3441 if (tt_global_entry)
3442 batadv_tt_global_entry_free_ref(tt_global_entry);
3444 batadv_tt_local_entry_free_ref(tt_local_entry);
3449 * batadv_tt_update_orig - update global translation table with new tt
3450 * information received via ogms
3451 * @bat_priv: the bat priv with all the soft interface information
3452 * @orig: the orig_node of the ogm
3453 * @tt_vlan: pointer to the first tvlv VLAN entry
3454 * @tt_num_vlan: number of tvlv VLAN entries
3455 * @tt_change: pointer to the first entry in the TT buffer
3456 * @tt_num_changes: number of tt changes inside the tt buffer
3457 * @ttvn: translation table version number of this changeset
3458 * @tt_crc: crc32 checksum of orig node's translation table
3460 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3461 struct batadv_orig_node *orig_node,
3462 const void *tt_buff, u16 tt_num_vlan,
3463 struct batadv_tvlv_tt_change *tt_change,
3464 u16 tt_num_changes, u8 ttvn)
3466 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3467 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3468 bool full_table = true;
3471 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3472 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3473 &orig_node->capa_initialized);
3475 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3476 * increased by one -> we can apply the attached changes
3478 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3479 /* the OGM could not contain the changes due to their size or
3480 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3482 * In this case send a tt request
3484 if (!tt_num_changes) {
3489 spin_lock_bh(&orig_node->tt_lock);
3491 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3494 /* Even if we received the precomputed crc with the OGM, we
3495 * prefer to recompute it to spot any possible inconsistency
3496 * in the global table
3498 batadv_tt_global_update_crc(bat_priv, orig_node);
3500 spin_unlock_bh(&orig_node->tt_lock);
3502 /* The ttvn alone is not enough to guarantee consistency
3503 * because a single value could represent different states
3504 * (due to the wrap around). Thus a node has to check whether
3505 * the resulting table (after applying the changes) is still
3506 * consistent or not. E.g. a node could disconnect while its
3507 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3508 * checking the CRC value is mandatory to detect the
3511 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3515 /* if we missed more than one change or our tables are not
3516 * in sync anymore -> request fresh tt data
3518 if (!has_tt_init || ttvn != orig_ttvn ||
3519 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3522 batadv_dbg(BATADV_DBG_TT, bat_priv,
3523 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3524 orig_node->orig, ttvn, orig_ttvn,
3526 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3527 tt_vlan, tt_num_vlan,
3535 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3536 * @bat_priv: the bat priv with all the soft interface information
3537 * @addr: the mac address of the client to check
3538 * @vid: VLAN identifier
3540 * Returns true if we know that the client has moved from its old originator
3541 * to another one. This entry is still kept for consistency purposes and will be
3542 * deleted later by a DEL or because of timeout
3544 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3545 u8 *addr, unsigned short vid)
3547 struct batadv_tt_global_entry *tt_global_entry;
3550 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3551 if (!tt_global_entry)
3554 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3555 batadv_tt_global_entry_free_ref(tt_global_entry);
3561 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3562 * @bat_priv: the bat priv with all the soft interface information
3563 * @addr: the mac address of the local client to query
3564 * @vid: VLAN identifier
3566 * Returns true if the local client is known to be roaming (it is not served by
3567 * this node anymore) or not. If yes, the client is still present in the table
3568 * to keep the latter consistent with the node TTVN
3570 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3571 u8 *addr, unsigned short vid)
3573 struct batadv_tt_local_entry *tt_local_entry;
3576 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3577 if (!tt_local_entry)
3580 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3581 batadv_tt_local_entry_free_ref(tt_local_entry);
3586 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3587 struct batadv_orig_node *orig_node,
3588 const unsigned char *addr,
3593 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3594 BATADV_TT_CLIENT_TEMP,
3595 atomic_read(&orig_node->last_ttvn)))
3598 batadv_dbg(BATADV_DBG_TT, bat_priv,
3599 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3600 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3607 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3608 * maximum packet size that can be transported through the mesh
3609 * @soft_iface: netdev struct of the mesh interface
3611 * Remove entries older than 'timeout' and half timeout if more entries need
3614 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3616 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3617 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3618 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3619 bool reduced = false;
3621 spin_lock_bh(&bat_priv->tt.commit_lock);
3624 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3625 if (packet_size_max >= table_size)
3628 batadv_tt_local_purge(bat_priv, timeout);
3629 batadv_tt_local_purge_pending_clients(bat_priv);
3633 net_ratelimited_function(batadv_info, soft_iface,
3634 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3638 /* commit these changes immediately, to avoid synchronization problem
3642 batadv_tt_local_commit_changes_nolock(bat_priv);
3644 spin_unlock_bh(&bat_priv->tt.commit_lock);
3648 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3649 * @bat_priv: the bat priv with all the soft interface information
3650 * @orig: the orig_node of the ogm
3651 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3652 * @tvlv_value: tvlv buffer containing the gateway data
3653 * @tvlv_value_len: tvlv buffer length
3655 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3656 struct batadv_orig_node *orig,
3657 u8 flags, void *tvlv_value,
3660 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3661 struct batadv_tvlv_tt_change *tt_change;
3662 struct batadv_tvlv_tt_data *tt_data;
3663 u16 num_entries, num_vlan;
3665 if (tvlv_value_len < sizeof(*tt_data))
3668 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3669 tvlv_value_len -= sizeof(*tt_data);
3671 num_vlan = ntohs(tt_data->num_vlan);
3673 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3676 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3677 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3678 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3680 num_entries = batadv_tt_entries(tvlv_value_len);
3682 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3683 num_entries, tt_data->ttvn);
3687 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3689 * @bat_priv: the bat priv with all the soft interface information
3690 * @src: mac address of tt tvlv sender
3691 * @dst: mac address of tt tvlv recipient
3692 * @tvlv_value: tvlv buffer containing the tt data
3693 * @tvlv_value_len: tvlv buffer length
3695 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3698 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3703 struct batadv_tvlv_tt_data *tt_data;
3704 u16 tt_vlan_len, tt_num_entries;
3708 if (tvlv_value_len < sizeof(*tt_data))
3709 return NET_RX_SUCCESS;
3711 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3712 tvlv_value_len -= sizeof(*tt_data);
3714 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3715 tt_vlan_len *= ntohs(tt_data->num_vlan);
3717 if (tvlv_value_len < tt_vlan_len)
3718 return NET_RX_SUCCESS;
3720 tvlv_value_len -= tt_vlan_len;
3721 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3723 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3724 case BATADV_TT_REQUEST:
3725 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3727 /* If this node cannot provide a TT response the tt_request is
3730 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3732 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3737 batadv_dbg(BATADV_DBG_TT, bat_priv,
3738 "Routing TT_REQUEST to %pM [%c]\n",
3740 /* tvlv API will re-route the packet */
3744 case BATADV_TT_RESPONSE:
3745 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3747 if (batadv_is_my_mac(bat_priv, dst)) {
3748 batadv_handle_tt_response(bat_priv, tt_data,
3749 src, tt_num_entries);
3750 return NET_RX_SUCCESS;
3753 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3758 batadv_dbg(BATADV_DBG_TT, bat_priv,
3759 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3761 /* tvlv API will re-route the packet */
3765 return NET_RX_SUCCESS;
3769 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3770 * @bat_priv: the bat priv with all the soft interface information
3771 * @src: mac address of tt tvlv sender
3772 * @dst: mac address of tt tvlv recipient
3773 * @tvlv_value: tvlv buffer containing the tt data
3774 * @tvlv_value_len: tvlv buffer length
3776 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3779 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3784 struct batadv_tvlv_roam_adv *roaming_adv;
3785 struct batadv_orig_node *orig_node = NULL;
3787 /* If this node is not the intended recipient of the
3788 * roaming advertisement the packet is forwarded
3789 * (the tvlv API will re-route the packet).
3791 if (!batadv_is_my_mac(bat_priv, dst))
3794 if (tvlv_value_len < sizeof(*roaming_adv))
3797 orig_node = batadv_orig_hash_find(bat_priv, src);
3801 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3802 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3804 batadv_dbg(BATADV_DBG_TT, bat_priv,
3805 "Received ROAMING_ADV from %pM (client %pM)\n",
3806 src, roaming_adv->client);
3808 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3809 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3810 atomic_read(&orig_node->last_ttvn) + 1);
3814 batadv_orig_node_free_ref(orig_node);
3815 return NET_RX_SUCCESS;
3819 * batadv_tt_init - initialise the translation table internals
3820 * @bat_priv: the bat priv with all the soft interface information
3822 * Return 0 on success or negative error number in case of failure.
3824 int batadv_tt_init(struct batadv_priv *bat_priv)
3828 /* synchronized flags must be remote */
3829 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3831 ret = batadv_tt_local_init(bat_priv);
3835 ret = batadv_tt_global_init(bat_priv);
3839 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3840 batadv_tt_tvlv_unicast_handler_v1,
3841 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3843 batadv_tvlv_handler_register(bat_priv, NULL,
3844 batadv_roam_tvlv_unicast_handler_v1,
3845 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3847 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3848 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3849 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3855 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3856 * @bat_priv: the bat priv with all the soft interface information
3857 * @addr: the mac address of the client
3858 * @vid: the identifier of the VLAN where this client is connected
3860 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3863 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3864 const u8 *addr, unsigned short vid)
3866 struct batadv_tt_global_entry *tt;
3869 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3873 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3875 batadv_tt_global_entry_free_ref(tt);