1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "translation-table.h"
22 #include <linux/atomic.h>
23 #include <linux/bitops.h>
24 #include <linux/build_bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/cache.h>
27 #include <linux/compiler.h>
28 #include <linux/crc32c.h>
29 #include <linux/errno.h>
30 #include <linux/etherdevice.h>
31 #include <linux/gfp.h>
32 #include <linux/if_ether.h>
33 #include <linux/init.h>
34 #include <linux/jhash.h>
35 #include <linux/jiffies.h>
36 #include <linux/kernel.h>
37 #include <linux/kref.h>
38 #include <linux/list.h>
39 #include <linux/lockdep.h>
40 #include <linux/net.h>
41 #include <linux/netdevice.h>
42 #include <linux/netlink.h>
43 #include <linux/rculist.h>
44 #include <linux/rcupdate.h>
45 #include <linux/seq_file.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/stddef.h>
50 #include <linux/string.h>
51 #include <linux/workqueue.h>
52 #include <net/genetlink.h>
53 #include <net/netlink.h>
55 #include <uapi/linux/batadv_packet.h>
56 #include <uapi/linux/batman_adv.h>
58 #include "bridge_loop_avoidance.h"
59 #include "hard-interface.h"
63 #include "originator.h"
64 #include "soft-interface.h"
67 static struct kmem_cache *batadv_tl_cache __read_mostly;
68 static struct kmem_cache *batadv_tg_cache __read_mostly;
69 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
70 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
71 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
72 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
75 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
76 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
78 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
80 struct batadv_orig_node *orig_node);
81 static void batadv_tt_purge(struct work_struct *work);
83 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
84 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
85 struct batadv_orig_node *orig_node,
86 const unsigned char *addr,
87 unsigned short vid, const char *message,
91 * batadv_compare_tt() - check if two TT entries are the same
92 * @node: the list element pointer of the first TT entry
93 * @data2: pointer to the tt_common_entry of the second TT entry
95 * Compare the MAC address and the VLAN ID of the two TT entries and check if
96 * they are the same TT client.
97 * Return: true if the two TT clients are the same, false otherwise
99 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
101 const void *data1 = container_of(node, struct batadv_tt_common_entry,
103 const struct batadv_tt_common_entry *tt1 = data1;
104 const struct batadv_tt_common_entry *tt2 = data2;
106 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
110 * batadv_choose_tt() - return the index of the tt entry in the hash table
111 * @data: pointer to the tt_common_entry object to map
112 * @size: the size of the hash table
114 * Return: the hash index where the object represented by 'data' should be
117 static inline u32 batadv_choose_tt(const void *data, u32 size)
119 struct batadv_tt_common_entry *tt;
122 tt = (struct batadv_tt_common_entry *)data;
123 hash = jhash(&tt->addr, ETH_ALEN, hash);
124 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
130 * batadv_tt_hash_find() - look for a client in the given hash table
131 * @hash: the hash table to search
132 * @addr: the mac address of the client to look for
133 * @vid: VLAN identifier
135 * Return: a pointer to the tt_common struct belonging to the searched client if
136 * found, NULL otherwise.
138 static struct batadv_tt_common_entry *
139 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
142 struct hlist_head *head;
143 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
149 ether_addr_copy(to_search.addr, addr);
152 index = batadv_choose_tt(&to_search, hash->size);
153 head = &hash->table[index];
156 hlist_for_each_entry_rcu(tt, head, hash_entry) {
157 if (!batadv_compare_eth(tt, addr))
163 if (!kref_get_unless_zero(&tt->refcount))
175 * batadv_tt_local_hash_find() - search the local table for a given client
176 * @bat_priv: the bat priv with all the soft interface information
177 * @addr: the mac address of the client to look for
178 * @vid: VLAN identifier
180 * Return: a pointer to the corresponding tt_local_entry struct if the client is
181 * found, NULL otherwise.
183 static struct batadv_tt_local_entry *
184 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
187 struct batadv_tt_common_entry *tt_common_entry;
188 struct batadv_tt_local_entry *tt_local_entry = NULL;
190 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
193 tt_local_entry = container_of(tt_common_entry,
194 struct batadv_tt_local_entry,
196 return tt_local_entry;
200 * batadv_tt_global_hash_find() - search the global table for a given client
201 * @bat_priv: the bat priv with all the soft interface information
202 * @addr: the mac address of the client to look for
203 * @vid: VLAN identifier
205 * Return: a pointer to the corresponding tt_global_entry struct if the client
206 * is found, NULL otherwise.
208 static struct batadv_tt_global_entry *
209 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
212 struct batadv_tt_common_entry *tt_common_entry;
213 struct batadv_tt_global_entry *tt_global_entry = NULL;
215 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
218 tt_global_entry = container_of(tt_common_entry,
219 struct batadv_tt_global_entry,
221 return tt_global_entry;
225 * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
226 * @rcu: rcu pointer of the tt_local_entry
228 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
230 struct batadv_tt_local_entry *tt_local_entry;
232 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
235 kmem_cache_free(batadv_tl_cache, tt_local_entry);
239 * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
240 * for free after rcu grace period
241 * @ref: kref pointer of the nc_node
243 static void batadv_tt_local_entry_release(struct kref *ref)
245 struct batadv_tt_local_entry *tt_local_entry;
247 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
250 batadv_softif_vlan_put(tt_local_entry->vlan);
252 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
256 * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
257 * possibly release it
258 * @tt_local_entry: tt_local_entry to be free'd
261 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
263 kref_put(&tt_local_entry->common.refcount,
264 batadv_tt_local_entry_release);
268 * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
269 * @rcu: rcu pointer of the tt_global_entry
271 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
273 struct batadv_tt_global_entry *tt_global_entry;
275 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
278 kmem_cache_free(batadv_tg_cache, tt_global_entry);
282 * batadv_tt_global_entry_release() - release tt_global_entry from lists and
283 * queue for free after rcu grace period
284 * @ref: kref pointer of the nc_node
286 static void batadv_tt_global_entry_release(struct kref *ref)
288 struct batadv_tt_global_entry *tt_global_entry;
290 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
293 batadv_tt_global_del_orig_list(tt_global_entry);
295 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
299 * batadv_tt_global_entry_put() - decrement the tt_global_entry refcounter and
300 * possibly release it
301 * @tt_global_entry: tt_global_entry to be free'd
304 batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
306 kref_put(&tt_global_entry->common.refcount,
307 batadv_tt_global_entry_release);
311 * batadv_tt_global_hash_count() - count the number of orig entries
312 * @bat_priv: the bat priv with all the soft interface information
313 * @addr: the mac address of the client to count entries for
314 * @vid: VLAN identifier
316 * Return: the number of originators advertising the given address/data
317 * (excluding ourself).
319 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
320 const u8 *addr, unsigned short vid)
322 struct batadv_tt_global_entry *tt_global_entry;
325 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
326 if (!tt_global_entry)
329 count = atomic_read(&tt_global_entry->orig_list_count);
330 batadv_tt_global_entry_put(tt_global_entry);
336 * batadv_tt_local_size_mod() - change the size by v of the local table
338 * @bat_priv: the bat priv with all the soft interface information
339 * @vid: the VLAN identifier of the sub-table to change
340 * @v: the amount to sum to the local table size
342 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
343 unsigned short vid, int v)
345 struct batadv_softif_vlan *vlan;
347 vlan = batadv_softif_vlan_get(bat_priv, vid);
351 atomic_add(v, &vlan->tt.num_entries);
353 batadv_softif_vlan_put(vlan);
357 * batadv_tt_local_size_inc() - increase by one the local table size for the
359 * @bat_priv: the bat priv with all the soft interface information
360 * @vid: the VLAN identifier
362 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
365 batadv_tt_local_size_mod(bat_priv, vid, 1);
369 * batadv_tt_local_size_dec() - decrease by one the local table size for the
371 * @bat_priv: the bat priv with all the soft interface information
372 * @vid: the VLAN identifier
374 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
377 batadv_tt_local_size_mod(bat_priv, vid, -1);
381 * batadv_tt_global_size_mod() - change the size by v of the global table
382 * for orig_node identified by vid
383 * @orig_node: the originator for which the table has to be modified
384 * @vid: the VLAN identifier
385 * @v: the amount to sum to the global table size
387 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
388 unsigned short vid, int v)
390 struct batadv_orig_node_vlan *vlan;
392 vlan = batadv_orig_node_vlan_new(orig_node, vid);
396 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
397 spin_lock_bh(&orig_node->vlan_list_lock);
398 if (!hlist_unhashed(&vlan->list)) {
399 hlist_del_init_rcu(&vlan->list);
400 batadv_orig_node_vlan_put(vlan);
402 spin_unlock_bh(&orig_node->vlan_list_lock);
405 batadv_orig_node_vlan_put(vlan);
409 * batadv_tt_global_size_inc() - increase by one the global table size for the
411 * @orig_node: the originator which global table size has to be decreased
412 * @vid: the vlan identifier
414 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
417 batadv_tt_global_size_mod(orig_node, vid, 1);
421 * batadv_tt_global_size_dec() - decrease by one the global table size for the
423 * @orig_node: the originator which global table size has to be decreased
424 * @vid: the vlan identifier
426 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
429 batadv_tt_global_size_mod(orig_node, vid, -1);
433 * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
434 * @rcu: rcu pointer of the orig_entry
436 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
438 struct batadv_tt_orig_list_entry *orig_entry;
440 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
442 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
446 * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
447 * queue for free after rcu grace period
448 * @ref: kref pointer of the tt orig entry
450 static void batadv_tt_orig_list_entry_release(struct kref *ref)
452 struct batadv_tt_orig_list_entry *orig_entry;
454 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
457 batadv_orig_node_put(orig_entry->orig_node);
458 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
462 * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
463 * possibly release it
464 * @orig_entry: tt orig entry to be free'd
467 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
469 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
473 * batadv_tt_local_event() - store a local TT event (ADD/DEL)
474 * @bat_priv: the bat priv with all the soft interface information
475 * @tt_local_entry: the TT entry involved in the event
476 * @event_flags: flags to store in the event structure
478 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
479 struct batadv_tt_local_entry *tt_local_entry,
482 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
483 struct batadv_tt_common_entry *common = &tt_local_entry->common;
484 u8 flags = common->flags | event_flags;
485 bool event_removed = false;
486 bool del_op_requested, del_op_entry;
488 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
492 tt_change_node->change.flags = flags;
493 memset(tt_change_node->change.reserved, 0,
494 sizeof(tt_change_node->change.reserved));
495 ether_addr_copy(tt_change_node->change.addr, common->addr);
496 tt_change_node->change.vid = htons(common->vid);
498 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
500 /* check for ADD+DEL or DEL+ADD events */
501 spin_lock_bh(&bat_priv->tt.changes_list_lock);
502 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
504 if (!batadv_compare_eth(entry->change.addr, common->addr))
507 /* DEL+ADD in the same orig interval have no effect and can be
508 * removed to avoid silly behaviour on the receiver side. The
509 * other way around (ADD+DEL) can happen in case of roaming of
510 * a client still in the NEW state. Roaming of NEW clients is
511 * now possible due to automatically recognition of "temporary"
514 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
515 if (!del_op_requested && del_op_entry)
517 if (del_op_requested && !del_op_entry)
520 /* this is a second add in the same originator interval. It
521 * means that flags have been changed: update them!
523 if (!del_op_requested && !del_op_entry)
524 entry->change.flags = flags;
528 list_del(&entry->list);
529 kmem_cache_free(batadv_tt_change_cache, entry);
530 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
531 event_removed = true;
535 /* track the change in the OGMinterval list */
536 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
539 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
542 atomic_dec(&bat_priv->tt.local_changes);
544 atomic_inc(&bat_priv->tt.local_changes);
548 * batadv_tt_len() - compute length in bytes of given number of tt changes
549 * @changes_num: number of tt changes
551 * Return: computed length in bytes.
553 static int batadv_tt_len(int changes_num)
555 return changes_num * sizeof(struct batadv_tvlv_tt_change);
559 * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
560 * @tt_len: available space
562 * Return: the number of entries.
564 static u16 batadv_tt_entries(u16 tt_len)
566 return tt_len / batadv_tt_len(1);
570 * batadv_tt_local_table_transmit_size() - calculates the local translation
571 * table size when transmitted over the air
572 * @bat_priv: the bat priv with all the soft interface information
574 * Return: local translation table size in bytes.
576 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
579 u16 tt_local_entries = 0;
580 struct batadv_softif_vlan *vlan;
584 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
586 tt_local_entries += atomic_read(&vlan->tt.num_entries);
590 /* header size of tvlv encapsulated tt response payload */
591 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
592 hdr_size += sizeof(struct batadv_tvlv_hdr);
593 hdr_size += sizeof(struct batadv_tvlv_tt_data);
594 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
596 return hdr_size + batadv_tt_len(tt_local_entries);
599 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
601 if (bat_priv->tt.local_hash)
604 bat_priv->tt.local_hash = batadv_hash_new(1024);
606 if (!bat_priv->tt.local_hash)
609 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
610 &batadv_tt_local_hash_lock_class_key);
615 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
616 struct batadv_tt_global_entry *tt_global,
619 struct batadv_tt_global_entry *tt_removed_entry;
620 struct hlist_node *tt_removed_node;
622 batadv_dbg(BATADV_DBG_TT, bat_priv,
623 "Deleting global tt entry %pM (vid: %d): %s\n",
624 tt_global->common.addr,
625 batadv_print_vid(tt_global->common.vid), message);
627 tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
631 if (!tt_removed_node)
634 /* drop reference of remove hash entry */
635 tt_removed_entry = hlist_entry(tt_removed_node,
636 struct batadv_tt_global_entry,
638 batadv_tt_global_entry_put(tt_removed_entry);
642 * batadv_tt_local_add() - add a new client to the local table or update an
644 * @soft_iface: netdev struct of the mesh interface
645 * @addr: the mac address of the client to add
646 * @vid: VLAN identifier
647 * @ifindex: index of the interface where the client is connected to (useful to
648 * identify wireless clients)
649 * @mark: the value contained in the skb->mark field of the received packet (if
652 * Return: true if the client was successfully added, false otherwise.
654 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
655 unsigned short vid, int ifindex, u32 mark)
657 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
658 struct batadv_tt_local_entry *tt_local;
659 struct batadv_tt_global_entry *tt_global = NULL;
660 struct net *net = dev_net(soft_iface);
661 struct batadv_softif_vlan *vlan;
662 struct net_device *in_dev = NULL;
663 struct batadv_hard_iface *in_hardif = NULL;
664 struct hlist_head *head;
665 struct batadv_tt_orig_list_entry *orig_entry;
666 int hash_added, table_size, packet_size_max;
668 bool roamed_back = false;
672 if (ifindex != BATADV_NULL_IFINDEX)
673 in_dev = dev_get_by_index(net, ifindex);
676 in_hardif = batadv_hardif_get_by_netdev(in_dev);
678 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
680 if (!is_multicast_ether_addr(addr))
681 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
684 tt_local->last_seen = jiffies;
685 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
686 batadv_dbg(BATADV_DBG_TT, bat_priv,
687 "Re-adding pending client %pM (vid: %d)\n",
688 addr, batadv_print_vid(vid));
689 /* whatever the reason why the PENDING flag was set,
690 * this is a client which was enqueued to be removed in
691 * this orig_interval. Since it popped up again, the
692 * flag can be reset like it was never enqueued
694 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
698 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
699 batadv_dbg(BATADV_DBG_TT, bat_priv,
700 "Roaming client %pM (vid: %d) came back to its original location\n",
701 addr, batadv_print_vid(vid));
702 /* the ROAM flag is set because this client roamed away
703 * and the node got a roaming_advertisement message. Now
704 * that the client popped up again at its original
705 * location such flag can be unset
707 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
713 /* Ignore the client if we cannot send it in a full table response. */
714 table_size = batadv_tt_local_table_transmit_size(bat_priv);
715 table_size += batadv_tt_len(1);
716 packet_size_max = atomic_read(&bat_priv->packet_size_max);
717 if (table_size > packet_size_max) {
718 net_ratelimited_function(batadv_info, soft_iface,
719 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
720 table_size, packet_size_max, addr);
724 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
728 /* increase the refcounter of the related vlan */
729 vlan = batadv_softif_vlan_get(bat_priv, vid);
731 net_ratelimited_function(batadv_info, soft_iface,
732 "adding TT local entry %pM to non-existent VLAN %d\n",
733 addr, batadv_print_vid(vid));
734 kmem_cache_free(batadv_tl_cache, tt_local);
739 batadv_dbg(BATADV_DBG_TT, bat_priv,
740 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
741 addr, batadv_print_vid(vid),
742 (u8)atomic_read(&bat_priv->tt.vn));
744 ether_addr_copy(tt_local->common.addr, addr);
745 /* The local entry has to be marked as NEW to avoid to send it in
746 * a full table response going out before the next ttvn increment
747 * (consistency check)
749 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
750 tt_local->common.vid = vid;
751 if (batadv_is_wifi_hardif(in_hardif))
752 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
753 kref_init(&tt_local->common.refcount);
754 tt_local->last_seen = jiffies;
755 tt_local->common.added_at = tt_local->last_seen;
756 tt_local->vlan = vlan;
758 /* the batman interface mac and multicast addresses should never be
761 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
762 is_multicast_ether_addr(addr))
763 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
765 kref_get(&tt_local->common.refcount);
766 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
767 batadv_choose_tt, &tt_local->common,
768 &tt_local->common.hash_entry);
770 if (unlikely(hash_added != 0)) {
771 /* remove the reference for the hash */
772 batadv_tt_local_entry_put(tt_local);
777 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
780 /* Check whether it is a roaming, but don't do anything if the roaming
781 * process has already been handled
783 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
784 /* These node are probably going to update their tt table */
785 head = &tt_global->orig_list;
787 hlist_for_each_entry_rcu(orig_entry, head, list) {
788 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
789 tt_global->common.vid,
790 orig_entry->orig_node);
794 batadv_tt_global_free(bat_priv, tt_global,
798 /* The global entry has to be marked as ROAMING and
799 * has to be kept for consistency purpose
801 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
802 tt_global->roam_at = jiffies;
806 /* store the current remote flags before altering them. This helps
807 * understanding is flags are changing or not
809 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
811 if (batadv_is_wifi_hardif(in_hardif))
812 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
814 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
816 /* check the mark in the skb: if it's equal to the configured
817 * isolation_mark, it means the packet is coming from an isolated
820 match_mark = (mark & bat_priv->isolation_mark_mask);
821 if (bat_priv->isolation_mark_mask &&
822 match_mark == bat_priv->isolation_mark)
823 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
825 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
827 /* if any "dynamic" flag has been modified, resend an ADD event for this
828 * entry so that all the nodes can get the new flags
830 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
831 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
836 batadv_hardif_put(in_hardif);
840 batadv_tt_local_entry_put(tt_local);
842 batadv_tt_global_entry_put(tt_global);
847 * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
848 * within a TT Response directed to another node
849 * @orig_node: originator for which the TT data has to be prepared
850 * @tt_data: uninitialised pointer to the address of the TVLV buffer
851 * @tt_change: uninitialised pointer to the address of the area where the TT
852 * changed can be stored
853 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
854 * function reserves the amount of space needed to send the entire global TT
855 * table. In case of success the value is updated with the real amount of
857 * Allocate the needed amount of memory for the entire TT TVLV and write its
858 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
859 * objects, one per active VLAN served by the originator node.
861 * Return: the size of the allocated buffer or 0 in case of failure.
864 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
865 struct batadv_tvlv_tt_data **tt_data,
866 struct batadv_tvlv_tt_change **tt_change,
873 struct batadv_tvlv_tt_vlan_data *tt_vlan;
874 struct batadv_orig_node_vlan *vlan;
877 spin_lock_bh(&orig_node->vlan_list_lock);
878 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
880 num_entries += atomic_read(&vlan->tt.num_entries);
883 change_offset = sizeof(**tt_data);
884 change_offset += num_vlan * sizeof(*tt_vlan);
886 /* if tt_len is negative, allocate the space needed by the full table */
888 *tt_len = batadv_tt_len(num_entries);
891 tvlv_len += change_offset;
893 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
899 (*tt_data)->flags = BATADV_NO_FLAGS;
900 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
901 (*tt_data)->num_vlan = htons(num_vlan);
903 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
904 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
905 tt_vlan->vid = htons(vlan->vid);
906 tt_vlan->crc = htonl(vlan->tt.crc);
911 tt_change_ptr = (u8 *)*tt_data + change_offset;
912 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
915 spin_unlock_bh(&orig_node->vlan_list_lock);
920 * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
922 * @bat_priv: the bat priv with all the soft interface information
923 * @tt_data: uninitialised pointer to the address of the TVLV buffer
924 * @tt_change: uninitialised pointer to the address of the area where the TT
925 * changes can be stored
926 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
927 * function reserves the amount of space needed to send the entire local TT
928 * table. In case of success the value is updated with the real amount of
931 * Allocate the needed amount of memory for the entire TT TVLV and write its
932 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
933 * objects, one per active VLAN.
935 * Return: the size of the allocated buffer or 0 in case of failure.
938 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
939 struct batadv_tvlv_tt_data **tt_data,
940 struct batadv_tvlv_tt_change **tt_change,
943 struct batadv_tvlv_tt_vlan_data *tt_vlan;
944 struct batadv_softif_vlan *vlan;
946 u16 vlan_entries = 0;
947 u16 total_entries = 0;
952 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
953 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
954 vlan_entries = atomic_read(&vlan->tt.num_entries);
955 if (vlan_entries < 1)
959 total_entries += vlan_entries;
962 change_offset = sizeof(**tt_data);
963 change_offset += num_vlan * sizeof(*tt_vlan);
965 /* if tt_len is negative, allocate the space needed by the full table */
967 *tt_len = batadv_tt_len(total_entries);
970 tvlv_len += change_offset;
972 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
978 (*tt_data)->flags = BATADV_NO_FLAGS;
979 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
980 (*tt_data)->num_vlan = htons(num_vlan);
982 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
983 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
984 vlan_entries = atomic_read(&vlan->tt.num_entries);
985 if (vlan_entries < 1)
988 tt_vlan->vid = htons(vlan->vid);
989 tt_vlan->crc = htonl(vlan->tt.crc);
994 tt_change_ptr = (u8 *)*tt_data + change_offset;
995 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
998 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
1003 * batadv_tt_tvlv_container_update() - update the translation table tvlv
1004 * container after local tt changes have been committed
1005 * @bat_priv: the bat priv with all the soft interface information
1007 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
1009 struct batadv_tt_change_node *entry, *safe;
1010 struct batadv_tvlv_tt_data *tt_data;
1011 struct batadv_tvlv_tt_change *tt_change;
1012 int tt_diff_len, tt_change_len = 0;
1013 int tt_diff_entries_num = 0;
1014 int tt_diff_entries_count = 0;
1017 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
1018 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
1020 /* if we have too many changes for one packet don't send any
1021 * and wait for the tt table request which will be fragmented
1023 if (tt_diff_len > bat_priv->soft_iface->mtu)
1026 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1027 &tt_change, &tt_diff_len);
1031 tt_data->flags = BATADV_TT_OGM_DIFF;
1033 if (tt_diff_len == 0)
1034 goto container_register;
1036 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1037 atomic_set(&bat_priv->tt.local_changes, 0);
1039 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1041 if (tt_diff_entries_count < tt_diff_entries_num) {
1042 memcpy(tt_change + tt_diff_entries_count,
1044 sizeof(struct batadv_tvlv_tt_change));
1045 tt_diff_entries_count++;
1047 list_del(&entry->list);
1048 kmem_cache_free(batadv_tt_change_cache, entry);
1050 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1052 /* Keep the buffer for possible tt_request */
1053 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1054 kfree(bat_priv->tt.last_changeset);
1055 bat_priv->tt.last_changeset_len = 0;
1056 bat_priv->tt.last_changeset = NULL;
1057 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1058 /* check whether this new OGM has no changes due to size problems */
1059 if (tt_diff_entries_count > 0) {
1060 /* if kmalloc() fails we will reply with the full table
1061 * instead of providing the diff
1063 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1064 if (bat_priv->tt.last_changeset) {
1065 memcpy(bat_priv->tt.last_changeset,
1066 tt_change, tt_change_len);
1067 bat_priv->tt.last_changeset_len = tt_diff_len;
1070 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1073 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1078 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1081 * batadv_tt_local_seq_print_text() - Print the local tt table in a seq file
1082 * @seq: seq file to print on
1087 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
1089 struct net_device *net_dev = (struct net_device *)seq->private;
1090 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1091 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1092 struct batadv_tt_common_entry *tt_common_entry;
1093 struct batadv_tt_local_entry *tt_local;
1094 struct batadv_hard_iface *primary_if;
1095 struct hlist_head *head;
1098 int last_seen_msecs;
1099 unsigned long last_seen_jiffies;
1101 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1103 primary_if = batadv_seq_print_text_primary_if_get(seq);
1108 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1109 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1111 " Client VID Flags Last seen (CRC )\n");
1113 for (i = 0; i < hash->size; i++) {
1114 head = &hash->table[i];
1117 hlist_for_each_entry_rcu(tt_common_entry,
1119 tt_local = container_of(tt_common_entry,
1120 struct batadv_tt_local_entry,
1122 last_seen_jiffies = jiffies - tt_local->last_seen;
1123 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1124 last_seen_secs = last_seen_msecs / 1000;
1125 last_seen_msecs = last_seen_msecs % 1000;
1127 no_purge = tt_common_entry->flags & np_flag;
1129 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1130 tt_common_entry->addr,
1131 batadv_print_vid(tt_common_entry->vid),
1132 ((tt_common_entry->flags &
1133 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1134 no_purge ? 'P' : '.',
1135 ((tt_common_entry->flags &
1136 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1137 ((tt_common_entry->flags &
1138 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1139 ((tt_common_entry->flags &
1140 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1141 ((tt_common_entry->flags &
1142 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1143 no_purge ? 0 : last_seen_secs,
1144 no_purge ? 0 : last_seen_msecs,
1145 tt_local->vlan->tt.crc);
1151 batadv_hardif_put(primary_if);
1157 * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1158 * @msg :Netlink message to dump into
1159 * @portid: Port making netlink request
1160 * @cb: Control block containing additional options
1161 * @bat_priv: The bat priv with all the soft interface information
1162 * @common: tt local & tt global common data
1164 * Return: Error code, or 0 on success
1167 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1168 struct netlink_callback *cb,
1169 struct batadv_priv *bat_priv,
1170 struct batadv_tt_common_entry *common)
1173 struct batadv_softif_vlan *vlan;
1174 struct batadv_tt_local_entry *local;
1175 unsigned int last_seen_msecs;
1178 local = container_of(common, struct batadv_tt_local_entry, common);
1179 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1181 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1187 batadv_softif_vlan_put(vlan);
1189 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1190 &batadv_netlink_family, NLM_F_MULTI,
1191 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1195 genl_dump_check_consistent(cb, hdr);
1197 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1198 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1199 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1200 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1201 goto nla_put_failure;
1203 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1204 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1205 goto nla_put_failure;
1207 genlmsg_end(msg, hdr);
1211 genlmsg_cancel(msg, hdr);
1216 * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1217 * @msg: Netlink message to dump into
1218 * @portid: Port making netlink request
1219 * @cb: Control block containing additional options
1220 * @bat_priv: The bat priv with all the soft interface information
1221 * @hash: hash to dump
1222 * @bucket: bucket index to dump
1223 * @idx_s: Number of entries to skip
1225 * Return: Error code, or 0 on success
1228 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1229 struct netlink_callback *cb,
1230 struct batadv_priv *bat_priv,
1231 struct batadv_hashtable *hash, unsigned int bucket,
1234 struct batadv_tt_common_entry *common;
1237 spin_lock_bh(&hash->list_locks[bucket]);
1238 cb->seq = atomic_read(&hash->generation) << 1 | 1;
1240 hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1244 if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1246 spin_unlock_bh(&hash->list_locks[bucket]);
1251 spin_unlock_bh(&hash->list_locks[bucket]);
1258 * batadv_tt_local_dump() - Dump TT local entries into a message
1259 * @msg: Netlink message to dump into
1260 * @cb: Parameters from query
1262 * Return: Error code, or 0 on success
1264 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1266 struct net *net = sock_net(cb->skb->sk);
1267 struct net_device *soft_iface;
1268 struct batadv_priv *bat_priv;
1269 struct batadv_hard_iface *primary_if = NULL;
1270 struct batadv_hashtable *hash;
1273 int bucket = cb->args[0];
1274 int idx = cb->args[1];
1275 int portid = NETLINK_CB(cb->skb).portid;
1277 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1281 soft_iface = dev_get_by_index(net, ifindex);
1282 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1287 bat_priv = netdev_priv(soft_iface);
1289 primary_if = batadv_primary_if_get_selected(bat_priv);
1290 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1295 hash = bat_priv->tt.local_hash;
1297 while (bucket < hash->size) {
1298 if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1299 hash, bucket, &idx))
1309 batadv_hardif_put(primary_if);
1311 dev_put(soft_iface);
1313 cb->args[0] = bucket;
1320 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1321 struct batadv_tt_local_entry *tt_local_entry,
1322 u16 flags, const char *message)
1324 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1326 /* The local client has to be marked as "pending to be removed" but has
1327 * to be kept in the table in order to send it in a full table
1328 * response issued before the net ttvn increment (consistency check)
1330 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1332 batadv_dbg(BATADV_DBG_TT, bat_priv,
1333 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1334 tt_local_entry->common.addr,
1335 batadv_print_vid(tt_local_entry->common.vid), message);
1339 * batadv_tt_local_remove() - logically remove an entry from the local table
1340 * @bat_priv: the bat priv with all the soft interface information
1341 * @addr: the MAC address of the client to remove
1342 * @vid: VLAN identifier
1343 * @message: message to append to the log on deletion
1344 * @roaming: true if the deletion is due to a roaming event
1346 * Return: the flags assigned to the local entry before being deleted
1348 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1349 unsigned short vid, const char *message,
1352 struct batadv_tt_local_entry *tt_removed_entry;
1353 struct batadv_tt_local_entry *tt_local_entry;
1354 u16 flags, curr_flags = BATADV_NO_FLAGS;
1355 struct hlist_node *tt_removed_node;
1357 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1358 if (!tt_local_entry)
1361 curr_flags = tt_local_entry->common.flags;
1363 flags = BATADV_TT_CLIENT_DEL;
1364 /* if this global entry addition is due to a roaming, the node has to
1365 * mark the local entry as "roamed" in order to correctly reroute
1369 flags |= BATADV_TT_CLIENT_ROAM;
1370 /* mark the local client as ROAMed */
1371 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1374 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1375 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1379 /* if this client has been added right now, it is possible to
1380 * immediately purge it
1382 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1384 tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1387 &tt_local_entry->common);
1388 if (!tt_removed_node)
1391 /* drop reference of remove hash entry */
1392 tt_removed_entry = hlist_entry(tt_removed_node,
1393 struct batadv_tt_local_entry,
1395 batadv_tt_local_entry_put(tt_removed_entry);
1399 batadv_tt_local_entry_put(tt_local_entry);
1405 * batadv_tt_local_purge_list() - purge inactive tt local entries
1406 * @bat_priv: the bat priv with all the soft interface information
1407 * @head: pointer to the list containing the local tt entries
1408 * @timeout: parameter deciding whether a given tt local entry is considered
1411 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1412 struct hlist_head *head,
1415 struct batadv_tt_local_entry *tt_local_entry;
1416 struct batadv_tt_common_entry *tt_common_entry;
1417 struct hlist_node *node_tmp;
1419 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1421 tt_local_entry = container_of(tt_common_entry,
1422 struct batadv_tt_local_entry,
1424 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1427 /* entry already marked for deletion */
1428 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1431 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1434 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1435 BATADV_TT_CLIENT_DEL, "timed out");
1440 * batadv_tt_local_purge() - purge inactive tt local entries
1441 * @bat_priv: the bat priv with all the soft interface information
1442 * @timeout: parameter deciding whether a given tt local entry is considered
1445 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1448 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1449 struct hlist_head *head;
1450 spinlock_t *list_lock; /* protects write access to the hash lists */
1453 for (i = 0; i < hash->size; i++) {
1454 head = &hash->table[i];
1455 list_lock = &hash->list_locks[i];
1457 spin_lock_bh(list_lock);
1458 batadv_tt_local_purge_list(bat_priv, head, timeout);
1459 spin_unlock_bh(list_lock);
1463 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1465 struct batadv_hashtable *hash;
1466 spinlock_t *list_lock; /* protects write access to the hash lists */
1467 struct batadv_tt_common_entry *tt_common_entry;
1468 struct batadv_tt_local_entry *tt_local;
1469 struct hlist_node *node_tmp;
1470 struct hlist_head *head;
1473 if (!bat_priv->tt.local_hash)
1476 hash = bat_priv->tt.local_hash;
1478 for (i = 0; i < hash->size; i++) {
1479 head = &hash->table[i];
1480 list_lock = &hash->list_locks[i];
1482 spin_lock_bh(list_lock);
1483 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1485 hlist_del_rcu(&tt_common_entry->hash_entry);
1486 tt_local = container_of(tt_common_entry,
1487 struct batadv_tt_local_entry,
1490 batadv_tt_local_entry_put(tt_local);
1492 spin_unlock_bh(list_lock);
1495 batadv_hash_destroy(hash);
1497 bat_priv->tt.local_hash = NULL;
1500 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1502 if (bat_priv->tt.global_hash)
1505 bat_priv->tt.global_hash = batadv_hash_new(1024);
1507 if (!bat_priv->tt.global_hash)
1510 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1511 &batadv_tt_global_hash_lock_class_key);
1516 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1518 struct batadv_tt_change_node *entry, *safe;
1520 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1522 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1524 list_del(&entry->list);
1525 kmem_cache_free(batadv_tt_change_cache, entry);
1528 atomic_set(&bat_priv->tt.local_changes, 0);
1529 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1533 * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1534 * @entry: the TT global entry where the orig_list_entry has to be
1536 * @orig_node: the originator for which the orig_list_entry has to be found
1538 * retrieve the orig_tt_list_entry belonging to orig_node from the
1539 * batadv_tt_global_entry list
1541 * Return: it with an increased refcounter, NULL if not found
1543 static struct batadv_tt_orig_list_entry *
1544 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1545 const struct batadv_orig_node *orig_node)
1547 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1548 const struct hlist_head *head;
1551 head = &entry->orig_list;
1552 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1553 if (tmp_orig_entry->orig_node != orig_node)
1555 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1558 orig_entry = tmp_orig_entry;
1567 * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1568 * handled by a given originator
1569 * @entry: the TT global entry to check
1570 * @orig_node: the originator to search in the list
1571 * @flags: a pointer to store TT flags for the given @entry received
1574 * find out if an orig_node is already in the list of a tt_global_entry.
1576 * Return: true if found, false otherwise
1579 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1580 const struct batadv_orig_node *orig_node,
1583 struct batadv_tt_orig_list_entry *orig_entry;
1586 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1591 *flags = orig_entry->flags;
1593 batadv_tt_orig_list_entry_put(orig_entry);
1600 * batadv_tt_global_sync_flags() - update TT sync flags
1601 * @tt_global: the TT global entry to update sync flags in
1603 * Updates the sync flag bits in the tt_global flag attribute with a logical
1604 * OR of all sync flags from any of its TT orig entries.
1607 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1609 struct batadv_tt_orig_list_entry *orig_entry;
1610 const struct hlist_head *head;
1611 u16 flags = BATADV_NO_FLAGS;
1614 head = &tt_global->orig_list;
1615 hlist_for_each_entry_rcu(orig_entry, head, list)
1616 flags |= orig_entry->flags;
1619 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1620 tt_global->common.flags = flags;
1624 * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1625 * @tt_global: the TT global entry to add an orig entry in
1626 * @orig_node: the originator to add an orig entry for
1627 * @ttvn: translation table version number of this changeset
1628 * @flags: TT sync flags
1631 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1632 struct batadv_orig_node *orig_node, int ttvn,
1635 struct batadv_tt_orig_list_entry *orig_entry;
1637 spin_lock_bh(&tt_global->list_lock);
1639 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1641 /* refresh the ttvn: the current value could be a bogus one that
1642 * was added during a "temporary client detection"
1644 orig_entry->ttvn = ttvn;
1645 orig_entry->flags = flags;
1649 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1653 INIT_HLIST_NODE(&orig_entry->list);
1654 kref_get(&orig_node->refcount);
1655 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1656 orig_entry->orig_node = orig_node;
1657 orig_entry->ttvn = ttvn;
1658 orig_entry->flags = flags;
1659 kref_init(&orig_entry->refcount);
1661 kref_get(&orig_entry->refcount);
1662 hlist_add_head_rcu(&orig_entry->list,
1663 &tt_global->orig_list);
1664 atomic_inc(&tt_global->orig_list_count);
1667 batadv_tt_global_sync_flags(tt_global);
1670 batadv_tt_orig_list_entry_put(orig_entry);
1672 spin_unlock_bh(&tt_global->list_lock);
1676 * batadv_tt_global_add() - add a new TT global entry or update an existing one
1677 * @bat_priv: the bat priv with all the soft interface information
1678 * @orig_node: the originator announcing the client
1679 * @tt_addr: the mac address of the non-mesh client
1680 * @vid: VLAN identifier
1681 * @flags: TT flags that have to be set for this non-mesh client
1682 * @ttvn: the tt version number ever announcing this non-mesh client
1684 * Add a new TT global entry for the given originator. If the entry already
1685 * exists add a new reference to the given originator (a global entry can have
1686 * references to multiple originators) and adjust the flags attribute to reflect
1687 * the function argument.
1688 * If a TT local entry exists for this non-mesh client remove it.
1690 * The caller must hold orig_node refcount.
1692 * Return: true if the new entry has been added, false otherwise
1694 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1695 struct batadv_orig_node *orig_node,
1696 const unsigned char *tt_addr,
1697 unsigned short vid, u16 flags, u8 ttvn)
1699 struct batadv_tt_global_entry *tt_global_entry;
1700 struct batadv_tt_local_entry *tt_local_entry;
1703 struct batadv_tt_common_entry *common;
1706 /* ignore global entries from backbone nodes */
1707 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1710 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1711 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1713 /* if the node already has a local client for this entry, it has to wait
1714 * for a roaming advertisement instead of manually messing up the global
1717 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1718 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1721 if (!tt_global_entry) {
1722 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1724 if (!tt_global_entry)
1727 common = &tt_global_entry->common;
1728 ether_addr_copy(common->addr, tt_addr);
1731 if (!is_multicast_ether_addr(common->addr))
1732 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1734 tt_global_entry->roam_at = 0;
1735 /* node must store current time in case of roaming. This is
1736 * needed to purge this entry out on timeout (if nobody claims
1739 if (flags & BATADV_TT_CLIENT_ROAM)
1740 tt_global_entry->roam_at = jiffies;
1741 kref_init(&common->refcount);
1742 common->added_at = jiffies;
1744 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1745 atomic_set(&tt_global_entry->orig_list_count, 0);
1746 spin_lock_init(&tt_global_entry->list_lock);
1748 kref_get(&common->refcount);
1749 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1751 batadv_choose_tt, common,
1752 &common->hash_entry);
1754 if (unlikely(hash_added != 0)) {
1755 /* remove the reference for the hash */
1756 batadv_tt_global_entry_put(tt_global_entry);
1760 common = &tt_global_entry->common;
1761 /* If there is already a global entry, we can use this one for
1763 * But if we are trying to add a temporary client then here are
1764 * two options at this point:
1765 * 1) the global client is not a temporary client: the global
1766 * client has to be left as it is, temporary information
1767 * should never override any already known client state
1768 * 2) the global client is a temporary client: purge the
1769 * originator list and add the new one orig_entry
1771 if (flags & BATADV_TT_CLIENT_TEMP) {
1772 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1774 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1777 batadv_tt_global_del_orig_list(tt_global_entry);
1778 goto add_orig_entry;
1781 /* if the client was temporary added before receiving the first
1782 * OGM announcing it, we have to clear the TEMP flag. Also,
1783 * remove the previous temporary orig node and re-add it
1784 * if required. If the orig entry changed, the new one which
1785 * is a non-temporary entry is preferred.
1787 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1788 batadv_tt_global_del_orig_list(tt_global_entry);
1789 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1792 /* the change can carry possible "attribute" flags like the
1793 * TT_CLIENT_TEMP, therefore they have to be copied in the
1796 if (!is_multicast_ether_addr(common->addr))
1797 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1799 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1800 * one originator left in the list and we previously received a
1801 * delete + roaming change for this originator.
1803 * We should first delete the old originator before adding the
1806 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1807 batadv_tt_global_del_orig_list(tt_global_entry);
1808 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1809 tt_global_entry->roam_at = 0;
1813 /* add the new orig_entry (if needed) or update it */
1814 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1815 flags & BATADV_TT_SYNC_MASK);
1817 batadv_dbg(BATADV_DBG_TT, bat_priv,
1818 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1819 common->addr, batadv_print_vid(common->vid),
1824 /* Do not remove multicast addresses from the local hash on
1827 if (is_multicast_ether_addr(tt_addr))
1830 /* remove address from local hash if present */
1831 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1832 "global tt received",
1833 flags & BATADV_TT_CLIENT_ROAM);
1834 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1836 if (!(flags & BATADV_TT_CLIENT_ROAM))
1837 /* this is a normal global add. Therefore the client is not in a
1838 * roaming state anymore.
1840 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1843 if (tt_global_entry)
1844 batadv_tt_global_entry_put(tt_global_entry);
1846 batadv_tt_local_entry_put(tt_local_entry);
1851 * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1852 * @bat_priv: the bat priv with all the soft interface information
1853 * @tt_global_entry: global translation table entry to be analyzed
1855 * This functon assumes the caller holds rcu_read_lock().
1856 * Return: best originator list entry or NULL on errors.
1858 static struct batadv_tt_orig_list_entry *
1859 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1860 struct batadv_tt_global_entry *tt_global_entry)
1862 struct batadv_neigh_node *router, *best_router = NULL;
1863 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1864 struct hlist_head *head;
1865 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1867 head = &tt_global_entry->orig_list;
1868 hlist_for_each_entry_rcu(orig_entry, head, list) {
1869 router = batadv_orig_router_get(orig_entry->orig_node,
1875 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1876 BATADV_IF_DEFAULT) <= 0) {
1877 batadv_neigh_node_put(router);
1881 /* release the refcount for the "old" best */
1883 batadv_neigh_node_put(best_router);
1885 best_entry = orig_entry;
1886 best_router = router;
1890 batadv_neigh_node_put(best_router);
1895 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1897 * batadv_tt_global_print_entry() - print all orig nodes who announce the
1898 * address for this global entry
1899 * @bat_priv: the bat priv with all the soft interface information
1900 * @tt_global_entry: global translation table entry to be printed
1901 * @seq: debugfs table seq_file struct
1903 * This functon assumes the caller holds rcu_read_lock().
1906 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1907 struct batadv_tt_global_entry *tt_global_entry,
1908 struct seq_file *seq)
1910 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1911 struct batadv_tt_common_entry *tt_common_entry;
1912 struct batadv_orig_node_vlan *vlan;
1913 struct hlist_head *head;
1917 tt_common_entry = &tt_global_entry->common;
1918 flags = tt_common_entry->flags;
1920 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1922 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1923 tt_common_entry->vid);
1926 " * Cannot retrieve VLAN %d for originator %pM\n",
1927 batadv_print_vid(tt_common_entry->vid),
1928 best_entry->orig_node->orig);
1932 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1934 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1935 '*', tt_global_entry->common.addr,
1936 batadv_print_vid(tt_global_entry->common.vid),
1937 best_entry->ttvn, best_entry->orig_node->orig,
1938 last_ttvn, vlan->tt.crc,
1939 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1940 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1941 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1942 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1944 batadv_orig_node_vlan_put(vlan);
1948 head = &tt_global_entry->orig_list;
1950 hlist_for_each_entry_rcu(orig_entry, head, list) {
1951 if (best_entry == orig_entry)
1954 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1955 tt_common_entry->vid);
1958 " + Cannot retrieve VLAN %d for originator %pM\n",
1959 batadv_print_vid(tt_common_entry->vid),
1960 orig_entry->orig_node->orig);
1964 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1966 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1967 '+', tt_global_entry->common.addr,
1968 batadv_print_vid(tt_global_entry->common.vid),
1969 orig_entry->ttvn, orig_entry->orig_node->orig,
1970 last_ttvn, vlan->tt.crc,
1971 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1972 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1973 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1974 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1976 batadv_orig_node_vlan_put(vlan);
1981 * batadv_tt_global_seq_print_text() - Print the global tt table in a seq file
1982 * @seq: seq file to print on
1987 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1989 struct net_device *net_dev = (struct net_device *)seq->private;
1990 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1991 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1992 struct batadv_tt_common_entry *tt_common_entry;
1993 struct batadv_tt_global_entry *tt_global;
1994 struct batadv_hard_iface *primary_if;
1995 struct hlist_head *head;
1998 primary_if = batadv_seq_print_text_primary_if_get(seq);
2003 "Globally announced TT entries received via the mesh %s\n",
2006 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
2008 for (i = 0; i < hash->size; i++) {
2009 head = &hash->table[i];
2012 hlist_for_each_entry_rcu(tt_common_entry,
2014 tt_global = container_of(tt_common_entry,
2015 struct batadv_tt_global_entry,
2017 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
2023 batadv_hardif_put(primary_if);
2029 * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
2030 * @msg: Netlink message to dump into
2031 * @portid: Port making netlink request
2032 * @seq: Sequence number of netlink message
2033 * @common: tt local & tt global common data
2034 * @orig: Originator node announcing a non-mesh client
2035 * @best: Is the best originator for the TT entry
2037 * Return: Error code, or 0 on success
2040 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2041 struct batadv_tt_common_entry *common,
2042 struct batadv_tt_orig_list_entry *orig,
2045 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
2047 struct batadv_orig_node_vlan *vlan;
2051 vlan = batadv_orig_node_vlan_get(orig->orig_node,
2058 batadv_orig_node_vlan_put(vlan);
2060 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2062 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2066 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2068 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2069 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2070 orig->orig_node->orig) ||
2071 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2072 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2073 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2074 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
2075 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
2076 goto nla_put_failure;
2078 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2079 goto nla_put_failure;
2081 genlmsg_end(msg, hdr);
2085 genlmsg_cancel(msg, hdr);
2090 * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
2091 * @msg: Netlink message to dump into
2092 * @portid: Port making netlink request
2093 * @seq: Sequence number of netlink message
2094 * @bat_priv: The bat priv with all the soft interface information
2095 * @common: tt local & tt global common data
2096 * @sub_s: Number of entries to skip
2098 * This function assumes the caller holds rcu_read_lock().
2100 * Return: Error code, or 0 on success
2103 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2104 struct batadv_priv *bat_priv,
2105 struct batadv_tt_common_entry *common, int *sub_s)
2107 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2108 struct batadv_tt_global_entry *global;
2109 struct hlist_head *head;
2113 global = container_of(common, struct batadv_tt_global_entry, common);
2114 best_entry = batadv_transtable_best_orig(bat_priv, global);
2115 head = &global->orig_list;
2117 hlist_for_each_entry_rcu(orig_entry, head, list) {
2121 best = (orig_entry == best_entry);
2123 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2124 orig_entry, best)) {
2135 * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
2136 * @msg: Netlink message to dump into
2137 * @portid: Port making netlink request
2138 * @seq: Sequence number of netlink message
2139 * @bat_priv: The bat priv with all the soft interface information
2140 * @head: Pointer to the list containing the global tt entries
2141 * @idx_s: Number of entries to skip
2142 * @sub: Number of entries to skip
2144 * Return: Error code, or 0 on success
2147 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2148 struct batadv_priv *bat_priv,
2149 struct hlist_head *head, int *idx_s, int *sub)
2151 struct batadv_tt_common_entry *common;
2155 hlist_for_each_entry_rcu(common, head, hash_entry) {
2159 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2174 * batadv_tt_global_dump() - Dump TT global entries into a message
2175 * @msg: Netlink message to dump into
2176 * @cb: Parameters from query
2178 * Return: Error code, or length of message on success
2180 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2182 struct net *net = sock_net(cb->skb->sk);
2183 struct net_device *soft_iface;
2184 struct batadv_priv *bat_priv;
2185 struct batadv_hard_iface *primary_if = NULL;
2186 struct batadv_hashtable *hash;
2187 struct hlist_head *head;
2190 int bucket = cb->args[0];
2191 int idx = cb->args[1];
2192 int sub = cb->args[2];
2193 int portid = NETLINK_CB(cb->skb).portid;
2195 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2199 soft_iface = dev_get_by_index(net, ifindex);
2200 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2205 bat_priv = netdev_priv(soft_iface);
2207 primary_if = batadv_primary_if_get_selected(bat_priv);
2208 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2213 hash = bat_priv->tt.global_hash;
2215 while (bucket < hash->size) {
2216 head = &hash->table[bucket];
2218 if (batadv_tt_global_dump_bucket(msg, portid,
2219 cb->nlh->nlmsg_seq, bat_priv,
2230 batadv_hardif_put(primary_if);
2232 dev_put(soft_iface);
2234 cb->args[0] = bucket;
2242 * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
2243 * @tt_global_entry: the global entry to remove the orig_entry from
2244 * @orig_entry: the orig entry to remove and free
2246 * Remove an orig_entry from its list in the given tt_global_entry and
2247 * free this orig_entry afterwards.
2249 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2253 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2254 struct batadv_tt_orig_list_entry *orig_entry)
2256 lockdep_assert_held(&tt_global_entry->list_lock);
2258 batadv_tt_global_size_dec(orig_entry->orig_node,
2259 tt_global_entry->common.vid);
2260 atomic_dec(&tt_global_entry->orig_list_count);
2261 /* requires holding tt_global_entry->list_lock and orig_entry->list
2262 * being part of a list
2264 hlist_del_rcu(&orig_entry->list);
2265 batadv_tt_orig_list_entry_put(orig_entry);
2268 /* deletes the orig list of a tt_global_entry */
2270 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2272 struct hlist_head *head;
2273 struct hlist_node *safe;
2274 struct batadv_tt_orig_list_entry *orig_entry;
2276 spin_lock_bh(&tt_global_entry->list_lock);
2277 head = &tt_global_entry->orig_list;
2278 hlist_for_each_entry_safe(orig_entry, safe, head, list)
2279 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2280 spin_unlock_bh(&tt_global_entry->list_lock);
2284 * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2285 * @bat_priv: the bat priv with all the soft interface information
2286 * @tt_global_entry: the global entry to remove the orig_node from
2287 * @orig_node: the originator announcing the client
2288 * @message: message to append to the log on deletion
2290 * Remove the given orig_node and its according orig_entry from the given
2294 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2295 struct batadv_tt_global_entry *tt_global_entry,
2296 struct batadv_orig_node *orig_node,
2297 const char *message)
2299 struct hlist_head *head;
2300 struct hlist_node *safe;
2301 struct batadv_tt_orig_list_entry *orig_entry;
2304 spin_lock_bh(&tt_global_entry->list_lock);
2305 head = &tt_global_entry->orig_list;
2306 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2307 if (orig_entry->orig_node == orig_node) {
2308 vid = tt_global_entry->common.vid;
2309 batadv_dbg(BATADV_DBG_TT, bat_priv,
2310 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2312 tt_global_entry->common.addr,
2313 batadv_print_vid(vid), message);
2314 _batadv_tt_global_del_orig_entry(tt_global_entry,
2318 spin_unlock_bh(&tt_global_entry->list_lock);
2321 /* If the client is to be deleted, we check if it is the last origantor entry
2322 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2323 * timer, otherwise we simply remove the originator scheduled for deletion.
2326 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2327 struct batadv_tt_global_entry *tt_global_entry,
2328 struct batadv_orig_node *orig_node,
2329 const char *message)
2331 bool last_entry = true;
2332 struct hlist_head *head;
2333 struct batadv_tt_orig_list_entry *orig_entry;
2335 /* no local entry exists, case 1:
2336 * Check if this is the last one or if other entries exist.
2340 head = &tt_global_entry->orig_list;
2341 hlist_for_each_entry_rcu(orig_entry, head, list) {
2342 if (orig_entry->orig_node != orig_node) {
2350 /* its the last one, mark for roaming. */
2351 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2352 tt_global_entry->roam_at = jiffies;
2354 /* there is another entry, we can simply delete this
2355 * one and can still use the other one.
2357 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2358 orig_node, message);
2363 * batadv_tt_global_del() - remove a client from the global table
2364 * @bat_priv: the bat priv with all the soft interface information
2365 * @orig_node: an originator serving this client
2366 * @addr: the mac address of the client
2367 * @vid: VLAN identifier
2368 * @message: a message explaining the reason for deleting the client to print
2369 * for debugging purpose
2370 * @roaming: true if the deletion has been triggered by a roaming event
2372 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2373 struct batadv_orig_node *orig_node,
2374 const unsigned char *addr, unsigned short vid,
2375 const char *message, bool roaming)
2377 struct batadv_tt_global_entry *tt_global_entry;
2378 struct batadv_tt_local_entry *local_entry = NULL;
2380 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2381 if (!tt_global_entry)
2385 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2386 orig_node, message);
2388 if (hlist_empty(&tt_global_entry->orig_list))
2389 batadv_tt_global_free(bat_priv, tt_global_entry,
2395 /* if we are deleting a global entry due to a roam
2396 * event, there are two possibilities:
2397 * 1) the client roamed from node A to node B => if there
2398 * is only one originator left for this client, we mark
2399 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2400 * wait for node B to claim it. In case of timeout
2401 * the entry is purged.
2403 * If there are other originators left, we directly delete
2405 * 2) the client roamed to us => we can directly delete
2406 * the global entry, since it is useless now.
2408 local_entry = batadv_tt_local_hash_find(bat_priv,
2409 tt_global_entry->common.addr,
2412 /* local entry exists, case 2: client roamed to us. */
2413 batadv_tt_global_del_orig_list(tt_global_entry);
2414 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2416 /* no local entry exists, case 1: check for roaming */
2417 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2418 orig_node, message);
2422 if (tt_global_entry)
2423 batadv_tt_global_entry_put(tt_global_entry);
2425 batadv_tt_local_entry_put(local_entry);
2429 * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2430 * the given originator matching the provided vid
2431 * @bat_priv: the bat priv with all the soft interface information
2432 * @orig_node: the originator owning the entries to remove
2433 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2435 * @message: debug message to print as "reason"
2437 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2438 struct batadv_orig_node *orig_node,
2440 const char *message)
2442 struct batadv_tt_global_entry *tt_global;
2443 struct batadv_tt_common_entry *tt_common_entry;
2445 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2446 struct hlist_node *safe;
2447 struct hlist_head *head;
2448 spinlock_t *list_lock; /* protects write access to the hash lists */
2454 for (i = 0; i < hash->size; i++) {
2455 head = &hash->table[i];
2456 list_lock = &hash->list_locks[i];
2458 spin_lock_bh(list_lock);
2459 hlist_for_each_entry_safe(tt_common_entry, safe,
2461 /* remove only matching entries */
2462 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2465 tt_global = container_of(tt_common_entry,
2466 struct batadv_tt_global_entry,
2469 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2470 orig_node, message);
2472 if (hlist_empty(&tt_global->orig_list)) {
2473 vid = tt_global->common.vid;
2474 batadv_dbg(BATADV_DBG_TT, bat_priv,
2475 "Deleting global tt entry %pM (vid: %d): %s\n",
2476 tt_global->common.addr,
2477 batadv_print_vid(vid), message);
2478 hlist_del_rcu(&tt_common_entry->hash_entry);
2479 batadv_tt_global_entry_put(tt_global);
2482 spin_unlock_bh(list_lock);
2484 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2487 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2491 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2492 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2494 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2495 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2497 *msg = "Roaming timeout\n";
2500 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2501 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2503 *msg = "Temporary client timeout\n";
2509 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2511 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2512 struct hlist_head *head;
2513 struct hlist_node *node_tmp;
2514 spinlock_t *list_lock; /* protects write access to the hash lists */
2517 struct batadv_tt_common_entry *tt_common;
2518 struct batadv_tt_global_entry *tt_global;
2520 for (i = 0; i < hash->size; i++) {
2521 head = &hash->table[i];
2522 list_lock = &hash->list_locks[i];
2524 spin_lock_bh(list_lock);
2525 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2527 tt_global = container_of(tt_common,
2528 struct batadv_tt_global_entry,
2531 if (!batadv_tt_global_to_purge(tt_global, &msg))
2534 batadv_dbg(BATADV_DBG_TT, bat_priv,
2535 "Deleting global tt entry %pM (vid: %d): %s\n",
2536 tt_global->common.addr,
2537 batadv_print_vid(tt_global->common.vid),
2540 hlist_del_rcu(&tt_common->hash_entry);
2542 batadv_tt_global_entry_put(tt_global);
2544 spin_unlock_bh(list_lock);
2548 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2550 struct batadv_hashtable *hash;
2551 spinlock_t *list_lock; /* protects write access to the hash lists */
2552 struct batadv_tt_common_entry *tt_common_entry;
2553 struct batadv_tt_global_entry *tt_global;
2554 struct hlist_node *node_tmp;
2555 struct hlist_head *head;
2558 if (!bat_priv->tt.global_hash)
2561 hash = bat_priv->tt.global_hash;
2563 for (i = 0; i < hash->size; i++) {
2564 head = &hash->table[i];
2565 list_lock = &hash->list_locks[i];
2567 spin_lock_bh(list_lock);
2568 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2570 hlist_del_rcu(&tt_common_entry->hash_entry);
2571 tt_global = container_of(tt_common_entry,
2572 struct batadv_tt_global_entry,
2574 batadv_tt_global_entry_put(tt_global);
2576 spin_unlock_bh(list_lock);
2579 batadv_hash_destroy(hash);
2581 bat_priv->tt.global_hash = NULL;
2585 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2586 struct batadv_tt_global_entry *tt_global_entry)
2588 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2589 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2592 /* check if the two clients are marked as isolated */
2593 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2594 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2601 * batadv_transtable_search() - get the mesh destination for a given client
2602 * @bat_priv: the bat priv with all the soft interface information
2603 * @src: mac address of the source client
2604 * @addr: mac address of the destination client
2605 * @vid: VLAN identifier
2607 * Return: a pointer to the originator that was selected as destination in the
2608 * mesh for contacting the client 'addr', NULL otherwise.
2609 * In case of multiple originators serving the same client, the function returns
2610 * the best one (best in terms of metric towards the destination node).
2612 * If the two clients are AP isolated the function returns NULL.
2614 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2619 struct batadv_tt_local_entry *tt_local_entry = NULL;
2620 struct batadv_tt_global_entry *tt_global_entry = NULL;
2621 struct batadv_orig_node *orig_node = NULL;
2622 struct batadv_tt_orig_list_entry *best_entry;
2624 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2625 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2626 if (!tt_local_entry ||
2627 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2631 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2632 if (!tt_global_entry)
2635 /* check whether the clients should not communicate due to AP
2638 if (tt_local_entry &&
2639 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2643 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2644 /* found anything? */
2646 orig_node = best_entry->orig_node;
2647 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2652 if (tt_global_entry)
2653 batadv_tt_global_entry_put(tt_global_entry);
2655 batadv_tt_local_entry_put(tt_local_entry);
2661 * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2662 * to the given orig_node
2663 * @bat_priv: the bat priv with all the soft interface information
2664 * @orig_node: originator for which the CRC should be computed
2665 * @vid: VLAN identifier for which the CRC32 has to be computed
2667 * This function computes the checksum for the global table corresponding to a
2668 * specific originator. In particular, the checksum is computed as follows: For
2669 * each client connected to the originator the CRC32C of the MAC address and the
2670 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2673 * The idea behind is that CRC32C should be used as much as possible in order to
2674 * produce a unique hash of the table, but since the order which is used to feed
2675 * the CRC32C function affects the result and since every node in the network
2676 * probably sorts the clients differently, the hash function cannot be directly
2677 * computed over the entire table. Hence the CRC32C is used only on
2678 * the single client entry, while all the results are then xor'ed together
2679 * because the XOR operation can combine them all while trying to reduce the
2680 * noise as much as possible.
2682 * Return: the checksum of the global table of a given originator.
2684 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2685 struct batadv_orig_node *orig_node,
2688 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2689 struct batadv_tt_orig_list_entry *tt_orig;
2690 struct batadv_tt_common_entry *tt_common;
2691 struct batadv_tt_global_entry *tt_global;
2692 struct hlist_head *head;
2693 u32 i, crc_tmp, crc = 0;
2697 for (i = 0; i < hash->size; i++) {
2698 head = &hash->table[i];
2701 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2702 tt_global = container_of(tt_common,
2703 struct batadv_tt_global_entry,
2705 /* compute the CRC only for entries belonging to the
2706 * VLAN identified by the vid passed as parameter
2708 if (tt_common->vid != vid)
2711 /* Roaming clients are in the global table for
2712 * consistency only. They don't have to be
2713 * taken into account while computing the
2716 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2718 /* Temporary clients have not been announced yet, so
2719 * they have to be skipped while computing the global
2722 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2725 /* find out if this global entry is announced by this
2728 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2733 /* use network order to read the VID: this ensures that
2734 * every node reads the bytes in the same order.
2736 tmp_vid = htons(tt_common->vid);
2737 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2739 /* compute the CRC on flags that have to be kept in sync
2742 flags = tt_orig->flags;
2743 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2745 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2747 batadv_tt_orig_list_entry_put(tt_orig);
2756 * batadv_tt_local_crc() - calculates the checksum of the local table
2757 * @bat_priv: the bat priv with all the soft interface information
2758 * @vid: VLAN identifier for which the CRC32 has to be computed
2760 * For details about the computation, please refer to the documentation for
2761 * batadv_tt_global_crc().
2763 * Return: the checksum of the local table
2765 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2768 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2769 struct batadv_tt_common_entry *tt_common;
2770 struct hlist_head *head;
2771 u32 i, crc_tmp, crc = 0;
2775 for (i = 0; i < hash->size; i++) {
2776 head = &hash->table[i];
2779 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2780 /* compute the CRC only for entries belonging to the
2781 * VLAN identified by vid
2783 if (tt_common->vid != vid)
2786 /* not yet committed clients have not to be taken into
2787 * account while computing the CRC
2789 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2792 /* use network order to read the VID: this ensures that
2793 * every node reads the bytes in the same order.
2795 tmp_vid = htons(tt_common->vid);
2796 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2798 /* compute the CRC on flags that have to be kept in sync
2801 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2802 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2804 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2813 * batadv_tt_req_node_release() - free tt_req node entry
2814 * @ref: kref pointer of the tt req_node entry
2816 static void batadv_tt_req_node_release(struct kref *ref)
2818 struct batadv_tt_req_node *tt_req_node;
2820 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2822 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2826 * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2827 * possibly release it
2828 * @tt_req_node: tt_req_node to be free'd
2830 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2832 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2835 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2837 struct batadv_tt_req_node *node;
2838 struct hlist_node *safe;
2840 spin_lock_bh(&bat_priv->tt.req_list_lock);
2842 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2843 hlist_del_init(&node->list);
2844 batadv_tt_req_node_put(node);
2847 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2850 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2851 struct batadv_orig_node *orig_node,
2852 const void *tt_buff,
2855 /* Replace the old buffer only if I received something in the
2856 * last OGM (the OGM could carry no changes)
2858 spin_lock_bh(&orig_node->tt_buff_lock);
2859 if (tt_buff_len > 0) {
2860 kfree(orig_node->tt_buff);
2861 orig_node->tt_buff_len = 0;
2862 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2863 if (orig_node->tt_buff) {
2864 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2865 orig_node->tt_buff_len = tt_buff_len;
2868 spin_unlock_bh(&orig_node->tt_buff_lock);
2871 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2873 struct batadv_tt_req_node *node;
2874 struct hlist_node *safe;
2876 spin_lock_bh(&bat_priv->tt.req_list_lock);
2877 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2878 if (batadv_has_timed_out(node->issued_at,
2879 BATADV_TT_REQUEST_TIMEOUT)) {
2880 hlist_del_init(&node->list);
2881 batadv_tt_req_node_put(node);
2884 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2888 * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2889 * @bat_priv: the bat priv with all the soft interface information
2890 * @orig_node: orig node this request is being issued for
2892 * Return: the pointer to the new tt_req_node struct if no request
2893 * has already been issued for this orig_node, NULL otherwise.
2895 static struct batadv_tt_req_node *
2896 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2897 struct batadv_orig_node *orig_node)
2899 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2901 spin_lock_bh(&bat_priv->tt.req_list_lock);
2902 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2903 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2904 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2905 BATADV_TT_REQUEST_TIMEOUT))
2909 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2913 kref_init(&tt_req_node->refcount);
2914 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2915 tt_req_node->issued_at = jiffies;
2917 kref_get(&tt_req_node->refcount);
2918 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2920 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2925 * batadv_tt_local_valid() - verify local tt entry and get flags
2926 * @entry_ptr: to be checked local tt entry
2927 * @data_ptr: not used but definition required to satisfy the callback prototype
2928 * @flags: a pointer to store TT flags for this client to
2930 * Checks the validity of the given local TT entry. If it is, then the provided
2931 * flags pointer is updated.
2933 * Return: true if the entry is a valid, false otherwise.
2935 static bool batadv_tt_local_valid(const void *entry_ptr,
2936 const void *data_ptr,
2939 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2941 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2945 *flags = tt_common_entry->flags;
2951 * batadv_tt_global_valid() - verify global tt entry and get flags
2952 * @entry_ptr: to be checked global tt entry
2953 * @data_ptr: an orig_node object (may be NULL)
2954 * @flags: a pointer to store TT flags for this client to
2956 * Checks the validity of the given global TT entry. If it is, then the provided
2957 * flags pointer is updated either with the common (summed) TT flags if data_ptr
2958 * is NULL or the specific, per originator TT flags otherwise.
2960 * Return: true if the entry is a valid, false otherwise.
2962 static bool batadv_tt_global_valid(const void *entry_ptr,
2963 const void *data_ptr,
2966 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2967 const struct batadv_tt_global_entry *tt_global_entry;
2968 const struct batadv_orig_node *orig_node = data_ptr;
2970 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2971 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2974 tt_global_entry = container_of(tt_common_entry,
2975 struct batadv_tt_global_entry,
2978 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2983 * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2985 * @bat_priv: the bat priv with all the soft interface information
2986 * @hash: hash table containing the tt entries
2987 * @tt_len: expected tvlv tt data buffer length in number of bytes
2988 * @tvlv_buff: pointer to the buffer to fill with the TT data
2989 * @valid_cb: function to filter tt change entries and to return TT flags
2990 * @cb_data: data passed to the filter function as argument
2992 * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2993 * is not provided then this becomes a no-op.
2995 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2996 struct batadv_hashtable *hash,
2997 void *tvlv_buff, u16 tt_len,
2998 bool (*valid_cb)(const void *,
3003 struct batadv_tt_common_entry *tt_common_entry;
3004 struct batadv_tvlv_tt_change *tt_change;
3005 struct hlist_head *head;
3006 u16 tt_tot, tt_num_entries = 0;
3011 tt_tot = batadv_tt_entries(tt_len);
3012 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
3018 for (i = 0; i < hash->size; i++) {
3019 head = &hash->table[i];
3021 hlist_for_each_entry_rcu(tt_common_entry,
3023 if (tt_tot == tt_num_entries)
3026 ret = valid_cb(tt_common_entry, cb_data, &flags);
3030 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
3031 tt_change->flags = flags;
3032 tt_change->vid = htons(tt_common_entry->vid);
3033 memset(tt_change->reserved, 0,
3034 sizeof(tt_change->reserved));
3044 * batadv_tt_global_check_crc() - check if all the CRCs are correct
3045 * @orig_node: originator for which the CRCs have to be checked
3046 * @tt_vlan: pointer to the first tvlv VLAN entry
3047 * @num_vlan: number of tvlv VLAN entries
3049 * Return: true if all the received CRCs match the locally stored ones, false
3052 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
3053 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3056 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
3057 struct batadv_orig_node_vlan *vlan;
3058 int i, orig_num_vlan;
3061 /* check if each received CRC matches the locally stored one */
3062 for (i = 0; i < num_vlan; i++) {
3063 tt_vlan_tmp = tt_vlan + i;
3065 /* if orig_node is a backbone node for this VLAN, don't check
3066 * the CRC as we ignore all the global entries over it
3068 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
3070 ntohs(tt_vlan_tmp->vid)))
3073 vlan = batadv_orig_node_vlan_get(orig_node,
3074 ntohs(tt_vlan_tmp->vid));
3079 batadv_orig_node_vlan_put(vlan);
3081 if (crc != ntohl(tt_vlan_tmp->crc))
3085 /* check if any excess VLANs exist locally for the originator
3086 * which are not mentioned in the TVLV from the originator.
3090 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
3094 if (orig_num_vlan > num_vlan)
3101 * batadv_tt_local_update_crc() - update all the local CRCs
3102 * @bat_priv: the bat priv with all the soft interface information
3104 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3106 struct batadv_softif_vlan *vlan;
3108 /* recompute the global CRC for each VLAN */
3110 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3111 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3117 * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
3118 * @bat_priv: the bat priv with all the soft interface information
3119 * @orig_node: the orig_node for which the CRCs have to be updated
3121 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3122 struct batadv_orig_node *orig_node)
3124 struct batadv_orig_node_vlan *vlan;
3127 /* recompute the global CRC for each VLAN */
3129 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
3130 /* if orig_node is a backbone node for this VLAN, don't compute
3131 * the CRC as we ignore all the global entries over it
3133 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3137 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3144 * batadv_send_tt_request() - send a TT Request message to a given node
3145 * @bat_priv: the bat priv with all the soft interface information
3146 * @dst_orig_node: the destination of the message
3147 * @ttvn: the version number that the source of the message is looking for
3148 * @tt_vlan: pointer to the first tvlv VLAN object to request
3149 * @num_vlan: number of tvlv VLAN entries
3150 * @full_table: ask for the entire translation table if true, while only for the
3151 * last TT diff otherwise
3153 * Return: true if the TT Request was sent, false otherwise
3155 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3156 struct batadv_orig_node *dst_orig_node,
3158 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3159 u16 num_vlan, bool full_table)
3161 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3162 struct batadv_tt_req_node *tt_req_node = NULL;
3163 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3164 struct batadv_hard_iface *primary_if;
3168 primary_if = batadv_primary_if_get_selected(bat_priv);
3172 /* The new tt_req will be issued only if I'm not waiting for a
3173 * reply from the same orig_node yet
3175 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
3179 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3180 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
3184 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3185 tvlv_tt_data->ttvn = ttvn;
3186 tvlv_tt_data->num_vlan = htons(num_vlan);
3188 /* send all the CRCs within the request. This is needed by intermediate
3189 * nodes to ensure they have the correct table before replying
3191 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3192 for (i = 0; i < num_vlan; i++) {
3193 tt_vlan_req->vid = tt_vlan->vid;
3194 tt_vlan_req->crc = tt_vlan->crc;
3201 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3203 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
3204 dst_orig_node->orig, full_table ? 'F' : '.');
3206 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
3207 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3208 dst_orig_node->orig, BATADV_TVLV_TT, 1,
3209 tvlv_tt_data, size);
3214 batadv_hardif_put(primary_if);
3216 if (ret && tt_req_node) {
3217 spin_lock_bh(&bat_priv->tt.req_list_lock);
3218 if (!hlist_unhashed(&tt_req_node->list)) {
3219 hlist_del_init(&tt_req_node->list);
3220 batadv_tt_req_node_put(tt_req_node);
3222 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3226 batadv_tt_req_node_put(tt_req_node);
3228 kfree(tvlv_tt_data);
3233 * batadv_send_other_tt_response() - send reply to tt request concerning another
3234 * node's translation table
3235 * @bat_priv: the bat priv with all the soft interface information
3236 * @tt_data: tt data containing the tt request information
3237 * @req_src: mac address of tt request sender
3238 * @req_dst: mac address of tt request recipient
3240 * Return: true if tt request reply was sent, false otherwise.
3242 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3243 struct batadv_tvlv_tt_data *tt_data,
3244 u8 *req_src, u8 *req_dst)
3246 struct batadv_orig_node *req_dst_orig_node;
3247 struct batadv_orig_node *res_dst_orig_node = NULL;
3248 struct batadv_tvlv_tt_change *tt_change;
3249 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3250 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3251 bool ret = false, full_table;
3252 u8 orig_ttvn, req_ttvn;
3256 batadv_dbg(BATADV_DBG_TT, bat_priv,
3257 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3258 req_src, tt_data->ttvn, req_dst,
3259 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3261 /* Let's get the orig node of the REAL destination */
3262 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3263 if (!req_dst_orig_node)
3266 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3267 if (!res_dst_orig_node)
3270 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3271 req_ttvn = tt_data->ttvn;
3273 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3274 /* this node doesn't have the requested data */
3275 if (orig_ttvn != req_ttvn ||
3276 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3277 ntohs(tt_data->num_vlan)))
3280 /* If the full table has been explicitly requested */
3281 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3282 !req_dst_orig_node->tt_buff)
3287 /* TT fragmentation hasn't been implemented yet, so send as many
3288 * TT entries fit a single packet as possible only
3291 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3292 tt_len = req_dst_orig_node->tt_buff_len;
3294 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3301 /* Copy the last orig_node's OGM buffer */
3302 memcpy(tt_change, req_dst_orig_node->tt_buff,
3303 req_dst_orig_node->tt_buff_len);
3304 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3306 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3307 * in the initial part
3310 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3317 /* fill the rest of the tvlv with the real TT entries */
3318 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3320 batadv_tt_global_valid,
3324 /* Don't send the response, if larger than fragmented packet. */
3325 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3326 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3327 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3328 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3329 res_dst_orig_node->orig);
3333 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3334 tvlv_tt_data->ttvn = req_ttvn;
3337 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3339 batadv_dbg(BATADV_DBG_TT, bat_priv,
3340 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3341 res_dst_orig_node->orig, req_dst_orig_node->orig,
3342 full_table ? 'F' : '.', req_ttvn);
3344 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3346 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3347 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3354 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3357 if (res_dst_orig_node)
3358 batadv_orig_node_put(res_dst_orig_node);
3359 if (req_dst_orig_node)
3360 batadv_orig_node_put(req_dst_orig_node);
3361 kfree(tvlv_tt_data);
3366 * batadv_send_my_tt_response() - send reply to tt request concerning this
3367 * node's translation table
3368 * @bat_priv: the bat priv with all the soft interface information
3369 * @tt_data: tt data containing the tt request information
3370 * @req_src: mac address of tt request sender
3372 * Return: true if tt request reply was sent, false otherwise.
3374 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3375 struct batadv_tvlv_tt_data *tt_data,
3378 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3379 struct batadv_hard_iface *primary_if = NULL;
3380 struct batadv_tvlv_tt_change *tt_change;
3381 struct batadv_orig_node *orig_node;
3382 u8 my_ttvn, req_ttvn;
3387 batadv_dbg(BATADV_DBG_TT, bat_priv,
3388 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3389 req_src, tt_data->ttvn,
3390 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3392 spin_lock_bh(&bat_priv->tt.commit_lock);
3394 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3395 req_ttvn = tt_data->ttvn;
3397 orig_node = batadv_orig_hash_find(bat_priv, req_src);
3401 primary_if = batadv_primary_if_get_selected(bat_priv);
3405 /* If the full table has been explicitly requested or the gap
3406 * is too big send the whole local translation table
3408 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3409 !bat_priv->tt.last_changeset)
3414 /* TT fragmentation hasn't been implemented yet, so send as many
3415 * TT entries fit a single packet as possible only
3418 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3420 tt_len = bat_priv->tt.last_changeset_len;
3421 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3425 if (!tt_len || !tvlv_len)
3428 /* Copy the last orig_node's OGM buffer */
3429 memcpy(tt_change, bat_priv->tt.last_changeset,
3430 bat_priv->tt.last_changeset_len);
3431 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3433 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3435 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3436 * in the initial part
3439 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3443 if (!tt_len || !tvlv_len)
3446 /* fill the rest of the tvlv with the real TT entries */
3447 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3449 batadv_tt_local_valid, NULL);
3452 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3453 tvlv_tt_data->ttvn = req_ttvn;
3456 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3458 batadv_dbg(BATADV_DBG_TT, bat_priv,
3459 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3460 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3462 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3464 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3465 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3471 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3473 spin_unlock_bh(&bat_priv->tt.commit_lock);
3475 batadv_orig_node_put(orig_node);
3477 batadv_hardif_put(primary_if);
3478 kfree(tvlv_tt_data);
3479 /* The packet was for this host, so it doesn't need to be re-routed */
3484 * batadv_send_tt_response() - send reply to tt request
3485 * @bat_priv: the bat priv with all the soft interface information
3486 * @tt_data: tt data containing the tt request information
3487 * @req_src: mac address of tt request sender
3488 * @req_dst: mac address of tt request recipient
3490 * Return: true if tt request reply was sent, false otherwise.
3492 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3493 struct batadv_tvlv_tt_data *tt_data,
3494 u8 *req_src, u8 *req_dst)
3496 if (batadv_is_my_mac(bat_priv, req_dst))
3497 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3498 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3502 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3503 struct batadv_orig_node *orig_node,
3504 struct batadv_tvlv_tt_change *tt_change,
3505 u16 tt_num_changes, u8 ttvn)
3510 for (i = 0; i < tt_num_changes; i++) {
3511 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3512 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3513 batadv_tt_global_del(bat_priv, orig_node,
3514 (tt_change + i)->addr,
3515 ntohs((tt_change + i)->vid),
3516 "tt removed by changes",
3519 if (!batadv_tt_global_add(bat_priv, orig_node,
3520 (tt_change + i)->addr,
3521 ntohs((tt_change + i)->vid),
3522 (tt_change + i)->flags, ttvn))
3523 /* In case of problem while storing a
3524 * global_entry, we stop the updating
3525 * procedure without committing the
3526 * ttvn change. This will avoid to send
3527 * corrupted data on tt_request
3532 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3535 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3536 struct batadv_tvlv_tt_change *tt_change,
3537 u8 ttvn, u8 *resp_src,
3540 struct batadv_orig_node *orig_node;
3542 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3546 /* Purge the old table first.. */
3547 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3548 "Received full table");
3550 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3553 spin_lock_bh(&orig_node->tt_buff_lock);
3554 kfree(orig_node->tt_buff);
3555 orig_node->tt_buff_len = 0;
3556 orig_node->tt_buff = NULL;
3557 spin_unlock_bh(&orig_node->tt_buff_lock);
3559 atomic_set(&orig_node->last_ttvn, ttvn);
3563 batadv_orig_node_put(orig_node);
3566 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3567 struct batadv_orig_node *orig_node,
3568 u16 tt_num_changes, u8 ttvn,
3569 struct batadv_tvlv_tt_change *tt_change)
3571 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3572 tt_num_changes, ttvn);
3574 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3575 batadv_tt_len(tt_num_changes));
3576 atomic_set(&orig_node->last_ttvn, ttvn);
3580 * batadv_is_my_client() - check if a client is served by the local node
3581 * @bat_priv: the bat priv with all the soft interface information
3582 * @addr: the mac address of the client to check
3583 * @vid: VLAN identifier
3585 * Return: true if the client is served by this node, false otherwise.
3587 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3590 struct batadv_tt_local_entry *tt_local_entry;
3593 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3594 if (!tt_local_entry)
3596 /* Check if the client has been logically deleted (but is kept for
3597 * consistency purpose)
3599 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3600 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3605 batadv_tt_local_entry_put(tt_local_entry);
3610 * batadv_handle_tt_response() - process incoming tt reply
3611 * @bat_priv: the bat priv with all the soft interface information
3612 * @tt_data: tt data containing the tt request information
3613 * @resp_src: mac address of tt reply sender
3614 * @num_entries: number of tt change entries appended to the tt data
3616 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3617 struct batadv_tvlv_tt_data *tt_data,
3618 u8 *resp_src, u16 num_entries)
3620 struct batadv_tt_req_node *node;
3621 struct hlist_node *safe;
3622 struct batadv_orig_node *orig_node = NULL;
3623 struct batadv_tvlv_tt_change *tt_change;
3624 u8 *tvlv_ptr = (u8 *)tt_data;
3627 batadv_dbg(BATADV_DBG_TT, bat_priv,
3628 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3629 resp_src, tt_data->ttvn, num_entries,
3630 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3632 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3636 spin_lock_bh(&orig_node->tt_lock);
3638 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3639 change_offset *= ntohs(tt_data->num_vlan);
3640 change_offset += sizeof(*tt_data);
3641 tvlv_ptr += change_offset;
3643 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3644 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3645 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3646 resp_src, num_entries);
3648 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3649 tt_data->ttvn, tt_change);
3652 /* Recalculate the CRC for this orig_node and store it */
3653 batadv_tt_global_update_crc(bat_priv, orig_node);
3655 spin_unlock_bh(&orig_node->tt_lock);
3657 /* Delete the tt_req_node from pending tt_requests list */
3658 spin_lock_bh(&bat_priv->tt.req_list_lock);
3659 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3660 if (!batadv_compare_eth(node->addr, resp_src))
3662 hlist_del_init(&node->list);
3663 batadv_tt_req_node_put(node);
3666 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3669 batadv_orig_node_put(orig_node);
3672 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3674 struct batadv_tt_roam_node *node, *safe;
3676 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3678 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3679 list_del(&node->list);
3680 kmem_cache_free(batadv_tt_roam_cache, node);
3683 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3686 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3688 struct batadv_tt_roam_node *node, *safe;
3690 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3691 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3692 if (!batadv_has_timed_out(node->first_time,
3693 BATADV_ROAMING_MAX_TIME))
3696 list_del(&node->list);
3697 kmem_cache_free(batadv_tt_roam_cache, node);
3699 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3703 * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3704 * @bat_priv: the bat priv with all the soft interface information
3705 * @client: mac address of the roaming client
3707 * This function checks whether the client already reached the
3708 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3711 * Return: true if the ROAMING_ADV can be sent, false otherwise
3713 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3715 struct batadv_tt_roam_node *tt_roam_node;
3718 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3719 /* The new tt_req will be issued only if I'm not waiting for a
3720 * reply from the same orig_node yet
3722 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3723 if (!batadv_compare_eth(tt_roam_node->addr, client))
3726 if (batadv_has_timed_out(tt_roam_node->first_time,
3727 BATADV_ROAMING_MAX_TIME))
3730 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3731 /* Sorry, you roamed too many times! */
3738 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3743 tt_roam_node->first_time = jiffies;
3744 atomic_set(&tt_roam_node->counter,
3745 BATADV_ROAMING_MAX_COUNT - 1);
3746 ether_addr_copy(tt_roam_node->addr, client);
3748 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3753 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3758 * batadv_send_roam_adv() - send a roaming advertisement message
3759 * @bat_priv: the bat priv with all the soft interface information
3760 * @client: mac address of the roaming client
3761 * @vid: VLAN identifier
3762 * @orig_node: message destination
3764 * Send a ROAMING_ADV message to the node which was previously serving this
3765 * client. This is done to inform the node that from now on all traffic destined
3766 * for this particular roamed client has to be forwarded to the sender of the
3769 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3771 struct batadv_orig_node *orig_node)
3773 struct batadv_hard_iface *primary_if;
3774 struct batadv_tvlv_roam_adv tvlv_roam;
3776 primary_if = batadv_primary_if_get_selected(bat_priv);
3780 /* before going on we have to check whether the client has
3781 * already roamed to us too many times
3783 if (!batadv_tt_check_roam_count(bat_priv, client))
3786 batadv_dbg(BATADV_DBG_TT, bat_priv,
3787 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3788 orig_node->orig, client, batadv_print_vid(vid));
3790 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3792 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3793 tvlv_roam.vid = htons(vid);
3795 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3796 orig_node->orig, BATADV_TVLV_ROAM, 1,
3797 &tvlv_roam, sizeof(tvlv_roam));
3801 batadv_hardif_put(primary_if);
3804 static void batadv_tt_purge(struct work_struct *work)
3806 struct delayed_work *delayed_work;
3807 struct batadv_priv_tt *priv_tt;
3808 struct batadv_priv *bat_priv;
3810 delayed_work = to_delayed_work(work);
3811 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3812 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3814 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3815 batadv_tt_global_purge(bat_priv);
3816 batadv_tt_req_purge(bat_priv);
3817 batadv_tt_roam_purge(bat_priv);
3819 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3820 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3824 * batadv_tt_free() - Free translation table of soft interface
3825 * @bat_priv: the bat priv with all the soft interface information
3827 void batadv_tt_free(struct batadv_priv *bat_priv)
3829 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3830 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3832 cancel_delayed_work_sync(&bat_priv->tt.work);
3834 batadv_tt_local_table_free(bat_priv);
3835 batadv_tt_global_table_free(bat_priv);
3836 batadv_tt_req_list_free(bat_priv);
3837 batadv_tt_changes_list_free(bat_priv);
3838 batadv_tt_roam_list_free(bat_priv);
3840 kfree(bat_priv->tt.last_changeset);
3844 * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3845 * table and possibly count them in the TT size
3846 * @bat_priv: the bat priv with all the soft interface information
3847 * @flags: the flag to switch
3848 * @enable: whether to set or unset the flag
3849 * @count: whether to increase the TT size by the number of changed entries
3851 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3852 bool enable, bool count)
3854 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3855 struct batadv_tt_common_entry *tt_common_entry;
3856 struct hlist_head *head;
3862 for (i = 0; i < hash->size; i++) {
3863 head = &hash->table[i];
3866 hlist_for_each_entry_rcu(tt_common_entry,
3869 if ((tt_common_entry->flags & flags) == flags)
3871 tt_common_entry->flags |= flags;
3873 if (!(tt_common_entry->flags & flags))
3875 tt_common_entry->flags &= ~flags;
3881 batadv_tt_local_size_inc(bat_priv,
3882 tt_common_entry->vid);
3888 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3889 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3891 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3892 struct batadv_tt_common_entry *tt_common;
3893 struct batadv_tt_local_entry *tt_local;
3894 struct hlist_node *node_tmp;
3895 struct hlist_head *head;
3896 spinlock_t *list_lock; /* protects write access to the hash lists */
3902 for (i = 0; i < hash->size; i++) {
3903 head = &hash->table[i];
3904 list_lock = &hash->list_locks[i];
3906 spin_lock_bh(list_lock);
3907 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3909 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3912 batadv_dbg(BATADV_DBG_TT, bat_priv,
3913 "Deleting local tt entry (%pM, vid: %d): pending\n",
3915 batadv_print_vid(tt_common->vid));
3917 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3918 hlist_del_rcu(&tt_common->hash_entry);
3919 tt_local = container_of(tt_common,
3920 struct batadv_tt_local_entry,
3923 batadv_tt_local_entry_put(tt_local);
3925 spin_unlock_bh(list_lock);
3930 * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3931 * which have been queued in the time since the last commit
3932 * @bat_priv: the bat priv with all the soft interface information
3934 * Caller must hold tt->commit_lock.
3936 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3938 lockdep_assert_held(&bat_priv->tt.commit_lock);
3940 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3941 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3942 batadv_tt_tvlv_container_update(bat_priv);
3946 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3948 batadv_tt_local_purge_pending_clients(bat_priv);
3949 batadv_tt_local_update_crc(bat_priv);
3951 /* Increment the TTVN only once per OGM interval */
3952 atomic_inc(&bat_priv->tt.vn);
3953 batadv_dbg(BATADV_DBG_TT, bat_priv,
3954 "Local changes committed, updating to ttvn %u\n",
3955 (u8)atomic_read(&bat_priv->tt.vn));
3957 /* reset the sending counter */
3958 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3959 batadv_tt_tvlv_container_update(bat_priv);
3963 * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3964 * have been queued in the time since the last commit
3965 * @bat_priv: the bat priv with all the soft interface information
3967 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3969 spin_lock_bh(&bat_priv->tt.commit_lock);
3970 batadv_tt_local_commit_changes_nolock(bat_priv);
3971 spin_unlock_bh(&bat_priv->tt.commit_lock);
3975 * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3976 * @bat_priv: the bat priv with all the soft interface information
3977 * @src: source mac address of packet
3978 * @dst: destination mac address of packet
3979 * @vid: vlan id of packet
3981 * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3983 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3986 struct batadv_tt_local_entry *tt_local_entry;
3987 struct batadv_tt_global_entry *tt_global_entry;
3988 struct batadv_softif_vlan *vlan;
3991 vlan = batadv_softif_vlan_get(bat_priv, vid);
3995 if (!atomic_read(&vlan->ap_isolation))
3998 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3999 if (!tt_local_entry)
4002 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
4003 if (!tt_global_entry)
4004 goto local_entry_put;
4006 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
4009 batadv_tt_global_entry_put(tt_global_entry);
4011 batadv_tt_local_entry_put(tt_local_entry);
4013 batadv_softif_vlan_put(vlan);
4018 * batadv_tt_update_orig() - update global translation table with new tt
4019 * information received via ogms
4020 * @bat_priv: the bat priv with all the soft interface information
4021 * @orig_node: the orig_node of the ogm
4022 * @tt_buff: pointer to the first tvlv VLAN entry
4023 * @tt_num_vlan: number of tvlv VLAN entries
4024 * @tt_change: pointer to the first entry in the TT buffer
4025 * @tt_num_changes: number of tt changes inside the tt buffer
4026 * @ttvn: translation table version number of this changeset
4028 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
4029 struct batadv_orig_node *orig_node,
4030 const void *tt_buff, u16 tt_num_vlan,
4031 struct batadv_tvlv_tt_change *tt_change,
4032 u16 tt_num_changes, u8 ttvn)
4034 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
4035 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4036 bool full_table = true;
4039 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
4040 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
4041 &orig_node->capa_initialized);
4043 /* orig table not initialised AND first diff is in the OGM OR the ttvn
4044 * increased by one -> we can apply the attached changes
4046 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
4047 /* the OGM could not contain the changes due to their size or
4048 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
4050 * In this case send a tt request
4052 if (!tt_num_changes) {
4057 spin_lock_bh(&orig_node->tt_lock);
4059 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
4062 /* Even if we received the precomputed crc with the OGM, we
4063 * prefer to recompute it to spot any possible inconsistency
4064 * in the global table
4066 batadv_tt_global_update_crc(bat_priv, orig_node);
4068 spin_unlock_bh(&orig_node->tt_lock);
4070 /* The ttvn alone is not enough to guarantee consistency
4071 * because a single value could represent different states
4072 * (due to the wrap around). Thus a node has to check whether
4073 * the resulting table (after applying the changes) is still
4074 * consistent or not. E.g. a node could disconnect while its
4075 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
4076 * checking the CRC value is mandatory to detect the
4079 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
4083 /* if we missed more than one change or our tables are not
4084 * in sync anymore -> request fresh tt data
4086 if (!has_tt_init || ttvn != orig_ttvn ||
4087 !batadv_tt_global_check_crc(orig_node, tt_vlan,
4090 batadv_dbg(BATADV_DBG_TT, bat_priv,
4091 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
4092 orig_node->orig, ttvn, orig_ttvn,
4094 batadv_send_tt_request(bat_priv, orig_node, ttvn,
4095 tt_vlan, tt_num_vlan,
4103 * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
4104 * @bat_priv: the bat priv with all the soft interface information
4105 * @addr: the mac address of the client to check
4106 * @vid: VLAN identifier
4108 * Return: true if we know that the client has moved from its old originator
4109 * to another one. This entry is still kept for consistency purposes and will be
4110 * deleted later by a DEL or because of timeout
4112 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
4113 u8 *addr, unsigned short vid)
4115 struct batadv_tt_global_entry *tt_global_entry;
4118 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
4119 if (!tt_global_entry)
4122 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4123 batadv_tt_global_entry_put(tt_global_entry);
4129 * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
4130 * @bat_priv: the bat priv with all the soft interface information
4131 * @addr: the mac address of the local client to query
4132 * @vid: VLAN identifier
4134 * Return: true if the local client is known to be roaming (it is not served by
4135 * this node anymore) or not. If yes, the client is still present in the table
4136 * to keep the latter consistent with the node TTVN
4138 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
4139 u8 *addr, unsigned short vid)
4141 struct batadv_tt_local_entry *tt_local_entry;
4144 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
4145 if (!tt_local_entry)
4148 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4149 batadv_tt_local_entry_put(tt_local_entry);
4155 * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
4156 * @bat_priv: the bat priv with all the soft interface information
4157 * @orig_node: orig node which the temporary entry should be associated with
4158 * @addr: mac address of the client
4159 * @vid: VLAN id of the new temporary global translation table
4161 * Return: true when temporary tt entry could be added, false otherwise
4163 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4164 struct batadv_orig_node *orig_node,
4165 const unsigned char *addr,
4168 /* ignore loop detect macs, they are not supposed to be in the tt local
4171 if (batadv_bla_is_loopdetect_mac(addr))
4174 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
4175 BATADV_TT_CLIENT_TEMP,
4176 atomic_read(&orig_node->last_ttvn)))
4179 batadv_dbg(BATADV_DBG_TT, bat_priv,
4180 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
4181 addr, batadv_print_vid(vid), orig_node->orig);
4187 * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
4188 * maximum packet size that can be transported through the mesh
4189 * @soft_iface: netdev struct of the mesh interface
4191 * Remove entries older than 'timeout' and half timeout if more entries need
4194 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4196 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4197 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4198 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4199 bool reduced = false;
4201 spin_lock_bh(&bat_priv->tt.commit_lock);
4204 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4205 if (packet_size_max >= table_size)
4208 batadv_tt_local_purge(bat_priv, timeout);
4209 batadv_tt_local_purge_pending_clients(bat_priv);
4213 net_ratelimited_function(batadv_info, soft_iface,
4214 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4218 /* commit these changes immediately, to avoid synchronization problem
4222 batadv_tt_local_commit_changes_nolock(bat_priv);
4224 spin_unlock_bh(&bat_priv->tt.commit_lock);
4228 * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
4229 * @bat_priv: the bat priv with all the soft interface information
4230 * @orig: the orig_node of the ogm
4231 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4232 * @tvlv_value: tvlv buffer containing the gateway data
4233 * @tvlv_value_len: tvlv buffer length
4235 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4236 struct batadv_orig_node *orig,
4237 u8 flags, void *tvlv_value,
4240 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4241 struct batadv_tvlv_tt_change *tt_change;
4242 struct batadv_tvlv_tt_data *tt_data;
4243 u16 num_entries, num_vlan;
4245 if (tvlv_value_len < sizeof(*tt_data))
4248 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4249 tvlv_value_len -= sizeof(*tt_data);
4251 num_vlan = ntohs(tt_data->num_vlan);
4253 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4256 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4257 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4258 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4260 num_entries = batadv_tt_entries(tvlv_value_len);
4262 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4263 num_entries, tt_data->ttvn);
4267 * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
4269 * @bat_priv: the bat priv with all the soft interface information
4270 * @src: mac address of tt tvlv sender
4271 * @dst: mac address of tt tvlv recipient
4272 * @tvlv_value: tvlv buffer containing the tt data
4273 * @tvlv_value_len: tvlv buffer length
4275 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4278 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4283 struct batadv_tvlv_tt_data *tt_data;
4284 u16 tt_vlan_len, tt_num_entries;
4288 if (tvlv_value_len < sizeof(*tt_data))
4289 return NET_RX_SUCCESS;
4291 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4292 tvlv_value_len -= sizeof(*tt_data);
4294 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4295 tt_vlan_len *= ntohs(tt_data->num_vlan);
4297 if (tvlv_value_len < tt_vlan_len)
4298 return NET_RX_SUCCESS;
4300 tvlv_value_len -= tt_vlan_len;
4301 tt_num_entries = batadv_tt_entries(tvlv_value_len);
4303 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4304 case BATADV_TT_REQUEST:
4305 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4307 /* If this node cannot provide a TT response the tt_request is
4310 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4312 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4317 batadv_dbg(BATADV_DBG_TT, bat_priv,
4318 "Routing TT_REQUEST to %pM [%c]\n",
4320 /* tvlv API will re-route the packet */
4324 case BATADV_TT_RESPONSE:
4325 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4327 if (batadv_is_my_mac(bat_priv, dst)) {
4328 batadv_handle_tt_response(bat_priv, tt_data,
4329 src, tt_num_entries);
4330 return NET_RX_SUCCESS;
4333 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4338 batadv_dbg(BATADV_DBG_TT, bat_priv,
4339 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4341 /* tvlv API will re-route the packet */
4345 return NET_RX_SUCCESS;
4349 * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4351 * @bat_priv: the bat priv with all the soft interface information
4352 * @src: mac address of tt tvlv sender
4353 * @dst: mac address of tt tvlv recipient
4354 * @tvlv_value: tvlv buffer containing the tt data
4355 * @tvlv_value_len: tvlv buffer length
4357 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4360 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4365 struct batadv_tvlv_roam_adv *roaming_adv;
4366 struct batadv_orig_node *orig_node = NULL;
4368 /* If this node is not the intended recipient of the
4369 * roaming advertisement the packet is forwarded
4370 * (the tvlv API will re-route the packet).
4372 if (!batadv_is_my_mac(bat_priv, dst))
4375 if (tvlv_value_len < sizeof(*roaming_adv))
4378 orig_node = batadv_orig_hash_find(bat_priv, src);
4382 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4383 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4385 batadv_dbg(BATADV_DBG_TT, bat_priv,
4386 "Received ROAMING_ADV from %pM (client %pM)\n",
4387 src, roaming_adv->client);
4389 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4390 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4391 atomic_read(&orig_node->last_ttvn) + 1);
4395 batadv_orig_node_put(orig_node);
4396 return NET_RX_SUCCESS;
4400 * batadv_tt_init() - initialise the translation table internals
4401 * @bat_priv: the bat priv with all the soft interface information
4403 * Return: 0 on success or negative error number in case of failure.
4405 int batadv_tt_init(struct batadv_priv *bat_priv)
4409 /* synchronized flags must be remote */
4410 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4412 ret = batadv_tt_local_init(bat_priv);
4416 ret = batadv_tt_global_init(bat_priv);
4420 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4421 batadv_tt_tvlv_unicast_handler_v1,
4422 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4424 batadv_tvlv_handler_register(bat_priv, NULL,
4425 batadv_roam_tvlv_unicast_handler_v1,
4426 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4428 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4429 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4430 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4436 * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4437 * @bat_priv: the bat priv with all the soft interface information
4438 * @addr: the mac address of the client
4439 * @vid: the identifier of the VLAN where this client is connected
4441 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4444 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4445 const u8 *addr, unsigned short vid)
4447 struct batadv_tt_global_entry *tt;
4450 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4454 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4456 batadv_tt_global_entry_put(tt);
4462 * batadv_tt_cache_init() - Initialize tt memory object cache
4464 * Return: 0 on success or negative error number in case of failure.
4466 int __init batadv_tt_cache_init(void)
4468 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4469 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4470 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4471 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4472 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4473 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4475 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4476 SLAB_HWCACHE_ALIGN, NULL);
4477 if (!batadv_tl_cache)
4480 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4481 SLAB_HWCACHE_ALIGN, NULL);
4482 if (!batadv_tg_cache)
4483 goto err_tt_tl_destroy;
4485 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4487 SLAB_HWCACHE_ALIGN, NULL);
4488 if (!batadv_tt_orig_cache)
4489 goto err_tt_tg_destroy;
4491 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4493 SLAB_HWCACHE_ALIGN, NULL);
4494 if (!batadv_tt_change_cache)
4495 goto err_tt_orig_destroy;
4497 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4499 SLAB_HWCACHE_ALIGN, NULL);
4500 if (!batadv_tt_req_cache)
4501 goto err_tt_change_destroy;
4503 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4505 SLAB_HWCACHE_ALIGN, NULL);
4506 if (!batadv_tt_roam_cache)
4507 goto err_tt_req_destroy;
4512 kmem_cache_destroy(batadv_tt_req_cache);
4513 batadv_tt_req_cache = NULL;
4514 err_tt_change_destroy:
4515 kmem_cache_destroy(batadv_tt_change_cache);
4516 batadv_tt_change_cache = NULL;
4517 err_tt_orig_destroy:
4518 kmem_cache_destroy(batadv_tt_orig_cache);
4519 batadv_tt_orig_cache = NULL;
4521 kmem_cache_destroy(batadv_tg_cache);
4522 batadv_tg_cache = NULL;
4524 kmem_cache_destroy(batadv_tl_cache);
4525 batadv_tl_cache = NULL;
4531 * batadv_tt_cache_destroy() - Destroy tt memory object cache
4533 void batadv_tt_cache_destroy(void)
4535 kmem_cache_destroy(batadv_tl_cache);
4536 kmem_cache_destroy(batadv_tg_cache);
4537 kmem_cache_destroy(batadv_tt_orig_cache);
4538 kmem_cache_destroy(batadv_tt_change_cache);
4539 kmem_cache_destroy(batadv_tt_req_cache);
4540 kmem_cache_destroy(batadv_tt_roam_cache);