1 /* Copyright (C) 2007-2017 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/cache.h>
26 #include <linux/compiler.h>
27 #include <linux/crc32c.h>
28 #include <linux/errno.h>
29 #include <linux/etherdevice.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/jhash.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/lockdep.h>
39 #include <linux/netdevice.h>
40 #include <linux/netlink.h>
41 #include <linux/rculist.h>
42 #include <linux/rcupdate.h>
43 #include <linux/seq_file.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/stddef.h>
48 #include <linux/string.h>
49 #include <linux/workqueue.h>
50 #include <net/genetlink.h>
51 #include <net/netlink.h>
53 #include <uapi/linux/batman_adv.h>
55 #include "bridge_loop_avoidance.h"
56 #include "hard-interface.h"
60 #include "originator.h"
62 #include "soft-interface.h"
65 static struct kmem_cache *batadv_tl_cache __read_mostly;
66 static struct kmem_cache *batadv_tg_cache __read_mostly;
67 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
68 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
69 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
70 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
73 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
74 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
76 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
78 struct batadv_orig_node *orig_node);
79 static void batadv_tt_purge(struct work_struct *work);
81 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
82 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
83 struct batadv_orig_node *orig_node,
84 const unsigned char *addr,
85 unsigned short vid, const char *message,
89 * batadv_compare_tt - check if two TT entries are the same
90 * @node: the list element pointer of the first TT entry
91 * @data2: pointer to the tt_common_entry of the second TT entry
93 * Compare the MAC address and the VLAN ID of the two TT entries and check if
94 * they are the same TT client.
95 * Return: true if the two TT clients are the same, false otherwise
97 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
99 const void *data1 = container_of(node, struct batadv_tt_common_entry,
101 const struct batadv_tt_common_entry *tt1 = data1;
102 const struct batadv_tt_common_entry *tt2 = data2;
104 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
108 * batadv_choose_tt - return the index of the tt entry in the hash table
109 * @data: pointer to the tt_common_entry object to map
110 * @size: the size of the hash table
112 * Return: the hash index where the object represented by 'data' should be
115 static inline u32 batadv_choose_tt(const void *data, u32 size)
117 struct batadv_tt_common_entry *tt;
120 tt = (struct batadv_tt_common_entry *)data;
121 hash = jhash(&tt->addr, ETH_ALEN, hash);
122 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
128 * batadv_tt_hash_find - look for a client in the given hash table
129 * @hash: the hash table to search
130 * @addr: the mac address of the client to look for
131 * @vid: VLAN identifier
133 * Return: a pointer to the tt_common struct belonging to the searched client if
134 * found, NULL otherwise.
136 static struct batadv_tt_common_entry *
137 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
140 struct hlist_head *head;
141 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
147 ether_addr_copy(to_search.addr, addr);
150 index = batadv_choose_tt(&to_search, hash->size);
151 head = &hash->table[index];
154 hlist_for_each_entry_rcu(tt, head, hash_entry) {
155 if (!batadv_compare_eth(tt, addr))
161 if (!kref_get_unless_zero(&tt->refcount))
173 * batadv_tt_local_hash_find - search the local 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 * Return: a pointer to the corresponding tt_local_entry struct if the client is
179 * found, NULL otherwise.
181 static struct batadv_tt_local_entry *
182 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
185 struct batadv_tt_common_entry *tt_common_entry;
186 struct batadv_tt_local_entry *tt_local_entry = NULL;
188 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
191 tt_local_entry = container_of(tt_common_entry,
192 struct batadv_tt_local_entry,
194 return tt_local_entry;
198 * batadv_tt_global_hash_find - search the global table for a given client
199 * @bat_priv: the bat priv with all the soft interface information
200 * @addr: the mac address of the client to look for
201 * @vid: VLAN identifier
203 * Return: a pointer to the corresponding tt_global_entry struct if the client
204 * is found, NULL otherwise.
206 static struct batadv_tt_global_entry *
207 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
210 struct batadv_tt_common_entry *tt_common_entry;
211 struct batadv_tt_global_entry *tt_global_entry = NULL;
213 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
216 tt_global_entry = container_of(tt_common_entry,
217 struct batadv_tt_global_entry,
219 return tt_global_entry;
223 * batadv_tt_local_entry_free_rcu - free the tt_local_entry
224 * @rcu: rcu pointer of the tt_local_entry
226 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
228 struct batadv_tt_local_entry *tt_local_entry;
230 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
233 kmem_cache_free(batadv_tl_cache, tt_local_entry);
237 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
238 * for free after rcu grace period
239 * @ref: kref pointer of the nc_node
241 static void batadv_tt_local_entry_release(struct kref *ref)
243 struct batadv_tt_local_entry *tt_local_entry;
245 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
248 batadv_softif_vlan_put(tt_local_entry->vlan);
250 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
254 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
255 * possibly release it
256 * @tt_local_entry: tt_local_entry to be free'd
259 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
261 kref_put(&tt_local_entry->common.refcount,
262 batadv_tt_local_entry_release);
266 * batadv_tt_global_entry_free_rcu - free the tt_global_entry
267 * @rcu: rcu pointer of the tt_global_entry
269 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
271 struct batadv_tt_global_entry *tt_global_entry;
273 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
276 kmem_cache_free(batadv_tg_cache, tt_global_entry);
280 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
281 * for free after rcu grace period
282 * @ref: kref pointer of the nc_node
284 static void batadv_tt_global_entry_release(struct kref *ref)
286 struct batadv_tt_global_entry *tt_global_entry;
288 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
291 batadv_tt_global_del_orig_list(tt_global_entry);
293 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
297 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
298 * possibly release it
299 * @tt_global_entry: tt_global_entry to be free'd
302 batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
304 kref_put(&tt_global_entry->common.refcount,
305 batadv_tt_global_entry_release);
309 * batadv_tt_global_hash_count - count the number of orig entries
310 * @bat_priv: the bat priv with all the soft interface information
311 * @addr: the mac address of the client to count entries for
312 * @vid: VLAN identifier
314 * Return: the number of originators advertising the given address/data
315 * (excluding ourself).
317 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
318 const u8 *addr, unsigned short vid)
320 struct batadv_tt_global_entry *tt_global_entry;
323 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
324 if (!tt_global_entry)
327 count = atomic_read(&tt_global_entry->orig_list_count);
328 batadv_tt_global_entry_put(tt_global_entry);
334 * batadv_tt_local_size_mod - change the size by v of the local table identified
336 * @bat_priv: the bat priv with all the soft interface information
337 * @vid: the VLAN identifier of the sub-table to change
338 * @v: the amount to sum to the local table size
340 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
341 unsigned short vid, int v)
343 struct batadv_softif_vlan *vlan;
345 vlan = batadv_softif_vlan_get(bat_priv, vid);
349 atomic_add(v, &vlan->tt.num_entries);
351 batadv_softif_vlan_put(vlan);
355 * batadv_tt_local_size_inc - increase by one the local table size for the given
357 * @bat_priv: the bat priv with all the soft interface information
358 * @vid: the VLAN identifier
360 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
363 batadv_tt_local_size_mod(bat_priv, vid, 1);
367 * batadv_tt_local_size_dec - decrease by one the local table size for the given
369 * @bat_priv: the bat priv with all the soft interface information
370 * @vid: the VLAN identifier
372 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
375 batadv_tt_local_size_mod(bat_priv, vid, -1);
379 * batadv_tt_global_size_mod - change the size by v of the global table
380 * for orig_node identified by vid
381 * @orig_node: the originator for which the table has to be modified
382 * @vid: the VLAN identifier
383 * @v: the amount to sum to the global table size
385 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
386 unsigned short vid, int v)
388 struct batadv_orig_node_vlan *vlan;
390 vlan = batadv_orig_node_vlan_new(orig_node, vid);
394 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
395 spin_lock_bh(&orig_node->vlan_list_lock);
396 if (!hlist_unhashed(&vlan->list)) {
397 hlist_del_init_rcu(&vlan->list);
398 batadv_orig_node_vlan_put(vlan);
400 spin_unlock_bh(&orig_node->vlan_list_lock);
403 batadv_orig_node_vlan_put(vlan);
407 * batadv_tt_global_size_inc - increase by one the global table size for the
409 * @orig_node: the originator which global table size has to be decreased
410 * @vid: the vlan identifier
412 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
415 batadv_tt_global_size_mod(orig_node, vid, 1);
419 * batadv_tt_global_size_dec - decrease by one the global table size for the
421 * @orig_node: the originator which global table size has to be decreased
422 * @vid: the vlan identifier
424 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
427 batadv_tt_global_size_mod(orig_node, vid, -1);
431 * batadv_tt_orig_list_entry_free_rcu - free the orig_entry
432 * @rcu: rcu pointer of the orig_entry
434 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
436 struct batadv_tt_orig_list_entry *orig_entry;
438 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
440 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
444 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
445 * queue for free after rcu grace period
446 * @ref: kref pointer of the tt orig entry
448 static void batadv_tt_orig_list_entry_release(struct kref *ref)
450 struct batadv_tt_orig_list_entry *orig_entry;
452 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
455 batadv_orig_node_put(orig_entry->orig_node);
456 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
460 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
461 * possibly release it
462 * @orig_entry: tt orig entry to be free'd
465 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
467 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
471 * batadv_tt_local_event - store a local TT event (ADD/DEL)
472 * @bat_priv: the bat priv with all the soft interface information
473 * @tt_local_entry: the TT entry involved in the event
474 * @event_flags: flags to store in the event structure
476 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
477 struct batadv_tt_local_entry *tt_local_entry,
480 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
481 struct batadv_tt_common_entry *common = &tt_local_entry->common;
482 u8 flags = common->flags | event_flags;
483 bool event_removed = false;
484 bool del_op_requested, del_op_entry;
486 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
490 tt_change_node->change.flags = flags;
491 memset(tt_change_node->change.reserved, 0,
492 sizeof(tt_change_node->change.reserved));
493 ether_addr_copy(tt_change_node->change.addr, common->addr);
494 tt_change_node->change.vid = htons(common->vid);
496 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
498 /* check for ADD+DEL or DEL+ADD events */
499 spin_lock_bh(&bat_priv->tt.changes_list_lock);
500 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
502 if (!batadv_compare_eth(entry->change.addr, common->addr))
505 /* DEL+ADD in the same orig interval have no effect and can be
506 * removed to avoid silly behaviour on the receiver side. The
507 * other way around (ADD+DEL) can happen in case of roaming of
508 * a client still in the NEW state. Roaming of NEW clients is
509 * now possible due to automatically recognition of "temporary"
512 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
513 if (!del_op_requested && del_op_entry)
515 if (del_op_requested && !del_op_entry)
518 /* this is a second add in the same originator interval. It
519 * means that flags have been changed: update them!
521 if (!del_op_requested && !del_op_entry)
522 entry->change.flags = flags;
526 list_del(&entry->list);
527 kmem_cache_free(batadv_tt_change_cache, entry);
528 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
529 event_removed = true;
533 /* track the change in the OGMinterval list */
534 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
537 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
540 atomic_dec(&bat_priv->tt.local_changes);
542 atomic_inc(&bat_priv->tt.local_changes);
546 * batadv_tt_len - compute length in bytes of given number of tt changes
547 * @changes_num: number of tt changes
549 * Return: computed length in bytes.
551 static int batadv_tt_len(int changes_num)
553 return changes_num * sizeof(struct batadv_tvlv_tt_change);
557 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
558 * @tt_len: available space
560 * Return: the number of entries.
562 static u16 batadv_tt_entries(u16 tt_len)
564 return tt_len / batadv_tt_len(1);
568 * batadv_tt_local_table_transmit_size - calculates the local translation table
569 * size when transmitted over the air
570 * @bat_priv: the bat priv with all the soft interface information
572 * Return: local translation table size in bytes.
574 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
577 u16 tt_local_entries = 0;
578 struct batadv_softif_vlan *vlan;
582 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
584 tt_local_entries += atomic_read(&vlan->tt.num_entries);
588 /* header size of tvlv encapsulated tt response payload */
589 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
590 hdr_size += sizeof(struct batadv_tvlv_hdr);
591 hdr_size += sizeof(struct batadv_tvlv_tt_data);
592 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
594 return hdr_size + batadv_tt_len(tt_local_entries);
597 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
599 if (bat_priv->tt.local_hash)
602 bat_priv->tt.local_hash = batadv_hash_new(1024);
604 if (!bat_priv->tt.local_hash)
607 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
608 &batadv_tt_local_hash_lock_class_key);
613 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
614 struct batadv_tt_global_entry *tt_global,
617 batadv_dbg(BATADV_DBG_TT, bat_priv,
618 "Deleting global tt entry %pM (vid: %d): %s\n",
619 tt_global->common.addr,
620 BATADV_PRINT_VID(tt_global->common.vid), message);
622 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
623 batadv_choose_tt, &tt_global->common);
624 batadv_tt_global_entry_put(tt_global);
628 * batadv_tt_local_add - add a new client to the local table or update an
630 * @soft_iface: netdev struct of the mesh interface
631 * @addr: the mac address of the client to add
632 * @vid: VLAN identifier
633 * @ifindex: index of the interface where the client is connected to (useful to
634 * identify wireless clients)
635 * @mark: the value contained in the skb->mark field of the received packet (if
638 * Return: true if the client was successfully added, false otherwise.
640 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
641 unsigned short vid, int ifindex, u32 mark)
643 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
644 struct batadv_tt_local_entry *tt_local;
645 struct batadv_tt_global_entry *tt_global = NULL;
646 struct net *net = dev_net(soft_iface);
647 struct batadv_softif_vlan *vlan;
648 struct net_device *in_dev = NULL;
649 struct batadv_hard_iface *in_hardif = NULL;
650 struct hlist_head *head;
651 struct batadv_tt_orig_list_entry *orig_entry;
652 int hash_added, table_size, packet_size_max;
654 bool roamed_back = false;
658 if (ifindex != BATADV_NULL_IFINDEX)
659 in_dev = dev_get_by_index(net, ifindex);
662 in_hardif = batadv_hardif_get_by_netdev(in_dev);
664 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
666 if (!is_multicast_ether_addr(addr))
667 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
670 tt_local->last_seen = jiffies;
671 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
672 batadv_dbg(BATADV_DBG_TT, bat_priv,
673 "Re-adding pending client %pM (vid: %d)\n",
674 addr, BATADV_PRINT_VID(vid));
675 /* whatever the reason why the PENDING flag was set,
676 * this is a client which was enqueued to be removed in
677 * this orig_interval. Since it popped up again, the
678 * flag can be reset like it was never enqueued
680 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
684 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
685 batadv_dbg(BATADV_DBG_TT, bat_priv,
686 "Roaming client %pM (vid: %d) came back to its original location\n",
687 addr, BATADV_PRINT_VID(vid));
688 /* the ROAM flag is set because this client roamed away
689 * and the node got a roaming_advertisement message. Now
690 * that the client popped up again at its original
691 * location such flag can be unset
693 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
699 /* Ignore the client if we cannot send it in a full table response. */
700 table_size = batadv_tt_local_table_transmit_size(bat_priv);
701 table_size += batadv_tt_len(1);
702 packet_size_max = atomic_read(&bat_priv->packet_size_max);
703 if (table_size > packet_size_max) {
704 net_ratelimited_function(batadv_info, soft_iface,
705 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
706 table_size, packet_size_max, addr);
710 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
714 /* increase the refcounter of the related vlan */
715 vlan = batadv_softif_vlan_get(bat_priv, vid);
717 net_ratelimited_function(batadv_info, soft_iface,
718 "adding TT local entry %pM to non-existent VLAN %d\n",
719 addr, BATADV_PRINT_VID(vid));
720 kmem_cache_free(batadv_tl_cache, tt_local);
725 batadv_dbg(BATADV_DBG_TT, bat_priv,
726 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
727 addr, BATADV_PRINT_VID(vid),
728 (u8)atomic_read(&bat_priv->tt.vn));
730 ether_addr_copy(tt_local->common.addr, addr);
731 /* The local entry has to be marked as NEW to avoid to send it in
732 * a full table response going out before the next ttvn increment
733 * (consistency check)
735 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
736 tt_local->common.vid = vid;
737 if (batadv_is_wifi_hardif(in_hardif))
738 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
739 kref_init(&tt_local->common.refcount);
740 tt_local->last_seen = jiffies;
741 tt_local->common.added_at = tt_local->last_seen;
742 tt_local->vlan = vlan;
744 /* the batman interface mac and multicast addresses should never be
747 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
748 is_multicast_ether_addr(addr))
749 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
751 kref_get(&tt_local->common.refcount);
752 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
753 batadv_choose_tt, &tt_local->common,
754 &tt_local->common.hash_entry);
756 if (unlikely(hash_added != 0)) {
757 /* remove the reference for the hash */
758 batadv_tt_local_entry_put(tt_local);
763 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
766 /* Check whether it is a roaming, but don't do anything if the roaming
767 * process has already been handled
769 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
770 /* These node are probably going to update their tt table */
771 head = &tt_global->orig_list;
773 hlist_for_each_entry_rcu(orig_entry, head, list) {
774 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
775 tt_global->common.vid,
776 orig_entry->orig_node);
780 batadv_tt_global_free(bat_priv, tt_global,
784 /* The global entry has to be marked as ROAMING and
785 * has to be kept for consistency purpose
787 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
788 tt_global->roam_at = jiffies;
792 /* store the current remote flags before altering them. This helps
793 * understanding is flags are changing or not
795 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
797 if (batadv_is_wifi_hardif(in_hardif))
798 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
800 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
802 /* check the mark in the skb: if it's equal to the configured
803 * isolation_mark, it means the packet is coming from an isolated
806 match_mark = (mark & bat_priv->isolation_mark_mask);
807 if (bat_priv->isolation_mark_mask &&
808 match_mark == bat_priv->isolation_mark)
809 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
811 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
813 /* if any "dynamic" flag has been modified, resend an ADD event for this
814 * entry so that all the nodes can get the new flags
816 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
817 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
822 batadv_hardif_put(in_hardif);
826 batadv_tt_local_entry_put(tt_local);
828 batadv_tt_global_entry_put(tt_global);
833 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
834 * within a TT Response directed to another node
835 * @orig_node: originator for which the TT data has to be prepared
836 * @tt_data: uninitialised pointer to the address of the TVLV buffer
837 * @tt_change: uninitialised pointer to the address of the area where the TT
838 * changed can be stored
839 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
840 * function reserves the amount of space needed to send the entire global TT
841 * table. In case of success the value is updated with the real amount of
843 * Allocate the needed amount of memory for the entire TT TVLV and write its
844 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
845 * objects, one per active VLAN served by the originator node.
847 * Return: the size of the allocated buffer or 0 in case of failure.
850 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
851 struct batadv_tvlv_tt_data **tt_data,
852 struct batadv_tvlv_tt_change **tt_change,
859 struct batadv_tvlv_tt_vlan_data *tt_vlan;
860 struct batadv_orig_node_vlan *vlan;
864 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
866 num_entries += atomic_read(&vlan->tt.num_entries);
869 change_offset = sizeof(**tt_data);
870 change_offset += num_vlan * sizeof(*tt_vlan);
872 /* if tt_len is negative, allocate the space needed by the full table */
874 *tt_len = batadv_tt_len(num_entries);
877 tvlv_len += change_offset;
879 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
885 (*tt_data)->flags = BATADV_NO_FLAGS;
886 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
887 (*tt_data)->num_vlan = htons(num_vlan);
889 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
890 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
891 tt_vlan->vid = htons(vlan->vid);
892 tt_vlan->crc = htonl(vlan->tt.crc);
897 tt_change_ptr = (u8 *)*tt_data + change_offset;
898 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
906 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
908 * @bat_priv: the bat priv with all the soft interface information
909 * @tt_data: uninitialised pointer to the address of the TVLV buffer
910 * @tt_change: uninitialised pointer to the address of the area where the TT
911 * changes can be stored
912 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
913 * function reserves the amount of space needed to send the entire local TT
914 * table. In case of success the value is updated with the real amount of
917 * Allocate the needed amount of memory for the entire TT TVLV and write its
918 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
919 * objects, one per active VLAN.
921 * Return: the size of the allocated buffer or 0 in case of failure.
924 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
925 struct batadv_tvlv_tt_data **tt_data,
926 struct batadv_tvlv_tt_change **tt_change,
929 struct batadv_tvlv_tt_vlan_data *tt_vlan;
930 struct batadv_softif_vlan *vlan;
938 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
940 num_entries += atomic_read(&vlan->tt.num_entries);
943 change_offset = sizeof(**tt_data);
944 change_offset += num_vlan * sizeof(*tt_vlan);
946 /* if tt_len is negative, allocate the space needed by the full table */
948 *tt_len = batadv_tt_len(num_entries);
951 tvlv_len += change_offset;
953 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
959 (*tt_data)->flags = BATADV_NO_FLAGS;
960 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
961 (*tt_data)->num_vlan = htons(num_vlan);
963 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
964 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
965 tt_vlan->vid = htons(vlan->vid);
966 tt_vlan->crc = htonl(vlan->tt.crc);
971 tt_change_ptr = (u8 *)*tt_data + change_offset;
972 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
980 * batadv_tt_tvlv_container_update - update the translation table tvlv container
981 * after local tt changes have been committed
982 * @bat_priv: the bat priv with all the soft interface information
984 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
986 struct batadv_tt_change_node *entry, *safe;
987 struct batadv_tvlv_tt_data *tt_data;
988 struct batadv_tvlv_tt_change *tt_change;
989 int tt_diff_len, tt_change_len = 0;
990 int tt_diff_entries_num = 0;
991 int tt_diff_entries_count = 0;
994 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
995 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
997 /* if we have too many changes for one packet don't send any
998 * and wait for the tt table request which will be fragmented
1000 if (tt_diff_len > bat_priv->soft_iface->mtu)
1003 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1004 &tt_change, &tt_diff_len);
1008 tt_data->flags = BATADV_TT_OGM_DIFF;
1010 if (tt_diff_len == 0)
1011 goto container_register;
1013 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1014 atomic_set(&bat_priv->tt.local_changes, 0);
1016 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1018 if (tt_diff_entries_count < tt_diff_entries_num) {
1019 memcpy(tt_change + tt_diff_entries_count,
1021 sizeof(struct batadv_tvlv_tt_change));
1022 tt_diff_entries_count++;
1024 list_del(&entry->list);
1025 kmem_cache_free(batadv_tt_change_cache, entry);
1027 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1029 /* Keep the buffer for possible tt_request */
1030 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1031 kfree(bat_priv->tt.last_changeset);
1032 bat_priv->tt.last_changeset_len = 0;
1033 bat_priv->tt.last_changeset = NULL;
1034 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1035 /* check whether this new OGM has no changes due to size problems */
1036 if (tt_diff_entries_count > 0) {
1037 /* if kmalloc() fails we will reply with the full table
1038 * instead of providing the diff
1040 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1041 if (bat_priv->tt.last_changeset) {
1042 memcpy(bat_priv->tt.last_changeset,
1043 tt_change, tt_change_len);
1044 bat_priv->tt.last_changeset_len = tt_diff_len;
1047 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1050 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1055 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1056 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
1058 struct net_device *net_dev = (struct net_device *)seq->private;
1059 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1060 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1061 struct batadv_tt_common_entry *tt_common_entry;
1062 struct batadv_tt_local_entry *tt_local;
1063 struct batadv_hard_iface *primary_if;
1064 struct hlist_head *head;
1067 int last_seen_msecs;
1068 unsigned long last_seen_jiffies;
1070 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1072 primary_if = batadv_seq_print_text_primary_if_get(seq);
1077 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1078 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1080 " Client VID Flags Last seen (CRC )\n");
1082 for (i = 0; i < hash->size; i++) {
1083 head = &hash->table[i];
1086 hlist_for_each_entry_rcu(tt_common_entry,
1088 tt_local = container_of(tt_common_entry,
1089 struct batadv_tt_local_entry,
1091 last_seen_jiffies = jiffies - tt_local->last_seen;
1092 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1093 last_seen_secs = last_seen_msecs / 1000;
1094 last_seen_msecs = last_seen_msecs % 1000;
1096 no_purge = tt_common_entry->flags & np_flag;
1098 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1099 tt_common_entry->addr,
1100 BATADV_PRINT_VID(tt_common_entry->vid),
1101 ((tt_common_entry->flags &
1102 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1103 no_purge ? 'P' : '.',
1104 ((tt_common_entry->flags &
1105 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1106 ((tt_common_entry->flags &
1107 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1108 ((tt_common_entry->flags &
1109 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1110 ((tt_common_entry->flags &
1111 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1112 no_purge ? 0 : last_seen_secs,
1113 no_purge ? 0 : last_seen_msecs,
1114 tt_local->vlan->tt.crc);
1120 batadv_hardif_put(primary_if);
1126 * batadv_tt_local_dump_entry - Dump one TT local entry into a message
1127 * @msg :Netlink message to dump into
1128 * @portid: Port making netlink request
1129 * @seq: Sequence number of netlink message
1130 * @bat_priv: The bat priv with all the soft interface information
1131 * @common: tt local & tt global common data
1133 * Return: Error code, or 0 on success
1136 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1137 struct batadv_priv *bat_priv,
1138 struct batadv_tt_common_entry *common)
1141 struct batadv_softif_vlan *vlan;
1142 struct batadv_tt_local_entry *local;
1143 unsigned int last_seen_msecs;
1146 local = container_of(common, struct batadv_tt_local_entry, common);
1147 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1149 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1155 batadv_softif_vlan_put(vlan);
1157 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1159 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1163 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1164 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1165 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1166 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1167 goto nla_put_failure;
1169 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1170 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1171 goto nla_put_failure;
1173 genlmsg_end(msg, hdr);
1177 genlmsg_cancel(msg, hdr);
1182 * batadv_tt_local_dump_bucket - Dump one TT local bucket into a message
1183 * @msg: Netlink message to dump into
1184 * @portid: Port making netlink request
1185 * @seq: Sequence number of netlink message
1186 * @bat_priv: The bat priv with all the soft interface information
1187 * @head: Pointer to the list containing the local tt entries
1188 * @idx_s: Number of entries to skip
1190 * Return: Error code, or 0 on success
1193 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1194 struct batadv_priv *bat_priv,
1195 struct hlist_head *head, int *idx_s)
1197 struct batadv_tt_common_entry *common;
1201 hlist_for_each_entry_rcu(common, head, hash_entry) {
1205 if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
1219 * batadv_tt_local_dump - Dump TT local entries into a message
1220 * @msg: Netlink message to dump into
1221 * @cb: Parameters from query
1223 * Return: Error code, or 0 on success
1225 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1227 struct net *net = sock_net(cb->skb->sk);
1228 struct net_device *soft_iface;
1229 struct batadv_priv *bat_priv;
1230 struct batadv_hard_iface *primary_if = NULL;
1231 struct batadv_hashtable *hash;
1232 struct hlist_head *head;
1235 int bucket = cb->args[0];
1236 int idx = cb->args[1];
1237 int portid = NETLINK_CB(cb->skb).portid;
1239 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1243 soft_iface = dev_get_by_index(net, ifindex);
1244 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1249 bat_priv = netdev_priv(soft_iface);
1251 primary_if = batadv_primary_if_get_selected(bat_priv);
1252 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1257 hash = bat_priv->tt.local_hash;
1259 while (bucket < hash->size) {
1260 head = &hash->table[bucket];
1262 if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
1263 bat_priv, head, &idx))
1273 batadv_hardif_put(primary_if);
1275 dev_put(soft_iface);
1277 cb->args[0] = bucket;
1284 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1285 struct batadv_tt_local_entry *tt_local_entry,
1286 u16 flags, const char *message)
1288 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1290 /* The local client has to be marked as "pending to be removed" but has
1291 * to be kept in the table in order to send it in a full table
1292 * response issued before the net ttvn increment (consistency check)
1294 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1296 batadv_dbg(BATADV_DBG_TT, bat_priv,
1297 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1298 tt_local_entry->common.addr,
1299 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1303 * batadv_tt_local_remove - logically remove an entry from the local table
1304 * @bat_priv: the bat priv with all the soft interface information
1305 * @addr: the MAC address of the client to remove
1306 * @vid: VLAN identifier
1307 * @message: message to append to the log on deletion
1308 * @roaming: true if the deletion is due to a roaming event
1310 * Return: the flags assigned to the local entry before being deleted
1312 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1313 unsigned short vid, const char *message,
1316 struct batadv_tt_local_entry *tt_local_entry;
1317 u16 flags, curr_flags = BATADV_NO_FLAGS;
1318 void *tt_entry_exists;
1320 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1321 if (!tt_local_entry)
1324 curr_flags = tt_local_entry->common.flags;
1326 flags = BATADV_TT_CLIENT_DEL;
1327 /* if this global entry addition is due to a roaming, the node has to
1328 * mark the local entry as "roamed" in order to correctly reroute
1332 flags |= BATADV_TT_CLIENT_ROAM;
1333 /* mark the local client as ROAMed */
1334 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1337 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1338 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1342 /* if this client has been added right now, it is possible to
1343 * immediately purge it
1345 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1347 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1350 &tt_local_entry->common);
1351 if (!tt_entry_exists)
1354 /* extra call to free the local tt entry */
1355 batadv_tt_local_entry_put(tt_local_entry);
1359 batadv_tt_local_entry_put(tt_local_entry);
1365 * batadv_tt_local_purge_list - purge inactive tt local entries
1366 * @bat_priv: the bat priv with all the soft interface information
1367 * @head: pointer to the list containing the local tt entries
1368 * @timeout: parameter deciding whether a given tt local entry is considered
1371 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1372 struct hlist_head *head,
1375 struct batadv_tt_local_entry *tt_local_entry;
1376 struct batadv_tt_common_entry *tt_common_entry;
1377 struct hlist_node *node_tmp;
1379 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1381 tt_local_entry = container_of(tt_common_entry,
1382 struct batadv_tt_local_entry,
1384 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1387 /* entry already marked for deletion */
1388 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1391 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1394 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1395 BATADV_TT_CLIENT_DEL, "timed out");
1400 * batadv_tt_local_purge - purge inactive tt local entries
1401 * @bat_priv: the bat priv with all the soft interface information
1402 * @timeout: parameter deciding whether a given tt local entry is considered
1405 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1408 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1409 struct hlist_head *head;
1410 spinlock_t *list_lock; /* protects write access to the hash lists */
1413 for (i = 0; i < hash->size; i++) {
1414 head = &hash->table[i];
1415 list_lock = &hash->list_locks[i];
1417 spin_lock_bh(list_lock);
1418 batadv_tt_local_purge_list(bat_priv, head, timeout);
1419 spin_unlock_bh(list_lock);
1423 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1425 struct batadv_hashtable *hash;
1426 spinlock_t *list_lock; /* protects write access to the hash lists */
1427 struct batadv_tt_common_entry *tt_common_entry;
1428 struct batadv_tt_local_entry *tt_local;
1429 struct hlist_node *node_tmp;
1430 struct hlist_head *head;
1433 if (!bat_priv->tt.local_hash)
1436 hash = bat_priv->tt.local_hash;
1438 for (i = 0; i < hash->size; i++) {
1439 head = &hash->table[i];
1440 list_lock = &hash->list_locks[i];
1442 spin_lock_bh(list_lock);
1443 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1445 hlist_del_rcu(&tt_common_entry->hash_entry);
1446 tt_local = container_of(tt_common_entry,
1447 struct batadv_tt_local_entry,
1450 batadv_tt_local_entry_put(tt_local);
1452 spin_unlock_bh(list_lock);
1455 batadv_hash_destroy(hash);
1457 bat_priv->tt.local_hash = NULL;
1460 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1462 if (bat_priv->tt.global_hash)
1465 bat_priv->tt.global_hash = batadv_hash_new(1024);
1467 if (!bat_priv->tt.global_hash)
1470 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1471 &batadv_tt_global_hash_lock_class_key);
1476 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1478 struct batadv_tt_change_node *entry, *safe;
1480 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1482 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1484 list_del(&entry->list);
1485 kmem_cache_free(batadv_tt_change_cache, entry);
1488 atomic_set(&bat_priv->tt.local_changes, 0);
1489 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1493 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1494 * @entry: the TT global entry where the orig_list_entry has to be
1496 * @orig_node: the originator for which the orig_list_entry has to be found
1498 * retrieve the orig_tt_list_entry belonging to orig_node from the
1499 * batadv_tt_global_entry list
1501 * Return: it with an increased refcounter, NULL if not found
1503 static struct batadv_tt_orig_list_entry *
1504 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1505 const struct batadv_orig_node *orig_node)
1507 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1508 const struct hlist_head *head;
1511 head = &entry->orig_list;
1512 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1513 if (tmp_orig_entry->orig_node != orig_node)
1515 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1518 orig_entry = tmp_orig_entry;
1527 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1528 * by a given originator
1529 * @entry: the TT global entry to check
1530 * @orig_node: the originator to search in the list
1532 * find out if an orig_node is already in the list of a tt_global_entry.
1534 * Return: true if found, false otherwise
1537 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1538 const struct batadv_orig_node *orig_node)
1540 struct batadv_tt_orig_list_entry *orig_entry;
1543 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1546 batadv_tt_orig_list_entry_put(orig_entry);
1553 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1554 struct batadv_orig_node *orig_node, int ttvn)
1556 struct batadv_tt_orig_list_entry *orig_entry;
1558 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1560 /* refresh the ttvn: the current value could be a bogus one that
1561 * was added during a "temporary client detection"
1563 orig_entry->ttvn = ttvn;
1567 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1571 INIT_HLIST_NODE(&orig_entry->list);
1572 kref_get(&orig_node->refcount);
1573 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1574 orig_entry->orig_node = orig_node;
1575 orig_entry->ttvn = ttvn;
1576 kref_init(&orig_entry->refcount);
1578 spin_lock_bh(&tt_global->list_lock);
1579 kref_get(&orig_entry->refcount);
1580 hlist_add_head_rcu(&orig_entry->list,
1581 &tt_global->orig_list);
1582 spin_unlock_bh(&tt_global->list_lock);
1583 atomic_inc(&tt_global->orig_list_count);
1587 batadv_tt_orig_list_entry_put(orig_entry);
1591 * batadv_tt_global_add - add a new TT global entry or update an existing one
1592 * @bat_priv: the bat priv with all the soft interface information
1593 * @orig_node: the originator announcing the client
1594 * @tt_addr: the mac address of the non-mesh client
1595 * @vid: VLAN identifier
1596 * @flags: TT flags that have to be set for this non-mesh client
1597 * @ttvn: the tt version number ever announcing this non-mesh client
1599 * Add a new TT global entry for the given originator. If the entry already
1600 * exists add a new reference to the given originator (a global entry can have
1601 * references to multiple originators) and adjust the flags attribute to reflect
1602 * the function argument.
1603 * If a TT local entry exists for this non-mesh client remove it.
1605 * The caller must hold orig_node refcount.
1607 * Return: true if the new entry has been added, false otherwise
1609 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1610 struct batadv_orig_node *orig_node,
1611 const unsigned char *tt_addr,
1612 unsigned short vid, u16 flags, u8 ttvn)
1614 struct batadv_tt_global_entry *tt_global_entry;
1615 struct batadv_tt_local_entry *tt_local_entry;
1618 struct batadv_tt_common_entry *common;
1621 /* ignore global entries from backbone nodes */
1622 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1625 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1626 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1628 /* if the node already has a local client for this entry, it has to wait
1629 * for a roaming advertisement instead of manually messing up the global
1632 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1633 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1636 if (!tt_global_entry) {
1637 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1639 if (!tt_global_entry)
1642 common = &tt_global_entry->common;
1643 ether_addr_copy(common->addr, tt_addr);
1646 common->flags = flags;
1647 tt_global_entry->roam_at = 0;
1648 /* node must store current time in case of roaming. This is
1649 * needed to purge this entry out on timeout (if nobody claims
1652 if (flags & BATADV_TT_CLIENT_ROAM)
1653 tt_global_entry->roam_at = jiffies;
1654 kref_init(&common->refcount);
1655 common->added_at = jiffies;
1657 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1658 atomic_set(&tt_global_entry->orig_list_count, 0);
1659 spin_lock_init(&tt_global_entry->list_lock);
1661 kref_get(&common->refcount);
1662 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1664 batadv_choose_tt, common,
1665 &common->hash_entry);
1667 if (unlikely(hash_added != 0)) {
1668 /* remove the reference for the hash */
1669 batadv_tt_global_entry_put(tt_global_entry);
1673 common = &tt_global_entry->common;
1674 /* If there is already a global entry, we can use this one for
1676 * But if we are trying to add a temporary client then here are
1677 * two options at this point:
1678 * 1) the global client is not a temporary client: the global
1679 * client has to be left as it is, temporary information
1680 * should never override any already known client state
1681 * 2) the global client is a temporary client: purge the
1682 * originator list and add the new one orig_entry
1684 if (flags & BATADV_TT_CLIENT_TEMP) {
1685 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1687 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1690 batadv_tt_global_del_orig_list(tt_global_entry);
1691 goto add_orig_entry;
1694 /* if the client was temporary added before receiving the first
1695 * OGM announcing it, we have to clear the TEMP flag. Also,
1696 * remove the previous temporary orig node and re-add it
1697 * if required. If the orig entry changed, the new one which
1698 * is a non-temporary entry is preferred.
1700 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1701 batadv_tt_global_del_orig_list(tt_global_entry);
1702 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1705 /* the change can carry possible "attribute" flags like the
1706 * TT_CLIENT_WIFI, therefore they have to be copied in the
1709 common->flags |= flags;
1711 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1712 * one originator left in the list and we previously received a
1713 * delete + roaming change for this originator.
1715 * We should first delete the old originator before adding the
1718 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1719 batadv_tt_global_del_orig_list(tt_global_entry);
1720 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1721 tt_global_entry->roam_at = 0;
1725 /* add the new orig_entry (if needed) or update it */
1726 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1728 batadv_dbg(BATADV_DBG_TT, bat_priv,
1729 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1730 common->addr, BATADV_PRINT_VID(common->vid),
1735 /* Do not remove multicast addresses from the local hash on
1738 if (is_multicast_ether_addr(tt_addr))
1741 /* remove address from local hash if present */
1742 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1743 "global tt received",
1744 flags & BATADV_TT_CLIENT_ROAM);
1745 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1747 if (!(flags & BATADV_TT_CLIENT_ROAM))
1748 /* this is a normal global add. Therefore the client is not in a
1749 * roaming state anymore.
1751 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1754 if (tt_global_entry)
1755 batadv_tt_global_entry_put(tt_global_entry);
1757 batadv_tt_local_entry_put(tt_local_entry);
1762 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1763 * @bat_priv: the bat priv with all the soft interface information
1764 * @tt_global_entry: global translation table entry to be analyzed
1766 * This functon assumes the caller holds rcu_read_lock().
1767 * Return: best originator list entry or NULL on errors.
1769 static struct batadv_tt_orig_list_entry *
1770 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1771 struct batadv_tt_global_entry *tt_global_entry)
1773 struct batadv_neigh_node *router, *best_router = NULL;
1774 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1775 struct hlist_head *head;
1776 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1778 head = &tt_global_entry->orig_list;
1779 hlist_for_each_entry_rcu(orig_entry, head, list) {
1780 router = batadv_orig_router_get(orig_entry->orig_node,
1786 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1787 BATADV_IF_DEFAULT) <= 0) {
1788 batadv_neigh_node_put(router);
1792 /* release the refcount for the "old" best */
1794 batadv_neigh_node_put(best_router);
1796 best_entry = orig_entry;
1797 best_router = router;
1801 batadv_neigh_node_put(best_router);
1806 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1808 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1809 * for this global entry
1810 * @bat_priv: the bat priv with all the soft interface information
1811 * @tt_global_entry: global translation table entry to be printed
1812 * @seq: debugfs table seq_file struct
1814 * This functon assumes the caller holds rcu_read_lock().
1817 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1818 struct batadv_tt_global_entry *tt_global_entry,
1819 struct seq_file *seq)
1821 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1822 struct batadv_tt_common_entry *tt_common_entry;
1823 struct batadv_orig_node_vlan *vlan;
1824 struct hlist_head *head;
1828 tt_common_entry = &tt_global_entry->common;
1829 flags = tt_common_entry->flags;
1831 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1833 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1834 tt_common_entry->vid);
1837 " * Cannot retrieve VLAN %d for originator %pM\n",
1838 BATADV_PRINT_VID(tt_common_entry->vid),
1839 best_entry->orig_node->orig);
1843 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1845 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1846 '*', tt_global_entry->common.addr,
1847 BATADV_PRINT_VID(tt_global_entry->common.vid),
1848 best_entry->ttvn, best_entry->orig_node->orig,
1849 last_ttvn, vlan->tt.crc,
1850 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1851 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1852 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1853 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1855 batadv_orig_node_vlan_put(vlan);
1859 head = &tt_global_entry->orig_list;
1861 hlist_for_each_entry_rcu(orig_entry, head, list) {
1862 if (best_entry == orig_entry)
1865 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1866 tt_common_entry->vid);
1869 " + Cannot retrieve VLAN %d for originator %pM\n",
1870 BATADV_PRINT_VID(tt_common_entry->vid),
1871 orig_entry->orig_node->orig);
1875 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1877 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1878 '+', tt_global_entry->common.addr,
1879 BATADV_PRINT_VID(tt_global_entry->common.vid),
1880 orig_entry->ttvn, orig_entry->orig_node->orig,
1881 last_ttvn, vlan->tt.crc,
1882 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1883 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1884 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1885 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1887 batadv_orig_node_vlan_put(vlan);
1891 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1893 struct net_device *net_dev = (struct net_device *)seq->private;
1894 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1895 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1896 struct batadv_tt_common_entry *tt_common_entry;
1897 struct batadv_tt_global_entry *tt_global;
1898 struct batadv_hard_iface *primary_if;
1899 struct hlist_head *head;
1902 primary_if = batadv_seq_print_text_primary_if_get(seq);
1907 "Globally announced TT entries received via the mesh %s\n",
1910 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1912 for (i = 0; i < hash->size; i++) {
1913 head = &hash->table[i];
1916 hlist_for_each_entry_rcu(tt_common_entry,
1918 tt_global = container_of(tt_common_entry,
1919 struct batadv_tt_global_entry,
1921 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1927 batadv_hardif_put(primary_if);
1933 * batadv_tt_global_dump_subentry - Dump all TT local entries into a message
1934 * @msg: Netlink message to dump into
1935 * @portid: Port making netlink request
1936 * @seq: Sequence number of netlink message
1937 * @common: tt local & tt global common data
1938 * @orig: Originator node announcing a non-mesh client
1939 * @best: Is the best originator for the TT entry
1941 * Return: Error code, or 0 on success
1944 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1945 struct batadv_tt_common_entry *common,
1946 struct batadv_tt_orig_list_entry *orig,
1950 struct batadv_orig_node_vlan *vlan;
1954 vlan = batadv_orig_node_vlan_get(orig->orig_node,
1961 batadv_orig_node_vlan_put(vlan);
1963 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1965 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
1969 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
1971 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1972 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1973 orig->orig_node->orig) ||
1974 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
1975 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
1976 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1977 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1978 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1979 goto nla_put_failure;
1981 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1982 goto nla_put_failure;
1984 genlmsg_end(msg, hdr);
1988 genlmsg_cancel(msg, hdr);
1993 * batadv_tt_global_dump_entry - Dump one TT global entry into a message
1994 * @msg: Netlink message to dump into
1995 * @portid: Port making netlink request
1996 * @seq: Sequence number of netlink message
1997 * @bat_priv: The bat priv with all the soft interface information
1998 * @common: tt local & tt global common data
1999 * @sub_s: Number of entries to skip
2001 * This function assumes the caller holds rcu_read_lock().
2003 * Return: Error code, or 0 on success
2006 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2007 struct batadv_priv *bat_priv,
2008 struct batadv_tt_common_entry *common, int *sub_s)
2010 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2011 struct batadv_tt_global_entry *global;
2012 struct hlist_head *head;
2016 global = container_of(common, struct batadv_tt_global_entry, common);
2017 best_entry = batadv_transtable_best_orig(bat_priv, global);
2018 head = &global->orig_list;
2020 hlist_for_each_entry_rcu(orig_entry, head, list) {
2024 best = (orig_entry == best_entry);
2026 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2027 orig_entry, best)) {
2038 * batadv_tt_global_dump_bucket - Dump one TT local bucket into a message
2039 * @msg: Netlink message to dump into
2040 * @portid: Port making netlink request
2041 * @seq: Sequence number of netlink message
2042 * @bat_priv: The bat priv with all the soft interface information
2043 * @head: Pointer to the list containing the global tt entries
2044 * @idx_s: Number of entries to skip
2045 * @sub: Number of entries to skip
2047 * Return: Error code, or 0 on success
2050 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2051 struct batadv_priv *bat_priv,
2052 struct hlist_head *head, int *idx_s, int *sub)
2054 struct batadv_tt_common_entry *common;
2058 hlist_for_each_entry_rcu(common, head, hash_entry) {
2062 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2077 * batadv_tt_global_dump - Dump TT global entries into a message
2078 * @msg: Netlink message to dump into
2079 * @cb: Parameters from query
2081 * Return: Error code, or length of message on success
2083 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2085 struct net *net = sock_net(cb->skb->sk);
2086 struct net_device *soft_iface;
2087 struct batadv_priv *bat_priv;
2088 struct batadv_hard_iface *primary_if = NULL;
2089 struct batadv_hashtable *hash;
2090 struct hlist_head *head;
2093 int bucket = cb->args[0];
2094 int idx = cb->args[1];
2095 int sub = cb->args[2];
2096 int portid = NETLINK_CB(cb->skb).portid;
2098 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2102 soft_iface = dev_get_by_index(net, ifindex);
2103 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2108 bat_priv = netdev_priv(soft_iface);
2110 primary_if = batadv_primary_if_get_selected(bat_priv);
2111 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2116 hash = bat_priv->tt.global_hash;
2118 while (bucket < hash->size) {
2119 head = &hash->table[bucket];
2121 if (batadv_tt_global_dump_bucket(msg, portid,
2122 cb->nlh->nlmsg_seq, bat_priv,
2133 batadv_hardif_put(primary_if);
2135 dev_put(soft_iface);
2137 cb->args[0] = bucket;
2145 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
2146 * @tt_global_entry: the global entry to remove the orig_entry from
2147 * @orig_entry: the orig entry to remove and free
2149 * Remove an orig_entry from its list in the given tt_global_entry and
2150 * free this orig_entry afterwards.
2152 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2156 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2157 struct batadv_tt_orig_list_entry *orig_entry)
2159 lockdep_assert_held(&tt_global_entry->list_lock);
2161 batadv_tt_global_size_dec(orig_entry->orig_node,
2162 tt_global_entry->common.vid);
2163 atomic_dec(&tt_global_entry->orig_list_count);
2164 /* requires holding tt_global_entry->list_lock and orig_entry->list
2165 * being part of a list
2167 hlist_del_rcu(&orig_entry->list);
2168 batadv_tt_orig_list_entry_put(orig_entry);
2171 /* deletes the orig list of a tt_global_entry */
2173 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2175 struct hlist_head *head;
2176 struct hlist_node *safe;
2177 struct batadv_tt_orig_list_entry *orig_entry;
2179 spin_lock_bh(&tt_global_entry->list_lock);
2180 head = &tt_global_entry->orig_list;
2181 hlist_for_each_entry_safe(orig_entry, safe, head, list)
2182 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2183 spin_unlock_bh(&tt_global_entry->list_lock);
2187 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
2188 * @bat_priv: the bat priv with all the soft interface information
2189 * @tt_global_entry: the global entry to remove the orig_node from
2190 * @orig_node: the originator announcing the client
2191 * @message: message to append to the log on deletion
2193 * Remove the given orig_node and its according orig_entry from the given
2197 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2198 struct batadv_tt_global_entry *tt_global_entry,
2199 struct batadv_orig_node *orig_node,
2200 const char *message)
2202 struct hlist_head *head;
2203 struct hlist_node *safe;
2204 struct batadv_tt_orig_list_entry *orig_entry;
2207 spin_lock_bh(&tt_global_entry->list_lock);
2208 head = &tt_global_entry->orig_list;
2209 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2210 if (orig_entry->orig_node == orig_node) {
2211 vid = tt_global_entry->common.vid;
2212 batadv_dbg(BATADV_DBG_TT, bat_priv,
2213 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2215 tt_global_entry->common.addr,
2216 BATADV_PRINT_VID(vid), message);
2217 _batadv_tt_global_del_orig_entry(tt_global_entry,
2221 spin_unlock_bh(&tt_global_entry->list_lock);
2224 /* If the client is to be deleted, we check if it is the last origantor entry
2225 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2226 * timer, otherwise we simply remove the originator scheduled for deletion.
2229 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2230 struct batadv_tt_global_entry *tt_global_entry,
2231 struct batadv_orig_node *orig_node,
2232 const char *message)
2234 bool last_entry = true;
2235 struct hlist_head *head;
2236 struct batadv_tt_orig_list_entry *orig_entry;
2238 /* no local entry exists, case 1:
2239 * Check if this is the last one or if other entries exist.
2243 head = &tt_global_entry->orig_list;
2244 hlist_for_each_entry_rcu(orig_entry, head, list) {
2245 if (orig_entry->orig_node != orig_node) {
2253 /* its the last one, mark for roaming. */
2254 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2255 tt_global_entry->roam_at = jiffies;
2257 /* there is another entry, we can simply delete this
2258 * one and can still use the other one.
2260 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2261 orig_node, message);
2265 * batadv_tt_global_del - remove a client from the global table
2266 * @bat_priv: the bat priv with all the soft interface information
2267 * @orig_node: an originator serving this client
2268 * @addr: the mac address of the client
2269 * @vid: VLAN identifier
2270 * @message: a message explaining the reason for deleting the client to print
2271 * for debugging purpose
2272 * @roaming: true if the deletion has been triggered by a roaming event
2274 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2275 struct batadv_orig_node *orig_node,
2276 const unsigned char *addr, unsigned short vid,
2277 const char *message, bool roaming)
2279 struct batadv_tt_global_entry *tt_global_entry;
2280 struct batadv_tt_local_entry *local_entry = NULL;
2282 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2283 if (!tt_global_entry)
2287 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2288 orig_node, message);
2290 if (hlist_empty(&tt_global_entry->orig_list))
2291 batadv_tt_global_free(bat_priv, tt_global_entry,
2297 /* if we are deleting a global entry due to a roam
2298 * event, there are two possibilities:
2299 * 1) the client roamed from node A to node B => if there
2300 * is only one originator left for this client, we mark
2301 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2302 * wait for node B to claim it. In case of timeout
2303 * the entry is purged.
2305 * If there are other originators left, we directly delete
2307 * 2) the client roamed to us => we can directly delete
2308 * the global entry, since it is useless now.
2310 local_entry = batadv_tt_local_hash_find(bat_priv,
2311 tt_global_entry->common.addr,
2314 /* local entry exists, case 2: client roamed to us. */
2315 batadv_tt_global_del_orig_list(tt_global_entry);
2316 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2318 /* no local entry exists, case 1: check for roaming */
2319 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2320 orig_node, message);
2323 if (tt_global_entry)
2324 batadv_tt_global_entry_put(tt_global_entry);
2326 batadv_tt_local_entry_put(local_entry);
2330 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
2331 * given originator matching the provided vid
2332 * @bat_priv: the bat priv with all the soft interface information
2333 * @orig_node: the originator owning the entries to remove
2334 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2336 * @message: debug message to print as "reason"
2338 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2339 struct batadv_orig_node *orig_node,
2341 const char *message)
2343 struct batadv_tt_global_entry *tt_global;
2344 struct batadv_tt_common_entry *tt_common_entry;
2346 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2347 struct hlist_node *safe;
2348 struct hlist_head *head;
2349 spinlock_t *list_lock; /* protects write access to the hash lists */
2355 for (i = 0; i < hash->size; i++) {
2356 head = &hash->table[i];
2357 list_lock = &hash->list_locks[i];
2359 spin_lock_bh(list_lock);
2360 hlist_for_each_entry_safe(tt_common_entry, safe,
2362 /* remove only matching entries */
2363 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2366 tt_global = container_of(tt_common_entry,
2367 struct batadv_tt_global_entry,
2370 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2371 orig_node, message);
2373 if (hlist_empty(&tt_global->orig_list)) {
2374 vid = tt_global->common.vid;
2375 batadv_dbg(BATADV_DBG_TT, bat_priv,
2376 "Deleting global tt entry %pM (vid: %d): %s\n",
2377 tt_global->common.addr,
2378 BATADV_PRINT_VID(vid), message);
2379 hlist_del_rcu(&tt_common_entry->hash_entry);
2380 batadv_tt_global_entry_put(tt_global);
2383 spin_unlock_bh(list_lock);
2385 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2388 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2392 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2393 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2395 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2396 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2398 *msg = "Roaming timeout\n";
2401 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2402 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2404 *msg = "Temporary client timeout\n";
2410 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2412 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2413 struct hlist_head *head;
2414 struct hlist_node *node_tmp;
2415 spinlock_t *list_lock; /* protects write access to the hash lists */
2418 struct batadv_tt_common_entry *tt_common;
2419 struct batadv_tt_global_entry *tt_global;
2421 for (i = 0; i < hash->size; i++) {
2422 head = &hash->table[i];
2423 list_lock = &hash->list_locks[i];
2425 spin_lock_bh(list_lock);
2426 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2428 tt_global = container_of(tt_common,
2429 struct batadv_tt_global_entry,
2432 if (!batadv_tt_global_to_purge(tt_global, &msg))
2435 batadv_dbg(BATADV_DBG_TT, bat_priv,
2436 "Deleting global tt entry %pM (vid: %d): %s\n",
2437 tt_global->common.addr,
2438 BATADV_PRINT_VID(tt_global->common.vid),
2441 hlist_del_rcu(&tt_common->hash_entry);
2443 batadv_tt_global_entry_put(tt_global);
2445 spin_unlock_bh(list_lock);
2449 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2451 struct batadv_hashtable *hash;
2452 spinlock_t *list_lock; /* protects write access to the hash lists */
2453 struct batadv_tt_common_entry *tt_common_entry;
2454 struct batadv_tt_global_entry *tt_global;
2455 struct hlist_node *node_tmp;
2456 struct hlist_head *head;
2459 if (!bat_priv->tt.global_hash)
2462 hash = bat_priv->tt.global_hash;
2464 for (i = 0; i < hash->size; i++) {
2465 head = &hash->table[i];
2466 list_lock = &hash->list_locks[i];
2468 spin_lock_bh(list_lock);
2469 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2471 hlist_del_rcu(&tt_common_entry->hash_entry);
2472 tt_global = container_of(tt_common_entry,
2473 struct batadv_tt_global_entry,
2475 batadv_tt_global_entry_put(tt_global);
2477 spin_unlock_bh(list_lock);
2480 batadv_hash_destroy(hash);
2482 bat_priv->tt.global_hash = NULL;
2486 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2487 struct batadv_tt_global_entry *tt_global_entry)
2491 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2492 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2495 /* check if the two clients are marked as isolated */
2496 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2497 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2504 * batadv_transtable_search - get the mesh destination for a given client
2505 * @bat_priv: the bat priv with all the soft interface information
2506 * @src: mac address of the source client
2507 * @addr: mac address of the destination client
2508 * @vid: VLAN identifier
2510 * Return: a pointer to the originator that was selected as destination in the
2511 * mesh for contacting the client 'addr', NULL otherwise.
2512 * In case of multiple originators serving the same client, the function returns
2513 * the best one (best in terms of metric towards the destination node).
2515 * If the two clients are AP isolated the function returns NULL.
2517 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2522 struct batadv_tt_local_entry *tt_local_entry = NULL;
2523 struct batadv_tt_global_entry *tt_global_entry = NULL;
2524 struct batadv_orig_node *orig_node = NULL;
2525 struct batadv_tt_orig_list_entry *best_entry;
2527 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2528 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2529 if (!tt_local_entry ||
2530 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2534 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2535 if (!tt_global_entry)
2538 /* check whether the clients should not communicate due to AP
2541 if (tt_local_entry &&
2542 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2546 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2547 /* found anything? */
2549 orig_node = best_entry->orig_node;
2550 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2555 if (tt_global_entry)
2556 batadv_tt_global_entry_put(tt_global_entry);
2558 batadv_tt_local_entry_put(tt_local_entry);
2564 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2565 * to the given orig_node
2566 * @bat_priv: the bat priv with all the soft interface information
2567 * @orig_node: originator for which the CRC should be computed
2568 * @vid: VLAN identifier for which the CRC32 has to be computed
2570 * This function computes the checksum for the global table corresponding to a
2571 * specific originator. In particular, the checksum is computed as follows: For
2572 * each client connected to the originator the CRC32C of the MAC address and the
2573 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2576 * The idea behind is that CRC32C should be used as much as possible in order to
2577 * produce a unique hash of the table, but since the order which is used to feed
2578 * the CRC32C function affects the result and since every node in the network
2579 * probably sorts the clients differently, the hash function cannot be directly
2580 * computed over the entire table. Hence the CRC32C is used only on
2581 * the single client entry, while all the results are then xor'ed together
2582 * because the XOR operation can combine them all while trying to reduce the
2583 * noise as much as possible.
2585 * Return: the checksum of the global table of a given originator.
2587 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2588 struct batadv_orig_node *orig_node,
2591 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2592 struct batadv_tt_common_entry *tt_common;
2593 struct batadv_tt_global_entry *tt_global;
2594 struct hlist_head *head;
2595 u32 i, crc_tmp, crc = 0;
2599 for (i = 0; i < hash->size; i++) {
2600 head = &hash->table[i];
2603 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2604 tt_global = container_of(tt_common,
2605 struct batadv_tt_global_entry,
2607 /* compute the CRC only for entries belonging to the
2608 * VLAN identified by the vid passed as parameter
2610 if (tt_common->vid != vid)
2613 /* Roaming clients are in the global table for
2614 * consistency only. They don't have to be
2615 * taken into account while computing the
2618 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2620 /* Temporary clients have not been announced yet, so
2621 * they have to be skipped while computing the global
2624 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2627 /* find out if this global entry is announced by this
2630 if (!batadv_tt_global_entry_has_orig(tt_global,
2634 /* use network order to read the VID: this ensures that
2635 * every node reads the bytes in the same order.
2637 tmp_vid = htons(tt_common->vid);
2638 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2640 /* compute the CRC on flags that have to be kept in sync
2643 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2644 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2646 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2655 * batadv_tt_local_crc - calculates the checksum of the local table
2656 * @bat_priv: the bat priv with all the soft interface information
2657 * @vid: VLAN identifier for which the CRC32 has to be computed
2659 * For details about the computation, please refer to the documentation for
2660 * batadv_tt_global_crc().
2662 * Return: the checksum of the local table
2664 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2667 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2668 struct batadv_tt_common_entry *tt_common;
2669 struct hlist_head *head;
2670 u32 i, crc_tmp, crc = 0;
2674 for (i = 0; i < hash->size; i++) {
2675 head = &hash->table[i];
2678 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2679 /* compute the CRC only for entries belonging to the
2680 * VLAN identified by vid
2682 if (tt_common->vid != vid)
2685 /* not yet committed clients have not to be taken into
2686 * account while computing the CRC
2688 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2691 /* use network order to read the VID: this ensures that
2692 * every node reads the bytes in the same order.
2694 tmp_vid = htons(tt_common->vid);
2695 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2697 /* compute the CRC on flags that have to be kept in sync
2700 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2701 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2703 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2712 * batadv_tt_req_node_release - free tt_req node entry
2713 * @ref: kref pointer of the tt req_node entry
2715 static void batadv_tt_req_node_release(struct kref *ref)
2717 struct batadv_tt_req_node *tt_req_node;
2719 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2721 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2725 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2726 * possibly release it
2727 * @tt_req_node: tt_req_node to be free'd
2729 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2731 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2734 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2736 struct batadv_tt_req_node *node;
2737 struct hlist_node *safe;
2739 spin_lock_bh(&bat_priv->tt.req_list_lock);
2741 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2742 hlist_del_init(&node->list);
2743 batadv_tt_req_node_put(node);
2746 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2749 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2750 struct batadv_orig_node *orig_node,
2751 const void *tt_buff,
2754 /* Replace the old buffer only if I received something in the
2755 * last OGM (the OGM could carry no changes)
2757 spin_lock_bh(&orig_node->tt_buff_lock);
2758 if (tt_buff_len > 0) {
2759 kfree(orig_node->tt_buff);
2760 orig_node->tt_buff_len = 0;
2761 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2762 if (orig_node->tt_buff) {
2763 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2764 orig_node->tt_buff_len = tt_buff_len;
2767 spin_unlock_bh(&orig_node->tt_buff_lock);
2770 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2772 struct batadv_tt_req_node *node;
2773 struct hlist_node *safe;
2775 spin_lock_bh(&bat_priv->tt.req_list_lock);
2776 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2777 if (batadv_has_timed_out(node->issued_at,
2778 BATADV_TT_REQUEST_TIMEOUT)) {
2779 hlist_del_init(&node->list);
2780 batadv_tt_req_node_put(node);
2783 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2787 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2788 * @bat_priv: the bat priv with all the soft interface information
2789 * @orig_node: orig node this request is being issued for
2791 * Return: the pointer to the new tt_req_node struct if no request
2792 * has already been issued for this orig_node, NULL otherwise.
2794 static struct batadv_tt_req_node *
2795 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2796 struct batadv_orig_node *orig_node)
2798 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2800 spin_lock_bh(&bat_priv->tt.req_list_lock);
2801 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2802 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2803 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2804 BATADV_TT_REQUEST_TIMEOUT))
2808 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2812 kref_init(&tt_req_node->refcount);
2813 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2814 tt_req_node->issued_at = jiffies;
2816 kref_get(&tt_req_node->refcount);
2817 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2819 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2824 * batadv_tt_local_valid - verify that given tt entry is a valid one
2825 * @entry_ptr: to be checked local tt entry
2826 * @data_ptr: not used but definition required to satisfy the callback prototype
2828 * Return: true if the entry is a valid, false otherwise.
2830 static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2832 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2834 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2839 static bool batadv_tt_global_valid(const void *entry_ptr,
2840 const void *data_ptr)
2842 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2843 const struct batadv_tt_global_entry *tt_global_entry;
2844 const struct batadv_orig_node *orig_node = data_ptr;
2846 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2847 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2850 tt_global_entry = container_of(tt_common_entry,
2851 struct batadv_tt_global_entry,
2854 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2858 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2860 * @bat_priv: the bat priv with all the soft interface information
2861 * @hash: hash table containing the tt entries
2862 * @tt_len: expected tvlv tt data buffer length in number of bytes
2863 * @tvlv_buff: pointer to the buffer to fill with the TT data
2864 * @valid_cb: function to filter tt change entries
2865 * @cb_data: data passed to the filter function as argument
2867 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2868 struct batadv_hashtable *hash,
2869 void *tvlv_buff, u16 tt_len,
2870 bool (*valid_cb)(const void *,
2874 struct batadv_tt_common_entry *tt_common_entry;
2875 struct batadv_tvlv_tt_change *tt_change;
2876 struct hlist_head *head;
2877 u16 tt_tot, tt_num_entries = 0;
2880 tt_tot = batadv_tt_entries(tt_len);
2881 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2884 for (i = 0; i < hash->size; i++) {
2885 head = &hash->table[i];
2887 hlist_for_each_entry_rcu(tt_common_entry,
2889 if (tt_tot == tt_num_entries)
2892 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2895 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2896 tt_change->flags = tt_common_entry->flags;
2897 tt_change->vid = htons(tt_common_entry->vid);
2898 memset(tt_change->reserved, 0,
2899 sizeof(tt_change->reserved));
2909 * batadv_tt_global_check_crc - check if all the CRCs are correct
2910 * @orig_node: originator for which the CRCs have to be checked
2911 * @tt_vlan: pointer to the first tvlv VLAN entry
2912 * @num_vlan: number of tvlv VLAN entries
2914 * Return: true if all the received CRCs match the locally stored ones, false
2917 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2918 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2921 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2922 struct batadv_orig_node_vlan *vlan;
2923 int i, orig_num_vlan;
2926 /* check if each received CRC matches the locally stored one */
2927 for (i = 0; i < num_vlan; i++) {
2928 tt_vlan_tmp = tt_vlan + i;
2930 /* if orig_node is a backbone node for this VLAN, don't check
2931 * the CRC as we ignore all the global entries over it
2933 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2935 ntohs(tt_vlan_tmp->vid)))
2938 vlan = batadv_orig_node_vlan_get(orig_node,
2939 ntohs(tt_vlan_tmp->vid));
2944 batadv_orig_node_vlan_put(vlan);
2946 if (crc != ntohl(tt_vlan_tmp->crc))
2950 /* check if any excess VLANs exist locally for the originator
2951 * which are not mentioned in the TVLV from the originator.
2955 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2959 if (orig_num_vlan > num_vlan)
2966 * batadv_tt_local_update_crc - update all the local CRCs
2967 * @bat_priv: the bat priv with all the soft interface information
2969 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2971 struct batadv_softif_vlan *vlan;
2973 /* recompute the global CRC for each VLAN */
2975 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2976 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2982 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2983 * @bat_priv: the bat priv with all the soft interface information
2984 * @orig_node: the orig_node for which the CRCs have to be updated
2986 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2987 struct batadv_orig_node *orig_node)
2989 struct batadv_orig_node_vlan *vlan;
2992 /* recompute the global CRC for each VLAN */
2994 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2995 /* if orig_node is a backbone node for this VLAN, don't compute
2996 * the CRC as we ignore all the global entries over it
2998 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3002 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3009 * batadv_send_tt_request - send a TT Request message to a given node
3010 * @bat_priv: the bat priv with all the soft interface information
3011 * @dst_orig_node: the destination of the message
3012 * @ttvn: the version number that the source of the message is looking for
3013 * @tt_vlan: pointer to the first tvlv VLAN object to request
3014 * @num_vlan: number of tvlv VLAN entries
3015 * @full_table: ask for the entire translation table if true, while only for the
3016 * last TT diff otherwise
3018 * Return: true if the TT Request was sent, false otherwise
3020 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3021 struct batadv_orig_node *dst_orig_node,
3023 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3024 u16 num_vlan, bool full_table)
3026 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3027 struct batadv_tt_req_node *tt_req_node = NULL;
3028 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3029 struct batadv_hard_iface *primary_if;
3033 primary_if = batadv_primary_if_get_selected(bat_priv);
3037 /* The new tt_req will be issued only if I'm not waiting for a
3038 * reply from the same orig_node yet
3040 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
3044 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3045 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
3049 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3050 tvlv_tt_data->ttvn = ttvn;
3051 tvlv_tt_data->num_vlan = htons(num_vlan);
3053 /* send all the CRCs within the request. This is needed by intermediate
3054 * nodes to ensure they have the correct table before replying
3056 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3057 for (i = 0; i < num_vlan; i++) {
3058 tt_vlan_req->vid = tt_vlan->vid;
3059 tt_vlan_req->crc = tt_vlan->crc;
3066 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3068 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
3069 dst_orig_node->orig, full_table ? 'F' : '.');
3071 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
3072 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3073 dst_orig_node->orig, BATADV_TVLV_TT, 1,
3074 tvlv_tt_data, size);
3079 batadv_hardif_put(primary_if);
3081 if (ret && tt_req_node) {
3082 spin_lock_bh(&bat_priv->tt.req_list_lock);
3083 if (!hlist_unhashed(&tt_req_node->list)) {
3084 hlist_del_init(&tt_req_node->list);
3085 batadv_tt_req_node_put(tt_req_node);
3087 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3091 batadv_tt_req_node_put(tt_req_node);
3093 kfree(tvlv_tt_data);
3098 * batadv_send_other_tt_response - send reply to tt request concerning another
3099 * node's translation table
3100 * @bat_priv: the bat priv with all the soft interface information
3101 * @tt_data: tt data containing the tt request information
3102 * @req_src: mac address of tt request sender
3103 * @req_dst: mac address of tt request recipient
3105 * Return: true if tt request reply was sent, false otherwise.
3107 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3108 struct batadv_tvlv_tt_data *tt_data,
3109 u8 *req_src, u8 *req_dst)
3111 struct batadv_orig_node *req_dst_orig_node;
3112 struct batadv_orig_node *res_dst_orig_node = NULL;
3113 struct batadv_tvlv_tt_change *tt_change;
3114 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3115 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3116 bool ret = false, full_table;
3117 u8 orig_ttvn, req_ttvn;
3121 batadv_dbg(BATADV_DBG_TT, bat_priv,
3122 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3123 req_src, tt_data->ttvn, req_dst,
3124 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3126 /* Let's get the orig node of the REAL destination */
3127 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3128 if (!req_dst_orig_node)
3131 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3132 if (!res_dst_orig_node)
3135 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3136 req_ttvn = tt_data->ttvn;
3138 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3139 /* this node doesn't have the requested data */
3140 if (orig_ttvn != req_ttvn ||
3141 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3142 ntohs(tt_data->num_vlan)))
3145 /* If the full table has been explicitly requested */
3146 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3147 !req_dst_orig_node->tt_buff)
3152 /* TT fragmentation hasn't been implemented yet, so send as many
3153 * TT entries fit a single packet as possible only
3156 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3157 tt_len = req_dst_orig_node->tt_buff_len;
3159 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3166 /* Copy the last orig_node's OGM buffer */
3167 memcpy(tt_change, req_dst_orig_node->tt_buff,
3168 req_dst_orig_node->tt_buff_len);
3169 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3171 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3172 * in the initial part
3175 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3182 /* fill the rest of the tvlv with the real TT entries */
3183 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3185 batadv_tt_global_valid,
3189 /* Don't send the response, if larger than fragmented packet. */
3190 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3191 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3192 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3193 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3194 res_dst_orig_node->orig);
3198 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3199 tvlv_tt_data->ttvn = req_ttvn;
3202 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3204 batadv_dbg(BATADV_DBG_TT, bat_priv,
3205 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3206 res_dst_orig_node->orig, req_dst_orig_node->orig,
3207 full_table ? 'F' : '.', req_ttvn);
3209 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3211 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3212 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3219 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3222 if (res_dst_orig_node)
3223 batadv_orig_node_put(res_dst_orig_node);
3224 if (req_dst_orig_node)
3225 batadv_orig_node_put(req_dst_orig_node);
3226 kfree(tvlv_tt_data);
3231 * batadv_send_my_tt_response - send reply to tt request concerning this node's
3233 * @bat_priv: the bat priv with all the soft interface information
3234 * @tt_data: tt data containing the tt request information
3235 * @req_src: mac address of tt request sender
3237 * Return: true if tt request reply was sent, false otherwise.
3239 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3240 struct batadv_tvlv_tt_data *tt_data,
3243 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3244 struct batadv_hard_iface *primary_if = NULL;
3245 struct batadv_tvlv_tt_change *tt_change;
3246 struct batadv_orig_node *orig_node;
3247 u8 my_ttvn, req_ttvn;
3252 batadv_dbg(BATADV_DBG_TT, bat_priv,
3253 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3254 req_src, tt_data->ttvn,
3255 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3257 spin_lock_bh(&bat_priv->tt.commit_lock);
3259 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3260 req_ttvn = tt_data->ttvn;
3262 orig_node = batadv_orig_hash_find(bat_priv, req_src);
3266 primary_if = batadv_primary_if_get_selected(bat_priv);
3270 /* If the full table has been explicitly requested or the gap
3271 * is too big send the whole local translation table
3273 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3274 !bat_priv->tt.last_changeset)
3279 /* TT fragmentation hasn't been implemented yet, so send as many
3280 * TT entries fit a single packet as possible only
3283 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3285 tt_len = bat_priv->tt.last_changeset_len;
3286 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3290 if (!tt_len || !tvlv_len)
3293 /* Copy the last orig_node's OGM buffer */
3294 memcpy(tt_change, bat_priv->tt.last_changeset,
3295 bat_priv->tt.last_changeset_len);
3296 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3298 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3300 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3301 * in the initial part
3304 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3308 if (!tt_len || !tvlv_len)
3311 /* fill the rest of the tvlv with the real TT entries */
3312 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3314 batadv_tt_local_valid, NULL);
3317 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3318 tvlv_tt_data->ttvn = req_ttvn;
3321 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3323 batadv_dbg(BATADV_DBG_TT, bat_priv,
3324 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3325 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3327 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3329 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3330 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3336 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3338 spin_unlock_bh(&bat_priv->tt.commit_lock);
3340 batadv_orig_node_put(orig_node);
3342 batadv_hardif_put(primary_if);
3343 kfree(tvlv_tt_data);
3344 /* The packet was for this host, so it doesn't need to be re-routed */
3349 * batadv_send_tt_response - send reply to tt request
3350 * @bat_priv: the bat priv with all the soft interface information
3351 * @tt_data: tt data containing the tt request information
3352 * @req_src: mac address of tt request sender
3353 * @req_dst: mac address of tt request recipient
3355 * Return: true if tt request reply was sent, false otherwise.
3357 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3358 struct batadv_tvlv_tt_data *tt_data,
3359 u8 *req_src, u8 *req_dst)
3361 if (batadv_is_my_mac(bat_priv, req_dst))
3362 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3363 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3367 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3368 struct batadv_orig_node *orig_node,
3369 struct batadv_tvlv_tt_change *tt_change,
3370 u16 tt_num_changes, u8 ttvn)
3375 for (i = 0; i < tt_num_changes; i++) {
3376 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3377 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3378 batadv_tt_global_del(bat_priv, orig_node,
3379 (tt_change + i)->addr,
3380 ntohs((tt_change + i)->vid),
3381 "tt removed by changes",
3384 if (!batadv_tt_global_add(bat_priv, orig_node,
3385 (tt_change + i)->addr,
3386 ntohs((tt_change + i)->vid),
3387 (tt_change + i)->flags, ttvn))
3388 /* In case of problem while storing a
3389 * global_entry, we stop the updating
3390 * procedure without committing the
3391 * ttvn change. This will avoid to send
3392 * corrupted data on tt_request
3397 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3400 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3401 struct batadv_tvlv_tt_change *tt_change,
3402 u8 ttvn, u8 *resp_src,
3405 struct batadv_orig_node *orig_node;
3407 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3411 /* Purge the old table first.. */
3412 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3413 "Received full table");
3415 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3418 spin_lock_bh(&orig_node->tt_buff_lock);
3419 kfree(orig_node->tt_buff);
3420 orig_node->tt_buff_len = 0;
3421 orig_node->tt_buff = NULL;
3422 spin_unlock_bh(&orig_node->tt_buff_lock);
3424 atomic_set(&orig_node->last_ttvn, ttvn);
3428 batadv_orig_node_put(orig_node);
3431 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3432 struct batadv_orig_node *orig_node,
3433 u16 tt_num_changes, u8 ttvn,
3434 struct batadv_tvlv_tt_change *tt_change)
3436 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3437 tt_num_changes, ttvn);
3439 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3440 batadv_tt_len(tt_num_changes));
3441 atomic_set(&orig_node->last_ttvn, ttvn);
3445 * batadv_is_my_client - check if a client is served by the local node
3446 * @bat_priv: the bat priv with all the soft interface information
3447 * @addr: the mac address of the client to check
3448 * @vid: VLAN identifier
3450 * Return: true if the client is served by this node, false otherwise.
3452 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3455 struct batadv_tt_local_entry *tt_local_entry;
3458 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3459 if (!tt_local_entry)
3461 /* Check if the client has been logically deleted (but is kept for
3462 * consistency purpose)
3464 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3465 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3470 batadv_tt_local_entry_put(tt_local_entry);
3475 * batadv_handle_tt_response - process incoming tt reply
3476 * @bat_priv: the bat priv with all the soft interface information
3477 * @tt_data: tt data containing the tt request information
3478 * @resp_src: mac address of tt reply sender
3479 * @num_entries: number of tt change entries appended to the tt data
3481 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3482 struct batadv_tvlv_tt_data *tt_data,
3483 u8 *resp_src, u16 num_entries)
3485 struct batadv_tt_req_node *node;
3486 struct hlist_node *safe;
3487 struct batadv_orig_node *orig_node = NULL;
3488 struct batadv_tvlv_tt_change *tt_change;
3489 u8 *tvlv_ptr = (u8 *)tt_data;
3492 batadv_dbg(BATADV_DBG_TT, bat_priv,
3493 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3494 resp_src, tt_data->ttvn, num_entries,
3495 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3497 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3501 spin_lock_bh(&orig_node->tt_lock);
3503 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3504 change_offset *= ntohs(tt_data->num_vlan);
3505 change_offset += sizeof(*tt_data);
3506 tvlv_ptr += change_offset;
3508 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3509 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3510 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3511 resp_src, num_entries);
3513 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3514 tt_data->ttvn, tt_change);
3517 /* Recalculate the CRC for this orig_node and store it */
3518 batadv_tt_global_update_crc(bat_priv, orig_node);
3520 spin_unlock_bh(&orig_node->tt_lock);
3522 /* Delete the tt_req_node from pending tt_requests list */
3523 spin_lock_bh(&bat_priv->tt.req_list_lock);
3524 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3525 if (!batadv_compare_eth(node->addr, resp_src))
3527 hlist_del_init(&node->list);
3528 batadv_tt_req_node_put(node);
3531 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3534 batadv_orig_node_put(orig_node);
3537 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3539 struct batadv_tt_roam_node *node, *safe;
3541 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3543 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3544 list_del(&node->list);
3545 kmem_cache_free(batadv_tt_roam_cache, node);
3548 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3551 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3553 struct batadv_tt_roam_node *node, *safe;
3555 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3556 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3557 if (!batadv_has_timed_out(node->first_time,
3558 BATADV_ROAMING_MAX_TIME))
3561 list_del(&node->list);
3562 kmem_cache_free(batadv_tt_roam_cache, node);
3564 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3568 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3569 * @bat_priv: the bat priv with all the soft interface information
3570 * @client: mac address of the roaming client
3572 * This function checks whether the client already reached the
3573 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3576 * Return: true if the ROAMING_ADV can be sent, false otherwise
3578 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3580 struct batadv_tt_roam_node *tt_roam_node;
3583 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3584 /* The new tt_req will be issued only if I'm not waiting for a
3585 * reply from the same orig_node yet
3587 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3588 if (!batadv_compare_eth(tt_roam_node->addr, client))
3591 if (batadv_has_timed_out(tt_roam_node->first_time,
3592 BATADV_ROAMING_MAX_TIME))
3595 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3596 /* Sorry, you roamed too many times! */
3603 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3608 tt_roam_node->first_time = jiffies;
3609 atomic_set(&tt_roam_node->counter,
3610 BATADV_ROAMING_MAX_COUNT - 1);
3611 ether_addr_copy(tt_roam_node->addr, client);
3613 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3618 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3623 * batadv_send_roam_adv - send a roaming advertisement message
3624 * @bat_priv: the bat priv with all the soft interface information
3625 * @client: mac address of the roaming client
3626 * @vid: VLAN identifier
3627 * @orig_node: message destination
3629 * Send a ROAMING_ADV message to the node which was previously serving this
3630 * client. This is done to inform the node that from now on all traffic destined
3631 * for this particular roamed client has to be forwarded to the sender of the
3634 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3636 struct batadv_orig_node *orig_node)
3638 struct batadv_hard_iface *primary_if;
3639 struct batadv_tvlv_roam_adv tvlv_roam;
3641 primary_if = batadv_primary_if_get_selected(bat_priv);
3645 /* before going on we have to check whether the client has
3646 * already roamed to us too many times
3648 if (!batadv_tt_check_roam_count(bat_priv, client))
3651 batadv_dbg(BATADV_DBG_TT, bat_priv,
3652 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3653 orig_node->orig, client, BATADV_PRINT_VID(vid));
3655 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3657 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3658 tvlv_roam.vid = htons(vid);
3660 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3661 orig_node->orig, BATADV_TVLV_ROAM, 1,
3662 &tvlv_roam, sizeof(tvlv_roam));
3666 batadv_hardif_put(primary_if);
3669 static void batadv_tt_purge(struct work_struct *work)
3671 struct delayed_work *delayed_work;
3672 struct batadv_priv_tt *priv_tt;
3673 struct batadv_priv *bat_priv;
3675 delayed_work = to_delayed_work(work);
3676 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3677 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3679 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3680 batadv_tt_global_purge(bat_priv);
3681 batadv_tt_req_purge(bat_priv);
3682 batadv_tt_roam_purge(bat_priv);
3684 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3685 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3688 void batadv_tt_free(struct batadv_priv *bat_priv)
3690 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3691 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3693 cancel_delayed_work_sync(&bat_priv->tt.work);
3695 batadv_tt_local_table_free(bat_priv);
3696 batadv_tt_global_table_free(bat_priv);
3697 batadv_tt_req_list_free(bat_priv);
3698 batadv_tt_changes_list_free(bat_priv);
3699 batadv_tt_roam_list_free(bat_priv);
3701 kfree(bat_priv->tt.last_changeset);
3705 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3706 * table and possibly count them in the TT size
3707 * @bat_priv: the bat priv with all the soft interface information
3708 * @flags: the flag to switch
3709 * @enable: whether to set or unset the flag
3710 * @count: whether to increase the TT size by the number of changed entries
3712 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3713 bool enable, bool count)
3715 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3716 struct batadv_tt_common_entry *tt_common_entry;
3717 struct hlist_head *head;
3723 for (i = 0; i < hash->size; i++) {
3724 head = &hash->table[i];
3727 hlist_for_each_entry_rcu(tt_common_entry,
3730 if ((tt_common_entry->flags & flags) == flags)
3732 tt_common_entry->flags |= flags;
3734 if (!(tt_common_entry->flags & flags))
3736 tt_common_entry->flags &= ~flags;
3742 batadv_tt_local_size_inc(bat_priv,
3743 tt_common_entry->vid);
3749 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3750 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3752 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3753 struct batadv_tt_common_entry *tt_common;
3754 struct batadv_tt_local_entry *tt_local;
3755 struct hlist_node *node_tmp;
3756 struct hlist_head *head;
3757 spinlock_t *list_lock; /* protects write access to the hash lists */
3763 for (i = 0; i < hash->size; i++) {
3764 head = &hash->table[i];
3765 list_lock = &hash->list_locks[i];
3767 spin_lock_bh(list_lock);
3768 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3770 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3773 batadv_dbg(BATADV_DBG_TT, bat_priv,
3774 "Deleting local tt entry (%pM, vid: %d): pending\n",
3776 BATADV_PRINT_VID(tt_common->vid));
3778 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3779 hlist_del_rcu(&tt_common->hash_entry);
3780 tt_local = container_of(tt_common,
3781 struct batadv_tt_local_entry,
3784 batadv_tt_local_entry_put(tt_local);
3786 spin_unlock_bh(list_lock);
3791 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3792 * which have been queued in the time since the last commit
3793 * @bat_priv: the bat priv with all the soft interface information
3795 * Caller must hold tt->commit_lock.
3797 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3799 lockdep_assert_held(&bat_priv->tt.commit_lock);
3801 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3802 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3803 batadv_tt_tvlv_container_update(bat_priv);
3807 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3809 batadv_tt_local_purge_pending_clients(bat_priv);
3810 batadv_tt_local_update_crc(bat_priv);
3812 /* Increment the TTVN only once per OGM interval */
3813 atomic_inc(&bat_priv->tt.vn);
3814 batadv_dbg(BATADV_DBG_TT, bat_priv,
3815 "Local changes committed, updating to ttvn %u\n",
3816 (u8)atomic_read(&bat_priv->tt.vn));
3818 /* reset the sending counter */
3819 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3820 batadv_tt_tvlv_container_update(bat_priv);
3824 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3825 * have been queued in the time since the last commit
3826 * @bat_priv: the bat priv with all the soft interface information
3828 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3830 spin_lock_bh(&bat_priv->tt.commit_lock);
3831 batadv_tt_local_commit_changes_nolock(bat_priv);
3832 spin_unlock_bh(&bat_priv->tt.commit_lock);
3835 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3838 struct batadv_tt_local_entry *tt_local_entry;
3839 struct batadv_tt_global_entry *tt_global_entry;
3840 struct batadv_softif_vlan *vlan;
3843 vlan = batadv_softif_vlan_get(bat_priv, vid);
3847 if (!atomic_read(&vlan->ap_isolation))
3850 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3851 if (!tt_local_entry)
3854 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3855 if (!tt_global_entry)
3856 goto local_entry_put;
3858 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3861 batadv_tt_global_entry_put(tt_global_entry);
3863 batadv_tt_local_entry_put(tt_local_entry);
3865 batadv_softif_vlan_put(vlan);
3870 * batadv_tt_update_orig - update global translation table with new tt
3871 * information received via ogms
3872 * @bat_priv: the bat priv with all the soft interface information
3873 * @orig_node: the orig_node of the ogm
3874 * @tt_buff: pointer to the first tvlv VLAN entry
3875 * @tt_num_vlan: number of tvlv VLAN entries
3876 * @tt_change: pointer to the first entry in the TT buffer
3877 * @tt_num_changes: number of tt changes inside the tt buffer
3878 * @ttvn: translation table version number of this changeset
3880 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3881 struct batadv_orig_node *orig_node,
3882 const void *tt_buff, u16 tt_num_vlan,
3883 struct batadv_tvlv_tt_change *tt_change,
3884 u16 tt_num_changes, u8 ttvn)
3886 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3887 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3888 bool full_table = true;
3891 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3892 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3893 &orig_node->capa_initialized);
3895 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3896 * increased by one -> we can apply the attached changes
3898 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3899 /* the OGM could not contain the changes due to their size or
3900 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3902 * In this case send a tt request
3904 if (!tt_num_changes) {
3909 spin_lock_bh(&orig_node->tt_lock);
3911 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3914 /* Even if we received the precomputed crc with the OGM, we
3915 * prefer to recompute it to spot any possible inconsistency
3916 * in the global table
3918 batadv_tt_global_update_crc(bat_priv, orig_node);
3920 spin_unlock_bh(&orig_node->tt_lock);
3922 /* The ttvn alone is not enough to guarantee consistency
3923 * because a single value could represent different states
3924 * (due to the wrap around). Thus a node has to check whether
3925 * the resulting table (after applying the changes) is still
3926 * consistent or not. E.g. a node could disconnect while its
3927 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3928 * checking the CRC value is mandatory to detect the
3931 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3935 /* if we missed more than one change or our tables are not
3936 * in sync anymore -> request fresh tt data
3938 if (!has_tt_init || ttvn != orig_ttvn ||
3939 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3942 batadv_dbg(BATADV_DBG_TT, bat_priv,
3943 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3944 orig_node->orig, ttvn, orig_ttvn,
3946 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3947 tt_vlan, tt_num_vlan,
3955 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3956 * @bat_priv: the bat priv with all the soft interface information
3957 * @addr: the mac address of the client to check
3958 * @vid: VLAN identifier
3960 * Return: true if we know that the client has moved from its old originator
3961 * to another one. This entry is still kept for consistency purposes and will be
3962 * deleted later by a DEL or because of timeout
3964 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3965 u8 *addr, unsigned short vid)
3967 struct batadv_tt_global_entry *tt_global_entry;
3970 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3971 if (!tt_global_entry)
3974 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3975 batadv_tt_global_entry_put(tt_global_entry);
3981 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3982 * @bat_priv: the bat priv with all the soft interface information
3983 * @addr: the mac address of the local client to query
3984 * @vid: VLAN identifier
3986 * Return: true if the local client is known to be roaming (it is not served by
3987 * this node anymore) or not. If yes, the client is still present in the table
3988 * to keep the latter consistent with the node TTVN
3990 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3991 u8 *addr, unsigned short vid)
3993 struct batadv_tt_local_entry *tt_local_entry;
3996 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3997 if (!tt_local_entry)
4000 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4001 batadv_tt_local_entry_put(tt_local_entry);
4006 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4007 struct batadv_orig_node *orig_node,
4008 const unsigned char *addr,
4013 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
4014 BATADV_TT_CLIENT_TEMP,
4015 atomic_read(&orig_node->last_ttvn)))
4018 batadv_dbg(BATADV_DBG_TT, bat_priv,
4019 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
4020 addr, BATADV_PRINT_VID(vid), orig_node->orig);
4027 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
4028 * maximum packet size that can be transported through the mesh
4029 * @soft_iface: netdev struct of the mesh interface
4031 * Remove entries older than 'timeout' and half timeout if more entries need
4034 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4036 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4037 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4038 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4039 bool reduced = false;
4041 spin_lock_bh(&bat_priv->tt.commit_lock);
4044 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4045 if (packet_size_max >= table_size)
4048 batadv_tt_local_purge(bat_priv, timeout);
4049 batadv_tt_local_purge_pending_clients(bat_priv);
4053 net_ratelimited_function(batadv_info, soft_iface,
4054 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4058 /* commit these changes immediately, to avoid synchronization problem
4062 batadv_tt_local_commit_changes_nolock(bat_priv);
4064 spin_unlock_bh(&bat_priv->tt.commit_lock);
4068 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
4069 * @bat_priv: the bat priv with all the soft interface information
4070 * @orig: the orig_node of the ogm
4071 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4072 * @tvlv_value: tvlv buffer containing the gateway data
4073 * @tvlv_value_len: tvlv buffer length
4075 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4076 struct batadv_orig_node *orig,
4077 u8 flags, void *tvlv_value,
4080 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4081 struct batadv_tvlv_tt_change *tt_change;
4082 struct batadv_tvlv_tt_data *tt_data;
4083 u16 num_entries, num_vlan;
4085 if (tvlv_value_len < sizeof(*tt_data))
4088 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4089 tvlv_value_len -= sizeof(*tt_data);
4091 num_vlan = ntohs(tt_data->num_vlan);
4093 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4096 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4097 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4098 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4100 num_entries = batadv_tt_entries(tvlv_value_len);
4102 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4103 num_entries, tt_data->ttvn);
4107 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
4109 * @bat_priv: the bat priv with all the soft interface information
4110 * @src: mac address of tt tvlv sender
4111 * @dst: mac address of tt tvlv recipient
4112 * @tvlv_value: tvlv buffer containing the tt data
4113 * @tvlv_value_len: tvlv buffer length
4115 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4118 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4123 struct batadv_tvlv_tt_data *tt_data;
4124 u16 tt_vlan_len, tt_num_entries;
4128 if (tvlv_value_len < sizeof(*tt_data))
4129 return NET_RX_SUCCESS;
4131 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4132 tvlv_value_len -= sizeof(*tt_data);
4134 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4135 tt_vlan_len *= ntohs(tt_data->num_vlan);
4137 if (tvlv_value_len < tt_vlan_len)
4138 return NET_RX_SUCCESS;
4140 tvlv_value_len -= tt_vlan_len;
4141 tt_num_entries = batadv_tt_entries(tvlv_value_len);
4143 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4144 case BATADV_TT_REQUEST:
4145 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4147 /* If this node cannot provide a TT response the tt_request is
4150 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4152 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4157 batadv_dbg(BATADV_DBG_TT, bat_priv,
4158 "Routing TT_REQUEST to %pM [%c]\n",
4160 /* tvlv API will re-route the packet */
4164 case BATADV_TT_RESPONSE:
4165 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4167 if (batadv_is_my_mac(bat_priv, dst)) {
4168 batadv_handle_tt_response(bat_priv, tt_data,
4169 src, tt_num_entries);
4170 return NET_RX_SUCCESS;
4173 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4178 batadv_dbg(BATADV_DBG_TT, bat_priv,
4179 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4181 /* tvlv API will re-route the packet */
4185 return NET_RX_SUCCESS;
4189 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
4190 * @bat_priv: the bat priv with all the soft interface information
4191 * @src: mac address of tt tvlv sender
4192 * @dst: mac address of tt tvlv recipient
4193 * @tvlv_value: tvlv buffer containing the tt data
4194 * @tvlv_value_len: tvlv buffer length
4196 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4199 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4204 struct batadv_tvlv_roam_adv *roaming_adv;
4205 struct batadv_orig_node *orig_node = NULL;
4207 /* If this node is not the intended recipient of the
4208 * roaming advertisement the packet is forwarded
4209 * (the tvlv API will re-route the packet).
4211 if (!batadv_is_my_mac(bat_priv, dst))
4214 if (tvlv_value_len < sizeof(*roaming_adv))
4217 orig_node = batadv_orig_hash_find(bat_priv, src);
4221 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4222 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4224 batadv_dbg(BATADV_DBG_TT, bat_priv,
4225 "Received ROAMING_ADV from %pM (client %pM)\n",
4226 src, roaming_adv->client);
4228 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4229 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4230 atomic_read(&orig_node->last_ttvn) + 1);
4234 batadv_orig_node_put(orig_node);
4235 return NET_RX_SUCCESS;
4239 * batadv_tt_init - initialise the translation table internals
4240 * @bat_priv: the bat priv with all the soft interface information
4242 * Return: 0 on success or negative error number in case of failure.
4244 int batadv_tt_init(struct batadv_priv *bat_priv)
4248 /* synchronized flags must be remote */
4249 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4251 ret = batadv_tt_local_init(bat_priv);
4255 ret = batadv_tt_global_init(bat_priv);
4259 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4260 batadv_tt_tvlv_unicast_handler_v1,
4261 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4263 batadv_tvlv_handler_register(bat_priv, NULL,
4264 batadv_roam_tvlv_unicast_handler_v1,
4265 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4267 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4268 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4269 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4275 * batadv_tt_global_is_isolated - check if a client is marked as isolated
4276 * @bat_priv: the bat priv with all the soft interface information
4277 * @addr: the mac address of the client
4278 * @vid: the identifier of the VLAN where this client is connected
4280 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4283 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4284 const u8 *addr, unsigned short vid)
4286 struct batadv_tt_global_entry *tt;
4289 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4293 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4295 batadv_tt_global_entry_put(tt);
4301 * batadv_tt_cache_init - Initialize tt memory object cache
4303 * Return: 0 on success or negative error number in case of failure.
4305 int __init batadv_tt_cache_init(void)
4307 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4308 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4309 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4310 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4311 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4312 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4314 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4315 SLAB_HWCACHE_ALIGN, NULL);
4316 if (!batadv_tl_cache)
4319 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4320 SLAB_HWCACHE_ALIGN, NULL);
4321 if (!batadv_tg_cache)
4322 goto err_tt_tl_destroy;
4324 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4326 SLAB_HWCACHE_ALIGN, NULL);
4327 if (!batadv_tt_orig_cache)
4328 goto err_tt_tg_destroy;
4330 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4332 SLAB_HWCACHE_ALIGN, NULL);
4333 if (!batadv_tt_change_cache)
4334 goto err_tt_orig_destroy;
4336 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4338 SLAB_HWCACHE_ALIGN, NULL);
4339 if (!batadv_tt_req_cache)
4340 goto err_tt_change_destroy;
4342 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4344 SLAB_HWCACHE_ALIGN, NULL);
4345 if (!batadv_tt_roam_cache)
4346 goto err_tt_req_destroy;
4351 kmem_cache_destroy(batadv_tt_req_cache);
4352 batadv_tt_req_cache = NULL;
4353 err_tt_change_destroy:
4354 kmem_cache_destroy(batadv_tt_change_cache);
4355 batadv_tt_change_cache = NULL;
4356 err_tt_orig_destroy:
4357 kmem_cache_destroy(batadv_tt_orig_cache);
4358 batadv_tt_orig_cache = NULL;
4360 kmem_cache_destroy(batadv_tg_cache);
4361 batadv_tg_cache = NULL;
4363 kmem_cache_destroy(batadv_tl_cache);
4364 batadv_tl_cache = NULL;
4370 * batadv_tt_cache_destroy - Destroy tt memory object cache
4372 void batadv_tt_cache_destroy(void)
4374 kmem_cache_destroy(batadv_tl_cache);
4375 kmem_cache_destroy(batadv_tg_cache);
4376 kmem_cache_destroy(batadv_tt_orig_cache);
4377 kmem_cache_destroy(batadv_tt_change_cache);
4378 kmem_cache_destroy(batadv_tt_req_cache);
4379 kmem_cache_destroy(batadv_tt_roam_cache);