xref: /linux/net/batman-adv/translation-table.c (revision ee8287e068a3995b0f8001dd6931e221dfb7c530)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5  */
6 
7 #include "translation-table.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/build_bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/cache.h>
15 #include <linux/compiler.h>
16 #include <linux/container_of.h>
17 #include <linux/crc32c.h>
18 #include <linux/errno.h>
19 #include <linux/etherdevice.h>
20 #include <linux/gfp.h>
21 #include <linux/if_ether.h>
22 #include <linux/init.h>
23 #include <linux/jhash.h>
24 #include <linux/jiffies.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/net.h>
29 #include <linux/netdevice.h>
30 #include <linux/netlink.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/skbuff.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/workqueue.h>
39 #include <net/genetlink.h>
40 #include <net/netlink.h>
41 #include <net/sock.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
44 
45 #include "bridge_loop_avoidance.h"
46 #include "hard-interface.h"
47 #include "hash.h"
48 #include "log.h"
49 #include "netlink.h"
50 #include "originator.h"
51 #include "soft-interface.h"
52 #include "tvlv.h"
53 
54 static struct kmem_cache *batadv_tl_cache __read_mostly;
55 static struct kmem_cache *batadv_tg_cache __read_mostly;
56 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
57 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
58 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
59 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
60 
61 /* hash class keys */
62 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
63 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
64 
65 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
66 				 unsigned short vid,
67 				 struct batadv_orig_node *orig_node);
68 static void batadv_tt_purge(struct work_struct *work);
69 static void
70 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
71 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
72 				 struct batadv_orig_node *orig_node,
73 				 const unsigned char *addr,
74 				 unsigned short vid, const char *message,
75 				 bool roaming);
76 
77 /**
78  * batadv_compare_tt() - check if two TT entries are the same
79  * @node: the list element pointer of the first TT entry
80  * @data2: pointer to the tt_common_entry of the second TT entry
81  *
82  * Compare the MAC address and the VLAN ID of the two TT entries and check if
83  * they are the same TT client.
84  * Return: true if the two TT clients are the same, false otherwise
85  */
86 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
87 {
88 	const void *data1 = container_of(node, struct batadv_tt_common_entry,
89 					 hash_entry);
90 	const struct batadv_tt_common_entry *tt1 = data1;
91 	const struct batadv_tt_common_entry *tt2 = data2;
92 
93 	return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
94 }
95 
96 /**
97  * batadv_choose_tt() - return the index of the tt entry in the hash table
98  * @data: pointer to the tt_common_entry object to map
99  * @size: the size of the hash table
100  *
101  * Return: the hash index where the object represented by 'data' should be
102  * stored at.
103  */
104 static inline u32 batadv_choose_tt(const void *data, u32 size)
105 {
106 	const struct batadv_tt_common_entry *tt;
107 	u32 hash = 0;
108 
109 	tt = data;
110 	hash = jhash(&tt->addr, ETH_ALEN, hash);
111 	hash = jhash(&tt->vid, sizeof(tt->vid), hash);
112 
113 	return hash % size;
114 }
115 
116 /**
117  * batadv_tt_hash_find() - look for a client in the given hash table
118  * @hash: the hash table to search
119  * @addr: the mac address of the client to look for
120  * @vid: VLAN identifier
121  *
122  * Return: a pointer to the tt_common struct belonging to the searched client if
123  * found, NULL otherwise.
124  */
125 static struct batadv_tt_common_entry *
126 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
127 		    unsigned short vid)
128 {
129 	struct hlist_head *head;
130 	struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
131 	u32 index;
132 
133 	if (!hash)
134 		return NULL;
135 
136 	ether_addr_copy(to_search.addr, addr);
137 	to_search.vid = vid;
138 
139 	index = batadv_choose_tt(&to_search, hash->size);
140 	head = &hash->table[index];
141 
142 	rcu_read_lock();
143 	hlist_for_each_entry_rcu(tt, head, hash_entry) {
144 		if (!batadv_compare_eth(tt, addr))
145 			continue;
146 
147 		if (tt->vid != vid)
148 			continue;
149 
150 		if (!kref_get_unless_zero(&tt->refcount))
151 			continue;
152 
153 		tt_tmp = tt;
154 		break;
155 	}
156 	rcu_read_unlock();
157 
158 	return tt_tmp;
159 }
160 
161 /**
162  * batadv_tt_local_hash_find() - search the local table for a given client
163  * @bat_priv: the bat priv with all the soft interface information
164  * @addr: the mac address of the client to look for
165  * @vid: VLAN identifier
166  *
167  * Return: a pointer to the corresponding tt_local_entry struct if the client is
168  * found, NULL otherwise.
169  */
170 static struct batadv_tt_local_entry *
171 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
172 			  unsigned short vid)
173 {
174 	struct batadv_tt_common_entry *tt_common_entry;
175 	struct batadv_tt_local_entry *tt_local_entry = NULL;
176 
177 	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
178 					      vid);
179 	if (tt_common_entry)
180 		tt_local_entry = container_of(tt_common_entry,
181 					      struct batadv_tt_local_entry,
182 					      common);
183 	return tt_local_entry;
184 }
185 
186 /**
187  * batadv_tt_global_hash_find() - search the global table for a given client
188  * @bat_priv: the bat priv with all the soft interface information
189  * @addr: the mac address of the client to look for
190  * @vid: VLAN identifier
191  *
192  * Return: a pointer to the corresponding tt_global_entry struct if the client
193  * is found, NULL otherwise.
194  */
195 struct batadv_tt_global_entry *
196 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
197 			   unsigned short vid)
198 {
199 	struct batadv_tt_common_entry *tt_common_entry;
200 	struct batadv_tt_global_entry *tt_global_entry = NULL;
201 
202 	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
203 					      vid);
204 	if (tt_common_entry)
205 		tt_global_entry = container_of(tt_common_entry,
206 					       struct batadv_tt_global_entry,
207 					       common);
208 	return tt_global_entry;
209 }
210 
211 /**
212  * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
213  *  for free after rcu grace period
214  * @ref: kref pointer of the nc_node
215  */
216 static void batadv_tt_local_entry_release(struct kref *ref)
217 {
218 	struct batadv_tt_local_entry *tt_local_entry;
219 
220 	tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
221 				      common.refcount);
222 
223 	batadv_softif_vlan_put(tt_local_entry->vlan);
224 
225 	kfree_rcu(tt_local_entry, common.rcu);
226 }
227 
228 /**
229  * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
230  *  possibly release it
231  * @tt_local_entry: tt_local_entry to be free'd
232  */
233 static void
234 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
235 {
236 	if (!tt_local_entry)
237 		return;
238 
239 	kref_put(&tt_local_entry->common.refcount,
240 		 batadv_tt_local_entry_release);
241 }
242 
243 /**
244  * batadv_tt_global_entry_release() - release tt_global_entry from lists and
245  *  queue for free after rcu grace period
246  * @ref: kref pointer of the nc_node
247  */
248 void batadv_tt_global_entry_release(struct kref *ref)
249 {
250 	struct batadv_tt_global_entry *tt_global_entry;
251 
252 	tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
253 				       common.refcount);
254 
255 	batadv_tt_global_del_orig_list(tt_global_entry);
256 
257 	kfree_rcu(tt_global_entry, common.rcu);
258 }
259 
260 /**
261  * batadv_tt_global_hash_count() - count the number of orig entries
262  * @bat_priv: the bat priv with all the soft interface information
263  * @addr: the mac address of the client to count entries for
264  * @vid: VLAN identifier
265  *
266  * Return: the number of originators advertising the given address/data
267  * (excluding our self).
268  */
269 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
270 				const u8 *addr, unsigned short vid)
271 {
272 	struct batadv_tt_global_entry *tt_global_entry;
273 	int count;
274 
275 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
276 	if (!tt_global_entry)
277 		return 0;
278 
279 	count = atomic_read(&tt_global_entry->orig_list_count);
280 	batadv_tt_global_entry_put(tt_global_entry);
281 
282 	return count;
283 }
284 
285 /**
286  * batadv_tt_local_size_mod() - change the size by v of the local table
287  *  identified by vid
288  * @bat_priv: the bat priv with all the soft interface information
289  * @vid: the VLAN identifier of the sub-table to change
290  * @v: the amount to sum to the local table size
291  */
292 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
293 				     unsigned short vid, int v)
294 {
295 	struct batadv_softif_vlan *vlan;
296 
297 	vlan = batadv_softif_vlan_get(bat_priv, vid);
298 	if (!vlan)
299 		return;
300 
301 	atomic_add(v, &vlan->tt.num_entries);
302 
303 	batadv_softif_vlan_put(vlan);
304 }
305 
306 /**
307  * batadv_tt_local_size_inc() - increase by one the local table size for the
308  *  given vid
309  * @bat_priv: the bat priv with all the soft interface information
310  * @vid: the VLAN identifier
311  */
312 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
313 				     unsigned short vid)
314 {
315 	batadv_tt_local_size_mod(bat_priv, vid, 1);
316 }
317 
318 /**
319  * batadv_tt_local_size_dec() - decrease by one the local table size for the
320  *  given vid
321  * @bat_priv: the bat priv with all the soft interface information
322  * @vid: the VLAN identifier
323  */
324 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
325 				     unsigned short vid)
326 {
327 	batadv_tt_local_size_mod(bat_priv, vid, -1);
328 }
329 
330 /**
331  * batadv_tt_global_size_mod() - change the size by v of the global table
332  *  for orig_node identified by vid
333  * @orig_node: the originator for which the table has to be modified
334  * @vid: the VLAN identifier
335  * @v: the amount to sum to the global table size
336  */
337 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
338 				      unsigned short vid, int v)
339 {
340 	struct batadv_orig_node_vlan *vlan;
341 
342 	vlan = batadv_orig_node_vlan_new(orig_node, vid);
343 	if (!vlan)
344 		return;
345 
346 	if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
347 		spin_lock_bh(&orig_node->vlan_list_lock);
348 		if (!hlist_unhashed(&vlan->list)) {
349 			hlist_del_init_rcu(&vlan->list);
350 			batadv_orig_node_vlan_put(vlan);
351 		}
352 		spin_unlock_bh(&orig_node->vlan_list_lock);
353 	}
354 
355 	batadv_orig_node_vlan_put(vlan);
356 }
357 
358 /**
359  * batadv_tt_global_size_inc() - increase by one the global table size for the
360  *  given vid
361  * @orig_node: the originator which global table size has to be decreased
362  * @vid: the vlan identifier
363  */
364 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
365 				      unsigned short vid)
366 {
367 	batadv_tt_global_size_mod(orig_node, vid, 1);
368 }
369 
370 /**
371  * batadv_tt_global_size_dec() - decrease by one the global table size for the
372  *  given vid
373  * @orig_node: the originator which global table size has to be decreased
374  * @vid: the vlan identifier
375  */
376 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
377 				      unsigned short vid)
378 {
379 	batadv_tt_global_size_mod(orig_node, vid, -1);
380 }
381 
382 /**
383  * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
384  *  queue for free after rcu grace period
385  * @ref: kref pointer of the tt orig entry
386  */
387 static void batadv_tt_orig_list_entry_release(struct kref *ref)
388 {
389 	struct batadv_tt_orig_list_entry *orig_entry;
390 
391 	orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
392 				  refcount);
393 
394 	batadv_orig_node_put(orig_entry->orig_node);
395 	kfree_rcu(orig_entry, rcu);
396 }
397 
398 /**
399  * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
400  *  possibly release it
401  * @orig_entry: tt orig entry to be free'd
402  */
403 static void
404 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
405 {
406 	if (!orig_entry)
407 		return;
408 
409 	kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
410 }
411 
412 /**
413  * batadv_tt_local_event() - store a local TT event (ADD/DEL)
414  * @bat_priv: the bat priv with all the soft interface information
415  * @tt_local_entry: the TT entry involved in the event
416  * @event_flags: flags to store in the event structure
417  */
418 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
419 				  struct batadv_tt_local_entry *tt_local_entry,
420 				  u8 event_flags)
421 {
422 	struct batadv_tt_change_node *tt_change_node, *entry, *safe;
423 	struct batadv_tt_common_entry *common = &tt_local_entry->common;
424 	u8 flags = common->flags | event_flags;
425 	bool event_removed = false;
426 	bool del_op_requested, del_op_entry;
427 
428 	tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
429 	if (!tt_change_node)
430 		return;
431 
432 	tt_change_node->change.flags = flags;
433 	memset(tt_change_node->change.reserved, 0,
434 	       sizeof(tt_change_node->change.reserved));
435 	ether_addr_copy(tt_change_node->change.addr, common->addr);
436 	tt_change_node->change.vid = htons(common->vid);
437 
438 	del_op_requested = flags & BATADV_TT_CLIENT_DEL;
439 
440 	/* check for ADD+DEL or DEL+ADD events */
441 	spin_lock_bh(&bat_priv->tt.changes_list_lock);
442 	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
443 				 list) {
444 		if (!batadv_compare_eth(entry->change.addr, common->addr))
445 			continue;
446 
447 		/* DEL+ADD in the same orig interval have no effect and can be
448 		 * removed to avoid silly behaviour on the receiver side. The
449 		 * other way around (ADD+DEL) can happen in case of roaming of
450 		 * a client still in the NEW state. Roaming of NEW clients is
451 		 * now possible due to automatically recognition of "temporary"
452 		 * clients
453 		 */
454 		del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
455 		if (!del_op_requested && del_op_entry)
456 			goto del;
457 		if (del_op_requested && !del_op_entry)
458 			goto del;
459 
460 		/* this is a second add in the same originator interval. It
461 		 * means that flags have been changed: update them!
462 		 */
463 		if (!del_op_requested && !del_op_entry)
464 			entry->change.flags = flags;
465 
466 		continue;
467 del:
468 		list_del(&entry->list);
469 		kmem_cache_free(batadv_tt_change_cache, entry);
470 		kmem_cache_free(batadv_tt_change_cache, tt_change_node);
471 		event_removed = true;
472 		goto unlock;
473 	}
474 
475 	/* track the change in the OGMinterval list */
476 	list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
477 
478 unlock:
479 	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
480 
481 	if (event_removed)
482 		atomic_dec(&bat_priv->tt.local_changes);
483 	else
484 		atomic_inc(&bat_priv->tt.local_changes);
485 }
486 
487 /**
488  * batadv_tt_len() - compute length in bytes of given number of tt changes
489  * @changes_num: number of tt changes
490  *
491  * Return: computed length in bytes.
492  */
493 static int batadv_tt_len(int changes_num)
494 {
495 	return changes_num * sizeof(struct batadv_tvlv_tt_change);
496 }
497 
498 /**
499  * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
500  * @tt_len: available space
501  *
502  * Return: the number of entries.
503  */
504 static u16 batadv_tt_entries(u16 tt_len)
505 {
506 	return tt_len / batadv_tt_len(1);
507 }
508 
509 /**
510  * batadv_tt_local_table_transmit_size() - calculates the local translation
511  *  table size when transmitted over the air
512  * @bat_priv: the bat priv with all the soft interface information
513  *
514  * Return: local translation table size in bytes.
515  */
516 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
517 {
518 	u16 num_vlan = 0;
519 	u16 tt_local_entries = 0;
520 	struct batadv_softif_vlan *vlan;
521 	int hdr_size;
522 
523 	rcu_read_lock();
524 	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
525 		num_vlan++;
526 		tt_local_entries += atomic_read(&vlan->tt.num_entries);
527 	}
528 	rcu_read_unlock();
529 
530 	/* header size of tvlv encapsulated tt response payload */
531 	hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
532 	hdr_size += sizeof(struct batadv_tvlv_hdr);
533 	hdr_size += sizeof(struct batadv_tvlv_tt_data);
534 	hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
535 
536 	return hdr_size + batadv_tt_len(tt_local_entries);
537 }
538 
539 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
540 {
541 	if (bat_priv->tt.local_hash)
542 		return 0;
543 
544 	bat_priv->tt.local_hash = batadv_hash_new(1024);
545 
546 	if (!bat_priv->tt.local_hash)
547 		return -ENOMEM;
548 
549 	batadv_hash_set_lock_class(bat_priv->tt.local_hash,
550 				   &batadv_tt_local_hash_lock_class_key);
551 
552 	return 0;
553 }
554 
555 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
556 				  struct batadv_tt_global_entry *tt_global,
557 				  const char *message)
558 {
559 	struct batadv_tt_global_entry *tt_removed_entry;
560 	struct hlist_node *tt_removed_node;
561 
562 	batadv_dbg(BATADV_DBG_TT, bat_priv,
563 		   "Deleting global tt entry %pM (vid: %d): %s\n",
564 		   tt_global->common.addr,
565 		   batadv_print_vid(tt_global->common.vid), message);
566 
567 	tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
568 					     batadv_compare_tt,
569 					     batadv_choose_tt,
570 					     &tt_global->common);
571 	if (!tt_removed_node)
572 		return;
573 
574 	/* drop reference of remove hash entry */
575 	tt_removed_entry = hlist_entry(tt_removed_node,
576 				       struct batadv_tt_global_entry,
577 				       common.hash_entry);
578 	batadv_tt_global_entry_put(tt_removed_entry);
579 }
580 
581 /**
582  * batadv_tt_local_add() - add a new client to the local table or update an
583  *  existing client
584  * @soft_iface: netdev struct of the mesh interface
585  * @addr: the mac address of the client to add
586  * @vid: VLAN identifier
587  * @ifindex: index of the interface where the client is connected to (useful to
588  *  identify wireless clients)
589  * @mark: the value contained in the skb->mark field of the received packet (if
590  *  any)
591  *
592  * Return: true if the client was successfully added, false otherwise.
593  */
594 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
595 			 unsigned short vid, int ifindex, u32 mark)
596 {
597 	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
598 	struct batadv_tt_local_entry *tt_local;
599 	struct batadv_tt_global_entry *tt_global = NULL;
600 	struct net *net = dev_net(soft_iface);
601 	struct batadv_softif_vlan *vlan;
602 	struct net_device *in_dev = NULL;
603 	struct batadv_hard_iface *in_hardif = NULL;
604 	struct hlist_head *head;
605 	struct batadv_tt_orig_list_entry *orig_entry;
606 	int hash_added, table_size, packet_size_max;
607 	bool ret = false;
608 	bool roamed_back = false;
609 	u8 remote_flags;
610 	u32 match_mark;
611 
612 	if (ifindex != BATADV_NULL_IFINDEX)
613 		in_dev = dev_get_by_index(net, ifindex);
614 
615 	if (in_dev)
616 		in_hardif = batadv_hardif_get_by_netdev(in_dev);
617 
618 	tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
619 
620 	if (!is_multicast_ether_addr(addr))
621 		tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
622 
623 	if (tt_local) {
624 		tt_local->last_seen = jiffies;
625 		if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
626 			batadv_dbg(BATADV_DBG_TT, bat_priv,
627 				   "Re-adding pending client %pM (vid: %d)\n",
628 				   addr, batadv_print_vid(vid));
629 			/* whatever the reason why the PENDING flag was set,
630 			 * this is a client which was enqueued to be removed in
631 			 * this orig_interval. Since it popped up again, the
632 			 * flag can be reset like it was never enqueued
633 			 */
634 			tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
635 			goto add_event;
636 		}
637 
638 		if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
639 			batadv_dbg(BATADV_DBG_TT, bat_priv,
640 				   "Roaming client %pM (vid: %d) came back to its original location\n",
641 				   addr, batadv_print_vid(vid));
642 			/* the ROAM flag is set because this client roamed away
643 			 * and the node got a roaming_advertisement message. Now
644 			 * that the client popped up again at its original
645 			 * location such flag can be unset
646 			 */
647 			tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
648 			roamed_back = true;
649 		}
650 		goto check_roaming;
651 	}
652 
653 	/* Ignore the client if we cannot send it in a full table response. */
654 	table_size = batadv_tt_local_table_transmit_size(bat_priv);
655 	table_size += batadv_tt_len(1);
656 	packet_size_max = atomic_read(&bat_priv->packet_size_max);
657 	if (table_size > packet_size_max) {
658 		net_ratelimited_function(batadv_info, soft_iface,
659 					 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
660 					 table_size, packet_size_max, addr);
661 		goto out;
662 	}
663 
664 	tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
665 	if (!tt_local)
666 		goto out;
667 
668 	/* increase the refcounter of the related vlan */
669 	vlan = batadv_softif_vlan_get(bat_priv, vid);
670 	if (!vlan) {
671 		net_ratelimited_function(batadv_info, soft_iface,
672 					 "adding TT local entry %pM to non-existent VLAN %d\n",
673 					 addr, batadv_print_vid(vid));
674 		kmem_cache_free(batadv_tl_cache, tt_local);
675 		tt_local = NULL;
676 		goto out;
677 	}
678 
679 	batadv_dbg(BATADV_DBG_TT, bat_priv,
680 		   "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
681 		   addr, batadv_print_vid(vid),
682 		   (u8)atomic_read(&bat_priv->tt.vn));
683 
684 	ether_addr_copy(tt_local->common.addr, addr);
685 	/* The local entry has to be marked as NEW to avoid to send it in
686 	 * a full table response going out before the next ttvn increment
687 	 * (consistency check)
688 	 */
689 	tt_local->common.flags = BATADV_TT_CLIENT_NEW;
690 	tt_local->common.vid = vid;
691 	if (batadv_is_wifi_hardif(in_hardif))
692 		tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
693 	kref_init(&tt_local->common.refcount);
694 	tt_local->last_seen = jiffies;
695 	tt_local->common.added_at = tt_local->last_seen;
696 	tt_local->vlan = vlan;
697 
698 	/* the batman interface mac and multicast addresses should never be
699 	 * purged
700 	 */
701 	if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
702 	    is_multicast_ether_addr(addr))
703 		tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
704 
705 	kref_get(&tt_local->common.refcount);
706 	hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
707 				     batadv_choose_tt, &tt_local->common,
708 				     &tt_local->common.hash_entry);
709 
710 	if (unlikely(hash_added != 0)) {
711 		/* remove the reference for the hash */
712 		batadv_tt_local_entry_put(tt_local);
713 		goto out;
714 	}
715 
716 add_event:
717 	batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
718 
719 check_roaming:
720 	/* Check whether it is a roaming, but don't do anything if the roaming
721 	 * process has already been handled
722 	 */
723 	if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
724 		/* These node are probably going to update their tt table */
725 		head = &tt_global->orig_list;
726 		rcu_read_lock();
727 		hlist_for_each_entry_rcu(orig_entry, head, list) {
728 			batadv_send_roam_adv(bat_priv, tt_global->common.addr,
729 					     tt_global->common.vid,
730 					     orig_entry->orig_node);
731 		}
732 		rcu_read_unlock();
733 		if (roamed_back) {
734 			batadv_tt_global_free(bat_priv, tt_global,
735 					      "Roaming canceled");
736 		} else {
737 			/* The global entry has to be marked as ROAMING and
738 			 * has to be kept for consistency purpose
739 			 */
740 			tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
741 			tt_global->roam_at = jiffies;
742 		}
743 	}
744 
745 	/* store the current remote flags before altering them. This helps
746 	 * understanding is flags are changing or not
747 	 */
748 	remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
749 
750 	if (batadv_is_wifi_hardif(in_hardif))
751 		tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
752 	else
753 		tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
754 
755 	/* check the mark in the skb: if it's equal to the configured
756 	 * isolation_mark, it means the packet is coming from an isolated
757 	 * non-mesh client
758 	 */
759 	match_mark = (mark & bat_priv->isolation_mark_mask);
760 	if (bat_priv->isolation_mark_mask &&
761 	    match_mark == bat_priv->isolation_mark)
762 		tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
763 	else
764 		tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
765 
766 	/* if any "dynamic" flag has been modified, resend an ADD event for this
767 	 * entry so that all the nodes can get the new flags
768 	 */
769 	if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
770 		batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
771 
772 	ret = true;
773 out:
774 	batadv_hardif_put(in_hardif);
775 	dev_put(in_dev);
776 	batadv_tt_local_entry_put(tt_local);
777 	batadv_tt_global_entry_put(tt_global);
778 	return ret;
779 }
780 
781 /**
782  * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
783  *  within a TT Response directed to another node
784  * @orig_node: originator for which the TT data has to be prepared
785  * @tt_data: uninitialised pointer to the address of the TVLV buffer
786  * @tt_change: uninitialised pointer to the address of the area where the TT
787  *  changed can be stored
788  * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
789  *  function reserves the amount of space needed to send the entire global TT
790  *  table. In case of success the value is updated with the real amount of
791  *  reserved bytes
792  * Allocate the needed amount of memory for the entire TT TVLV and write its
793  * header made up of one tvlv_tt_data object and a series of tvlv_tt_vlan_data
794  * objects, one per active VLAN served by the originator node.
795  *
796  * Return: the size of the allocated buffer or 0 in case of failure.
797  */
798 static u16
799 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
800 				   struct batadv_tvlv_tt_data **tt_data,
801 				   struct batadv_tvlv_tt_change **tt_change,
802 				   s32 *tt_len)
803 {
804 	u16 num_vlan = 0;
805 	u16 num_entries = 0;
806 	u16 change_offset;
807 	u16 tvlv_len;
808 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
809 	struct batadv_orig_node_vlan *vlan;
810 	u8 *tt_change_ptr;
811 
812 	spin_lock_bh(&orig_node->vlan_list_lock);
813 	hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
814 		num_vlan++;
815 		num_entries += atomic_read(&vlan->tt.num_entries);
816 	}
817 
818 	change_offset = sizeof(**tt_data);
819 	change_offset += num_vlan * sizeof(*tt_vlan);
820 
821 	/* if tt_len is negative, allocate the space needed by the full table */
822 	if (*tt_len < 0)
823 		*tt_len = batadv_tt_len(num_entries);
824 
825 	tvlv_len = *tt_len;
826 	tvlv_len += change_offset;
827 
828 	*tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
829 	if (!*tt_data) {
830 		*tt_len = 0;
831 		goto out;
832 	}
833 
834 	(*tt_data)->flags = BATADV_NO_FLAGS;
835 	(*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
836 	(*tt_data)->num_vlan = htons(num_vlan);
837 
838 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
839 	hlist_for_each_entry(vlan, &orig_node->vlan_list, list) {
840 		tt_vlan->vid = htons(vlan->vid);
841 		tt_vlan->crc = htonl(vlan->tt.crc);
842 		tt_vlan->reserved = 0;
843 
844 		tt_vlan++;
845 	}
846 
847 	tt_change_ptr = (u8 *)*tt_data + change_offset;
848 	*tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
849 
850 out:
851 	spin_unlock_bh(&orig_node->vlan_list_lock);
852 	return tvlv_len;
853 }
854 
855 /**
856  * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
857  *  this node
858  * @bat_priv: the bat priv with all the soft interface information
859  * @tt_data: uninitialised pointer to the address of the TVLV buffer
860  * @tt_change: uninitialised pointer to the address of the area where the TT
861  *  changes can be stored
862  * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
863  *  function reserves the amount of space needed to send the entire local TT
864  *  table. In case of success the value is updated with the real amount of
865  *  reserved bytes
866  *
867  * Allocate the needed amount of memory for the entire TT TVLV and write its
868  * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
869  * objects, one per active VLAN.
870  *
871  * Return: the size of the allocated buffer or 0 in case of failure.
872  */
873 static u16
874 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
875 				  struct batadv_tvlv_tt_data **tt_data,
876 				  struct batadv_tvlv_tt_change **tt_change,
877 				  s32 *tt_len)
878 {
879 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
880 	struct batadv_softif_vlan *vlan;
881 	u16 num_vlan = 0;
882 	u16 vlan_entries = 0;
883 	u16 total_entries = 0;
884 	u16 tvlv_len;
885 	u8 *tt_change_ptr;
886 	int change_offset;
887 
888 	spin_lock_bh(&bat_priv->softif_vlan_list_lock);
889 	hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
890 		vlan_entries = atomic_read(&vlan->tt.num_entries);
891 		if (vlan_entries < 1)
892 			continue;
893 
894 		num_vlan++;
895 		total_entries += vlan_entries;
896 	}
897 
898 	change_offset = sizeof(**tt_data);
899 	change_offset += num_vlan * sizeof(*tt_vlan);
900 
901 	/* if tt_len is negative, allocate the space needed by the full table */
902 	if (*tt_len < 0)
903 		*tt_len = batadv_tt_len(total_entries);
904 
905 	tvlv_len = *tt_len;
906 	tvlv_len += change_offset;
907 
908 	*tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
909 	if (!*tt_data) {
910 		tvlv_len = 0;
911 		goto out;
912 	}
913 
914 	(*tt_data)->flags = BATADV_NO_FLAGS;
915 	(*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
916 	(*tt_data)->num_vlan = htons(num_vlan);
917 
918 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
919 	hlist_for_each_entry(vlan, &bat_priv->softif_vlan_list, list) {
920 		vlan_entries = atomic_read(&vlan->tt.num_entries);
921 		if (vlan_entries < 1)
922 			continue;
923 
924 		tt_vlan->vid = htons(vlan->vid);
925 		tt_vlan->crc = htonl(vlan->tt.crc);
926 		tt_vlan->reserved = 0;
927 
928 		tt_vlan++;
929 	}
930 
931 	tt_change_ptr = (u8 *)*tt_data + change_offset;
932 	*tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
933 
934 out:
935 	spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
936 	return tvlv_len;
937 }
938 
939 /**
940  * batadv_tt_tvlv_container_update() - update the translation table tvlv
941  *  container after local tt changes have been committed
942  * @bat_priv: the bat priv with all the soft interface information
943  */
944 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
945 {
946 	struct batadv_tt_change_node *entry, *safe;
947 	struct batadv_tvlv_tt_data *tt_data;
948 	struct batadv_tvlv_tt_change *tt_change;
949 	int tt_diff_len, tt_change_len = 0;
950 	int tt_diff_entries_num = 0;
951 	int tt_diff_entries_count = 0;
952 	u16 tvlv_len;
953 
954 	tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
955 	tt_diff_len = batadv_tt_len(tt_diff_entries_num);
956 
957 	/* if we have too many changes for one packet don't send any
958 	 * and wait for the tt table request which will be fragmented
959 	 */
960 	if (tt_diff_len > bat_priv->soft_iface->mtu)
961 		tt_diff_len = 0;
962 
963 	tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
964 						     &tt_change, &tt_diff_len);
965 	if (!tvlv_len)
966 		return;
967 
968 	tt_data->flags = BATADV_TT_OGM_DIFF;
969 
970 	if (tt_diff_len == 0)
971 		goto container_register;
972 
973 	spin_lock_bh(&bat_priv->tt.changes_list_lock);
974 	atomic_set(&bat_priv->tt.local_changes, 0);
975 
976 	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
977 				 list) {
978 		if (tt_diff_entries_count < tt_diff_entries_num) {
979 			memcpy(tt_change + tt_diff_entries_count,
980 			       &entry->change,
981 			       sizeof(struct batadv_tvlv_tt_change));
982 			tt_diff_entries_count++;
983 		}
984 		list_del(&entry->list);
985 		kmem_cache_free(batadv_tt_change_cache, entry);
986 	}
987 	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
988 
989 	/* Keep the buffer for possible tt_request */
990 	spin_lock_bh(&bat_priv->tt.last_changeset_lock);
991 	kfree(bat_priv->tt.last_changeset);
992 	bat_priv->tt.last_changeset_len = 0;
993 	bat_priv->tt.last_changeset = NULL;
994 	tt_change_len = batadv_tt_len(tt_diff_entries_count);
995 	/* check whether this new OGM has no changes due to size problems */
996 	if (tt_diff_entries_count > 0) {
997 		/* if kmalloc() fails we will reply with the full table
998 		 * instead of providing the diff
999 		 */
1000 		bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1001 		if (bat_priv->tt.last_changeset) {
1002 			memcpy(bat_priv->tt.last_changeset,
1003 			       tt_change, tt_change_len);
1004 			bat_priv->tt.last_changeset_len = tt_diff_len;
1005 		}
1006 	}
1007 	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1008 
1009 container_register:
1010 	batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1011 				       tvlv_len);
1012 	kfree(tt_data);
1013 }
1014 
1015 /**
1016  * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1017  * @msg :Netlink message to dump into
1018  * @portid: Port making netlink request
1019  * @cb: Control block containing additional options
1020  * @bat_priv: The bat priv with all the soft interface information
1021  * @common: tt local & tt global common data
1022  *
1023  * Return: Error code, or 0 on success
1024  */
1025 static int
1026 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1027 			   struct netlink_callback *cb,
1028 			   struct batadv_priv *bat_priv,
1029 			   struct batadv_tt_common_entry *common)
1030 {
1031 	void *hdr;
1032 	struct batadv_softif_vlan *vlan;
1033 	struct batadv_tt_local_entry *local;
1034 	unsigned int last_seen_msecs;
1035 	u32 crc;
1036 
1037 	local = container_of(common, struct batadv_tt_local_entry, common);
1038 	last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1039 
1040 	vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1041 	if (!vlan)
1042 		return 0;
1043 
1044 	crc = vlan->tt.crc;
1045 
1046 	batadv_softif_vlan_put(vlan);
1047 
1048 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1049 			  &batadv_netlink_family,  NLM_F_MULTI,
1050 			  BATADV_CMD_GET_TRANSTABLE_LOCAL);
1051 	if (!hdr)
1052 		return -ENOBUFS;
1053 
1054 	genl_dump_check_consistent(cb, hdr);
1055 
1056 	if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1057 	    nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1058 	    nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1059 	    nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1060 		goto nla_put_failure;
1061 
1062 	if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1063 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1064 		goto nla_put_failure;
1065 
1066 	genlmsg_end(msg, hdr);
1067 	return 0;
1068 
1069  nla_put_failure:
1070 	genlmsg_cancel(msg, hdr);
1071 	return -EMSGSIZE;
1072 }
1073 
1074 /**
1075  * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1076  * @msg: Netlink message to dump into
1077  * @portid: Port making netlink request
1078  * @cb: Control block containing additional options
1079  * @bat_priv: The bat priv with all the soft interface information
1080  * @hash: hash to dump
1081  * @bucket: bucket index to dump
1082  * @idx_s: Number of entries to skip
1083  *
1084  * Return: Error code, or 0 on success
1085  */
1086 static int
1087 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1088 			    struct netlink_callback *cb,
1089 			    struct batadv_priv *bat_priv,
1090 			    struct batadv_hashtable *hash, unsigned int bucket,
1091 			    int *idx_s)
1092 {
1093 	struct batadv_tt_common_entry *common;
1094 	int idx = 0;
1095 
1096 	spin_lock_bh(&hash->list_locks[bucket]);
1097 	cb->seq = atomic_read(&hash->generation) << 1 | 1;
1098 
1099 	hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1100 		if (idx++ < *idx_s)
1101 			continue;
1102 
1103 		if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1104 					       common)) {
1105 			spin_unlock_bh(&hash->list_locks[bucket]);
1106 			*idx_s = idx - 1;
1107 			return -EMSGSIZE;
1108 		}
1109 	}
1110 	spin_unlock_bh(&hash->list_locks[bucket]);
1111 
1112 	*idx_s = 0;
1113 	return 0;
1114 }
1115 
1116 /**
1117  * batadv_tt_local_dump() - Dump TT local entries into a message
1118  * @msg: Netlink message to dump into
1119  * @cb: Parameters from query
1120  *
1121  * Return: Error code, or 0 on success
1122  */
1123 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1124 {
1125 	struct net *net = sock_net(cb->skb->sk);
1126 	struct net_device *soft_iface;
1127 	struct batadv_priv *bat_priv;
1128 	struct batadv_hard_iface *primary_if = NULL;
1129 	struct batadv_hashtable *hash;
1130 	int ret;
1131 	int ifindex;
1132 	int bucket = cb->args[0];
1133 	int idx = cb->args[1];
1134 	int portid = NETLINK_CB(cb->skb).portid;
1135 
1136 	ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1137 	if (!ifindex)
1138 		return -EINVAL;
1139 
1140 	soft_iface = dev_get_by_index(net, ifindex);
1141 	if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1142 		ret = -ENODEV;
1143 		goto out;
1144 	}
1145 
1146 	bat_priv = netdev_priv(soft_iface);
1147 
1148 	primary_if = batadv_primary_if_get_selected(bat_priv);
1149 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1150 		ret = -ENOENT;
1151 		goto out;
1152 	}
1153 
1154 	hash = bat_priv->tt.local_hash;
1155 
1156 	while (bucket < hash->size) {
1157 		if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1158 						hash, bucket, &idx))
1159 			break;
1160 
1161 		bucket++;
1162 	}
1163 
1164 	ret = msg->len;
1165 
1166  out:
1167 	batadv_hardif_put(primary_if);
1168 	dev_put(soft_iface);
1169 
1170 	cb->args[0] = bucket;
1171 	cb->args[1] = idx;
1172 
1173 	return ret;
1174 }
1175 
1176 static void
1177 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1178 			    struct batadv_tt_local_entry *tt_local_entry,
1179 			    u16 flags, const char *message)
1180 {
1181 	batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1182 
1183 	/* The local client has to be marked as "pending to be removed" but has
1184 	 * to be kept in the table in order to send it in a full table
1185 	 * response issued before the net ttvn increment (consistency check)
1186 	 */
1187 	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1188 
1189 	batadv_dbg(BATADV_DBG_TT, bat_priv,
1190 		   "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1191 		   tt_local_entry->common.addr,
1192 		   batadv_print_vid(tt_local_entry->common.vid), message);
1193 }
1194 
1195 /**
1196  * batadv_tt_local_remove() - logically remove an entry from the local table
1197  * @bat_priv: the bat priv with all the soft interface information
1198  * @addr: the MAC address of the client to remove
1199  * @vid: VLAN identifier
1200  * @message: message to append to the log on deletion
1201  * @roaming: true if the deletion is due to a roaming event
1202  *
1203  * Return: the flags assigned to the local entry before being deleted
1204  */
1205 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1206 			   unsigned short vid, const char *message,
1207 			   bool roaming)
1208 {
1209 	struct batadv_tt_local_entry *tt_removed_entry;
1210 	struct batadv_tt_local_entry *tt_local_entry;
1211 	u16 flags, curr_flags = BATADV_NO_FLAGS;
1212 	struct hlist_node *tt_removed_node;
1213 
1214 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1215 	if (!tt_local_entry)
1216 		goto out;
1217 
1218 	curr_flags = tt_local_entry->common.flags;
1219 
1220 	flags = BATADV_TT_CLIENT_DEL;
1221 	/* if this global entry addition is due to a roaming, the node has to
1222 	 * mark the local entry as "roamed" in order to correctly reroute
1223 	 * packets later
1224 	 */
1225 	if (roaming) {
1226 		flags |= BATADV_TT_CLIENT_ROAM;
1227 		/* mark the local client as ROAMed */
1228 		tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1229 	}
1230 
1231 	if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1232 		batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1233 					    message);
1234 		goto out;
1235 	}
1236 	/* if this client has been added right now, it is possible to
1237 	 * immediately purge it
1238 	 */
1239 	batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1240 
1241 	tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1242 					     batadv_compare_tt,
1243 					     batadv_choose_tt,
1244 					     &tt_local_entry->common);
1245 	if (!tt_removed_node)
1246 		goto out;
1247 
1248 	/* drop reference of remove hash entry */
1249 	tt_removed_entry = hlist_entry(tt_removed_node,
1250 				       struct batadv_tt_local_entry,
1251 				       common.hash_entry);
1252 	batadv_tt_local_entry_put(tt_removed_entry);
1253 
1254 out:
1255 	batadv_tt_local_entry_put(tt_local_entry);
1256 
1257 	return curr_flags;
1258 }
1259 
1260 /**
1261  * batadv_tt_local_purge_list() - purge inactive tt local entries
1262  * @bat_priv: the bat priv with all the soft interface information
1263  * @head: pointer to the list containing the local tt entries
1264  * @timeout: parameter deciding whether a given tt local entry is considered
1265  *  inactive or not
1266  */
1267 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1268 				       struct hlist_head *head,
1269 				       int timeout)
1270 {
1271 	struct batadv_tt_local_entry *tt_local_entry;
1272 	struct batadv_tt_common_entry *tt_common_entry;
1273 	struct hlist_node *node_tmp;
1274 
1275 	hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1276 				  hash_entry) {
1277 		tt_local_entry = container_of(tt_common_entry,
1278 					      struct batadv_tt_local_entry,
1279 					      common);
1280 		if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1281 			continue;
1282 
1283 		/* entry already marked for deletion */
1284 		if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1285 			continue;
1286 
1287 		if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1288 			continue;
1289 
1290 		batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1291 					    BATADV_TT_CLIENT_DEL, "timed out");
1292 	}
1293 }
1294 
1295 /**
1296  * batadv_tt_local_purge() - purge inactive tt local entries
1297  * @bat_priv: the bat priv with all the soft interface information
1298  * @timeout: parameter deciding whether a given tt local entry is considered
1299  *  inactive or not
1300  */
1301 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1302 				  int timeout)
1303 {
1304 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1305 	struct hlist_head *head;
1306 	spinlock_t *list_lock; /* protects write access to the hash lists */
1307 	u32 i;
1308 
1309 	for (i = 0; i < hash->size; i++) {
1310 		head = &hash->table[i];
1311 		list_lock = &hash->list_locks[i];
1312 
1313 		spin_lock_bh(list_lock);
1314 		batadv_tt_local_purge_list(bat_priv, head, timeout);
1315 		spin_unlock_bh(list_lock);
1316 	}
1317 }
1318 
1319 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1320 {
1321 	struct batadv_hashtable *hash;
1322 	spinlock_t *list_lock; /* protects write access to the hash lists */
1323 	struct batadv_tt_common_entry *tt_common_entry;
1324 	struct batadv_tt_local_entry *tt_local;
1325 	struct hlist_node *node_tmp;
1326 	struct hlist_head *head;
1327 	u32 i;
1328 
1329 	if (!bat_priv->tt.local_hash)
1330 		return;
1331 
1332 	hash = bat_priv->tt.local_hash;
1333 
1334 	for (i = 0; i < hash->size; i++) {
1335 		head = &hash->table[i];
1336 		list_lock = &hash->list_locks[i];
1337 
1338 		spin_lock_bh(list_lock);
1339 		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1340 					  head, hash_entry) {
1341 			hlist_del_rcu(&tt_common_entry->hash_entry);
1342 			tt_local = container_of(tt_common_entry,
1343 						struct batadv_tt_local_entry,
1344 						common);
1345 
1346 			batadv_tt_local_entry_put(tt_local);
1347 		}
1348 		spin_unlock_bh(list_lock);
1349 	}
1350 
1351 	batadv_hash_destroy(hash);
1352 
1353 	bat_priv->tt.local_hash = NULL;
1354 }
1355 
1356 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1357 {
1358 	if (bat_priv->tt.global_hash)
1359 		return 0;
1360 
1361 	bat_priv->tt.global_hash = batadv_hash_new(1024);
1362 
1363 	if (!bat_priv->tt.global_hash)
1364 		return -ENOMEM;
1365 
1366 	batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1367 				   &batadv_tt_global_hash_lock_class_key);
1368 
1369 	return 0;
1370 }
1371 
1372 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1373 {
1374 	struct batadv_tt_change_node *entry, *safe;
1375 
1376 	spin_lock_bh(&bat_priv->tt.changes_list_lock);
1377 
1378 	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1379 				 list) {
1380 		list_del(&entry->list);
1381 		kmem_cache_free(batadv_tt_change_cache, entry);
1382 	}
1383 
1384 	atomic_set(&bat_priv->tt.local_changes, 0);
1385 	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1386 }
1387 
1388 /**
1389  * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1390  * @entry: the TT global entry where the orig_list_entry has to be
1391  *  extracted from
1392  * @orig_node: the originator for which the orig_list_entry has to be found
1393  *
1394  * retrieve the orig_tt_list_entry belonging to orig_node from the
1395  * batadv_tt_global_entry list
1396  *
1397  * Return: it with an increased refcounter, NULL if not found
1398  */
1399 static struct batadv_tt_orig_list_entry *
1400 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1401 				 const struct batadv_orig_node *orig_node)
1402 {
1403 	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1404 	const struct hlist_head *head;
1405 
1406 	rcu_read_lock();
1407 	head = &entry->orig_list;
1408 	hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1409 		if (tmp_orig_entry->orig_node != orig_node)
1410 			continue;
1411 		if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1412 			continue;
1413 
1414 		orig_entry = tmp_orig_entry;
1415 		break;
1416 	}
1417 	rcu_read_unlock();
1418 
1419 	return orig_entry;
1420 }
1421 
1422 /**
1423  * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1424  *  handled by a given originator
1425  * @entry: the TT global entry to check
1426  * @orig_node: the originator to search in the list
1427  * @flags: a pointer to store TT flags for the given @entry received
1428  *  from @orig_node
1429  *
1430  * find out if an orig_node is already in the list of a tt_global_entry.
1431  *
1432  * Return: true if found, false otherwise
1433  */
1434 static bool
1435 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1436 				const struct batadv_orig_node *orig_node,
1437 				u8 *flags)
1438 {
1439 	struct batadv_tt_orig_list_entry *orig_entry;
1440 	bool found = false;
1441 
1442 	orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1443 	if (orig_entry) {
1444 		found = true;
1445 
1446 		if (flags)
1447 			*flags = orig_entry->flags;
1448 
1449 		batadv_tt_orig_list_entry_put(orig_entry);
1450 	}
1451 
1452 	return found;
1453 }
1454 
1455 /**
1456  * batadv_tt_global_sync_flags() - update TT sync flags
1457  * @tt_global: the TT global entry to update sync flags in
1458  *
1459  * Updates the sync flag bits in the tt_global flag attribute with a logical
1460  * OR of all sync flags from any of its TT orig entries.
1461  */
1462 static void
1463 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1464 {
1465 	struct batadv_tt_orig_list_entry *orig_entry;
1466 	const struct hlist_head *head;
1467 	u16 flags = BATADV_NO_FLAGS;
1468 
1469 	rcu_read_lock();
1470 	head = &tt_global->orig_list;
1471 	hlist_for_each_entry_rcu(orig_entry, head, list)
1472 		flags |= orig_entry->flags;
1473 	rcu_read_unlock();
1474 
1475 	flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1476 	tt_global->common.flags = flags;
1477 }
1478 
1479 /**
1480  * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1481  * @tt_global: the TT global entry to add an orig entry in
1482  * @orig_node: the originator to add an orig entry for
1483  * @ttvn: translation table version number of this changeset
1484  * @flags: TT sync flags
1485  */
1486 static void
1487 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1488 				struct batadv_orig_node *orig_node, int ttvn,
1489 				u8 flags)
1490 {
1491 	struct batadv_tt_orig_list_entry *orig_entry;
1492 
1493 	spin_lock_bh(&tt_global->list_lock);
1494 
1495 	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1496 	if (orig_entry) {
1497 		/* refresh the ttvn: the current value could be a bogus one that
1498 		 * was added during a "temporary client detection"
1499 		 */
1500 		orig_entry->ttvn = ttvn;
1501 		orig_entry->flags = flags;
1502 		goto sync_flags;
1503 	}
1504 
1505 	orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1506 	if (!orig_entry)
1507 		goto out;
1508 
1509 	INIT_HLIST_NODE(&orig_entry->list);
1510 	kref_get(&orig_node->refcount);
1511 	batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1512 	orig_entry->orig_node = orig_node;
1513 	orig_entry->ttvn = ttvn;
1514 	orig_entry->flags = flags;
1515 	kref_init(&orig_entry->refcount);
1516 
1517 	kref_get(&orig_entry->refcount);
1518 	hlist_add_head_rcu(&orig_entry->list,
1519 			   &tt_global->orig_list);
1520 	atomic_inc(&tt_global->orig_list_count);
1521 
1522 sync_flags:
1523 	batadv_tt_global_sync_flags(tt_global);
1524 out:
1525 	batadv_tt_orig_list_entry_put(orig_entry);
1526 
1527 	spin_unlock_bh(&tt_global->list_lock);
1528 }
1529 
1530 /**
1531  * batadv_tt_global_add() - add a new TT global entry or update an existing one
1532  * @bat_priv: the bat priv with all the soft interface information
1533  * @orig_node: the originator announcing the client
1534  * @tt_addr: the mac address of the non-mesh client
1535  * @vid: VLAN identifier
1536  * @flags: TT flags that have to be set for this non-mesh client
1537  * @ttvn: the tt version number ever announcing this non-mesh client
1538  *
1539  * Add a new TT global entry for the given originator. If the entry already
1540  * exists add a new reference to the given originator (a global entry can have
1541  * references to multiple originators) and adjust the flags attribute to reflect
1542  * the function argument.
1543  * If a TT local entry exists for this non-mesh client remove it.
1544  *
1545  * The caller must hold the orig_node refcount.
1546  *
1547  * Return: true if the new entry has been added, false otherwise
1548  */
1549 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1550 				 struct batadv_orig_node *orig_node,
1551 				 const unsigned char *tt_addr,
1552 				 unsigned short vid, u16 flags, u8 ttvn)
1553 {
1554 	struct batadv_tt_global_entry *tt_global_entry;
1555 	struct batadv_tt_local_entry *tt_local_entry;
1556 	bool ret = false;
1557 	int hash_added;
1558 	struct batadv_tt_common_entry *common;
1559 	u16 local_flags;
1560 
1561 	/* ignore global entries from backbone nodes */
1562 	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1563 		return true;
1564 
1565 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1566 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1567 
1568 	/* if the node already has a local client for this entry, it has to wait
1569 	 * for a roaming advertisement instead of manually messing up the global
1570 	 * table
1571 	 */
1572 	if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1573 	    !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1574 		goto out;
1575 
1576 	if (!tt_global_entry) {
1577 		tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1578 						    GFP_ATOMIC);
1579 		if (!tt_global_entry)
1580 			goto out;
1581 
1582 		common = &tt_global_entry->common;
1583 		ether_addr_copy(common->addr, tt_addr);
1584 		common->vid = vid;
1585 
1586 		if (!is_multicast_ether_addr(common->addr))
1587 			common->flags = flags & (~BATADV_TT_SYNC_MASK);
1588 
1589 		tt_global_entry->roam_at = 0;
1590 		/* node must store current time in case of roaming. This is
1591 		 * needed to purge this entry out on timeout (if nobody claims
1592 		 * it)
1593 		 */
1594 		if (flags & BATADV_TT_CLIENT_ROAM)
1595 			tt_global_entry->roam_at = jiffies;
1596 		kref_init(&common->refcount);
1597 		common->added_at = jiffies;
1598 
1599 		INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1600 		atomic_set(&tt_global_entry->orig_list_count, 0);
1601 		spin_lock_init(&tt_global_entry->list_lock);
1602 
1603 		kref_get(&common->refcount);
1604 		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1605 					     batadv_compare_tt,
1606 					     batadv_choose_tt, common,
1607 					     &common->hash_entry);
1608 
1609 		if (unlikely(hash_added != 0)) {
1610 			/* remove the reference for the hash */
1611 			batadv_tt_global_entry_put(tt_global_entry);
1612 			goto out_remove;
1613 		}
1614 	} else {
1615 		common = &tt_global_entry->common;
1616 		/* If there is already a global entry, we can use this one for
1617 		 * our processing.
1618 		 * But if we are trying to add a temporary client then here are
1619 		 * two options at this point:
1620 		 * 1) the global client is not a temporary client: the global
1621 		 *    client has to be left as it is, temporary information
1622 		 *    should never override any already known client state
1623 		 * 2) the global client is a temporary client: purge the
1624 		 *    originator list and add the new one orig_entry
1625 		 */
1626 		if (flags & BATADV_TT_CLIENT_TEMP) {
1627 			if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1628 				goto out;
1629 			if (batadv_tt_global_entry_has_orig(tt_global_entry,
1630 							    orig_node, NULL))
1631 				goto out_remove;
1632 			batadv_tt_global_del_orig_list(tt_global_entry);
1633 			goto add_orig_entry;
1634 		}
1635 
1636 		/* if the client was temporary added before receiving the first
1637 		 * OGM announcing it, we have to clear the TEMP flag. Also,
1638 		 * remove the previous temporary orig node and re-add it
1639 		 * if required. If the orig entry changed, the new one which
1640 		 * is a non-temporary entry is preferred.
1641 		 */
1642 		if (common->flags & BATADV_TT_CLIENT_TEMP) {
1643 			batadv_tt_global_del_orig_list(tt_global_entry);
1644 			common->flags &= ~BATADV_TT_CLIENT_TEMP;
1645 		}
1646 
1647 		/* the change can carry possible "attribute" flags like the
1648 		 * TT_CLIENT_TEMP, therefore they have to be copied in the
1649 		 * client entry
1650 		 */
1651 		if (!is_multicast_ether_addr(common->addr))
1652 			common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1653 
1654 		/* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1655 		 * one originator left in the list and we previously received a
1656 		 * delete + roaming change for this originator.
1657 		 *
1658 		 * We should first delete the old originator before adding the
1659 		 * new one.
1660 		 */
1661 		if (common->flags & BATADV_TT_CLIENT_ROAM) {
1662 			batadv_tt_global_del_orig_list(tt_global_entry);
1663 			common->flags &= ~BATADV_TT_CLIENT_ROAM;
1664 			tt_global_entry->roam_at = 0;
1665 		}
1666 	}
1667 add_orig_entry:
1668 	/* add the new orig_entry (if needed) or update it */
1669 	batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1670 					flags & BATADV_TT_SYNC_MASK);
1671 
1672 	batadv_dbg(BATADV_DBG_TT, bat_priv,
1673 		   "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1674 		   common->addr, batadv_print_vid(common->vid),
1675 		   orig_node->orig);
1676 	ret = true;
1677 
1678 out_remove:
1679 	/* Do not remove multicast addresses from the local hash on
1680 	 * global additions
1681 	 */
1682 	if (is_multicast_ether_addr(tt_addr))
1683 		goto out;
1684 
1685 	/* remove address from local hash if present */
1686 	local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1687 					     "global tt received",
1688 					     flags & BATADV_TT_CLIENT_ROAM);
1689 	tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1690 
1691 	if (!(flags & BATADV_TT_CLIENT_ROAM))
1692 		/* this is a normal global add. Therefore the client is not in a
1693 		 * roaming state anymore.
1694 		 */
1695 		tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1696 
1697 out:
1698 	batadv_tt_global_entry_put(tt_global_entry);
1699 	batadv_tt_local_entry_put(tt_local_entry);
1700 	return ret;
1701 }
1702 
1703 /**
1704  * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1705  * @bat_priv: the bat priv with all the soft interface information
1706  * @tt_global_entry: global translation table entry to be analyzed
1707  *
1708  * This function assumes the caller holds rcu_read_lock().
1709  * Return: best originator list entry or NULL on errors.
1710  */
1711 static struct batadv_tt_orig_list_entry *
1712 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1713 			    struct batadv_tt_global_entry *tt_global_entry)
1714 {
1715 	struct batadv_neigh_node *router, *best_router = NULL;
1716 	struct batadv_algo_ops *bao = bat_priv->algo_ops;
1717 	struct hlist_head *head;
1718 	struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1719 
1720 	head = &tt_global_entry->orig_list;
1721 	hlist_for_each_entry_rcu(orig_entry, head, list) {
1722 		router = batadv_orig_router_get(orig_entry->orig_node,
1723 						BATADV_IF_DEFAULT);
1724 		if (!router)
1725 			continue;
1726 
1727 		if (best_router &&
1728 		    bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1729 				   BATADV_IF_DEFAULT) <= 0) {
1730 			batadv_neigh_node_put(router);
1731 			continue;
1732 		}
1733 
1734 		/* release the refcount for the "old" best */
1735 		batadv_neigh_node_put(best_router);
1736 
1737 		best_entry = orig_entry;
1738 		best_router = router;
1739 	}
1740 
1741 	batadv_neigh_node_put(best_router);
1742 
1743 	return best_entry;
1744 }
1745 
1746 /**
1747  * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
1748  * @msg: Netlink message to dump into
1749  * @portid: Port making netlink request
1750  * @seq: Sequence number of netlink message
1751  * @common: tt local & tt global common data
1752  * @orig: Originator node announcing a non-mesh client
1753  * @best: Is the best originator for the TT entry
1754  *
1755  * Return: Error code, or 0 on success
1756  */
1757 static int
1758 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1759 			       struct batadv_tt_common_entry *common,
1760 			       struct batadv_tt_orig_list_entry *orig,
1761 			       bool best)
1762 {
1763 	u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
1764 	void *hdr;
1765 	struct batadv_orig_node_vlan *vlan;
1766 	u8 last_ttvn;
1767 	u32 crc;
1768 
1769 	vlan = batadv_orig_node_vlan_get(orig->orig_node,
1770 					 common->vid);
1771 	if (!vlan)
1772 		return 0;
1773 
1774 	crc = vlan->tt.crc;
1775 
1776 	batadv_orig_node_vlan_put(vlan);
1777 
1778 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1779 			  NLM_F_MULTI,
1780 			  BATADV_CMD_GET_TRANSTABLE_GLOBAL);
1781 	if (!hdr)
1782 		return -ENOBUFS;
1783 
1784 	last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
1785 
1786 	if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1787 	    nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1788 		    orig->orig_node->orig) ||
1789 	    nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
1790 	    nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
1791 	    nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1792 	    nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1793 	    nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
1794 		goto nla_put_failure;
1795 
1796 	if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1797 		goto nla_put_failure;
1798 
1799 	genlmsg_end(msg, hdr);
1800 	return 0;
1801 
1802  nla_put_failure:
1803 	genlmsg_cancel(msg, hdr);
1804 	return -EMSGSIZE;
1805 }
1806 
1807 /**
1808  * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
1809  * @msg: Netlink message to dump into
1810  * @portid: Port making netlink request
1811  * @seq: Sequence number of netlink message
1812  * @bat_priv: The bat priv with all the soft interface information
1813  * @common: tt local & tt global common data
1814  * @sub_s: Number of entries to skip
1815  *
1816  * This function assumes the caller holds rcu_read_lock().
1817  *
1818  * Return: Error code, or 0 on success
1819  */
1820 static int
1821 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1822 			    struct batadv_priv *bat_priv,
1823 			    struct batadv_tt_common_entry *common, int *sub_s)
1824 {
1825 	struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1826 	struct batadv_tt_global_entry *global;
1827 	struct hlist_head *head;
1828 	int sub = 0;
1829 	bool best;
1830 
1831 	global = container_of(common, struct batadv_tt_global_entry, common);
1832 	best_entry = batadv_transtable_best_orig(bat_priv, global);
1833 	head = &global->orig_list;
1834 
1835 	hlist_for_each_entry_rcu(orig_entry, head, list) {
1836 		if (sub++ < *sub_s)
1837 			continue;
1838 
1839 		best = (orig_entry == best_entry);
1840 
1841 		if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
1842 						   orig_entry, best)) {
1843 			*sub_s = sub - 1;
1844 			return -EMSGSIZE;
1845 		}
1846 	}
1847 
1848 	*sub_s = 0;
1849 	return 0;
1850 }
1851 
1852 /**
1853  * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
1854  * @msg: Netlink message to dump into
1855  * @portid: Port making netlink request
1856  * @seq: Sequence number of netlink message
1857  * @bat_priv: The bat priv with all the soft interface information
1858  * @head: Pointer to the list containing the global tt entries
1859  * @idx_s: Number of entries to skip
1860  * @sub: Number of entries to skip
1861  *
1862  * Return: Error code, or 0 on success
1863  */
1864 static int
1865 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1866 			     struct batadv_priv *bat_priv,
1867 			     struct hlist_head *head, int *idx_s, int *sub)
1868 {
1869 	struct batadv_tt_common_entry *common;
1870 	int idx = 0;
1871 
1872 	rcu_read_lock();
1873 	hlist_for_each_entry_rcu(common, head, hash_entry) {
1874 		if (idx++ < *idx_s)
1875 			continue;
1876 
1877 		if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
1878 						common, sub)) {
1879 			rcu_read_unlock();
1880 			*idx_s = idx - 1;
1881 			return -EMSGSIZE;
1882 		}
1883 	}
1884 	rcu_read_unlock();
1885 
1886 	*idx_s = 0;
1887 	*sub = 0;
1888 	return 0;
1889 }
1890 
1891 /**
1892  * batadv_tt_global_dump() -  Dump TT global entries into a message
1893  * @msg: Netlink message to dump into
1894  * @cb: Parameters from query
1895  *
1896  * Return: Error code, or length of message on success
1897  */
1898 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
1899 {
1900 	struct net *net = sock_net(cb->skb->sk);
1901 	struct net_device *soft_iface;
1902 	struct batadv_priv *bat_priv;
1903 	struct batadv_hard_iface *primary_if = NULL;
1904 	struct batadv_hashtable *hash;
1905 	struct hlist_head *head;
1906 	int ret;
1907 	int ifindex;
1908 	int bucket = cb->args[0];
1909 	int idx = cb->args[1];
1910 	int sub = cb->args[2];
1911 	int portid = NETLINK_CB(cb->skb).portid;
1912 
1913 	ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1914 	if (!ifindex)
1915 		return -EINVAL;
1916 
1917 	soft_iface = dev_get_by_index(net, ifindex);
1918 	if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1919 		ret = -ENODEV;
1920 		goto out;
1921 	}
1922 
1923 	bat_priv = netdev_priv(soft_iface);
1924 
1925 	primary_if = batadv_primary_if_get_selected(bat_priv);
1926 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1927 		ret = -ENOENT;
1928 		goto out;
1929 	}
1930 
1931 	hash = bat_priv->tt.global_hash;
1932 
1933 	while (bucket < hash->size) {
1934 		head = &hash->table[bucket];
1935 
1936 		if (batadv_tt_global_dump_bucket(msg, portid,
1937 						 cb->nlh->nlmsg_seq, bat_priv,
1938 						 head, &idx, &sub))
1939 			break;
1940 
1941 		bucket++;
1942 	}
1943 
1944 	ret = msg->len;
1945 
1946  out:
1947 	batadv_hardif_put(primary_if);
1948 	dev_put(soft_iface);
1949 
1950 	cb->args[0] = bucket;
1951 	cb->args[1] = idx;
1952 	cb->args[2] = sub;
1953 
1954 	return ret;
1955 }
1956 
1957 /**
1958  * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
1959  * @tt_global_entry: the global entry to remove the orig_entry from
1960  * @orig_entry: the orig entry to remove and free
1961  *
1962  * Remove an orig_entry from its list in the given tt_global_entry and
1963  * free this orig_entry afterwards.
1964  *
1965  * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1966  * part of a list.
1967  */
1968 static void
1969 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1970 				 struct batadv_tt_orig_list_entry *orig_entry)
1971 {
1972 	lockdep_assert_held(&tt_global_entry->list_lock);
1973 
1974 	batadv_tt_global_size_dec(orig_entry->orig_node,
1975 				  tt_global_entry->common.vid);
1976 	atomic_dec(&tt_global_entry->orig_list_count);
1977 	/* requires holding tt_global_entry->list_lock and orig_entry->list
1978 	 * being part of a list
1979 	 */
1980 	hlist_del_rcu(&orig_entry->list);
1981 	batadv_tt_orig_list_entry_put(orig_entry);
1982 }
1983 
1984 /* deletes the orig list of a tt_global_entry */
1985 static void
1986 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1987 {
1988 	struct hlist_head *head;
1989 	struct hlist_node *safe;
1990 	struct batadv_tt_orig_list_entry *orig_entry;
1991 
1992 	spin_lock_bh(&tt_global_entry->list_lock);
1993 	head = &tt_global_entry->orig_list;
1994 	hlist_for_each_entry_safe(orig_entry, safe, head, list)
1995 		_batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1996 	spin_unlock_bh(&tt_global_entry->list_lock);
1997 }
1998 
1999 /**
2000  * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2001  * @bat_priv: the bat priv with all the soft interface information
2002  * @tt_global_entry: the global entry to remove the orig_node from
2003  * @orig_node: the originator announcing the client
2004  * @message: message to append to the log on deletion
2005  *
2006  * Remove the given orig_node and its according orig_entry from the given
2007  * global tt entry.
2008  */
2009 static void
2010 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2011 			       struct batadv_tt_global_entry *tt_global_entry,
2012 			       struct batadv_orig_node *orig_node,
2013 			       const char *message)
2014 {
2015 	struct hlist_head *head;
2016 	struct hlist_node *safe;
2017 	struct batadv_tt_orig_list_entry *orig_entry;
2018 	unsigned short vid;
2019 
2020 	spin_lock_bh(&tt_global_entry->list_lock);
2021 	head = &tt_global_entry->orig_list;
2022 	hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2023 		if (orig_entry->orig_node == orig_node) {
2024 			vid = tt_global_entry->common.vid;
2025 			batadv_dbg(BATADV_DBG_TT, bat_priv,
2026 				   "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2027 				   orig_node->orig,
2028 				   tt_global_entry->common.addr,
2029 				   batadv_print_vid(vid), message);
2030 			_batadv_tt_global_del_orig_entry(tt_global_entry,
2031 							 orig_entry);
2032 		}
2033 	}
2034 	spin_unlock_bh(&tt_global_entry->list_lock);
2035 }
2036 
2037 /* If the client is to be deleted, we check if it is the last origantor entry
2038  * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2039  * timer, otherwise we simply remove the originator scheduled for deletion.
2040  */
2041 static void
2042 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2043 			     struct batadv_tt_global_entry *tt_global_entry,
2044 			     struct batadv_orig_node *orig_node,
2045 			     const char *message)
2046 {
2047 	bool last_entry = true;
2048 	struct hlist_head *head;
2049 	struct batadv_tt_orig_list_entry *orig_entry;
2050 
2051 	/* no local entry exists, case 1:
2052 	 * Check if this is the last one or if other entries exist.
2053 	 */
2054 
2055 	rcu_read_lock();
2056 	head = &tt_global_entry->orig_list;
2057 	hlist_for_each_entry_rcu(orig_entry, head, list) {
2058 		if (orig_entry->orig_node != orig_node) {
2059 			last_entry = false;
2060 			break;
2061 		}
2062 	}
2063 	rcu_read_unlock();
2064 
2065 	if (last_entry) {
2066 		/* its the last one, mark for roaming. */
2067 		tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2068 		tt_global_entry->roam_at = jiffies;
2069 	} else {
2070 		/* there is another entry, we can simply delete this
2071 		 * one and can still use the other one.
2072 		 */
2073 		batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2074 					       orig_node, message);
2075 	}
2076 }
2077 
2078 /**
2079  * batadv_tt_global_del() - remove a client from the global table
2080  * @bat_priv: the bat priv with all the soft interface information
2081  * @orig_node: an originator serving this client
2082  * @addr: the mac address of the client
2083  * @vid: VLAN identifier
2084  * @message: a message explaining the reason for deleting the client to print
2085  *  for debugging purpose
2086  * @roaming: true if the deletion has been triggered by a roaming event
2087  */
2088 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2089 				 struct batadv_orig_node *orig_node,
2090 				 const unsigned char *addr, unsigned short vid,
2091 				 const char *message, bool roaming)
2092 {
2093 	struct batadv_tt_global_entry *tt_global_entry;
2094 	struct batadv_tt_local_entry *local_entry = NULL;
2095 
2096 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2097 	if (!tt_global_entry)
2098 		goto out;
2099 
2100 	if (!roaming) {
2101 		batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2102 					       orig_node, message);
2103 
2104 		if (hlist_empty(&tt_global_entry->orig_list))
2105 			batadv_tt_global_free(bat_priv, tt_global_entry,
2106 					      message);
2107 
2108 		goto out;
2109 	}
2110 
2111 	/* if we are deleting a global entry due to a roam
2112 	 * event, there are two possibilities:
2113 	 * 1) the client roamed from node A to node B => if there
2114 	 *    is only one originator left for this client, we mark
2115 	 *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2116 	 *    wait for node B to claim it. In case of timeout
2117 	 *    the entry is purged.
2118 	 *
2119 	 *    If there are other originators left, we directly delete
2120 	 *    the originator.
2121 	 * 2) the client roamed to us => we can directly delete
2122 	 *    the global entry, since it is useless now.
2123 	 */
2124 	local_entry = batadv_tt_local_hash_find(bat_priv,
2125 						tt_global_entry->common.addr,
2126 						vid);
2127 	if (local_entry) {
2128 		/* local entry exists, case 2: client roamed to us. */
2129 		batadv_tt_global_del_orig_list(tt_global_entry);
2130 		batadv_tt_global_free(bat_priv, tt_global_entry, message);
2131 	} else {
2132 		/* no local entry exists, case 1: check for roaming */
2133 		batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2134 					     orig_node, message);
2135 	}
2136 
2137 out:
2138 	batadv_tt_global_entry_put(tt_global_entry);
2139 	batadv_tt_local_entry_put(local_entry);
2140 }
2141 
2142 /**
2143  * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2144  *  the given originator matching the provided vid
2145  * @bat_priv: the bat priv with all the soft interface information
2146  * @orig_node: the originator owning the entries to remove
2147  * @match_vid: the VLAN identifier to match. If negative all the entries will be
2148  *  removed
2149  * @message: debug message to print as "reason"
2150  */
2151 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2152 			       struct batadv_orig_node *orig_node,
2153 			       s32 match_vid,
2154 			       const char *message)
2155 {
2156 	struct batadv_tt_global_entry *tt_global;
2157 	struct batadv_tt_common_entry *tt_common_entry;
2158 	u32 i;
2159 	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2160 	struct hlist_node *safe;
2161 	struct hlist_head *head;
2162 	spinlock_t *list_lock; /* protects write access to the hash lists */
2163 	unsigned short vid;
2164 
2165 	if (!hash)
2166 		return;
2167 
2168 	for (i = 0; i < hash->size; i++) {
2169 		head = &hash->table[i];
2170 		list_lock = &hash->list_locks[i];
2171 
2172 		spin_lock_bh(list_lock);
2173 		hlist_for_each_entry_safe(tt_common_entry, safe,
2174 					  head, hash_entry) {
2175 			/* remove only matching entries */
2176 			if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2177 				continue;
2178 
2179 			tt_global = container_of(tt_common_entry,
2180 						 struct batadv_tt_global_entry,
2181 						 common);
2182 
2183 			batadv_tt_global_del_orig_node(bat_priv, tt_global,
2184 						       orig_node, message);
2185 
2186 			if (hlist_empty(&tt_global->orig_list)) {
2187 				vid = tt_global->common.vid;
2188 				batadv_dbg(BATADV_DBG_TT, bat_priv,
2189 					   "Deleting global tt entry %pM (vid: %d): %s\n",
2190 					   tt_global->common.addr,
2191 					   batadv_print_vid(vid), message);
2192 				hlist_del_rcu(&tt_common_entry->hash_entry);
2193 				batadv_tt_global_entry_put(tt_global);
2194 			}
2195 		}
2196 		spin_unlock_bh(list_lock);
2197 	}
2198 	clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2199 }
2200 
2201 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2202 				      char **msg)
2203 {
2204 	bool purge = false;
2205 	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2206 	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2207 
2208 	if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2209 	    batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2210 		purge = true;
2211 		*msg = "Roaming timeout\n";
2212 	}
2213 
2214 	if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2215 	    batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2216 		purge = true;
2217 		*msg = "Temporary client timeout\n";
2218 	}
2219 
2220 	return purge;
2221 }
2222 
2223 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2224 {
2225 	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2226 	struct hlist_head *head;
2227 	struct hlist_node *node_tmp;
2228 	spinlock_t *list_lock; /* protects write access to the hash lists */
2229 	u32 i;
2230 	char *msg = NULL;
2231 	struct batadv_tt_common_entry *tt_common;
2232 	struct batadv_tt_global_entry *tt_global;
2233 
2234 	for (i = 0; i < hash->size; i++) {
2235 		head = &hash->table[i];
2236 		list_lock = &hash->list_locks[i];
2237 
2238 		spin_lock_bh(list_lock);
2239 		hlist_for_each_entry_safe(tt_common, node_tmp, head,
2240 					  hash_entry) {
2241 			tt_global = container_of(tt_common,
2242 						 struct batadv_tt_global_entry,
2243 						 common);
2244 
2245 			if (!batadv_tt_global_to_purge(tt_global, &msg))
2246 				continue;
2247 
2248 			batadv_dbg(BATADV_DBG_TT, bat_priv,
2249 				   "Deleting global tt entry %pM (vid: %d): %s\n",
2250 				   tt_global->common.addr,
2251 				   batadv_print_vid(tt_global->common.vid),
2252 				   msg);
2253 
2254 			hlist_del_rcu(&tt_common->hash_entry);
2255 
2256 			batadv_tt_global_entry_put(tt_global);
2257 		}
2258 		spin_unlock_bh(list_lock);
2259 	}
2260 }
2261 
2262 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2263 {
2264 	struct batadv_hashtable *hash;
2265 	spinlock_t *list_lock; /* protects write access to the hash lists */
2266 	struct batadv_tt_common_entry *tt_common_entry;
2267 	struct batadv_tt_global_entry *tt_global;
2268 	struct hlist_node *node_tmp;
2269 	struct hlist_head *head;
2270 	u32 i;
2271 
2272 	if (!bat_priv->tt.global_hash)
2273 		return;
2274 
2275 	hash = bat_priv->tt.global_hash;
2276 
2277 	for (i = 0; i < hash->size; i++) {
2278 		head = &hash->table[i];
2279 		list_lock = &hash->list_locks[i];
2280 
2281 		spin_lock_bh(list_lock);
2282 		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2283 					  head, hash_entry) {
2284 			hlist_del_rcu(&tt_common_entry->hash_entry);
2285 			tt_global = container_of(tt_common_entry,
2286 						 struct batadv_tt_global_entry,
2287 						 common);
2288 			batadv_tt_global_entry_put(tt_global);
2289 		}
2290 		spin_unlock_bh(list_lock);
2291 	}
2292 
2293 	batadv_hash_destroy(hash);
2294 
2295 	bat_priv->tt.global_hash = NULL;
2296 }
2297 
2298 static bool
2299 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2300 		       struct batadv_tt_global_entry *tt_global_entry)
2301 {
2302 	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2303 	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2304 		return true;
2305 
2306 	/* check if the two clients are marked as isolated */
2307 	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2308 	    tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2309 		return true;
2310 
2311 	return false;
2312 }
2313 
2314 /**
2315  * batadv_transtable_search() - get the mesh destination for a given client
2316  * @bat_priv: the bat priv with all the soft interface information
2317  * @src: mac address of the source client
2318  * @addr: mac address of the destination client
2319  * @vid: VLAN identifier
2320  *
2321  * Return: a pointer to the originator that was selected as destination in the
2322  * mesh for contacting the client 'addr', NULL otherwise.
2323  * In case of multiple originators serving the same client, the function returns
2324  * the best one (best in terms of metric towards the destination node).
2325  *
2326  * If the two clients are AP isolated the function returns NULL.
2327  */
2328 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2329 						  const u8 *src,
2330 						  const u8 *addr,
2331 						  unsigned short vid)
2332 {
2333 	struct batadv_tt_local_entry *tt_local_entry = NULL;
2334 	struct batadv_tt_global_entry *tt_global_entry = NULL;
2335 	struct batadv_orig_node *orig_node = NULL;
2336 	struct batadv_tt_orig_list_entry *best_entry;
2337 
2338 	if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2339 		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2340 		if (!tt_local_entry ||
2341 		    (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2342 			goto out;
2343 	}
2344 
2345 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2346 	if (!tt_global_entry)
2347 		goto out;
2348 
2349 	/* check whether the clients should not communicate due to AP
2350 	 * isolation
2351 	 */
2352 	if (tt_local_entry &&
2353 	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2354 		goto out;
2355 
2356 	rcu_read_lock();
2357 	best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2358 	/* found anything? */
2359 	if (best_entry)
2360 		orig_node = best_entry->orig_node;
2361 	if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2362 		orig_node = NULL;
2363 	rcu_read_unlock();
2364 
2365 out:
2366 	batadv_tt_global_entry_put(tt_global_entry);
2367 	batadv_tt_local_entry_put(tt_local_entry);
2368 
2369 	return orig_node;
2370 }
2371 
2372 /**
2373  * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2374  *  to the given orig_node
2375  * @bat_priv: the bat priv with all the soft interface information
2376  * @orig_node: originator for which the CRC should be computed
2377  * @vid: VLAN identifier for which the CRC32 has to be computed
2378  *
2379  * This function computes the checksum for the global table corresponding to a
2380  * specific originator. In particular, the checksum is computed as follows: For
2381  * each client connected to the originator the CRC32C of the MAC address and the
2382  * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2383  * together.
2384  *
2385  * The idea behind is that CRC32C should be used as much as possible in order to
2386  * produce a unique hash of the table, but since the order which is used to feed
2387  * the CRC32C function affects the result and since every node in the network
2388  * probably sorts the clients differently, the hash function cannot be directly
2389  * computed over the entire table. Hence the CRC32C is used only on
2390  * the single client entry, while all the results are then xor'ed together
2391  * because the XOR operation can combine them all while trying to reduce the
2392  * noise as much as possible.
2393  *
2394  * Return: the checksum of the global table of a given originator.
2395  */
2396 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2397 				struct batadv_orig_node *orig_node,
2398 				unsigned short vid)
2399 {
2400 	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2401 	struct batadv_tt_orig_list_entry *tt_orig;
2402 	struct batadv_tt_common_entry *tt_common;
2403 	struct batadv_tt_global_entry *tt_global;
2404 	struct hlist_head *head;
2405 	u32 i, crc_tmp, crc = 0;
2406 	u8 flags;
2407 	__be16 tmp_vid;
2408 
2409 	for (i = 0; i < hash->size; i++) {
2410 		head = &hash->table[i];
2411 
2412 		rcu_read_lock();
2413 		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2414 			tt_global = container_of(tt_common,
2415 						 struct batadv_tt_global_entry,
2416 						 common);
2417 			/* compute the CRC only for entries belonging to the
2418 			 * VLAN identified by the vid passed as parameter
2419 			 */
2420 			if (tt_common->vid != vid)
2421 				continue;
2422 
2423 			/* Roaming clients are in the global table for
2424 			 * consistency only. They don't have to be
2425 			 * taken into account while computing the
2426 			 * global crc
2427 			 */
2428 			if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2429 				continue;
2430 			/* Temporary clients have not been announced yet, so
2431 			 * they have to be skipped while computing the global
2432 			 * crc
2433 			 */
2434 			if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2435 				continue;
2436 
2437 			/* find out if this global entry is announced by this
2438 			 * originator
2439 			 */
2440 			tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2441 								   orig_node);
2442 			if (!tt_orig)
2443 				continue;
2444 
2445 			/* use network order to read the VID: this ensures that
2446 			 * every node reads the bytes in the same order.
2447 			 */
2448 			tmp_vid = htons(tt_common->vid);
2449 			crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2450 
2451 			/* compute the CRC on flags that have to be kept in sync
2452 			 * among nodes
2453 			 */
2454 			flags = tt_orig->flags;
2455 			crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2456 
2457 			crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2458 
2459 			batadv_tt_orig_list_entry_put(tt_orig);
2460 		}
2461 		rcu_read_unlock();
2462 	}
2463 
2464 	return crc;
2465 }
2466 
2467 /**
2468  * batadv_tt_local_crc() - calculates the checksum of the local table
2469  * @bat_priv: the bat priv with all the soft interface information
2470  * @vid: VLAN identifier for which the CRC32 has to be computed
2471  *
2472  * For details about the computation, please refer to the documentation for
2473  * batadv_tt_global_crc().
2474  *
2475  * Return: the checksum of the local table
2476  */
2477 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2478 			       unsigned short vid)
2479 {
2480 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2481 	struct batadv_tt_common_entry *tt_common;
2482 	struct hlist_head *head;
2483 	u32 i, crc_tmp, crc = 0;
2484 	u8 flags;
2485 	__be16 tmp_vid;
2486 
2487 	for (i = 0; i < hash->size; i++) {
2488 		head = &hash->table[i];
2489 
2490 		rcu_read_lock();
2491 		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2492 			/* compute the CRC only for entries belonging to the
2493 			 * VLAN identified by vid
2494 			 */
2495 			if (tt_common->vid != vid)
2496 				continue;
2497 
2498 			/* not yet committed clients have not to be taken into
2499 			 * account while computing the CRC
2500 			 */
2501 			if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2502 				continue;
2503 
2504 			/* use network order to read the VID: this ensures that
2505 			 * every node reads the bytes in the same order.
2506 			 */
2507 			tmp_vid = htons(tt_common->vid);
2508 			crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2509 
2510 			/* compute the CRC on flags that have to be kept in sync
2511 			 * among nodes
2512 			 */
2513 			flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2514 			crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2515 
2516 			crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2517 		}
2518 		rcu_read_unlock();
2519 	}
2520 
2521 	return crc;
2522 }
2523 
2524 /**
2525  * batadv_tt_req_node_release() - free tt_req node entry
2526  * @ref: kref pointer of the tt req_node entry
2527  */
2528 static void batadv_tt_req_node_release(struct kref *ref)
2529 {
2530 	struct batadv_tt_req_node *tt_req_node;
2531 
2532 	tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2533 
2534 	kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2535 }
2536 
2537 /**
2538  * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2539  *  possibly release it
2540  * @tt_req_node: tt_req_node to be free'd
2541  */
2542 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2543 {
2544 	if (!tt_req_node)
2545 		return;
2546 
2547 	kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2548 }
2549 
2550 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2551 {
2552 	struct batadv_tt_req_node *node;
2553 	struct hlist_node *safe;
2554 
2555 	spin_lock_bh(&bat_priv->tt.req_list_lock);
2556 
2557 	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2558 		hlist_del_init(&node->list);
2559 		batadv_tt_req_node_put(node);
2560 	}
2561 
2562 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2563 }
2564 
2565 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2566 				       struct batadv_orig_node *orig_node,
2567 				       const void *tt_buff,
2568 				       u16 tt_buff_len)
2569 {
2570 	/* Replace the old buffer only if I received something in the
2571 	 * last OGM (the OGM could carry no changes)
2572 	 */
2573 	spin_lock_bh(&orig_node->tt_buff_lock);
2574 	if (tt_buff_len > 0) {
2575 		kfree(orig_node->tt_buff);
2576 		orig_node->tt_buff_len = 0;
2577 		orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2578 		if (orig_node->tt_buff) {
2579 			memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2580 			orig_node->tt_buff_len = tt_buff_len;
2581 		}
2582 	}
2583 	spin_unlock_bh(&orig_node->tt_buff_lock);
2584 }
2585 
2586 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2587 {
2588 	struct batadv_tt_req_node *node;
2589 	struct hlist_node *safe;
2590 
2591 	spin_lock_bh(&bat_priv->tt.req_list_lock);
2592 	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2593 		if (batadv_has_timed_out(node->issued_at,
2594 					 BATADV_TT_REQUEST_TIMEOUT)) {
2595 			hlist_del_init(&node->list);
2596 			batadv_tt_req_node_put(node);
2597 		}
2598 	}
2599 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2600 }
2601 
2602 /**
2603  * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2604  * @bat_priv: the bat priv with all the soft interface information
2605  * @orig_node: orig node this request is being issued for
2606  *
2607  * Return: the pointer to the new tt_req_node struct if no request
2608  * has already been issued for this orig_node, NULL otherwise.
2609  */
2610 static struct batadv_tt_req_node *
2611 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2612 		       struct batadv_orig_node *orig_node)
2613 {
2614 	struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2615 
2616 	spin_lock_bh(&bat_priv->tt.req_list_lock);
2617 	hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2618 		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2619 		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2620 					  BATADV_TT_REQUEST_TIMEOUT))
2621 			goto unlock;
2622 	}
2623 
2624 	tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2625 	if (!tt_req_node)
2626 		goto unlock;
2627 
2628 	kref_init(&tt_req_node->refcount);
2629 	ether_addr_copy(tt_req_node->addr, orig_node->orig);
2630 	tt_req_node->issued_at = jiffies;
2631 
2632 	kref_get(&tt_req_node->refcount);
2633 	hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2634 unlock:
2635 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2636 	return tt_req_node;
2637 }
2638 
2639 /**
2640  * batadv_tt_local_valid() - verify local tt entry and get flags
2641  * @entry_ptr: to be checked local tt entry
2642  * @data_ptr: not used but definition required to satisfy the callback prototype
2643  * @flags: a pointer to store TT flags for this client to
2644  *
2645  * Checks the validity of the given local TT entry. If it is, then the provided
2646  * flags pointer is updated.
2647  *
2648  * Return: true if the entry is a valid, false otherwise.
2649  */
2650 static bool batadv_tt_local_valid(const void *entry_ptr,
2651 				  const void *data_ptr,
2652 				  u8 *flags)
2653 {
2654 	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2655 
2656 	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2657 		return false;
2658 
2659 	if (flags)
2660 		*flags = tt_common_entry->flags;
2661 
2662 	return true;
2663 }
2664 
2665 /**
2666  * batadv_tt_global_valid() - verify global tt entry and get flags
2667  * @entry_ptr: to be checked global tt entry
2668  * @data_ptr: an orig_node object (may be NULL)
2669  * @flags: a pointer to store TT flags for this client to
2670  *
2671  * Checks the validity of the given global TT entry. If it is, then the provided
2672  * flags pointer is updated either with the common (summed) TT flags if data_ptr
2673  * is NULL or the specific, per originator TT flags otherwise.
2674  *
2675  * Return: true if the entry is a valid, false otherwise.
2676  */
2677 static bool batadv_tt_global_valid(const void *entry_ptr,
2678 				   const void *data_ptr,
2679 				   u8 *flags)
2680 {
2681 	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2682 	const struct batadv_tt_global_entry *tt_global_entry;
2683 	const struct batadv_orig_node *orig_node = data_ptr;
2684 
2685 	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2686 	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2687 		return false;
2688 
2689 	tt_global_entry = container_of(tt_common_entry,
2690 				       struct batadv_tt_global_entry,
2691 				       common);
2692 
2693 	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2694 					       flags);
2695 }
2696 
2697 /**
2698  * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2699  *  specified tt hash
2700  * @bat_priv: the bat priv with all the soft interface information
2701  * @hash: hash table containing the tt entries
2702  * @tt_len: expected tvlv tt data buffer length in number of bytes
2703  * @tvlv_buff: pointer to the buffer to fill with the TT data
2704  * @valid_cb: function to filter tt change entries and to return TT flags
2705  * @cb_data: data passed to the filter function as argument
2706  *
2707  * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2708  * is not provided then this becomes a no-op.
2709  */
2710 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2711 				    struct batadv_hashtable *hash,
2712 				    void *tvlv_buff, u16 tt_len,
2713 				    bool (*valid_cb)(const void *,
2714 						     const void *,
2715 						     u8 *flags),
2716 				    void *cb_data)
2717 {
2718 	struct batadv_tt_common_entry *tt_common_entry;
2719 	struct batadv_tvlv_tt_change *tt_change;
2720 	struct hlist_head *head;
2721 	u16 tt_tot, tt_num_entries = 0;
2722 	u8 flags;
2723 	bool ret;
2724 	u32 i;
2725 
2726 	tt_tot = batadv_tt_entries(tt_len);
2727 	tt_change = tvlv_buff;
2728 
2729 	if (!valid_cb)
2730 		return;
2731 
2732 	rcu_read_lock();
2733 	for (i = 0; i < hash->size; i++) {
2734 		head = &hash->table[i];
2735 
2736 		hlist_for_each_entry_rcu(tt_common_entry,
2737 					 head, hash_entry) {
2738 			if (tt_tot == tt_num_entries)
2739 				break;
2740 
2741 			ret = valid_cb(tt_common_entry, cb_data, &flags);
2742 			if (!ret)
2743 				continue;
2744 
2745 			ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2746 			tt_change->flags = flags;
2747 			tt_change->vid = htons(tt_common_entry->vid);
2748 			memset(tt_change->reserved, 0,
2749 			       sizeof(tt_change->reserved));
2750 
2751 			tt_num_entries++;
2752 			tt_change++;
2753 		}
2754 	}
2755 	rcu_read_unlock();
2756 }
2757 
2758 /**
2759  * batadv_tt_global_check_crc() - check if all the CRCs are correct
2760  * @orig_node: originator for which the CRCs have to be checked
2761  * @tt_vlan: pointer to the first tvlv VLAN entry
2762  * @num_vlan: number of tvlv VLAN entries
2763  *
2764  * Return: true if all the received CRCs match the locally stored ones, false
2765  * otherwise
2766  */
2767 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2768 				       struct batadv_tvlv_tt_vlan_data *tt_vlan,
2769 				       u16 num_vlan)
2770 {
2771 	struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2772 	struct batadv_orig_node_vlan *vlan;
2773 	int i, orig_num_vlan;
2774 	u32 crc;
2775 
2776 	/* check if each received CRC matches the locally stored one */
2777 	for (i = 0; i < num_vlan; i++) {
2778 		tt_vlan_tmp = tt_vlan + i;
2779 
2780 		/* if orig_node is a backbone node for this VLAN, don't check
2781 		 * the CRC as we ignore all the global entries over it
2782 		 */
2783 		if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2784 						   orig_node->orig,
2785 						   ntohs(tt_vlan_tmp->vid)))
2786 			continue;
2787 
2788 		vlan = batadv_orig_node_vlan_get(orig_node,
2789 						 ntohs(tt_vlan_tmp->vid));
2790 		if (!vlan)
2791 			return false;
2792 
2793 		crc = vlan->tt.crc;
2794 		batadv_orig_node_vlan_put(vlan);
2795 
2796 		if (crc != ntohl(tt_vlan_tmp->crc))
2797 			return false;
2798 	}
2799 
2800 	/* check if any excess VLANs exist locally for the originator
2801 	 * which are not mentioned in the TVLV from the originator.
2802 	 */
2803 	rcu_read_lock();
2804 	orig_num_vlan = 0;
2805 	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2806 		orig_num_vlan++;
2807 	rcu_read_unlock();
2808 
2809 	if (orig_num_vlan > num_vlan)
2810 		return false;
2811 
2812 	return true;
2813 }
2814 
2815 /**
2816  * batadv_tt_local_update_crc() - update all the local CRCs
2817  * @bat_priv: the bat priv with all the soft interface information
2818  */
2819 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2820 {
2821 	struct batadv_softif_vlan *vlan;
2822 
2823 	/* recompute the global CRC for each VLAN */
2824 	rcu_read_lock();
2825 	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2826 		vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2827 	}
2828 	rcu_read_unlock();
2829 }
2830 
2831 /**
2832  * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
2833  * @bat_priv: the bat priv with all the soft interface information
2834  * @orig_node: the orig_node for which the CRCs have to be updated
2835  */
2836 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2837 					struct batadv_orig_node *orig_node)
2838 {
2839 	struct batadv_orig_node_vlan *vlan;
2840 	u32 crc;
2841 
2842 	/* recompute the global CRC for each VLAN */
2843 	rcu_read_lock();
2844 	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2845 		/* if orig_node is a backbone node for this VLAN, don't compute
2846 		 * the CRC as we ignore all the global entries over it
2847 		 */
2848 		if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2849 						   vlan->vid))
2850 			continue;
2851 
2852 		crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2853 		vlan->tt.crc = crc;
2854 	}
2855 	rcu_read_unlock();
2856 }
2857 
2858 /**
2859  * batadv_send_tt_request() - send a TT Request message to a given node
2860  * @bat_priv: the bat priv with all the soft interface information
2861  * @dst_orig_node: the destination of the message
2862  * @ttvn: the version number that the source of the message is looking for
2863  * @tt_vlan: pointer to the first tvlv VLAN object to request
2864  * @num_vlan: number of tvlv VLAN entries
2865  * @full_table: ask for the entire translation table if true, while only for the
2866  *  last TT diff otherwise
2867  *
2868  * Return: true if the TT Request was sent, false otherwise
2869  */
2870 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2871 				   struct batadv_orig_node *dst_orig_node,
2872 				   u8 ttvn,
2873 				   struct batadv_tvlv_tt_vlan_data *tt_vlan,
2874 				   u16 num_vlan, bool full_table)
2875 {
2876 	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2877 	struct batadv_tt_req_node *tt_req_node = NULL;
2878 	struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2879 	struct batadv_hard_iface *primary_if;
2880 	bool ret = false;
2881 	int i, size;
2882 
2883 	primary_if = batadv_primary_if_get_selected(bat_priv);
2884 	if (!primary_if)
2885 		goto out;
2886 
2887 	/* The new tt_req will be issued only if I'm not waiting for a
2888 	 * reply from the same orig_node yet
2889 	 */
2890 	tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2891 	if (!tt_req_node)
2892 		goto out;
2893 
2894 	size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2895 	tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2896 	if (!tvlv_tt_data)
2897 		goto out;
2898 
2899 	tvlv_tt_data->flags = BATADV_TT_REQUEST;
2900 	tvlv_tt_data->ttvn = ttvn;
2901 	tvlv_tt_data->num_vlan = htons(num_vlan);
2902 
2903 	/* send all the CRCs within the request. This is needed by intermediate
2904 	 * nodes to ensure they have the correct table before replying
2905 	 */
2906 	tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2907 	for (i = 0; i < num_vlan; i++) {
2908 		tt_vlan_req->vid = tt_vlan->vid;
2909 		tt_vlan_req->crc = tt_vlan->crc;
2910 
2911 		tt_vlan_req++;
2912 		tt_vlan++;
2913 	}
2914 
2915 	if (full_table)
2916 		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2917 
2918 	batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2919 		   dst_orig_node->orig, full_table ? 'F' : '.');
2920 
2921 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2922 	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2923 				 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2924 				 tvlv_tt_data, size);
2925 	ret = true;
2926 
2927 out:
2928 	batadv_hardif_put(primary_if);
2929 
2930 	if (ret && tt_req_node) {
2931 		spin_lock_bh(&bat_priv->tt.req_list_lock);
2932 		if (!hlist_unhashed(&tt_req_node->list)) {
2933 			hlist_del_init(&tt_req_node->list);
2934 			batadv_tt_req_node_put(tt_req_node);
2935 		}
2936 		spin_unlock_bh(&bat_priv->tt.req_list_lock);
2937 	}
2938 
2939 	batadv_tt_req_node_put(tt_req_node);
2940 
2941 	kfree(tvlv_tt_data);
2942 	return ret;
2943 }
2944 
2945 /**
2946  * batadv_send_other_tt_response() - send reply to tt request concerning another
2947  *  node's translation table
2948  * @bat_priv: the bat priv with all the soft interface information
2949  * @tt_data: tt data containing the tt request information
2950  * @req_src: mac address of tt request sender
2951  * @req_dst: mac address of tt request recipient
2952  *
2953  * Return: true if tt request reply was sent, false otherwise.
2954  */
2955 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2956 					  struct batadv_tvlv_tt_data *tt_data,
2957 					  u8 *req_src, u8 *req_dst)
2958 {
2959 	struct batadv_orig_node *req_dst_orig_node;
2960 	struct batadv_orig_node *res_dst_orig_node = NULL;
2961 	struct batadv_tvlv_tt_change *tt_change;
2962 	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2963 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
2964 	bool ret = false, full_table;
2965 	u8 orig_ttvn, req_ttvn;
2966 	u16 tvlv_len;
2967 	s32 tt_len;
2968 
2969 	batadv_dbg(BATADV_DBG_TT, bat_priv,
2970 		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2971 		   req_src, tt_data->ttvn, req_dst,
2972 		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2973 
2974 	/* Let's get the orig node of the REAL destination */
2975 	req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2976 	if (!req_dst_orig_node)
2977 		goto out;
2978 
2979 	res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2980 	if (!res_dst_orig_node)
2981 		goto out;
2982 
2983 	orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2984 	req_ttvn = tt_data->ttvn;
2985 
2986 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2987 	/* this node doesn't have the requested data */
2988 	if (orig_ttvn != req_ttvn ||
2989 	    !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2990 					ntohs(tt_data->num_vlan)))
2991 		goto out;
2992 
2993 	/* If the full table has been explicitly requested */
2994 	if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2995 	    !req_dst_orig_node->tt_buff)
2996 		full_table = true;
2997 	else
2998 		full_table = false;
2999 
3000 	/* TT fragmentation hasn't been implemented yet, so send as many
3001 	 * TT entries fit a single packet as possible only
3002 	 */
3003 	if (!full_table) {
3004 		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3005 		tt_len = req_dst_orig_node->tt_buff_len;
3006 
3007 		tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3008 							      &tvlv_tt_data,
3009 							      &tt_change,
3010 							      &tt_len);
3011 		if (!tt_len)
3012 			goto unlock;
3013 
3014 		/* Copy the last orig_node's OGM buffer */
3015 		memcpy(tt_change, req_dst_orig_node->tt_buff,
3016 		       req_dst_orig_node->tt_buff_len);
3017 		spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3018 	} else {
3019 		/* allocate the tvlv, put the tt_data and all the tt_vlan_data
3020 		 * in the initial part
3021 		 */
3022 		tt_len = -1;
3023 		tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3024 							      &tvlv_tt_data,
3025 							      &tt_change,
3026 							      &tt_len);
3027 		if (!tt_len)
3028 			goto out;
3029 
3030 		/* fill the rest of the tvlv with the real TT entries */
3031 		batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3032 					tt_change, tt_len,
3033 					batadv_tt_global_valid,
3034 					req_dst_orig_node);
3035 	}
3036 
3037 	/* Don't send the response, if larger than fragmented packet. */
3038 	tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3039 	if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3040 		net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3041 					 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3042 					 res_dst_orig_node->orig);
3043 		goto out;
3044 	}
3045 
3046 	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3047 	tvlv_tt_data->ttvn = req_ttvn;
3048 
3049 	if (full_table)
3050 		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3051 
3052 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3053 		   "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3054 		   res_dst_orig_node->orig, req_dst_orig_node->orig,
3055 		   full_table ? 'F' : '.', req_ttvn);
3056 
3057 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3058 
3059 	batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3060 				 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3061 				 tvlv_len);
3062 
3063 	ret = true;
3064 	goto out;
3065 
3066 unlock:
3067 	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3068 
3069 out:
3070 	batadv_orig_node_put(res_dst_orig_node);
3071 	batadv_orig_node_put(req_dst_orig_node);
3072 	kfree(tvlv_tt_data);
3073 	return ret;
3074 }
3075 
3076 /**
3077  * batadv_send_my_tt_response() - send reply to tt request concerning this
3078  *  node's translation table
3079  * @bat_priv: the bat priv with all the soft interface information
3080  * @tt_data: tt data containing the tt request information
3081  * @req_src: mac address of tt request sender
3082  *
3083  * Return: true if tt request reply was sent, false otherwise.
3084  */
3085 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3086 				       struct batadv_tvlv_tt_data *tt_data,
3087 				       u8 *req_src)
3088 {
3089 	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3090 	struct batadv_hard_iface *primary_if = NULL;
3091 	struct batadv_tvlv_tt_change *tt_change;
3092 	struct batadv_orig_node *orig_node;
3093 	u8 my_ttvn, req_ttvn;
3094 	u16 tvlv_len;
3095 	bool full_table;
3096 	s32 tt_len;
3097 
3098 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3099 		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3100 		   req_src, tt_data->ttvn,
3101 		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3102 
3103 	spin_lock_bh(&bat_priv->tt.commit_lock);
3104 
3105 	my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3106 	req_ttvn = tt_data->ttvn;
3107 
3108 	orig_node = batadv_orig_hash_find(bat_priv, req_src);
3109 	if (!orig_node)
3110 		goto out;
3111 
3112 	primary_if = batadv_primary_if_get_selected(bat_priv);
3113 	if (!primary_if)
3114 		goto out;
3115 
3116 	/* If the full table has been explicitly requested or the gap
3117 	 * is too big send the whole local translation table
3118 	 */
3119 	if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3120 	    !bat_priv->tt.last_changeset)
3121 		full_table = true;
3122 	else
3123 		full_table = false;
3124 
3125 	/* TT fragmentation hasn't been implemented yet, so send as many
3126 	 * TT entries fit a single packet as possible only
3127 	 */
3128 	if (!full_table) {
3129 		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3130 
3131 		tt_len = bat_priv->tt.last_changeset_len;
3132 		tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3133 							     &tvlv_tt_data,
3134 							     &tt_change,
3135 							     &tt_len);
3136 		if (!tt_len || !tvlv_len)
3137 			goto unlock;
3138 
3139 		/* Copy the last orig_node's OGM buffer */
3140 		memcpy(tt_change, bat_priv->tt.last_changeset,
3141 		       bat_priv->tt.last_changeset_len);
3142 		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3143 	} else {
3144 		req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3145 
3146 		/* allocate the tvlv, put the tt_data and all the tt_vlan_data
3147 		 * in the initial part
3148 		 */
3149 		tt_len = -1;
3150 		tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3151 							     &tvlv_tt_data,
3152 							     &tt_change,
3153 							     &tt_len);
3154 		if (!tt_len || !tvlv_len)
3155 			goto out;
3156 
3157 		/* fill the rest of the tvlv with the real TT entries */
3158 		batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3159 					tt_change, tt_len,
3160 					batadv_tt_local_valid, NULL);
3161 	}
3162 
3163 	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3164 	tvlv_tt_data->ttvn = req_ttvn;
3165 
3166 	if (full_table)
3167 		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3168 
3169 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3170 		   "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3171 		   orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3172 
3173 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3174 
3175 	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3176 				 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3177 				 tvlv_len);
3178 
3179 	goto out;
3180 
3181 unlock:
3182 	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3183 out:
3184 	spin_unlock_bh(&bat_priv->tt.commit_lock);
3185 	batadv_orig_node_put(orig_node);
3186 	batadv_hardif_put(primary_if);
3187 	kfree(tvlv_tt_data);
3188 	/* The packet was for this host, so it doesn't need to be re-routed */
3189 	return true;
3190 }
3191 
3192 /**
3193  * batadv_send_tt_response() - send reply to tt request
3194  * @bat_priv: the bat priv with all the soft interface information
3195  * @tt_data: tt data containing the tt request information
3196  * @req_src: mac address of tt request sender
3197  * @req_dst: mac address of tt request recipient
3198  *
3199  * Return: true if tt request reply was sent, false otherwise.
3200  */
3201 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3202 				    struct batadv_tvlv_tt_data *tt_data,
3203 				    u8 *req_src, u8 *req_dst)
3204 {
3205 	if (batadv_is_my_mac(bat_priv, req_dst))
3206 		return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3207 	return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3208 					     req_dst);
3209 }
3210 
3211 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3212 				      struct batadv_orig_node *orig_node,
3213 				      struct batadv_tvlv_tt_change *tt_change,
3214 				      u16 tt_num_changes, u8 ttvn)
3215 {
3216 	int i;
3217 	int roams;
3218 
3219 	for (i = 0; i < tt_num_changes; i++) {
3220 		if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3221 			roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3222 			batadv_tt_global_del(bat_priv, orig_node,
3223 					     (tt_change + i)->addr,
3224 					     ntohs((tt_change + i)->vid),
3225 					     "tt removed by changes",
3226 					     roams);
3227 		} else {
3228 			if (!batadv_tt_global_add(bat_priv, orig_node,
3229 						  (tt_change + i)->addr,
3230 						  ntohs((tt_change + i)->vid),
3231 						  (tt_change + i)->flags, ttvn))
3232 				/* In case of problem while storing a
3233 				 * global_entry, we stop the updating
3234 				 * procedure without committing the
3235 				 * ttvn change. This will avoid to send
3236 				 * corrupted data on tt_request
3237 				 */
3238 				return;
3239 		}
3240 	}
3241 	set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3242 }
3243 
3244 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3245 				  struct batadv_tvlv_tt_change *tt_change,
3246 				  u8 ttvn, u8 *resp_src,
3247 				  u16 num_entries)
3248 {
3249 	struct batadv_orig_node *orig_node;
3250 
3251 	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3252 	if (!orig_node)
3253 		goto out;
3254 
3255 	/* Purge the old table first.. */
3256 	batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3257 				  "Received full table");
3258 
3259 	_batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3260 				  ttvn);
3261 
3262 	spin_lock_bh(&orig_node->tt_buff_lock);
3263 	kfree(orig_node->tt_buff);
3264 	orig_node->tt_buff_len = 0;
3265 	orig_node->tt_buff = NULL;
3266 	spin_unlock_bh(&orig_node->tt_buff_lock);
3267 
3268 	atomic_set(&orig_node->last_ttvn, ttvn);
3269 
3270 out:
3271 	batadv_orig_node_put(orig_node);
3272 }
3273 
3274 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3275 				     struct batadv_orig_node *orig_node,
3276 				     u16 tt_num_changes, u8 ttvn,
3277 				     struct batadv_tvlv_tt_change *tt_change)
3278 {
3279 	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3280 				  tt_num_changes, ttvn);
3281 
3282 	batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3283 				   batadv_tt_len(tt_num_changes));
3284 	atomic_set(&orig_node->last_ttvn, ttvn);
3285 }
3286 
3287 /**
3288  * batadv_is_my_client() - check if a client is served by the local node
3289  * @bat_priv: the bat priv with all the soft interface information
3290  * @addr: the mac address of the client to check
3291  * @vid: VLAN identifier
3292  *
3293  * Return: true if the client is served by this node, false otherwise.
3294  */
3295 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3296 			 unsigned short vid)
3297 {
3298 	struct batadv_tt_local_entry *tt_local_entry;
3299 	bool ret = false;
3300 
3301 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3302 	if (!tt_local_entry)
3303 		goto out;
3304 	/* Check if the client has been logically deleted (but is kept for
3305 	 * consistency purpose)
3306 	 */
3307 	if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3308 	    (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3309 		goto out;
3310 	ret = true;
3311 out:
3312 	batadv_tt_local_entry_put(tt_local_entry);
3313 	return ret;
3314 }
3315 
3316 /**
3317  * batadv_handle_tt_response() - process incoming tt reply
3318  * @bat_priv: the bat priv with all the soft interface information
3319  * @tt_data: tt data containing the tt request information
3320  * @resp_src: mac address of tt reply sender
3321  * @num_entries: number of tt change entries appended to the tt data
3322  */
3323 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3324 				      struct batadv_tvlv_tt_data *tt_data,
3325 				      u8 *resp_src, u16 num_entries)
3326 {
3327 	struct batadv_tt_req_node *node;
3328 	struct hlist_node *safe;
3329 	struct batadv_orig_node *orig_node = NULL;
3330 	struct batadv_tvlv_tt_change *tt_change;
3331 	u8 *tvlv_ptr = (u8 *)tt_data;
3332 	u16 change_offset;
3333 
3334 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3335 		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3336 		   resp_src, tt_data->ttvn, num_entries,
3337 		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3338 
3339 	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3340 	if (!orig_node)
3341 		goto out;
3342 
3343 	spin_lock_bh(&orig_node->tt_lock);
3344 
3345 	change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3346 	change_offset *= ntohs(tt_data->num_vlan);
3347 	change_offset += sizeof(*tt_data);
3348 	tvlv_ptr += change_offset;
3349 
3350 	tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3351 	if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3352 		batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3353 				      resp_src, num_entries);
3354 	} else {
3355 		batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3356 					 tt_data->ttvn, tt_change);
3357 	}
3358 
3359 	/* Recalculate the CRC for this orig_node and store it */
3360 	batadv_tt_global_update_crc(bat_priv, orig_node);
3361 
3362 	spin_unlock_bh(&orig_node->tt_lock);
3363 
3364 	/* Delete the tt_req_node from pending tt_requests list */
3365 	spin_lock_bh(&bat_priv->tt.req_list_lock);
3366 	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3367 		if (!batadv_compare_eth(node->addr, resp_src))
3368 			continue;
3369 		hlist_del_init(&node->list);
3370 		batadv_tt_req_node_put(node);
3371 	}
3372 
3373 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
3374 out:
3375 	batadv_orig_node_put(orig_node);
3376 }
3377 
3378 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3379 {
3380 	struct batadv_tt_roam_node *node, *safe;
3381 
3382 	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3383 
3384 	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3385 		list_del(&node->list);
3386 		kmem_cache_free(batadv_tt_roam_cache, node);
3387 	}
3388 
3389 	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3390 }
3391 
3392 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3393 {
3394 	struct batadv_tt_roam_node *node, *safe;
3395 
3396 	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3397 	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3398 		if (!batadv_has_timed_out(node->first_time,
3399 					  BATADV_ROAMING_MAX_TIME))
3400 			continue;
3401 
3402 		list_del(&node->list);
3403 		kmem_cache_free(batadv_tt_roam_cache, node);
3404 	}
3405 	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3406 }
3407 
3408 /**
3409  * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3410  * @bat_priv: the bat priv with all the soft interface information
3411  * @client: mac address of the roaming client
3412  *
3413  * This function checks whether the client already reached the
3414  * maximum number of possible roaming phases. In this case the ROAMING_ADV
3415  * will not be sent.
3416  *
3417  * Return: true if the ROAMING_ADV can be sent, false otherwise
3418  */
3419 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3420 {
3421 	struct batadv_tt_roam_node *tt_roam_node;
3422 	bool ret = false;
3423 
3424 	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3425 	/* The new tt_req will be issued only if I'm not waiting for a
3426 	 * reply from the same orig_node yet
3427 	 */
3428 	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3429 		if (!batadv_compare_eth(tt_roam_node->addr, client))
3430 			continue;
3431 
3432 		if (batadv_has_timed_out(tt_roam_node->first_time,
3433 					 BATADV_ROAMING_MAX_TIME))
3434 			continue;
3435 
3436 		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3437 			/* Sorry, you roamed too many times! */
3438 			goto unlock;
3439 		ret = true;
3440 		break;
3441 	}
3442 
3443 	if (!ret) {
3444 		tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3445 						GFP_ATOMIC);
3446 		if (!tt_roam_node)
3447 			goto unlock;
3448 
3449 		tt_roam_node->first_time = jiffies;
3450 		atomic_set(&tt_roam_node->counter,
3451 			   BATADV_ROAMING_MAX_COUNT - 1);
3452 		ether_addr_copy(tt_roam_node->addr, client);
3453 
3454 		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3455 		ret = true;
3456 	}
3457 
3458 unlock:
3459 	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3460 	return ret;
3461 }
3462 
3463 /**
3464  * batadv_send_roam_adv() - send a roaming advertisement message
3465  * @bat_priv: the bat priv with all the soft interface information
3466  * @client: mac address of the roaming client
3467  * @vid: VLAN identifier
3468  * @orig_node: message destination
3469  *
3470  * Send a ROAMING_ADV message to the node which was previously serving this
3471  * client. This is done to inform the node that from now on all traffic destined
3472  * for this particular roamed client has to be forwarded to the sender of the
3473  * roaming message.
3474  */
3475 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3476 				 unsigned short vid,
3477 				 struct batadv_orig_node *orig_node)
3478 {
3479 	struct batadv_hard_iface *primary_if;
3480 	struct batadv_tvlv_roam_adv tvlv_roam;
3481 
3482 	primary_if = batadv_primary_if_get_selected(bat_priv);
3483 	if (!primary_if)
3484 		goto out;
3485 
3486 	/* before going on we have to check whether the client has
3487 	 * already roamed to us too many times
3488 	 */
3489 	if (!batadv_tt_check_roam_count(bat_priv, client))
3490 		goto out;
3491 
3492 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3493 		   "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3494 		   orig_node->orig, client, batadv_print_vid(vid));
3495 
3496 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3497 
3498 	memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3499 	tvlv_roam.vid = htons(vid);
3500 
3501 	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3502 				 orig_node->orig, BATADV_TVLV_ROAM, 1,
3503 				 &tvlv_roam, sizeof(tvlv_roam));
3504 
3505 out:
3506 	batadv_hardif_put(primary_if);
3507 }
3508 
3509 static void batadv_tt_purge(struct work_struct *work)
3510 {
3511 	struct delayed_work *delayed_work;
3512 	struct batadv_priv_tt *priv_tt;
3513 	struct batadv_priv *bat_priv;
3514 
3515 	delayed_work = to_delayed_work(work);
3516 	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3517 	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3518 
3519 	batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3520 	batadv_tt_global_purge(bat_priv);
3521 	batadv_tt_req_purge(bat_priv);
3522 	batadv_tt_roam_purge(bat_priv);
3523 
3524 	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3525 			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3526 }
3527 
3528 /**
3529  * batadv_tt_free() - Free translation table of soft interface
3530  * @bat_priv: the bat priv with all the soft interface information
3531  */
3532 void batadv_tt_free(struct batadv_priv *bat_priv)
3533 {
3534 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3535 
3536 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3537 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3538 
3539 	cancel_delayed_work_sync(&bat_priv->tt.work);
3540 
3541 	batadv_tt_local_table_free(bat_priv);
3542 	batadv_tt_global_table_free(bat_priv);
3543 	batadv_tt_req_list_free(bat_priv);
3544 	batadv_tt_changes_list_free(bat_priv);
3545 	batadv_tt_roam_list_free(bat_priv);
3546 
3547 	kfree(bat_priv->tt.last_changeset);
3548 }
3549 
3550 /**
3551  * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3552  *  table and possibly count them in the TT size
3553  * @bat_priv: the bat priv with all the soft interface information
3554  * @flags: the flag to switch
3555  * @enable: whether to set or unset the flag
3556  * @count: whether to increase the TT size by the number of changed entries
3557  */
3558 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3559 				      bool enable, bool count)
3560 {
3561 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3562 	struct batadv_tt_common_entry *tt_common_entry;
3563 	struct hlist_head *head;
3564 	u32 i;
3565 
3566 	if (!hash)
3567 		return;
3568 
3569 	for (i = 0; i < hash->size; i++) {
3570 		head = &hash->table[i];
3571 
3572 		rcu_read_lock();
3573 		hlist_for_each_entry_rcu(tt_common_entry,
3574 					 head, hash_entry) {
3575 			if (enable) {
3576 				if ((tt_common_entry->flags & flags) == flags)
3577 					continue;
3578 				tt_common_entry->flags |= flags;
3579 			} else {
3580 				if (!(tt_common_entry->flags & flags))
3581 					continue;
3582 				tt_common_entry->flags &= ~flags;
3583 			}
3584 
3585 			if (!count)
3586 				continue;
3587 
3588 			batadv_tt_local_size_inc(bat_priv,
3589 						 tt_common_entry->vid);
3590 		}
3591 		rcu_read_unlock();
3592 	}
3593 }
3594 
3595 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3596 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3597 {
3598 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3599 	struct batadv_tt_common_entry *tt_common;
3600 	struct batadv_tt_local_entry *tt_local;
3601 	struct hlist_node *node_tmp;
3602 	struct hlist_head *head;
3603 	spinlock_t *list_lock; /* protects write access to the hash lists */
3604 	u32 i;
3605 
3606 	if (!hash)
3607 		return;
3608 
3609 	for (i = 0; i < hash->size; i++) {
3610 		head = &hash->table[i];
3611 		list_lock = &hash->list_locks[i];
3612 
3613 		spin_lock_bh(list_lock);
3614 		hlist_for_each_entry_safe(tt_common, node_tmp, head,
3615 					  hash_entry) {
3616 			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3617 				continue;
3618 
3619 			batadv_dbg(BATADV_DBG_TT, bat_priv,
3620 				   "Deleting local tt entry (%pM, vid: %d): pending\n",
3621 				   tt_common->addr,
3622 				   batadv_print_vid(tt_common->vid));
3623 
3624 			batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3625 			hlist_del_rcu(&tt_common->hash_entry);
3626 			tt_local = container_of(tt_common,
3627 						struct batadv_tt_local_entry,
3628 						common);
3629 
3630 			batadv_tt_local_entry_put(tt_local);
3631 		}
3632 		spin_unlock_bh(list_lock);
3633 	}
3634 }
3635 
3636 /**
3637  * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3638  *  which have been queued in the time since the last commit
3639  * @bat_priv: the bat priv with all the soft interface information
3640  *
3641  * Caller must hold tt->commit_lock.
3642  */
3643 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3644 {
3645 	lockdep_assert_held(&bat_priv->tt.commit_lock);
3646 
3647 	if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3648 		if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3649 			batadv_tt_tvlv_container_update(bat_priv);
3650 		return;
3651 	}
3652 
3653 	batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3654 
3655 	batadv_tt_local_purge_pending_clients(bat_priv);
3656 	batadv_tt_local_update_crc(bat_priv);
3657 
3658 	/* Increment the TTVN only once per OGM interval */
3659 	atomic_inc(&bat_priv->tt.vn);
3660 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3661 		   "Local changes committed, updating to ttvn %u\n",
3662 		   (u8)atomic_read(&bat_priv->tt.vn));
3663 
3664 	/* reset the sending counter */
3665 	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3666 	batadv_tt_tvlv_container_update(bat_priv);
3667 }
3668 
3669 /**
3670  * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3671  *  have been queued in the time since the last commit
3672  * @bat_priv: the bat priv with all the soft interface information
3673  */
3674 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3675 {
3676 	spin_lock_bh(&bat_priv->tt.commit_lock);
3677 	batadv_tt_local_commit_changes_nolock(bat_priv);
3678 	spin_unlock_bh(&bat_priv->tt.commit_lock);
3679 }
3680 
3681 /**
3682  * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3683  * @bat_priv: the bat priv with all the soft interface information
3684  * @src: source mac address of packet
3685  * @dst: destination mac address of packet
3686  * @vid: vlan id of packet
3687  *
3688  * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3689  */
3690 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3691 			   unsigned short vid)
3692 {
3693 	struct batadv_tt_local_entry *tt_local_entry;
3694 	struct batadv_tt_global_entry *tt_global_entry;
3695 	struct batadv_softif_vlan *vlan;
3696 	bool ret = false;
3697 
3698 	vlan = batadv_softif_vlan_get(bat_priv, vid);
3699 	if (!vlan)
3700 		return false;
3701 
3702 	if (!atomic_read(&vlan->ap_isolation))
3703 		goto vlan_put;
3704 
3705 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3706 	if (!tt_local_entry)
3707 		goto vlan_put;
3708 
3709 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3710 	if (!tt_global_entry)
3711 		goto local_entry_put;
3712 
3713 	if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3714 		ret = true;
3715 
3716 	batadv_tt_global_entry_put(tt_global_entry);
3717 local_entry_put:
3718 	batadv_tt_local_entry_put(tt_local_entry);
3719 vlan_put:
3720 	batadv_softif_vlan_put(vlan);
3721 	return ret;
3722 }
3723 
3724 /**
3725  * batadv_tt_update_orig() - update global translation table with new tt
3726  *  information received via ogms
3727  * @bat_priv: the bat priv with all the soft interface information
3728  * @orig_node: the orig_node of the ogm
3729  * @tt_buff: pointer to the first tvlv VLAN entry
3730  * @tt_num_vlan: number of tvlv VLAN entries
3731  * @tt_change: pointer to the first entry in the TT buffer
3732  * @tt_num_changes: number of tt changes inside the tt buffer
3733  * @ttvn: translation table version number of this changeset
3734  */
3735 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3736 				  struct batadv_orig_node *orig_node,
3737 				  const void *tt_buff, u16 tt_num_vlan,
3738 				  struct batadv_tvlv_tt_change *tt_change,
3739 				  u16 tt_num_changes, u8 ttvn)
3740 {
3741 	u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3742 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
3743 	bool full_table = true;
3744 	bool has_tt_init;
3745 
3746 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3747 	has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3748 			       &orig_node->capa_initialized);
3749 
3750 	/* orig table not initialised AND first diff is in the OGM OR the ttvn
3751 	 * increased by one -> we can apply the attached changes
3752 	 */
3753 	if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3754 		/* the OGM could not contain the changes due to their size or
3755 		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3756 		 * times.
3757 		 * In this case send a tt request
3758 		 */
3759 		if (!tt_num_changes) {
3760 			full_table = false;
3761 			goto request_table;
3762 		}
3763 
3764 		spin_lock_bh(&orig_node->tt_lock);
3765 
3766 		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3767 					 ttvn, tt_change);
3768 
3769 		/* Even if we received the precomputed crc with the OGM, we
3770 		 * prefer to recompute it to spot any possible inconsistency
3771 		 * in the global table
3772 		 */
3773 		batadv_tt_global_update_crc(bat_priv, orig_node);
3774 
3775 		spin_unlock_bh(&orig_node->tt_lock);
3776 
3777 		/* The ttvn alone is not enough to guarantee consistency
3778 		 * because a single value could represent different states
3779 		 * (due to the wrap around). Thus a node has to check whether
3780 		 * the resulting table (after applying the changes) is still
3781 		 * consistent or not. E.g. a node could disconnect while its
3782 		 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3783 		 * checking the CRC value is mandatory to detect the
3784 		 * inconsistency
3785 		 */
3786 		if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3787 						tt_num_vlan))
3788 			goto request_table;
3789 	} else {
3790 		/* if we missed more than one change or our tables are not
3791 		 * in sync anymore -> request fresh tt data
3792 		 */
3793 		if (!has_tt_init || ttvn != orig_ttvn ||
3794 		    !batadv_tt_global_check_crc(orig_node, tt_vlan,
3795 						tt_num_vlan)) {
3796 request_table:
3797 			batadv_dbg(BATADV_DBG_TT, bat_priv,
3798 				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3799 				   orig_node->orig, ttvn, orig_ttvn,
3800 				   tt_num_changes);
3801 			batadv_send_tt_request(bat_priv, orig_node, ttvn,
3802 					       tt_vlan, tt_num_vlan,
3803 					       full_table);
3804 			return;
3805 		}
3806 	}
3807 }
3808 
3809 /**
3810  * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
3811  * @bat_priv: the bat priv with all the soft interface information
3812  * @addr: the mac address of the client to check
3813  * @vid: VLAN identifier
3814  *
3815  * Return: true if we know that the client has moved from its old originator
3816  * to another one. This entry is still kept for consistency purposes and will be
3817  * deleted later by a DEL or because of timeout
3818  */
3819 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3820 					u8 *addr, unsigned short vid)
3821 {
3822 	struct batadv_tt_global_entry *tt_global_entry;
3823 	bool ret = false;
3824 
3825 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3826 	if (!tt_global_entry)
3827 		goto out;
3828 
3829 	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3830 	batadv_tt_global_entry_put(tt_global_entry);
3831 out:
3832 	return ret;
3833 }
3834 
3835 /**
3836  * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
3837  * @bat_priv: the bat priv with all the soft interface information
3838  * @addr: the mac address of the local client to query
3839  * @vid: VLAN identifier
3840  *
3841  * Return: true if the local client is known to be roaming (it is not served by
3842  * this node anymore) or not. If yes, the client is still present in the table
3843  * to keep the latter consistent with the node TTVN
3844  */
3845 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3846 				       u8 *addr, unsigned short vid)
3847 {
3848 	struct batadv_tt_local_entry *tt_local_entry;
3849 	bool ret = false;
3850 
3851 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3852 	if (!tt_local_entry)
3853 		goto out;
3854 
3855 	ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3856 	batadv_tt_local_entry_put(tt_local_entry);
3857 out:
3858 	return ret;
3859 }
3860 
3861 /**
3862  * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
3863  * @bat_priv: the bat priv with all the soft interface information
3864  * @orig_node: orig node which the temporary entry should be associated with
3865  * @addr: mac address of the client
3866  * @vid: VLAN id of the new temporary global translation table
3867  *
3868  * Return: true when temporary tt entry could be added, false otherwise
3869  */
3870 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3871 					  struct batadv_orig_node *orig_node,
3872 					  const unsigned char *addr,
3873 					  unsigned short vid)
3874 {
3875 	/* ignore loop detect macs, they are not supposed to be in the tt local
3876 	 * data as well.
3877 	 */
3878 	if (batadv_bla_is_loopdetect_mac(addr))
3879 		return false;
3880 
3881 	if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3882 				  BATADV_TT_CLIENT_TEMP,
3883 				  atomic_read(&orig_node->last_ttvn)))
3884 		return false;
3885 
3886 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3887 		   "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3888 		   addr, batadv_print_vid(vid), orig_node->orig);
3889 
3890 	return true;
3891 }
3892 
3893 /**
3894  * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
3895  *  maximum packet size that can be transported through the mesh
3896  * @soft_iface: netdev struct of the mesh interface
3897  *
3898  * Remove entries older than 'timeout' and half timeout if more entries need
3899  * to be removed.
3900  */
3901 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3902 {
3903 	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3904 	int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3905 	int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3906 	bool reduced = false;
3907 
3908 	spin_lock_bh(&bat_priv->tt.commit_lock);
3909 
3910 	while (timeout) {
3911 		table_size = batadv_tt_local_table_transmit_size(bat_priv);
3912 		if (packet_size_max >= table_size)
3913 			break;
3914 
3915 		batadv_tt_local_purge(bat_priv, timeout);
3916 		batadv_tt_local_purge_pending_clients(bat_priv);
3917 
3918 		timeout /= 2;
3919 		reduced = true;
3920 		net_ratelimited_function(batadv_info, soft_iface,
3921 					 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3922 					 packet_size_max);
3923 	}
3924 
3925 	/* commit these changes immediately, to avoid synchronization problem
3926 	 * with the TTVN
3927 	 */
3928 	if (reduced)
3929 		batadv_tt_local_commit_changes_nolock(bat_priv);
3930 
3931 	spin_unlock_bh(&bat_priv->tt.commit_lock);
3932 }
3933 
3934 /**
3935  * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
3936  * @bat_priv: the bat priv with all the soft interface information
3937  * @orig: the orig_node of the ogm
3938  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3939  * @tvlv_value: tvlv buffer containing the gateway data
3940  * @tvlv_value_len: tvlv buffer length
3941  */
3942 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3943 					  struct batadv_orig_node *orig,
3944 					  u8 flags, void *tvlv_value,
3945 					  u16 tvlv_value_len)
3946 {
3947 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
3948 	struct batadv_tvlv_tt_change *tt_change;
3949 	struct batadv_tvlv_tt_data *tt_data;
3950 	u16 num_entries, num_vlan;
3951 
3952 	if (tvlv_value_len < sizeof(*tt_data))
3953 		return;
3954 
3955 	tt_data = tvlv_value;
3956 	tvlv_value_len -= sizeof(*tt_data);
3957 
3958 	num_vlan = ntohs(tt_data->num_vlan);
3959 
3960 	if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3961 		return;
3962 
3963 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3964 	tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3965 	tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3966 
3967 	num_entries = batadv_tt_entries(tvlv_value_len);
3968 
3969 	batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3970 			      num_entries, tt_data->ttvn);
3971 }
3972 
3973 /**
3974  * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
3975  *  container
3976  * @bat_priv: the bat priv with all the soft interface information
3977  * @src: mac address of tt tvlv sender
3978  * @dst: mac address of tt tvlv recipient
3979  * @tvlv_value: tvlv buffer containing the tt data
3980  * @tvlv_value_len: tvlv buffer length
3981  *
3982  * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3983  * otherwise.
3984  */
3985 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3986 					     u8 *src, u8 *dst,
3987 					     void *tvlv_value,
3988 					     u16 tvlv_value_len)
3989 {
3990 	struct batadv_tvlv_tt_data *tt_data;
3991 	u16 tt_vlan_len, tt_num_entries;
3992 	char tt_flag;
3993 	bool ret;
3994 
3995 	if (tvlv_value_len < sizeof(*tt_data))
3996 		return NET_RX_SUCCESS;
3997 
3998 	tt_data = tvlv_value;
3999 	tvlv_value_len -= sizeof(*tt_data);
4000 
4001 	tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4002 	tt_vlan_len *= ntohs(tt_data->num_vlan);
4003 
4004 	if (tvlv_value_len < tt_vlan_len)
4005 		return NET_RX_SUCCESS;
4006 
4007 	tvlv_value_len -= tt_vlan_len;
4008 	tt_num_entries = batadv_tt_entries(tvlv_value_len);
4009 
4010 	switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4011 	case BATADV_TT_REQUEST:
4012 		batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4013 
4014 		/* If this node cannot provide a TT response the tt_request is
4015 		 * forwarded
4016 		 */
4017 		ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4018 		if (!ret) {
4019 			if (tt_data->flags & BATADV_TT_FULL_TABLE)
4020 				tt_flag = 'F';
4021 			else
4022 				tt_flag = '.';
4023 
4024 			batadv_dbg(BATADV_DBG_TT, bat_priv,
4025 				   "Routing TT_REQUEST to %pM [%c]\n",
4026 				   dst, tt_flag);
4027 			/* tvlv API will re-route the packet */
4028 			return NET_RX_DROP;
4029 		}
4030 		break;
4031 	case BATADV_TT_RESPONSE:
4032 		batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4033 
4034 		if (batadv_is_my_mac(bat_priv, dst)) {
4035 			batadv_handle_tt_response(bat_priv, tt_data,
4036 						  src, tt_num_entries);
4037 			return NET_RX_SUCCESS;
4038 		}
4039 
4040 		if (tt_data->flags & BATADV_TT_FULL_TABLE)
4041 			tt_flag =  'F';
4042 		else
4043 			tt_flag = '.';
4044 
4045 		batadv_dbg(BATADV_DBG_TT, bat_priv,
4046 			   "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4047 
4048 		/* tvlv API will re-route the packet */
4049 		return NET_RX_DROP;
4050 	}
4051 
4052 	return NET_RX_SUCCESS;
4053 }
4054 
4055 /**
4056  * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4057  *  container
4058  * @bat_priv: the bat priv with all the soft interface information
4059  * @src: mac address of tt tvlv sender
4060  * @dst: mac address of tt tvlv recipient
4061  * @tvlv_value: tvlv buffer containing the tt data
4062  * @tvlv_value_len: tvlv buffer length
4063  *
4064  * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4065  * otherwise.
4066  */
4067 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4068 					       u8 *src, u8 *dst,
4069 					       void *tvlv_value,
4070 					       u16 tvlv_value_len)
4071 {
4072 	struct batadv_tvlv_roam_adv *roaming_adv;
4073 	struct batadv_orig_node *orig_node = NULL;
4074 
4075 	/* If this node is not the intended recipient of the
4076 	 * roaming advertisement the packet is forwarded
4077 	 * (the tvlv API will re-route the packet).
4078 	 */
4079 	if (!batadv_is_my_mac(bat_priv, dst))
4080 		return NET_RX_DROP;
4081 
4082 	if (tvlv_value_len < sizeof(*roaming_adv))
4083 		goto out;
4084 
4085 	orig_node = batadv_orig_hash_find(bat_priv, src);
4086 	if (!orig_node)
4087 		goto out;
4088 
4089 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4090 	roaming_adv = tvlv_value;
4091 
4092 	batadv_dbg(BATADV_DBG_TT, bat_priv,
4093 		   "Received ROAMING_ADV from %pM (client %pM)\n",
4094 		   src, roaming_adv->client);
4095 
4096 	batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4097 			     ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4098 			     atomic_read(&orig_node->last_ttvn) + 1);
4099 
4100 out:
4101 	batadv_orig_node_put(orig_node);
4102 	return NET_RX_SUCCESS;
4103 }
4104 
4105 /**
4106  * batadv_tt_init() - initialise the translation table internals
4107  * @bat_priv: the bat priv with all the soft interface information
4108  *
4109  * Return: 0 on success or negative error number in case of failure.
4110  */
4111 int batadv_tt_init(struct batadv_priv *bat_priv)
4112 {
4113 	int ret;
4114 
4115 	/* synchronized flags must be remote */
4116 	BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4117 
4118 	ret = batadv_tt_local_init(bat_priv);
4119 	if (ret < 0)
4120 		return ret;
4121 
4122 	ret = batadv_tt_global_init(bat_priv);
4123 	if (ret < 0) {
4124 		batadv_tt_local_table_free(bat_priv);
4125 		return ret;
4126 	}
4127 
4128 	batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4129 				     batadv_tt_tvlv_unicast_handler_v1, NULL,
4130 				     BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4131 
4132 	batadv_tvlv_handler_register(bat_priv, NULL,
4133 				     batadv_roam_tvlv_unicast_handler_v1, NULL,
4134 				     BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4135 
4136 	INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4137 	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4138 			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4139 
4140 	return 1;
4141 }
4142 
4143 /**
4144  * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4145  * @bat_priv: the bat priv with all the soft interface information
4146  * @addr: the mac address of the client
4147  * @vid: the identifier of the VLAN where this client is connected
4148  *
4149  * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4150  * otherwise
4151  */
4152 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4153 				  const u8 *addr, unsigned short vid)
4154 {
4155 	struct batadv_tt_global_entry *tt;
4156 	bool ret;
4157 
4158 	tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4159 	if (!tt)
4160 		return false;
4161 
4162 	ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4163 
4164 	batadv_tt_global_entry_put(tt);
4165 
4166 	return ret;
4167 }
4168 
4169 /**
4170  * batadv_tt_cache_init() - Initialize tt memory object cache
4171  *
4172  * Return: 0 on success or negative error number in case of failure.
4173  */
4174 int __init batadv_tt_cache_init(void)
4175 {
4176 	size_t tl_size = sizeof(struct batadv_tt_local_entry);
4177 	size_t tg_size = sizeof(struct batadv_tt_global_entry);
4178 	size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4179 	size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4180 	size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4181 	size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4182 
4183 	batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4184 					    SLAB_HWCACHE_ALIGN, NULL);
4185 	if (!batadv_tl_cache)
4186 		return -ENOMEM;
4187 
4188 	batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4189 					    SLAB_HWCACHE_ALIGN, NULL);
4190 	if (!batadv_tg_cache)
4191 		goto err_tt_tl_destroy;
4192 
4193 	batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4194 						 tt_orig_size, 0,
4195 						 SLAB_HWCACHE_ALIGN, NULL);
4196 	if (!batadv_tt_orig_cache)
4197 		goto err_tt_tg_destroy;
4198 
4199 	batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4200 						   tt_change_size, 0,
4201 						   SLAB_HWCACHE_ALIGN, NULL);
4202 	if (!batadv_tt_change_cache)
4203 		goto err_tt_orig_destroy;
4204 
4205 	batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4206 						tt_req_size, 0,
4207 						SLAB_HWCACHE_ALIGN, NULL);
4208 	if (!batadv_tt_req_cache)
4209 		goto err_tt_change_destroy;
4210 
4211 	batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4212 						 tt_roam_size, 0,
4213 						 SLAB_HWCACHE_ALIGN, NULL);
4214 	if (!batadv_tt_roam_cache)
4215 		goto err_tt_req_destroy;
4216 
4217 	return 0;
4218 
4219 err_tt_req_destroy:
4220 	kmem_cache_destroy(batadv_tt_req_cache);
4221 	batadv_tt_req_cache = NULL;
4222 err_tt_change_destroy:
4223 	kmem_cache_destroy(batadv_tt_change_cache);
4224 	batadv_tt_change_cache = NULL;
4225 err_tt_orig_destroy:
4226 	kmem_cache_destroy(batadv_tt_orig_cache);
4227 	batadv_tt_orig_cache = NULL;
4228 err_tt_tg_destroy:
4229 	kmem_cache_destroy(batadv_tg_cache);
4230 	batadv_tg_cache = NULL;
4231 err_tt_tl_destroy:
4232 	kmem_cache_destroy(batadv_tl_cache);
4233 	batadv_tl_cache = NULL;
4234 
4235 	return -ENOMEM;
4236 }
4237 
4238 /**
4239  * batadv_tt_cache_destroy() - Destroy tt memory object cache
4240  */
4241 void batadv_tt_cache_destroy(void)
4242 {
4243 	kmem_cache_destroy(batadv_tl_cache);
4244 	kmem_cache_destroy(batadv_tg_cache);
4245 	kmem_cache_destroy(batadv_tt_orig_cache);
4246 	kmem_cache_destroy(batadv_tt_change_cache);
4247 	kmem_cache_destroy(batadv_tt_req_cache);
4248 	kmem_cache_destroy(batadv_tt_roam_cache);
4249 }
4250