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