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