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 	u16 tvlv_len;
994 
995 	tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
996 	tt_diff_len = batadv_tt_len(tt_diff_entries_num);
997 
998 	/* if we have too many changes for one packet don't send any
999 	 * and wait for the tt table request which will be fragmented
1000 	 */
1001 	if (tt_diff_len > bat_priv->soft_iface->mtu)
1002 		tt_diff_len = 0;
1003 
1004 	tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1005 						     &tt_change, &tt_diff_len);
1006 	if (!tvlv_len)
1007 		return;
1008 
1009 	tt_data->flags = BATADV_TT_OGM_DIFF;
1010 
1011 	if (tt_diff_len == 0)
1012 		goto container_register;
1013 
1014 	spin_lock_bh(&bat_priv->tt.changes_list_lock);
1015 	atomic_set(&bat_priv->tt.local_changes, 0);
1016 
1017 	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1018 				 list) {
1019 		if (tt_diff_entries_count < tt_diff_entries_num) {
1020 			memcpy(tt_change + tt_diff_entries_count,
1021 			       &entry->change,
1022 			       sizeof(struct batadv_tvlv_tt_change));
1023 			tt_diff_entries_count++;
1024 		}
1025 		list_del(&entry->list);
1026 		kmem_cache_free(batadv_tt_change_cache, entry);
1027 	}
1028 	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1029 
1030 	/* Keep the buffer for possible tt_request */
1031 	spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1032 	kfree(bat_priv->tt.last_changeset);
1033 	bat_priv->tt.last_changeset_len = 0;
1034 	bat_priv->tt.last_changeset = NULL;
1035 	tt_change_len = batadv_tt_len(tt_diff_entries_count);
1036 	/* check whether this new OGM has no changes due to size problems */
1037 	if (tt_diff_entries_count > 0) {
1038 		/* if kmalloc() fails we will reply with the full table
1039 		 * instead of providing the diff
1040 		 */
1041 		bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1042 		if (bat_priv->tt.last_changeset) {
1043 			memcpy(bat_priv->tt.last_changeset,
1044 			       tt_change, tt_change_len);
1045 			bat_priv->tt.last_changeset_len = tt_diff_len;
1046 		}
1047 	}
1048 	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1049 
1050 container_register:
1051 	batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1052 				       tvlv_len);
1053 	kfree(tt_data);
1054 }
1055 
1056 /**
1057  * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1058  * @msg :Netlink message to dump into
1059  * @portid: Port making netlink request
1060  * @cb: Control block containing additional options
1061  * @bat_priv: The bat priv with all the soft interface information
1062  * @common: tt local & tt global common data
1063  *
1064  * Return: Error code, or 0 on success
1065  */
1066 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)1067 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1068 			   struct netlink_callback *cb,
1069 			   struct batadv_priv *bat_priv,
1070 			   struct batadv_tt_common_entry *common)
1071 {
1072 	void *hdr;
1073 	struct batadv_softif_vlan *vlan;
1074 	struct batadv_tt_local_entry *local;
1075 	unsigned int last_seen_msecs;
1076 	u32 crc;
1077 
1078 	local = container_of(common, struct batadv_tt_local_entry, common);
1079 	last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1080 
1081 	vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1082 	if (!vlan)
1083 		return 0;
1084 
1085 	crc = vlan->tt.crc;
1086 
1087 	batadv_softif_vlan_put(vlan);
1088 
1089 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1090 			  &batadv_netlink_family,  NLM_F_MULTI,
1091 			  BATADV_CMD_GET_TRANSTABLE_LOCAL);
1092 	if (!hdr)
1093 		return -ENOBUFS;
1094 
1095 	genl_dump_check_consistent(cb, hdr);
1096 
1097 	if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1098 	    nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1099 	    nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1100 	    nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1101 		goto nla_put_failure;
1102 
1103 	if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1104 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1105 		goto nla_put_failure;
1106 
1107 	genlmsg_end(msg, hdr);
1108 	return 0;
1109 
1110  nla_put_failure:
1111 	genlmsg_cancel(msg, hdr);
1112 	return -EMSGSIZE;
1113 }
1114 
1115 /**
1116  * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1117  * @msg: Netlink message to dump into
1118  * @portid: Port making netlink request
1119  * @cb: Control block containing additional options
1120  * @bat_priv: The bat priv with all the soft interface information
1121  * @hash: hash to dump
1122  * @bucket: bucket index to dump
1123  * @idx_s: Number of entries to skip
1124  *
1125  * Return: Error code, or 0 on success
1126  */
1127 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)1128 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1129 			    struct netlink_callback *cb,
1130 			    struct batadv_priv *bat_priv,
1131 			    struct batadv_hashtable *hash, unsigned int bucket,
1132 			    int *idx_s)
1133 {
1134 	struct batadv_tt_common_entry *common;
1135 	int idx = 0;
1136 
1137 	spin_lock_bh(&hash->list_locks[bucket]);
1138 	cb->seq = atomic_read(&hash->generation) << 1 | 1;
1139 
1140 	hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1141 		if (idx++ < *idx_s)
1142 			continue;
1143 
1144 		if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1145 					       common)) {
1146 			spin_unlock_bh(&hash->list_locks[bucket]);
1147 			*idx_s = idx - 1;
1148 			return -EMSGSIZE;
1149 		}
1150 	}
1151 	spin_unlock_bh(&hash->list_locks[bucket]);
1152 
1153 	*idx_s = 0;
1154 	return 0;
1155 }
1156 
1157 /**
1158  * batadv_tt_local_dump() - Dump TT local entries into a message
1159  * @msg: Netlink message to dump into
1160  * @cb: Parameters from query
1161  *
1162  * Return: Error code, or 0 on success
1163  */
batadv_tt_local_dump(struct sk_buff * msg,struct netlink_callback * cb)1164 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1165 {
1166 	struct net *net = sock_net(cb->skb->sk);
1167 	struct net_device *soft_iface;
1168 	struct batadv_priv *bat_priv;
1169 	struct batadv_hard_iface *primary_if = NULL;
1170 	struct batadv_hashtable *hash;
1171 	int ret;
1172 	int ifindex;
1173 	int bucket = cb->args[0];
1174 	int idx = cb->args[1];
1175 	int portid = NETLINK_CB(cb->skb).portid;
1176 
1177 	ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1178 	if (!ifindex)
1179 		return -EINVAL;
1180 
1181 	soft_iface = dev_get_by_index(net, ifindex);
1182 	if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1183 		ret = -ENODEV;
1184 		goto out;
1185 	}
1186 
1187 	bat_priv = netdev_priv(soft_iface);
1188 
1189 	primary_if = batadv_primary_if_get_selected(bat_priv);
1190 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1191 		ret = -ENOENT;
1192 		goto out;
1193 	}
1194 
1195 	hash = bat_priv->tt.local_hash;
1196 
1197 	while (bucket < hash->size) {
1198 		if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1199 						hash, bucket, &idx))
1200 			break;
1201 
1202 		bucket++;
1203 	}
1204 
1205 	ret = msg->len;
1206 
1207  out:
1208 	batadv_hardif_put(primary_if);
1209 	dev_put(soft_iface);
1210 
1211 	cb->args[0] = bucket;
1212 	cb->args[1] = idx;
1213 
1214 	return ret;
1215 }
1216 
1217 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)1218 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1219 			    struct batadv_tt_local_entry *tt_local_entry,
1220 			    u16 flags, const char *message)
1221 {
1222 	batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1223 
1224 	/* The local client has to be marked as "pending to be removed" but has
1225 	 * to be kept in the table in order to send it in a full table
1226 	 * response issued before the net ttvn increment (consistency check)
1227 	 */
1228 	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1229 
1230 	batadv_dbg(BATADV_DBG_TT, bat_priv,
1231 		   "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1232 		   tt_local_entry->common.addr,
1233 		   batadv_print_vid(tt_local_entry->common.vid), message);
1234 }
1235 
1236 /**
1237  * batadv_tt_local_remove() - logically remove an entry from the local table
1238  * @bat_priv: the bat priv with all the soft interface information
1239  * @addr: the MAC address of the client to remove
1240  * @vid: VLAN identifier
1241  * @message: message to append to the log on deletion
1242  * @roaming: true if the deletion is due to a roaming event
1243  *
1244  * Return: the flags assigned to the local entry before being deleted
1245  */
batadv_tt_local_remove(struct batadv_priv * bat_priv,const u8 * addr,unsigned short vid,const char * message,bool roaming)1246 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1247 			   unsigned short vid, const char *message,
1248 			   bool roaming)
1249 {
1250 	struct batadv_tt_local_entry *tt_removed_entry;
1251 	struct batadv_tt_local_entry *tt_local_entry;
1252 	u16 flags, curr_flags = BATADV_NO_FLAGS;
1253 	struct hlist_node *tt_removed_node;
1254 
1255 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1256 	if (!tt_local_entry)
1257 		goto out;
1258 
1259 	curr_flags = tt_local_entry->common.flags;
1260 
1261 	flags = BATADV_TT_CLIENT_DEL;
1262 	/* if this global entry addition is due to a roaming, the node has to
1263 	 * mark the local entry as "roamed" in order to correctly reroute
1264 	 * packets later
1265 	 */
1266 	if (roaming) {
1267 		flags |= BATADV_TT_CLIENT_ROAM;
1268 		/* mark the local client as ROAMed */
1269 		tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1270 	}
1271 
1272 	if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1273 		batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1274 					    message);
1275 		goto out;
1276 	}
1277 	/* if this client has been added right now, it is possible to
1278 	 * immediately purge it
1279 	 */
1280 	batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1281 
1282 	tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1283 					     batadv_compare_tt,
1284 					     batadv_choose_tt,
1285 					     &tt_local_entry->common);
1286 	if (!tt_removed_node)
1287 		goto out;
1288 
1289 	/* drop reference of remove hash entry */
1290 	tt_removed_entry = hlist_entry(tt_removed_node,
1291 				       struct batadv_tt_local_entry,
1292 				       common.hash_entry);
1293 	batadv_tt_local_entry_put(tt_removed_entry);
1294 
1295 out:
1296 	batadv_tt_local_entry_put(tt_local_entry);
1297 
1298 	return curr_flags;
1299 }
1300 
1301 /**
1302  * batadv_tt_local_purge_list() - purge inactive tt local entries
1303  * @bat_priv: the bat priv with all the soft interface information
1304  * @head: pointer to the list containing the local tt entries
1305  * @timeout: parameter deciding whether a given tt local entry is considered
1306  *  inactive or not
1307  */
batadv_tt_local_purge_list(struct batadv_priv * bat_priv,struct hlist_head * head,int timeout)1308 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1309 				       struct hlist_head *head,
1310 				       int timeout)
1311 {
1312 	struct batadv_tt_local_entry *tt_local_entry;
1313 	struct batadv_tt_common_entry *tt_common_entry;
1314 	struct hlist_node *node_tmp;
1315 
1316 	hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1317 				  hash_entry) {
1318 		tt_local_entry = container_of(tt_common_entry,
1319 					      struct batadv_tt_local_entry,
1320 					      common);
1321 		if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1322 			continue;
1323 
1324 		/* entry already marked for deletion */
1325 		if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1326 			continue;
1327 
1328 		if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1329 			continue;
1330 
1331 		batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1332 					    BATADV_TT_CLIENT_DEL, "timed out");
1333 	}
1334 }
1335 
1336 /**
1337  * batadv_tt_local_purge() - purge inactive tt local entries
1338  * @bat_priv: the bat priv with all the soft interface information
1339  * @timeout: parameter deciding whether a given tt local entry is considered
1340  *  inactive or not
1341  */
batadv_tt_local_purge(struct batadv_priv * bat_priv,int timeout)1342 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1343 				  int timeout)
1344 {
1345 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1346 	struct hlist_head *head;
1347 	spinlock_t *list_lock; /* protects write access to the hash lists */
1348 	u32 i;
1349 
1350 	for (i = 0; i < hash->size; i++) {
1351 		head = &hash->table[i];
1352 		list_lock = &hash->list_locks[i];
1353 
1354 		spin_lock_bh(list_lock);
1355 		batadv_tt_local_purge_list(bat_priv, head, timeout);
1356 		spin_unlock_bh(list_lock);
1357 	}
1358 }
1359 
batadv_tt_local_table_free(struct batadv_priv * bat_priv)1360 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1361 {
1362 	struct batadv_hashtable *hash;
1363 	spinlock_t *list_lock; /* protects write access to the hash lists */
1364 	struct batadv_tt_common_entry *tt_common_entry;
1365 	struct batadv_tt_local_entry *tt_local;
1366 	struct hlist_node *node_tmp;
1367 	struct hlist_head *head;
1368 	u32 i;
1369 
1370 	if (!bat_priv->tt.local_hash)
1371 		return;
1372 
1373 	hash = bat_priv->tt.local_hash;
1374 
1375 	for (i = 0; i < hash->size; i++) {
1376 		head = &hash->table[i];
1377 		list_lock = &hash->list_locks[i];
1378 
1379 		spin_lock_bh(list_lock);
1380 		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1381 					  head, hash_entry) {
1382 			hlist_del_rcu(&tt_common_entry->hash_entry);
1383 			tt_local = container_of(tt_common_entry,
1384 						struct batadv_tt_local_entry,
1385 						common);
1386 
1387 			batadv_tt_local_entry_put(tt_local);
1388 		}
1389 		spin_unlock_bh(list_lock);
1390 	}
1391 
1392 	batadv_hash_destroy(hash);
1393 
1394 	bat_priv->tt.local_hash = NULL;
1395 }
1396 
batadv_tt_global_init(struct batadv_priv * bat_priv)1397 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1398 {
1399 	if (bat_priv->tt.global_hash)
1400 		return 0;
1401 
1402 	bat_priv->tt.global_hash = batadv_hash_new(1024);
1403 
1404 	if (!bat_priv->tt.global_hash)
1405 		return -ENOMEM;
1406 
1407 	batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1408 				   &batadv_tt_global_hash_lock_class_key);
1409 
1410 	return 0;
1411 }
1412 
batadv_tt_changes_list_free(struct batadv_priv * bat_priv)1413 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1414 {
1415 	struct batadv_tt_change_node *entry, *safe;
1416 
1417 	spin_lock_bh(&bat_priv->tt.changes_list_lock);
1418 
1419 	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1420 				 list) {
1421 		list_del(&entry->list);
1422 		kmem_cache_free(batadv_tt_change_cache, entry);
1423 	}
1424 
1425 	atomic_set(&bat_priv->tt.local_changes, 0);
1426 	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1427 }
1428 
1429 /**
1430  * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1431  * @entry: the TT global entry where the orig_list_entry has to be
1432  *  extracted from
1433  * @orig_node: the originator for which the orig_list_entry has to be found
1434  *
1435  * retrieve the orig_tt_list_entry belonging to orig_node from the
1436  * batadv_tt_global_entry list
1437  *
1438  * Return: it with an increased refcounter, NULL if not found
1439  */
1440 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)1441 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1442 				 const struct batadv_orig_node *orig_node)
1443 {
1444 	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1445 	const struct hlist_head *head;
1446 
1447 	rcu_read_lock();
1448 	head = &entry->orig_list;
1449 	hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1450 		if (tmp_orig_entry->orig_node != orig_node)
1451 			continue;
1452 		if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1453 			continue;
1454 
1455 		orig_entry = tmp_orig_entry;
1456 		break;
1457 	}
1458 	rcu_read_unlock();
1459 
1460 	return orig_entry;
1461 }
1462 
1463 /**
1464  * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1465  *  handled by a given originator
1466  * @entry: the TT global entry to check
1467  * @orig_node: the originator to search in the list
1468  * @flags: a pointer to store TT flags for the given @entry received
1469  *  from @orig_node
1470  *
1471  * find out if an orig_node is already in the list of a tt_global_entry.
1472  *
1473  * Return: true if found, false otherwise
1474  */
1475 static bool
batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry * entry,const struct batadv_orig_node * orig_node,u8 * flags)1476 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1477 				const struct batadv_orig_node *orig_node,
1478 				u8 *flags)
1479 {
1480 	struct batadv_tt_orig_list_entry *orig_entry;
1481 	bool found = false;
1482 
1483 	orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1484 	if (orig_entry) {
1485 		found = true;
1486 
1487 		if (flags)
1488 			*flags = orig_entry->flags;
1489 
1490 		batadv_tt_orig_list_entry_put(orig_entry);
1491 	}
1492 
1493 	return found;
1494 }
1495 
1496 /**
1497  * batadv_tt_global_sync_flags() - update TT sync flags
1498  * @tt_global: the TT global entry to update sync flags in
1499  *
1500  * Updates the sync flag bits in the tt_global flag attribute with a logical
1501  * OR of all sync flags from any of its TT orig entries.
1502  */
1503 static void
batadv_tt_global_sync_flags(struct batadv_tt_global_entry * tt_global)1504 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1505 {
1506 	struct batadv_tt_orig_list_entry *orig_entry;
1507 	const struct hlist_head *head;
1508 	u16 flags = BATADV_NO_FLAGS;
1509 
1510 	rcu_read_lock();
1511 	head = &tt_global->orig_list;
1512 	hlist_for_each_entry_rcu(orig_entry, head, list)
1513 		flags |= orig_entry->flags;
1514 	rcu_read_unlock();
1515 
1516 	flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1517 	tt_global->common.flags = flags;
1518 }
1519 
1520 /**
1521  * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1522  * @tt_global: the TT global entry to add an orig entry in
1523  * @orig_node: the originator to add an orig entry for
1524  * @ttvn: translation table version number of this changeset
1525  * @flags: TT sync flags
1526  */
1527 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)1528 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1529 				struct batadv_orig_node *orig_node, int ttvn,
1530 				u8 flags)
1531 {
1532 	struct batadv_tt_orig_list_entry *orig_entry;
1533 
1534 	spin_lock_bh(&tt_global->list_lock);
1535 
1536 	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1537 	if (orig_entry) {
1538 		/* refresh the ttvn: the current value could be a bogus one that
1539 		 * was added during a "temporary client detection"
1540 		 */
1541 		orig_entry->ttvn = ttvn;
1542 		orig_entry->flags = flags;
1543 		goto sync_flags;
1544 	}
1545 
1546 	orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1547 	if (!orig_entry)
1548 		goto out;
1549 
1550 	INIT_HLIST_NODE(&orig_entry->list);
1551 	kref_get(&orig_node->refcount);
1552 	batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1553 	orig_entry->orig_node = orig_node;
1554 	orig_entry->ttvn = ttvn;
1555 	orig_entry->flags = flags;
1556 	kref_init(&orig_entry->refcount);
1557 
1558 	kref_get(&orig_entry->refcount);
1559 	hlist_add_head_rcu(&orig_entry->list,
1560 			   &tt_global->orig_list);
1561 	atomic_inc(&tt_global->orig_list_count);
1562 
1563 sync_flags:
1564 	batadv_tt_global_sync_flags(tt_global);
1565 out:
1566 	batadv_tt_orig_list_entry_put(orig_entry);
1567 
1568 	spin_unlock_bh(&tt_global->list_lock);
1569 }
1570 
1571 /**
1572  * batadv_tt_global_add() - add a new TT global entry or update an existing one
1573  * @bat_priv: the bat priv with all the soft interface information
1574  * @orig_node: the originator announcing the client
1575  * @tt_addr: the mac address of the non-mesh client
1576  * @vid: VLAN identifier
1577  * @flags: TT flags that have to be set for this non-mesh client
1578  * @ttvn: the tt version number ever announcing this non-mesh client
1579  *
1580  * Add a new TT global entry for the given originator. If the entry already
1581  * exists add a new reference to the given originator (a global entry can have
1582  * references to multiple originators) and adjust the flags attribute to reflect
1583  * the function argument.
1584  * If a TT local entry exists for this non-mesh client remove it.
1585  *
1586  * The caller must hold the orig_node refcount.
1587  *
1588  * Return: true if the new entry has been added, false otherwise
1589  */
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)1590 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1591 				 struct batadv_orig_node *orig_node,
1592 				 const unsigned char *tt_addr,
1593 				 unsigned short vid, u16 flags, u8 ttvn)
1594 {
1595 	struct batadv_tt_global_entry *tt_global_entry;
1596 	struct batadv_tt_local_entry *tt_local_entry;
1597 	bool ret = false;
1598 	int hash_added;
1599 	struct batadv_tt_common_entry *common;
1600 	u16 local_flags;
1601 
1602 	/* ignore global entries from backbone nodes */
1603 	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1604 		return true;
1605 
1606 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1607 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1608 
1609 	/* if the node already has a local client for this entry, it has to wait
1610 	 * for a roaming advertisement instead of manually messing up the global
1611 	 * table
1612 	 */
1613 	if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1614 	    !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1615 		goto out;
1616 
1617 	if (!tt_global_entry) {
1618 		tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1619 						    GFP_ATOMIC);
1620 		if (!tt_global_entry)
1621 			goto out;
1622 
1623 		common = &tt_global_entry->common;
1624 		ether_addr_copy(common->addr, tt_addr);
1625 		common->vid = vid;
1626 
1627 		if (!is_multicast_ether_addr(common->addr))
1628 			common->flags = flags & (~BATADV_TT_SYNC_MASK);
1629 
1630 		tt_global_entry->roam_at = 0;
1631 		/* node must store current time in case of roaming. This is
1632 		 * needed to purge this entry out on timeout (if nobody claims
1633 		 * it)
1634 		 */
1635 		if (flags & BATADV_TT_CLIENT_ROAM)
1636 			tt_global_entry->roam_at = jiffies;
1637 		kref_init(&common->refcount);
1638 		common->added_at = jiffies;
1639 
1640 		INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1641 		atomic_set(&tt_global_entry->orig_list_count, 0);
1642 		spin_lock_init(&tt_global_entry->list_lock);
1643 
1644 		kref_get(&common->refcount);
1645 		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1646 					     batadv_compare_tt,
1647 					     batadv_choose_tt, common,
1648 					     &common->hash_entry);
1649 
1650 		if (unlikely(hash_added != 0)) {
1651 			/* remove the reference for the hash */
1652 			batadv_tt_global_entry_put(tt_global_entry);
1653 			goto out_remove;
1654 		}
1655 	} else {
1656 		common = &tt_global_entry->common;
1657 		/* If there is already a global entry, we can use this one for
1658 		 * our processing.
1659 		 * But if we are trying to add a temporary client then here are
1660 		 * two options at this point:
1661 		 * 1) the global client is not a temporary client: the global
1662 		 *    client has to be left as it is, temporary information
1663 		 *    should never override any already known client state
1664 		 * 2) the global client is a temporary client: purge the
1665 		 *    originator list and add the new one orig_entry
1666 		 */
1667 		if (flags & BATADV_TT_CLIENT_TEMP) {
1668 			if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1669 				goto out;
1670 			if (batadv_tt_global_entry_has_orig(tt_global_entry,
1671 							    orig_node, NULL))
1672 				goto out_remove;
1673 			batadv_tt_global_del_orig_list(tt_global_entry);
1674 			goto add_orig_entry;
1675 		}
1676 
1677 		/* if the client was temporary added before receiving the first
1678 		 * OGM announcing it, we have to clear the TEMP flag. Also,
1679 		 * remove the previous temporary orig node and re-add it
1680 		 * if required. If the orig entry changed, the new one which
1681 		 * is a non-temporary entry is preferred.
1682 		 */
1683 		if (common->flags & BATADV_TT_CLIENT_TEMP) {
1684 			batadv_tt_global_del_orig_list(tt_global_entry);
1685 			common->flags &= ~BATADV_TT_CLIENT_TEMP;
1686 		}
1687 
1688 		/* the change can carry possible "attribute" flags like the
1689 		 * TT_CLIENT_TEMP, therefore they have to be copied in the
1690 		 * client entry
1691 		 */
1692 		if (!is_multicast_ether_addr(common->addr))
1693 			common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1694 
1695 		/* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1696 		 * one originator left in the list and we previously received a
1697 		 * delete + roaming change for this originator.
1698 		 *
1699 		 * We should first delete the old originator before adding the
1700 		 * new one.
1701 		 */
1702 		if (common->flags & BATADV_TT_CLIENT_ROAM) {
1703 			batadv_tt_global_del_orig_list(tt_global_entry);
1704 			common->flags &= ~BATADV_TT_CLIENT_ROAM;
1705 			tt_global_entry->roam_at = 0;
1706 		}
1707 	}
1708 add_orig_entry:
1709 	/* add the new orig_entry (if needed) or update it */
1710 	batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1711 					flags & BATADV_TT_SYNC_MASK);
1712 
1713 	batadv_dbg(BATADV_DBG_TT, bat_priv,
1714 		   "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1715 		   common->addr, batadv_print_vid(common->vid),
1716 		   orig_node->orig);
1717 	ret = true;
1718 
1719 out_remove:
1720 	/* Do not remove multicast addresses from the local hash on
1721 	 * global additions
1722 	 */
1723 	if (is_multicast_ether_addr(tt_addr))
1724 		goto out;
1725 
1726 	/* remove address from local hash if present */
1727 	local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1728 					     "global tt received",
1729 					     flags & BATADV_TT_CLIENT_ROAM);
1730 	tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1731 
1732 	if (!(flags & BATADV_TT_CLIENT_ROAM))
1733 		/* this is a normal global add. Therefore the client is not in a
1734 		 * roaming state anymore.
1735 		 */
1736 		tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1737 
1738 out:
1739 	batadv_tt_global_entry_put(tt_global_entry);
1740 	batadv_tt_local_entry_put(tt_local_entry);
1741 	return ret;
1742 }
1743 
1744 /**
1745  * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1746  * @bat_priv: the bat priv with all the soft interface information
1747  * @tt_global_entry: global translation table entry to be analyzed
1748  *
1749  * This function assumes the caller holds rcu_read_lock().
1750  * Return: best originator list entry or NULL on errors.
1751  */
1752 static struct batadv_tt_orig_list_entry *
batadv_transtable_best_orig(struct batadv_priv * bat_priv,struct batadv_tt_global_entry * tt_global_entry)1753 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1754 			    struct batadv_tt_global_entry *tt_global_entry)
1755 {
1756 	struct batadv_neigh_node *router, *best_router = NULL;
1757 	struct batadv_algo_ops *bao = bat_priv->algo_ops;
1758 	struct hlist_head *head;
1759 	struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1760 
1761 	head = &tt_global_entry->orig_list;
1762 	hlist_for_each_entry_rcu(orig_entry, head, list) {
1763 		router = batadv_orig_router_get(orig_entry->orig_node,
1764 						BATADV_IF_DEFAULT);
1765 		if (!router)
1766 			continue;
1767 
1768 		if (best_router &&
1769 		    bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1770 				   BATADV_IF_DEFAULT) <= 0) {
1771 			batadv_neigh_node_put(router);
1772 			continue;
1773 		}
1774 
1775 		/* release the refcount for the "old" best */
1776 		batadv_neigh_node_put(best_router);
1777 
1778 		best_entry = orig_entry;
1779 		best_router = router;
1780 	}
1781 
1782 	batadv_neigh_node_put(best_router);
1783 
1784 	return best_entry;
1785 }
1786 
1787 /**
1788  * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
1789  * @msg: Netlink message to dump into
1790  * @portid: Port making netlink request
1791  * @seq: Sequence number of netlink message
1792  * @common: tt local & tt global common data
1793  * @orig: Originator node announcing a non-mesh client
1794  * @best: Is the best originator for the TT entry
1795  *
1796  * Return: Error code, or 0 on success
1797  */
1798 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)1799 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1800 			       struct batadv_tt_common_entry *common,
1801 			       struct batadv_tt_orig_list_entry *orig,
1802 			       bool best)
1803 {
1804 	u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
1805 	void *hdr;
1806 	struct batadv_orig_node_vlan *vlan;
1807 	u8 last_ttvn;
1808 	u32 crc;
1809 
1810 	vlan = batadv_orig_node_vlan_get(orig->orig_node,
1811 					 common->vid);
1812 	if (!vlan)
1813 		return 0;
1814 
1815 	crc = vlan->tt.crc;
1816 
1817 	batadv_orig_node_vlan_put(vlan);
1818 
1819 	hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1820 			  NLM_F_MULTI,
1821 			  BATADV_CMD_GET_TRANSTABLE_GLOBAL);
1822 	if (!hdr)
1823 		return -ENOBUFS;
1824 
1825 	last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
1826 
1827 	if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1828 	    nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
1829 		    orig->orig_node->orig) ||
1830 	    nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
1831 	    nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
1832 	    nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1833 	    nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1834 	    nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
1835 		goto nla_put_failure;
1836 
1837 	if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
1838 		goto nla_put_failure;
1839 
1840 	genlmsg_end(msg, hdr);
1841 	return 0;
1842 
1843  nla_put_failure:
1844 	genlmsg_cancel(msg, hdr);
1845 	return -EMSGSIZE;
1846 }
1847 
1848 /**
1849  * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
1850  * @msg: Netlink message to dump into
1851  * @portid: Port making netlink request
1852  * @seq: Sequence number of netlink message
1853  * @bat_priv: The bat priv with all the soft interface information
1854  * @common: tt local & tt global common data
1855  * @sub_s: Number of entries to skip
1856  *
1857  * This function assumes the caller holds rcu_read_lock().
1858  *
1859  * Return: Error code, or 0 on success
1860  */
1861 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)1862 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1863 			    struct batadv_priv *bat_priv,
1864 			    struct batadv_tt_common_entry *common, int *sub_s)
1865 {
1866 	struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1867 	struct batadv_tt_global_entry *global;
1868 	struct hlist_head *head;
1869 	int sub = 0;
1870 	bool best;
1871 
1872 	global = container_of(common, struct batadv_tt_global_entry, common);
1873 	best_entry = batadv_transtable_best_orig(bat_priv, global);
1874 	head = &global->orig_list;
1875 
1876 	hlist_for_each_entry_rcu(orig_entry, head, list) {
1877 		if (sub++ < *sub_s)
1878 			continue;
1879 
1880 		best = (orig_entry == best_entry);
1881 
1882 		if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
1883 						   orig_entry, best)) {
1884 			*sub_s = sub - 1;
1885 			return -EMSGSIZE;
1886 		}
1887 	}
1888 
1889 	*sub_s = 0;
1890 	return 0;
1891 }
1892 
1893 /**
1894  * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
1895  * @msg: Netlink message to dump into
1896  * @portid: Port making netlink request
1897  * @seq: Sequence number of netlink message
1898  * @bat_priv: The bat priv with all the soft interface information
1899  * @head: Pointer to the list containing the global tt entries
1900  * @idx_s: Number of entries to skip
1901  * @sub: Number of entries to skip
1902  *
1903  * Return: Error code, or 0 on success
1904  */
1905 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)1906 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1907 			     struct batadv_priv *bat_priv,
1908 			     struct hlist_head *head, int *idx_s, int *sub)
1909 {
1910 	struct batadv_tt_common_entry *common;
1911 	int idx = 0;
1912 
1913 	rcu_read_lock();
1914 	hlist_for_each_entry_rcu(common, head, hash_entry) {
1915 		if (idx++ < *idx_s)
1916 			continue;
1917 
1918 		if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
1919 						common, sub)) {
1920 			rcu_read_unlock();
1921 			*idx_s = idx - 1;
1922 			return -EMSGSIZE;
1923 		}
1924 	}
1925 	rcu_read_unlock();
1926 
1927 	*idx_s = 0;
1928 	*sub = 0;
1929 	return 0;
1930 }
1931 
1932 /**
1933  * batadv_tt_global_dump() -  Dump TT global entries into a message
1934  * @msg: Netlink message to dump into
1935  * @cb: Parameters from query
1936  *
1937  * Return: Error code, or length of message on success
1938  */
batadv_tt_global_dump(struct sk_buff * msg,struct netlink_callback * cb)1939 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
1940 {
1941 	struct net *net = sock_net(cb->skb->sk);
1942 	struct net_device *soft_iface;
1943 	struct batadv_priv *bat_priv;
1944 	struct batadv_hard_iface *primary_if = NULL;
1945 	struct batadv_hashtable *hash;
1946 	struct hlist_head *head;
1947 	int ret;
1948 	int ifindex;
1949 	int bucket = cb->args[0];
1950 	int idx = cb->args[1];
1951 	int sub = cb->args[2];
1952 	int portid = NETLINK_CB(cb->skb).portid;
1953 
1954 	ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1955 	if (!ifindex)
1956 		return -EINVAL;
1957 
1958 	soft_iface = dev_get_by_index(net, ifindex);
1959 	if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1960 		ret = -ENODEV;
1961 		goto out;
1962 	}
1963 
1964 	bat_priv = netdev_priv(soft_iface);
1965 
1966 	primary_if = batadv_primary_if_get_selected(bat_priv);
1967 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1968 		ret = -ENOENT;
1969 		goto out;
1970 	}
1971 
1972 	hash = bat_priv->tt.global_hash;
1973 
1974 	while (bucket < hash->size) {
1975 		head = &hash->table[bucket];
1976 
1977 		if (batadv_tt_global_dump_bucket(msg, portid,
1978 						 cb->nlh->nlmsg_seq, bat_priv,
1979 						 head, &idx, &sub))
1980 			break;
1981 
1982 		bucket++;
1983 	}
1984 
1985 	ret = msg->len;
1986 
1987  out:
1988 	batadv_hardif_put(primary_if);
1989 	dev_put(soft_iface);
1990 
1991 	cb->args[0] = bucket;
1992 	cb->args[1] = idx;
1993 	cb->args[2] = sub;
1994 
1995 	return ret;
1996 }
1997 
1998 /**
1999  * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
2000  * @tt_global_entry: the global entry to remove the orig_entry from
2001  * @orig_entry: the orig entry to remove and free
2002  *
2003  * Remove an orig_entry from its list in the given tt_global_entry and
2004  * free this orig_entry afterwards.
2005  *
2006  * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2007  * part of a list.
2008  */
2009 static void
_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry * tt_global_entry,struct batadv_tt_orig_list_entry * orig_entry)2010 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2011 				 struct batadv_tt_orig_list_entry *orig_entry)
2012 {
2013 	lockdep_assert_held(&tt_global_entry->list_lock);
2014 
2015 	batadv_tt_global_size_dec(orig_entry->orig_node,
2016 				  tt_global_entry->common.vid);
2017 	atomic_dec(&tt_global_entry->orig_list_count);
2018 	/* requires holding tt_global_entry->list_lock and orig_entry->list
2019 	 * being part of a list
2020 	 */
2021 	hlist_del_rcu(&orig_entry->list);
2022 	batadv_tt_orig_list_entry_put(orig_entry);
2023 }
2024 
2025 /* deletes the orig list of a tt_global_entry */
2026 static void
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry * tt_global_entry)2027 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2028 {
2029 	struct hlist_head *head;
2030 	struct hlist_node *safe;
2031 	struct batadv_tt_orig_list_entry *orig_entry;
2032 
2033 	spin_lock_bh(&tt_global_entry->list_lock);
2034 	head = &tt_global_entry->orig_list;
2035 	hlist_for_each_entry_safe(orig_entry, safe, head, list)
2036 		_batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2037 	spin_unlock_bh(&tt_global_entry->list_lock);
2038 }
2039 
2040 /**
2041  * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2042  * @bat_priv: the bat priv with all the soft interface information
2043  * @tt_global_entry: the global entry to remove the orig_node from
2044  * @orig_node: the originator announcing the client
2045  * @message: message to append to the log on deletion
2046  *
2047  * Remove the given orig_node and its according orig_entry from the given
2048  * global tt entry.
2049  */
2050 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)2051 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2052 			       struct batadv_tt_global_entry *tt_global_entry,
2053 			       struct batadv_orig_node *orig_node,
2054 			       const char *message)
2055 {
2056 	struct hlist_head *head;
2057 	struct hlist_node *safe;
2058 	struct batadv_tt_orig_list_entry *orig_entry;
2059 	unsigned short vid;
2060 
2061 	spin_lock_bh(&tt_global_entry->list_lock);
2062 	head = &tt_global_entry->orig_list;
2063 	hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2064 		if (orig_entry->orig_node == orig_node) {
2065 			vid = tt_global_entry->common.vid;
2066 			batadv_dbg(BATADV_DBG_TT, bat_priv,
2067 				   "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2068 				   orig_node->orig,
2069 				   tt_global_entry->common.addr,
2070 				   batadv_print_vid(vid), message);
2071 			_batadv_tt_global_del_orig_entry(tt_global_entry,
2072 							 orig_entry);
2073 		}
2074 	}
2075 	spin_unlock_bh(&tt_global_entry->list_lock);
2076 }
2077 
2078 /* If the client is to be deleted, we check if it is the last origantor entry
2079  * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2080  * timer, otherwise we simply remove the originator scheduled for deletion.
2081  */
2082 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)2083 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2084 			     struct batadv_tt_global_entry *tt_global_entry,
2085 			     struct batadv_orig_node *orig_node,
2086 			     const char *message)
2087 {
2088 	bool last_entry = true;
2089 	struct hlist_head *head;
2090 	struct batadv_tt_orig_list_entry *orig_entry;
2091 
2092 	/* no local entry exists, case 1:
2093 	 * Check if this is the last one or if other entries exist.
2094 	 */
2095 
2096 	rcu_read_lock();
2097 	head = &tt_global_entry->orig_list;
2098 	hlist_for_each_entry_rcu(orig_entry, head, list) {
2099 		if (orig_entry->orig_node != orig_node) {
2100 			last_entry = false;
2101 			break;
2102 		}
2103 	}
2104 	rcu_read_unlock();
2105 
2106 	if (last_entry) {
2107 		/* its the last one, mark for roaming. */
2108 		tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2109 		tt_global_entry->roam_at = jiffies;
2110 	} else {
2111 		/* there is another entry, we can simply delete this
2112 		 * one and can still use the other one.
2113 		 */
2114 		batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2115 					       orig_node, message);
2116 	}
2117 }
2118 
2119 /**
2120  * batadv_tt_global_del() - remove a client from the global table
2121  * @bat_priv: the bat priv with all the soft interface information
2122  * @orig_node: an originator serving this client
2123  * @addr: the mac address of the client
2124  * @vid: VLAN identifier
2125  * @message: a message explaining the reason for deleting the client to print
2126  *  for debugging purpose
2127  * @roaming: true if the deletion has been triggered by a roaming event
2128  */
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)2129 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2130 				 struct batadv_orig_node *orig_node,
2131 				 const unsigned char *addr, unsigned short vid,
2132 				 const char *message, bool roaming)
2133 {
2134 	struct batadv_tt_global_entry *tt_global_entry;
2135 	struct batadv_tt_local_entry *local_entry = NULL;
2136 
2137 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2138 	if (!tt_global_entry)
2139 		goto out;
2140 
2141 	if (!roaming) {
2142 		batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2143 					       orig_node, message);
2144 
2145 		if (hlist_empty(&tt_global_entry->orig_list))
2146 			batadv_tt_global_free(bat_priv, tt_global_entry,
2147 					      message);
2148 
2149 		goto out;
2150 	}
2151 
2152 	/* if we are deleting a global entry due to a roam
2153 	 * event, there are two possibilities:
2154 	 * 1) the client roamed from node A to node B => if there
2155 	 *    is only one originator left for this client, we mark
2156 	 *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2157 	 *    wait for node B to claim it. In case of timeout
2158 	 *    the entry is purged.
2159 	 *
2160 	 *    If there are other originators left, we directly delete
2161 	 *    the originator.
2162 	 * 2) the client roamed to us => we can directly delete
2163 	 *    the global entry, since it is useless now.
2164 	 */
2165 	local_entry = batadv_tt_local_hash_find(bat_priv,
2166 						tt_global_entry->common.addr,
2167 						vid);
2168 	if (local_entry) {
2169 		/* local entry exists, case 2: client roamed to us. */
2170 		batadv_tt_global_del_orig_list(tt_global_entry);
2171 		batadv_tt_global_free(bat_priv, tt_global_entry, message);
2172 	} else {
2173 		/* no local entry exists, case 1: check for roaming */
2174 		batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2175 					     orig_node, message);
2176 	}
2177 
2178 out:
2179 	batadv_tt_global_entry_put(tt_global_entry);
2180 	batadv_tt_local_entry_put(local_entry);
2181 }
2182 
2183 /**
2184  * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2185  *  the given originator matching the provided vid
2186  * @bat_priv: the bat priv with all the soft interface information
2187  * @orig_node: the originator owning the entries to remove
2188  * @match_vid: the VLAN identifier to match. If negative all the entries will be
2189  *  removed
2190  * @message: debug message to print as "reason"
2191  */
batadv_tt_global_del_orig(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,s32 match_vid,const char * message)2192 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2193 			       struct batadv_orig_node *orig_node,
2194 			       s32 match_vid,
2195 			       const char *message)
2196 {
2197 	struct batadv_tt_global_entry *tt_global;
2198 	struct batadv_tt_common_entry *tt_common_entry;
2199 	u32 i;
2200 	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2201 	struct hlist_node *safe;
2202 	struct hlist_head *head;
2203 	spinlock_t *list_lock; /* protects write access to the hash lists */
2204 	unsigned short vid;
2205 
2206 	if (!hash)
2207 		return;
2208 
2209 	for (i = 0; i < hash->size; i++) {
2210 		head = &hash->table[i];
2211 		list_lock = &hash->list_locks[i];
2212 
2213 		spin_lock_bh(list_lock);
2214 		hlist_for_each_entry_safe(tt_common_entry, safe,
2215 					  head, hash_entry) {
2216 			/* remove only matching entries */
2217 			if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2218 				continue;
2219 
2220 			tt_global = container_of(tt_common_entry,
2221 						 struct batadv_tt_global_entry,
2222 						 common);
2223 
2224 			batadv_tt_global_del_orig_node(bat_priv, tt_global,
2225 						       orig_node, message);
2226 
2227 			if (hlist_empty(&tt_global->orig_list)) {
2228 				vid = tt_global->common.vid;
2229 				batadv_dbg(BATADV_DBG_TT, bat_priv,
2230 					   "Deleting global tt entry %pM (vid: %d): %s\n",
2231 					   tt_global->common.addr,
2232 					   batadv_print_vid(vid), message);
2233 				hlist_del_rcu(&tt_common_entry->hash_entry);
2234 				batadv_tt_global_entry_put(tt_global);
2235 			}
2236 		}
2237 		spin_unlock_bh(list_lock);
2238 	}
2239 	clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2240 }
2241 
batadv_tt_global_to_purge(struct batadv_tt_global_entry * tt_global,char ** msg)2242 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2243 				      char **msg)
2244 {
2245 	bool purge = false;
2246 	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2247 	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2248 
2249 	if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2250 	    batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2251 		purge = true;
2252 		*msg = "Roaming timeout\n";
2253 	}
2254 
2255 	if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2256 	    batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2257 		purge = true;
2258 		*msg = "Temporary client timeout\n";
2259 	}
2260 
2261 	return purge;
2262 }
2263 
batadv_tt_global_purge(struct batadv_priv * bat_priv)2264 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2265 {
2266 	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2267 	struct hlist_head *head;
2268 	struct hlist_node *node_tmp;
2269 	spinlock_t *list_lock; /* protects write access to the hash lists */
2270 	u32 i;
2271 	char *msg = NULL;
2272 	struct batadv_tt_common_entry *tt_common;
2273 	struct batadv_tt_global_entry *tt_global;
2274 
2275 	for (i = 0; i < hash->size; i++) {
2276 		head = &hash->table[i];
2277 		list_lock = &hash->list_locks[i];
2278 
2279 		spin_lock_bh(list_lock);
2280 		hlist_for_each_entry_safe(tt_common, node_tmp, head,
2281 					  hash_entry) {
2282 			tt_global = container_of(tt_common,
2283 						 struct batadv_tt_global_entry,
2284 						 common);
2285 
2286 			if (!batadv_tt_global_to_purge(tt_global, &msg))
2287 				continue;
2288 
2289 			batadv_dbg(BATADV_DBG_TT, bat_priv,
2290 				   "Deleting global tt entry %pM (vid: %d): %s\n",
2291 				   tt_global->common.addr,
2292 				   batadv_print_vid(tt_global->common.vid),
2293 				   msg);
2294 
2295 			hlist_del_rcu(&tt_common->hash_entry);
2296 
2297 			batadv_tt_global_entry_put(tt_global);
2298 		}
2299 		spin_unlock_bh(list_lock);
2300 	}
2301 }
2302 
batadv_tt_global_table_free(struct batadv_priv * bat_priv)2303 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2304 {
2305 	struct batadv_hashtable *hash;
2306 	spinlock_t *list_lock; /* protects write access to the hash lists */
2307 	struct batadv_tt_common_entry *tt_common_entry;
2308 	struct batadv_tt_global_entry *tt_global;
2309 	struct hlist_node *node_tmp;
2310 	struct hlist_head *head;
2311 	u32 i;
2312 
2313 	if (!bat_priv->tt.global_hash)
2314 		return;
2315 
2316 	hash = bat_priv->tt.global_hash;
2317 
2318 	for (i = 0; i < hash->size; i++) {
2319 		head = &hash->table[i];
2320 		list_lock = &hash->list_locks[i];
2321 
2322 		spin_lock_bh(list_lock);
2323 		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2324 					  head, hash_entry) {
2325 			hlist_del_rcu(&tt_common_entry->hash_entry);
2326 			tt_global = container_of(tt_common_entry,
2327 						 struct batadv_tt_global_entry,
2328 						 common);
2329 			batadv_tt_global_entry_put(tt_global);
2330 		}
2331 		spin_unlock_bh(list_lock);
2332 	}
2333 
2334 	batadv_hash_destroy(hash);
2335 
2336 	bat_priv->tt.global_hash = NULL;
2337 }
2338 
2339 static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry * tt_local_entry,struct batadv_tt_global_entry * tt_global_entry)2340 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2341 		       struct batadv_tt_global_entry *tt_global_entry)
2342 {
2343 	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2344 	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2345 		return true;
2346 
2347 	/* check if the two clients are marked as isolated */
2348 	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2349 	    tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2350 		return true;
2351 
2352 	return false;
2353 }
2354 
2355 /**
2356  * batadv_transtable_search() - get the mesh destination for a given client
2357  * @bat_priv: the bat priv with all the soft interface information
2358  * @src: mac address of the source client
2359  * @addr: mac address of the destination client
2360  * @vid: VLAN identifier
2361  *
2362  * Return: a pointer to the originator that was selected as destination in the
2363  * mesh for contacting the client 'addr', NULL otherwise.
2364  * In case of multiple originators serving the same client, the function returns
2365  * the best one (best in terms of metric towards the destination node).
2366  *
2367  * If the two clients are AP isolated the function returns NULL.
2368  */
batadv_transtable_search(struct batadv_priv * bat_priv,const u8 * src,const u8 * addr,unsigned short vid)2369 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2370 						  const u8 *src,
2371 						  const u8 *addr,
2372 						  unsigned short vid)
2373 {
2374 	struct batadv_tt_local_entry *tt_local_entry = NULL;
2375 	struct batadv_tt_global_entry *tt_global_entry = NULL;
2376 	struct batadv_orig_node *orig_node = NULL;
2377 	struct batadv_tt_orig_list_entry *best_entry;
2378 
2379 	if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2380 		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2381 		if (!tt_local_entry ||
2382 		    (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2383 			goto out;
2384 	}
2385 
2386 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2387 	if (!tt_global_entry)
2388 		goto out;
2389 
2390 	/* check whether the clients should not communicate due to AP
2391 	 * isolation
2392 	 */
2393 	if (tt_local_entry &&
2394 	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2395 		goto out;
2396 
2397 	rcu_read_lock();
2398 	best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2399 	/* found anything? */
2400 	if (best_entry)
2401 		orig_node = best_entry->orig_node;
2402 	if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2403 		orig_node = NULL;
2404 	rcu_read_unlock();
2405 
2406 out:
2407 	batadv_tt_global_entry_put(tt_global_entry);
2408 	batadv_tt_local_entry_put(tt_local_entry);
2409 
2410 	return orig_node;
2411 }
2412 
2413 /**
2414  * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2415  *  to the given orig_node
2416  * @bat_priv: the bat priv with all the soft interface information
2417  * @orig_node: originator for which the CRC should be computed
2418  * @vid: VLAN identifier for which the CRC32 has to be computed
2419  *
2420  * This function computes the checksum for the global table corresponding to a
2421  * specific originator. In particular, the checksum is computed as follows: For
2422  * each client connected to the originator the CRC32C of the MAC address and the
2423  * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2424  * together.
2425  *
2426  * The idea behind is that CRC32C should be used as much as possible in order to
2427  * produce a unique hash of the table, but since the order which is used to feed
2428  * the CRC32C function affects the result and since every node in the network
2429  * probably sorts the clients differently, the hash function cannot be directly
2430  * computed over the entire table. Hence the CRC32C is used only on
2431  * the single client entry, while all the results are then xor'ed together
2432  * because the XOR operation can combine them all while trying to reduce the
2433  * noise as much as possible.
2434  *
2435  * Return: the checksum of the global table of a given originator.
2436  */
batadv_tt_global_crc(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,unsigned short vid)2437 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2438 				struct batadv_orig_node *orig_node,
2439 				unsigned short vid)
2440 {
2441 	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2442 	struct batadv_tt_orig_list_entry *tt_orig;
2443 	struct batadv_tt_common_entry *tt_common;
2444 	struct batadv_tt_global_entry *tt_global;
2445 	struct hlist_head *head;
2446 	u32 i, crc_tmp, crc = 0;
2447 	u8 flags;
2448 	__be16 tmp_vid;
2449 
2450 	for (i = 0; i < hash->size; i++) {
2451 		head = &hash->table[i];
2452 
2453 		rcu_read_lock();
2454 		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2455 			tt_global = container_of(tt_common,
2456 						 struct batadv_tt_global_entry,
2457 						 common);
2458 			/* compute the CRC only for entries belonging to the
2459 			 * VLAN identified by the vid passed as parameter
2460 			 */
2461 			if (tt_common->vid != vid)
2462 				continue;
2463 
2464 			/* Roaming clients are in the global table for
2465 			 * consistency only. They don't have to be
2466 			 * taken into account while computing the
2467 			 * global crc
2468 			 */
2469 			if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2470 				continue;
2471 			/* Temporary clients have not been announced yet, so
2472 			 * they have to be skipped while computing the global
2473 			 * crc
2474 			 */
2475 			if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2476 				continue;
2477 
2478 			/* find out if this global entry is announced by this
2479 			 * originator
2480 			 */
2481 			tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2482 								   orig_node);
2483 			if (!tt_orig)
2484 				continue;
2485 
2486 			/* use network order to read the VID: this ensures that
2487 			 * every node reads the bytes in the same order.
2488 			 */
2489 			tmp_vid = htons(tt_common->vid);
2490 			crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2491 
2492 			/* compute the CRC on flags that have to be kept in sync
2493 			 * among nodes
2494 			 */
2495 			flags = tt_orig->flags;
2496 			crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2497 
2498 			crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2499 
2500 			batadv_tt_orig_list_entry_put(tt_orig);
2501 		}
2502 		rcu_read_unlock();
2503 	}
2504 
2505 	return crc;
2506 }
2507 
2508 /**
2509  * batadv_tt_local_crc() - calculates the checksum of the local table
2510  * @bat_priv: the bat priv with all the soft interface information
2511  * @vid: VLAN identifier for which the CRC32 has to be computed
2512  *
2513  * For details about the computation, please refer to the documentation for
2514  * batadv_tt_global_crc().
2515  *
2516  * Return: the checksum of the local table
2517  */
batadv_tt_local_crc(struct batadv_priv * bat_priv,unsigned short vid)2518 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2519 			       unsigned short vid)
2520 {
2521 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2522 	struct batadv_tt_common_entry *tt_common;
2523 	struct hlist_head *head;
2524 	u32 i, crc_tmp, crc = 0;
2525 	u8 flags;
2526 	__be16 tmp_vid;
2527 
2528 	for (i = 0; i < hash->size; i++) {
2529 		head = &hash->table[i];
2530 
2531 		rcu_read_lock();
2532 		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2533 			/* compute the CRC only for entries belonging to the
2534 			 * VLAN identified by vid
2535 			 */
2536 			if (tt_common->vid != vid)
2537 				continue;
2538 
2539 			/* not yet committed clients have not to be taken into
2540 			 * account while computing the CRC
2541 			 */
2542 			if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2543 				continue;
2544 
2545 			/* use network order to read the VID: this ensures that
2546 			 * every node reads the bytes in the same order.
2547 			 */
2548 			tmp_vid = htons(tt_common->vid);
2549 			crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2550 
2551 			/* compute the CRC on flags that have to be kept in sync
2552 			 * among nodes
2553 			 */
2554 			flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2555 			crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2556 
2557 			crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2558 		}
2559 		rcu_read_unlock();
2560 	}
2561 
2562 	return crc;
2563 }
2564 
2565 /**
2566  * batadv_tt_req_node_release() - free tt_req node entry
2567  * @ref: kref pointer of the tt req_node entry
2568  */
batadv_tt_req_node_release(struct kref * ref)2569 static void batadv_tt_req_node_release(struct kref *ref)
2570 {
2571 	struct batadv_tt_req_node *tt_req_node;
2572 
2573 	tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2574 
2575 	kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2576 }
2577 
2578 /**
2579  * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2580  *  possibly release it
2581  * @tt_req_node: tt_req_node to be free'd
2582  */
batadv_tt_req_node_put(struct batadv_tt_req_node * tt_req_node)2583 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2584 {
2585 	if (!tt_req_node)
2586 		return;
2587 
2588 	kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2589 }
2590 
batadv_tt_req_list_free(struct batadv_priv * bat_priv)2591 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2592 {
2593 	struct batadv_tt_req_node *node;
2594 	struct hlist_node *safe;
2595 
2596 	spin_lock_bh(&bat_priv->tt.req_list_lock);
2597 
2598 	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2599 		hlist_del_init(&node->list);
2600 		batadv_tt_req_node_put(node);
2601 	}
2602 
2603 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2604 }
2605 
batadv_tt_save_orig_buffer(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,const void * tt_buff,u16 tt_buff_len)2606 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2607 				       struct batadv_orig_node *orig_node,
2608 				       const void *tt_buff,
2609 				       u16 tt_buff_len)
2610 {
2611 	/* Replace the old buffer only if I received something in the
2612 	 * last OGM (the OGM could carry no changes)
2613 	 */
2614 	spin_lock_bh(&orig_node->tt_buff_lock);
2615 	if (tt_buff_len > 0) {
2616 		kfree(orig_node->tt_buff);
2617 		orig_node->tt_buff_len = 0;
2618 		orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2619 		if (orig_node->tt_buff) {
2620 			memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2621 			orig_node->tt_buff_len = tt_buff_len;
2622 		}
2623 	}
2624 	spin_unlock_bh(&orig_node->tt_buff_lock);
2625 }
2626 
batadv_tt_req_purge(struct batadv_priv * bat_priv)2627 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2628 {
2629 	struct batadv_tt_req_node *node;
2630 	struct hlist_node *safe;
2631 
2632 	spin_lock_bh(&bat_priv->tt.req_list_lock);
2633 	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2634 		if (batadv_has_timed_out(node->issued_at,
2635 					 BATADV_TT_REQUEST_TIMEOUT)) {
2636 			hlist_del_init(&node->list);
2637 			batadv_tt_req_node_put(node);
2638 		}
2639 	}
2640 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2641 }
2642 
2643 /**
2644  * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2645  * @bat_priv: the bat priv with all the soft interface information
2646  * @orig_node: orig node this request is being issued for
2647  *
2648  * Return: the pointer to the new tt_req_node struct if no request
2649  * has already been issued for this orig_node, NULL otherwise.
2650  */
2651 static struct batadv_tt_req_node *
batadv_tt_req_node_new(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)2652 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2653 		       struct batadv_orig_node *orig_node)
2654 {
2655 	struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2656 
2657 	spin_lock_bh(&bat_priv->tt.req_list_lock);
2658 	hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2659 		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2660 		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2661 					  BATADV_TT_REQUEST_TIMEOUT))
2662 			goto unlock;
2663 	}
2664 
2665 	tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2666 	if (!tt_req_node)
2667 		goto unlock;
2668 
2669 	kref_init(&tt_req_node->refcount);
2670 	ether_addr_copy(tt_req_node->addr, orig_node->orig);
2671 	tt_req_node->issued_at = jiffies;
2672 
2673 	kref_get(&tt_req_node->refcount);
2674 	hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2675 unlock:
2676 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2677 	return tt_req_node;
2678 }
2679 
2680 /**
2681  * batadv_tt_local_valid() - verify local tt entry and get flags
2682  * @entry_ptr: to be checked local tt entry
2683  * @data_ptr: not used but definition required to satisfy the callback prototype
2684  * @flags: a pointer to store TT flags for this client to
2685  *
2686  * Checks the validity of the given local TT entry. If it is, then the provided
2687  * flags pointer is updated.
2688  *
2689  * Return: true if the entry is a valid, false otherwise.
2690  */
batadv_tt_local_valid(const void * entry_ptr,const void * data_ptr,u8 * flags)2691 static bool batadv_tt_local_valid(const void *entry_ptr,
2692 				  const void *data_ptr,
2693 				  u8 *flags)
2694 {
2695 	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2696 
2697 	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2698 		return false;
2699 
2700 	if (flags)
2701 		*flags = tt_common_entry->flags;
2702 
2703 	return true;
2704 }
2705 
2706 /**
2707  * batadv_tt_global_valid() - verify global tt entry and get flags
2708  * @entry_ptr: to be checked global tt entry
2709  * @data_ptr: an orig_node object (may be NULL)
2710  * @flags: a pointer to store TT flags for this client to
2711  *
2712  * Checks the validity of the given global TT entry. If it is, then the provided
2713  * flags pointer is updated either with the common (summed) TT flags if data_ptr
2714  * is NULL or the specific, per originator TT flags otherwise.
2715  *
2716  * Return: true if the entry is a valid, false otherwise.
2717  */
batadv_tt_global_valid(const void * entry_ptr,const void * data_ptr,u8 * flags)2718 static bool batadv_tt_global_valid(const void *entry_ptr,
2719 				   const void *data_ptr,
2720 				   u8 *flags)
2721 {
2722 	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2723 	const struct batadv_tt_global_entry *tt_global_entry;
2724 	const struct batadv_orig_node *orig_node = data_ptr;
2725 
2726 	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2727 	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2728 		return false;
2729 
2730 	tt_global_entry = container_of(tt_common_entry,
2731 				       struct batadv_tt_global_entry,
2732 				       common);
2733 
2734 	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2735 					       flags);
2736 }
2737 
2738 /**
2739  * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2740  *  specified tt hash
2741  * @bat_priv: the bat priv with all the soft interface information
2742  * @hash: hash table containing the tt entries
2743  * @tt_len: expected tvlv tt data buffer length in number of bytes
2744  * @tvlv_buff: pointer to the buffer to fill with the TT data
2745  * @valid_cb: function to filter tt change entries and to return TT flags
2746  * @cb_data: data passed to the filter function as argument
2747  *
2748  * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2749  * is not provided then this becomes a no-op.
2750  */
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)2751 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2752 				    struct batadv_hashtable *hash,
2753 				    void *tvlv_buff, u16 tt_len,
2754 				    bool (*valid_cb)(const void *,
2755 						     const void *,
2756 						     u8 *flags),
2757 				    void *cb_data)
2758 {
2759 	struct batadv_tt_common_entry *tt_common_entry;
2760 	struct batadv_tvlv_tt_change *tt_change;
2761 	struct hlist_head *head;
2762 	u16 tt_tot, tt_num_entries = 0;
2763 	u8 flags;
2764 	bool ret;
2765 	u32 i;
2766 
2767 	tt_tot = batadv_tt_entries(tt_len);
2768 	tt_change = tvlv_buff;
2769 
2770 	if (!valid_cb)
2771 		return;
2772 
2773 	rcu_read_lock();
2774 	for (i = 0; i < hash->size; i++) {
2775 		head = &hash->table[i];
2776 
2777 		hlist_for_each_entry_rcu(tt_common_entry,
2778 					 head, hash_entry) {
2779 			if (tt_tot == tt_num_entries)
2780 				break;
2781 
2782 			ret = valid_cb(tt_common_entry, cb_data, &flags);
2783 			if (!ret)
2784 				continue;
2785 
2786 			ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2787 			tt_change->flags = flags;
2788 			tt_change->vid = htons(tt_common_entry->vid);
2789 			memset(tt_change->reserved, 0,
2790 			       sizeof(tt_change->reserved));
2791 
2792 			tt_num_entries++;
2793 			tt_change++;
2794 		}
2795 	}
2796 	rcu_read_unlock();
2797 }
2798 
2799 /**
2800  * batadv_tt_global_check_crc() - check if all the CRCs are correct
2801  * @orig_node: originator for which the CRCs have to be checked
2802  * @tt_vlan: pointer to the first tvlv VLAN entry
2803  * @num_vlan: number of tvlv VLAN entries
2804  *
2805  * Return: true if all the received CRCs match the locally stored ones, false
2806  * otherwise
2807  */
batadv_tt_global_check_crc(struct batadv_orig_node * orig_node,struct batadv_tvlv_tt_vlan_data * tt_vlan,u16 num_vlan)2808 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2809 				       struct batadv_tvlv_tt_vlan_data *tt_vlan,
2810 				       u16 num_vlan)
2811 {
2812 	struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2813 	struct batadv_orig_node_vlan *vlan;
2814 	int i, orig_num_vlan;
2815 	u32 crc;
2816 
2817 	/* check if each received CRC matches the locally stored one */
2818 	for (i = 0; i < num_vlan; i++) {
2819 		tt_vlan_tmp = tt_vlan + i;
2820 
2821 		/* if orig_node is a backbone node for this VLAN, don't check
2822 		 * the CRC as we ignore all the global entries over it
2823 		 */
2824 		if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2825 						   orig_node->orig,
2826 						   ntohs(tt_vlan_tmp->vid)))
2827 			continue;
2828 
2829 		vlan = batadv_orig_node_vlan_get(orig_node,
2830 						 ntohs(tt_vlan_tmp->vid));
2831 		if (!vlan)
2832 			return false;
2833 
2834 		crc = vlan->tt.crc;
2835 		batadv_orig_node_vlan_put(vlan);
2836 
2837 		if (crc != ntohl(tt_vlan_tmp->crc))
2838 			return false;
2839 	}
2840 
2841 	/* check if any excess VLANs exist locally for the originator
2842 	 * which are not mentioned in the TVLV from the originator.
2843 	 */
2844 	rcu_read_lock();
2845 	orig_num_vlan = 0;
2846 	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
2847 		orig_num_vlan++;
2848 	rcu_read_unlock();
2849 
2850 	if (orig_num_vlan > num_vlan)
2851 		return false;
2852 
2853 	return true;
2854 }
2855 
2856 /**
2857  * batadv_tt_local_update_crc() - update all the local CRCs
2858  * @bat_priv: the bat priv with all the soft interface information
2859  */
batadv_tt_local_update_crc(struct batadv_priv * bat_priv)2860 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2861 {
2862 	struct batadv_softif_vlan *vlan;
2863 
2864 	/* recompute the global CRC for each VLAN */
2865 	rcu_read_lock();
2866 	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2867 		vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2868 	}
2869 	rcu_read_unlock();
2870 }
2871 
2872 /**
2873  * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
2874  * @bat_priv: the bat priv with all the soft interface information
2875  * @orig_node: the orig_node for which the CRCs have to be updated
2876  */
batadv_tt_global_update_crc(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)2877 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2878 					struct batadv_orig_node *orig_node)
2879 {
2880 	struct batadv_orig_node_vlan *vlan;
2881 	u32 crc;
2882 
2883 	/* recompute the global CRC for each VLAN */
2884 	rcu_read_lock();
2885 	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2886 		/* if orig_node is a backbone node for this VLAN, don't compute
2887 		 * the CRC as we ignore all the global entries over it
2888 		 */
2889 		if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2890 						   vlan->vid))
2891 			continue;
2892 
2893 		crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2894 		vlan->tt.crc = crc;
2895 	}
2896 	rcu_read_unlock();
2897 }
2898 
2899 /**
2900  * batadv_send_tt_request() - send a TT Request message to a given node
2901  * @bat_priv: the bat priv with all the soft interface information
2902  * @dst_orig_node: the destination of the message
2903  * @ttvn: the version number that the source of the message is looking for
2904  * @tt_vlan: pointer to the first tvlv VLAN object to request
2905  * @num_vlan: number of tvlv VLAN entries
2906  * @full_table: ask for the entire translation table if true, while only for the
2907  *  last TT diff otherwise
2908  *
2909  * Return: true if the TT Request was sent, false otherwise
2910  */
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)2911 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
2912 				   struct batadv_orig_node *dst_orig_node,
2913 				   u8 ttvn,
2914 				   struct batadv_tvlv_tt_vlan_data *tt_vlan,
2915 				   u16 num_vlan, bool full_table)
2916 {
2917 	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2918 	struct batadv_tt_req_node *tt_req_node = NULL;
2919 	struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2920 	struct batadv_hard_iface *primary_if;
2921 	bool ret = false;
2922 	int i, size;
2923 
2924 	primary_if = batadv_primary_if_get_selected(bat_priv);
2925 	if (!primary_if)
2926 		goto out;
2927 
2928 	/* The new tt_req will be issued only if I'm not waiting for a
2929 	 * reply from the same orig_node yet
2930 	 */
2931 	tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2932 	if (!tt_req_node)
2933 		goto out;
2934 
2935 	size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2936 	tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2937 	if (!tvlv_tt_data)
2938 		goto out;
2939 
2940 	tvlv_tt_data->flags = BATADV_TT_REQUEST;
2941 	tvlv_tt_data->ttvn = ttvn;
2942 	tvlv_tt_data->num_vlan = htons(num_vlan);
2943 
2944 	/* send all the CRCs within the request. This is needed by intermediate
2945 	 * nodes to ensure they have the correct table before replying
2946 	 */
2947 	tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2948 	for (i = 0; i < num_vlan; i++) {
2949 		tt_vlan_req->vid = tt_vlan->vid;
2950 		tt_vlan_req->crc = tt_vlan->crc;
2951 
2952 		tt_vlan_req++;
2953 		tt_vlan++;
2954 	}
2955 
2956 	if (full_table)
2957 		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2958 
2959 	batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2960 		   dst_orig_node->orig, full_table ? 'F' : '.');
2961 
2962 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2963 	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2964 				 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2965 				 tvlv_tt_data, size);
2966 	ret = true;
2967 
2968 out:
2969 	batadv_hardif_put(primary_if);
2970 
2971 	if (ret && tt_req_node) {
2972 		spin_lock_bh(&bat_priv->tt.req_list_lock);
2973 		if (!hlist_unhashed(&tt_req_node->list)) {
2974 			hlist_del_init(&tt_req_node->list);
2975 			batadv_tt_req_node_put(tt_req_node);
2976 		}
2977 		spin_unlock_bh(&bat_priv->tt.req_list_lock);
2978 	}
2979 
2980 	batadv_tt_req_node_put(tt_req_node);
2981 
2982 	kfree(tvlv_tt_data);
2983 	return ret;
2984 }
2985 
2986 /**
2987  * batadv_send_other_tt_response() - send reply to tt request concerning another
2988  *  node's translation table
2989  * @bat_priv: the bat priv with all the soft interface information
2990  * @tt_data: tt data containing the tt request information
2991  * @req_src: mac address of tt request sender
2992  * @req_dst: mac address of tt request recipient
2993  *
2994  * Return: true if tt request reply was sent, false otherwise.
2995  */
batadv_send_other_tt_response(struct batadv_priv * bat_priv,struct batadv_tvlv_tt_data * tt_data,u8 * req_src,u8 * req_dst)2996 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2997 					  struct batadv_tvlv_tt_data *tt_data,
2998 					  u8 *req_src, u8 *req_dst)
2999 {
3000 	struct batadv_orig_node *req_dst_orig_node;
3001 	struct batadv_orig_node *res_dst_orig_node = NULL;
3002 	struct batadv_tvlv_tt_change *tt_change;
3003 	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3004 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
3005 	bool ret = false, full_table;
3006 	u8 orig_ttvn, req_ttvn;
3007 	u16 tvlv_len;
3008 	s32 tt_len;
3009 
3010 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3011 		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3012 		   req_src, tt_data->ttvn, req_dst,
3013 		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3014 
3015 	/* Let's get the orig node of the REAL destination */
3016 	req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3017 	if (!req_dst_orig_node)
3018 		goto out;
3019 
3020 	res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3021 	if (!res_dst_orig_node)
3022 		goto out;
3023 
3024 	orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3025 	req_ttvn = tt_data->ttvn;
3026 
3027 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3028 	/* this node doesn't have the requested data */
3029 	if (orig_ttvn != req_ttvn ||
3030 	    !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3031 					ntohs(tt_data->num_vlan)))
3032 		goto out;
3033 
3034 	/* If the full table has been explicitly requested */
3035 	if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3036 	    !req_dst_orig_node->tt_buff)
3037 		full_table = true;
3038 	else
3039 		full_table = false;
3040 
3041 	/* TT fragmentation hasn't been implemented yet, so send as many
3042 	 * TT entries fit a single packet as possible only
3043 	 */
3044 	if (!full_table) {
3045 		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3046 		tt_len = req_dst_orig_node->tt_buff_len;
3047 
3048 		tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3049 							      &tvlv_tt_data,
3050 							      &tt_change,
3051 							      &tt_len);
3052 		if (!tt_len)
3053 			goto unlock;
3054 
3055 		/* Copy the last orig_node's OGM buffer */
3056 		memcpy(tt_change, req_dst_orig_node->tt_buff,
3057 		       req_dst_orig_node->tt_buff_len);
3058 		spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3059 	} else {
3060 		/* allocate the tvlv, put the tt_data and all the tt_vlan_data
3061 		 * in the initial part
3062 		 */
3063 		tt_len = -1;
3064 		tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3065 							      &tvlv_tt_data,
3066 							      &tt_change,
3067 							      &tt_len);
3068 		if (!tt_len)
3069 			goto out;
3070 
3071 		/* fill the rest of the tvlv with the real TT entries */
3072 		batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3073 					tt_change, tt_len,
3074 					batadv_tt_global_valid,
3075 					req_dst_orig_node);
3076 	}
3077 
3078 	/* Don't send the response, if larger than fragmented packet. */
3079 	tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3080 	if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3081 		net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3082 					 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3083 					 res_dst_orig_node->orig);
3084 		goto out;
3085 	}
3086 
3087 	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3088 	tvlv_tt_data->ttvn = req_ttvn;
3089 
3090 	if (full_table)
3091 		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3092 
3093 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3094 		   "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3095 		   res_dst_orig_node->orig, req_dst_orig_node->orig,
3096 		   full_table ? 'F' : '.', req_ttvn);
3097 
3098 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3099 
3100 	batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3101 				 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3102 				 tvlv_len);
3103 
3104 	ret = true;
3105 	goto out;
3106 
3107 unlock:
3108 	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3109 
3110 out:
3111 	batadv_orig_node_put(res_dst_orig_node);
3112 	batadv_orig_node_put(req_dst_orig_node);
3113 	kfree(tvlv_tt_data);
3114 	return ret;
3115 }
3116 
3117 /**
3118  * batadv_send_my_tt_response() - send reply to tt request concerning this
3119  *  node's translation table
3120  * @bat_priv: the bat priv with all the soft interface information
3121  * @tt_data: tt data containing the tt request information
3122  * @req_src: mac address of tt request sender
3123  *
3124  * Return: true if tt request reply was sent, false otherwise.
3125  */
batadv_send_my_tt_response(struct batadv_priv * bat_priv,struct batadv_tvlv_tt_data * tt_data,u8 * req_src)3126 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3127 				       struct batadv_tvlv_tt_data *tt_data,
3128 				       u8 *req_src)
3129 {
3130 	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3131 	struct batadv_hard_iface *primary_if = NULL;
3132 	struct batadv_tvlv_tt_change *tt_change;
3133 	struct batadv_orig_node *orig_node;
3134 	u8 my_ttvn, req_ttvn;
3135 	u16 tvlv_len;
3136 	bool full_table;
3137 	s32 tt_len;
3138 
3139 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3140 		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3141 		   req_src, tt_data->ttvn,
3142 		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3143 
3144 	spin_lock_bh(&bat_priv->tt.commit_lock);
3145 
3146 	my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3147 	req_ttvn = tt_data->ttvn;
3148 
3149 	orig_node = batadv_orig_hash_find(bat_priv, req_src);
3150 	if (!orig_node)
3151 		goto out;
3152 
3153 	primary_if = batadv_primary_if_get_selected(bat_priv);
3154 	if (!primary_if)
3155 		goto out;
3156 
3157 	/* If the full table has been explicitly requested or the gap
3158 	 * is too big send the whole local translation table
3159 	 */
3160 	if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3161 	    !bat_priv->tt.last_changeset)
3162 		full_table = true;
3163 	else
3164 		full_table = false;
3165 
3166 	/* TT fragmentation hasn't been implemented yet, so send as many
3167 	 * TT entries fit a single packet as possible only
3168 	 */
3169 	if (!full_table) {
3170 		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3171 
3172 		tt_len = bat_priv->tt.last_changeset_len;
3173 		tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3174 							     &tvlv_tt_data,
3175 							     &tt_change,
3176 							     &tt_len);
3177 		if (!tt_len || !tvlv_len)
3178 			goto unlock;
3179 
3180 		/* Copy the last orig_node's OGM buffer */
3181 		memcpy(tt_change, bat_priv->tt.last_changeset,
3182 		       bat_priv->tt.last_changeset_len);
3183 		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3184 	} else {
3185 		req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3186 
3187 		/* allocate the tvlv, put the tt_data and all the tt_vlan_data
3188 		 * in the initial part
3189 		 */
3190 		tt_len = -1;
3191 		tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3192 							     &tvlv_tt_data,
3193 							     &tt_change,
3194 							     &tt_len);
3195 		if (!tt_len || !tvlv_len)
3196 			goto out;
3197 
3198 		/* fill the rest of the tvlv with the real TT entries */
3199 		batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3200 					tt_change, tt_len,
3201 					batadv_tt_local_valid, NULL);
3202 	}
3203 
3204 	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3205 	tvlv_tt_data->ttvn = req_ttvn;
3206 
3207 	if (full_table)
3208 		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3209 
3210 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3211 		   "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3212 		   orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3213 
3214 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3215 
3216 	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3217 				 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3218 				 tvlv_len);
3219 
3220 	goto out;
3221 
3222 unlock:
3223 	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3224 out:
3225 	spin_unlock_bh(&bat_priv->tt.commit_lock);
3226 	batadv_orig_node_put(orig_node);
3227 	batadv_hardif_put(primary_if);
3228 	kfree(tvlv_tt_data);
3229 	/* The packet was for this host, so it doesn't need to be re-routed */
3230 	return true;
3231 }
3232 
3233 /**
3234  * batadv_send_tt_response() - send reply to tt request
3235  * @bat_priv: the bat priv with all the soft interface information
3236  * @tt_data: tt data containing the tt request information
3237  * @req_src: mac address of tt request sender
3238  * @req_dst: mac address of tt request recipient
3239  *
3240  * Return: true if tt request reply was sent, false otherwise.
3241  */
batadv_send_tt_response(struct batadv_priv * bat_priv,struct batadv_tvlv_tt_data * tt_data,u8 * req_src,u8 * req_dst)3242 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3243 				    struct batadv_tvlv_tt_data *tt_data,
3244 				    u8 *req_src, u8 *req_dst)
3245 {
3246 	if (batadv_is_my_mac(bat_priv, req_dst))
3247 		return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3248 	return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3249 					     req_dst);
3250 }
3251 
_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)3252 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3253 				      struct batadv_orig_node *orig_node,
3254 				      struct batadv_tvlv_tt_change *tt_change,
3255 				      u16 tt_num_changes, u8 ttvn)
3256 {
3257 	int i;
3258 	int roams;
3259 
3260 	for (i = 0; i < tt_num_changes; i++) {
3261 		if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3262 			roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3263 			batadv_tt_global_del(bat_priv, orig_node,
3264 					     (tt_change + i)->addr,
3265 					     ntohs((tt_change + i)->vid),
3266 					     "tt removed by changes",
3267 					     roams);
3268 		} else {
3269 			if (!batadv_tt_global_add(bat_priv, orig_node,
3270 						  (tt_change + i)->addr,
3271 						  ntohs((tt_change + i)->vid),
3272 						  (tt_change + i)->flags, ttvn))
3273 				/* In case of problem while storing a
3274 				 * global_entry, we stop the updating
3275 				 * procedure without committing the
3276 				 * ttvn change. This will avoid to send
3277 				 * corrupted data on tt_request
3278 				 */
3279 				return;
3280 		}
3281 	}
3282 	set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3283 }
3284 
batadv_tt_fill_gtable(struct batadv_priv * bat_priv,struct batadv_tvlv_tt_change * tt_change,u8 ttvn,u8 * resp_src,u16 num_entries)3285 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3286 				  struct batadv_tvlv_tt_change *tt_change,
3287 				  u8 ttvn, u8 *resp_src,
3288 				  u16 num_entries)
3289 {
3290 	struct batadv_orig_node *orig_node;
3291 
3292 	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3293 	if (!orig_node)
3294 		goto out;
3295 
3296 	/* Purge the old table first.. */
3297 	batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3298 				  "Received full table");
3299 
3300 	_batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3301 				  ttvn);
3302 
3303 	spin_lock_bh(&orig_node->tt_buff_lock);
3304 	kfree(orig_node->tt_buff);
3305 	orig_node->tt_buff_len = 0;
3306 	orig_node->tt_buff = NULL;
3307 	spin_unlock_bh(&orig_node->tt_buff_lock);
3308 
3309 	atomic_set(&orig_node->last_ttvn, ttvn);
3310 
3311 out:
3312 	batadv_orig_node_put(orig_node);
3313 }
3314 
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)3315 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3316 				     struct batadv_orig_node *orig_node,
3317 				     u16 tt_num_changes, u8 ttvn,
3318 				     struct batadv_tvlv_tt_change *tt_change)
3319 {
3320 	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3321 				  tt_num_changes, ttvn);
3322 
3323 	batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3324 				   batadv_tt_len(tt_num_changes));
3325 	atomic_set(&orig_node->last_ttvn, ttvn);
3326 }
3327 
3328 /**
3329  * batadv_is_my_client() - check if a client is served by the local node
3330  * @bat_priv: the bat priv with all the soft interface information
3331  * @addr: the mac address of the client to check
3332  * @vid: VLAN identifier
3333  *
3334  * Return: true if the client is served by this node, false otherwise.
3335  */
batadv_is_my_client(struct batadv_priv * bat_priv,const u8 * addr,unsigned short vid)3336 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3337 			 unsigned short vid)
3338 {
3339 	struct batadv_tt_local_entry *tt_local_entry;
3340 	bool ret = false;
3341 
3342 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3343 	if (!tt_local_entry)
3344 		goto out;
3345 	/* Check if the client has been logically deleted (but is kept for
3346 	 * consistency purpose)
3347 	 */
3348 	if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3349 	    (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3350 		goto out;
3351 	ret = true;
3352 out:
3353 	batadv_tt_local_entry_put(tt_local_entry);
3354 	return ret;
3355 }
3356 
3357 /**
3358  * batadv_handle_tt_response() - process incoming tt reply
3359  * @bat_priv: the bat priv with all the soft interface information
3360  * @tt_data: tt data containing the tt request information
3361  * @resp_src: mac address of tt reply sender
3362  * @num_entries: number of tt change entries appended to the tt data
3363  */
batadv_handle_tt_response(struct batadv_priv * bat_priv,struct batadv_tvlv_tt_data * tt_data,u8 * resp_src,u16 num_entries)3364 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3365 				      struct batadv_tvlv_tt_data *tt_data,
3366 				      u8 *resp_src, u16 num_entries)
3367 {
3368 	struct batadv_tt_req_node *node;
3369 	struct hlist_node *safe;
3370 	struct batadv_orig_node *orig_node = NULL;
3371 	struct batadv_tvlv_tt_change *tt_change;
3372 	u8 *tvlv_ptr = (u8 *)tt_data;
3373 	u16 change_offset;
3374 
3375 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3376 		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3377 		   resp_src, tt_data->ttvn, num_entries,
3378 		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3379 
3380 	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3381 	if (!orig_node)
3382 		goto out;
3383 
3384 	spin_lock_bh(&orig_node->tt_lock);
3385 
3386 	change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3387 	change_offset *= ntohs(tt_data->num_vlan);
3388 	change_offset += sizeof(*tt_data);
3389 	tvlv_ptr += change_offset;
3390 
3391 	tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3392 	if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3393 		batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3394 				      resp_src, num_entries);
3395 	} else {
3396 		batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3397 					 tt_data->ttvn, tt_change);
3398 	}
3399 
3400 	/* Recalculate the CRC for this orig_node and store it */
3401 	batadv_tt_global_update_crc(bat_priv, orig_node);
3402 
3403 	spin_unlock_bh(&orig_node->tt_lock);
3404 
3405 	/* Delete the tt_req_node from pending tt_requests list */
3406 	spin_lock_bh(&bat_priv->tt.req_list_lock);
3407 	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3408 		if (!batadv_compare_eth(node->addr, resp_src))
3409 			continue;
3410 		hlist_del_init(&node->list);
3411 		batadv_tt_req_node_put(node);
3412 	}
3413 
3414 	spin_unlock_bh(&bat_priv->tt.req_list_lock);
3415 out:
3416 	batadv_orig_node_put(orig_node);
3417 }
3418 
batadv_tt_roam_list_free(struct batadv_priv * bat_priv)3419 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3420 {
3421 	struct batadv_tt_roam_node *node, *safe;
3422 
3423 	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3424 
3425 	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3426 		list_del(&node->list);
3427 		kmem_cache_free(batadv_tt_roam_cache, node);
3428 	}
3429 
3430 	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3431 }
3432 
batadv_tt_roam_purge(struct batadv_priv * bat_priv)3433 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3434 {
3435 	struct batadv_tt_roam_node *node, *safe;
3436 
3437 	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3438 	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3439 		if (!batadv_has_timed_out(node->first_time,
3440 					  BATADV_ROAMING_MAX_TIME))
3441 			continue;
3442 
3443 		list_del(&node->list);
3444 		kmem_cache_free(batadv_tt_roam_cache, node);
3445 	}
3446 	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3447 }
3448 
3449 /**
3450  * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3451  * @bat_priv: the bat priv with all the soft interface information
3452  * @client: mac address of the roaming client
3453  *
3454  * This function checks whether the client already reached the
3455  * maximum number of possible roaming phases. In this case the ROAMING_ADV
3456  * will not be sent.
3457  *
3458  * Return: true if the ROAMING_ADV can be sent, false otherwise
3459  */
batadv_tt_check_roam_count(struct batadv_priv * bat_priv,u8 * client)3460 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3461 {
3462 	struct batadv_tt_roam_node *tt_roam_node;
3463 	bool ret = false;
3464 
3465 	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3466 	/* The new tt_req will be issued only if I'm not waiting for a
3467 	 * reply from the same orig_node yet
3468 	 */
3469 	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3470 		if (!batadv_compare_eth(tt_roam_node->addr, client))
3471 			continue;
3472 
3473 		if (batadv_has_timed_out(tt_roam_node->first_time,
3474 					 BATADV_ROAMING_MAX_TIME))
3475 			continue;
3476 
3477 		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3478 			/* Sorry, you roamed too many times! */
3479 			goto unlock;
3480 		ret = true;
3481 		break;
3482 	}
3483 
3484 	if (!ret) {
3485 		tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3486 						GFP_ATOMIC);
3487 		if (!tt_roam_node)
3488 			goto unlock;
3489 
3490 		tt_roam_node->first_time = jiffies;
3491 		atomic_set(&tt_roam_node->counter,
3492 			   BATADV_ROAMING_MAX_COUNT - 1);
3493 		ether_addr_copy(tt_roam_node->addr, client);
3494 
3495 		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3496 		ret = true;
3497 	}
3498 
3499 unlock:
3500 	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3501 	return ret;
3502 }
3503 
3504 /**
3505  * batadv_send_roam_adv() - send a roaming advertisement message
3506  * @bat_priv: the bat priv with all the soft interface information
3507  * @client: mac address of the roaming client
3508  * @vid: VLAN identifier
3509  * @orig_node: message destination
3510  *
3511  * Send a ROAMING_ADV message to the node which was previously serving this
3512  * client. This is done to inform the node that from now on all traffic destined
3513  * for this particular roamed client has to be forwarded to the sender of the
3514  * roaming message.
3515  */
batadv_send_roam_adv(struct batadv_priv * bat_priv,u8 * client,unsigned short vid,struct batadv_orig_node * orig_node)3516 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3517 				 unsigned short vid,
3518 				 struct batadv_orig_node *orig_node)
3519 {
3520 	struct batadv_hard_iface *primary_if;
3521 	struct batadv_tvlv_roam_adv tvlv_roam;
3522 
3523 	primary_if = batadv_primary_if_get_selected(bat_priv);
3524 	if (!primary_if)
3525 		goto out;
3526 
3527 	/* before going on we have to check whether the client has
3528 	 * already roamed to us too many times
3529 	 */
3530 	if (!batadv_tt_check_roam_count(bat_priv, client))
3531 		goto out;
3532 
3533 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3534 		   "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3535 		   orig_node->orig, client, batadv_print_vid(vid));
3536 
3537 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3538 
3539 	memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3540 	tvlv_roam.vid = htons(vid);
3541 
3542 	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3543 				 orig_node->orig, BATADV_TVLV_ROAM, 1,
3544 				 &tvlv_roam, sizeof(tvlv_roam));
3545 
3546 out:
3547 	batadv_hardif_put(primary_if);
3548 }
3549 
batadv_tt_purge(struct work_struct * work)3550 static void batadv_tt_purge(struct work_struct *work)
3551 {
3552 	struct delayed_work *delayed_work;
3553 	struct batadv_priv_tt *priv_tt;
3554 	struct batadv_priv *bat_priv;
3555 
3556 	delayed_work = to_delayed_work(work);
3557 	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3558 	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3559 
3560 	batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3561 	batadv_tt_global_purge(bat_priv);
3562 	batadv_tt_req_purge(bat_priv);
3563 	batadv_tt_roam_purge(bat_priv);
3564 
3565 	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3566 			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3567 }
3568 
3569 /**
3570  * batadv_tt_free() - Free translation table of soft interface
3571  * @bat_priv: the bat priv with all the soft interface information
3572  */
batadv_tt_free(struct batadv_priv * bat_priv)3573 void batadv_tt_free(struct batadv_priv *bat_priv)
3574 {
3575 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3576 
3577 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3578 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3579 
3580 	cancel_delayed_work_sync(&bat_priv->tt.work);
3581 
3582 	batadv_tt_local_table_free(bat_priv);
3583 	batadv_tt_global_table_free(bat_priv);
3584 	batadv_tt_req_list_free(bat_priv);
3585 	batadv_tt_changes_list_free(bat_priv);
3586 	batadv_tt_roam_list_free(bat_priv);
3587 
3588 	kfree(bat_priv->tt.last_changeset);
3589 }
3590 
3591 /**
3592  * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3593  *  table and possibly count them in the TT size
3594  * @bat_priv: the bat priv with all the soft interface information
3595  * @flags: the flag to switch
3596  * @enable: whether to set or unset the flag
3597  * @count: whether to increase the TT size by the number of changed entries
3598  */
batadv_tt_local_set_flags(struct batadv_priv * bat_priv,u16 flags,bool enable,bool count)3599 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3600 				      bool enable, bool count)
3601 {
3602 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3603 	struct batadv_tt_common_entry *tt_common_entry;
3604 	struct hlist_head *head;
3605 	u32 i;
3606 
3607 	if (!hash)
3608 		return;
3609 
3610 	for (i = 0; i < hash->size; i++) {
3611 		head = &hash->table[i];
3612 
3613 		rcu_read_lock();
3614 		hlist_for_each_entry_rcu(tt_common_entry,
3615 					 head, hash_entry) {
3616 			if (enable) {
3617 				if ((tt_common_entry->flags & flags) == flags)
3618 					continue;
3619 				tt_common_entry->flags |= flags;
3620 			} else {
3621 				if (!(tt_common_entry->flags & flags))
3622 					continue;
3623 				tt_common_entry->flags &= ~flags;
3624 			}
3625 
3626 			if (!count)
3627 				continue;
3628 
3629 			batadv_tt_local_size_inc(bat_priv,
3630 						 tt_common_entry->vid);
3631 		}
3632 		rcu_read_unlock();
3633 	}
3634 }
3635 
3636 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
batadv_tt_local_purge_pending_clients(struct batadv_priv * bat_priv)3637 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3638 {
3639 	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3640 	struct batadv_tt_common_entry *tt_common;
3641 	struct batadv_tt_local_entry *tt_local;
3642 	struct hlist_node *node_tmp;
3643 	struct hlist_head *head;
3644 	spinlock_t *list_lock; /* protects write access to the hash lists */
3645 	u32 i;
3646 
3647 	if (!hash)
3648 		return;
3649 
3650 	for (i = 0; i < hash->size; i++) {
3651 		head = &hash->table[i];
3652 		list_lock = &hash->list_locks[i];
3653 
3654 		spin_lock_bh(list_lock);
3655 		hlist_for_each_entry_safe(tt_common, node_tmp, head,
3656 					  hash_entry) {
3657 			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3658 				continue;
3659 
3660 			batadv_dbg(BATADV_DBG_TT, bat_priv,
3661 				   "Deleting local tt entry (%pM, vid: %d): pending\n",
3662 				   tt_common->addr,
3663 				   batadv_print_vid(tt_common->vid));
3664 
3665 			batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3666 			hlist_del_rcu(&tt_common->hash_entry);
3667 			tt_local = container_of(tt_common,
3668 						struct batadv_tt_local_entry,
3669 						common);
3670 
3671 			batadv_tt_local_entry_put(tt_local);
3672 		}
3673 		spin_unlock_bh(list_lock);
3674 	}
3675 }
3676 
3677 /**
3678  * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3679  *  which have been queued in the time since the last commit
3680  * @bat_priv: the bat priv with all the soft interface information
3681  *
3682  * Caller must hold tt->commit_lock.
3683  */
batadv_tt_local_commit_changes_nolock(struct batadv_priv * bat_priv)3684 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3685 {
3686 	lockdep_assert_held(&bat_priv->tt.commit_lock);
3687 
3688 	if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3689 		if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3690 			batadv_tt_tvlv_container_update(bat_priv);
3691 		return;
3692 	}
3693 
3694 	batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3695 
3696 	batadv_tt_local_purge_pending_clients(bat_priv);
3697 	batadv_tt_local_update_crc(bat_priv);
3698 
3699 	/* Increment the TTVN only once per OGM interval */
3700 	atomic_inc(&bat_priv->tt.vn);
3701 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3702 		   "Local changes committed, updating to ttvn %u\n",
3703 		   (u8)atomic_read(&bat_priv->tt.vn));
3704 
3705 	/* reset the sending counter */
3706 	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3707 	batadv_tt_tvlv_container_update(bat_priv);
3708 }
3709 
3710 /**
3711  * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3712  *  have been queued in the time since the last commit
3713  * @bat_priv: the bat priv with all the soft interface information
3714  */
batadv_tt_local_commit_changes(struct batadv_priv * bat_priv)3715 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3716 {
3717 	spin_lock_bh(&bat_priv->tt.commit_lock);
3718 	batadv_tt_local_commit_changes_nolock(bat_priv);
3719 	spin_unlock_bh(&bat_priv->tt.commit_lock);
3720 }
3721 
3722 /**
3723  * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3724  * @bat_priv: the bat priv with all the soft interface information
3725  * @src: source mac address of packet
3726  * @dst: destination mac address of packet
3727  * @vid: vlan id of packet
3728  *
3729  * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3730  */
batadv_is_ap_isolated(struct batadv_priv * bat_priv,u8 * src,u8 * dst,unsigned short vid)3731 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3732 			   unsigned short vid)
3733 {
3734 	struct batadv_tt_local_entry *tt_local_entry;
3735 	struct batadv_tt_global_entry *tt_global_entry;
3736 	struct batadv_softif_vlan *vlan;
3737 	bool ret = false;
3738 
3739 	vlan = batadv_softif_vlan_get(bat_priv, vid);
3740 	if (!vlan)
3741 		return false;
3742 
3743 	if (!atomic_read(&vlan->ap_isolation))
3744 		goto vlan_put;
3745 
3746 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3747 	if (!tt_local_entry)
3748 		goto vlan_put;
3749 
3750 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3751 	if (!tt_global_entry)
3752 		goto local_entry_put;
3753 
3754 	if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3755 		ret = true;
3756 
3757 	batadv_tt_global_entry_put(tt_global_entry);
3758 local_entry_put:
3759 	batadv_tt_local_entry_put(tt_local_entry);
3760 vlan_put:
3761 	batadv_softif_vlan_put(vlan);
3762 	return ret;
3763 }
3764 
3765 /**
3766  * batadv_tt_update_orig() - update global translation table with new tt
3767  *  information received via ogms
3768  * @bat_priv: the bat priv with all the soft interface information
3769  * @orig_node: the orig_node of the ogm
3770  * @tt_buff: pointer to the first tvlv VLAN entry
3771  * @tt_num_vlan: number of tvlv VLAN entries
3772  * @tt_change: pointer to the first entry in the TT buffer
3773  * @tt_num_changes: number of tt changes inside the tt buffer
3774  * @ttvn: translation table version number of this changeset
3775  */
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)3776 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3777 				  struct batadv_orig_node *orig_node,
3778 				  const void *tt_buff, u16 tt_num_vlan,
3779 				  struct batadv_tvlv_tt_change *tt_change,
3780 				  u16 tt_num_changes, u8 ttvn)
3781 {
3782 	u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3783 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
3784 	bool full_table = true;
3785 	bool has_tt_init;
3786 
3787 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3788 	has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3789 			       &orig_node->capa_initialized);
3790 
3791 	/* orig table not initialised AND first diff is in the OGM OR the ttvn
3792 	 * increased by one -> we can apply the attached changes
3793 	 */
3794 	if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3795 		/* the OGM could not contain the changes due to their size or
3796 		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3797 		 * times.
3798 		 * In this case send a tt request
3799 		 */
3800 		if (!tt_num_changes) {
3801 			full_table = false;
3802 			goto request_table;
3803 		}
3804 
3805 		spin_lock_bh(&orig_node->tt_lock);
3806 
3807 		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3808 					 ttvn, tt_change);
3809 
3810 		/* Even if we received the precomputed crc with the OGM, we
3811 		 * prefer to recompute it to spot any possible inconsistency
3812 		 * in the global table
3813 		 */
3814 		batadv_tt_global_update_crc(bat_priv, orig_node);
3815 
3816 		spin_unlock_bh(&orig_node->tt_lock);
3817 
3818 		/* The ttvn alone is not enough to guarantee consistency
3819 		 * because a single value could represent different states
3820 		 * (due to the wrap around). Thus a node has to check whether
3821 		 * the resulting table (after applying the changes) is still
3822 		 * consistent or not. E.g. a node could disconnect while its
3823 		 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3824 		 * checking the CRC value is mandatory to detect the
3825 		 * inconsistency
3826 		 */
3827 		if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3828 						tt_num_vlan))
3829 			goto request_table;
3830 	} else {
3831 		/* if we missed more than one change or our tables are not
3832 		 * in sync anymore -> request fresh tt data
3833 		 */
3834 		if (!has_tt_init || ttvn != orig_ttvn ||
3835 		    !batadv_tt_global_check_crc(orig_node, tt_vlan,
3836 						tt_num_vlan)) {
3837 request_table:
3838 			batadv_dbg(BATADV_DBG_TT, bat_priv,
3839 				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3840 				   orig_node->orig, ttvn, orig_ttvn,
3841 				   tt_num_changes);
3842 			batadv_send_tt_request(bat_priv, orig_node, ttvn,
3843 					       tt_vlan, tt_num_vlan,
3844 					       full_table);
3845 			return;
3846 		}
3847 	}
3848 }
3849 
3850 /**
3851  * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
3852  * @bat_priv: the bat priv with all the soft interface information
3853  * @addr: the mac address of the client to check
3854  * @vid: VLAN identifier
3855  *
3856  * Return: true if we know that the client has moved from its old originator
3857  * to another one. This entry is still kept for consistency purposes and will be
3858  * deleted later by a DEL or because of timeout
3859  */
batadv_tt_global_client_is_roaming(struct batadv_priv * bat_priv,u8 * addr,unsigned short vid)3860 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3861 					u8 *addr, unsigned short vid)
3862 {
3863 	struct batadv_tt_global_entry *tt_global_entry;
3864 	bool ret = false;
3865 
3866 	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3867 	if (!tt_global_entry)
3868 		goto out;
3869 
3870 	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3871 	batadv_tt_global_entry_put(tt_global_entry);
3872 out:
3873 	return ret;
3874 }
3875 
3876 /**
3877  * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
3878  * @bat_priv: the bat priv with all the soft interface information
3879  * @addr: the mac address of the local client to query
3880  * @vid: VLAN identifier
3881  *
3882  * Return: true if the local client is known to be roaming (it is not served by
3883  * this node anymore) or not. If yes, the client is still present in the table
3884  * to keep the latter consistent with the node TTVN
3885  */
batadv_tt_local_client_is_roaming(struct batadv_priv * bat_priv,u8 * addr,unsigned short vid)3886 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3887 				       u8 *addr, unsigned short vid)
3888 {
3889 	struct batadv_tt_local_entry *tt_local_entry;
3890 	bool ret = false;
3891 
3892 	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3893 	if (!tt_local_entry)
3894 		goto out;
3895 
3896 	ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3897 	batadv_tt_local_entry_put(tt_local_entry);
3898 out:
3899 	return ret;
3900 }
3901 
3902 /**
3903  * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
3904  * @bat_priv: the bat priv with all the soft interface information
3905  * @orig_node: orig node which the temporary entry should be associated with
3906  * @addr: mac address of the client
3907  * @vid: VLAN id of the new temporary global translation table
3908  *
3909  * Return: true when temporary tt entry could be added, false otherwise
3910  */
batadv_tt_add_temporary_global_entry(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,const unsigned char * addr,unsigned short vid)3911 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3912 					  struct batadv_orig_node *orig_node,
3913 					  const unsigned char *addr,
3914 					  unsigned short vid)
3915 {
3916 	/* ignore loop detect macs, they are not supposed to be in the tt local
3917 	 * data as well.
3918 	 */
3919 	if (batadv_bla_is_loopdetect_mac(addr))
3920 		return false;
3921 
3922 	if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3923 				  BATADV_TT_CLIENT_TEMP,
3924 				  atomic_read(&orig_node->last_ttvn)))
3925 		return false;
3926 
3927 	batadv_dbg(BATADV_DBG_TT, bat_priv,
3928 		   "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3929 		   addr, batadv_print_vid(vid), orig_node->orig);
3930 
3931 	return true;
3932 }
3933 
3934 /**
3935  * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
3936  *  maximum packet size that can be transported through the mesh
3937  * @soft_iface: netdev struct of the mesh interface
3938  *
3939  * Remove entries older than 'timeout' and half timeout if more entries need
3940  * to be removed.
3941  */
batadv_tt_local_resize_to_mtu(struct net_device * soft_iface)3942 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3943 {
3944 	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3945 	int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3946 	int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3947 	bool reduced = false;
3948 
3949 	spin_lock_bh(&bat_priv->tt.commit_lock);
3950 
3951 	while (true) {
3952 		table_size = batadv_tt_local_table_transmit_size(bat_priv);
3953 		if (packet_size_max >= table_size)
3954 			break;
3955 
3956 		batadv_tt_local_purge(bat_priv, timeout);
3957 		batadv_tt_local_purge_pending_clients(bat_priv);
3958 
3959 		timeout /= 2;
3960 		reduced = true;
3961 		net_ratelimited_function(batadv_info, soft_iface,
3962 					 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3963 					 packet_size_max);
3964 	}
3965 
3966 	/* commit these changes immediately, to avoid synchronization problem
3967 	 * with the TTVN
3968 	 */
3969 	if (reduced)
3970 		batadv_tt_local_commit_changes_nolock(bat_priv);
3971 
3972 	spin_unlock_bh(&bat_priv->tt.commit_lock);
3973 }
3974 
3975 /**
3976  * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
3977  * @bat_priv: the bat priv with all the soft interface information
3978  * @orig: the orig_node of the ogm
3979  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3980  * @tvlv_value: tvlv buffer containing the gateway data
3981  * @tvlv_value_len: tvlv buffer length
3982  */
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)3983 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3984 					  struct batadv_orig_node *orig,
3985 					  u8 flags, void *tvlv_value,
3986 					  u16 tvlv_value_len)
3987 {
3988 	struct batadv_tvlv_tt_vlan_data *tt_vlan;
3989 	struct batadv_tvlv_tt_change *tt_change;
3990 	struct batadv_tvlv_tt_data *tt_data;
3991 	u16 num_entries, num_vlan;
3992 
3993 	if (tvlv_value_len < sizeof(*tt_data))
3994 		return;
3995 
3996 	tt_data = tvlv_value;
3997 	tvlv_value_len -= sizeof(*tt_data);
3998 
3999 	num_vlan = ntohs(tt_data->num_vlan);
4000 
4001 	if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4002 		return;
4003 
4004 	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4005 	tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4006 	tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4007 
4008 	num_entries = batadv_tt_entries(tvlv_value_len);
4009 
4010 	batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4011 			      num_entries, tt_data->ttvn);
4012 }
4013 
4014 /**
4015  * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
4016  *  container
4017  * @bat_priv: the bat priv with all the soft interface information
4018  * @src: mac address of tt tvlv sender
4019  * @dst: mac address of tt tvlv recipient
4020  * @tvlv_value: tvlv buffer containing the tt data
4021  * @tvlv_value_len: tvlv buffer length
4022  *
4023  * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4024  * otherwise.
4025  */
batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv * bat_priv,u8 * src,u8 * dst,void * tvlv_value,u16 tvlv_value_len)4026 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4027 					     u8 *src, u8 *dst,
4028 					     void *tvlv_value,
4029 					     u16 tvlv_value_len)
4030 {
4031 	struct batadv_tvlv_tt_data *tt_data;
4032 	u16 tt_vlan_len, tt_num_entries;
4033 	char tt_flag;
4034 	bool ret;
4035 
4036 	if (tvlv_value_len < sizeof(*tt_data))
4037 		return NET_RX_SUCCESS;
4038 
4039 	tt_data = tvlv_value;
4040 	tvlv_value_len -= sizeof(*tt_data);
4041 
4042 	tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4043 	tt_vlan_len *= ntohs(tt_data->num_vlan);
4044 
4045 	if (tvlv_value_len < tt_vlan_len)
4046 		return NET_RX_SUCCESS;
4047 
4048 	tvlv_value_len -= tt_vlan_len;
4049 	tt_num_entries = batadv_tt_entries(tvlv_value_len);
4050 
4051 	switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4052 	case BATADV_TT_REQUEST:
4053 		batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4054 
4055 		/* If this node cannot provide a TT response the tt_request is
4056 		 * forwarded
4057 		 */
4058 		ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4059 		if (!ret) {
4060 			if (tt_data->flags & BATADV_TT_FULL_TABLE)
4061 				tt_flag = 'F';
4062 			else
4063 				tt_flag = '.';
4064 
4065 			batadv_dbg(BATADV_DBG_TT, bat_priv,
4066 				   "Routing TT_REQUEST to %pM [%c]\n",
4067 				   dst, tt_flag);
4068 			/* tvlv API will re-route the packet */
4069 			return NET_RX_DROP;
4070 		}
4071 		break;
4072 	case BATADV_TT_RESPONSE:
4073 		batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4074 
4075 		if (batadv_is_my_mac(bat_priv, dst)) {
4076 			batadv_handle_tt_response(bat_priv, tt_data,
4077 						  src, tt_num_entries);
4078 			return NET_RX_SUCCESS;
4079 		}
4080 
4081 		if (tt_data->flags & BATADV_TT_FULL_TABLE)
4082 			tt_flag =  'F';
4083 		else
4084 			tt_flag = '.';
4085 
4086 		batadv_dbg(BATADV_DBG_TT, bat_priv,
4087 			   "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4088 
4089 		/* tvlv API will re-route the packet */
4090 		return NET_RX_DROP;
4091 	}
4092 
4093 	return NET_RX_SUCCESS;
4094 }
4095 
4096 /**
4097  * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4098  *  container
4099  * @bat_priv: the bat priv with all the soft interface information
4100  * @src: mac address of tt tvlv sender
4101  * @dst: mac address of tt tvlv recipient
4102  * @tvlv_value: tvlv buffer containing the tt data
4103  * @tvlv_value_len: tvlv buffer length
4104  *
4105  * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4106  * otherwise.
4107  */
batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv * bat_priv,u8 * src,u8 * dst,void * tvlv_value,u16 tvlv_value_len)4108 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4109 					       u8 *src, u8 *dst,
4110 					       void *tvlv_value,
4111 					       u16 tvlv_value_len)
4112 {
4113 	struct batadv_tvlv_roam_adv *roaming_adv;
4114 	struct batadv_orig_node *orig_node = NULL;
4115 
4116 	/* If this node is not the intended recipient of the
4117 	 * roaming advertisement the packet is forwarded
4118 	 * (the tvlv API will re-route the packet).
4119 	 */
4120 	if (!batadv_is_my_mac(bat_priv, dst))
4121 		return NET_RX_DROP;
4122 
4123 	if (tvlv_value_len < sizeof(*roaming_adv))
4124 		goto out;
4125 
4126 	orig_node = batadv_orig_hash_find(bat_priv, src);
4127 	if (!orig_node)
4128 		goto out;
4129 
4130 	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4131 	roaming_adv = tvlv_value;
4132 
4133 	batadv_dbg(BATADV_DBG_TT, bat_priv,
4134 		   "Received ROAMING_ADV from %pM (client %pM)\n",
4135 		   src, roaming_adv->client);
4136 
4137 	batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4138 			     ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4139 			     atomic_read(&orig_node->last_ttvn) + 1);
4140 
4141 out:
4142 	batadv_orig_node_put(orig_node);
4143 	return NET_RX_SUCCESS;
4144 }
4145 
4146 /**
4147  * batadv_tt_init() - initialise the translation table internals
4148  * @bat_priv: the bat priv with all the soft interface information
4149  *
4150  * Return: 0 on success or negative error number in case of failure.
4151  */
batadv_tt_init(struct batadv_priv * bat_priv)4152 int batadv_tt_init(struct batadv_priv *bat_priv)
4153 {
4154 	int ret;
4155 
4156 	/* synchronized flags must be remote */
4157 	BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4158 
4159 	ret = batadv_tt_local_init(bat_priv);
4160 	if (ret < 0)
4161 		return ret;
4162 
4163 	ret = batadv_tt_global_init(bat_priv);
4164 	if (ret < 0) {
4165 		batadv_tt_local_table_free(bat_priv);
4166 		return ret;
4167 	}
4168 
4169 	batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4170 				     batadv_tt_tvlv_unicast_handler_v1, NULL,
4171 				     BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4172 
4173 	batadv_tvlv_handler_register(bat_priv, NULL,
4174 				     batadv_roam_tvlv_unicast_handler_v1, NULL,
4175 				     BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4176 
4177 	INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4178 	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4179 			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4180 
4181 	return 1;
4182 }
4183 
4184 /**
4185  * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4186  * @bat_priv: the bat priv with all the soft interface information
4187  * @addr: the mac address of the client
4188  * @vid: the identifier of the VLAN where this client is connected
4189  *
4190  * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4191  * otherwise
4192  */
batadv_tt_global_is_isolated(struct batadv_priv * bat_priv,const u8 * addr,unsigned short vid)4193 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4194 				  const u8 *addr, unsigned short vid)
4195 {
4196 	struct batadv_tt_global_entry *tt;
4197 	bool ret;
4198 
4199 	tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4200 	if (!tt)
4201 		return false;
4202 
4203 	ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4204 
4205 	batadv_tt_global_entry_put(tt);
4206 
4207 	return ret;
4208 }
4209 
4210 /**
4211  * batadv_tt_cache_init() - Initialize tt memory object cache
4212  *
4213  * Return: 0 on success or negative error number in case of failure.
4214  */
batadv_tt_cache_init(void)4215 int __init batadv_tt_cache_init(void)
4216 {
4217 	size_t tl_size = sizeof(struct batadv_tt_local_entry);
4218 	size_t tg_size = sizeof(struct batadv_tt_global_entry);
4219 	size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4220 	size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4221 	size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4222 	size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4223 
4224 	batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4225 					    SLAB_HWCACHE_ALIGN, NULL);
4226 	if (!batadv_tl_cache)
4227 		return -ENOMEM;
4228 
4229 	batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4230 					    SLAB_HWCACHE_ALIGN, NULL);
4231 	if (!batadv_tg_cache)
4232 		goto err_tt_tl_destroy;
4233 
4234 	batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4235 						 tt_orig_size, 0,
4236 						 SLAB_HWCACHE_ALIGN, NULL);
4237 	if (!batadv_tt_orig_cache)
4238 		goto err_tt_tg_destroy;
4239 
4240 	batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4241 						   tt_change_size, 0,
4242 						   SLAB_HWCACHE_ALIGN, NULL);
4243 	if (!batadv_tt_change_cache)
4244 		goto err_tt_orig_destroy;
4245 
4246 	batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4247 						tt_req_size, 0,
4248 						SLAB_HWCACHE_ALIGN, NULL);
4249 	if (!batadv_tt_req_cache)
4250 		goto err_tt_change_destroy;
4251 
4252 	batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4253 						 tt_roam_size, 0,
4254 						 SLAB_HWCACHE_ALIGN, NULL);
4255 	if (!batadv_tt_roam_cache)
4256 		goto err_tt_req_destroy;
4257 
4258 	return 0;
4259 
4260 err_tt_req_destroy:
4261 	kmem_cache_destroy(batadv_tt_req_cache);
4262 	batadv_tt_req_cache = NULL;
4263 err_tt_change_destroy:
4264 	kmem_cache_destroy(batadv_tt_change_cache);
4265 	batadv_tt_change_cache = NULL;
4266 err_tt_orig_destroy:
4267 	kmem_cache_destroy(batadv_tt_orig_cache);
4268 	batadv_tt_orig_cache = NULL;
4269 err_tt_tg_destroy:
4270 	kmem_cache_destroy(batadv_tg_cache);
4271 	batadv_tg_cache = NULL;
4272 err_tt_tl_destroy:
4273 	kmem_cache_destroy(batadv_tl_cache);
4274 	batadv_tl_cache = NULL;
4275 
4276 	return -ENOMEM;
4277 }
4278 
4279 /**
4280  * batadv_tt_cache_destroy() - Destroy tt memory object cache
4281  */
batadv_tt_cache_destroy(void)4282 void batadv_tt_cache_destroy(void)
4283 {
4284 	kmem_cache_destroy(batadv_tl_cache);
4285 	kmem_cache_destroy(batadv_tg_cache);
4286 	kmem_cache_destroy(batadv_tt_orig_cache);
4287 	kmem_cache_destroy(batadv_tt_change_cache);
4288 	kmem_cache_destroy(batadv_tt_req_cache);
4289 	kmem_cache_destroy(batadv_tt_roam_cache);
4290 }
4291