1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
7 #include "translation-table.h"
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/build_bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/cache.h>
15 #include <linux/compiler.h>
16 #include <linux/crc32c.h>
17 #include <linux/errno.h>
18 #include <linux/etherdevice.h>
19 #include <linux/gfp.h>
20 #include <linux/if_ether.h>
21 #include <linux/init.h>
22 #include <linux/jhash.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/net.h>
29 #include <linux/netdevice.h>
30 #include <linux/netlink.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/seq_file.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/workqueue.h>
40 #include <net/genetlink.h>
41 #include <net/netlink.h>
43 #include <uapi/linux/batadv_packet.h>
44 #include <uapi/linux/batman_adv.h>
46 #include "bridge_loop_avoidance.h"
47 #include "hard-interface.h"
51 #include "originator.h"
52 #include "soft-interface.h"
55 static struct kmem_cache *batadv_tl_cache __read_mostly;
56 static struct kmem_cache *batadv_tg_cache __read_mostly;
57 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
58 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
59 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
60 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
63 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
64 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
66 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
68 struct batadv_orig_node *orig_node);
69 static void batadv_tt_purge(struct work_struct *work);
71 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
72 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
73 struct batadv_orig_node *orig_node,
74 const unsigned char *addr,
75 unsigned short vid, const char *message,
79 * batadv_compare_tt() - check if two TT entries are the same
80 * @node: the list element pointer of the first TT entry
81 * @data2: pointer to the tt_common_entry of the second TT entry
83 * Compare the MAC address and the VLAN ID of the two TT entries and check if
84 * they are the same TT client.
85 * Return: true if the two TT clients are the same, false otherwise
87 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
89 const void *data1 = container_of(node, struct batadv_tt_common_entry,
91 const struct batadv_tt_common_entry *tt1 = data1;
92 const struct batadv_tt_common_entry *tt2 = data2;
94 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
98 * batadv_choose_tt() - return the index of the tt entry in the hash table
99 * @data: pointer to the tt_common_entry object to map
100 * @size: the size of the hash table
102 * Return: the hash index where the object represented by 'data' should be
105 static inline u32 batadv_choose_tt(const void *data, u32 size)
107 struct batadv_tt_common_entry *tt;
110 tt = (struct batadv_tt_common_entry *)data;
111 hash = jhash(&tt->addr, ETH_ALEN, hash);
112 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
118 * batadv_tt_hash_find() - look for a client in the given hash table
119 * @hash: the hash table to search
120 * @addr: the mac address of the client to look for
121 * @vid: VLAN identifier
123 * Return: a pointer to the tt_common struct belonging to the searched client if
124 * found, NULL otherwise.
126 static struct batadv_tt_common_entry *
127 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
130 struct hlist_head *head;
131 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
137 ether_addr_copy(to_search.addr, addr);
140 index = batadv_choose_tt(&to_search, hash->size);
141 head = &hash->table[index];
144 hlist_for_each_entry_rcu(tt, head, hash_entry) {
145 if (!batadv_compare_eth(tt, addr))
151 if (!kref_get_unless_zero(&tt->refcount))
163 * batadv_tt_local_hash_find() - search the local table for a given client
164 * @bat_priv: the bat priv with all the soft interface information
165 * @addr: the mac address of the client to look for
166 * @vid: VLAN identifier
168 * Return: a pointer to the corresponding tt_local_entry struct if the client is
169 * found, NULL otherwise.
171 static struct batadv_tt_local_entry *
172 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
175 struct batadv_tt_common_entry *tt_common_entry;
176 struct batadv_tt_local_entry *tt_local_entry = NULL;
178 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
181 tt_local_entry = container_of(tt_common_entry,
182 struct batadv_tt_local_entry,
184 return tt_local_entry;
188 * batadv_tt_global_hash_find() - search the global table for a given client
189 * @bat_priv: the bat priv with all the soft interface information
190 * @addr: the mac address of the client to look for
191 * @vid: VLAN identifier
193 * Return: a pointer to the corresponding tt_global_entry struct if the client
194 * is found, NULL otherwise.
196 struct batadv_tt_global_entry *
197 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
200 struct batadv_tt_common_entry *tt_common_entry;
201 struct batadv_tt_global_entry *tt_global_entry = NULL;
203 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
206 tt_global_entry = container_of(tt_common_entry,
207 struct batadv_tt_global_entry,
209 return tt_global_entry;
213 * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
214 * @rcu: rcu pointer of the tt_local_entry
216 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
218 struct batadv_tt_local_entry *tt_local_entry;
220 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
223 kmem_cache_free(batadv_tl_cache, tt_local_entry);
227 * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
228 * for free after rcu grace period
229 * @ref: kref pointer of the nc_node
231 static void batadv_tt_local_entry_release(struct kref *ref)
233 struct batadv_tt_local_entry *tt_local_entry;
235 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
238 batadv_softif_vlan_put(tt_local_entry->vlan);
240 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
244 * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
245 * possibly release it
246 * @tt_local_entry: tt_local_entry to be free'd
249 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
254 kref_put(&tt_local_entry->common.refcount,
255 batadv_tt_local_entry_release);
259 * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
260 * @rcu: rcu pointer of the tt_global_entry
262 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
264 struct batadv_tt_global_entry *tt_global_entry;
266 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
269 kmem_cache_free(batadv_tg_cache, tt_global_entry);
273 * batadv_tt_global_entry_release() - release tt_global_entry from lists and
274 * queue for free after rcu grace period
275 * @ref: kref pointer of the nc_node
277 void batadv_tt_global_entry_release(struct kref *ref)
279 struct batadv_tt_global_entry *tt_global_entry;
281 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
284 batadv_tt_global_del_orig_list(tt_global_entry);
286 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
290 * batadv_tt_global_hash_count() - count the number of orig entries
291 * @bat_priv: the bat priv with all the soft interface information
292 * @addr: the mac address of the client to count entries for
293 * @vid: VLAN identifier
295 * Return: the number of originators advertising the given address/data
296 * (excluding our self).
298 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
299 const u8 *addr, unsigned short vid)
301 struct batadv_tt_global_entry *tt_global_entry;
304 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
305 if (!tt_global_entry)
308 count = atomic_read(&tt_global_entry->orig_list_count);
309 batadv_tt_global_entry_put(tt_global_entry);
315 * batadv_tt_local_size_mod() - change the size by v of the local table
317 * @bat_priv: the bat priv with all the soft interface information
318 * @vid: the VLAN identifier of the sub-table to change
319 * @v: the amount to sum to the local table size
321 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
322 unsigned short vid, int v)
324 struct batadv_softif_vlan *vlan;
326 vlan = batadv_softif_vlan_get(bat_priv, vid);
330 atomic_add(v, &vlan->tt.num_entries);
332 batadv_softif_vlan_put(vlan);
336 * batadv_tt_local_size_inc() - increase by one the local table size for the
338 * @bat_priv: the bat priv with all the soft interface information
339 * @vid: the VLAN identifier
341 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
344 batadv_tt_local_size_mod(bat_priv, vid, 1);
348 * batadv_tt_local_size_dec() - decrease by one the local table size for the
350 * @bat_priv: the bat priv with all the soft interface information
351 * @vid: the VLAN identifier
353 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
356 batadv_tt_local_size_mod(bat_priv, vid, -1);
360 * batadv_tt_global_size_mod() - change the size by v of the global table
361 * for orig_node identified by vid
362 * @orig_node: the originator for which the table has to be modified
363 * @vid: the VLAN identifier
364 * @v: the amount to sum to the global table size
366 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
367 unsigned short vid, int v)
369 struct batadv_orig_node_vlan *vlan;
371 vlan = batadv_orig_node_vlan_new(orig_node, vid);
375 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
376 spin_lock_bh(&orig_node->vlan_list_lock);
377 if (!hlist_unhashed(&vlan->list)) {
378 hlist_del_init_rcu(&vlan->list);
379 batadv_orig_node_vlan_put(vlan);
381 spin_unlock_bh(&orig_node->vlan_list_lock);
384 batadv_orig_node_vlan_put(vlan);
388 * batadv_tt_global_size_inc() - increase by one the global table size for the
390 * @orig_node: the originator which global table size has to be decreased
391 * @vid: the vlan identifier
393 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
396 batadv_tt_global_size_mod(orig_node, vid, 1);
400 * batadv_tt_global_size_dec() - decrease by one the global table size for the
402 * @orig_node: the originator which global table size has to be decreased
403 * @vid: the vlan identifier
405 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
408 batadv_tt_global_size_mod(orig_node, vid, -1);
412 * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
413 * @rcu: rcu pointer of the orig_entry
415 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
417 struct batadv_tt_orig_list_entry *orig_entry;
419 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
421 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
425 * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
426 * queue for free after rcu grace period
427 * @ref: kref pointer of the tt orig entry
429 static void batadv_tt_orig_list_entry_release(struct kref *ref)
431 struct batadv_tt_orig_list_entry *orig_entry;
433 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
436 batadv_orig_node_put(orig_entry->orig_node);
437 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
441 * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
442 * possibly release it
443 * @orig_entry: tt orig entry to be free'd
446 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
451 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
455 * batadv_tt_local_event() - store a local TT event (ADD/DEL)
456 * @bat_priv: the bat priv with all the soft interface information
457 * @tt_local_entry: the TT entry involved in the event
458 * @event_flags: flags to store in the event structure
460 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
461 struct batadv_tt_local_entry *tt_local_entry,
464 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
465 struct batadv_tt_common_entry *common = &tt_local_entry->common;
466 u8 flags = common->flags | event_flags;
467 bool event_removed = false;
468 bool del_op_requested, del_op_entry;
470 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
474 tt_change_node->change.flags = flags;
475 memset(tt_change_node->change.reserved, 0,
476 sizeof(tt_change_node->change.reserved));
477 ether_addr_copy(tt_change_node->change.addr, common->addr);
478 tt_change_node->change.vid = htons(common->vid);
480 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
482 /* check for ADD+DEL or DEL+ADD events */
483 spin_lock_bh(&bat_priv->tt.changes_list_lock);
484 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
486 if (!batadv_compare_eth(entry->change.addr, common->addr))
489 /* DEL+ADD in the same orig interval have no effect and can be
490 * removed to avoid silly behaviour on the receiver side. The
491 * other way around (ADD+DEL) can happen in case of roaming of
492 * a client still in the NEW state. Roaming of NEW clients is
493 * now possible due to automatically recognition of "temporary"
496 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
497 if (!del_op_requested && del_op_entry)
499 if (del_op_requested && !del_op_entry)
502 /* this is a second add in the same originator interval. It
503 * means that flags have been changed: update them!
505 if (!del_op_requested && !del_op_entry)
506 entry->change.flags = flags;
510 list_del(&entry->list);
511 kmem_cache_free(batadv_tt_change_cache, entry);
512 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
513 event_removed = true;
517 /* track the change in the OGMinterval list */
518 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
521 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
524 atomic_dec(&bat_priv->tt.local_changes);
526 atomic_inc(&bat_priv->tt.local_changes);
530 * batadv_tt_len() - compute length in bytes of given number of tt changes
531 * @changes_num: number of tt changes
533 * Return: computed length in bytes.
535 static int batadv_tt_len(int changes_num)
537 return changes_num * sizeof(struct batadv_tvlv_tt_change);
541 * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
542 * @tt_len: available space
544 * Return: the number of entries.
546 static u16 batadv_tt_entries(u16 tt_len)
548 return tt_len / batadv_tt_len(1);
552 * batadv_tt_local_table_transmit_size() - calculates the local translation
553 * table size when transmitted over the air
554 * @bat_priv: the bat priv with all the soft interface information
556 * Return: local translation table size in bytes.
558 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
561 u16 tt_local_entries = 0;
562 struct batadv_softif_vlan *vlan;
566 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
568 tt_local_entries += atomic_read(&vlan->tt.num_entries);
572 /* header size of tvlv encapsulated tt response payload */
573 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
574 hdr_size += sizeof(struct batadv_tvlv_hdr);
575 hdr_size += sizeof(struct batadv_tvlv_tt_data);
576 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
578 return hdr_size + batadv_tt_len(tt_local_entries);
581 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
583 if (bat_priv->tt.local_hash)
586 bat_priv->tt.local_hash = batadv_hash_new(1024);
588 if (!bat_priv->tt.local_hash)
591 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
592 &batadv_tt_local_hash_lock_class_key);
597 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
598 struct batadv_tt_global_entry *tt_global,
601 struct batadv_tt_global_entry *tt_removed_entry;
602 struct hlist_node *tt_removed_node;
604 batadv_dbg(BATADV_DBG_TT, bat_priv,
605 "Deleting global tt entry %pM (vid: %d): %s\n",
606 tt_global->common.addr,
607 batadv_print_vid(tt_global->common.vid), message);
609 tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
613 if (!tt_removed_node)
616 /* drop reference of remove hash entry */
617 tt_removed_entry = hlist_entry(tt_removed_node,
618 struct batadv_tt_global_entry,
620 batadv_tt_global_entry_put(tt_removed_entry);
624 * batadv_tt_local_add() - add a new client to the local table or update an
626 * @soft_iface: netdev struct of the mesh interface
627 * @addr: the mac address of the client to add
628 * @vid: VLAN identifier
629 * @ifindex: index of the interface where the client is connected to (useful to
630 * identify wireless clients)
631 * @mark: the value contained in the skb->mark field of the received packet (if
634 * Return: true if the client was successfully added, false otherwise.
636 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
637 unsigned short vid, int ifindex, u32 mark)
639 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
640 struct batadv_tt_local_entry *tt_local;
641 struct batadv_tt_global_entry *tt_global = NULL;
642 struct net *net = dev_net(soft_iface);
643 struct batadv_softif_vlan *vlan;
644 struct net_device *in_dev = NULL;
645 struct batadv_hard_iface *in_hardif = NULL;
646 struct hlist_head *head;
647 struct batadv_tt_orig_list_entry *orig_entry;
648 int hash_added, table_size, packet_size_max;
650 bool roamed_back = false;
654 if (ifindex != BATADV_NULL_IFINDEX)
655 in_dev = dev_get_by_index(net, ifindex);
658 in_hardif = batadv_hardif_get_by_netdev(in_dev);
660 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
662 if (!is_multicast_ether_addr(addr))
663 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
666 tt_local->last_seen = jiffies;
667 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
668 batadv_dbg(BATADV_DBG_TT, bat_priv,
669 "Re-adding pending client %pM (vid: %d)\n",
670 addr, batadv_print_vid(vid));
671 /* whatever the reason why the PENDING flag was set,
672 * this is a client which was enqueued to be removed in
673 * this orig_interval. Since it popped up again, the
674 * flag can be reset like it was never enqueued
676 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
680 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
681 batadv_dbg(BATADV_DBG_TT, bat_priv,
682 "Roaming client %pM (vid: %d) came back to its original location\n",
683 addr, batadv_print_vid(vid));
684 /* the ROAM flag is set because this client roamed away
685 * and the node got a roaming_advertisement message. Now
686 * that the client popped up again at its original
687 * location such flag can be unset
689 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
695 /* Ignore the client if we cannot send it in a full table response. */
696 table_size = batadv_tt_local_table_transmit_size(bat_priv);
697 table_size += batadv_tt_len(1);
698 packet_size_max = atomic_read(&bat_priv->packet_size_max);
699 if (table_size > packet_size_max) {
700 net_ratelimited_function(batadv_info, soft_iface,
701 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
702 table_size, packet_size_max, addr);
706 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
710 /* increase the refcounter of the related vlan */
711 vlan = batadv_softif_vlan_get(bat_priv, vid);
713 net_ratelimited_function(batadv_info, soft_iface,
714 "adding TT local entry %pM to non-existent VLAN %d\n",
715 addr, batadv_print_vid(vid));
716 kmem_cache_free(batadv_tl_cache, tt_local);
721 batadv_dbg(BATADV_DBG_TT, bat_priv,
722 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
723 addr, batadv_print_vid(vid),
724 (u8)atomic_read(&bat_priv->tt.vn));
726 ether_addr_copy(tt_local->common.addr, addr);
727 /* The local entry has to be marked as NEW to avoid to send it in
728 * a full table response going out before the next ttvn increment
729 * (consistency check)
731 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
732 tt_local->common.vid = vid;
733 if (batadv_is_wifi_hardif(in_hardif))
734 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
735 kref_init(&tt_local->common.refcount);
736 tt_local->last_seen = jiffies;
737 tt_local->common.added_at = tt_local->last_seen;
738 tt_local->vlan = vlan;
740 /* the batman interface mac and multicast addresses should never be
743 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
744 is_multicast_ether_addr(addr))
745 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
747 kref_get(&tt_local->common.refcount);
748 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
749 batadv_choose_tt, &tt_local->common,
750 &tt_local->common.hash_entry);
752 if (unlikely(hash_added != 0)) {
753 /* remove the reference for the hash */
754 batadv_tt_local_entry_put(tt_local);
759 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
762 /* Check whether it is a roaming, but don't do anything if the roaming
763 * process has already been handled
765 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
766 /* These node are probably going to update their tt table */
767 head = &tt_global->orig_list;
769 hlist_for_each_entry_rcu(orig_entry, head, list) {
770 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
771 tt_global->common.vid,
772 orig_entry->orig_node);
776 batadv_tt_global_free(bat_priv, tt_global,
779 /* The global entry has to be marked as ROAMING and
780 * has to be kept for consistency purpose
782 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
783 tt_global->roam_at = jiffies;
787 /* store the current remote flags before altering them. This helps
788 * understanding is flags are changing or not
790 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
792 if (batadv_is_wifi_hardif(in_hardif))
793 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
795 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
797 /* check the mark in the skb: if it's equal to the configured
798 * isolation_mark, it means the packet is coming from an isolated
801 match_mark = (mark & bat_priv->isolation_mark_mask);
802 if (bat_priv->isolation_mark_mask &&
803 match_mark == bat_priv->isolation_mark)
804 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
806 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
808 /* if any "dynamic" flag has been modified, resend an ADD event for this
809 * entry so that all the nodes can get the new flags
811 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
812 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
817 batadv_hardif_put(in_hardif);
821 batadv_tt_local_entry_put(tt_local);
823 batadv_tt_global_entry_put(tt_global);
828 * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
829 * within a TT Response directed to another node
830 * @orig_node: originator for which the TT data has to be prepared
831 * @tt_data: uninitialised pointer to the address of the TVLV buffer
832 * @tt_change: uninitialised pointer to the address of the area where the TT
833 * changed can be stored
834 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
835 * function reserves the amount of space needed to send the entire global TT
836 * table. In case of success the value is updated with the real amount of
838 * Allocate the needed amount of memory for the entire TT TVLV and write its
839 * header made up of one tvlv_tt_data object and a series of tvlv_tt_vlan_data
840 * objects, one per active VLAN served by the originator node.
842 * Return: the size of the allocated buffer or 0 in case of failure.
845 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
846 struct batadv_tvlv_tt_data **tt_data,
847 struct batadv_tvlv_tt_change **tt_change,
854 struct batadv_tvlv_tt_vlan_data *tt_vlan;
855 struct batadv_orig_node_vlan *vlan;
858 spin_lock_bh(&orig_node->vlan_list_lock);
859 hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
861 num_entries += atomic_read(&vlan->tt.num_entries);
864 change_offset = sizeof(**tt_data);
865 change_offset += num_vlan * sizeof(*tt_vlan);
867 /* if tt_len is negative, allocate the space needed by the full table */
869 *tt_len = batadv_tt_len(num_entries);
872 tvlv_len += change_offset;
874 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
880 (*tt_data)->flags = BATADV_NO_FLAGS;
881 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
882 (*tt_data)->num_vlan = htons(num_vlan);
884 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
885 hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
886 tt_vlan->vid = htons(vlan->vid);
887 tt_vlan->crc = htonl(vlan->tt.crc);
888 tt_vlan->reserved = 0;
893 tt_change_ptr = (u8 *)*tt_data + change_offset;
894 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
897 spin_unlock_bh(&orig_node->vlan_list_lock);
902 * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
904 * @bat_priv: the bat priv with all the soft interface information
905 * @tt_data: uninitialised pointer to the address of the TVLV buffer
906 * @tt_change: uninitialised pointer to the address of the area where the TT
907 * changes can be stored
908 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
909 * function reserves the amount of space needed to send the entire local TT
910 * table. In case of success the value is updated with the real amount of
913 * Allocate the needed amount of memory for the entire TT TVLV and write its
914 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
915 * objects, one per active VLAN.
917 * Return: the size of the allocated buffer or 0 in case of failure.
920 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
921 struct batadv_tvlv_tt_data **tt_data,
922 struct batadv_tvlv_tt_change **tt_change,
925 struct batadv_tvlv_tt_vlan_data *tt_vlan;
926 struct batadv_softif_vlan *vlan;
928 u16 vlan_entries = 0;
929 u16 total_entries = 0;
934 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
935 hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
936 vlan_entries = atomic_read(&vlan->tt.num_entries);
937 if (vlan_entries < 1)
941 total_entries += vlan_entries;
944 change_offset = sizeof(**tt_data);
945 change_offset += num_vlan * sizeof(*tt_vlan);
947 /* if tt_len is negative, allocate the space needed by the full table */
949 *tt_len = batadv_tt_len(total_entries);
952 tvlv_len += change_offset;
954 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
960 (*tt_data)->flags = BATADV_NO_FLAGS;
961 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
962 (*tt_data)->num_vlan = htons(num_vlan);
964 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
965 hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
966 vlan_entries = atomic_read(&vlan->tt.num_entries);
967 if (vlan_entries < 1)
970 tt_vlan->vid = htons(vlan->vid);
971 tt_vlan->crc = htonl(vlan->tt.crc);
972 tt_vlan->reserved = 0;
977 tt_change_ptr = (u8 *)*tt_data + change_offset;
978 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
981 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
986 * batadv_tt_tvlv_container_update() - update the translation table tvlv
987 * container after local tt changes have been committed
988 * @bat_priv: the bat priv with all the soft interface information
990 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
992 struct batadv_tt_change_node *entry, *safe;
993 struct batadv_tvlv_tt_data *tt_data;
994 struct batadv_tvlv_tt_change *tt_change;
995 int tt_diff_len, tt_change_len = 0;
996 int tt_diff_entries_num = 0;
997 int tt_diff_entries_count = 0;
1000 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
1001 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
1003 /* if we have too many changes for one packet don't send any
1004 * and wait for the tt table request which will be fragmented
1006 if (tt_diff_len > bat_priv->soft_iface->mtu)
1009 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1010 &tt_change, &tt_diff_len);
1014 tt_data->flags = BATADV_TT_OGM_DIFF;
1016 if (tt_diff_len == 0)
1017 goto container_register;
1019 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1020 atomic_set(&bat_priv->tt.local_changes, 0);
1022 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1024 if (tt_diff_entries_count < tt_diff_entries_num) {
1025 memcpy(tt_change + tt_diff_entries_count,
1027 sizeof(struct batadv_tvlv_tt_change));
1028 tt_diff_entries_count++;
1030 list_del(&entry->list);
1031 kmem_cache_free(batadv_tt_change_cache, entry);
1033 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1035 /* Keep the buffer for possible tt_request */
1036 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1037 kfree(bat_priv->tt.last_changeset);
1038 bat_priv->tt.last_changeset_len = 0;
1039 bat_priv->tt.last_changeset = NULL;
1040 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1041 /* check whether this new OGM has no changes due to size problems */
1042 if (tt_diff_entries_count > 0) {
1043 /* if kmalloc() fails we will reply with the full table
1044 * instead of providing the diff
1046 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1047 if (bat_priv->tt.last_changeset) {
1048 memcpy(bat_priv->tt.last_changeset,
1049 tt_change, tt_change_len);
1050 bat_priv->tt.last_changeset_len = tt_diff_len;
1053 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1056 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1061 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1064 * batadv_tt_local_seq_print_text() - Print the local tt table in a seq file
1065 * @seq: seq file to print on
1070 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
1072 struct net_device *net_dev = (struct net_device *)seq->private;
1073 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1074 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1075 struct batadv_tt_common_entry *tt_common_entry;
1076 struct batadv_tt_local_entry *tt_local;
1077 struct batadv_hard_iface *primary_if;
1078 struct hlist_head *head;
1081 int last_seen_msecs;
1082 unsigned long last_seen_jiffies;
1084 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1086 primary_if = batadv_seq_print_text_primary_if_get(seq);
1091 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1092 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1094 " Client VID Flags Last seen (CRC )\n");
1096 for (i = 0; i < hash->size; i++) {
1097 head = &hash->table[i];
1100 hlist_for_each_entry_rcu(tt_common_entry,
1102 tt_local = container_of(tt_common_entry,
1103 struct batadv_tt_local_entry,
1105 last_seen_jiffies = jiffies - tt_local->last_seen;
1106 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1107 last_seen_secs = last_seen_msecs / 1000;
1108 last_seen_msecs = last_seen_msecs % 1000;
1110 no_purge = tt_common_entry->flags & np_flag;
1112 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1113 tt_common_entry->addr,
1114 batadv_print_vid(tt_common_entry->vid),
1115 ((tt_common_entry->flags &
1116 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1117 no_purge ? 'P' : '.',
1118 ((tt_common_entry->flags &
1119 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1120 ((tt_common_entry->flags &
1121 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1122 ((tt_common_entry->flags &
1123 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1124 ((tt_common_entry->flags &
1125 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1126 no_purge ? 0 : last_seen_secs,
1127 no_purge ? 0 : last_seen_msecs,
1128 tt_local->vlan->tt.crc);
1134 batadv_hardif_put(primary_if);
1140 * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1141 * @msg :Netlink message to dump into
1142 * @portid: Port making netlink request
1143 * @cb: Control block containing additional options
1144 * @bat_priv: The bat priv with all the soft interface information
1145 * @common: tt local & tt global common data
1147 * Return: Error code, or 0 on success
1150 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1151 struct netlink_callback *cb,
1152 struct batadv_priv *bat_priv,
1153 struct batadv_tt_common_entry *common)
1156 struct batadv_softif_vlan *vlan;
1157 struct batadv_tt_local_entry *local;
1158 unsigned int last_seen_msecs;
1161 local = container_of(common, struct batadv_tt_local_entry, common);
1162 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1164 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1170 batadv_softif_vlan_put(vlan);
1172 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1173 &batadv_netlink_family, NLM_F_MULTI,
1174 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1178 genl_dump_check_consistent(cb, hdr);
1180 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1181 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1182 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1183 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1184 goto nla_put_failure;
1186 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1187 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1188 goto nla_put_failure;
1190 genlmsg_end(msg, hdr);
1194 genlmsg_cancel(msg, hdr);
1199 * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1200 * @msg: Netlink message to dump into
1201 * @portid: Port making netlink request
1202 * @cb: Control block containing additional options
1203 * @bat_priv: The bat priv with all the soft interface information
1204 * @hash: hash to dump
1205 * @bucket: bucket index to dump
1206 * @idx_s: Number of entries to skip
1208 * Return: Error code, or 0 on success
1211 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1212 struct netlink_callback *cb,
1213 struct batadv_priv *bat_priv,
1214 struct batadv_hashtable *hash, unsigned int bucket,
1217 struct batadv_tt_common_entry *common;
1220 spin_lock_bh(&hash->list_locks[bucket]);
1221 cb->seq = atomic_read(&hash->generation) << 1 | 1;
1223 hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1227 if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1229 spin_unlock_bh(&hash->list_locks[bucket]);
1234 spin_unlock_bh(&hash->list_locks[bucket]);
1241 * batadv_tt_local_dump() - Dump TT local entries into a message
1242 * @msg: Netlink message to dump into
1243 * @cb: Parameters from query
1245 * Return: Error code, or 0 on success
1247 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1249 struct net *net = sock_net(cb->skb->sk);
1250 struct net_device *soft_iface;
1251 struct batadv_priv *bat_priv;
1252 struct batadv_hard_iface *primary_if = NULL;
1253 struct batadv_hashtable *hash;
1256 int bucket = cb->args[0];
1257 int idx = cb->args[1];
1258 int portid = NETLINK_CB(cb->skb).portid;
1260 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1264 soft_iface = dev_get_by_index(net, ifindex);
1265 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1270 bat_priv = netdev_priv(soft_iface);
1272 primary_if = batadv_primary_if_get_selected(bat_priv);
1273 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1278 hash = bat_priv->tt.local_hash;
1280 while (bucket < hash->size) {
1281 if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1282 hash, bucket, &idx))
1292 batadv_hardif_put(primary_if);
1294 dev_put(soft_iface);
1296 cb->args[0] = bucket;
1303 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1304 struct batadv_tt_local_entry *tt_local_entry,
1305 u16 flags, const char *message)
1307 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1309 /* The local client has to be marked as "pending to be removed" but has
1310 * to be kept in the table in order to send it in a full table
1311 * response issued before the net ttvn increment (consistency check)
1313 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1315 batadv_dbg(BATADV_DBG_TT, bat_priv,
1316 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1317 tt_local_entry->common.addr,
1318 batadv_print_vid(tt_local_entry->common.vid), message);
1322 * batadv_tt_local_remove() - logically remove an entry from the local table
1323 * @bat_priv: the bat priv with all the soft interface information
1324 * @addr: the MAC address of the client to remove
1325 * @vid: VLAN identifier
1326 * @message: message to append to the log on deletion
1327 * @roaming: true if the deletion is due to a roaming event
1329 * Return: the flags assigned to the local entry before being deleted
1331 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1332 unsigned short vid, const char *message,
1335 struct batadv_tt_local_entry *tt_removed_entry;
1336 struct batadv_tt_local_entry *tt_local_entry;
1337 u16 flags, curr_flags = BATADV_NO_FLAGS;
1338 struct hlist_node *tt_removed_node;
1340 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1341 if (!tt_local_entry)
1344 curr_flags = tt_local_entry->common.flags;
1346 flags = BATADV_TT_CLIENT_DEL;
1347 /* if this global entry addition is due to a roaming, the node has to
1348 * mark the local entry as "roamed" in order to correctly reroute
1352 flags |= BATADV_TT_CLIENT_ROAM;
1353 /* mark the local client as ROAMed */
1354 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1357 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1358 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1362 /* if this client has been added right now, it is possible to
1363 * immediately purge it
1365 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1367 tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1370 &tt_local_entry->common);
1371 if (!tt_removed_node)
1374 /* drop reference of remove hash entry */
1375 tt_removed_entry = hlist_entry(tt_removed_node,
1376 struct batadv_tt_local_entry,
1378 batadv_tt_local_entry_put(tt_removed_entry);
1382 batadv_tt_local_entry_put(tt_local_entry);
1388 * batadv_tt_local_purge_list() - purge inactive tt local entries
1389 * @bat_priv: the bat priv with all the soft interface information
1390 * @head: pointer to the list containing the local tt entries
1391 * @timeout: parameter deciding whether a given tt local entry is considered
1394 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1395 struct hlist_head *head,
1398 struct batadv_tt_local_entry *tt_local_entry;
1399 struct batadv_tt_common_entry *tt_common_entry;
1400 struct hlist_node *node_tmp;
1402 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1404 tt_local_entry = container_of(tt_common_entry,
1405 struct batadv_tt_local_entry,
1407 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1410 /* entry already marked for deletion */
1411 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1414 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1417 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1418 BATADV_TT_CLIENT_DEL, "timed out");
1423 * batadv_tt_local_purge() - purge inactive tt local entries
1424 * @bat_priv: the bat priv with all the soft interface information
1425 * @timeout: parameter deciding whether a given tt local entry is considered
1428 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1431 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1432 struct hlist_head *head;
1433 spinlock_t *list_lock; /* protects write access to the hash lists */
1436 for (i = 0; i < hash->size; i++) {
1437 head = &hash->table[i];
1438 list_lock = &hash->list_locks[i];
1440 spin_lock_bh(list_lock);
1441 batadv_tt_local_purge_list(bat_priv, head, timeout);
1442 spin_unlock_bh(list_lock);
1446 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1448 struct batadv_hashtable *hash;
1449 spinlock_t *list_lock; /* protects write access to the hash lists */
1450 struct batadv_tt_common_entry *tt_common_entry;
1451 struct batadv_tt_local_entry *tt_local;
1452 struct hlist_node *node_tmp;
1453 struct hlist_head *head;
1456 if (!bat_priv->tt.local_hash)
1459 hash = bat_priv->tt.local_hash;
1461 for (i = 0; i < hash->size; i++) {
1462 head = &hash->table[i];
1463 list_lock = &hash->list_locks[i];
1465 spin_lock_bh(list_lock);
1466 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1468 hlist_del_rcu(&tt_common_entry->hash_entry);
1469 tt_local = container_of(tt_common_entry,
1470 struct batadv_tt_local_entry,
1473 batadv_tt_local_entry_put(tt_local);
1475 spin_unlock_bh(list_lock);
1478 batadv_hash_destroy(hash);
1480 bat_priv->tt.local_hash = NULL;
1483 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1485 if (bat_priv->tt.global_hash)
1488 bat_priv->tt.global_hash = batadv_hash_new(1024);
1490 if (!bat_priv->tt.global_hash)
1493 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1494 &batadv_tt_global_hash_lock_class_key);
1499 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1501 struct batadv_tt_change_node *entry, *safe;
1503 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1505 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1507 list_del(&entry->list);
1508 kmem_cache_free(batadv_tt_change_cache, entry);
1511 atomic_set(&bat_priv->tt.local_changes, 0);
1512 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1516 * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1517 * @entry: the TT global entry where the orig_list_entry has to be
1519 * @orig_node: the originator for which the orig_list_entry has to be found
1521 * retrieve the orig_tt_list_entry belonging to orig_node from the
1522 * batadv_tt_global_entry list
1524 * Return: it with an increased refcounter, NULL if not found
1526 static struct batadv_tt_orig_list_entry *
1527 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1528 const struct batadv_orig_node *orig_node)
1530 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1531 const struct hlist_head *head;
1534 head = &entry->orig_list;
1535 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1536 if (tmp_orig_entry->orig_node != orig_node)
1538 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1541 orig_entry = tmp_orig_entry;
1550 * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1551 * handled by a given originator
1552 * @entry: the TT global entry to check
1553 * @orig_node: the originator to search in the list
1554 * @flags: a pointer to store TT flags for the given @entry received
1557 * find out if an orig_node is already in the list of a tt_global_entry.
1559 * Return: true if found, false otherwise
1562 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1563 const struct batadv_orig_node *orig_node,
1566 struct batadv_tt_orig_list_entry *orig_entry;
1569 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1574 *flags = orig_entry->flags;
1576 batadv_tt_orig_list_entry_put(orig_entry);
1583 * batadv_tt_global_sync_flags() - update TT sync flags
1584 * @tt_global: the TT global entry to update sync flags in
1586 * Updates the sync flag bits in the tt_global flag attribute with a logical
1587 * OR of all sync flags from any of its TT orig entries.
1590 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1592 struct batadv_tt_orig_list_entry *orig_entry;
1593 const struct hlist_head *head;
1594 u16 flags = BATADV_NO_FLAGS;
1597 head = &tt_global->orig_list;
1598 hlist_for_each_entry_rcu(orig_entry, head, list)
1599 flags |= orig_entry->flags;
1602 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1603 tt_global->common.flags = flags;
1607 * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1608 * @tt_global: the TT global entry to add an orig entry in
1609 * @orig_node: the originator to add an orig entry for
1610 * @ttvn: translation table version number of this changeset
1611 * @flags: TT sync flags
1614 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1615 struct batadv_orig_node *orig_node, int ttvn,
1618 struct batadv_tt_orig_list_entry *orig_entry;
1620 spin_lock_bh(&tt_global->list_lock);
1622 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1624 /* refresh the ttvn: the current value could be a bogus one that
1625 * was added during a "temporary client detection"
1627 orig_entry->ttvn = ttvn;
1628 orig_entry->flags = flags;
1632 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1636 INIT_HLIST_NODE(&orig_entry->list);
1637 kref_get(&orig_node->refcount);
1638 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1639 orig_entry->orig_node = orig_node;
1640 orig_entry->ttvn = ttvn;
1641 orig_entry->flags = flags;
1642 kref_init(&orig_entry->refcount);
1644 kref_get(&orig_entry->refcount);
1645 hlist_add_head_rcu(&orig_entry->list,
1646 &tt_global->orig_list);
1647 atomic_inc(&tt_global->orig_list_count);
1650 batadv_tt_global_sync_flags(tt_global);
1653 batadv_tt_orig_list_entry_put(orig_entry);
1655 spin_unlock_bh(&tt_global->list_lock);
1659 * batadv_tt_global_add() - add a new TT global entry or update an existing one
1660 * @bat_priv: the bat priv with all the soft interface information
1661 * @orig_node: the originator announcing the client
1662 * @tt_addr: the mac address of the non-mesh client
1663 * @vid: VLAN identifier
1664 * @flags: TT flags that have to be set for this non-mesh client
1665 * @ttvn: the tt version number ever announcing this non-mesh client
1667 * Add a new TT global entry for the given originator. If the entry already
1668 * exists add a new reference to the given originator (a global entry can have
1669 * references to multiple originators) and adjust the flags attribute to reflect
1670 * the function argument.
1671 * If a TT local entry exists for this non-mesh client remove it.
1673 * The caller must hold the orig_node refcount.
1675 * Return: true if the new entry has been added, false otherwise
1677 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1678 struct batadv_orig_node *orig_node,
1679 const unsigned char *tt_addr,
1680 unsigned short vid, u16 flags, u8 ttvn)
1682 struct batadv_tt_global_entry *tt_global_entry;
1683 struct batadv_tt_local_entry *tt_local_entry;
1686 struct batadv_tt_common_entry *common;
1689 /* ignore global entries from backbone nodes */
1690 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1693 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1694 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1696 /* if the node already has a local client for this entry, it has to wait
1697 * for a roaming advertisement instead of manually messing up the global
1700 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1701 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1704 if (!tt_global_entry) {
1705 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1707 if (!tt_global_entry)
1710 common = &tt_global_entry->common;
1711 ether_addr_copy(common->addr, tt_addr);
1714 if (!is_multicast_ether_addr(common->addr))
1715 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1717 tt_global_entry->roam_at = 0;
1718 /* node must store current time in case of roaming. This is
1719 * needed to purge this entry out on timeout (if nobody claims
1722 if (flags & BATADV_TT_CLIENT_ROAM)
1723 tt_global_entry->roam_at = jiffies;
1724 kref_init(&common->refcount);
1725 common->added_at = jiffies;
1727 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1728 atomic_set(&tt_global_entry->orig_list_count, 0);
1729 spin_lock_init(&tt_global_entry->list_lock);
1731 kref_get(&common->refcount);
1732 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1734 batadv_choose_tt, common,
1735 &common->hash_entry);
1737 if (unlikely(hash_added != 0)) {
1738 /* remove the reference for the hash */
1739 batadv_tt_global_entry_put(tt_global_entry);
1743 common = &tt_global_entry->common;
1744 /* If there is already a global entry, we can use this one for
1746 * But if we are trying to add a temporary client then here are
1747 * two options at this point:
1748 * 1) the global client is not a temporary client: the global
1749 * client has to be left as it is, temporary information
1750 * should never override any already known client state
1751 * 2) the global client is a temporary client: purge the
1752 * originator list and add the new one orig_entry
1754 if (flags & BATADV_TT_CLIENT_TEMP) {
1755 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1757 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1760 batadv_tt_global_del_orig_list(tt_global_entry);
1761 goto add_orig_entry;
1764 /* if the client was temporary added before receiving the first
1765 * OGM announcing it, we have to clear the TEMP flag. Also,
1766 * remove the previous temporary orig node and re-add it
1767 * if required. If the orig entry changed, the new one which
1768 * is a non-temporary entry is preferred.
1770 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1771 batadv_tt_global_del_orig_list(tt_global_entry);
1772 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1775 /* the change can carry possible "attribute" flags like the
1776 * TT_CLIENT_TEMP, therefore they have to be copied in the
1779 if (!is_multicast_ether_addr(common->addr))
1780 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1782 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1783 * one originator left in the list and we previously received a
1784 * delete + roaming change for this originator.
1786 * We should first delete the old originator before adding the
1789 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1790 batadv_tt_global_del_orig_list(tt_global_entry);
1791 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1792 tt_global_entry->roam_at = 0;
1796 /* add the new orig_entry (if needed) or update it */
1797 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1798 flags & BATADV_TT_SYNC_MASK);
1800 batadv_dbg(BATADV_DBG_TT, bat_priv,
1801 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1802 common->addr, batadv_print_vid(common->vid),
1807 /* Do not remove multicast addresses from the local hash on
1810 if (is_multicast_ether_addr(tt_addr))
1813 /* remove address from local hash if present */
1814 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1815 "global tt received",
1816 flags & BATADV_TT_CLIENT_ROAM);
1817 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1819 if (!(flags & BATADV_TT_CLIENT_ROAM))
1820 /* this is a normal global add. Therefore the client is not in a
1821 * roaming state anymore.
1823 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1826 if (tt_global_entry)
1827 batadv_tt_global_entry_put(tt_global_entry);
1829 batadv_tt_local_entry_put(tt_local_entry);
1834 * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1835 * @bat_priv: the bat priv with all the soft interface information
1836 * @tt_global_entry: global translation table entry to be analyzed
1838 * This function assumes the caller holds rcu_read_lock().
1839 * Return: best originator list entry or NULL on errors.
1841 static struct batadv_tt_orig_list_entry *
1842 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1843 struct batadv_tt_global_entry *tt_global_entry)
1845 struct batadv_neigh_node *router, *best_router = NULL;
1846 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1847 struct hlist_head *head;
1848 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1850 head = &tt_global_entry->orig_list;
1851 hlist_for_each_entry_rcu(orig_entry, head, list) {
1852 router = batadv_orig_router_get(orig_entry->orig_node,
1858 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1859 BATADV_IF_DEFAULT) <= 0) {
1860 batadv_neigh_node_put(router);
1864 /* release the refcount for the "old" best */
1866 batadv_neigh_node_put(best_router);
1868 best_entry = orig_entry;
1869 best_router = router;
1873 batadv_neigh_node_put(best_router);
1878 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1880 * batadv_tt_global_print_entry() - print all orig nodes who announce the
1881 * address for this global entry
1882 * @bat_priv: the bat priv with all the soft interface information
1883 * @tt_global_entry: global translation table entry to be printed
1884 * @seq: debugfs table seq_file struct
1886 * This function assumes the caller holds rcu_read_lock().
1889 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1890 struct batadv_tt_global_entry *tt_global_entry,
1891 struct seq_file *seq)
1893 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1894 struct batadv_tt_common_entry *tt_common_entry;
1895 struct batadv_orig_node_vlan *vlan;
1896 struct hlist_head *head;
1900 tt_common_entry = &tt_global_entry->common;
1901 flags = tt_common_entry->flags;
1903 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1905 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1906 tt_common_entry->vid);
1909 " * Cannot retrieve VLAN %d for originator %pM\n",
1910 batadv_print_vid(tt_common_entry->vid),
1911 best_entry->orig_node->orig);
1915 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1917 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1918 '*', tt_global_entry->common.addr,
1919 batadv_print_vid(tt_global_entry->common.vid),
1920 best_entry->ttvn, best_entry->orig_node->orig,
1921 last_ttvn, vlan->tt.crc,
1922 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1923 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1924 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1925 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1927 batadv_orig_node_vlan_put(vlan);
1931 head = &tt_global_entry->orig_list;
1933 hlist_for_each_entry_rcu(orig_entry, head, list) {
1934 if (best_entry == orig_entry)
1937 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1938 tt_common_entry->vid);
1941 " + Cannot retrieve VLAN %d for originator %pM\n",
1942 batadv_print_vid(tt_common_entry->vid),
1943 orig_entry->orig_node->orig);
1947 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1949 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1950 '+', tt_global_entry->common.addr,
1951 batadv_print_vid(tt_global_entry->common.vid),
1952 orig_entry->ttvn, orig_entry->orig_node->orig,
1953 last_ttvn, vlan->tt.crc,
1954 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1955 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1956 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1957 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1959 batadv_orig_node_vlan_put(vlan);
1964 * batadv_tt_global_seq_print_text() - Print the global tt table in a seq file
1965 * @seq: seq file to print on
1970 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1972 struct net_device *net_dev = (struct net_device *)seq->private;
1973 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1974 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1975 struct batadv_tt_common_entry *tt_common_entry;
1976 struct batadv_tt_global_entry *tt_global;
1977 struct batadv_hard_iface *primary_if;
1978 struct hlist_head *head;
1981 primary_if = batadv_seq_print_text_primary_if_get(seq);
1986 "Globally announced TT entries received via the mesh %s\n",
1989 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1991 for (i = 0; i < hash->size; i++) {
1992 head = &hash->table[i];
1995 hlist_for_each_entry_rcu(tt_common_entry,
1997 tt_global = container_of(tt_common_entry,
1998 struct batadv_tt_global_entry,
2000 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
2006 batadv_hardif_put(primary_if);
2012 * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
2013 * @msg: Netlink message to dump into
2014 * @portid: Port making netlink request
2015 * @seq: Sequence number of netlink message
2016 * @common: tt local & tt global common data
2017 * @orig: Originator node announcing a non-mesh client
2018 * @best: Is the best originator for the TT entry
2020 * Return: Error code, or 0 on success
2023 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2024 struct batadv_tt_common_entry *common,
2025 struct batadv_tt_orig_list_entry *orig,
2028 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
2030 struct batadv_orig_node_vlan *vlan;
2034 vlan = batadv_orig_node_vlan_get(orig->orig_node,
2041 batadv_orig_node_vlan_put(vlan);
2043 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2045 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2049 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2051 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2052 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2053 orig->orig_node->orig) ||
2054 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2055 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2056 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2057 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
2058 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
2059 goto nla_put_failure;
2061 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2062 goto nla_put_failure;
2064 genlmsg_end(msg, hdr);
2068 genlmsg_cancel(msg, hdr);
2073 * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
2074 * @msg: Netlink message to dump into
2075 * @portid: Port making netlink request
2076 * @seq: Sequence number of netlink message
2077 * @bat_priv: The bat priv with all the soft interface information
2078 * @common: tt local & tt global common data
2079 * @sub_s: Number of entries to skip
2081 * This function assumes the caller holds rcu_read_lock().
2083 * Return: Error code, or 0 on success
2086 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2087 struct batadv_priv *bat_priv,
2088 struct batadv_tt_common_entry *common, int *sub_s)
2090 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2091 struct batadv_tt_global_entry *global;
2092 struct hlist_head *head;
2096 global = container_of(common, struct batadv_tt_global_entry, common);
2097 best_entry = batadv_transtable_best_orig(bat_priv, global);
2098 head = &global->orig_list;
2100 hlist_for_each_entry_rcu(orig_entry, head, list) {
2104 best = (orig_entry == best_entry);
2106 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2107 orig_entry, best)) {
2118 * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
2119 * @msg: Netlink message to dump into
2120 * @portid: Port making netlink request
2121 * @seq: Sequence number of netlink message
2122 * @bat_priv: The bat priv with all the soft interface information
2123 * @head: Pointer to the list containing the global tt entries
2124 * @idx_s: Number of entries to skip
2125 * @sub: Number of entries to skip
2127 * Return: Error code, or 0 on success
2130 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2131 struct batadv_priv *bat_priv,
2132 struct hlist_head *head, int *idx_s, int *sub)
2134 struct batadv_tt_common_entry *common;
2138 hlist_for_each_entry_rcu(common, head, hash_entry) {
2142 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2157 * batadv_tt_global_dump() - Dump TT global entries into a message
2158 * @msg: Netlink message to dump into
2159 * @cb: Parameters from query
2161 * Return: Error code, or length of message on success
2163 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2165 struct net *net = sock_net(cb->skb->sk);
2166 struct net_device *soft_iface;
2167 struct batadv_priv *bat_priv;
2168 struct batadv_hard_iface *primary_if = NULL;
2169 struct batadv_hashtable *hash;
2170 struct hlist_head *head;
2173 int bucket = cb->args[0];
2174 int idx = cb->args[1];
2175 int sub = cb->args[2];
2176 int portid = NETLINK_CB(cb->skb).portid;
2178 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2182 soft_iface = dev_get_by_index(net, ifindex);
2183 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2188 bat_priv = netdev_priv(soft_iface);
2190 primary_if = batadv_primary_if_get_selected(bat_priv);
2191 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2196 hash = bat_priv->tt.global_hash;
2198 while (bucket < hash->size) {
2199 head = &hash->table[bucket];
2201 if (batadv_tt_global_dump_bucket(msg, portid,
2202 cb->nlh->nlmsg_seq, bat_priv,
2213 batadv_hardif_put(primary_if);
2215 dev_put(soft_iface);
2217 cb->args[0] = bucket;
2225 * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
2226 * @tt_global_entry: the global entry to remove the orig_entry from
2227 * @orig_entry: the orig entry to remove and free
2229 * Remove an orig_entry from its list in the given tt_global_entry and
2230 * free this orig_entry afterwards.
2232 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2236 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2237 struct batadv_tt_orig_list_entry *orig_entry)
2239 lockdep_assert_held(&tt_global_entry->list_lock);
2241 batadv_tt_global_size_dec(orig_entry->orig_node,
2242 tt_global_entry->common.vid);
2243 atomic_dec(&tt_global_entry->orig_list_count);
2244 /* requires holding tt_global_entry->list_lock and orig_entry->list
2245 * being part of a list
2247 hlist_del_rcu(&orig_entry->list);
2248 batadv_tt_orig_list_entry_put(orig_entry);
2251 /* deletes the orig list of a tt_global_entry */
2253 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2255 struct hlist_head *head;
2256 struct hlist_node *safe;
2257 struct batadv_tt_orig_list_entry *orig_entry;
2259 spin_lock_bh(&tt_global_entry->list_lock);
2260 head = &tt_global_entry->orig_list;
2261 hlist_for_each_entry_safe(orig_entry, safe, head, list)
2262 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2263 spin_unlock_bh(&tt_global_entry->list_lock);
2267 * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2268 * @bat_priv: the bat priv with all the soft interface information
2269 * @tt_global_entry: the global entry to remove the orig_node from
2270 * @orig_node: the originator announcing the client
2271 * @message: message to append to the log on deletion
2273 * Remove the given orig_node and its according orig_entry from the given
2277 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2278 struct batadv_tt_global_entry *tt_global_entry,
2279 struct batadv_orig_node *orig_node,
2280 const char *message)
2282 struct hlist_head *head;
2283 struct hlist_node *safe;
2284 struct batadv_tt_orig_list_entry *orig_entry;
2287 spin_lock_bh(&tt_global_entry->list_lock);
2288 head = &tt_global_entry->orig_list;
2289 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2290 if (orig_entry->orig_node == orig_node) {
2291 vid = tt_global_entry->common.vid;
2292 batadv_dbg(BATADV_DBG_TT, bat_priv,
2293 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2295 tt_global_entry->common.addr,
2296 batadv_print_vid(vid), message);
2297 _batadv_tt_global_del_orig_entry(tt_global_entry,
2301 spin_unlock_bh(&tt_global_entry->list_lock);
2304 /* If the client is to be deleted, we check if it is the last origantor entry
2305 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2306 * timer, otherwise we simply remove the originator scheduled for deletion.
2309 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2310 struct batadv_tt_global_entry *tt_global_entry,
2311 struct batadv_orig_node *orig_node,
2312 const char *message)
2314 bool last_entry = true;
2315 struct hlist_head *head;
2316 struct batadv_tt_orig_list_entry *orig_entry;
2318 /* no local entry exists, case 1:
2319 * Check if this is the last one or if other entries exist.
2323 head = &tt_global_entry->orig_list;
2324 hlist_for_each_entry_rcu(orig_entry, head, list) {
2325 if (orig_entry->orig_node != orig_node) {
2333 /* its the last one, mark for roaming. */
2334 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2335 tt_global_entry->roam_at = jiffies;
2337 /* there is another entry, we can simply delete this
2338 * one and can still use the other one.
2340 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2341 orig_node, message);
2346 * batadv_tt_global_del() - remove a client from the global table
2347 * @bat_priv: the bat priv with all the soft interface information
2348 * @orig_node: an originator serving this client
2349 * @addr: the mac address of the client
2350 * @vid: VLAN identifier
2351 * @message: a message explaining the reason for deleting the client to print
2352 * for debugging purpose
2353 * @roaming: true if the deletion has been triggered by a roaming event
2355 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2356 struct batadv_orig_node *orig_node,
2357 const unsigned char *addr, unsigned short vid,
2358 const char *message, bool roaming)
2360 struct batadv_tt_global_entry *tt_global_entry;
2361 struct batadv_tt_local_entry *local_entry = NULL;
2363 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2364 if (!tt_global_entry)
2368 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2369 orig_node, message);
2371 if (hlist_empty(&tt_global_entry->orig_list))
2372 batadv_tt_global_free(bat_priv, tt_global_entry,
2378 /* if we are deleting a global entry due to a roam
2379 * event, there are two possibilities:
2380 * 1) the client roamed from node A to node B => if there
2381 * is only one originator left for this client, we mark
2382 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2383 * wait for node B to claim it. In case of timeout
2384 * the entry is purged.
2386 * If there are other originators left, we directly delete
2388 * 2) the client roamed to us => we can directly delete
2389 * the global entry, since it is useless now.
2391 local_entry = batadv_tt_local_hash_find(bat_priv,
2392 tt_global_entry->common.addr,
2395 /* local entry exists, case 2: client roamed to us. */
2396 batadv_tt_global_del_orig_list(tt_global_entry);
2397 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2399 /* no local entry exists, case 1: check for roaming */
2400 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2401 orig_node, message);
2405 if (tt_global_entry)
2406 batadv_tt_global_entry_put(tt_global_entry);
2408 batadv_tt_local_entry_put(local_entry);
2412 * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2413 * the given originator matching the provided vid
2414 * @bat_priv: the bat priv with all the soft interface information
2415 * @orig_node: the originator owning the entries to remove
2416 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2418 * @message: debug message to print as "reason"
2420 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2421 struct batadv_orig_node *orig_node,
2423 const char *message)
2425 struct batadv_tt_global_entry *tt_global;
2426 struct batadv_tt_common_entry *tt_common_entry;
2428 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2429 struct hlist_node *safe;
2430 struct hlist_head *head;
2431 spinlock_t *list_lock; /* protects write access to the hash lists */
2437 for (i = 0; i < hash->size; i++) {
2438 head = &hash->table[i];
2439 list_lock = &hash->list_locks[i];
2441 spin_lock_bh(list_lock);
2442 hlist_for_each_entry_safe(tt_common_entry, safe,
2444 /* remove only matching entries */
2445 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2448 tt_global = container_of(tt_common_entry,
2449 struct batadv_tt_global_entry,
2452 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2453 orig_node, message);
2455 if (hlist_empty(&tt_global->orig_list)) {
2456 vid = tt_global->common.vid;
2457 batadv_dbg(BATADV_DBG_TT, bat_priv,
2458 "Deleting global tt entry %pM (vid: %d): %s\n",
2459 tt_global->common.addr,
2460 batadv_print_vid(vid), message);
2461 hlist_del_rcu(&tt_common_entry->hash_entry);
2462 batadv_tt_global_entry_put(tt_global);
2465 spin_unlock_bh(list_lock);
2467 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2470 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2474 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2475 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2477 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2478 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2480 *msg = "Roaming timeout\n";
2483 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2484 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2486 *msg = "Temporary client timeout\n";
2492 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2494 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2495 struct hlist_head *head;
2496 struct hlist_node *node_tmp;
2497 spinlock_t *list_lock; /* protects write access to the hash lists */
2500 struct batadv_tt_common_entry *tt_common;
2501 struct batadv_tt_global_entry *tt_global;
2503 for (i = 0; i < hash->size; i++) {
2504 head = &hash->table[i];
2505 list_lock = &hash->list_locks[i];
2507 spin_lock_bh(list_lock);
2508 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2510 tt_global = container_of(tt_common,
2511 struct batadv_tt_global_entry,
2514 if (!batadv_tt_global_to_purge(tt_global, &msg))
2517 batadv_dbg(BATADV_DBG_TT, bat_priv,
2518 "Deleting global tt entry %pM (vid: %d): %s\n",
2519 tt_global->common.addr,
2520 batadv_print_vid(tt_global->common.vid),
2523 hlist_del_rcu(&tt_common->hash_entry);
2525 batadv_tt_global_entry_put(tt_global);
2527 spin_unlock_bh(list_lock);
2531 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2533 struct batadv_hashtable *hash;
2534 spinlock_t *list_lock; /* protects write access to the hash lists */
2535 struct batadv_tt_common_entry *tt_common_entry;
2536 struct batadv_tt_global_entry *tt_global;
2537 struct hlist_node *node_tmp;
2538 struct hlist_head *head;
2541 if (!bat_priv->tt.global_hash)
2544 hash = bat_priv->tt.global_hash;
2546 for (i = 0; i < hash->size; i++) {
2547 head = &hash->table[i];
2548 list_lock = &hash->list_locks[i];
2550 spin_lock_bh(list_lock);
2551 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2553 hlist_del_rcu(&tt_common_entry->hash_entry);
2554 tt_global = container_of(tt_common_entry,
2555 struct batadv_tt_global_entry,
2557 batadv_tt_global_entry_put(tt_global);
2559 spin_unlock_bh(list_lock);
2562 batadv_hash_destroy(hash);
2564 bat_priv->tt.global_hash = NULL;
2568 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2569 struct batadv_tt_global_entry *tt_global_entry)
2571 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2572 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2575 /* check if the two clients are marked as isolated */
2576 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2577 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2584 * batadv_transtable_search() - get the mesh destination for a given client
2585 * @bat_priv: the bat priv with all the soft interface information
2586 * @src: mac address of the source client
2587 * @addr: mac address of the destination client
2588 * @vid: VLAN identifier
2590 * Return: a pointer to the originator that was selected as destination in the
2591 * mesh for contacting the client 'addr', NULL otherwise.
2592 * In case of multiple originators serving the same client, the function returns
2593 * the best one (best in terms of metric towards the destination node).
2595 * If the two clients are AP isolated the function returns NULL.
2597 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2602 struct batadv_tt_local_entry *tt_local_entry = NULL;
2603 struct batadv_tt_global_entry *tt_global_entry = NULL;
2604 struct batadv_orig_node *orig_node = NULL;
2605 struct batadv_tt_orig_list_entry *best_entry;
2607 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2608 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2609 if (!tt_local_entry ||
2610 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2614 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2615 if (!tt_global_entry)
2618 /* check whether the clients should not communicate due to AP
2621 if (tt_local_entry &&
2622 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2626 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2627 /* found anything? */
2629 orig_node = best_entry->orig_node;
2630 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2635 if (tt_global_entry)
2636 batadv_tt_global_entry_put(tt_global_entry);
2638 batadv_tt_local_entry_put(tt_local_entry);
2644 * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2645 * to the given orig_node
2646 * @bat_priv: the bat priv with all the soft interface information
2647 * @orig_node: originator for which the CRC should be computed
2648 * @vid: VLAN identifier for which the CRC32 has to be computed
2650 * This function computes the checksum for the global table corresponding to a
2651 * specific originator. In particular, the checksum is computed as follows: For
2652 * each client connected to the originator the CRC32C of the MAC address and the
2653 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2656 * The idea behind is that CRC32C should be used as much as possible in order to
2657 * produce a unique hash of the table, but since the order which is used to feed
2658 * the CRC32C function affects the result and since every node in the network
2659 * probably sorts the clients differently, the hash function cannot be directly
2660 * computed over the entire table. Hence the CRC32C is used only on
2661 * the single client entry, while all the results are then xor'ed together
2662 * because the XOR operation can combine them all while trying to reduce the
2663 * noise as much as possible.
2665 * Return: the checksum of the global table of a given originator.
2667 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2668 struct batadv_orig_node *orig_node,
2671 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2672 struct batadv_tt_orig_list_entry *tt_orig;
2673 struct batadv_tt_common_entry *tt_common;
2674 struct batadv_tt_global_entry *tt_global;
2675 struct hlist_head *head;
2676 u32 i, crc_tmp, crc = 0;
2680 for (i = 0; i < hash->size; i++) {
2681 head = &hash->table[i];
2684 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2685 tt_global = container_of(tt_common,
2686 struct batadv_tt_global_entry,
2688 /* compute the CRC only for entries belonging to the
2689 * VLAN identified by the vid passed as parameter
2691 if (tt_common->vid != vid)
2694 /* Roaming clients are in the global table for
2695 * consistency only. They don't have to be
2696 * taken into account while computing the
2699 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2701 /* Temporary clients have not been announced yet, so
2702 * they have to be skipped while computing the global
2705 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2708 /* find out if this global entry is announced by this
2711 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2716 /* use network order to read the VID: this ensures that
2717 * every node reads the bytes in the same order.
2719 tmp_vid = htons(tt_common->vid);
2720 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2722 /* compute the CRC on flags that have to be kept in sync
2725 flags = tt_orig->flags;
2726 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2728 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2730 batadv_tt_orig_list_entry_put(tt_orig);
2739 * batadv_tt_local_crc() - calculates the checksum of the local table
2740 * @bat_priv: the bat priv with all the soft interface information
2741 * @vid: VLAN identifier for which the CRC32 has to be computed
2743 * For details about the computation, please refer to the documentation for
2744 * batadv_tt_global_crc().
2746 * Return: the checksum of the local table
2748 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2751 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2752 struct batadv_tt_common_entry *tt_common;
2753 struct hlist_head *head;
2754 u32 i, crc_tmp, crc = 0;
2758 for (i = 0; i < hash->size; i++) {
2759 head = &hash->table[i];
2762 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2763 /* compute the CRC only for entries belonging to the
2764 * VLAN identified by vid
2766 if (tt_common->vid != vid)
2769 /* not yet committed clients have not to be taken into
2770 * account while computing the CRC
2772 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2775 /* use network order to read the VID: this ensures that
2776 * every node reads the bytes in the same order.
2778 tmp_vid = htons(tt_common->vid);
2779 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2781 /* compute the CRC on flags that have to be kept in sync
2784 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2785 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2787 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2796 * batadv_tt_req_node_release() - free tt_req node entry
2797 * @ref: kref pointer of the tt req_node entry
2799 static void batadv_tt_req_node_release(struct kref *ref)
2801 struct batadv_tt_req_node *tt_req_node;
2803 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2805 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2809 * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2810 * possibly release it
2811 * @tt_req_node: tt_req_node to be free'd
2813 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2818 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2821 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2823 struct batadv_tt_req_node *node;
2824 struct hlist_node *safe;
2826 spin_lock_bh(&bat_priv->tt.req_list_lock);
2828 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2829 hlist_del_init(&node->list);
2830 batadv_tt_req_node_put(node);
2833 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2836 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2837 struct batadv_orig_node *orig_node,
2838 const void *tt_buff,
2841 /* Replace the old buffer only if I received something in the
2842 * last OGM (the OGM could carry no changes)
2844 spin_lock_bh(&orig_node->tt_buff_lock);
2845 if (tt_buff_len > 0) {
2846 kfree(orig_node->tt_buff);
2847 orig_node->tt_buff_len = 0;
2848 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2849 if (orig_node->tt_buff) {
2850 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2851 orig_node->tt_buff_len = tt_buff_len;
2854 spin_unlock_bh(&orig_node->tt_buff_lock);
2857 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2859 struct batadv_tt_req_node *node;
2860 struct hlist_node *safe;
2862 spin_lock_bh(&bat_priv->tt.req_list_lock);
2863 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2864 if (batadv_has_timed_out(node->issued_at,
2865 BATADV_TT_REQUEST_TIMEOUT)) {
2866 hlist_del_init(&node->list);
2867 batadv_tt_req_node_put(node);
2870 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2874 * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2875 * @bat_priv: the bat priv with all the soft interface information
2876 * @orig_node: orig node this request is being issued for
2878 * Return: the pointer to the new tt_req_node struct if no request
2879 * has already been issued for this orig_node, NULL otherwise.
2881 static struct batadv_tt_req_node *
2882 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2883 struct batadv_orig_node *orig_node)
2885 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2887 spin_lock_bh(&bat_priv->tt.req_list_lock);
2888 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2889 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2890 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2891 BATADV_TT_REQUEST_TIMEOUT))
2895 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2899 kref_init(&tt_req_node->refcount);
2900 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2901 tt_req_node->issued_at = jiffies;
2903 kref_get(&tt_req_node->refcount);
2904 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2906 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2911 * batadv_tt_local_valid() - verify local tt entry and get flags
2912 * @entry_ptr: to be checked local tt entry
2913 * @data_ptr: not used but definition required to satisfy the callback prototype
2914 * @flags: a pointer to store TT flags for this client to
2916 * Checks the validity of the given local TT entry. If it is, then the provided
2917 * flags pointer is updated.
2919 * Return: true if the entry is a valid, false otherwise.
2921 static bool batadv_tt_local_valid(const void *entry_ptr,
2922 const void *data_ptr,
2925 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2927 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2931 *flags = tt_common_entry->flags;
2937 * batadv_tt_global_valid() - verify global tt entry and get flags
2938 * @entry_ptr: to be checked global tt entry
2939 * @data_ptr: an orig_node object (may be NULL)
2940 * @flags: a pointer to store TT flags for this client to
2942 * Checks the validity of the given global TT entry. If it is, then the provided
2943 * flags pointer is updated either with the common (summed) TT flags if data_ptr
2944 * is NULL or the specific, per originator TT flags otherwise.
2946 * Return: true if the entry is a valid, false otherwise.
2948 static bool batadv_tt_global_valid(const void *entry_ptr,
2949 const void *data_ptr,
2952 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2953 const struct batadv_tt_global_entry *tt_global_entry;
2954 const struct batadv_orig_node *orig_node = data_ptr;
2956 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2957 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2960 tt_global_entry = container_of(tt_common_entry,
2961 struct batadv_tt_global_entry,
2964 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2969 * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2971 * @bat_priv: the bat priv with all the soft interface information
2972 * @hash: hash table containing the tt entries
2973 * @tt_len: expected tvlv tt data buffer length in number of bytes
2974 * @tvlv_buff: pointer to the buffer to fill with the TT data
2975 * @valid_cb: function to filter tt change entries and to return TT flags
2976 * @cb_data: data passed to the filter function as argument
2978 * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2979 * is not provided then this becomes a no-op.
2981 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2982 struct batadv_hashtable *hash,
2983 void *tvlv_buff, u16 tt_len,
2984 bool (*valid_cb)(const void *,
2989 struct batadv_tt_common_entry *tt_common_entry;
2990 struct batadv_tvlv_tt_change *tt_change;
2991 struct hlist_head *head;
2992 u16 tt_tot, tt_num_entries = 0;
2997 tt_tot = batadv_tt_entries(tt_len);
2998 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
3004 for (i = 0; i < hash->size; i++) {
3005 head = &hash->table[i];
3007 hlist_for_each_entry_rcu(tt_common_entry,
3009 if (tt_tot == tt_num_entries)
3012 ret = valid_cb(tt_common_entry, cb_data, &flags);
3016 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
3017 tt_change->flags = flags;
3018 tt_change->vid = htons(tt_common_entry->vid);
3019 memset(tt_change->reserved, 0,
3020 sizeof(tt_change->reserved));
3030 * batadv_tt_global_check_crc() - check if all the CRCs are correct
3031 * @orig_node: originator for which the CRCs have to be checked
3032 * @tt_vlan: pointer to the first tvlv VLAN entry
3033 * @num_vlan: number of tvlv VLAN entries
3035 * Return: true if all the received CRCs match the locally stored ones, false
3038 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
3039 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3042 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
3043 struct batadv_orig_node_vlan *vlan;
3044 int i, orig_num_vlan;
3047 /* check if each received CRC matches the locally stored one */
3048 for (i = 0; i < num_vlan; i++) {
3049 tt_vlan_tmp = tt_vlan + i;
3051 /* if orig_node is a backbone node for this VLAN, don't check
3052 * the CRC as we ignore all the global entries over it
3054 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
3056 ntohs(tt_vlan_tmp->vid)))
3059 vlan = batadv_orig_node_vlan_get(orig_node,
3060 ntohs(tt_vlan_tmp->vid));
3065 batadv_orig_node_vlan_put(vlan);
3067 if (crc != ntohl(tt_vlan_tmp->crc))
3071 /* check if any excess VLANs exist locally for the originator
3072 * which are not mentioned in the TVLV from the originator.
3076 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
3080 if (orig_num_vlan > num_vlan)
3087 * batadv_tt_local_update_crc() - update all the local CRCs
3088 * @bat_priv: the bat priv with all the soft interface information
3090 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3092 struct batadv_softif_vlan *vlan;
3094 /* recompute the global CRC for each VLAN */
3096 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3097 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3103 * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
3104 * @bat_priv: the bat priv with all the soft interface information
3105 * @orig_node: the orig_node for which the CRCs have to be updated
3107 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3108 struct batadv_orig_node *orig_node)
3110 struct batadv_orig_node_vlan *vlan;
3113 /* recompute the global CRC for each VLAN */
3115 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
3116 /* if orig_node is a backbone node for this VLAN, don't compute
3117 * the CRC as we ignore all the global entries over it
3119 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3123 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3130 * batadv_send_tt_request() - send a TT Request message to a given node
3131 * @bat_priv: the bat priv with all the soft interface information
3132 * @dst_orig_node: the destination of the message
3133 * @ttvn: the version number that the source of the message is looking for
3134 * @tt_vlan: pointer to the first tvlv VLAN object to request
3135 * @num_vlan: number of tvlv VLAN entries
3136 * @full_table: ask for the entire translation table if true, while only for the
3137 * last TT diff otherwise
3139 * Return: true if the TT Request was sent, false otherwise
3141 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3142 struct batadv_orig_node *dst_orig_node,
3144 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3145 u16 num_vlan, bool full_table)
3147 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3148 struct batadv_tt_req_node *tt_req_node = NULL;
3149 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3150 struct batadv_hard_iface *primary_if;
3154 primary_if = batadv_primary_if_get_selected(bat_priv);
3158 /* The new tt_req will be issued only if I'm not waiting for a
3159 * reply from the same orig_node yet
3161 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
3165 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3166 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
3170 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3171 tvlv_tt_data->ttvn = ttvn;
3172 tvlv_tt_data->num_vlan = htons(num_vlan);
3174 /* send all the CRCs within the request. This is needed by intermediate
3175 * nodes to ensure they have the correct table before replying
3177 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3178 for (i = 0; i < num_vlan; i++) {
3179 tt_vlan_req->vid = tt_vlan->vid;
3180 tt_vlan_req->crc = tt_vlan->crc;
3187 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3189 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
3190 dst_orig_node->orig, full_table ? 'F' : '.');
3192 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
3193 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3194 dst_orig_node->orig, BATADV_TVLV_TT, 1,
3195 tvlv_tt_data, size);
3200 batadv_hardif_put(primary_if);
3202 if (ret && tt_req_node) {
3203 spin_lock_bh(&bat_priv->tt.req_list_lock);
3204 if (!hlist_unhashed(&tt_req_node->list)) {
3205 hlist_del_init(&tt_req_node->list);
3206 batadv_tt_req_node_put(tt_req_node);
3208 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3212 batadv_tt_req_node_put(tt_req_node);
3214 kfree(tvlv_tt_data);
3219 * batadv_send_other_tt_response() - send reply to tt request concerning another
3220 * node's translation table
3221 * @bat_priv: the bat priv with all the soft interface information
3222 * @tt_data: tt data containing the tt request information
3223 * @req_src: mac address of tt request sender
3224 * @req_dst: mac address of tt request recipient
3226 * Return: true if tt request reply was sent, false otherwise.
3228 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3229 struct batadv_tvlv_tt_data *tt_data,
3230 u8 *req_src, u8 *req_dst)
3232 struct batadv_orig_node *req_dst_orig_node;
3233 struct batadv_orig_node *res_dst_orig_node = NULL;
3234 struct batadv_tvlv_tt_change *tt_change;
3235 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3236 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3237 bool ret = false, full_table;
3238 u8 orig_ttvn, req_ttvn;
3242 batadv_dbg(BATADV_DBG_TT, bat_priv,
3243 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3244 req_src, tt_data->ttvn, req_dst,
3245 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3247 /* Let's get the orig node of the REAL destination */
3248 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3249 if (!req_dst_orig_node)
3252 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3253 if (!res_dst_orig_node)
3256 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3257 req_ttvn = tt_data->ttvn;
3259 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3260 /* this node doesn't have the requested data */
3261 if (orig_ttvn != req_ttvn ||
3262 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3263 ntohs(tt_data->num_vlan)))
3266 /* If the full table has been explicitly requested */
3267 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3268 !req_dst_orig_node->tt_buff)
3273 /* TT fragmentation hasn't been implemented yet, so send as many
3274 * TT entries fit a single packet as possible only
3277 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3278 tt_len = req_dst_orig_node->tt_buff_len;
3280 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3287 /* Copy the last orig_node's OGM buffer */
3288 memcpy(tt_change, req_dst_orig_node->tt_buff,
3289 req_dst_orig_node->tt_buff_len);
3290 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3292 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3293 * in the initial part
3296 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3303 /* fill the rest of the tvlv with the real TT entries */
3304 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3306 batadv_tt_global_valid,
3310 /* Don't send the response, if larger than fragmented packet. */
3311 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3312 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3313 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3314 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3315 res_dst_orig_node->orig);
3319 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3320 tvlv_tt_data->ttvn = req_ttvn;
3323 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3325 batadv_dbg(BATADV_DBG_TT, bat_priv,
3326 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3327 res_dst_orig_node->orig, req_dst_orig_node->orig,
3328 full_table ? 'F' : '.', req_ttvn);
3330 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3332 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3333 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3340 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3343 if (res_dst_orig_node)
3344 batadv_orig_node_put(res_dst_orig_node);
3345 if (req_dst_orig_node)
3346 batadv_orig_node_put(req_dst_orig_node);
3347 kfree(tvlv_tt_data);
3352 * batadv_send_my_tt_response() - send reply to tt request concerning this
3353 * node's translation table
3354 * @bat_priv: the bat priv with all the soft interface information
3355 * @tt_data: tt data containing the tt request information
3356 * @req_src: mac address of tt request sender
3358 * Return: true if tt request reply was sent, false otherwise.
3360 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3361 struct batadv_tvlv_tt_data *tt_data,
3364 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3365 struct batadv_hard_iface *primary_if = NULL;
3366 struct batadv_tvlv_tt_change *tt_change;
3367 struct batadv_orig_node *orig_node;
3368 u8 my_ttvn, req_ttvn;
3373 batadv_dbg(BATADV_DBG_TT, bat_priv,
3374 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3375 req_src, tt_data->ttvn,
3376 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3378 spin_lock_bh(&bat_priv->tt.commit_lock);
3380 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3381 req_ttvn = tt_data->ttvn;
3383 orig_node = batadv_orig_hash_find(bat_priv, req_src);
3387 primary_if = batadv_primary_if_get_selected(bat_priv);
3391 /* If the full table has been explicitly requested or the gap
3392 * is too big send the whole local translation table
3394 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3395 !bat_priv->tt.last_changeset)
3400 /* TT fragmentation hasn't been implemented yet, so send as many
3401 * TT entries fit a single packet as possible only
3404 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3406 tt_len = bat_priv->tt.last_changeset_len;
3407 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3411 if (!tt_len || !tvlv_len)
3414 /* Copy the last orig_node's OGM buffer */
3415 memcpy(tt_change, bat_priv->tt.last_changeset,
3416 bat_priv->tt.last_changeset_len);
3417 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3419 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3421 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3422 * in the initial part
3425 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3429 if (!tt_len || !tvlv_len)
3432 /* fill the rest of the tvlv with the real TT entries */
3433 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3435 batadv_tt_local_valid, NULL);
3438 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3439 tvlv_tt_data->ttvn = req_ttvn;
3442 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3444 batadv_dbg(BATADV_DBG_TT, bat_priv,
3445 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3446 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3448 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3450 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3451 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3457 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3459 spin_unlock_bh(&bat_priv->tt.commit_lock);
3461 batadv_orig_node_put(orig_node);
3463 batadv_hardif_put(primary_if);
3464 kfree(tvlv_tt_data);
3465 /* The packet was for this host, so it doesn't need to be re-routed */
3470 * batadv_send_tt_response() - send reply to tt request
3471 * @bat_priv: the bat priv with all the soft interface information
3472 * @tt_data: tt data containing the tt request information
3473 * @req_src: mac address of tt request sender
3474 * @req_dst: mac address of tt request recipient
3476 * Return: true if tt request reply was sent, false otherwise.
3478 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3479 struct batadv_tvlv_tt_data *tt_data,
3480 u8 *req_src, u8 *req_dst)
3482 if (batadv_is_my_mac(bat_priv, req_dst))
3483 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3484 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3488 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3489 struct batadv_orig_node *orig_node,
3490 struct batadv_tvlv_tt_change *tt_change,
3491 u16 tt_num_changes, u8 ttvn)
3496 for (i = 0; i < tt_num_changes; i++) {
3497 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3498 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3499 batadv_tt_global_del(bat_priv, orig_node,
3500 (tt_change + i)->addr,
3501 ntohs((tt_change + i)->vid),
3502 "tt removed by changes",
3505 if (!batadv_tt_global_add(bat_priv, orig_node,
3506 (tt_change + i)->addr,
3507 ntohs((tt_change + i)->vid),
3508 (tt_change + i)->flags, ttvn))
3509 /* In case of problem while storing a
3510 * global_entry, we stop the updating
3511 * procedure without committing the
3512 * ttvn change. This will avoid to send
3513 * corrupted data on tt_request
3518 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3521 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3522 struct batadv_tvlv_tt_change *tt_change,
3523 u8 ttvn, u8 *resp_src,
3526 struct batadv_orig_node *orig_node;
3528 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3532 /* Purge the old table first.. */
3533 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3534 "Received full table");
3536 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3539 spin_lock_bh(&orig_node->tt_buff_lock);
3540 kfree(orig_node->tt_buff);
3541 orig_node->tt_buff_len = 0;
3542 orig_node->tt_buff = NULL;
3543 spin_unlock_bh(&orig_node->tt_buff_lock);
3545 atomic_set(&orig_node->last_ttvn, ttvn);
3549 batadv_orig_node_put(orig_node);
3552 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3553 struct batadv_orig_node *orig_node,
3554 u16 tt_num_changes, u8 ttvn,
3555 struct batadv_tvlv_tt_change *tt_change)
3557 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3558 tt_num_changes, ttvn);
3560 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3561 batadv_tt_len(tt_num_changes));
3562 atomic_set(&orig_node->last_ttvn, ttvn);
3566 * batadv_is_my_client() - check if a client is served by the local node
3567 * @bat_priv: the bat priv with all the soft interface information
3568 * @addr: the mac address of the client to check
3569 * @vid: VLAN identifier
3571 * Return: true if the client is served by this node, false otherwise.
3573 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3576 struct batadv_tt_local_entry *tt_local_entry;
3579 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3580 if (!tt_local_entry)
3582 /* Check if the client has been logically deleted (but is kept for
3583 * consistency purpose)
3585 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3586 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3591 batadv_tt_local_entry_put(tt_local_entry);
3596 * batadv_handle_tt_response() - process incoming tt reply
3597 * @bat_priv: the bat priv with all the soft interface information
3598 * @tt_data: tt data containing the tt request information
3599 * @resp_src: mac address of tt reply sender
3600 * @num_entries: number of tt change entries appended to the tt data
3602 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3603 struct batadv_tvlv_tt_data *tt_data,
3604 u8 *resp_src, u16 num_entries)
3606 struct batadv_tt_req_node *node;
3607 struct hlist_node *safe;
3608 struct batadv_orig_node *orig_node = NULL;
3609 struct batadv_tvlv_tt_change *tt_change;
3610 u8 *tvlv_ptr = (u8 *)tt_data;
3613 batadv_dbg(BATADV_DBG_TT, bat_priv,
3614 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3615 resp_src, tt_data->ttvn, num_entries,
3616 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3618 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3622 spin_lock_bh(&orig_node->tt_lock);
3624 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3625 change_offset *= ntohs(tt_data->num_vlan);
3626 change_offset += sizeof(*tt_data);
3627 tvlv_ptr += change_offset;
3629 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3630 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3631 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3632 resp_src, num_entries);
3634 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3635 tt_data->ttvn, tt_change);
3638 /* Recalculate the CRC for this orig_node and store it */
3639 batadv_tt_global_update_crc(bat_priv, orig_node);
3641 spin_unlock_bh(&orig_node->tt_lock);
3643 /* Delete the tt_req_node from pending tt_requests list */
3644 spin_lock_bh(&bat_priv->tt.req_list_lock);
3645 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3646 if (!batadv_compare_eth(node->addr, resp_src))
3648 hlist_del_init(&node->list);
3649 batadv_tt_req_node_put(node);
3652 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3655 batadv_orig_node_put(orig_node);
3658 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3660 struct batadv_tt_roam_node *node, *safe;
3662 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3664 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3665 list_del(&node->list);
3666 kmem_cache_free(batadv_tt_roam_cache, node);
3669 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3672 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3674 struct batadv_tt_roam_node *node, *safe;
3676 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3677 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3678 if (!batadv_has_timed_out(node->first_time,
3679 BATADV_ROAMING_MAX_TIME))
3682 list_del(&node->list);
3683 kmem_cache_free(batadv_tt_roam_cache, node);
3685 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3689 * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3690 * @bat_priv: the bat priv with all the soft interface information
3691 * @client: mac address of the roaming client
3693 * This function checks whether the client already reached the
3694 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3697 * Return: true if the ROAMING_ADV can be sent, false otherwise
3699 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3701 struct batadv_tt_roam_node *tt_roam_node;
3704 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3705 /* The new tt_req will be issued only if I'm not waiting for a
3706 * reply from the same orig_node yet
3708 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3709 if (!batadv_compare_eth(tt_roam_node->addr, client))
3712 if (batadv_has_timed_out(tt_roam_node->first_time,
3713 BATADV_ROAMING_MAX_TIME))
3716 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3717 /* Sorry, you roamed too many times! */
3724 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3729 tt_roam_node->first_time = jiffies;
3730 atomic_set(&tt_roam_node->counter,
3731 BATADV_ROAMING_MAX_COUNT - 1);
3732 ether_addr_copy(tt_roam_node->addr, client);
3734 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3739 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3744 * batadv_send_roam_adv() - send a roaming advertisement message
3745 * @bat_priv: the bat priv with all the soft interface information
3746 * @client: mac address of the roaming client
3747 * @vid: VLAN identifier
3748 * @orig_node: message destination
3750 * Send a ROAMING_ADV message to the node which was previously serving this
3751 * client. This is done to inform the node that from now on all traffic destined
3752 * for this particular roamed client has to be forwarded to the sender of the
3755 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3757 struct batadv_orig_node *orig_node)
3759 struct batadv_hard_iface *primary_if;
3760 struct batadv_tvlv_roam_adv tvlv_roam;
3762 primary_if = batadv_primary_if_get_selected(bat_priv);
3766 /* before going on we have to check whether the client has
3767 * already roamed to us too many times
3769 if (!batadv_tt_check_roam_count(bat_priv, client))
3772 batadv_dbg(BATADV_DBG_TT, bat_priv,
3773 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3774 orig_node->orig, client, batadv_print_vid(vid));
3776 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3778 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3779 tvlv_roam.vid = htons(vid);
3781 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3782 orig_node->orig, BATADV_TVLV_ROAM, 1,
3783 &tvlv_roam, sizeof(tvlv_roam));
3787 batadv_hardif_put(primary_if);
3790 static void batadv_tt_purge(struct work_struct *work)
3792 struct delayed_work *delayed_work;
3793 struct batadv_priv_tt *priv_tt;
3794 struct batadv_priv *bat_priv;
3796 delayed_work = to_delayed_work(work);
3797 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3798 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3800 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3801 batadv_tt_global_purge(bat_priv);
3802 batadv_tt_req_purge(bat_priv);
3803 batadv_tt_roam_purge(bat_priv);
3805 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3806 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3810 * batadv_tt_free() - Free translation table of soft interface
3811 * @bat_priv: the bat priv with all the soft interface information
3813 void batadv_tt_free(struct batadv_priv *bat_priv)
3815 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3817 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3818 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3820 cancel_delayed_work_sync(&bat_priv->tt.work);
3822 batadv_tt_local_table_free(bat_priv);
3823 batadv_tt_global_table_free(bat_priv);
3824 batadv_tt_req_list_free(bat_priv);
3825 batadv_tt_changes_list_free(bat_priv);
3826 batadv_tt_roam_list_free(bat_priv);
3828 kfree(bat_priv->tt.last_changeset);
3832 * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3833 * table and possibly count them in the TT size
3834 * @bat_priv: the bat priv with all the soft interface information
3835 * @flags: the flag to switch
3836 * @enable: whether to set or unset the flag
3837 * @count: whether to increase the TT size by the number of changed entries
3839 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3840 bool enable, bool count)
3842 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3843 struct batadv_tt_common_entry *tt_common_entry;
3844 struct hlist_head *head;
3850 for (i = 0; i < hash->size; i++) {
3851 head = &hash->table[i];
3854 hlist_for_each_entry_rcu(tt_common_entry,
3857 if ((tt_common_entry->flags & flags) == flags)
3859 tt_common_entry->flags |= flags;
3861 if (!(tt_common_entry->flags & flags))
3863 tt_common_entry->flags &= ~flags;
3869 batadv_tt_local_size_inc(bat_priv,
3870 tt_common_entry->vid);
3876 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3877 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3879 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3880 struct batadv_tt_common_entry *tt_common;
3881 struct batadv_tt_local_entry *tt_local;
3882 struct hlist_node *node_tmp;
3883 struct hlist_head *head;
3884 spinlock_t *list_lock; /* protects write access to the hash lists */
3890 for (i = 0; i < hash->size; i++) {
3891 head = &hash->table[i];
3892 list_lock = &hash->list_locks[i];
3894 spin_lock_bh(list_lock);
3895 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3897 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3900 batadv_dbg(BATADV_DBG_TT, bat_priv,
3901 "Deleting local tt entry (%pM, vid: %d): pending\n",
3903 batadv_print_vid(tt_common->vid));
3905 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3906 hlist_del_rcu(&tt_common->hash_entry);
3907 tt_local = container_of(tt_common,
3908 struct batadv_tt_local_entry,
3911 batadv_tt_local_entry_put(tt_local);
3913 spin_unlock_bh(list_lock);
3918 * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3919 * which have been queued in the time since the last commit
3920 * @bat_priv: the bat priv with all the soft interface information
3922 * Caller must hold tt->commit_lock.
3924 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3926 lockdep_assert_held(&bat_priv->tt.commit_lock);
3928 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3929 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3930 batadv_tt_tvlv_container_update(bat_priv);
3934 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3936 batadv_tt_local_purge_pending_clients(bat_priv);
3937 batadv_tt_local_update_crc(bat_priv);
3939 /* Increment the TTVN only once per OGM interval */
3940 atomic_inc(&bat_priv->tt.vn);
3941 batadv_dbg(BATADV_DBG_TT, bat_priv,
3942 "Local changes committed, updating to ttvn %u\n",
3943 (u8)atomic_read(&bat_priv->tt.vn));
3945 /* reset the sending counter */
3946 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3947 batadv_tt_tvlv_container_update(bat_priv);
3951 * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3952 * have been queued in the time since the last commit
3953 * @bat_priv: the bat priv with all the soft interface information
3955 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3957 spin_lock_bh(&bat_priv->tt.commit_lock);
3958 batadv_tt_local_commit_changes_nolock(bat_priv);
3959 spin_unlock_bh(&bat_priv->tt.commit_lock);
3963 * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3964 * @bat_priv: the bat priv with all the soft interface information
3965 * @src: source mac address of packet
3966 * @dst: destination mac address of packet
3967 * @vid: vlan id of packet
3969 * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3971 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3974 struct batadv_tt_local_entry *tt_local_entry;
3975 struct batadv_tt_global_entry *tt_global_entry;
3976 struct batadv_softif_vlan *vlan;
3979 vlan = batadv_softif_vlan_get(bat_priv, vid);
3983 if (!atomic_read(&vlan->ap_isolation))
3986 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3987 if (!tt_local_entry)
3990 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3991 if (!tt_global_entry)
3992 goto local_entry_put;
3994 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3997 batadv_tt_global_entry_put(tt_global_entry);
3999 batadv_tt_local_entry_put(tt_local_entry);
4001 batadv_softif_vlan_put(vlan);
4006 * batadv_tt_update_orig() - update global translation table with new tt
4007 * information received via ogms
4008 * @bat_priv: the bat priv with all the soft interface information
4009 * @orig_node: the orig_node of the ogm
4010 * @tt_buff: pointer to the first tvlv VLAN entry
4011 * @tt_num_vlan: number of tvlv VLAN entries
4012 * @tt_change: pointer to the first entry in the TT buffer
4013 * @tt_num_changes: number of tt changes inside the tt buffer
4014 * @ttvn: translation table version number of this changeset
4016 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
4017 struct batadv_orig_node *orig_node,
4018 const void *tt_buff, u16 tt_num_vlan,
4019 struct batadv_tvlv_tt_change *tt_change,
4020 u16 tt_num_changes, u8 ttvn)
4022 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
4023 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4024 bool full_table = true;
4027 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
4028 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
4029 &orig_node->capa_initialized);
4031 /* orig table not initialised AND first diff is in the OGM OR the ttvn
4032 * increased by one -> we can apply the attached changes
4034 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
4035 /* the OGM could not contain the changes due to their size or
4036 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
4038 * In this case send a tt request
4040 if (!tt_num_changes) {
4045 spin_lock_bh(&orig_node->tt_lock);
4047 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
4050 /* Even if we received the precomputed crc with the OGM, we
4051 * prefer to recompute it to spot any possible inconsistency
4052 * in the global table
4054 batadv_tt_global_update_crc(bat_priv, orig_node);
4056 spin_unlock_bh(&orig_node->tt_lock);
4058 /* The ttvn alone is not enough to guarantee consistency
4059 * because a single value could represent different states
4060 * (due to the wrap around). Thus a node has to check whether
4061 * the resulting table (after applying the changes) is still
4062 * consistent or not. E.g. a node could disconnect while its
4063 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
4064 * checking the CRC value is mandatory to detect the
4067 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
4071 /* if we missed more than one change or our tables are not
4072 * in sync anymore -> request fresh tt data
4074 if (!has_tt_init || ttvn != orig_ttvn ||
4075 !batadv_tt_global_check_crc(orig_node, tt_vlan,
4078 batadv_dbg(BATADV_DBG_TT, bat_priv,
4079 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
4080 orig_node->orig, ttvn, orig_ttvn,
4082 batadv_send_tt_request(bat_priv, orig_node, ttvn,
4083 tt_vlan, tt_num_vlan,
4091 * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
4092 * @bat_priv: the bat priv with all the soft interface information
4093 * @addr: the mac address of the client to check
4094 * @vid: VLAN identifier
4096 * Return: true if we know that the client has moved from its old originator
4097 * to another one. This entry is still kept for consistency purposes and will be
4098 * deleted later by a DEL or because of timeout
4100 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
4101 u8 *addr, unsigned short vid)
4103 struct batadv_tt_global_entry *tt_global_entry;
4106 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
4107 if (!tt_global_entry)
4110 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4111 batadv_tt_global_entry_put(tt_global_entry);
4117 * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
4118 * @bat_priv: the bat priv with all the soft interface information
4119 * @addr: the mac address of the local client to query
4120 * @vid: VLAN identifier
4122 * Return: true if the local client is known to be roaming (it is not served by
4123 * this node anymore) or not. If yes, the client is still present in the table
4124 * to keep the latter consistent with the node TTVN
4126 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
4127 u8 *addr, unsigned short vid)
4129 struct batadv_tt_local_entry *tt_local_entry;
4132 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
4133 if (!tt_local_entry)
4136 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4137 batadv_tt_local_entry_put(tt_local_entry);
4143 * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
4144 * @bat_priv: the bat priv with all the soft interface information
4145 * @orig_node: orig node which the temporary entry should be associated with
4146 * @addr: mac address of the client
4147 * @vid: VLAN id of the new temporary global translation table
4149 * Return: true when temporary tt entry could be added, false otherwise
4151 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4152 struct batadv_orig_node *orig_node,
4153 const unsigned char *addr,
4156 /* ignore loop detect macs, they are not supposed to be in the tt local
4159 if (batadv_bla_is_loopdetect_mac(addr))
4162 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
4163 BATADV_TT_CLIENT_TEMP,
4164 atomic_read(&orig_node->last_ttvn)))
4167 batadv_dbg(BATADV_DBG_TT, bat_priv,
4168 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
4169 addr, batadv_print_vid(vid), orig_node->orig);
4175 * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
4176 * maximum packet size that can be transported through the mesh
4177 * @soft_iface: netdev struct of the mesh interface
4179 * Remove entries older than 'timeout' and half timeout if more entries need
4182 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4184 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4185 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4186 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4187 bool reduced = false;
4189 spin_lock_bh(&bat_priv->tt.commit_lock);
4192 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4193 if (packet_size_max >= table_size)
4196 batadv_tt_local_purge(bat_priv, timeout);
4197 batadv_tt_local_purge_pending_clients(bat_priv);
4201 net_ratelimited_function(batadv_info, soft_iface,
4202 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4206 /* commit these changes immediately, to avoid synchronization problem
4210 batadv_tt_local_commit_changes_nolock(bat_priv);
4212 spin_unlock_bh(&bat_priv->tt.commit_lock);
4216 * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
4217 * @bat_priv: the bat priv with all the soft interface information
4218 * @orig: the orig_node of the ogm
4219 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4220 * @tvlv_value: tvlv buffer containing the gateway data
4221 * @tvlv_value_len: tvlv buffer length
4223 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4224 struct batadv_orig_node *orig,
4225 u8 flags, void *tvlv_value,
4228 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4229 struct batadv_tvlv_tt_change *tt_change;
4230 struct batadv_tvlv_tt_data *tt_data;
4231 u16 num_entries, num_vlan;
4233 if (tvlv_value_len < sizeof(*tt_data))
4236 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4237 tvlv_value_len -= sizeof(*tt_data);
4239 num_vlan = ntohs(tt_data->num_vlan);
4241 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4244 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4245 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4246 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4248 num_entries = batadv_tt_entries(tvlv_value_len);
4250 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4251 num_entries, tt_data->ttvn);
4255 * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
4257 * @bat_priv: the bat priv with all the soft interface information
4258 * @src: mac address of tt tvlv sender
4259 * @dst: mac address of tt tvlv recipient
4260 * @tvlv_value: tvlv buffer containing the tt data
4261 * @tvlv_value_len: tvlv buffer length
4263 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4266 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4271 struct batadv_tvlv_tt_data *tt_data;
4272 u16 tt_vlan_len, tt_num_entries;
4276 if (tvlv_value_len < sizeof(*tt_data))
4277 return NET_RX_SUCCESS;
4279 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4280 tvlv_value_len -= sizeof(*tt_data);
4282 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4283 tt_vlan_len *= ntohs(tt_data->num_vlan);
4285 if (tvlv_value_len < tt_vlan_len)
4286 return NET_RX_SUCCESS;
4288 tvlv_value_len -= tt_vlan_len;
4289 tt_num_entries = batadv_tt_entries(tvlv_value_len);
4291 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4292 case BATADV_TT_REQUEST:
4293 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4295 /* If this node cannot provide a TT response the tt_request is
4298 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4300 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4305 batadv_dbg(BATADV_DBG_TT, bat_priv,
4306 "Routing TT_REQUEST to %pM [%c]\n",
4308 /* tvlv API will re-route the packet */
4312 case BATADV_TT_RESPONSE:
4313 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4315 if (batadv_is_my_mac(bat_priv, dst)) {
4316 batadv_handle_tt_response(bat_priv, tt_data,
4317 src, tt_num_entries);
4318 return NET_RX_SUCCESS;
4321 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4326 batadv_dbg(BATADV_DBG_TT, bat_priv,
4327 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4329 /* tvlv API will re-route the packet */
4333 return NET_RX_SUCCESS;
4337 * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4339 * @bat_priv: the bat priv with all the soft interface information
4340 * @src: mac address of tt tvlv sender
4341 * @dst: mac address of tt tvlv recipient
4342 * @tvlv_value: tvlv buffer containing the tt data
4343 * @tvlv_value_len: tvlv buffer length
4345 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4348 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4353 struct batadv_tvlv_roam_adv *roaming_adv;
4354 struct batadv_orig_node *orig_node = NULL;
4356 /* If this node is not the intended recipient of the
4357 * roaming advertisement the packet is forwarded
4358 * (the tvlv API will re-route the packet).
4360 if (!batadv_is_my_mac(bat_priv, dst))
4363 if (tvlv_value_len < sizeof(*roaming_adv))
4366 orig_node = batadv_orig_hash_find(bat_priv, src);
4370 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4371 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4373 batadv_dbg(BATADV_DBG_TT, bat_priv,
4374 "Received ROAMING_ADV from %pM (client %pM)\n",
4375 src, roaming_adv->client);
4377 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4378 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4379 atomic_read(&orig_node->last_ttvn) + 1);
4383 batadv_orig_node_put(orig_node);
4384 return NET_RX_SUCCESS;
4388 * batadv_tt_init() - initialise the translation table internals
4389 * @bat_priv: the bat priv with all the soft interface information
4391 * Return: 0 on success or negative error number in case of failure.
4393 int batadv_tt_init(struct batadv_priv *bat_priv)
4397 /* synchronized flags must be remote */
4398 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4400 ret = batadv_tt_local_init(bat_priv);
4404 ret = batadv_tt_global_init(bat_priv);
4406 batadv_tt_local_table_free(bat_priv);
4410 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4411 batadv_tt_tvlv_unicast_handler_v1,
4412 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4414 batadv_tvlv_handler_register(bat_priv, NULL,
4415 batadv_roam_tvlv_unicast_handler_v1,
4416 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4418 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4419 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4420 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4426 * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4427 * @bat_priv: the bat priv with all the soft interface information
4428 * @addr: the mac address of the client
4429 * @vid: the identifier of the VLAN where this client is connected
4431 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4434 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4435 const u8 *addr, unsigned short vid)
4437 struct batadv_tt_global_entry *tt;
4440 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4444 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4446 batadv_tt_global_entry_put(tt);
4452 * batadv_tt_cache_init() - Initialize tt memory object cache
4454 * Return: 0 on success or negative error number in case of failure.
4456 int __init batadv_tt_cache_init(void)
4458 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4459 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4460 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4461 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4462 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4463 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4465 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4466 SLAB_HWCACHE_ALIGN, NULL);
4467 if (!batadv_tl_cache)
4470 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4471 SLAB_HWCACHE_ALIGN, NULL);
4472 if (!batadv_tg_cache)
4473 goto err_tt_tl_destroy;
4475 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4477 SLAB_HWCACHE_ALIGN, NULL);
4478 if (!batadv_tt_orig_cache)
4479 goto err_tt_tg_destroy;
4481 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4483 SLAB_HWCACHE_ALIGN, NULL);
4484 if (!batadv_tt_change_cache)
4485 goto err_tt_orig_destroy;
4487 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4489 SLAB_HWCACHE_ALIGN, NULL);
4490 if (!batadv_tt_req_cache)
4491 goto err_tt_change_destroy;
4493 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4495 SLAB_HWCACHE_ALIGN, NULL);
4496 if (!batadv_tt_roam_cache)
4497 goto err_tt_req_destroy;
4502 kmem_cache_destroy(batadv_tt_req_cache);
4503 batadv_tt_req_cache = NULL;
4504 err_tt_change_destroy:
4505 kmem_cache_destroy(batadv_tt_change_cache);
4506 batadv_tt_change_cache = NULL;
4507 err_tt_orig_destroy:
4508 kmem_cache_destroy(batadv_tt_orig_cache);
4509 batadv_tt_orig_cache = NULL;
4511 kmem_cache_destroy(batadv_tg_cache);
4512 batadv_tg_cache = NULL;
4514 kmem_cache_destroy(batadv_tl_cache);
4515 batadv_tl_cache = NULL;
4521 * batadv_tt_cache_destroy() - Destroy tt memory object cache
4523 void batadv_tt_cache_destroy(void)
4525 kmem_cache_destroy(batadv_tl_cache);
4526 kmem_cache_destroy(batadv_tg_cache);
4527 kmem_cache_destroy(batadv_tt_orig_cache);
4528 kmem_cache_destroy(batadv_tt_change_cache);
4529 kmem_cache_destroy(batadv_tt_req_cache);
4530 kmem_cache_destroy(batadv_tt_roam_cache);