1 /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "translation-table.h"
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/cache.h>
26 #include <linux/compiler.h>
27 #include <linux/crc32c.h>
28 #include <linux/errno.h>
29 #include <linux/etherdevice.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/jhash.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/lockdep.h>
39 #include <linux/netdevice.h>
40 #include <linux/rculist.h>
41 #include <linux/rcupdate.h>
42 #include <linux/seq_file.h>
43 #include <linux/slab.h>
44 #include <linux/spinlock.h>
45 #include <linux/stddef.h>
46 #include <linux/string.h>
47 #include <linux/workqueue.h>
49 #include "bridge_loop_avoidance.h"
50 #include "hard-interface.h"
53 #include "multicast.h"
54 #include "originator.h"
56 #include "soft-interface.h"
59 static struct kmem_cache *batadv_tl_cache __read_mostly;
60 static struct kmem_cache *batadv_tg_cache __read_mostly;
61 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
62 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
63 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
64 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
67 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
68 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
70 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
72 struct batadv_orig_node *orig_node);
73 static void batadv_tt_purge(struct work_struct *work);
75 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
76 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
77 struct batadv_orig_node *orig_node,
78 const unsigned char *addr,
79 unsigned short vid, const char *message,
83 * batadv_compare_tt - check if two TT entries are the same
84 * @node: the list element pointer of the first TT entry
85 * @data2: pointer to the tt_common_entry of the second TT entry
87 * Compare the MAC address and the VLAN ID of the two TT entries and check if
88 * they are the same TT client.
89 * Return: true if the two TT clients are the same, false otherwise
91 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
93 const void *data1 = container_of(node, struct batadv_tt_common_entry,
95 const struct batadv_tt_common_entry *tt1 = data1;
96 const struct batadv_tt_common_entry *tt2 = data2;
98 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
102 * batadv_choose_tt - return the index of the tt entry in the hash table
103 * @data: pointer to the tt_common_entry object to map
104 * @size: the size of the hash table
106 * Return: the hash index where the object represented by 'data' should be
109 static inline u32 batadv_choose_tt(const void *data, u32 size)
111 struct batadv_tt_common_entry *tt;
114 tt = (struct batadv_tt_common_entry *)data;
115 hash = jhash(&tt->addr, ETH_ALEN, hash);
116 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
122 * batadv_tt_hash_find - look for a client in the given hash table
123 * @hash: the hash table to search
124 * @addr: the mac address of the client to look for
125 * @vid: VLAN identifier
127 * Return: a pointer to the tt_common struct belonging to the searched client if
128 * found, NULL otherwise.
130 static struct batadv_tt_common_entry *
131 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
134 struct hlist_head *head;
135 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
141 ether_addr_copy(to_search.addr, addr);
144 index = batadv_choose_tt(&to_search, hash->size);
145 head = &hash->table[index];
148 hlist_for_each_entry_rcu(tt, head, hash_entry) {
149 if (!batadv_compare_eth(tt, addr))
155 if (!kref_get_unless_zero(&tt->refcount))
167 * batadv_tt_local_hash_find - search the local table for a given client
168 * @bat_priv: the bat priv with all the soft interface information
169 * @addr: the mac address of the client to look for
170 * @vid: VLAN identifier
172 * Return: a pointer to the corresponding tt_local_entry struct if the client is
173 * found, NULL otherwise.
175 static struct batadv_tt_local_entry *
176 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
179 struct batadv_tt_common_entry *tt_common_entry;
180 struct batadv_tt_local_entry *tt_local_entry = NULL;
182 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
185 tt_local_entry = container_of(tt_common_entry,
186 struct batadv_tt_local_entry,
188 return tt_local_entry;
192 * batadv_tt_global_hash_find - search the global table for a given client
193 * @bat_priv: the bat priv with all the soft interface information
194 * @addr: the mac address of the client to look for
195 * @vid: VLAN identifier
197 * Return: a pointer to the corresponding tt_global_entry struct if the client
198 * is found, NULL otherwise.
200 static struct batadv_tt_global_entry *
201 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
204 struct batadv_tt_common_entry *tt_common_entry;
205 struct batadv_tt_global_entry *tt_global_entry = NULL;
207 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
210 tt_global_entry = container_of(tt_common_entry,
211 struct batadv_tt_global_entry,
213 return tt_global_entry;
217 * batadv_tt_local_entry_free_rcu - free the tt_local_entry
218 * @rcu: rcu pointer of the tt_local_entry
220 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
222 struct batadv_tt_local_entry *tt_local_entry;
224 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
227 kmem_cache_free(batadv_tl_cache, tt_local_entry);
231 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
232 * for free after rcu grace period
233 * @ref: kref pointer of the nc_node
235 static void batadv_tt_local_entry_release(struct kref *ref)
237 struct batadv_tt_local_entry *tt_local_entry;
239 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
242 batadv_softif_vlan_put(tt_local_entry->vlan);
244 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
248 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
249 * possibly release it
250 * @tt_local_entry: tt_local_entry to be free'd
253 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
255 kref_put(&tt_local_entry->common.refcount,
256 batadv_tt_local_entry_release);
260 * batadv_tt_global_entry_free_rcu - free the tt_global_entry
261 * @rcu: rcu pointer of the tt_global_entry
263 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
265 struct batadv_tt_global_entry *tt_global_entry;
267 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
270 kmem_cache_free(batadv_tg_cache, tt_global_entry);
274 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
275 * for free after rcu grace period
276 * @ref: kref pointer of the nc_node
278 static void batadv_tt_global_entry_release(struct kref *ref)
280 struct batadv_tt_global_entry *tt_global_entry;
282 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
285 batadv_tt_global_del_orig_list(tt_global_entry);
287 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
291 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
292 * possibly release it
293 * @tt_global_entry: tt_global_entry to be free'd
296 batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
298 kref_put(&tt_global_entry->common.refcount,
299 batadv_tt_global_entry_release);
303 * batadv_tt_global_hash_count - count the number of orig entries
304 * @bat_priv: the bat priv with all the soft interface information
305 * @addr: the mac address of the client to count entries for
306 * @vid: VLAN identifier
308 * Return: the number of originators advertising the given address/data
309 * (excluding ourself).
311 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
312 const u8 *addr, unsigned short vid)
314 struct batadv_tt_global_entry *tt_global_entry;
317 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
318 if (!tt_global_entry)
321 count = atomic_read(&tt_global_entry->orig_list_count);
322 batadv_tt_global_entry_put(tt_global_entry);
328 * batadv_tt_local_size_mod - change the size by v of the local table identified
330 * @bat_priv: the bat priv with all the soft interface information
331 * @vid: the VLAN identifier of the sub-table to change
332 * @v: the amount to sum to the local table size
334 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
335 unsigned short vid, int v)
337 struct batadv_softif_vlan *vlan;
339 vlan = batadv_softif_vlan_get(bat_priv, vid);
343 atomic_add(v, &vlan->tt.num_entries);
345 batadv_softif_vlan_put(vlan);
349 * batadv_tt_local_size_inc - increase by one the local table size for the given
351 * @bat_priv: the bat priv with all the soft interface information
352 * @vid: the VLAN identifier
354 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
357 batadv_tt_local_size_mod(bat_priv, vid, 1);
361 * batadv_tt_local_size_dec - decrease by one the local table size for the given
363 * @bat_priv: the bat priv with all the soft interface information
364 * @vid: the VLAN identifier
366 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
369 batadv_tt_local_size_mod(bat_priv, vid, -1);
373 * batadv_tt_global_size_mod - change the size by v of the global table
374 * for orig_node identified by vid
375 * @orig_node: the originator for which the table has to be modified
376 * @vid: the VLAN identifier
377 * @v: the amount to sum to the global table size
379 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
380 unsigned short vid, int v)
382 struct batadv_orig_node_vlan *vlan;
384 vlan = batadv_orig_node_vlan_new(orig_node, vid);
388 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
389 spin_lock_bh(&orig_node->vlan_list_lock);
390 if (!hlist_unhashed(&vlan->list)) {
391 hlist_del_init_rcu(&vlan->list);
392 batadv_orig_node_vlan_put(vlan);
394 spin_unlock_bh(&orig_node->vlan_list_lock);
397 batadv_orig_node_vlan_put(vlan);
401 * batadv_tt_global_size_inc - increase by one the global table size for the
403 * @orig_node: the originator which global table size has to be decreased
404 * @vid: the vlan identifier
406 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
409 batadv_tt_global_size_mod(orig_node, vid, 1);
413 * batadv_tt_global_size_dec - decrease by one the global table size for the
415 * @orig_node: the originator which global table size has to be decreased
416 * @vid: the vlan identifier
418 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
421 batadv_tt_global_size_mod(orig_node, vid, -1);
425 * batadv_tt_orig_list_entry_free_rcu - free the orig_entry
426 * @rcu: rcu pointer of the orig_entry
428 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
430 struct batadv_tt_orig_list_entry *orig_entry;
432 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
434 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
438 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
439 * queue for free after rcu grace period
440 * @ref: kref pointer of the tt orig entry
442 static void batadv_tt_orig_list_entry_release(struct kref *ref)
444 struct batadv_tt_orig_list_entry *orig_entry;
446 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
449 batadv_orig_node_put(orig_entry->orig_node);
450 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
454 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
455 * possibly release it
456 * @orig_entry: tt orig entry to be free'd
459 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
461 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
465 * batadv_tt_local_event - store a local TT event (ADD/DEL)
466 * @bat_priv: the bat priv with all the soft interface information
467 * @tt_local_entry: the TT entry involved in the event
468 * @event_flags: flags to store in the event structure
470 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
471 struct batadv_tt_local_entry *tt_local_entry,
474 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
475 struct batadv_tt_common_entry *common = &tt_local_entry->common;
476 u8 flags = common->flags | event_flags;
477 bool event_removed = false;
478 bool del_op_requested, del_op_entry;
480 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
484 tt_change_node->change.flags = flags;
485 memset(tt_change_node->change.reserved, 0,
486 sizeof(tt_change_node->change.reserved));
487 ether_addr_copy(tt_change_node->change.addr, common->addr);
488 tt_change_node->change.vid = htons(common->vid);
490 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
492 /* check for ADD+DEL or DEL+ADD events */
493 spin_lock_bh(&bat_priv->tt.changes_list_lock);
494 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
496 if (!batadv_compare_eth(entry->change.addr, common->addr))
499 /* DEL+ADD in the same orig interval have no effect and can be
500 * removed to avoid silly behaviour on the receiver side. The
501 * other way around (ADD+DEL) can happen in case of roaming of
502 * a client still in the NEW state. Roaming of NEW clients is
503 * now possible due to automatically recognition of "temporary"
506 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
507 if (!del_op_requested && del_op_entry)
509 if (del_op_requested && !del_op_entry)
512 /* this is a second add in the same originator interval. It
513 * means that flags have been changed: update them!
515 if (!del_op_requested && !del_op_entry)
516 entry->change.flags = flags;
520 list_del(&entry->list);
521 kmem_cache_free(batadv_tt_change_cache, entry);
522 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
523 event_removed = true;
527 /* track the change in the OGMinterval list */
528 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
531 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
534 atomic_dec(&bat_priv->tt.local_changes);
536 atomic_inc(&bat_priv->tt.local_changes);
540 * batadv_tt_len - compute length in bytes of given number of tt changes
541 * @changes_num: number of tt changes
543 * Return: computed length in bytes.
545 static int batadv_tt_len(int changes_num)
547 return changes_num * sizeof(struct batadv_tvlv_tt_change);
551 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
552 * @tt_len: available space
554 * Return: the number of entries.
556 static u16 batadv_tt_entries(u16 tt_len)
558 return tt_len / batadv_tt_len(1);
562 * batadv_tt_local_table_transmit_size - calculates the local translation table
563 * size when transmitted over the air
564 * @bat_priv: the bat priv with all the soft interface information
566 * Return: local translation table size in bytes.
568 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
571 u16 tt_local_entries = 0;
572 struct batadv_softif_vlan *vlan;
576 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
578 tt_local_entries += atomic_read(&vlan->tt.num_entries);
582 /* header size of tvlv encapsulated tt response payload */
583 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
584 hdr_size += sizeof(struct batadv_tvlv_hdr);
585 hdr_size += sizeof(struct batadv_tvlv_tt_data);
586 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
588 return hdr_size + batadv_tt_len(tt_local_entries);
591 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
593 if (bat_priv->tt.local_hash)
596 bat_priv->tt.local_hash = batadv_hash_new(1024);
598 if (!bat_priv->tt.local_hash)
601 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
602 &batadv_tt_local_hash_lock_class_key);
607 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
608 struct batadv_tt_global_entry *tt_global,
611 batadv_dbg(BATADV_DBG_TT, bat_priv,
612 "Deleting global tt entry %pM (vid: %d): %s\n",
613 tt_global->common.addr,
614 BATADV_PRINT_VID(tt_global->common.vid), message);
616 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
617 batadv_choose_tt, &tt_global->common);
618 batadv_tt_global_entry_put(tt_global);
622 * batadv_tt_local_add - add a new client to the local table or update an
624 * @soft_iface: netdev struct of the mesh interface
625 * @addr: the mac address of the client to add
626 * @vid: VLAN identifier
627 * @ifindex: index of the interface where the client is connected to (useful to
628 * identify wireless clients)
629 * @mark: the value contained in the skb->mark field of the received packet (if
632 * Return: true if the client was successfully added, false otherwise.
634 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
635 unsigned short vid, int ifindex, u32 mark)
637 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
638 struct batadv_tt_local_entry *tt_local;
639 struct batadv_tt_global_entry *tt_global = NULL;
640 struct net *net = dev_net(soft_iface);
641 struct batadv_softif_vlan *vlan;
642 struct net_device *in_dev = NULL;
643 struct hlist_head *head;
644 struct batadv_tt_orig_list_entry *orig_entry;
645 int hash_added, table_size, packet_size_max;
647 bool roamed_back = false;
651 if (ifindex != BATADV_NULL_IFINDEX)
652 in_dev = dev_get_by_index(net, ifindex);
654 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
656 if (!is_multicast_ether_addr(addr))
657 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
660 tt_local->last_seen = jiffies;
661 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
662 batadv_dbg(BATADV_DBG_TT, bat_priv,
663 "Re-adding pending client %pM (vid: %d)\n",
664 addr, BATADV_PRINT_VID(vid));
665 /* whatever the reason why the PENDING flag was set,
666 * this is a client which was enqueued to be removed in
667 * this orig_interval. Since it popped up again, the
668 * flag can be reset like it was never enqueued
670 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
674 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
675 batadv_dbg(BATADV_DBG_TT, bat_priv,
676 "Roaming client %pM (vid: %d) came back to its original location\n",
677 addr, BATADV_PRINT_VID(vid));
678 /* the ROAM flag is set because this client roamed away
679 * and the node got a roaming_advertisement message. Now
680 * that the client popped up again at its original
681 * location such flag can be unset
683 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
689 /* Ignore the client if we cannot send it in a full table response. */
690 table_size = batadv_tt_local_table_transmit_size(bat_priv);
691 table_size += batadv_tt_len(1);
692 packet_size_max = atomic_read(&bat_priv->packet_size_max);
693 if (table_size > packet_size_max) {
694 net_ratelimited_function(batadv_info, soft_iface,
695 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
696 table_size, packet_size_max, addr);
700 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
704 /* increase the refcounter of the related vlan */
705 vlan = batadv_softif_vlan_get(bat_priv, vid);
707 net_ratelimited_function(batadv_info, soft_iface,
708 "adding TT local entry %pM to non-existent VLAN %d\n",
709 addr, BATADV_PRINT_VID(vid));
710 kmem_cache_free(batadv_tl_cache, tt_local);
715 batadv_dbg(BATADV_DBG_TT, bat_priv,
716 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
717 addr, BATADV_PRINT_VID(vid),
718 (u8)atomic_read(&bat_priv->tt.vn));
720 ether_addr_copy(tt_local->common.addr, addr);
721 /* The local entry has to be marked as NEW to avoid to send it in
722 * a full table response going out before the next ttvn increment
723 * (consistency check)
725 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
726 tt_local->common.vid = vid;
727 if (batadv_is_wifi_netdev(in_dev))
728 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
729 kref_init(&tt_local->common.refcount);
730 kref_get(&tt_local->common.refcount);
731 tt_local->last_seen = jiffies;
732 tt_local->common.added_at = tt_local->last_seen;
733 tt_local->vlan = vlan;
735 /* the batman interface mac and multicast addresses should never be
738 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
739 is_multicast_ether_addr(addr))
740 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
742 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
743 batadv_choose_tt, &tt_local->common,
744 &tt_local->common.hash_entry);
746 if (unlikely(hash_added != 0)) {
747 /* remove the reference for the hash */
748 batadv_tt_local_entry_put(tt_local);
753 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
756 /* Check whether it is a roaming, but don't do anything if the roaming
757 * process has already been handled
759 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
760 /* These node are probably going to update their tt table */
761 head = &tt_global->orig_list;
763 hlist_for_each_entry_rcu(orig_entry, head, list) {
764 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
765 tt_global->common.vid,
766 orig_entry->orig_node);
770 batadv_tt_global_free(bat_priv, tt_global,
774 /* The global entry has to be marked as ROAMING and
775 * has to be kept for consistency purpose
777 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
778 tt_global->roam_at = jiffies;
782 /* store the current remote flags before altering them. This helps
783 * understanding is flags are changing or not
785 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
787 if (batadv_is_wifi_netdev(in_dev))
788 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
790 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
792 /* check the mark in the skb: if it's equal to the configured
793 * isolation_mark, it means the packet is coming from an isolated
796 match_mark = (mark & bat_priv->isolation_mark_mask);
797 if (bat_priv->isolation_mark_mask &&
798 match_mark == bat_priv->isolation_mark)
799 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
801 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
803 /* if any "dynamic" flag has been modified, resend an ADD event for this
804 * entry so that all the nodes can get the new flags
806 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
807 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
814 batadv_tt_local_entry_put(tt_local);
816 batadv_tt_global_entry_put(tt_global);
821 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
822 * within a TT Response directed to another node
823 * @orig_node: originator for which the TT data has to be prepared
824 * @tt_data: uninitialised pointer to the address of the TVLV buffer
825 * @tt_change: uninitialised pointer to the address of the area where the TT
826 * changed can be stored
827 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
828 * function reserves the amount of space needed to send the entire global TT
829 * table. In case of success the value is updated with the real amount of
831 * Allocate the needed amount of memory for the entire TT TVLV and write its
832 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
833 * objects, one per active VLAN served by the originator node.
835 * Return: the size of the allocated buffer or 0 in case of failure.
838 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
839 struct batadv_tvlv_tt_data **tt_data,
840 struct batadv_tvlv_tt_change **tt_change,
847 struct batadv_tvlv_tt_vlan_data *tt_vlan;
848 struct batadv_orig_node_vlan *vlan;
852 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
854 num_entries += atomic_read(&vlan->tt.num_entries);
857 change_offset = sizeof(**tt_data);
858 change_offset += num_vlan * sizeof(*tt_vlan);
860 /* if tt_len is negative, allocate the space needed by the full table */
862 *tt_len = batadv_tt_len(num_entries);
865 tvlv_len += change_offset;
867 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
873 (*tt_data)->flags = BATADV_NO_FLAGS;
874 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
875 (*tt_data)->num_vlan = htons(num_vlan);
877 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
878 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
879 tt_vlan->vid = htons(vlan->vid);
880 tt_vlan->crc = htonl(vlan->tt.crc);
885 tt_change_ptr = (u8 *)*tt_data + change_offset;
886 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
894 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
896 * @bat_priv: the bat priv with all the soft interface information
897 * @tt_data: uninitialised pointer to the address of the TVLV buffer
898 * @tt_change: uninitialised pointer to the address of the area where the TT
899 * changes can be stored
900 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
901 * function reserves the amount of space needed to send the entire local TT
902 * table. In case of success the value is updated with the real amount of
905 * Allocate the needed amount of memory for the entire TT TVLV and write its
906 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
907 * objects, one per active VLAN.
909 * Return: the size of the allocated buffer or 0 in case of failure.
912 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
913 struct batadv_tvlv_tt_data **tt_data,
914 struct batadv_tvlv_tt_change **tt_change,
917 struct batadv_tvlv_tt_vlan_data *tt_vlan;
918 struct batadv_softif_vlan *vlan;
926 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
928 num_entries += atomic_read(&vlan->tt.num_entries);
931 change_offset = sizeof(**tt_data);
932 change_offset += num_vlan * sizeof(*tt_vlan);
934 /* if tt_len is negative, allocate the space needed by the full table */
936 *tt_len = batadv_tt_len(num_entries);
939 tvlv_len += change_offset;
941 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
947 (*tt_data)->flags = BATADV_NO_FLAGS;
948 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
949 (*tt_data)->num_vlan = htons(num_vlan);
951 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
952 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
953 tt_vlan->vid = htons(vlan->vid);
954 tt_vlan->crc = htonl(vlan->tt.crc);
959 tt_change_ptr = (u8 *)*tt_data + change_offset;
960 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
968 * batadv_tt_tvlv_container_update - update the translation table tvlv container
969 * after local tt changes have been committed
970 * @bat_priv: the bat priv with all the soft interface information
972 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
974 struct batadv_tt_change_node *entry, *safe;
975 struct batadv_tvlv_tt_data *tt_data;
976 struct batadv_tvlv_tt_change *tt_change;
977 int tt_diff_len, tt_change_len = 0;
978 int tt_diff_entries_num = 0;
979 int tt_diff_entries_count = 0;
982 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
983 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
985 /* if we have too many changes for one packet don't send any
986 * and wait for the tt table request which will be fragmented
988 if (tt_diff_len > bat_priv->soft_iface->mtu)
991 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
992 &tt_change, &tt_diff_len);
996 tt_data->flags = BATADV_TT_OGM_DIFF;
998 if (tt_diff_len == 0)
999 goto container_register;
1001 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1002 atomic_set(&bat_priv->tt.local_changes, 0);
1004 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1006 if (tt_diff_entries_count < tt_diff_entries_num) {
1007 memcpy(tt_change + tt_diff_entries_count,
1009 sizeof(struct batadv_tvlv_tt_change));
1010 tt_diff_entries_count++;
1012 list_del(&entry->list);
1013 kmem_cache_free(batadv_tt_change_cache, entry);
1015 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1017 /* Keep the buffer for possible tt_request */
1018 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1019 kfree(bat_priv->tt.last_changeset);
1020 bat_priv->tt.last_changeset_len = 0;
1021 bat_priv->tt.last_changeset = NULL;
1022 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1023 /* check whether this new OGM has no changes due to size problems */
1024 if (tt_diff_entries_count > 0) {
1025 /* if kmalloc() fails we will reply with the full table
1026 * instead of providing the diff
1028 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1029 if (bat_priv->tt.last_changeset) {
1030 memcpy(bat_priv->tt.last_changeset,
1031 tt_change, tt_change_len);
1032 bat_priv->tt.last_changeset_len = tt_diff_len;
1035 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1038 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1043 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
1045 struct net_device *net_dev = (struct net_device *)seq->private;
1046 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1047 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1048 struct batadv_tt_common_entry *tt_common_entry;
1049 struct batadv_tt_local_entry *tt_local;
1050 struct batadv_hard_iface *primary_if;
1051 struct hlist_head *head;
1054 int last_seen_msecs;
1055 unsigned long last_seen_jiffies;
1057 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1059 primary_if = batadv_seq_print_text_primary_if_get(seq);
1064 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1065 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1067 " Client VID Flags Last seen (CRC )\n");
1069 for (i = 0; i < hash->size; i++) {
1070 head = &hash->table[i];
1073 hlist_for_each_entry_rcu(tt_common_entry,
1075 tt_local = container_of(tt_common_entry,
1076 struct batadv_tt_local_entry,
1078 last_seen_jiffies = jiffies - tt_local->last_seen;
1079 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1080 last_seen_secs = last_seen_msecs / 1000;
1081 last_seen_msecs = last_seen_msecs % 1000;
1083 no_purge = tt_common_entry->flags & np_flag;
1085 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1086 tt_common_entry->addr,
1087 BATADV_PRINT_VID(tt_common_entry->vid),
1088 ((tt_common_entry->flags &
1089 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1090 no_purge ? 'P' : '.',
1091 ((tt_common_entry->flags &
1092 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1093 ((tt_common_entry->flags &
1094 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1095 ((tt_common_entry->flags &
1096 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1097 ((tt_common_entry->flags &
1098 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1099 no_purge ? 0 : last_seen_secs,
1100 no_purge ? 0 : last_seen_msecs,
1101 tt_local->vlan->tt.crc);
1107 batadv_hardif_put(primary_if);
1112 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1113 struct batadv_tt_local_entry *tt_local_entry,
1114 u16 flags, const char *message)
1116 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1118 /* The local client has to be marked as "pending to be removed" but has
1119 * to be kept in the table in order to send it in a full table
1120 * response issued before the net ttvn increment (consistency check)
1122 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1124 batadv_dbg(BATADV_DBG_TT, bat_priv,
1125 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1126 tt_local_entry->common.addr,
1127 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1131 * batadv_tt_local_remove - logically remove an entry from the local table
1132 * @bat_priv: the bat priv with all the soft interface information
1133 * @addr: the MAC address of the client to remove
1134 * @vid: VLAN identifier
1135 * @message: message to append to the log on deletion
1136 * @roaming: true if the deletion is due to a roaming event
1138 * Return: the flags assigned to the local entry before being deleted
1140 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1141 unsigned short vid, const char *message,
1144 struct batadv_tt_local_entry *tt_local_entry;
1145 u16 flags, curr_flags = BATADV_NO_FLAGS;
1146 void *tt_entry_exists;
1148 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1149 if (!tt_local_entry)
1152 curr_flags = tt_local_entry->common.flags;
1154 flags = BATADV_TT_CLIENT_DEL;
1155 /* if this global entry addition is due to a roaming, the node has to
1156 * mark the local entry as "roamed" in order to correctly reroute
1160 flags |= BATADV_TT_CLIENT_ROAM;
1161 /* mark the local client as ROAMed */
1162 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1165 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1166 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1170 /* if this client has been added right now, it is possible to
1171 * immediately purge it
1173 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1175 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1178 &tt_local_entry->common);
1179 if (!tt_entry_exists)
1182 /* extra call to free the local tt entry */
1183 batadv_tt_local_entry_put(tt_local_entry);
1187 batadv_tt_local_entry_put(tt_local_entry);
1193 * batadv_tt_local_purge_list - purge inactive tt local entries
1194 * @bat_priv: the bat priv with all the soft interface information
1195 * @head: pointer to the list containing the local tt entries
1196 * @timeout: parameter deciding whether a given tt local entry is considered
1199 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1200 struct hlist_head *head,
1203 struct batadv_tt_local_entry *tt_local_entry;
1204 struct batadv_tt_common_entry *tt_common_entry;
1205 struct hlist_node *node_tmp;
1207 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1209 tt_local_entry = container_of(tt_common_entry,
1210 struct batadv_tt_local_entry,
1212 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1215 /* entry already marked for deletion */
1216 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1219 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1222 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1223 BATADV_TT_CLIENT_DEL, "timed out");
1228 * batadv_tt_local_purge - purge inactive tt local entries
1229 * @bat_priv: the bat priv with all the soft interface information
1230 * @timeout: parameter deciding whether a given tt local entry is considered
1233 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1236 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1237 struct hlist_head *head;
1238 spinlock_t *list_lock; /* protects write access to the hash lists */
1241 for (i = 0; i < hash->size; i++) {
1242 head = &hash->table[i];
1243 list_lock = &hash->list_locks[i];
1245 spin_lock_bh(list_lock);
1246 batadv_tt_local_purge_list(bat_priv, head, timeout);
1247 spin_unlock_bh(list_lock);
1251 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1253 struct batadv_hashtable *hash;
1254 spinlock_t *list_lock; /* protects write access to the hash lists */
1255 struct batadv_tt_common_entry *tt_common_entry;
1256 struct batadv_tt_local_entry *tt_local;
1257 struct hlist_node *node_tmp;
1258 struct hlist_head *head;
1261 if (!bat_priv->tt.local_hash)
1264 hash = bat_priv->tt.local_hash;
1266 for (i = 0; i < hash->size; i++) {
1267 head = &hash->table[i];
1268 list_lock = &hash->list_locks[i];
1270 spin_lock_bh(list_lock);
1271 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1273 hlist_del_rcu(&tt_common_entry->hash_entry);
1274 tt_local = container_of(tt_common_entry,
1275 struct batadv_tt_local_entry,
1278 batadv_tt_local_entry_put(tt_local);
1280 spin_unlock_bh(list_lock);
1283 batadv_hash_destroy(hash);
1285 bat_priv->tt.local_hash = NULL;
1288 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1290 if (bat_priv->tt.global_hash)
1293 bat_priv->tt.global_hash = batadv_hash_new(1024);
1295 if (!bat_priv->tt.global_hash)
1298 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1299 &batadv_tt_global_hash_lock_class_key);
1304 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1306 struct batadv_tt_change_node *entry, *safe;
1308 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1310 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1312 list_del(&entry->list);
1313 kmem_cache_free(batadv_tt_change_cache, entry);
1316 atomic_set(&bat_priv->tt.local_changes, 0);
1317 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1321 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1322 * @entry: the TT global entry where the orig_list_entry has to be
1324 * @orig_node: the originator for which the orig_list_entry has to be found
1326 * retrieve the orig_tt_list_entry belonging to orig_node from the
1327 * batadv_tt_global_entry list
1329 * Return: it with an increased refcounter, NULL if not found
1331 static struct batadv_tt_orig_list_entry *
1332 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1333 const struct batadv_orig_node *orig_node)
1335 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1336 const struct hlist_head *head;
1339 head = &entry->orig_list;
1340 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1341 if (tmp_orig_entry->orig_node != orig_node)
1343 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1346 orig_entry = tmp_orig_entry;
1355 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1356 * by a given originator
1357 * @entry: the TT global entry to check
1358 * @orig_node: the originator to search in the list
1360 * find out if an orig_node is already in the list of a tt_global_entry.
1362 * Return: true if found, false otherwise
1365 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1366 const struct batadv_orig_node *orig_node)
1368 struct batadv_tt_orig_list_entry *orig_entry;
1371 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1374 batadv_tt_orig_list_entry_put(orig_entry);
1381 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1382 struct batadv_orig_node *orig_node, int ttvn)
1384 struct batadv_tt_orig_list_entry *orig_entry;
1386 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1388 /* refresh the ttvn: the current value could be a bogus one that
1389 * was added during a "temporary client detection"
1391 orig_entry->ttvn = ttvn;
1395 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1399 INIT_HLIST_NODE(&orig_entry->list);
1400 kref_get(&orig_node->refcount);
1401 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1402 orig_entry->orig_node = orig_node;
1403 orig_entry->ttvn = ttvn;
1404 kref_init(&orig_entry->refcount);
1405 kref_get(&orig_entry->refcount);
1407 spin_lock_bh(&tt_global->list_lock);
1408 hlist_add_head_rcu(&orig_entry->list,
1409 &tt_global->orig_list);
1410 spin_unlock_bh(&tt_global->list_lock);
1411 atomic_inc(&tt_global->orig_list_count);
1415 batadv_tt_orig_list_entry_put(orig_entry);
1419 * batadv_tt_global_add - add a new TT global entry or update an existing one
1420 * @bat_priv: the bat priv with all the soft interface information
1421 * @orig_node: the originator announcing the client
1422 * @tt_addr: the mac address of the non-mesh client
1423 * @vid: VLAN identifier
1424 * @flags: TT flags that have to be set for this non-mesh client
1425 * @ttvn: the tt version number ever announcing this non-mesh client
1427 * Add a new TT global entry for the given originator. If the entry already
1428 * exists add a new reference to the given originator (a global entry can have
1429 * references to multiple originators) and adjust the flags attribute to reflect
1430 * the function argument.
1431 * If a TT local entry exists for this non-mesh client remove it.
1433 * The caller must hold orig_node refcount.
1435 * Return: true if the new entry has been added, false otherwise
1437 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1438 struct batadv_orig_node *orig_node,
1439 const unsigned char *tt_addr,
1440 unsigned short vid, u16 flags, u8 ttvn)
1442 struct batadv_tt_global_entry *tt_global_entry;
1443 struct batadv_tt_local_entry *tt_local_entry;
1446 struct batadv_tt_common_entry *common;
1449 /* ignore global entries from backbone nodes */
1450 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1453 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1454 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1456 /* if the node already has a local client for this entry, it has to wait
1457 * for a roaming advertisement instead of manually messing up the global
1460 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1461 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1464 if (!tt_global_entry) {
1465 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1467 if (!tt_global_entry)
1470 common = &tt_global_entry->common;
1471 ether_addr_copy(common->addr, tt_addr);
1474 common->flags = flags;
1475 tt_global_entry->roam_at = 0;
1476 /* node must store current time in case of roaming. This is
1477 * needed to purge this entry out on timeout (if nobody claims
1480 if (flags & BATADV_TT_CLIENT_ROAM)
1481 tt_global_entry->roam_at = jiffies;
1482 kref_init(&common->refcount);
1483 kref_get(&common->refcount);
1484 common->added_at = jiffies;
1486 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1487 atomic_set(&tt_global_entry->orig_list_count, 0);
1488 spin_lock_init(&tt_global_entry->list_lock);
1490 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1492 batadv_choose_tt, common,
1493 &common->hash_entry);
1495 if (unlikely(hash_added != 0)) {
1496 /* remove the reference for the hash */
1497 batadv_tt_global_entry_put(tt_global_entry);
1501 common = &tt_global_entry->common;
1502 /* If there is already a global entry, we can use this one for
1504 * But if we are trying to add a temporary client then here are
1505 * two options at this point:
1506 * 1) the global client is not a temporary client: the global
1507 * client has to be left as it is, temporary information
1508 * should never override any already known client state
1509 * 2) the global client is a temporary client: purge the
1510 * originator list and add the new one orig_entry
1512 if (flags & BATADV_TT_CLIENT_TEMP) {
1513 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1515 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1518 batadv_tt_global_del_orig_list(tt_global_entry);
1519 goto add_orig_entry;
1522 /* if the client was temporary added before receiving the first
1523 * OGM announcing it, we have to clear the TEMP flag. Also,
1524 * remove the previous temporary orig node and re-add it
1525 * if required. If the orig entry changed, the new one which
1526 * is a non-temporary entry is preferred.
1528 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1529 batadv_tt_global_del_orig_list(tt_global_entry);
1530 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1533 /* the change can carry possible "attribute" flags like the
1534 * TT_CLIENT_WIFI, therefore they have to be copied in the
1537 common->flags |= flags;
1539 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1540 * one originator left in the list and we previously received a
1541 * delete + roaming change for this originator.
1543 * We should first delete the old originator before adding the
1546 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1547 batadv_tt_global_del_orig_list(tt_global_entry);
1548 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1549 tt_global_entry->roam_at = 0;
1553 /* add the new orig_entry (if needed) or update it */
1554 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1556 batadv_dbg(BATADV_DBG_TT, bat_priv,
1557 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1558 common->addr, BATADV_PRINT_VID(common->vid),
1563 /* Do not remove multicast addresses from the local hash on
1566 if (is_multicast_ether_addr(tt_addr))
1569 /* remove address from local hash if present */
1570 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1571 "global tt received",
1572 flags & BATADV_TT_CLIENT_ROAM);
1573 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1575 if (!(flags & BATADV_TT_CLIENT_ROAM))
1576 /* this is a normal global add. Therefore the client is not in a
1577 * roaming state anymore.
1579 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1582 if (tt_global_entry)
1583 batadv_tt_global_entry_put(tt_global_entry);
1585 batadv_tt_local_entry_put(tt_local_entry);
1590 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1591 * @bat_priv: the bat priv with all the soft interface information
1592 * @tt_global_entry: global translation table entry to be analyzed
1594 * This functon assumes the caller holds rcu_read_lock().
1595 * Return: best originator list entry or NULL on errors.
1597 static struct batadv_tt_orig_list_entry *
1598 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1599 struct batadv_tt_global_entry *tt_global_entry)
1601 struct batadv_neigh_node *router, *best_router = NULL;
1602 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1603 struct hlist_head *head;
1604 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1606 head = &tt_global_entry->orig_list;
1607 hlist_for_each_entry_rcu(orig_entry, head, list) {
1608 router = batadv_orig_router_get(orig_entry->orig_node,
1614 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1615 BATADV_IF_DEFAULT) <= 0) {
1616 batadv_neigh_node_put(router);
1620 /* release the refcount for the "old" best */
1622 batadv_neigh_node_put(best_router);
1624 best_entry = orig_entry;
1625 best_router = router;
1629 batadv_neigh_node_put(best_router);
1635 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1636 * for this global entry
1637 * @bat_priv: the bat priv with all the soft interface information
1638 * @tt_global_entry: global translation table entry to be printed
1639 * @seq: debugfs table seq_file struct
1641 * This functon assumes the caller holds rcu_read_lock().
1644 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1645 struct batadv_tt_global_entry *tt_global_entry,
1646 struct seq_file *seq)
1648 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1649 struct batadv_tt_common_entry *tt_common_entry;
1650 struct batadv_orig_node_vlan *vlan;
1651 struct hlist_head *head;
1655 tt_common_entry = &tt_global_entry->common;
1656 flags = tt_common_entry->flags;
1658 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1660 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1661 tt_common_entry->vid);
1664 " * Cannot retrieve VLAN %d for originator %pM\n",
1665 BATADV_PRINT_VID(tt_common_entry->vid),
1666 best_entry->orig_node->orig);
1670 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1672 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1673 '*', tt_global_entry->common.addr,
1674 BATADV_PRINT_VID(tt_global_entry->common.vid),
1675 best_entry->ttvn, best_entry->orig_node->orig,
1676 last_ttvn, vlan->tt.crc,
1677 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1678 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1679 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1680 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1682 batadv_orig_node_vlan_put(vlan);
1686 head = &tt_global_entry->orig_list;
1688 hlist_for_each_entry_rcu(orig_entry, head, list) {
1689 if (best_entry == orig_entry)
1692 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1693 tt_common_entry->vid);
1696 " + Cannot retrieve VLAN %d for originator %pM\n",
1697 BATADV_PRINT_VID(tt_common_entry->vid),
1698 orig_entry->orig_node->orig);
1702 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1704 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1705 '+', tt_global_entry->common.addr,
1706 BATADV_PRINT_VID(tt_global_entry->common.vid),
1707 orig_entry->ttvn, orig_entry->orig_node->orig,
1708 last_ttvn, vlan->tt.crc,
1709 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1710 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1711 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1712 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1714 batadv_orig_node_vlan_put(vlan);
1718 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1720 struct net_device *net_dev = (struct net_device *)seq->private;
1721 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1722 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1723 struct batadv_tt_common_entry *tt_common_entry;
1724 struct batadv_tt_global_entry *tt_global;
1725 struct batadv_hard_iface *primary_if;
1726 struct hlist_head *head;
1729 primary_if = batadv_seq_print_text_primary_if_get(seq);
1734 "Globally announced TT entries received via the mesh %s\n",
1737 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1739 for (i = 0; i < hash->size; i++) {
1740 head = &hash->table[i];
1743 hlist_for_each_entry_rcu(tt_common_entry,
1745 tt_global = container_of(tt_common_entry,
1746 struct batadv_tt_global_entry,
1748 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1754 batadv_hardif_put(primary_if);
1759 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1760 * @tt_global_entry: the global entry to remove the orig_entry from
1761 * @orig_entry: the orig entry to remove and free
1763 * Remove an orig_entry from its list in the given tt_global_entry and
1764 * free this orig_entry afterwards.
1766 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1770 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1771 struct batadv_tt_orig_list_entry *orig_entry)
1773 lockdep_assert_held(&tt_global_entry->list_lock);
1775 batadv_tt_global_size_dec(orig_entry->orig_node,
1776 tt_global_entry->common.vid);
1777 atomic_dec(&tt_global_entry->orig_list_count);
1778 /* requires holding tt_global_entry->list_lock and orig_entry->list
1779 * being part of a list
1781 hlist_del_rcu(&orig_entry->list);
1782 batadv_tt_orig_list_entry_put(orig_entry);
1785 /* deletes the orig list of a tt_global_entry */
1787 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1789 struct hlist_head *head;
1790 struct hlist_node *safe;
1791 struct batadv_tt_orig_list_entry *orig_entry;
1793 spin_lock_bh(&tt_global_entry->list_lock);
1794 head = &tt_global_entry->orig_list;
1795 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1796 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1797 spin_unlock_bh(&tt_global_entry->list_lock);
1801 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1802 * @bat_priv: the bat priv with all the soft interface information
1803 * @tt_global_entry: the global entry to remove the orig_node from
1804 * @orig_node: the originator announcing the client
1805 * @message: message to append to the log on deletion
1807 * Remove the given orig_node and its according orig_entry from the given
1811 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1812 struct batadv_tt_global_entry *tt_global_entry,
1813 struct batadv_orig_node *orig_node,
1814 const char *message)
1816 struct hlist_head *head;
1817 struct hlist_node *safe;
1818 struct batadv_tt_orig_list_entry *orig_entry;
1821 spin_lock_bh(&tt_global_entry->list_lock);
1822 head = &tt_global_entry->orig_list;
1823 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1824 if (orig_entry->orig_node == orig_node) {
1825 vid = tt_global_entry->common.vid;
1826 batadv_dbg(BATADV_DBG_TT, bat_priv,
1827 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1829 tt_global_entry->common.addr,
1830 BATADV_PRINT_VID(vid), message);
1831 _batadv_tt_global_del_orig_entry(tt_global_entry,
1835 spin_unlock_bh(&tt_global_entry->list_lock);
1838 /* If the client is to be deleted, we check if it is the last origantor entry
1839 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1840 * timer, otherwise we simply remove the originator scheduled for deletion.
1843 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1844 struct batadv_tt_global_entry *tt_global_entry,
1845 struct batadv_orig_node *orig_node,
1846 const char *message)
1848 bool last_entry = true;
1849 struct hlist_head *head;
1850 struct batadv_tt_orig_list_entry *orig_entry;
1852 /* no local entry exists, case 1:
1853 * Check if this is the last one or if other entries exist.
1857 head = &tt_global_entry->orig_list;
1858 hlist_for_each_entry_rcu(orig_entry, head, list) {
1859 if (orig_entry->orig_node != orig_node) {
1867 /* its the last one, mark for roaming. */
1868 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1869 tt_global_entry->roam_at = jiffies;
1871 /* there is another entry, we can simply delete this
1872 * one and can still use the other one.
1874 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1875 orig_node, message);
1879 * batadv_tt_global_del - remove a client from the global table
1880 * @bat_priv: the bat priv with all the soft interface information
1881 * @orig_node: an originator serving this client
1882 * @addr: the mac address of the client
1883 * @vid: VLAN identifier
1884 * @message: a message explaining the reason for deleting the client to print
1885 * for debugging purpose
1886 * @roaming: true if the deletion has been triggered by a roaming event
1888 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1889 struct batadv_orig_node *orig_node,
1890 const unsigned char *addr, unsigned short vid,
1891 const char *message, bool roaming)
1893 struct batadv_tt_global_entry *tt_global_entry;
1894 struct batadv_tt_local_entry *local_entry = NULL;
1896 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1897 if (!tt_global_entry)
1901 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1902 orig_node, message);
1904 if (hlist_empty(&tt_global_entry->orig_list))
1905 batadv_tt_global_free(bat_priv, tt_global_entry,
1911 /* if we are deleting a global entry due to a roam
1912 * event, there are two possibilities:
1913 * 1) the client roamed from node A to node B => if there
1914 * is only one originator left for this client, we mark
1915 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1916 * wait for node B to claim it. In case of timeout
1917 * the entry is purged.
1919 * If there are other originators left, we directly delete
1921 * 2) the client roamed to us => we can directly delete
1922 * the global entry, since it is useless now.
1924 local_entry = batadv_tt_local_hash_find(bat_priv,
1925 tt_global_entry->common.addr,
1928 /* local entry exists, case 2: client roamed to us. */
1929 batadv_tt_global_del_orig_list(tt_global_entry);
1930 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1932 /* no local entry exists, case 1: check for roaming */
1933 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1934 orig_node, message);
1937 if (tt_global_entry)
1938 batadv_tt_global_entry_put(tt_global_entry);
1940 batadv_tt_local_entry_put(local_entry);
1944 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1945 * given originator matching the provided vid
1946 * @bat_priv: the bat priv with all the soft interface information
1947 * @orig_node: the originator owning the entries to remove
1948 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1950 * @message: debug message to print as "reason"
1952 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1953 struct batadv_orig_node *orig_node,
1955 const char *message)
1957 struct batadv_tt_global_entry *tt_global;
1958 struct batadv_tt_common_entry *tt_common_entry;
1960 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1961 struct hlist_node *safe;
1962 struct hlist_head *head;
1963 spinlock_t *list_lock; /* protects write access to the hash lists */
1969 for (i = 0; i < hash->size; i++) {
1970 head = &hash->table[i];
1971 list_lock = &hash->list_locks[i];
1973 spin_lock_bh(list_lock);
1974 hlist_for_each_entry_safe(tt_common_entry, safe,
1976 /* remove only matching entries */
1977 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1980 tt_global = container_of(tt_common_entry,
1981 struct batadv_tt_global_entry,
1984 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1985 orig_node, message);
1987 if (hlist_empty(&tt_global->orig_list)) {
1988 vid = tt_global->common.vid;
1989 batadv_dbg(BATADV_DBG_TT, bat_priv,
1990 "Deleting global tt entry %pM (vid: %d): %s\n",
1991 tt_global->common.addr,
1992 BATADV_PRINT_VID(vid), message);
1993 hlist_del_rcu(&tt_common_entry->hash_entry);
1994 batadv_tt_global_entry_put(tt_global);
1997 spin_unlock_bh(list_lock);
1999 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2002 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2006 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2007 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2009 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2010 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2012 *msg = "Roaming timeout\n";
2015 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2016 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2018 *msg = "Temporary client timeout\n";
2024 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2026 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2027 struct hlist_head *head;
2028 struct hlist_node *node_tmp;
2029 spinlock_t *list_lock; /* protects write access to the hash lists */
2032 struct batadv_tt_common_entry *tt_common;
2033 struct batadv_tt_global_entry *tt_global;
2035 for (i = 0; i < hash->size; i++) {
2036 head = &hash->table[i];
2037 list_lock = &hash->list_locks[i];
2039 spin_lock_bh(list_lock);
2040 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2042 tt_global = container_of(tt_common,
2043 struct batadv_tt_global_entry,
2046 if (!batadv_tt_global_to_purge(tt_global, &msg))
2049 batadv_dbg(BATADV_DBG_TT, bat_priv,
2050 "Deleting global tt entry %pM (vid: %d): %s\n",
2051 tt_global->common.addr,
2052 BATADV_PRINT_VID(tt_global->common.vid),
2055 hlist_del_rcu(&tt_common->hash_entry);
2057 batadv_tt_global_entry_put(tt_global);
2059 spin_unlock_bh(list_lock);
2063 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2065 struct batadv_hashtable *hash;
2066 spinlock_t *list_lock; /* protects write access to the hash lists */
2067 struct batadv_tt_common_entry *tt_common_entry;
2068 struct batadv_tt_global_entry *tt_global;
2069 struct hlist_node *node_tmp;
2070 struct hlist_head *head;
2073 if (!bat_priv->tt.global_hash)
2076 hash = bat_priv->tt.global_hash;
2078 for (i = 0; i < hash->size; i++) {
2079 head = &hash->table[i];
2080 list_lock = &hash->list_locks[i];
2082 spin_lock_bh(list_lock);
2083 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2085 hlist_del_rcu(&tt_common_entry->hash_entry);
2086 tt_global = container_of(tt_common_entry,
2087 struct batadv_tt_global_entry,
2089 batadv_tt_global_entry_put(tt_global);
2091 spin_unlock_bh(list_lock);
2094 batadv_hash_destroy(hash);
2096 bat_priv->tt.global_hash = NULL;
2100 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2101 struct batadv_tt_global_entry *tt_global_entry)
2105 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2106 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2109 /* check if the two clients are marked as isolated */
2110 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2111 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2118 * batadv_transtable_search - get the mesh destination for a given client
2119 * @bat_priv: the bat priv with all the soft interface information
2120 * @src: mac address of the source client
2121 * @addr: mac address of the destination client
2122 * @vid: VLAN identifier
2124 * Return: a pointer to the originator that was selected as destination in the
2125 * mesh for contacting the client 'addr', NULL otherwise.
2126 * In case of multiple originators serving the same client, the function returns
2127 * the best one (best in terms of metric towards the destination node).
2129 * If the two clients are AP isolated the function returns NULL.
2131 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2136 struct batadv_tt_local_entry *tt_local_entry = NULL;
2137 struct batadv_tt_global_entry *tt_global_entry = NULL;
2138 struct batadv_orig_node *orig_node = NULL;
2139 struct batadv_tt_orig_list_entry *best_entry;
2141 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2143 if (!tt_local_entry ||
2144 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2148 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2149 if (!tt_global_entry)
2152 /* check whether the clients should not communicate due to AP
2155 if (tt_local_entry &&
2156 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2160 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2161 /* found anything? */
2163 orig_node = best_entry->orig_node;
2164 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2169 if (tt_global_entry)
2170 batadv_tt_global_entry_put(tt_global_entry);
2172 batadv_tt_local_entry_put(tt_local_entry);
2178 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2179 * to the given orig_node
2180 * @bat_priv: the bat priv with all the soft interface information
2181 * @orig_node: originator for which the CRC should be computed
2182 * @vid: VLAN identifier for which the CRC32 has to be computed
2184 * This function computes the checksum for the global table corresponding to a
2185 * specific originator. In particular, the checksum is computed as follows: For
2186 * each client connected to the originator the CRC32C of the MAC address and the
2187 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2190 * The idea behind is that CRC32C should be used as much as possible in order to
2191 * produce a unique hash of the table, but since the order which is used to feed
2192 * the CRC32C function affects the result and since every node in the network
2193 * probably sorts the clients differently, the hash function cannot be directly
2194 * computed over the entire table. Hence the CRC32C is used only on
2195 * the single client entry, while all the results are then xor'ed together
2196 * because the XOR operation can combine them all while trying to reduce the
2197 * noise as much as possible.
2199 * Return: the checksum of the global table of a given originator.
2201 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2202 struct batadv_orig_node *orig_node,
2205 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2206 struct batadv_tt_common_entry *tt_common;
2207 struct batadv_tt_global_entry *tt_global;
2208 struct hlist_head *head;
2209 u32 i, crc_tmp, crc = 0;
2213 for (i = 0; i < hash->size; i++) {
2214 head = &hash->table[i];
2217 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2218 tt_global = container_of(tt_common,
2219 struct batadv_tt_global_entry,
2221 /* compute the CRC only for entries belonging to the
2222 * VLAN identified by the vid passed as parameter
2224 if (tt_common->vid != vid)
2227 /* Roaming clients are in the global table for
2228 * consistency only. They don't have to be
2229 * taken into account while computing the
2232 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2234 /* Temporary clients have not been announced yet, so
2235 * they have to be skipped while computing the global
2238 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2241 /* find out if this global entry is announced by this
2244 if (!batadv_tt_global_entry_has_orig(tt_global,
2248 /* use network order to read the VID: this ensures that
2249 * every node reads the bytes in the same order.
2251 tmp_vid = htons(tt_common->vid);
2252 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2254 /* compute the CRC on flags that have to be kept in sync
2257 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2258 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2260 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2269 * batadv_tt_local_crc - calculates the checksum of the local table
2270 * @bat_priv: the bat priv with all the soft interface information
2271 * @vid: VLAN identifier for which the CRC32 has to be computed
2273 * For details about the computation, please refer to the documentation for
2274 * batadv_tt_global_crc().
2276 * Return: the checksum of the local table
2278 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2281 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2282 struct batadv_tt_common_entry *tt_common;
2283 struct hlist_head *head;
2284 u32 i, crc_tmp, crc = 0;
2288 for (i = 0; i < hash->size; i++) {
2289 head = &hash->table[i];
2292 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2293 /* compute the CRC only for entries belonging to the
2294 * VLAN identified by vid
2296 if (tt_common->vid != vid)
2299 /* not yet committed clients have not to be taken into
2300 * account while computing the CRC
2302 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2305 /* use network order to read the VID: this ensures that
2306 * every node reads the bytes in the same order.
2308 tmp_vid = htons(tt_common->vid);
2309 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2311 /* compute the CRC on flags that have to be kept in sync
2314 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2315 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2317 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2326 * batadv_tt_req_node_release - free tt_req node entry
2327 * @ref: kref pointer of the tt req_node entry
2329 static void batadv_tt_req_node_release(struct kref *ref)
2331 struct batadv_tt_req_node *tt_req_node;
2333 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2335 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2339 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2340 * possibly release it
2341 * @tt_req_node: tt_req_node to be free'd
2343 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2345 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2348 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2350 struct batadv_tt_req_node *node;
2351 struct hlist_node *safe;
2353 spin_lock_bh(&bat_priv->tt.req_list_lock);
2355 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2356 hlist_del_init(&node->list);
2357 batadv_tt_req_node_put(node);
2360 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2363 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2364 struct batadv_orig_node *orig_node,
2365 const void *tt_buff,
2368 /* Replace the old buffer only if I received something in the
2369 * last OGM (the OGM could carry no changes)
2371 spin_lock_bh(&orig_node->tt_buff_lock);
2372 if (tt_buff_len > 0) {
2373 kfree(orig_node->tt_buff);
2374 orig_node->tt_buff_len = 0;
2375 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2376 if (orig_node->tt_buff) {
2377 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2378 orig_node->tt_buff_len = tt_buff_len;
2381 spin_unlock_bh(&orig_node->tt_buff_lock);
2384 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2386 struct batadv_tt_req_node *node;
2387 struct hlist_node *safe;
2389 spin_lock_bh(&bat_priv->tt.req_list_lock);
2390 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2391 if (batadv_has_timed_out(node->issued_at,
2392 BATADV_TT_REQUEST_TIMEOUT)) {
2393 hlist_del_init(&node->list);
2394 batadv_tt_req_node_put(node);
2397 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2401 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2402 * @bat_priv: the bat priv with all the soft interface information
2403 * @orig_node: orig node this request is being issued for
2405 * Return: the pointer to the new tt_req_node struct if no request
2406 * has already been issued for this orig_node, NULL otherwise.
2408 static struct batadv_tt_req_node *
2409 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2410 struct batadv_orig_node *orig_node)
2412 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2414 spin_lock_bh(&bat_priv->tt.req_list_lock);
2415 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2416 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2417 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2418 BATADV_TT_REQUEST_TIMEOUT))
2422 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2426 kref_init(&tt_req_node->refcount);
2427 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2428 tt_req_node->issued_at = jiffies;
2430 kref_get(&tt_req_node->refcount);
2431 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2433 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2438 * batadv_tt_local_valid - verify that given tt entry is a valid one
2439 * @entry_ptr: to be checked local tt entry
2440 * @data_ptr: not used but definition required to satisfy the callback prototype
2442 * Return: true if the entry is a valid, false otherwise.
2444 static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2446 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2448 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2453 static bool batadv_tt_global_valid(const void *entry_ptr,
2454 const void *data_ptr)
2456 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2457 const struct batadv_tt_global_entry *tt_global_entry;
2458 const struct batadv_orig_node *orig_node = data_ptr;
2460 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2461 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2464 tt_global_entry = container_of(tt_common_entry,
2465 struct batadv_tt_global_entry,
2468 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2472 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2474 * @bat_priv: the bat priv with all the soft interface information
2475 * @hash: hash table containing the tt entries
2476 * @tt_len: expected tvlv tt data buffer length in number of bytes
2477 * @tvlv_buff: pointer to the buffer to fill with the TT data
2478 * @valid_cb: function to filter tt change entries
2479 * @cb_data: data passed to the filter function as argument
2481 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2482 struct batadv_hashtable *hash,
2483 void *tvlv_buff, u16 tt_len,
2484 bool (*valid_cb)(const void *,
2488 struct batadv_tt_common_entry *tt_common_entry;
2489 struct batadv_tvlv_tt_change *tt_change;
2490 struct hlist_head *head;
2491 u16 tt_tot, tt_num_entries = 0;
2494 tt_tot = batadv_tt_entries(tt_len);
2495 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2498 for (i = 0; i < hash->size; i++) {
2499 head = &hash->table[i];
2501 hlist_for_each_entry_rcu(tt_common_entry,
2503 if (tt_tot == tt_num_entries)
2506 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2509 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2510 tt_change->flags = tt_common_entry->flags;
2511 tt_change->vid = htons(tt_common_entry->vid);
2512 memset(tt_change->reserved, 0,
2513 sizeof(tt_change->reserved));
2523 * batadv_tt_global_check_crc - check if all the CRCs are correct
2524 * @orig_node: originator for which the CRCs have to be checked
2525 * @tt_vlan: pointer to the first tvlv VLAN entry
2526 * @num_vlan: number of tvlv VLAN entries
2528 * Return: true if all the received CRCs match the locally stored ones, false
2531 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2532 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2535 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2536 struct batadv_orig_node_vlan *vlan;
2537 int i, orig_num_vlan;
2540 /* check if each received CRC matches the locally stored one */
2541 for (i = 0; i < num_vlan; i++) {
2542 tt_vlan_tmp = tt_vlan + i;
2544 /* if orig_node is a backbone node for this VLAN, don't check
2545 * the CRC as we ignore all the global entries over it
2547 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2549 ntohs(tt_vlan_tmp->vid)))
2552 vlan = batadv_orig_node_vlan_get(orig_node,
2553 ntohs(tt_vlan_tmp->vid));
2558 batadv_orig_node_vlan_put(vlan);
2560 if (crc != ntohl(tt_vlan_tmp->crc))
2564 /* check if any excess VLANs exist locally for the originator
2565 * which are not mentioned in the TVLV from the originator.
2569 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2573 if (orig_num_vlan > num_vlan)
2580 * batadv_tt_local_update_crc - update all the local CRCs
2581 * @bat_priv: the bat priv with all the soft interface information
2583 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2585 struct batadv_softif_vlan *vlan;
2587 /* recompute the global CRC for each VLAN */
2589 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2590 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2596 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2597 * @bat_priv: the bat priv with all the soft interface information
2598 * @orig_node: the orig_node for which the CRCs have to be updated
2600 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2601 struct batadv_orig_node *orig_node)
2603 struct batadv_orig_node_vlan *vlan;
2606 /* recompute the global CRC for each VLAN */
2608 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2609 /* if orig_node is a backbone node for this VLAN, don't compute
2610 * the CRC as we ignore all the global entries over it
2612 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2616 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2623 * batadv_send_tt_request - send a TT Request message to a given node
2624 * @bat_priv: the bat priv with all the soft interface information
2625 * @dst_orig_node: the destination of the message
2626 * @ttvn: the version number that the source of the message is looking for
2627 * @tt_vlan: pointer to the first tvlv VLAN object to request
2628 * @num_vlan: number of tvlv VLAN entries
2629 * @full_table: ask for the entire translation table if true, while only for the
2630 * last TT diff otherwise
2632 * Return: true if the TT Request was sent, false otherwise
2634 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2635 struct batadv_orig_node *dst_orig_node,
2637 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2638 u16 num_vlan, bool full_table)
2640 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2641 struct batadv_tt_req_node *tt_req_node = NULL;
2642 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2643 struct batadv_hard_iface *primary_if;
2647 primary_if = batadv_primary_if_get_selected(bat_priv);
2651 /* The new tt_req will be issued only if I'm not waiting for a
2652 * reply from the same orig_node yet
2654 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2658 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2659 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2663 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2664 tvlv_tt_data->ttvn = ttvn;
2665 tvlv_tt_data->num_vlan = htons(num_vlan);
2667 /* send all the CRCs within the request. This is needed by intermediate
2668 * nodes to ensure they have the correct table before replying
2670 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2671 for (i = 0; i < num_vlan; i++) {
2672 tt_vlan_req->vid = tt_vlan->vid;
2673 tt_vlan_req->crc = tt_vlan->crc;
2680 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2682 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2683 dst_orig_node->orig, full_table ? 'F' : '.');
2685 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2686 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2687 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2688 tvlv_tt_data, size);
2693 batadv_hardif_put(primary_if);
2695 if (ret && tt_req_node) {
2696 spin_lock_bh(&bat_priv->tt.req_list_lock);
2697 if (!hlist_unhashed(&tt_req_node->list)) {
2698 hlist_del_init(&tt_req_node->list);
2699 batadv_tt_req_node_put(tt_req_node);
2701 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2705 batadv_tt_req_node_put(tt_req_node);
2707 kfree(tvlv_tt_data);
2712 * batadv_send_other_tt_response - send reply to tt request concerning another
2713 * node's translation table
2714 * @bat_priv: the bat priv with all the soft interface information
2715 * @tt_data: tt data containing the tt request information
2716 * @req_src: mac address of tt request sender
2717 * @req_dst: mac address of tt request recipient
2719 * Return: true if tt request reply was sent, false otherwise.
2721 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2722 struct batadv_tvlv_tt_data *tt_data,
2723 u8 *req_src, u8 *req_dst)
2725 struct batadv_orig_node *req_dst_orig_node;
2726 struct batadv_orig_node *res_dst_orig_node = NULL;
2727 struct batadv_tvlv_tt_change *tt_change;
2728 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2729 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2730 bool ret = false, full_table;
2731 u8 orig_ttvn, req_ttvn;
2735 batadv_dbg(BATADV_DBG_TT, bat_priv,
2736 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2737 req_src, tt_data->ttvn, req_dst,
2738 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2740 /* Let's get the orig node of the REAL destination */
2741 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2742 if (!req_dst_orig_node)
2745 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2746 if (!res_dst_orig_node)
2749 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2750 req_ttvn = tt_data->ttvn;
2752 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2753 /* this node doesn't have the requested data */
2754 if (orig_ttvn != req_ttvn ||
2755 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2756 ntohs(tt_data->num_vlan)))
2759 /* If the full table has been explicitly requested */
2760 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2761 !req_dst_orig_node->tt_buff)
2766 /* TT fragmentation hasn't been implemented yet, so send as many
2767 * TT entries fit a single packet as possible only
2770 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2771 tt_len = req_dst_orig_node->tt_buff_len;
2773 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2780 /* Copy the last orig_node's OGM buffer */
2781 memcpy(tt_change, req_dst_orig_node->tt_buff,
2782 req_dst_orig_node->tt_buff_len);
2783 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2785 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2786 * in the initial part
2789 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2796 /* fill the rest of the tvlv with the real TT entries */
2797 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2799 batadv_tt_global_valid,
2803 /* Don't send the response, if larger than fragmented packet. */
2804 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2805 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2806 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2807 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2808 res_dst_orig_node->orig);
2812 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2813 tvlv_tt_data->ttvn = req_ttvn;
2816 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2818 batadv_dbg(BATADV_DBG_TT, bat_priv,
2819 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2820 res_dst_orig_node->orig, req_dst_orig_node->orig,
2821 full_table ? 'F' : '.', req_ttvn);
2823 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2825 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2826 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2833 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2836 if (res_dst_orig_node)
2837 batadv_orig_node_put(res_dst_orig_node);
2838 if (req_dst_orig_node)
2839 batadv_orig_node_put(req_dst_orig_node);
2840 kfree(tvlv_tt_data);
2845 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2847 * @bat_priv: the bat priv with all the soft interface information
2848 * @tt_data: tt data containing the tt request information
2849 * @req_src: mac address of tt request sender
2851 * Return: true if tt request reply was sent, false otherwise.
2853 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2854 struct batadv_tvlv_tt_data *tt_data,
2857 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2858 struct batadv_hard_iface *primary_if = NULL;
2859 struct batadv_tvlv_tt_change *tt_change;
2860 struct batadv_orig_node *orig_node;
2861 u8 my_ttvn, req_ttvn;
2866 batadv_dbg(BATADV_DBG_TT, bat_priv,
2867 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2868 req_src, tt_data->ttvn,
2869 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2871 spin_lock_bh(&bat_priv->tt.commit_lock);
2873 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2874 req_ttvn = tt_data->ttvn;
2876 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2880 primary_if = batadv_primary_if_get_selected(bat_priv);
2884 /* If the full table has been explicitly requested or the gap
2885 * is too big send the whole local translation table
2887 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2888 !bat_priv->tt.last_changeset)
2893 /* TT fragmentation hasn't been implemented yet, so send as many
2894 * TT entries fit a single packet as possible only
2897 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2899 tt_len = bat_priv->tt.last_changeset_len;
2900 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2907 /* Copy the last orig_node's OGM buffer */
2908 memcpy(tt_change, bat_priv->tt.last_changeset,
2909 bat_priv->tt.last_changeset_len);
2910 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2912 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2914 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2915 * in the initial part
2918 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2925 /* fill the rest of the tvlv with the real TT entries */
2926 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2928 batadv_tt_local_valid, NULL);
2931 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2932 tvlv_tt_data->ttvn = req_ttvn;
2935 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2937 batadv_dbg(BATADV_DBG_TT, bat_priv,
2938 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2939 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2941 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2943 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2944 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2950 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2952 spin_unlock_bh(&bat_priv->tt.commit_lock);
2954 batadv_orig_node_put(orig_node);
2956 batadv_hardif_put(primary_if);
2957 kfree(tvlv_tt_data);
2958 /* The packet was for this host, so it doesn't need to be re-routed */
2963 * batadv_send_tt_response - send reply to tt request
2964 * @bat_priv: the bat priv with all the soft interface information
2965 * @tt_data: tt data containing the tt request information
2966 * @req_src: mac address of tt request sender
2967 * @req_dst: mac address of tt request recipient
2969 * Return: true if tt request reply was sent, false otherwise.
2971 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2972 struct batadv_tvlv_tt_data *tt_data,
2973 u8 *req_src, u8 *req_dst)
2975 if (batadv_is_my_mac(bat_priv, req_dst))
2976 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2977 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2981 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2982 struct batadv_orig_node *orig_node,
2983 struct batadv_tvlv_tt_change *tt_change,
2984 u16 tt_num_changes, u8 ttvn)
2989 for (i = 0; i < tt_num_changes; i++) {
2990 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2991 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2992 batadv_tt_global_del(bat_priv, orig_node,
2993 (tt_change + i)->addr,
2994 ntohs((tt_change + i)->vid),
2995 "tt removed by changes",
2998 if (!batadv_tt_global_add(bat_priv, orig_node,
2999 (tt_change + i)->addr,
3000 ntohs((tt_change + i)->vid),
3001 (tt_change + i)->flags, ttvn))
3002 /* In case of problem while storing a
3003 * global_entry, we stop the updating
3004 * procedure without committing the
3005 * ttvn change. This will avoid to send
3006 * corrupted data on tt_request
3011 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3014 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3015 struct batadv_tvlv_tt_change *tt_change,
3016 u8 ttvn, u8 *resp_src,
3019 struct batadv_orig_node *orig_node;
3021 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3025 /* Purge the old table first.. */
3026 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3027 "Received full table");
3029 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3032 spin_lock_bh(&orig_node->tt_buff_lock);
3033 kfree(orig_node->tt_buff);
3034 orig_node->tt_buff_len = 0;
3035 orig_node->tt_buff = NULL;
3036 spin_unlock_bh(&orig_node->tt_buff_lock);
3038 atomic_set(&orig_node->last_ttvn, ttvn);
3042 batadv_orig_node_put(orig_node);
3045 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3046 struct batadv_orig_node *orig_node,
3047 u16 tt_num_changes, u8 ttvn,
3048 struct batadv_tvlv_tt_change *tt_change)
3050 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3051 tt_num_changes, ttvn);
3053 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3054 batadv_tt_len(tt_num_changes));
3055 atomic_set(&orig_node->last_ttvn, ttvn);
3059 * batadv_is_my_client - check if a client is served by the local node
3060 * @bat_priv: the bat priv with all the soft interface information
3061 * @addr: the mac address of the client to check
3062 * @vid: VLAN identifier
3064 * Return: true if the client is served by this node, false otherwise.
3066 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3069 struct batadv_tt_local_entry *tt_local_entry;
3072 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3073 if (!tt_local_entry)
3075 /* Check if the client has been logically deleted (but is kept for
3076 * consistency purpose)
3078 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3079 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3084 batadv_tt_local_entry_put(tt_local_entry);
3089 * batadv_handle_tt_response - process incoming tt reply
3090 * @bat_priv: the bat priv with all the soft interface information
3091 * @tt_data: tt data containing the tt request information
3092 * @resp_src: mac address of tt reply sender
3093 * @num_entries: number of tt change entries appended to the tt data
3095 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3096 struct batadv_tvlv_tt_data *tt_data,
3097 u8 *resp_src, u16 num_entries)
3099 struct batadv_tt_req_node *node;
3100 struct hlist_node *safe;
3101 struct batadv_orig_node *orig_node = NULL;
3102 struct batadv_tvlv_tt_change *tt_change;
3103 u8 *tvlv_ptr = (u8 *)tt_data;
3106 batadv_dbg(BATADV_DBG_TT, bat_priv,
3107 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3108 resp_src, tt_data->ttvn, num_entries,
3109 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3111 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3115 spin_lock_bh(&orig_node->tt_lock);
3117 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3118 change_offset *= ntohs(tt_data->num_vlan);
3119 change_offset += sizeof(*tt_data);
3120 tvlv_ptr += change_offset;
3122 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3123 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3124 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3125 resp_src, num_entries);
3127 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3128 tt_data->ttvn, tt_change);
3131 /* Recalculate the CRC for this orig_node and store it */
3132 batadv_tt_global_update_crc(bat_priv, orig_node);
3134 spin_unlock_bh(&orig_node->tt_lock);
3136 /* Delete the tt_req_node from pending tt_requests list */
3137 spin_lock_bh(&bat_priv->tt.req_list_lock);
3138 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3139 if (!batadv_compare_eth(node->addr, resp_src))
3141 hlist_del_init(&node->list);
3142 batadv_tt_req_node_put(node);
3145 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3148 batadv_orig_node_put(orig_node);
3151 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3153 struct batadv_tt_roam_node *node, *safe;
3155 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3157 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3158 list_del(&node->list);
3159 kmem_cache_free(batadv_tt_roam_cache, node);
3162 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3165 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3167 struct batadv_tt_roam_node *node, *safe;
3169 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3170 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3171 if (!batadv_has_timed_out(node->first_time,
3172 BATADV_ROAMING_MAX_TIME))
3175 list_del(&node->list);
3176 kmem_cache_free(batadv_tt_roam_cache, node);
3178 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3182 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3183 * @bat_priv: the bat priv with all the soft interface information
3184 * @client: mac address of the roaming client
3186 * This function checks whether the client already reached the
3187 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3190 * Return: true if the ROAMING_ADV can be sent, false otherwise
3192 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3194 struct batadv_tt_roam_node *tt_roam_node;
3197 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3198 /* The new tt_req will be issued only if I'm not waiting for a
3199 * reply from the same orig_node yet
3201 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3202 if (!batadv_compare_eth(tt_roam_node->addr, client))
3205 if (batadv_has_timed_out(tt_roam_node->first_time,
3206 BATADV_ROAMING_MAX_TIME))
3209 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3210 /* Sorry, you roamed too many times! */
3217 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3222 tt_roam_node->first_time = jiffies;
3223 atomic_set(&tt_roam_node->counter,
3224 BATADV_ROAMING_MAX_COUNT - 1);
3225 ether_addr_copy(tt_roam_node->addr, client);
3227 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3232 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3237 * batadv_send_roam_adv - send a roaming advertisement message
3238 * @bat_priv: the bat priv with all the soft interface information
3239 * @client: mac address of the roaming client
3240 * @vid: VLAN identifier
3241 * @orig_node: message destination
3243 * Send a ROAMING_ADV message to the node which was previously serving this
3244 * client. This is done to inform the node that from now on all traffic destined
3245 * for this particular roamed client has to be forwarded to the sender of the
3248 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3250 struct batadv_orig_node *orig_node)
3252 struct batadv_hard_iface *primary_if;
3253 struct batadv_tvlv_roam_adv tvlv_roam;
3255 primary_if = batadv_primary_if_get_selected(bat_priv);
3259 /* before going on we have to check whether the client has
3260 * already roamed to us too many times
3262 if (!batadv_tt_check_roam_count(bat_priv, client))
3265 batadv_dbg(BATADV_DBG_TT, bat_priv,
3266 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3267 orig_node->orig, client, BATADV_PRINT_VID(vid));
3269 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3271 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3272 tvlv_roam.vid = htons(vid);
3274 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3275 orig_node->orig, BATADV_TVLV_ROAM, 1,
3276 &tvlv_roam, sizeof(tvlv_roam));
3280 batadv_hardif_put(primary_if);
3283 static void batadv_tt_purge(struct work_struct *work)
3285 struct delayed_work *delayed_work;
3286 struct batadv_priv_tt *priv_tt;
3287 struct batadv_priv *bat_priv;
3289 delayed_work = to_delayed_work(work);
3290 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3291 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3293 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3294 batadv_tt_global_purge(bat_priv);
3295 batadv_tt_req_purge(bat_priv);
3296 batadv_tt_roam_purge(bat_priv);
3298 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3299 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3302 void batadv_tt_free(struct batadv_priv *bat_priv)
3304 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3305 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3307 cancel_delayed_work_sync(&bat_priv->tt.work);
3309 batadv_tt_local_table_free(bat_priv);
3310 batadv_tt_global_table_free(bat_priv);
3311 batadv_tt_req_list_free(bat_priv);
3312 batadv_tt_changes_list_free(bat_priv);
3313 batadv_tt_roam_list_free(bat_priv);
3315 kfree(bat_priv->tt.last_changeset);
3319 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3320 * table and possibly count them in the TT size
3321 * @bat_priv: the bat priv with all the soft interface information
3322 * @flags: the flag to switch
3323 * @enable: whether to set or unset the flag
3324 * @count: whether to increase the TT size by the number of changed entries
3326 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3327 bool enable, bool count)
3329 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3330 struct batadv_tt_common_entry *tt_common_entry;
3331 u16 changed_num = 0;
3332 struct hlist_head *head;
3338 for (i = 0; i < hash->size; i++) {
3339 head = &hash->table[i];
3342 hlist_for_each_entry_rcu(tt_common_entry,
3345 if ((tt_common_entry->flags & flags) == flags)
3347 tt_common_entry->flags |= flags;
3349 if (!(tt_common_entry->flags & flags))
3351 tt_common_entry->flags &= ~flags;
3358 batadv_tt_local_size_inc(bat_priv,
3359 tt_common_entry->vid);
3365 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3366 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3368 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3369 struct batadv_tt_common_entry *tt_common;
3370 struct batadv_tt_local_entry *tt_local;
3371 struct hlist_node *node_tmp;
3372 struct hlist_head *head;
3373 spinlock_t *list_lock; /* protects write access to the hash lists */
3379 for (i = 0; i < hash->size; i++) {
3380 head = &hash->table[i];
3381 list_lock = &hash->list_locks[i];
3383 spin_lock_bh(list_lock);
3384 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3386 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3389 batadv_dbg(BATADV_DBG_TT, bat_priv,
3390 "Deleting local tt entry (%pM, vid: %d): pending\n",
3392 BATADV_PRINT_VID(tt_common->vid));
3394 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3395 hlist_del_rcu(&tt_common->hash_entry);
3396 tt_local = container_of(tt_common,
3397 struct batadv_tt_local_entry,
3400 batadv_tt_local_entry_put(tt_local);
3402 spin_unlock_bh(list_lock);
3407 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3408 * which have been queued in the time since the last commit
3409 * @bat_priv: the bat priv with all the soft interface information
3411 * Caller must hold tt->commit_lock.
3413 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3415 lockdep_assert_held(&bat_priv->tt.commit_lock);
3417 /* Update multicast addresses in local translation table */
3418 batadv_mcast_mla_update(bat_priv);
3420 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3421 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3422 batadv_tt_tvlv_container_update(bat_priv);
3426 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3428 batadv_tt_local_purge_pending_clients(bat_priv);
3429 batadv_tt_local_update_crc(bat_priv);
3431 /* Increment the TTVN only once per OGM interval */
3432 atomic_inc(&bat_priv->tt.vn);
3433 batadv_dbg(BATADV_DBG_TT, bat_priv,
3434 "Local changes committed, updating to ttvn %u\n",
3435 (u8)atomic_read(&bat_priv->tt.vn));
3437 /* reset the sending counter */
3438 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3439 batadv_tt_tvlv_container_update(bat_priv);
3443 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3444 * have been queued in the time since the last commit
3445 * @bat_priv: the bat priv with all the soft interface information
3447 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3449 spin_lock_bh(&bat_priv->tt.commit_lock);
3450 batadv_tt_local_commit_changes_nolock(bat_priv);
3451 spin_unlock_bh(&bat_priv->tt.commit_lock);
3454 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3457 struct batadv_tt_local_entry *tt_local_entry = NULL;
3458 struct batadv_tt_global_entry *tt_global_entry = NULL;
3459 struct batadv_softif_vlan *vlan;
3462 vlan = batadv_softif_vlan_get(bat_priv, vid);
3466 if (!atomic_read(&vlan->ap_isolation))
3469 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3470 if (!tt_local_entry)
3473 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3474 if (!tt_global_entry)
3477 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3483 batadv_softif_vlan_put(vlan);
3484 if (tt_global_entry)
3485 batadv_tt_global_entry_put(tt_global_entry);
3487 batadv_tt_local_entry_put(tt_local_entry);
3492 * batadv_tt_update_orig - update global translation table with new tt
3493 * information received via ogms
3494 * @bat_priv: the bat priv with all the soft interface information
3495 * @orig_node: the orig_node of the ogm
3496 * @tt_buff: pointer to the first tvlv VLAN entry
3497 * @tt_num_vlan: number of tvlv VLAN entries
3498 * @tt_change: pointer to the first entry in the TT buffer
3499 * @tt_num_changes: number of tt changes inside the tt buffer
3500 * @ttvn: translation table version number of this changeset
3502 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3503 struct batadv_orig_node *orig_node,
3504 const void *tt_buff, u16 tt_num_vlan,
3505 struct batadv_tvlv_tt_change *tt_change,
3506 u16 tt_num_changes, u8 ttvn)
3508 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3509 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3510 bool full_table = true;
3513 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3514 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3515 &orig_node->capa_initialized);
3517 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3518 * increased by one -> we can apply the attached changes
3520 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3521 /* the OGM could not contain the changes due to their size or
3522 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3524 * In this case send a tt request
3526 if (!tt_num_changes) {
3531 spin_lock_bh(&orig_node->tt_lock);
3533 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3536 /* Even if we received the precomputed crc with the OGM, we
3537 * prefer to recompute it to spot any possible inconsistency
3538 * in the global table
3540 batadv_tt_global_update_crc(bat_priv, orig_node);
3542 spin_unlock_bh(&orig_node->tt_lock);
3544 /* The ttvn alone is not enough to guarantee consistency
3545 * because a single value could represent different states
3546 * (due to the wrap around). Thus a node has to check whether
3547 * the resulting table (after applying the changes) is still
3548 * consistent or not. E.g. a node could disconnect while its
3549 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3550 * checking the CRC value is mandatory to detect the
3553 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3557 /* if we missed more than one change or our tables are not
3558 * in sync anymore -> request fresh tt data
3560 if (!has_tt_init || ttvn != orig_ttvn ||
3561 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3564 batadv_dbg(BATADV_DBG_TT, bat_priv,
3565 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3566 orig_node->orig, ttvn, orig_ttvn,
3568 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3569 tt_vlan, tt_num_vlan,
3577 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3578 * @bat_priv: the bat priv with all the soft interface information
3579 * @addr: the mac address of the client to check
3580 * @vid: VLAN identifier
3582 * Return: true if we know that the client has moved from its old originator
3583 * to another one. This entry is still kept for consistency purposes and will be
3584 * deleted later by a DEL or because of timeout
3586 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3587 u8 *addr, unsigned short vid)
3589 struct batadv_tt_global_entry *tt_global_entry;
3592 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3593 if (!tt_global_entry)
3596 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3597 batadv_tt_global_entry_put(tt_global_entry);
3603 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3604 * @bat_priv: the bat priv with all the soft interface information
3605 * @addr: the mac address of the local client to query
3606 * @vid: VLAN identifier
3608 * Return: true if the local client is known to be roaming (it is not served by
3609 * this node anymore) or not. If yes, the client is still present in the table
3610 * to keep the latter consistent with the node TTVN
3612 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3613 u8 *addr, unsigned short vid)
3615 struct batadv_tt_local_entry *tt_local_entry;
3618 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3619 if (!tt_local_entry)
3622 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3623 batadv_tt_local_entry_put(tt_local_entry);
3628 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3629 struct batadv_orig_node *orig_node,
3630 const unsigned char *addr,
3635 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3636 BATADV_TT_CLIENT_TEMP,
3637 atomic_read(&orig_node->last_ttvn)))
3640 batadv_dbg(BATADV_DBG_TT, bat_priv,
3641 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3642 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3649 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3650 * maximum packet size that can be transported through the mesh
3651 * @soft_iface: netdev struct of the mesh interface
3653 * Remove entries older than 'timeout' and half timeout if more entries need
3656 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3658 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3659 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3660 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3661 bool reduced = false;
3663 spin_lock_bh(&bat_priv->tt.commit_lock);
3666 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3667 if (packet_size_max >= table_size)
3670 batadv_tt_local_purge(bat_priv, timeout);
3671 batadv_tt_local_purge_pending_clients(bat_priv);
3675 net_ratelimited_function(batadv_info, soft_iface,
3676 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3680 /* commit these changes immediately, to avoid synchronization problem
3684 batadv_tt_local_commit_changes_nolock(bat_priv);
3686 spin_unlock_bh(&bat_priv->tt.commit_lock);
3690 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3691 * @bat_priv: the bat priv with all the soft interface information
3692 * @orig: the orig_node of the ogm
3693 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3694 * @tvlv_value: tvlv buffer containing the gateway data
3695 * @tvlv_value_len: tvlv buffer length
3697 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3698 struct batadv_orig_node *orig,
3699 u8 flags, void *tvlv_value,
3702 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3703 struct batadv_tvlv_tt_change *tt_change;
3704 struct batadv_tvlv_tt_data *tt_data;
3705 u16 num_entries, num_vlan;
3707 if (tvlv_value_len < sizeof(*tt_data))
3710 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3711 tvlv_value_len -= sizeof(*tt_data);
3713 num_vlan = ntohs(tt_data->num_vlan);
3715 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3718 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3719 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3720 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3722 num_entries = batadv_tt_entries(tvlv_value_len);
3724 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3725 num_entries, tt_data->ttvn);
3729 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3731 * @bat_priv: the bat priv with all the soft interface information
3732 * @src: mac address of tt tvlv sender
3733 * @dst: mac address of tt tvlv recipient
3734 * @tvlv_value: tvlv buffer containing the tt data
3735 * @tvlv_value_len: tvlv buffer length
3737 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3740 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3745 struct batadv_tvlv_tt_data *tt_data;
3746 u16 tt_vlan_len, tt_num_entries;
3750 if (tvlv_value_len < sizeof(*tt_data))
3751 return NET_RX_SUCCESS;
3753 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3754 tvlv_value_len -= sizeof(*tt_data);
3756 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3757 tt_vlan_len *= ntohs(tt_data->num_vlan);
3759 if (tvlv_value_len < tt_vlan_len)
3760 return NET_RX_SUCCESS;
3762 tvlv_value_len -= tt_vlan_len;
3763 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3765 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3766 case BATADV_TT_REQUEST:
3767 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3769 /* If this node cannot provide a TT response the tt_request is
3772 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3774 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3779 batadv_dbg(BATADV_DBG_TT, bat_priv,
3780 "Routing TT_REQUEST to %pM [%c]\n",
3782 /* tvlv API will re-route the packet */
3786 case BATADV_TT_RESPONSE:
3787 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3789 if (batadv_is_my_mac(bat_priv, dst)) {
3790 batadv_handle_tt_response(bat_priv, tt_data,
3791 src, tt_num_entries);
3792 return NET_RX_SUCCESS;
3795 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3800 batadv_dbg(BATADV_DBG_TT, bat_priv,
3801 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3803 /* tvlv API will re-route the packet */
3807 return NET_RX_SUCCESS;
3811 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3812 * @bat_priv: the bat priv with all the soft interface information
3813 * @src: mac address of tt tvlv sender
3814 * @dst: mac address of tt tvlv recipient
3815 * @tvlv_value: tvlv buffer containing the tt data
3816 * @tvlv_value_len: tvlv buffer length
3818 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3821 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3826 struct batadv_tvlv_roam_adv *roaming_adv;
3827 struct batadv_orig_node *orig_node = NULL;
3829 /* If this node is not the intended recipient of the
3830 * roaming advertisement the packet is forwarded
3831 * (the tvlv API will re-route the packet).
3833 if (!batadv_is_my_mac(bat_priv, dst))
3836 if (tvlv_value_len < sizeof(*roaming_adv))
3839 orig_node = batadv_orig_hash_find(bat_priv, src);
3843 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3844 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3846 batadv_dbg(BATADV_DBG_TT, bat_priv,
3847 "Received ROAMING_ADV from %pM (client %pM)\n",
3848 src, roaming_adv->client);
3850 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3851 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3852 atomic_read(&orig_node->last_ttvn) + 1);
3856 batadv_orig_node_put(orig_node);
3857 return NET_RX_SUCCESS;
3861 * batadv_tt_init - initialise the translation table internals
3862 * @bat_priv: the bat priv with all the soft interface information
3864 * Return: 0 on success or negative error number in case of failure.
3866 int batadv_tt_init(struct batadv_priv *bat_priv)
3870 /* synchronized flags must be remote */
3871 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3873 ret = batadv_tt_local_init(bat_priv);
3877 ret = batadv_tt_global_init(bat_priv);
3881 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3882 batadv_tt_tvlv_unicast_handler_v1,
3883 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3885 batadv_tvlv_handler_register(bat_priv, NULL,
3886 batadv_roam_tvlv_unicast_handler_v1,
3887 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3889 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3890 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3891 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3897 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3898 * @bat_priv: the bat priv with all the soft interface information
3899 * @addr: the mac address of the client
3900 * @vid: the identifier of the VLAN where this client is connected
3902 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3905 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3906 const u8 *addr, unsigned short vid)
3908 struct batadv_tt_global_entry *tt;
3911 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3915 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3917 batadv_tt_global_entry_put(tt);
3923 * batadv_tt_cache_init - Initialize tt memory object cache
3925 * Return: 0 on success or negative error number in case of failure.
3927 int __init batadv_tt_cache_init(void)
3929 size_t tl_size = sizeof(struct batadv_tt_local_entry);
3930 size_t tg_size = sizeof(struct batadv_tt_global_entry);
3931 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
3932 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
3933 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
3934 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
3936 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
3937 SLAB_HWCACHE_ALIGN, NULL);
3938 if (!batadv_tl_cache)
3941 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
3942 SLAB_HWCACHE_ALIGN, NULL);
3943 if (!batadv_tg_cache)
3944 goto err_tt_tl_destroy;
3946 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
3948 SLAB_HWCACHE_ALIGN, NULL);
3949 if (!batadv_tt_orig_cache)
3950 goto err_tt_tg_destroy;
3952 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
3954 SLAB_HWCACHE_ALIGN, NULL);
3955 if (!batadv_tt_change_cache)
3956 goto err_tt_orig_destroy;
3958 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
3960 SLAB_HWCACHE_ALIGN, NULL);
3961 if (!batadv_tt_req_cache)
3962 goto err_tt_change_destroy;
3964 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
3966 SLAB_HWCACHE_ALIGN, NULL);
3967 if (!batadv_tt_roam_cache)
3968 goto err_tt_req_destroy;
3973 kmem_cache_destroy(batadv_tt_req_cache);
3974 batadv_tt_req_cache = NULL;
3975 err_tt_change_destroy:
3976 kmem_cache_destroy(batadv_tt_change_cache);
3977 batadv_tt_change_cache = NULL;
3978 err_tt_orig_destroy:
3979 kmem_cache_destroy(batadv_tt_orig_cache);
3980 batadv_tt_orig_cache = NULL;
3982 kmem_cache_destroy(batadv_tg_cache);
3983 batadv_tg_cache = NULL;
3985 kmem_cache_destroy(batadv_tl_cache);
3986 batadv_tl_cache = NULL;
3992 * batadv_tt_cache_destroy - Destroy tt memory object cache
3994 void batadv_tt_cache_destroy(void)
3996 kmem_cache_destroy(batadv_tl_cache);
3997 kmem_cache_destroy(batadv_tg_cache);
3998 kmem_cache_destroy(batadv_tt_orig_cache);
3999 kmem_cache_destroy(batadv_tt_change_cache);
4000 kmem_cache_destroy(batadv_tt_req_cache);
4001 kmem_cache_destroy(batadv_tt_roam_cache);