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