1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 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/skbuff.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/workqueue.h>
39 #include <net/genetlink.h>
40 #include <net/netlink.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
45 #include "bridge_loop_avoidance.h"
46 #include "hard-interface.h"
50 #include "originator.h"
51 #include "soft-interface.h"
54 static struct kmem_cache *batadv_tl_cache __read_mostly;
55 static struct kmem_cache *batadv_tg_cache __read_mostly;
56 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
57 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
58 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
59 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
62 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
63 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
65 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
67 struct batadv_orig_node *orig_node);
68 static void batadv_tt_purge(struct work_struct *work);
70 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
71 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
72 struct batadv_orig_node *orig_node,
73 const unsigned char *addr,
74 unsigned short vid, const char *message,
78 * batadv_compare_tt() - check if two TT entries are the same
79 * @node: the list element pointer of the first TT entry
80 * @data2: pointer to the tt_common_entry of the second TT entry
82 * Compare the MAC address and the VLAN ID of the two TT entries and check if
83 * they are the same TT client.
84 * Return: true if the two TT clients are the same, false otherwise
86 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
88 const void *data1 = container_of(node, struct batadv_tt_common_entry,
90 const struct batadv_tt_common_entry *tt1 = data1;
91 const struct batadv_tt_common_entry *tt2 = data2;
93 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
97 * batadv_choose_tt() - return the index of the tt entry in the hash table
98 * @data: pointer to the tt_common_entry object to map
99 * @size: the size of the hash table
101 * Return: the hash index where the object represented by 'data' should be
104 static inline u32 batadv_choose_tt(const void *data, u32 size)
106 struct batadv_tt_common_entry *tt;
109 tt = (struct batadv_tt_common_entry *)data;
110 hash = jhash(&tt->addr, ETH_ALEN, hash);
111 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
117 * batadv_tt_hash_find() - look for a client in the given hash table
118 * @hash: the hash table to search
119 * @addr: the mac address of the client to look for
120 * @vid: VLAN identifier
122 * Return: a pointer to the tt_common struct belonging to the searched client if
123 * found, NULL otherwise.
125 static struct batadv_tt_common_entry *
126 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
129 struct hlist_head *head;
130 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
136 ether_addr_copy(to_search.addr, addr);
139 index = batadv_choose_tt(&to_search, hash->size);
140 head = &hash->table[index];
143 hlist_for_each_entry_rcu(tt, head, hash_entry) {
144 if (!batadv_compare_eth(tt, addr))
150 if (!kref_get_unless_zero(&tt->refcount))
162 * batadv_tt_local_hash_find() - search the local table for a given client
163 * @bat_priv: the bat priv with all the soft interface information
164 * @addr: the mac address of the client to look for
165 * @vid: VLAN identifier
167 * Return: a pointer to the corresponding tt_local_entry struct if the client is
168 * found, NULL otherwise.
170 static struct batadv_tt_local_entry *
171 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
174 struct batadv_tt_common_entry *tt_common_entry;
175 struct batadv_tt_local_entry *tt_local_entry = NULL;
177 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
180 tt_local_entry = container_of(tt_common_entry,
181 struct batadv_tt_local_entry,
183 return tt_local_entry;
187 * batadv_tt_global_hash_find() - search the global table for a given client
188 * @bat_priv: the bat priv with all the soft interface information
189 * @addr: the mac address of the client to look for
190 * @vid: VLAN identifier
192 * Return: a pointer to the corresponding tt_global_entry struct if the client
193 * is found, NULL otherwise.
195 struct batadv_tt_global_entry *
196 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
199 struct batadv_tt_common_entry *tt_common_entry;
200 struct batadv_tt_global_entry *tt_global_entry = NULL;
202 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
205 tt_global_entry = container_of(tt_common_entry,
206 struct batadv_tt_global_entry,
208 return tt_global_entry;
212 * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
213 * @rcu: rcu pointer of the tt_local_entry
215 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
217 struct batadv_tt_local_entry *tt_local_entry;
219 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
222 kmem_cache_free(batadv_tl_cache, tt_local_entry);
226 * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
227 * for free after rcu grace period
228 * @ref: kref pointer of the nc_node
230 static void batadv_tt_local_entry_release(struct kref *ref)
232 struct batadv_tt_local_entry *tt_local_entry;
234 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
237 batadv_softif_vlan_put(tt_local_entry->vlan);
239 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
243 * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
244 * possibly release it
245 * @tt_local_entry: tt_local_entry to be free'd
248 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
250 kref_put(&tt_local_entry->common.refcount,
251 batadv_tt_local_entry_release);
255 * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
256 * @rcu: rcu pointer of the tt_global_entry
258 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
260 struct batadv_tt_global_entry *tt_global_entry;
262 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
265 kmem_cache_free(batadv_tg_cache, tt_global_entry);
269 * batadv_tt_global_entry_release() - release tt_global_entry from lists and
270 * queue for free after rcu grace period
271 * @ref: kref pointer of the nc_node
273 static void batadv_tt_global_entry_release(struct kref *ref)
275 struct batadv_tt_global_entry *tt_global_entry;
277 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
280 batadv_tt_global_del_orig_list(tt_global_entry);
282 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
286 * batadv_tt_global_entry_put() - decrement the tt_global_entry refcounter and
287 * possibly release it
288 * @tt_global_entry: tt_global_entry to be free'd
290 void batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
292 kref_put(&tt_global_entry->common.refcount,
293 batadv_tt_global_entry_release);
297 * batadv_tt_global_hash_count() - count the number of orig entries
298 * @bat_priv: the bat priv with all the soft interface information
299 * @addr: the mac address of the client to count entries for
300 * @vid: VLAN identifier
302 * Return: the number of originators advertising the given address/data
303 * (excluding our self).
305 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
306 const u8 *addr, unsigned short vid)
308 struct batadv_tt_global_entry *tt_global_entry;
311 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
312 if (!tt_global_entry)
315 count = atomic_read(&tt_global_entry->orig_list_count);
316 batadv_tt_global_entry_put(tt_global_entry);
322 * batadv_tt_local_size_mod() - change the size by v of the local table
324 * @bat_priv: the bat priv with all the soft interface information
325 * @vid: the VLAN identifier of the sub-table to change
326 * @v: the amount to sum to the local table size
328 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
329 unsigned short vid, int v)
331 struct batadv_softif_vlan *vlan;
333 vlan = batadv_softif_vlan_get(bat_priv, vid);
337 atomic_add(v, &vlan->tt.num_entries);
339 batadv_softif_vlan_put(vlan);
343 * batadv_tt_local_size_inc() - increase by one the local table size for the
345 * @bat_priv: the bat priv with all the soft interface information
346 * @vid: the VLAN identifier
348 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
351 batadv_tt_local_size_mod(bat_priv, vid, 1);
355 * batadv_tt_local_size_dec() - decrease by one the local table size for the
357 * @bat_priv: the bat priv with all the soft interface information
358 * @vid: the VLAN identifier
360 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
363 batadv_tt_local_size_mod(bat_priv, vid, -1);
367 * batadv_tt_global_size_mod() - change the size by v of the global table
368 * for orig_node identified by vid
369 * @orig_node: the originator for which the table has to be modified
370 * @vid: the VLAN identifier
371 * @v: the amount to sum to the global table size
373 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
374 unsigned short vid, int v)
376 struct batadv_orig_node_vlan *vlan;
378 vlan = batadv_orig_node_vlan_new(orig_node, vid);
382 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
383 spin_lock_bh(&orig_node->vlan_list_lock);
384 if (!hlist_unhashed(&vlan->list)) {
385 hlist_del_init_rcu(&vlan->list);
386 batadv_orig_node_vlan_put(vlan);
388 spin_unlock_bh(&orig_node->vlan_list_lock);
391 batadv_orig_node_vlan_put(vlan);
395 * batadv_tt_global_size_inc() - increase by one the global table size for the
397 * @orig_node: the originator which global table size has to be decreased
398 * @vid: the vlan identifier
400 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
403 batadv_tt_global_size_mod(orig_node, vid, 1);
407 * batadv_tt_global_size_dec() - decrease by one the global table size for the
409 * @orig_node: the originator which global table size has to be decreased
410 * @vid: the vlan identifier
412 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
415 batadv_tt_global_size_mod(orig_node, vid, -1);
419 * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
420 * @rcu: rcu pointer of the orig_entry
422 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
424 struct batadv_tt_orig_list_entry *orig_entry;
426 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
428 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
432 * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
433 * queue for free after rcu grace period
434 * @ref: kref pointer of the tt orig entry
436 static void batadv_tt_orig_list_entry_release(struct kref *ref)
438 struct batadv_tt_orig_list_entry *orig_entry;
440 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
443 batadv_orig_node_put(orig_entry->orig_node);
444 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
448 * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
449 * possibly release it
450 * @orig_entry: tt orig entry to be free'd
453 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
455 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
459 * batadv_tt_local_event() - store a local TT event (ADD/DEL)
460 * @bat_priv: the bat priv with all the soft interface information
461 * @tt_local_entry: the TT entry involved in the event
462 * @event_flags: flags to store in the event structure
464 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
465 struct batadv_tt_local_entry *tt_local_entry,
468 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
469 struct batadv_tt_common_entry *common = &tt_local_entry->common;
470 u8 flags = common->flags | event_flags;
471 bool event_removed = false;
472 bool del_op_requested, del_op_entry;
474 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
478 tt_change_node->change.flags = flags;
479 memset(tt_change_node->change.reserved, 0,
480 sizeof(tt_change_node->change.reserved));
481 ether_addr_copy(tt_change_node->change.addr, common->addr);
482 tt_change_node->change.vid = htons(common->vid);
484 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
486 /* check for ADD+DEL or DEL+ADD events */
487 spin_lock_bh(&bat_priv->tt.changes_list_lock);
488 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
490 if (!batadv_compare_eth(entry->change.addr, common->addr))
493 /* DEL+ADD in the same orig interval have no effect and can be
494 * removed to avoid silly behaviour on the receiver side. The
495 * other way around (ADD+DEL) can happen in case of roaming of
496 * a client still in the NEW state. Roaming of NEW clients is
497 * now possible due to automatically recognition of "temporary"
500 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
501 if (!del_op_requested && del_op_entry)
503 if (del_op_requested && !del_op_entry)
506 /* this is a second add in the same originator interval. It
507 * means that flags have been changed: update them!
509 if (!del_op_requested && !del_op_entry)
510 entry->change.flags = flags;
514 list_del(&entry->list);
515 kmem_cache_free(batadv_tt_change_cache, entry);
516 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
517 event_removed = true;
521 /* track the change in the OGMinterval list */
522 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
525 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
528 atomic_dec(&bat_priv->tt.local_changes);
530 atomic_inc(&bat_priv->tt.local_changes);
534 * batadv_tt_len() - compute length in bytes of given number of tt changes
535 * @changes_num: number of tt changes
537 * Return: computed length in bytes.
539 static int batadv_tt_len(int changes_num)
541 return changes_num * sizeof(struct batadv_tvlv_tt_change);
545 * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
546 * @tt_len: available space
548 * Return: the number of entries.
550 static u16 batadv_tt_entries(u16 tt_len)
552 return tt_len / batadv_tt_len(1);
556 * batadv_tt_local_table_transmit_size() - calculates the local translation
557 * table size when transmitted over the air
558 * @bat_priv: the bat priv with all the soft interface information
560 * Return: local translation table size in bytes.
562 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
565 u16 tt_local_entries = 0;
566 struct batadv_softif_vlan *vlan;
570 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
572 tt_local_entries += atomic_read(&vlan->tt.num_entries);
576 /* header size of tvlv encapsulated tt response payload */
577 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
578 hdr_size += sizeof(struct batadv_tvlv_hdr);
579 hdr_size += sizeof(struct batadv_tvlv_tt_data);
580 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
582 return hdr_size + batadv_tt_len(tt_local_entries);
585 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
587 if (bat_priv->tt.local_hash)
590 bat_priv->tt.local_hash = batadv_hash_new(1024);
592 if (!bat_priv->tt.local_hash)
595 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
596 &batadv_tt_local_hash_lock_class_key);
601 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
602 struct batadv_tt_global_entry *tt_global,
605 struct batadv_tt_global_entry *tt_removed_entry;
606 struct hlist_node *tt_removed_node;
608 batadv_dbg(BATADV_DBG_TT, bat_priv,
609 "Deleting global tt entry %pM (vid: %d): %s\n",
610 tt_global->common.addr,
611 batadv_print_vid(tt_global->common.vid), message);
613 tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
617 if (!tt_removed_node)
620 /* drop reference of remove hash entry */
621 tt_removed_entry = hlist_entry(tt_removed_node,
622 struct batadv_tt_global_entry,
624 batadv_tt_global_entry_put(tt_removed_entry);
628 * batadv_tt_local_add() - add a new client to the local table or update an
630 * @soft_iface: netdev struct of the mesh interface
631 * @addr: the mac address of the client to add
632 * @vid: VLAN identifier
633 * @ifindex: index of the interface where the client is connected to (useful to
634 * identify wireless clients)
635 * @mark: the value contained in the skb->mark field of the received packet (if
638 * Return: true if the client was successfully added, false otherwise.
640 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
641 unsigned short vid, int ifindex, u32 mark)
643 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
644 struct batadv_tt_local_entry *tt_local;
645 struct batadv_tt_global_entry *tt_global = NULL;
646 struct net *net = dev_net(soft_iface);
647 struct batadv_softif_vlan *vlan;
648 struct net_device *in_dev = NULL;
649 struct batadv_hard_iface *in_hardif = NULL;
650 struct hlist_head *head;
651 struct batadv_tt_orig_list_entry *orig_entry;
652 int hash_added, table_size, packet_size_max;
654 bool roamed_back = false;
658 if (ifindex != BATADV_NULL_IFINDEX)
659 in_dev = dev_get_by_index(net, ifindex);
662 in_hardif = batadv_hardif_get_by_netdev(in_dev);
664 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
666 if (!is_multicast_ether_addr(addr))
667 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
670 tt_local->last_seen = jiffies;
671 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
672 batadv_dbg(BATADV_DBG_TT, bat_priv,
673 "Re-adding pending client %pM (vid: %d)\n",
674 addr, batadv_print_vid(vid));
675 /* whatever the reason why the PENDING flag was set,
676 * this is a client which was enqueued to be removed in
677 * this orig_interval. Since it popped up again, the
678 * flag can be reset like it was never enqueued
680 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
684 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
685 batadv_dbg(BATADV_DBG_TT, bat_priv,
686 "Roaming client %pM (vid: %d) came back to its original location\n",
687 addr, batadv_print_vid(vid));
688 /* the ROAM flag is set because this client roamed away
689 * and the node got a roaming_advertisement message. Now
690 * that the client popped up again at its original
691 * location such flag can be unset
693 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
699 /* Ignore the client if we cannot send it in a full table response. */
700 table_size = batadv_tt_local_table_transmit_size(bat_priv);
701 table_size += batadv_tt_len(1);
702 packet_size_max = atomic_read(&bat_priv->packet_size_max);
703 if (table_size > packet_size_max) {
704 net_ratelimited_function(batadv_info, soft_iface,
705 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
706 table_size, packet_size_max, addr);
710 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
714 /* increase the refcounter of the related vlan */
715 vlan = batadv_softif_vlan_get(bat_priv, vid);
717 net_ratelimited_function(batadv_info, soft_iface,
718 "adding TT local entry %pM to non-existent VLAN %d\n",
719 addr, batadv_print_vid(vid));
720 kmem_cache_free(batadv_tl_cache, tt_local);
725 batadv_dbg(BATADV_DBG_TT, bat_priv,
726 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
727 addr, batadv_print_vid(vid),
728 (u8)atomic_read(&bat_priv->tt.vn));
730 ether_addr_copy(tt_local->common.addr, addr);
731 /* The local entry has to be marked as NEW to avoid to send it in
732 * a full table response going out before the next ttvn increment
733 * (consistency check)
735 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
736 tt_local->common.vid = vid;
737 if (batadv_is_wifi_hardif(in_hardif))
738 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
739 kref_init(&tt_local->common.refcount);
740 tt_local->last_seen = jiffies;
741 tt_local->common.added_at = tt_local->last_seen;
742 tt_local->vlan = vlan;
744 /* the batman interface mac and multicast addresses should never be
747 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
748 is_multicast_ether_addr(addr))
749 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
751 kref_get(&tt_local->common.refcount);
752 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
753 batadv_choose_tt, &tt_local->common,
754 &tt_local->common.hash_entry);
756 if (unlikely(hash_added != 0)) {
757 /* remove the reference for the hash */
758 batadv_tt_local_entry_put(tt_local);
763 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
766 /* Check whether it is a roaming, but don't do anything if the roaming
767 * process has already been handled
769 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
770 /* These node are probably going to update their tt table */
771 head = &tt_global->orig_list;
773 hlist_for_each_entry_rcu(orig_entry, head, list) {
774 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
775 tt_global->common.vid,
776 orig_entry->orig_node);
780 batadv_tt_global_free(bat_priv, tt_global,
784 /* The global entry has to be marked as ROAMING and
785 * has to be kept for consistency purpose
787 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
788 tt_global->roam_at = jiffies;
792 /* store the current remote flags before altering them. This helps
793 * understanding is flags are changing or not
795 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
797 if (batadv_is_wifi_hardif(in_hardif))
798 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
800 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
802 /* check the mark in the skb: if it's equal to the configured
803 * isolation_mark, it means the packet is coming from an isolated
806 match_mark = (mark & bat_priv->isolation_mark_mask);
807 if (bat_priv->isolation_mark_mask &&
808 match_mark == bat_priv->isolation_mark)
809 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
811 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
813 /* if any "dynamic" flag has been modified, resend an ADD event for this
814 * entry so that all the nodes can get the new flags
816 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
817 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
822 batadv_hardif_put(in_hardif);
826 batadv_tt_local_entry_put(tt_local);
828 batadv_tt_global_entry_put(tt_global);
833 * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
834 * within a TT Response directed to another node
835 * @orig_node: originator for which the TT data has to be prepared
836 * @tt_data: uninitialised pointer to the address of the TVLV buffer
837 * @tt_change: uninitialised pointer to the address of the area where the TT
838 * changed can be stored
839 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
840 * function reserves the amount of space needed to send the entire global TT
841 * table. In case of success the value is updated with the real amount of
843 * Allocate the needed amount of memory for the entire TT TVLV and write its
844 * header made up of one tvlv_tt_data object and a series of tvlv_tt_vlan_data
845 * objects, one per active VLAN served by the originator node.
847 * Return: the size of the allocated buffer or 0 in case of failure.
850 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
851 struct batadv_tvlv_tt_data **tt_data,
852 struct batadv_tvlv_tt_change **tt_change,
859 struct batadv_tvlv_tt_vlan_data *tt_vlan;
860 struct batadv_orig_node_vlan *vlan;
863 spin_lock_bh(&orig_node->vlan_list_lock);
864 hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
866 num_entries += atomic_read(&vlan->tt.num_entries);
869 change_offset = sizeof(**tt_data);
870 change_offset += num_vlan * sizeof(*tt_vlan);
872 /* if tt_len is negative, allocate the space needed by the full table */
874 *tt_len = batadv_tt_len(num_entries);
877 tvlv_len += change_offset;
879 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
885 (*tt_data)->flags = BATADV_NO_FLAGS;
886 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
887 (*tt_data)->num_vlan = htons(num_vlan);
889 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
890 hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
891 tt_vlan->vid = htons(vlan->vid);
892 tt_vlan->crc = htonl(vlan->tt.crc);
893 tt_vlan->reserved = 0;
898 tt_change_ptr = (u8 *)*tt_data + change_offset;
899 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
902 spin_unlock_bh(&orig_node->vlan_list_lock);
907 * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
909 * @bat_priv: the bat priv with all the soft interface information
910 * @tt_data: uninitialised pointer to the address of the TVLV buffer
911 * @tt_change: uninitialised pointer to the address of the area where the TT
912 * changes can be stored
913 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
914 * function reserves the amount of space needed to send the entire local TT
915 * table. In case of success the value is updated with the real amount of
918 * Allocate the needed amount of memory for the entire TT TVLV and write its
919 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
920 * objects, one per active VLAN.
922 * Return: the size of the allocated buffer or 0 in case of failure.
925 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
926 struct batadv_tvlv_tt_data **tt_data,
927 struct batadv_tvlv_tt_change **tt_change,
930 struct batadv_tvlv_tt_vlan_data *tt_vlan;
931 struct batadv_softif_vlan *vlan;
933 u16 vlan_entries = 0;
934 u16 total_entries = 0;
939 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
940 hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
941 vlan_entries = atomic_read(&vlan->tt.num_entries);
942 if (vlan_entries < 1)
946 total_entries += vlan_entries;
949 change_offset = sizeof(**tt_data);
950 change_offset += num_vlan * sizeof(*tt_vlan);
952 /* if tt_len is negative, allocate the space needed by the full table */
954 *tt_len = batadv_tt_len(total_entries);
957 tvlv_len += change_offset;
959 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
965 (*tt_data)->flags = BATADV_NO_FLAGS;
966 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
967 (*tt_data)->num_vlan = htons(num_vlan);
969 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
970 hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
971 vlan_entries = atomic_read(&vlan->tt.num_entries);
972 if (vlan_entries < 1)
975 tt_vlan->vid = htons(vlan->vid);
976 tt_vlan->crc = htonl(vlan->tt.crc);
977 tt_vlan->reserved = 0;
982 tt_change_ptr = (u8 *)*tt_data + change_offset;
983 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
986 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
991 * batadv_tt_tvlv_container_update() - update the translation table tvlv
992 * container after local tt changes have been committed
993 * @bat_priv: the bat priv with all the soft interface information
995 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
997 struct batadv_tt_change_node *entry, *safe;
998 struct batadv_tvlv_tt_data *tt_data;
999 struct batadv_tvlv_tt_change *tt_change;
1000 int tt_diff_len, tt_change_len = 0;
1001 int tt_diff_entries_num = 0;
1002 int tt_diff_entries_count = 0;
1005 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
1006 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
1008 /* if we have too many changes for one packet don't send any
1009 * and wait for the tt table request which will be fragmented
1011 if (tt_diff_len > bat_priv->soft_iface->mtu)
1014 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1015 &tt_change, &tt_diff_len);
1019 tt_data->flags = BATADV_TT_OGM_DIFF;
1021 if (tt_diff_len == 0)
1022 goto container_register;
1024 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1025 atomic_set(&bat_priv->tt.local_changes, 0);
1027 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1029 if (tt_diff_entries_count < tt_diff_entries_num) {
1030 memcpy(tt_change + tt_diff_entries_count,
1032 sizeof(struct batadv_tvlv_tt_change));
1033 tt_diff_entries_count++;
1035 list_del(&entry->list);
1036 kmem_cache_free(batadv_tt_change_cache, entry);
1038 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1040 /* Keep the buffer for possible tt_request */
1041 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1042 kfree(bat_priv->tt.last_changeset);
1043 bat_priv->tt.last_changeset_len = 0;
1044 bat_priv->tt.last_changeset = NULL;
1045 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1046 /* check whether this new OGM has no changes due to size problems */
1047 if (tt_diff_entries_count > 0) {
1048 /* if kmalloc() fails we will reply with the full table
1049 * instead of providing the diff
1051 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1052 if (bat_priv->tt.last_changeset) {
1053 memcpy(bat_priv->tt.last_changeset,
1054 tt_change, tt_change_len);
1055 bat_priv->tt.last_changeset_len = tt_diff_len;
1058 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1061 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1067 * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1068 * @msg :Netlink message to dump into
1069 * @portid: Port making netlink request
1070 * @cb: Control block containing additional options
1071 * @bat_priv: The bat priv with all the soft interface information
1072 * @common: tt local & tt global common data
1074 * Return: Error code, or 0 on success
1077 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1078 struct netlink_callback *cb,
1079 struct batadv_priv *bat_priv,
1080 struct batadv_tt_common_entry *common)
1083 struct batadv_softif_vlan *vlan;
1084 struct batadv_tt_local_entry *local;
1085 unsigned int last_seen_msecs;
1088 local = container_of(common, struct batadv_tt_local_entry, common);
1089 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1091 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1097 batadv_softif_vlan_put(vlan);
1099 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1100 &batadv_netlink_family, NLM_F_MULTI,
1101 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1105 genl_dump_check_consistent(cb, hdr);
1107 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1108 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1109 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1110 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1111 goto nla_put_failure;
1113 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1114 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1115 goto nla_put_failure;
1117 genlmsg_end(msg, hdr);
1121 genlmsg_cancel(msg, hdr);
1126 * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1127 * @msg: Netlink message to dump into
1128 * @portid: Port making netlink request
1129 * @cb: Control block containing additional options
1130 * @bat_priv: The bat priv with all the soft interface information
1131 * @hash: hash to dump
1132 * @bucket: bucket index to dump
1133 * @idx_s: Number of entries to skip
1135 * Return: Error code, or 0 on success
1138 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1139 struct netlink_callback *cb,
1140 struct batadv_priv *bat_priv,
1141 struct batadv_hashtable *hash, unsigned int bucket,
1144 struct batadv_tt_common_entry *common;
1147 spin_lock_bh(&hash->list_locks[bucket]);
1148 cb->seq = atomic_read(&hash->generation) << 1 | 1;
1150 hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1154 if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1156 spin_unlock_bh(&hash->list_locks[bucket]);
1161 spin_unlock_bh(&hash->list_locks[bucket]);
1168 * batadv_tt_local_dump() - Dump TT local entries into a message
1169 * @msg: Netlink message to dump into
1170 * @cb: Parameters from query
1172 * Return: Error code, or 0 on success
1174 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1176 struct net *net = sock_net(cb->skb->sk);
1177 struct net_device *soft_iface;
1178 struct batadv_priv *bat_priv;
1179 struct batadv_hard_iface *primary_if = NULL;
1180 struct batadv_hashtable *hash;
1183 int bucket = cb->args[0];
1184 int idx = cb->args[1];
1185 int portid = NETLINK_CB(cb->skb).portid;
1187 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1191 soft_iface = dev_get_by_index(net, ifindex);
1192 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1197 bat_priv = netdev_priv(soft_iface);
1199 primary_if = batadv_primary_if_get_selected(bat_priv);
1200 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1205 hash = bat_priv->tt.local_hash;
1207 while (bucket < hash->size) {
1208 if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1209 hash, bucket, &idx))
1219 batadv_hardif_put(primary_if);
1221 dev_put(soft_iface);
1223 cb->args[0] = bucket;
1230 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1231 struct batadv_tt_local_entry *tt_local_entry,
1232 u16 flags, const char *message)
1234 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1236 /* The local client has to be marked as "pending to be removed" but has
1237 * to be kept in the table in order to send it in a full table
1238 * response issued before the net ttvn increment (consistency check)
1240 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1242 batadv_dbg(BATADV_DBG_TT, bat_priv,
1243 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1244 tt_local_entry->common.addr,
1245 batadv_print_vid(tt_local_entry->common.vid), message);
1249 * batadv_tt_local_remove() - logically remove an entry from the local table
1250 * @bat_priv: the bat priv with all the soft interface information
1251 * @addr: the MAC address of the client to remove
1252 * @vid: VLAN identifier
1253 * @message: message to append to the log on deletion
1254 * @roaming: true if the deletion is due to a roaming event
1256 * Return: the flags assigned to the local entry before being deleted
1258 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1259 unsigned short vid, const char *message,
1262 struct batadv_tt_local_entry *tt_removed_entry;
1263 struct batadv_tt_local_entry *tt_local_entry;
1264 u16 flags, curr_flags = BATADV_NO_FLAGS;
1265 struct hlist_node *tt_removed_node;
1267 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1268 if (!tt_local_entry)
1271 curr_flags = tt_local_entry->common.flags;
1273 flags = BATADV_TT_CLIENT_DEL;
1274 /* if this global entry addition is due to a roaming, the node has to
1275 * mark the local entry as "roamed" in order to correctly reroute
1279 flags |= BATADV_TT_CLIENT_ROAM;
1280 /* mark the local client as ROAMed */
1281 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1284 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1285 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1289 /* if this client has been added right now, it is possible to
1290 * immediately purge it
1292 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1294 tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1297 &tt_local_entry->common);
1298 if (!tt_removed_node)
1301 /* drop reference of remove hash entry */
1302 tt_removed_entry = hlist_entry(tt_removed_node,
1303 struct batadv_tt_local_entry,
1305 batadv_tt_local_entry_put(tt_removed_entry);
1309 batadv_tt_local_entry_put(tt_local_entry);
1315 * batadv_tt_local_purge_list() - purge inactive tt local entries
1316 * @bat_priv: the bat priv with all the soft interface information
1317 * @head: pointer to the list containing the local tt entries
1318 * @timeout: parameter deciding whether a given tt local entry is considered
1321 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1322 struct hlist_head *head,
1325 struct batadv_tt_local_entry *tt_local_entry;
1326 struct batadv_tt_common_entry *tt_common_entry;
1327 struct hlist_node *node_tmp;
1329 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1331 tt_local_entry = container_of(tt_common_entry,
1332 struct batadv_tt_local_entry,
1334 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1337 /* entry already marked for deletion */
1338 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1341 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1344 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1345 BATADV_TT_CLIENT_DEL, "timed out");
1350 * batadv_tt_local_purge() - purge inactive tt local entries
1351 * @bat_priv: the bat priv with all the soft interface information
1352 * @timeout: parameter deciding whether a given tt local entry is considered
1355 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1358 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1359 struct hlist_head *head;
1360 spinlock_t *list_lock; /* protects write access to the hash lists */
1363 for (i = 0; i < hash->size; i++) {
1364 head = &hash->table[i];
1365 list_lock = &hash->list_locks[i];
1367 spin_lock_bh(list_lock);
1368 batadv_tt_local_purge_list(bat_priv, head, timeout);
1369 spin_unlock_bh(list_lock);
1373 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1375 struct batadv_hashtable *hash;
1376 spinlock_t *list_lock; /* protects write access to the hash lists */
1377 struct batadv_tt_common_entry *tt_common_entry;
1378 struct batadv_tt_local_entry *tt_local;
1379 struct hlist_node *node_tmp;
1380 struct hlist_head *head;
1383 if (!bat_priv->tt.local_hash)
1386 hash = bat_priv->tt.local_hash;
1388 for (i = 0; i < hash->size; i++) {
1389 head = &hash->table[i];
1390 list_lock = &hash->list_locks[i];
1392 spin_lock_bh(list_lock);
1393 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1395 hlist_del_rcu(&tt_common_entry->hash_entry);
1396 tt_local = container_of(tt_common_entry,
1397 struct batadv_tt_local_entry,
1400 batadv_tt_local_entry_put(tt_local);
1402 spin_unlock_bh(list_lock);
1405 batadv_hash_destroy(hash);
1407 bat_priv->tt.local_hash = NULL;
1410 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1412 if (bat_priv->tt.global_hash)
1415 bat_priv->tt.global_hash = batadv_hash_new(1024);
1417 if (!bat_priv->tt.global_hash)
1420 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1421 &batadv_tt_global_hash_lock_class_key);
1426 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1428 struct batadv_tt_change_node *entry, *safe;
1430 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1432 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1434 list_del(&entry->list);
1435 kmem_cache_free(batadv_tt_change_cache, entry);
1438 atomic_set(&bat_priv->tt.local_changes, 0);
1439 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1443 * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1444 * @entry: the TT global entry where the orig_list_entry has to be
1446 * @orig_node: the originator for which the orig_list_entry has to be found
1448 * retrieve the orig_tt_list_entry belonging to orig_node from the
1449 * batadv_tt_global_entry list
1451 * Return: it with an increased refcounter, NULL if not found
1453 static struct batadv_tt_orig_list_entry *
1454 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1455 const struct batadv_orig_node *orig_node)
1457 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1458 const struct hlist_head *head;
1461 head = &entry->orig_list;
1462 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1463 if (tmp_orig_entry->orig_node != orig_node)
1465 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1468 orig_entry = tmp_orig_entry;
1477 * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1478 * handled by a given originator
1479 * @entry: the TT global entry to check
1480 * @orig_node: the originator to search in the list
1481 * @flags: a pointer to store TT flags for the given @entry received
1484 * find out if an orig_node is already in the list of a tt_global_entry.
1486 * Return: true if found, false otherwise
1489 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1490 const struct batadv_orig_node *orig_node,
1493 struct batadv_tt_orig_list_entry *orig_entry;
1496 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1501 *flags = orig_entry->flags;
1503 batadv_tt_orig_list_entry_put(orig_entry);
1510 * batadv_tt_global_sync_flags() - update TT sync flags
1511 * @tt_global: the TT global entry to update sync flags in
1513 * Updates the sync flag bits in the tt_global flag attribute with a logical
1514 * OR of all sync flags from any of its TT orig entries.
1517 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1519 struct batadv_tt_orig_list_entry *orig_entry;
1520 const struct hlist_head *head;
1521 u16 flags = BATADV_NO_FLAGS;
1524 head = &tt_global->orig_list;
1525 hlist_for_each_entry_rcu(orig_entry, head, list)
1526 flags |= orig_entry->flags;
1529 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1530 tt_global->common.flags = flags;
1534 * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1535 * @tt_global: the TT global entry to add an orig entry in
1536 * @orig_node: the originator to add an orig entry for
1537 * @ttvn: translation table version number of this changeset
1538 * @flags: TT sync flags
1541 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1542 struct batadv_orig_node *orig_node, int ttvn,
1545 struct batadv_tt_orig_list_entry *orig_entry;
1547 spin_lock_bh(&tt_global->list_lock);
1549 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1551 /* refresh the ttvn: the current value could be a bogus one that
1552 * was added during a "temporary client detection"
1554 orig_entry->ttvn = ttvn;
1555 orig_entry->flags = flags;
1559 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1563 INIT_HLIST_NODE(&orig_entry->list);
1564 kref_get(&orig_node->refcount);
1565 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1566 orig_entry->orig_node = orig_node;
1567 orig_entry->ttvn = ttvn;
1568 orig_entry->flags = flags;
1569 kref_init(&orig_entry->refcount);
1571 kref_get(&orig_entry->refcount);
1572 hlist_add_head_rcu(&orig_entry->list,
1573 &tt_global->orig_list);
1574 atomic_inc(&tt_global->orig_list_count);
1577 batadv_tt_global_sync_flags(tt_global);
1580 batadv_tt_orig_list_entry_put(orig_entry);
1582 spin_unlock_bh(&tt_global->list_lock);
1586 * batadv_tt_global_add() - add a new TT global entry or update an existing one
1587 * @bat_priv: the bat priv with all the soft interface information
1588 * @orig_node: the originator announcing the client
1589 * @tt_addr: the mac address of the non-mesh client
1590 * @vid: VLAN identifier
1591 * @flags: TT flags that have to be set for this non-mesh client
1592 * @ttvn: the tt version number ever announcing this non-mesh client
1594 * Add a new TT global entry for the given originator. If the entry already
1595 * exists add a new reference to the given originator (a global entry can have
1596 * references to multiple originators) and adjust the flags attribute to reflect
1597 * the function argument.
1598 * If a TT local entry exists for this non-mesh client remove it.
1600 * The caller must hold the orig_node refcount.
1602 * Return: true if the new entry has been added, false otherwise
1604 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1605 struct batadv_orig_node *orig_node,
1606 const unsigned char *tt_addr,
1607 unsigned short vid, u16 flags, u8 ttvn)
1609 struct batadv_tt_global_entry *tt_global_entry;
1610 struct batadv_tt_local_entry *tt_local_entry;
1613 struct batadv_tt_common_entry *common;
1616 /* ignore global entries from backbone nodes */
1617 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1620 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1621 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1623 /* if the node already has a local client for this entry, it has to wait
1624 * for a roaming advertisement instead of manually messing up the global
1627 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1628 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1631 if (!tt_global_entry) {
1632 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1634 if (!tt_global_entry)
1637 common = &tt_global_entry->common;
1638 ether_addr_copy(common->addr, tt_addr);
1641 if (!is_multicast_ether_addr(common->addr))
1642 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1644 tt_global_entry->roam_at = 0;
1645 /* node must store current time in case of roaming. This is
1646 * needed to purge this entry out on timeout (if nobody claims
1649 if (flags & BATADV_TT_CLIENT_ROAM)
1650 tt_global_entry->roam_at = jiffies;
1651 kref_init(&common->refcount);
1652 common->added_at = jiffies;
1654 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1655 atomic_set(&tt_global_entry->orig_list_count, 0);
1656 spin_lock_init(&tt_global_entry->list_lock);
1658 kref_get(&common->refcount);
1659 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1661 batadv_choose_tt, common,
1662 &common->hash_entry);
1664 if (unlikely(hash_added != 0)) {
1665 /* remove the reference for the hash */
1666 batadv_tt_global_entry_put(tt_global_entry);
1670 common = &tt_global_entry->common;
1671 /* If there is already a global entry, we can use this one for
1673 * But if we are trying to add a temporary client then here are
1674 * two options at this point:
1675 * 1) the global client is not a temporary client: the global
1676 * client has to be left as it is, temporary information
1677 * should never override any already known client state
1678 * 2) the global client is a temporary client: purge the
1679 * originator list and add the new one orig_entry
1681 if (flags & BATADV_TT_CLIENT_TEMP) {
1682 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1684 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1687 batadv_tt_global_del_orig_list(tt_global_entry);
1688 goto add_orig_entry;
1691 /* if the client was temporary added before receiving the first
1692 * OGM announcing it, we have to clear the TEMP flag. Also,
1693 * remove the previous temporary orig node and re-add it
1694 * if required. If the orig entry changed, the new one which
1695 * is a non-temporary entry is preferred.
1697 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1698 batadv_tt_global_del_orig_list(tt_global_entry);
1699 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1702 /* the change can carry possible "attribute" flags like the
1703 * TT_CLIENT_TEMP, therefore they have to be copied in the
1706 if (!is_multicast_ether_addr(common->addr))
1707 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1709 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1710 * one originator left in the list and we previously received a
1711 * delete + roaming change for this originator.
1713 * We should first delete the old originator before adding the
1716 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1717 batadv_tt_global_del_orig_list(tt_global_entry);
1718 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1719 tt_global_entry->roam_at = 0;
1723 /* add the new orig_entry (if needed) or update it */
1724 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1725 flags & BATADV_TT_SYNC_MASK);
1727 batadv_dbg(BATADV_DBG_TT, bat_priv,
1728 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1729 common->addr, batadv_print_vid(common->vid),
1734 /* Do not remove multicast addresses from the local hash on
1737 if (is_multicast_ether_addr(tt_addr))
1740 /* remove address from local hash if present */
1741 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1742 "global tt received",
1743 flags & BATADV_TT_CLIENT_ROAM);
1744 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1746 if (!(flags & BATADV_TT_CLIENT_ROAM))
1747 /* this is a normal global add. Therefore the client is not in a
1748 * roaming state anymore.
1750 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1753 if (tt_global_entry)
1754 batadv_tt_global_entry_put(tt_global_entry);
1756 batadv_tt_local_entry_put(tt_local_entry);
1761 * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1762 * @bat_priv: the bat priv with all the soft interface information
1763 * @tt_global_entry: global translation table entry to be analyzed
1765 * This function assumes the caller holds rcu_read_lock().
1766 * Return: best originator list entry or NULL on errors.
1768 static struct batadv_tt_orig_list_entry *
1769 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1770 struct batadv_tt_global_entry *tt_global_entry)
1772 struct batadv_neigh_node *router, *best_router = NULL;
1773 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1774 struct hlist_head *head;
1775 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1777 head = &tt_global_entry->orig_list;
1778 hlist_for_each_entry_rcu(orig_entry, head, list) {
1779 router = batadv_orig_router_get(orig_entry->orig_node,
1785 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1786 BATADV_IF_DEFAULT) <= 0) {
1787 batadv_neigh_node_put(router);
1791 /* release the refcount for the "old" best */
1793 batadv_neigh_node_put(best_router);
1795 best_entry = orig_entry;
1796 best_router = router;
1800 batadv_neigh_node_put(best_router);
1806 * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
1807 * @msg: Netlink message to dump into
1808 * @portid: Port making netlink request
1809 * @seq: Sequence number of netlink message
1810 * @common: tt local & tt global common data
1811 * @orig: Originator node announcing a non-mesh client
1812 * @best: Is the best originator for the TT entry
1814 * Return: Error code, or 0 on success
1817 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1818 struct batadv_tt_common_entry *common,
1819 struct batadv_tt_orig_list_entry *orig,
1822 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
1824 struct batadv_orig_node_vlan *vlan;
1828 vlan = batadv_orig_node_vlan_get(orig->orig_node,
1835 batadv_orig_node_vlan_put(vlan);
1837 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1839 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
1843 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
1845 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1846 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1847 orig->orig_node->orig) ||
1848 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
1849 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
1850 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1851 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1852 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
1853 goto nla_put_failure;
1855 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1856 goto nla_put_failure;
1858 genlmsg_end(msg, hdr);
1862 genlmsg_cancel(msg, hdr);
1867 * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
1868 * @msg: Netlink message to dump into
1869 * @portid: Port making netlink request
1870 * @seq: Sequence number of netlink message
1871 * @bat_priv: The bat priv with all the soft interface information
1872 * @common: tt local & tt global common data
1873 * @sub_s: Number of entries to skip
1875 * This function assumes the caller holds rcu_read_lock().
1877 * Return: Error code, or 0 on success
1880 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1881 struct batadv_priv *bat_priv,
1882 struct batadv_tt_common_entry *common, int *sub_s)
1884 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1885 struct batadv_tt_global_entry *global;
1886 struct hlist_head *head;
1890 global = container_of(common, struct batadv_tt_global_entry, common);
1891 best_entry = batadv_transtable_best_orig(bat_priv, global);
1892 head = &global->orig_list;
1894 hlist_for_each_entry_rcu(orig_entry, head, list) {
1898 best = (orig_entry == best_entry);
1900 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
1901 orig_entry, best)) {
1912 * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
1913 * @msg: Netlink message to dump into
1914 * @portid: Port making netlink request
1915 * @seq: Sequence number of netlink message
1916 * @bat_priv: The bat priv with all the soft interface information
1917 * @head: Pointer to the list containing the global tt entries
1918 * @idx_s: Number of entries to skip
1919 * @sub: Number of entries to skip
1921 * Return: Error code, or 0 on success
1924 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1925 struct batadv_priv *bat_priv,
1926 struct hlist_head *head, int *idx_s, int *sub)
1928 struct batadv_tt_common_entry *common;
1932 hlist_for_each_entry_rcu(common, head, hash_entry) {
1936 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
1951 * batadv_tt_global_dump() - Dump TT global entries into a message
1952 * @msg: Netlink message to dump into
1953 * @cb: Parameters from query
1955 * Return: Error code, or length of message on success
1957 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
1959 struct net *net = sock_net(cb->skb->sk);
1960 struct net_device *soft_iface;
1961 struct batadv_priv *bat_priv;
1962 struct batadv_hard_iface *primary_if = NULL;
1963 struct batadv_hashtable *hash;
1964 struct hlist_head *head;
1967 int bucket = cb->args[0];
1968 int idx = cb->args[1];
1969 int sub = cb->args[2];
1970 int portid = NETLINK_CB(cb->skb).portid;
1972 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1976 soft_iface = dev_get_by_index(net, ifindex);
1977 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1982 bat_priv = netdev_priv(soft_iface);
1984 primary_if = batadv_primary_if_get_selected(bat_priv);
1985 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1990 hash = bat_priv->tt.global_hash;
1992 while (bucket < hash->size) {
1993 head = &hash->table[bucket];
1995 if (batadv_tt_global_dump_bucket(msg, portid,
1996 cb->nlh->nlmsg_seq, bat_priv,
2007 batadv_hardif_put(primary_if);
2009 dev_put(soft_iface);
2011 cb->args[0] = bucket;
2019 * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
2020 * @tt_global_entry: the global entry to remove the orig_entry from
2021 * @orig_entry: the orig entry to remove and free
2023 * Remove an orig_entry from its list in the given tt_global_entry and
2024 * free this orig_entry afterwards.
2026 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2030 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2031 struct batadv_tt_orig_list_entry *orig_entry)
2033 lockdep_assert_held(&tt_global_entry->list_lock);
2035 batadv_tt_global_size_dec(orig_entry->orig_node,
2036 tt_global_entry->common.vid);
2037 atomic_dec(&tt_global_entry->orig_list_count);
2038 /* requires holding tt_global_entry->list_lock and orig_entry->list
2039 * being part of a list
2041 hlist_del_rcu(&orig_entry->list);
2042 batadv_tt_orig_list_entry_put(orig_entry);
2045 /* deletes the orig list of a tt_global_entry */
2047 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2049 struct hlist_head *head;
2050 struct hlist_node *safe;
2051 struct batadv_tt_orig_list_entry *orig_entry;
2053 spin_lock_bh(&tt_global_entry->list_lock);
2054 head = &tt_global_entry->orig_list;
2055 hlist_for_each_entry_safe(orig_entry, safe, head, list)
2056 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2057 spin_unlock_bh(&tt_global_entry->list_lock);
2061 * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2062 * @bat_priv: the bat priv with all the soft interface information
2063 * @tt_global_entry: the global entry to remove the orig_node from
2064 * @orig_node: the originator announcing the client
2065 * @message: message to append to the log on deletion
2067 * Remove the given orig_node and its according orig_entry from the given
2071 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2072 struct batadv_tt_global_entry *tt_global_entry,
2073 struct batadv_orig_node *orig_node,
2074 const char *message)
2076 struct hlist_head *head;
2077 struct hlist_node *safe;
2078 struct batadv_tt_orig_list_entry *orig_entry;
2081 spin_lock_bh(&tt_global_entry->list_lock);
2082 head = &tt_global_entry->orig_list;
2083 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2084 if (orig_entry->orig_node == orig_node) {
2085 vid = tt_global_entry->common.vid;
2086 batadv_dbg(BATADV_DBG_TT, bat_priv,
2087 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2089 tt_global_entry->common.addr,
2090 batadv_print_vid(vid), message);
2091 _batadv_tt_global_del_orig_entry(tt_global_entry,
2095 spin_unlock_bh(&tt_global_entry->list_lock);
2098 /* If the client is to be deleted, we check if it is the last origantor entry
2099 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2100 * timer, otherwise we simply remove the originator scheduled for deletion.
2103 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2104 struct batadv_tt_global_entry *tt_global_entry,
2105 struct batadv_orig_node *orig_node,
2106 const char *message)
2108 bool last_entry = true;
2109 struct hlist_head *head;
2110 struct batadv_tt_orig_list_entry *orig_entry;
2112 /* no local entry exists, case 1:
2113 * Check if this is the last one or if other entries exist.
2117 head = &tt_global_entry->orig_list;
2118 hlist_for_each_entry_rcu(orig_entry, head, list) {
2119 if (orig_entry->orig_node != orig_node) {
2127 /* its the last one, mark for roaming. */
2128 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2129 tt_global_entry->roam_at = jiffies;
2131 /* there is another entry, we can simply delete this
2132 * one and can still use the other one.
2134 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2135 orig_node, message);
2140 * batadv_tt_global_del() - remove a client from the global table
2141 * @bat_priv: the bat priv with all the soft interface information
2142 * @orig_node: an originator serving this client
2143 * @addr: the mac address of the client
2144 * @vid: VLAN identifier
2145 * @message: a message explaining the reason for deleting the client to print
2146 * for debugging purpose
2147 * @roaming: true if the deletion has been triggered by a roaming event
2149 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2150 struct batadv_orig_node *orig_node,
2151 const unsigned char *addr, unsigned short vid,
2152 const char *message, bool roaming)
2154 struct batadv_tt_global_entry *tt_global_entry;
2155 struct batadv_tt_local_entry *local_entry = NULL;
2157 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2158 if (!tt_global_entry)
2162 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2163 orig_node, message);
2165 if (hlist_empty(&tt_global_entry->orig_list))
2166 batadv_tt_global_free(bat_priv, tt_global_entry,
2172 /* if we are deleting a global entry due to a roam
2173 * event, there are two possibilities:
2174 * 1) the client roamed from node A to node B => if there
2175 * is only one originator left for this client, we mark
2176 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2177 * wait for node B to claim it. In case of timeout
2178 * the entry is purged.
2180 * If there are other originators left, we directly delete
2182 * 2) the client roamed to us => we can directly delete
2183 * the global entry, since it is useless now.
2185 local_entry = batadv_tt_local_hash_find(bat_priv,
2186 tt_global_entry->common.addr,
2189 /* local entry exists, case 2: client roamed to us. */
2190 batadv_tt_global_del_orig_list(tt_global_entry);
2191 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2193 /* no local entry exists, case 1: check for roaming */
2194 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2195 orig_node, message);
2199 if (tt_global_entry)
2200 batadv_tt_global_entry_put(tt_global_entry);
2202 batadv_tt_local_entry_put(local_entry);
2206 * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2207 * the given originator matching the provided vid
2208 * @bat_priv: the bat priv with all the soft interface information
2209 * @orig_node: the originator owning the entries to remove
2210 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2212 * @message: debug message to print as "reason"
2214 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2215 struct batadv_orig_node *orig_node,
2217 const char *message)
2219 struct batadv_tt_global_entry *tt_global;
2220 struct batadv_tt_common_entry *tt_common_entry;
2222 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2223 struct hlist_node *safe;
2224 struct hlist_head *head;
2225 spinlock_t *list_lock; /* protects write access to the hash lists */
2231 for (i = 0; i < hash->size; i++) {
2232 head = &hash->table[i];
2233 list_lock = &hash->list_locks[i];
2235 spin_lock_bh(list_lock);
2236 hlist_for_each_entry_safe(tt_common_entry, safe,
2238 /* remove only matching entries */
2239 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2242 tt_global = container_of(tt_common_entry,
2243 struct batadv_tt_global_entry,
2246 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2247 orig_node, message);
2249 if (hlist_empty(&tt_global->orig_list)) {
2250 vid = tt_global->common.vid;
2251 batadv_dbg(BATADV_DBG_TT, bat_priv,
2252 "Deleting global tt entry %pM (vid: %d): %s\n",
2253 tt_global->common.addr,
2254 batadv_print_vid(vid), message);
2255 hlist_del_rcu(&tt_common_entry->hash_entry);
2256 batadv_tt_global_entry_put(tt_global);
2259 spin_unlock_bh(list_lock);
2261 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2264 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2268 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2269 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2271 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2272 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2274 *msg = "Roaming timeout\n";
2277 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2278 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2280 *msg = "Temporary client timeout\n";
2286 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2288 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2289 struct hlist_head *head;
2290 struct hlist_node *node_tmp;
2291 spinlock_t *list_lock; /* protects write access to the hash lists */
2294 struct batadv_tt_common_entry *tt_common;
2295 struct batadv_tt_global_entry *tt_global;
2297 for (i = 0; i < hash->size; i++) {
2298 head = &hash->table[i];
2299 list_lock = &hash->list_locks[i];
2301 spin_lock_bh(list_lock);
2302 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2304 tt_global = container_of(tt_common,
2305 struct batadv_tt_global_entry,
2308 if (!batadv_tt_global_to_purge(tt_global, &msg))
2311 batadv_dbg(BATADV_DBG_TT, bat_priv,
2312 "Deleting global tt entry %pM (vid: %d): %s\n",
2313 tt_global->common.addr,
2314 batadv_print_vid(tt_global->common.vid),
2317 hlist_del_rcu(&tt_common->hash_entry);
2319 batadv_tt_global_entry_put(tt_global);
2321 spin_unlock_bh(list_lock);
2325 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2327 struct batadv_hashtable *hash;
2328 spinlock_t *list_lock; /* protects write access to the hash lists */
2329 struct batadv_tt_common_entry *tt_common_entry;
2330 struct batadv_tt_global_entry *tt_global;
2331 struct hlist_node *node_tmp;
2332 struct hlist_head *head;
2335 if (!bat_priv->tt.global_hash)
2338 hash = bat_priv->tt.global_hash;
2340 for (i = 0; i < hash->size; i++) {
2341 head = &hash->table[i];
2342 list_lock = &hash->list_locks[i];
2344 spin_lock_bh(list_lock);
2345 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2347 hlist_del_rcu(&tt_common_entry->hash_entry);
2348 tt_global = container_of(tt_common_entry,
2349 struct batadv_tt_global_entry,
2351 batadv_tt_global_entry_put(tt_global);
2353 spin_unlock_bh(list_lock);
2356 batadv_hash_destroy(hash);
2358 bat_priv->tt.global_hash = NULL;
2362 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2363 struct batadv_tt_global_entry *tt_global_entry)
2365 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2366 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2369 /* check if the two clients are marked as isolated */
2370 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2371 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2378 * batadv_transtable_search() - get the mesh destination for a given client
2379 * @bat_priv: the bat priv with all the soft interface information
2380 * @src: mac address of the source client
2381 * @addr: mac address of the destination client
2382 * @vid: VLAN identifier
2384 * Return: a pointer to the originator that was selected as destination in the
2385 * mesh for contacting the client 'addr', NULL otherwise.
2386 * In case of multiple originators serving the same client, the function returns
2387 * the best one (best in terms of metric towards the destination node).
2389 * If the two clients are AP isolated the function returns NULL.
2391 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2396 struct batadv_tt_local_entry *tt_local_entry = NULL;
2397 struct batadv_tt_global_entry *tt_global_entry = NULL;
2398 struct batadv_orig_node *orig_node = NULL;
2399 struct batadv_tt_orig_list_entry *best_entry;
2401 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2402 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2403 if (!tt_local_entry ||
2404 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2408 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2409 if (!tt_global_entry)
2412 /* check whether the clients should not communicate due to AP
2415 if (tt_local_entry &&
2416 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2420 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2421 /* found anything? */
2423 orig_node = best_entry->orig_node;
2424 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2429 if (tt_global_entry)
2430 batadv_tt_global_entry_put(tt_global_entry);
2432 batadv_tt_local_entry_put(tt_local_entry);
2438 * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2439 * to the given orig_node
2440 * @bat_priv: the bat priv with all the soft interface information
2441 * @orig_node: originator for which the CRC should be computed
2442 * @vid: VLAN identifier for which the CRC32 has to be computed
2444 * This function computes the checksum for the global table corresponding to a
2445 * specific originator. In particular, the checksum is computed as follows: For
2446 * each client connected to the originator the CRC32C of the MAC address and the
2447 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2450 * The idea behind is that CRC32C should be used as much as possible in order to
2451 * produce a unique hash of the table, but since the order which is used to feed
2452 * the CRC32C function affects the result and since every node in the network
2453 * probably sorts the clients differently, the hash function cannot be directly
2454 * computed over the entire table. Hence the CRC32C is used only on
2455 * the single client entry, while all the results are then xor'ed together
2456 * because the XOR operation can combine them all while trying to reduce the
2457 * noise as much as possible.
2459 * Return: the checksum of the global table of a given originator.
2461 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2462 struct batadv_orig_node *orig_node,
2465 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2466 struct batadv_tt_orig_list_entry *tt_orig;
2467 struct batadv_tt_common_entry *tt_common;
2468 struct batadv_tt_global_entry *tt_global;
2469 struct hlist_head *head;
2470 u32 i, crc_tmp, crc = 0;
2474 for (i = 0; i < hash->size; i++) {
2475 head = &hash->table[i];
2478 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2479 tt_global = container_of(tt_common,
2480 struct batadv_tt_global_entry,
2482 /* compute the CRC only for entries belonging to the
2483 * VLAN identified by the vid passed as parameter
2485 if (tt_common->vid != vid)
2488 /* Roaming clients are in the global table for
2489 * consistency only. They don't have to be
2490 * taken into account while computing the
2493 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2495 /* Temporary clients have not been announced yet, so
2496 * they have to be skipped while computing the global
2499 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2502 /* find out if this global entry is announced by this
2505 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2510 /* use network order to read the VID: this ensures that
2511 * every node reads the bytes in the same order.
2513 tmp_vid = htons(tt_common->vid);
2514 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2516 /* compute the CRC on flags that have to be kept in sync
2519 flags = tt_orig->flags;
2520 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2522 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2524 batadv_tt_orig_list_entry_put(tt_orig);
2533 * batadv_tt_local_crc() - calculates the checksum of the local table
2534 * @bat_priv: the bat priv with all the soft interface information
2535 * @vid: VLAN identifier for which the CRC32 has to be computed
2537 * For details about the computation, please refer to the documentation for
2538 * batadv_tt_global_crc().
2540 * Return: the checksum of the local table
2542 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2545 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2546 struct batadv_tt_common_entry *tt_common;
2547 struct hlist_head *head;
2548 u32 i, crc_tmp, crc = 0;
2552 for (i = 0; i < hash->size; i++) {
2553 head = &hash->table[i];
2556 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2557 /* compute the CRC only for entries belonging to the
2558 * VLAN identified by vid
2560 if (tt_common->vid != vid)
2563 /* not yet committed clients have not to be taken into
2564 * account while computing the CRC
2566 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2569 /* use network order to read the VID: this ensures that
2570 * every node reads the bytes in the same order.
2572 tmp_vid = htons(tt_common->vid);
2573 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2575 /* compute the CRC on flags that have to be kept in sync
2578 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2579 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2581 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2590 * batadv_tt_req_node_release() - free tt_req node entry
2591 * @ref: kref pointer of the tt req_node entry
2593 static void batadv_tt_req_node_release(struct kref *ref)
2595 struct batadv_tt_req_node *tt_req_node;
2597 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2599 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2603 * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2604 * possibly release it
2605 * @tt_req_node: tt_req_node to be free'd
2607 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2609 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2612 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2614 struct batadv_tt_req_node *node;
2615 struct hlist_node *safe;
2617 spin_lock_bh(&bat_priv->tt.req_list_lock);
2619 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2620 hlist_del_init(&node->list);
2621 batadv_tt_req_node_put(node);
2624 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2627 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2628 struct batadv_orig_node *orig_node,
2629 const void *tt_buff,
2632 /* Replace the old buffer only if I received something in the
2633 * last OGM (the OGM could carry no changes)
2635 spin_lock_bh(&orig_node->tt_buff_lock);
2636 if (tt_buff_len > 0) {
2637 kfree(orig_node->tt_buff);
2638 orig_node->tt_buff_len = 0;
2639 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2640 if (orig_node->tt_buff) {
2641 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2642 orig_node->tt_buff_len = tt_buff_len;
2645 spin_unlock_bh(&orig_node->tt_buff_lock);
2648 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2650 struct batadv_tt_req_node *node;
2651 struct hlist_node *safe;
2653 spin_lock_bh(&bat_priv->tt.req_list_lock);
2654 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2655 if (batadv_has_timed_out(node->issued_at,
2656 BATADV_TT_REQUEST_TIMEOUT)) {
2657 hlist_del_init(&node->list);
2658 batadv_tt_req_node_put(node);
2661 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2665 * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2666 * @bat_priv: the bat priv with all the soft interface information
2667 * @orig_node: orig node this request is being issued for
2669 * Return: the pointer to the new tt_req_node struct if no request
2670 * has already been issued for this orig_node, NULL otherwise.
2672 static struct batadv_tt_req_node *
2673 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2674 struct batadv_orig_node *orig_node)
2676 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2678 spin_lock_bh(&bat_priv->tt.req_list_lock);
2679 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2680 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2681 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2682 BATADV_TT_REQUEST_TIMEOUT))
2686 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2690 kref_init(&tt_req_node->refcount);
2691 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2692 tt_req_node->issued_at = jiffies;
2694 kref_get(&tt_req_node->refcount);
2695 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2697 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2702 * batadv_tt_local_valid() - verify local tt entry and get flags
2703 * @entry_ptr: to be checked local tt entry
2704 * @data_ptr: not used but definition required to satisfy the callback prototype
2705 * @flags: a pointer to store TT flags for this client to
2707 * Checks the validity of the given local TT entry. If it is, then the provided
2708 * flags pointer is updated.
2710 * Return: true if the entry is a valid, false otherwise.
2712 static bool batadv_tt_local_valid(const void *entry_ptr,
2713 const void *data_ptr,
2716 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2718 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2722 *flags = tt_common_entry->flags;
2728 * batadv_tt_global_valid() - verify global tt entry and get flags
2729 * @entry_ptr: to be checked global tt entry
2730 * @data_ptr: an orig_node object (may be NULL)
2731 * @flags: a pointer to store TT flags for this client to
2733 * Checks the validity of the given global TT entry. If it is, then the provided
2734 * flags pointer is updated either with the common (summed) TT flags if data_ptr
2735 * is NULL or the specific, per originator TT flags otherwise.
2737 * Return: true if the entry is a valid, false otherwise.
2739 static bool batadv_tt_global_valid(const void *entry_ptr,
2740 const void *data_ptr,
2743 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2744 const struct batadv_tt_global_entry *tt_global_entry;
2745 const struct batadv_orig_node *orig_node = data_ptr;
2747 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2748 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2751 tt_global_entry = container_of(tt_common_entry,
2752 struct batadv_tt_global_entry,
2755 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2760 * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2762 * @bat_priv: the bat priv with all the soft interface information
2763 * @hash: hash table containing the tt entries
2764 * @tt_len: expected tvlv tt data buffer length in number of bytes
2765 * @tvlv_buff: pointer to the buffer to fill with the TT data
2766 * @valid_cb: function to filter tt change entries and to return TT flags
2767 * @cb_data: data passed to the filter function as argument
2769 * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2770 * is not provided then this becomes a no-op.
2772 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2773 struct batadv_hashtable *hash,
2774 void *tvlv_buff, u16 tt_len,
2775 bool (*valid_cb)(const void *,
2780 struct batadv_tt_common_entry *tt_common_entry;
2781 struct batadv_tvlv_tt_change *tt_change;
2782 struct hlist_head *head;
2783 u16 tt_tot, tt_num_entries = 0;
2788 tt_tot = batadv_tt_entries(tt_len);
2789 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2795 for (i = 0; i < hash->size; i++) {
2796 head = &hash->table[i];
2798 hlist_for_each_entry_rcu(tt_common_entry,
2800 if (tt_tot == tt_num_entries)
2803 ret = valid_cb(tt_common_entry, cb_data, &flags);
2807 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2808 tt_change->flags = flags;
2809 tt_change->vid = htons(tt_common_entry->vid);
2810 memset(tt_change->reserved, 0,
2811 sizeof(tt_change->reserved));
2821 * batadv_tt_global_check_crc() - check if all the CRCs are correct
2822 * @orig_node: originator for which the CRCs have to be checked
2823 * @tt_vlan: pointer to the first tvlv VLAN entry
2824 * @num_vlan: number of tvlv VLAN entries
2826 * Return: true if all the received CRCs match the locally stored ones, false
2829 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2830 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2833 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2834 struct batadv_orig_node_vlan *vlan;
2835 int i, orig_num_vlan;
2838 /* check if each received CRC matches the locally stored one */
2839 for (i = 0; i < num_vlan; i++) {
2840 tt_vlan_tmp = tt_vlan + i;
2842 /* if orig_node is a backbone node for this VLAN, don't check
2843 * the CRC as we ignore all the global entries over it
2845 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2847 ntohs(tt_vlan_tmp->vid)))
2850 vlan = batadv_orig_node_vlan_get(orig_node,
2851 ntohs(tt_vlan_tmp->vid));
2856 batadv_orig_node_vlan_put(vlan);
2858 if (crc != ntohl(tt_vlan_tmp->crc))
2862 /* check if any excess VLANs exist locally for the originator
2863 * which are not mentioned in the TVLV from the originator.
2867 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2871 if (orig_num_vlan > num_vlan)
2878 * batadv_tt_local_update_crc() - update all the local CRCs
2879 * @bat_priv: the bat priv with all the soft interface information
2881 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2883 struct batadv_softif_vlan *vlan;
2885 /* recompute the global CRC for each VLAN */
2887 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2888 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2894 * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
2895 * @bat_priv: the bat priv with all the soft interface information
2896 * @orig_node: the orig_node for which the CRCs have to be updated
2898 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2899 struct batadv_orig_node *orig_node)
2901 struct batadv_orig_node_vlan *vlan;
2904 /* recompute the global CRC for each VLAN */
2906 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2907 /* if orig_node is a backbone node for this VLAN, don't compute
2908 * the CRC as we ignore all the global entries over it
2910 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2914 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2921 * batadv_send_tt_request() - send a TT Request message to a given node
2922 * @bat_priv: the bat priv with all the soft interface information
2923 * @dst_orig_node: the destination of the message
2924 * @ttvn: the version number that the source of the message is looking for
2925 * @tt_vlan: pointer to the first tvlv VLAN object to request
2926 * @num_vlan: number of tvlv VLAN entries
2927 * @full_table: ask for the entire translation table if true, while only for the
2928 * last TT diff otherwise
2930 * Return: true if the TT Request was sent, false otherwise
2932 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2933 struct batadv_orig_node *dst_orig_node,
2935 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2936 u16 num_vlan, bool full_table)
2938 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2939 struct batadv_tt_req_node *tt_req_node = NULL;
2940 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2941 struct batadv_hard_iface *primary_if;
2945 primary_if = batadv_primary_if_get_selected(bat_priv);
2949 /* The new tt_req will be issued only if I'm not waiting for a
2950 * reply from the same orig_node yet
2952 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2956 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2957 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2961 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2962 tvlv_tt_data->ttvn = ttvn;
2963 tvlv_tt_data->num_vlan = htons(num_vlan);
2965 /* send all the CRCs within the request. This is needed by intermediate
2966 * nodes to ensure they have the correct table before replying
2968 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2969 for (i = 0; i < num_vlan; i++) {
2970 tt_vlan_req->vid = tt_vlan->vid;
2971 tt_vlan_req->crc = tt_vlan->crc;
2978 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2980 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2981 dst_orig_node->orig, full_table ? 'F' : '.');
2983 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2984 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2985 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2986 tvlv_tt_data, size);
2991 batadv_hardif_put(primary_if);
2993 if (ret && tt_req_node) {
2994 spin_lock_bh(&bat_priv->tt.req_list_lock);
2995 if (!hlist_unhashed(&tt_req_node->list)) {
2996 hlist_del_init(&tt_req_node->list);
2997 batadv_tt_req_node_put(tt_req_node);
2999 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3003 batadv_tt_req_node_put(tt_req_node);
3005 kfree(tvlv_tt_data);
3010 * batadv_send_other_tt_response() - send reply to tt request concerning another
3011 * node's translation table
3012 * @bat_priv: the bat priv with all the soft interface information
3013 * @tt_data: tt data containing the tt request information
3014 * @req_src: mac address of tt request sender
3015 * @req_dst: mac address of tt request recipient
3017 * Return: true if tt request reply was sent, false otherwise.
3019 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3020 struct batadv_tvlv_tt_data *tt_data,
3021 u8 *req_src, u8 *req_dst)
3023 struct batadv_orig_node *req_dst_orig_node;
3024 struct batadv_orig_node *res_dst_orig_node = NULL;
3025 struct batadv_tvlv_tt_change *tt_change;
3026 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3027 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3028 bool ret = false, full_table;
3029 u8 orig_ttvn, req_ttvn;
3033 batadv_dbg(BATADV_DBG_TT, bat_priv,
3034 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3035 req_src, tt_data->ttvn, req_dst,
3036 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3038 /* Let's get the orig node of the REAL destination */
3039 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3040 if (!req_dst_orig_node)
3043 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3044 if (!res_dst_orig_node)
3047 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3048 req_ttvn = tt_data->ttvn;
3050 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3051 /* this node doesn't have the requested data */
3052 if (orig_ttvn != req_ttvn ||
3053 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3054 ntohs(tt_data->num_vlan)))
3057 /* If the full table has been explicitly requested */
3058 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3059 !req_dst_orig_node->tt_buff)
3064 /* TT fragmentation hasn't been implemented yet, so send as many
3065 * TT entries fit a single packet as possible only
3068 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3069 tt_len = req_dst_orig_node->tt_buff_len;
3071 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3078 /* Copy the last orig_node's OGM buffer */
3079 memcpy(tt_change, req_dst_orig_node->tt_buff,
3080 req_dst_orig_node->tt_buff_len);
3081 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3083 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3084 * in the initial part
3087 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3094 /* fill the rest of the tvlv with the real TT entries */
3095 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3097 batadv_tt_global_valid,
3101 /* Don't send the response, if larger than fragmented packet. */
3102 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3103 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3104 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3105 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3106 res_dst_orig_node->orig);
3110 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3111 tvlv_tt_data->ttvn = req_ttvn;
3114 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3116 batadv_dbg(BATADV_DBG_TT, bat_priv,
3117 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3118 res_dst_orig_node->orig, req_dst_orig_node->orig,
3119 full_table ? 'F' : '.', req_ttvn);
3121 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3123 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3124 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3131 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3134 if (res_dst_orig_node)
3135 batadv_orig_node_put(res_dst_orig_node);
3136 if (req_dst_orig_node)
3137 batadv_orig_node_put(req_dst_orig_node);
3138 kfree(tvlv_tt_data);
3143 * batadv_send_my_tt_response() - send reply to tt request concerning this
3144 * node's translation table
3145 * @bat_priv: the bat priv with all the soft interface information
3146 * @tt_data: tt data containing the tt request information
3147 * @req_src: mac address of tt request sender
3149 * Return: true if tt request reply was sent, false otherwise.
3151 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3152 struct batadv_tvlv_tt_data *tt_data,
3155 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3156 struct batadv_hard_iface *primary_if = NULL;
3157 struct batadv_tvlv_tt_change *tt_change;
3158 struct batadv_orig_node *orig_node;
3159 u8 my_ttvn, req_ttvn;
3164 batadv_dbg(BATADV_DBG_TT, bat_priv,
3165 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3166 req_src, tt_data->ttvn,
3167 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3169 spin_lock_bh(&bat_priv->tt.commit_lock);
3171 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3172 req_ttvn = tt_data->ttvn;
3174 orig_node = batadv_orig_hash_find(bat_priv, req_src);
3178 primary_if = batadv_primary_if_get_selected(bat_priv);
3182 /* If the full table has been explicitly requested or the gap
3183 * is too big send the whole local translation table
3185 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3186 !bat_priv->tt.last_changeset)
3191 /* TT fragmentation hasn't been implemented yet, so send as many
3192 * TT entries fit a single packet as possible only
3195 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3197 tt_len = bat_priv->tt.last_changeset_len;
3198 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3202 if (!tt_len || !tvlv_len)
3205 /* Copy the last orig_node's OGM buffer */
3206 memcpy(tt_change, bat_priv->tt.last_changeset,
3207 bat_priv->tt.last_changeset_len);
3208 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3210 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3212 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3213 * in the initial part
3216 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3220 if (!tt_len || !tvlv_len)
3223 /* fill the rest of the tvlv with the real TT entries */
3224 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3226 batadv_tt_local_valid, NULL);
3229 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3230 tvlv_tt_data->ttvn = req_ttvn;
3233 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3235 batadv_dbg(BATADV_DBG_TT, bat_priv,
3236 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3237 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3239 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3241 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3242 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3248 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3250 spin_unlock_bh(&bat_priv->tt.commit_lock);
3252 batadv_orig_node_put(orig_node);
3254 batadv_hardif_put(primary_if);
3255 kfree(tvlv_tt_data);
3256 /* The packet was for this host, so it doesn't need to be re-routed */
3261 * batadv_send_tt_response() - send reply to tt request
3262 * @bat_priv: the bat priv with all the soft interface information
3263 * @tt_data: tt data containing the tt request information
3264 * @req_src: mac address of tt request sender
3265 * @req_dst: mac address of tt request recipient
3267 * Return: true if tt request reply was sent, false otherwise.
3269 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3270 struct batadv_tvlv_tt_data *tt_data,
3271 u8 *req_src, u8 *req_dst)
3273 if (batadv_is_my_mac(bat_priv, req_dst))
3274 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3275 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3279 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3280 struct batadv_orig_node *orig_node,
3281 struct batadv_tvlv_tt_change *tt_change,
3282 u16 tt_num_changes, u8 ttvn)
3287 for (i = 0; i < tt_num_changes; i++) {
3288 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3289 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3290 batadv_tt_global_del(bat_priv, orig_node,
3291 (tt_change + i)->addr,
3292 ntohs((tt_change + i)->vid),
3293 "tt removed by changes",
3296 if (!batadv_tt_global_add(bat_priv, orig_node,
3297 (tt_change + i)->addr,
3298 ntohs((tt_change + i)->vid),
3299 (tt_change + i)->flags, ttvn))
3300 /* In case of problem while storing a
3301 * global_entry, we stop the updating
3302 * procedure without committing the
3303 * ttvn change. This will avoid to send
3304 * corrupted data on tt_request
3309 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3312 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3313 struct batadv_tvlv_tt_change *tt_change,
3314 u8 ttvn, u8 *resp_src,
3317 struct batadv_orig_node *orig_node;
3319 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3323 /* Purge the old table first.. */
3324 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3325 "Received full table");
3327 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3330 spin_lock_bh(&orig_node->tt_buff_lock);
3331 kfree(orig_node->tt_buff);
3332 orig_node->tt_buff_len = 0;
3333 orig_node->tt_buff = NULL;
3334 spin_unlock_bh(&orig_node->tt_buff_lock);
3336 atomic_set(&orig_node->last_ttvn, ttvn);
3340 batadv_orig_node_put(orig_node);
3343 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3344 struct batadv_orig_node *orig_node,
3345 u16 tt_num_changes, u8 ttvn,
3346 struct batadv_tvlv_tt_change *tt_change)
3348 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3349 tt_num_changes, ttvn);
3351 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3352 batadv_tt_len(tt_num_changes));
3353 atomic_set(&orig_node->last_ttvn, ttvn);
3357 * batadv_is_my_client() - check if a client is served by the local node
3358 * @bat_priv: the bat priv with all the soft interface information
3359 * @addr: the mac address of the client to check
3360 * @vid: VLAN identifier
3362 * Return: true if the client is served by this node, false otherwise.
3364 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3367 struct batadv_tt_local_entry *tt_local_entry;
3370 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3371 if (!tt_local_entry)
3373 /* Check if the client has been logically deleted (but is kept for
3374 * consistency purpose)
3376 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3377 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3382 batadv_tt_local_entry_put(tt_local_entry);
3387 * batadv_handle_tt_response() - process incoming tt reply
3388 * @bat_priv: the bat priv with all the soft interface information
3389 * @tt_data: tt data containing the tt request information
3390 * @resp_src: mac address of tt reply sender
3391 * @num_entries: number of tt change entries appended to the tt data
3393 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3394 struct batadv_tvlv_tt_data *tt_data,
3395 u8 *resp_src, u16 num_entries)
3397 struct batadv_tt_req_node *node;
3398 struct hlist_node *safe;
3399 struct batadv_orig_node *orig_node = NULL;
3400 struct batadv_tvlv_tt_change *tt_change;
3401 u8 *tvlv_ptr = (u8 *)tt_data;
3404 batadv_dbg(BATADV_DBG_TT, bat_priv,
3405 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3406 resp_src, tt_data->ttvn, num_entries,
3407 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3409 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3413 spin_lock_bh(&orig_node->tt_lock);
3415 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3416 change_offset *= ntohs(tt_data->num_vlan);
3417 change_offset += sizeof(*tt_data);
3418 tvlv_ptr += change_offset;
3420 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3421 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3422 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3423 resp_src, num_entries);
3425 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3426 tt_data->ttvn, tt_change);
3429 /* Recalculate the CRC for this orig_node and store it */
3430 batadv_tt_global_update_crc(bat_priv, orig_node);
3432 spin_unlock_bh(&orig_node->tt_lock);
3434 /* Delete the tt_req_node from pending tt_requests list */
3435 spin_lock_bh(&bat_priv->tt.req_list_lock);
3436 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3437 if (!batadv_compare_eth(node->addr, resp_src))
3439 hlist_del_init(&node->list);
3440 batadv_tt_req_node_put(node);
3443 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3446 batadv_orig_node_put(orig_node);
3449 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3451 struct batadv_tt_roam_node *node, *safe;
3453 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3455 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3456 list_del(&node->list);
3457 kmem_cache_free(batadv_tt_roam_cache, node);
3460 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3463 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3465 struct batadv_tt_roam_node *node, *safe;
3467 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3468 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3469 if (!batadv_has_timed_out(node->first_time,
3470 BATADV_ROAMING_MAX_TIME))
3473 list_del(&node->list);
3474 kmem_cache_free(batadv_tt_roam_cache, node);
3476 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3480 * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3481 * @bat_priv: the bat priv with all the soft interface information
3482 * @client: mac address of the roaming client
3484 * This function checks whether the client already reached the
3485 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3488 * Return: true if the ROAMING_ADV can be sent, false otherwise
3490 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3492 struct batadv_tt_roam_node *tt_roam_node;
3495 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3496 /* The new tt_req will be issued only if I'm not waiting for a
3497 * reply from the same orig_node yet
3499 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3500 if (!batadv_compare_eth(tt_roam_node->addr, client))
3503 if (batadv_has_timed_out(tt_roam_node->first_time,
3504 BATADV_ROAMING_MAX_TIME))
3507 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3508 /* Sorry, you roamed too many times! */
3515 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3520 tt_roam_node->first_time = jiffies;
3521 atomic_set(&tt_roam_node->counter,
3522 BATADV_ROAMING_MAX_COUNT - 1);
3523 ether_addr_copy(tt_roam_node->addr, client);
3525 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3530 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3535 * batadv_send_roam_adv() - send a roaming advertisement message
3536 * @bat_priv: the bat priv with all the soft interface information
3537 * @client: mac address of the roaming client
3538 * @vid: VLAN identifier
3539 * @orig_node: message destination
3541 * Send a ROAMING_ADV message to the node which was previously serving this
3542 * client. This is done to inform the node that from now on all traffic destined
3543 * for this particular roamed client has to be forwarded to the sender of the
3546 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3548 struct batadv_orig_node *orig_node)
3550 struct batadv_hard_iface *primary_if;
3551 struct batadv_tvlv_roam_adv tvlv_roam;
3553 primary_if = batadv_primary_if_get_selected(bat_priv);
3557 /* before going on we have to check whether the client has
3558 * already roamed to us too many times
3560 if (!batadv_tt_check_roam_count(bat_priv, client))
3563 batadv_dbg(BATADV_DBG_TT, bat_priv,
3564 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3565 orig_node->orig, client, batadv_print_vid(vid));
3567 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3569 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3570 tvlv_roam.vid = htons(vid);
3572 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3573 orig_node->orig, BATADV_TVLV_ROAM, 1,
3574 &tvlv_roam, sizeof(tvlv_roam));
3578 batadv_hardif_put(primary_if);
3581 static void batadv_tt_purge(struct work_struct *work)
3583 struct delayed_work *delayed_work;
3584 struct batadv_priv_tt *priv_tt;
3585 struct batadv_priv *bat_priv;
3587 delayed_work = to_delayed_work(work);
3588 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3589 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3591 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3592 batadv_tt_global_purge(bat_priv);
3593 batadv_tt_req_purge(bat_priv);
3594 batadv_tt_roam_purge(bat_priv);
3596 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3597 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3601 * batadv_tt_free() - Free translation table of soft interface
3602 * @bat_priv: the bat priv with all the soft interface information
3604 void batadv_tt_free(struct batadv_priv *bat_priv)
3606 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3608 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3609 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3611 cancel_delayed_work_sync(&bat_priv->tt.work);
3613 batadv_tt_local_table_free(bat_priv);
3614 batadv_tt_global_table_free(bat_priv);
3615 batadv_tt_req_list_free(bat_priv);
3616 batadv_tt_changes_list_free(bat_priv);
3617 batadv_tt_roam_list_free(bat_priv);
3619 kfree(bat_priv->tt.last_changeset);
3623 * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3624 * table and possibly count them in the TT size
3625 * @bat_priv: the bat priv with all the soft interface information
3626 * @flags: the flag to switch
3627 * @enable: whether to set or unset the flag
3628 * @count: whether to increase the TT size by the number of changed entries
3630 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3631 bool enable, bool count)
3633 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3634 struct batadv_tt_common_entry *tt_common_entry;
3635 struct hlist_head *head;
3641 for (i = 0; i < hash->size; i++) {
3642 head = &hash->table[i];
3645 hlist_for_each_entry_rcu(tt_common_entry,
3648 if ((tt_common_entry->flags & flags) == flags)
3650 tt_common_entry->flags |= flags;
3652 if (!(tt_common_entry->flags & flags))
3654 tt_common_entry->flags &= ~flags;
3660 batadv_tt_local_size_inc(bat_priv,
3661 tt_common_entry->vid);
3667 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3668 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3670 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3671 struct batadv_tt_common_entry *tt_common;
3672 struct batadv_tt_local_entry *tt_local;
3673 struct hlist_node *node_tmp;
3674 struct hlist_head *head;
3675 spinlock_t *list_lock; /* protects write access to the hash lists */
3681 for (i = 0; i < hash->size; i++) {
3682 head = &hash->table[i];
3683 list_lock = &hash->list_locks[i];
3685 spin_lock_bh(list_lock);
3686 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3688 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3691 batadv_dbg(BATADV_DBG_TT, bat_priv,
3692 "Deleting local tt entry (%pM, vid: %d): pending\n",
3694 batadv_print_vid(tt_common->vid));
3696 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3697 hlist_del_rcu(&tt_common->hash_entry);
3698 tt_local = container_of(tt_common,
3699 struct batadv_tt_local_entry,
3702 batadv_tt_local_entry_put(tt_local);
3704 spin_unlock_bh(list_lock);
3709 * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3710 * which have been queued in the time since the last commit
3711 * @bat_priv: the bat priv with all the soft interface information
3713 * Caller must hold tt->commit_lock.
3715 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3717 lockdep_assert_held(&bat_priv->tt.commit_lock);
3719 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3720 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3721 batadv_tt_tvlv_container_update(bat_priv);
3725 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3727 batadv_tt_local_purge_pending_clients(bat_priv);
3728 batadv_tt_local_update_crc(bat_priv);
3730 /* Increment the TTVN only once per OGM interval */
3731 atomic_inc(&bat_priv->tt.vn);
3732 batadv_dbg(BATADV_DBG_TT, bat_priv,
3733 "Local changes committed, updating to ttvn %u\n",
3734 (u8)atomic_read(&bat_priv->tt.vn));
3736 /* reset the sending counter */
3737 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3738 batadv_tt_tvlv_container_update(bat_priv);
3742 * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3743 * have been queued in the time since the last commit
3744 * @bat_priv: the bat priv with all the soft interface information
3746 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3748 spin_lock_bh(&bat_priv->tt.commit_lock);
3749 batadv_tt_local_commit_changes_nolock(bat_priv);
3750 spin_unlock_bh(&bat_priv->tt.commit_lock);
3754 * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3755 * @bat_priv: the bat priv with all the soft interface information
3756 * @src: source mac address of packet
3757 * @dst: destination mac address of packet
3758 * @vid: vlan id of packet
3760 * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3762 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3765 struct batadv_tt_local_entry *tt_local_entry;
3766 struct batadv_tt_global_entry *tt_global_entry;
3767 struct batadv_softif_vlan *vlan;
3770 vlan = batadv_softif_vlan_get(bat_priv, vid);
3774 if (!atomic_read(&vlan->ap_isolation))
3777 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3778 if (!tt_local_entry)
3781 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3782 if (!tt_global_entry)
3783 goto local_entry_put;
3785 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3788 batadv_tt_global_entry_put(tt_global_entry);
3790 batadv_tt_local_entry_put(tt_local_entry);
3792 batadv_softif_vlan_put(vlan);
3797 * batadv_tt_update_orig() - update global translation table with new tt
3798 * information received via ogms
3799 * @bat_priv: the bat priv with all the soft interface information
3800 * @orig_node: the orig_node of the ogm
3801 * @tt_buff: pointer to the first tvlv VLAN entry
3802 * @tt_num_vlan: number of tvlv VLAN entries
3803 * @tt_change: pointer to the first entry in the TT buffer
3804 * @tt_num_changes: number of tt changes inside the tt buffer
3805 * @ttvn: translation table version number of this changeset
3807 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3808 struct batadv_orig_node *orig_node,
3809 const void *tt_buff, u16 tt_num_vlan,
3810 struct batadv_tvlv_tt_change *tt_change,
3811 u16 tt_num_changes, u8 ttvn)
3813 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3814 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3815 bool full_table = true;
3818 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3819 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3820 &orig_node->capa_initialized);
3822 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3823 * increased by one -> we can apply the attached changes
3825 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3826 /* the OGM could not contain the changes due to their size or
3827 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3829 * In this case send a tt request
3831 if (!tt_num_changes) {
3836 spin_lock_bh(&orig_node->tt_lock);
3838 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3841 /* Even if we received the precomputed crc with the OGM, we
3842 * prefer to recompute it to spot any possible inconsistency
3843 * in the global table
3845 batadv_tt_global_update_crc(bat_priv, orig_node);
3847 spin_unlock_bh(&orig_node->tt_lock);
3849 /* The ttvn alone is not enough to guarantee consistency
3850 * because a single value could represent different states
3851 * (due to the wrap around). Thus a node has to check whether
3852 * the resulting table (after applying the changes) is still
3853 * consistent or not. E.g. a node could disconnect while its
3854 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3855 * checking the CRC value is mandatory to detect the
3858 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3862 /* if we missed more than one change or our tables are not
3863 * in sync anymore -> request fresh tt data
3865 if (!has_tt_init || ttvn != orig_ttvn ||
3866 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3869 batadv_dbg(BATADV_DBG_TT, bat_priv,
3870 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3871 orig_node->orig, ttvn, orig_ttvn,
3873 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3874 tt_vlan, tt_num_vlan,
3882 * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
3883 * @bat_priv: the bat priv with all the soft interface information
3884 * @addr: the mac address of the client to check
3885 * @vid: VLAN identifier
3887 * Return: true if we know that the client has moved from its old originator
3888 * to another one. This entry is still kept for consistency purposes and will be
3889 * deleted later by a DEL or because of timeout
3891 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3892 u8 *addr, unsigned short vid)
3894 struct batadv_tt_global_entry *tt_global_entry;
3897 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3898 if (!tt_global_entry)
3901 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3902 batadv_tt_global_entry_put(tt_global_entry);
3908 * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
3909 * @bat_priv: the bat priv with all the soft interface information
3910 * @addr: the mac address of the local client to query
3911 * @vid: VLAN identifier
3913 * Return: true if the local client is known to be roaming (it is not served by
3914 * this node anymore) or not. If yes, the client is still present in the table
3915 * to keep the latter consistent with the node TTVN
3917 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3918 u8 *addr, unsigned short vid)
3920 struct batadv_tt_local_entry *tt_local_entry;
3923 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3924 if (!tt_local_entry)
3927 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3928 batadv_tt_local_entry_put(tt_local_entry);
3934 * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
3935 * @bat_priv: the bat priv with all the soft interface information
3936 * @orig_node: orig node which the temporary entry should be associated with
3937 * @addr: mac address of the client
3938 * @vid: VLAN id of the new temporary global translation table
3940 * Return: true when temporary tt entry could be added, false otherwise
3942 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3943 struct batadv_orig_node *orig_node,
3944 const unsigned char *addr,
3947 /* ignore loop detect macs, they are not supposed to be in the tt local
3950 if (batadv_bla_is_loopdetect_mac(addr))
3953 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3954 BATADV_TT_CLIENT_TEMP,
3955 atomic_read(&orig_node->last_ttvn)))
3958 batadv_dbg(BATADV_DBG_TT, bat_priv,
3959 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3960 addr, batadv_print_vid(vid), orig_node->orig);
3966 * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
3967 * maximum packet size that can be transported through the mesh
3968 * @soft_iface: netdev struct of the mesh interface
3970 * Remove entries older than 'timeout' and half timeout if more entries need
3973 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3975 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3976 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3977 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3978 bool reduced = false;
3980 spin_lock_bh(&bat_priv->tt.commit_lock);
3983 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3984 if (packet_size_max >= table_size)
3987 batadv_tt_local_purge(bat_priv, timeout);
3988 batadv_tt_local_purge_pending_clients(bat_priv);
3992 net_ratelimited_function(batadv_info, soft_iface,
3993 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3997 /* commit these changes immediately, to avoid synchronization problem
4001 batadv_tt_local_commit_changes_nolock(bat_priv);
4003 spin_unlock_bh(&bat_priv->tt.commit_lock);
4007 * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
4008 * @bat_priv: the bat priv with all the soft interface information
4009 * @orig: the orig_node of the ogm
4010 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4011 * @tvlv_value: tvlv buffer containing the gateway data
4012 * @tvlv_value_len: tvlv buffer length
4014 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4015 struct batadv_orig_node *orig,
4016 u8 flags, void *tvlv_value,
4019 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4020 struct batadv_tvlv_tt_change *tt_change;
4021 struct batadv_tvlv_tt_data *tt_data;
4022 u16 num_entries, num_vlan;
4024 if (tvlv_value_len < sizeof(*tt_data))
4027 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4028 tvlv_value_len -= sizeof(*tt_data);
4030 num_vlan = ntohs(tt_data->num_vlan);
4032 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4035 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4036 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4037 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4039 num_entries = batadv_tt_entries(tvlv_value_len);
4041 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4042 num_entries, tt_data->ttvn);
4046 * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
4048 * @bat_priv: the bat priv with all the soft interface information
4049 * @src: mac address of tt tvlv sender
4050 * @dst: mac address of tt tvlv recipient
4051 * @tvlv_value: tvlv buffer containing the tt data
4052 * @tvlv_value_len: tvlv buffer length
4054 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4057 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4062 struct batadv_tvlv_tt_data *tt_data;
4063 u16 tt_vlan_len, tt_num_entries;
4067 if (tvlv_value_len < sizeof(*tt_data))
4068 return NET_RX_SUCCESS;
4070 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4071 tvlv_value_len -= sizeof(*tt_data);
4073 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4074 tt_vlan_len *= ntohs(tt_data->num_vlan);
4076 if (tvlv_value_len < tt_vlan_len)
4077 return NET_RX_SUCCESS;
4079 tvlv_value_len -= tt_vlan_len;
4080 tt_num_entries = batadv_tt_entries(tvlv_value_len);
4082 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4083 case BATADV_TT_REQUEST:
4084 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4086 /* If this node cannot provide a TT response the tt_request is
4089 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4091 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4096 batadv_dbg(BATADV_DBG_TT, bat_priv,
4097 "Routing TT_REQUEST to %pM [%c]\n",
4099 /* tvlv API will re-route the packet */
4103 case BATADV_TT_RESPONSE:
4104 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4106 if (batadv_is_my_mac(bat_priv, dst)) {
4107 batadv_handle_tt_response(bat_priv, tt_data,
4108 src, tt_num_entries);
4109 return NET_RX_SUCCESS;
4112 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4117 batadv_dbg(BATADV_DBG_TT, bat_priv,
4118 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4120 /* tvlv API will re-route the packet */
4124 return NET_RX_SUCCESS;
4128 * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4130 * @bat_priv: the bat priv with all the soft interface information
4131 * @src: mac address of tt tvlv sender
4132 * @dst: mac address of tt tvlv recipient
4133 * @tvlv_value: tvlv buffer containing the tt data
4134 * @tvlv_value_len: tvlv buffer length
4136 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4139 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4144 struct batadv_tvlv_roam_adv *roaming_adv;
4145 struct batadv_orig_node *orig_node = NULL;
4147 /* If this node is not the intended recipient of the
4148 * roaming advertisement the packet is forwarded
4149 * (the tvlv API will re-route the packet).
4151 if (!batadv_is_my_mac(bat_priv, dst))
4154 if (tvlv_value_len < sizeof(*roaming_adv))
4157 orig_node = batadv_orig_hash_find(bat_priv, src);
4161 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4162 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4164 batadv_dbg(BATADV_DBG_TT, bat_priv,
4165 "Received ROAMING_ADV from %pM (client %pM)\n",
4166 src, roaming_adv->client);
4168 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4169 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4170 atomic_read(&orig_node->last_ttvn) + 1);
4174 batadv_orig_node_put(orig_node);
4175 return NET_RX_SUCCESS;
4179 * batadv_tt_init() - initialise the translation table internals
4180 * @bat_priv: the bat priv with all the soft interface information
4182 * Return: 0 on success or negative error number in case of failure.
4184 int batadv_tt_init(struct batadv_priv *bat_priv)
4188 /* synchronized flags must be remote */
4189 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4191 ret = batadv_tt_local_init(bat_priv);
4195 ret = batadv_tt_global_init(bat_priv);
4199 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4200 batadv_tt_tvlv_unicast_handler_v1,
4201 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4203 batadv_tvlv_handler_register(bat_priv, NULL,
4204 batadv_roam_tvlv_unicast_handler_v1,
4205 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4207 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4208 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4209 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4215 * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4216 * @bat_priv: the bat priv with all the soft interface information
4217 * @addr: the mac address of the client
4218 * @vid: the identifier of the VLAN where this client is connected
4220 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4223 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4224 const u8 *addr, unsigned short vid)
4226 struct batadv_tt_global_entry *tt;
4229 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4233 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4235 batadv_tt_global_entry_put(tt);
4241 * batadv_tt_cache_init() - Initialize tt memory object cache
4243 * Return: 0 on success or negative error number in case of failure.
4245 int __init batadv_tt_cache_init(void)
4247 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4248 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4249 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4250 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4251 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4252 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4254 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4255 SLAB_HWCACHE_ALIGN, NULL);
4256 if (!batadv_tl_cache)
4259 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4260 SLAB_HWCACHE_ALIGN, NULL);
4261 if (!batadv_tg_cache)
4262 goto err_tt_tl_destroy;
4264 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4266 SLAB_HWCACHE_ALIGN, NULL);
4267 if (!batadv_tt_orig_cache)
4268 goto err_tt_tg_destroy;
4270 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4272 SLAB_HWCACHE_ALIGN, NULL);
4273 if (!batadv_tt_change_cache)
4274 goto err_tt_orig_destroy;
4276 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4278 SLAB_HWCACHE_ALIGN, NULL);
4279 if (!batadv_tt_req_cache)
4280 goto err_tt_change_destroy;
4282 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4284 SLAB_HWCACHE_ALIGN, NULL);
4285 if (!batadv_tt_roam_cache)
4286 goto err_tt_req_destroy;
4291 kmem_cache_destroy(batadv_tt_req_cache);
4292 batadv_tt_req_cache = NULL;
4293 err_tt_change_destroy:
4294 kmem_cache_destroy(batadv_tt_change_cache);
4295 batadv_tt_change_cache = NULL;
4296 err_tt_orig_destroy:
4297 kmem_cache_destroy(batadv_tt_orig_cache);
4298 batadv_tt_orig_cache = NULL;
4300 kmem_cache_destroy(batadv_tg_cache);
4301 batadv_tg_cache = NULL;
4303 kmem_cache_destroy(batadv_tl_cache);
4304 batadv_tl_cache = NULL;
4310 * batadv_tt_cache_destroy() - Destroy tt memory object cache
4312 void batadv_tt_cache_destroy(void)
4314 kmem_cache_destroy(batadv_tl_cache);
4315 kmem_cache_destroy(batadv_tg_cache);
4316 kmem_cache_destroy(batadv_tt_orig_cache);
4317 kmem_cache_destroy(batadv_tt_change_cache);
4318 kmem_cache_destroy(batadv_tt_req_cache);
4319 kmem_cache_destroy(batadv_tt_roam_cache);