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