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