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