translation-table.c 113.7 KB
Newer Older
1
/* Copyright (C) 2007-2016  B.A.T.M.A.N. contributors:
2
 *
3
 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * 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 "translation-table.h"
19 20 21
#include "main.h"

#include <linux/atomic.h>
22
#include <linux/bitops.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <linux/bug.h>
#include <linux/byteorder/generic.h>
#include <linux/compiler.h>
#include <linux/crc32c.h>
#include <linux/errno.h>
#include <linux/etherdevice.h>
#include <linux/fs.h>
#include <linux/if_ether.h>
#include <linux/jhash.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/lockdep.h>
#include <linux/netdevice.h>
#include <linux/rculist.h>
#include <linux/rcupdate.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/workqueue.h>
#include <net/net_namespace.h>

#include "bridge_loop_avoidance.h"
48
#include "hard-interface.h"
49
#include "hash.h"
50
#include "multicast.h"
51 52 53
#include "originator.h"
#include "packet.h"
#include "soft-interface.h"
54

55 56 57 58
/* hash class keys */
static struct lock_class_key batadv_tt_local_hash_lock_class_key;
static struct lock_class_key batadv_tt_global_hash_lock_class_key;

59
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
60
				 unsigned short vid,
61
				 struct batadv_orig_node *orig_node);
62 63
static void batadv_tt_purge(struct work_struct *work);
static void
64
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
65 66 67
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
				 const unsigned char *addr,
68 69
				 unsigned short vid, const char *message,
				 bool roaming);
70

71
/**
72 73 74
 * batadv_compare_tt - check if two TT entries are the same
 * @node: the list element pointer of the first TT entry
 * @data2: pointer to the tt_common_entry of the second TT entry
75
 *
76 77 78
 * Compare the MAC address and the VLAN ID of the two TT entries and check if
 * they are the same TT client.
 * Return: 1 if the two TT clients are the same, 0 otherwise
79
 */
80
static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
81
{
82
	const void *data1 = container_of(node, struct batadv_tt_common_entry,
83
					 hash_entry);
84 85
	const struct batadv_tt_common_entry *tt1 = data1;
	const struct batadv_tt_common_entry *tt2 = data2;
86

87
	return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
88 89
}

90 91 92 93 94
/**
 * batadv_choose_tt - return the index of the tt entry in the hash table
 * @data: pointer to the tt_common_entry object to map
 * @size: the size of the hash table
 *
95
 * Return: the hash index where the object represented by 'data' should be
96 97
 * stored at.
 */
98
static inline u32 batadv_choose_tt(const void *data, u32 size)
99 100
{
	struct batadv_tt_common_entry *tt;
101
	u32 hash = 0;
102 103

	tt = (struct batadv_tt_common_entry *)data;
104 105
	hash = jhash(&tt->addr, ETH_ALEN, hash);
	hash = jhash(&tt->vid, sizeof(tt->vid), hash);
106 107 108 109 110 111 112 113 114 115

	return hash % size;
}

/**
 * batadv_tt_hash_find - look for a client in the given hash table
 * @hash: the hash table to search
 * @addr: the mac address of the client to look for
 * @vid: VLAN identifier
 *
116
 * Return: a pointer to the tt_common struct belonging to the searched client if
117 118
 * found, NULL otherwise.
 */
119
static struct batadv_tt_common_entry *
120
batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
121
		    unsigned short vid)
122 123
{
	struct hlist_head *head;
124
	struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
125
	u32 index;
126 127 128 129

	if (!hash)
		return NULL;

130
	ether_addr_copy(to_search.addr, addr);
131 132 133
	to_search.vid = vid;

	index = batadv_choose_tt(&to_search, hash->size);
134 135 136
	head = &hash->table[index];

	rcu_read_lock();
137 138 139 140 141
	hlist_for_each_entry_rcu(tt, head, hash_entry) {
		if (!batadv_compare_eth(tt, addr))
			continue;

		if (tt->vid != vid)
142 143
			continue;

144
		if (!atomic_inc_not_zero(&tt->refcount))
145 146
			continue;

147
		tt_tmp = tt;
148 149 150 151
		break;
	}
	rcu_read_unlock();

152
	return tt_tmp;
153 154
}

155 156 157 158 159 160
/**
 * batadv_tt_local_hash_find - search the local table for a given client
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: the mac address of the client to look for
 * @vid: VLAN identifier
 *
161
 * Return: a pointer to the corresponding tt_local_entry struct if the client is
162 163
 * found, NULL otherwise.
 */
164
static struct batadv_tt_local_entry *
165
batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
166
			  unsigned short vid)
167
{
168 169
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local_entry = NULL;
170

171 172
	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
					      vid);
173 174
	if (tt_common_entry)
		tt_local_entry = container_of(tt_common_entry,
175 176
					      struct batadv_tt_local_entry,
					      common);
177 178
	return tt_local_entry;
}
179

180 181 182 183 184 185
/**
 * batadv_tt_global_hash_find - search the global table for a given client
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: the mac address of the client to look for
 * @vid: VLAN identifier
 *
186
 * Return: a pointer to the corresponding tt_global_entry struct if the client
187 188
 * is found, NULL otherwise.
 */
189
static struct batadv_tt_global_entry *
190
batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
191
			   unsigned short vid)
192
{
193 194
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
195

196 197
	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
					      vid);
198 199
	if (tt_common_entry)
		tt_global_entry = container_of(tt_common_entry,
200 201
					       struct batadv_tt_global_entry,
					       common);
202
	return tt_global_entry;
203 204
}

205
static void
206
batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
207
{
208 209
	if (atomic_dec_and_test(&tt_local_entry->common.refcount))
		kfree_rcu(tt_local_entry, common.rcu);
210 211
}

212 213 214 215 216
/**
 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
 *  tt_global_entry and possibly free it
 * @tt_global_entry: the object to free
 */
217
static void
218
batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
219
{
220
	if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
221
		batadv_tt_global_del_orig_list(tt_global_entry);
222
		kfree_rcu(tt_global_entry, common.rcu);
223 224 225
	}
}

226 227
/**
 * batadv_tt_global_hash_count - count the number of orig entries
228
 * @bat_priv: the bat priv with all the soft interface information
229 230 231
 * @addr: the mac address of the client to count entries for
 * @vid: VLAN identifier
 *
232
 * Return: the number of originators advertising the given address/data
233 234 235
 * (excluding ourself).
 */
int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
236
				const u8 *addr, unsigned short vid)
237 238 239 240 241 242 243 244 245 246 247 248 249 250
{
	struct batadv_tt_global_entry *tt_global_entry;
	int count;

	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
	if (!tt_global_entry)
		return 0;

	count = atomic_read(&tt_global_entry->orig_list_count);
	batadv_tt_global_entry_free_ref(tt_global_entry);

	return count;
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/**
 * batadv_tt_local_size_mod - change the size by v of the local table identified
 *  by vid
 * @bat_priv: the bat priv with all the soft interface information
 * @vid: the VLAN identifier of the sub-table to change
 * @v: the amount to sum to the local table size
 */
static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
				     unsigned short vid, int v)
{
	struct batadv_softif_vlan *vlan;

	vlan = batadv_softif_vlan_get(bat_priv, vid);
	if (!vlan)
		return;

	atomic_add(v, &vlan->tt.num_entries);

	batadv_softif_vlan_free_ref(vlan);
}

/**
 * batadv_tt_local_size_inc - increase by one the local table size for the given
 *  vid
 * @bat_priv: the bat priv with all the soft interface information
 * @vid: the VLAN identifier
 */
static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
				     unsigned short vid)
{
	batadv_tt_local_size_mod(bat_priv, vid, 1);
}

/**
 * batadv_tt_local_size_dec - decrease by one the local table size for the given
 *  vid
 * @bat_priv: the bat priv with all the soft interface information
 * @vid: the VLAN identifier
 */
static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
				     unsigned short vid)
{
	batadv_tt_local_size_mod(bat_priv, vid, -1);
}

/**
297 298 299
 * batadv_tt_global_size_mod - change the size by v of the global table
 *  for orig_node identified by vid
 * @orig_node: the originator for which the table has to be modified
300 301 302 303 304 305 306 307 308 309 310 311 312 313
 * @vid: the VLAN identifier
 * @v: the amount to sum to the global table size
 */
static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
				      unsigned short vid, int v)
{
	struct batadv_orig_node_vlan *vlan;

	vlan = batadv_orig_node_vlan_new(orig_node, vid);
	if (!vlan)
		return;

	if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
		spin_lock_bh(&orig_node->vlan_list_lock);
314
		hlist_del_init_rcu(&vlan->list);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
		spin_unlock_bh(&orig_node->vlan_list_lock);
		batadv_orig_node_vlan_free_ref(vlan);
	}

	batadv_orig_node_vlan_free_ref(vlan);
}

/**
 * batadv_tt_global_size_inc - increase by one the global table size for the
 *  given vid
 * @orig_node: the originator which global table size has to be decreased
 * @vid: the vlan identifier
 */
static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
				      unsigned short vid)
{
	batadv_tt_global_size_mod(orig_node, vid, 1);
}

/**
 * batadv_tt_global_size_dec - decrease by one the global table size for the
 *  given vid
 * @orig_node: the originator which global table size has to be decreased
 * @vid: the vlan identifier
 */
static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
				      unsigned short vid)
{
	batadv_tt_global_size_mod(orig_node, vid, -1);
}

346 347 348 349 350 351 352 353 354 355 356 357
/**
 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
 *  queue for free after rcu grace period
 * @orig_entry: tt orig entry to be free'd
 */
static void
batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
{
	batadv_orig_node_free_ref(orig_entry->orig_node);
	kfree_rcu(orig_entry, rcu);
}

358
static void
359
batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
360
{
361 362
	if (!atomic_dec_and_test(&orig_entry->refcount))
		return;
363

364
	batadv_tt_orig_list_entry_release(orig_entry);
365 366
}

367 368 369 370 371 372
/**
 * batadv_tt_local_event - store a local TT event (ADD/DEL)
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_local_entry: the TT entry involved in the event
 * @event_flags: flags to store in the event structure
 */
373
static void batadv_tt_local_event(struct batadv_priv *bat_priv,
374
				  struct batadv_tt_local_entry *tt_local_entry,
375
				  u8 event_flags)
376
{
377
	struct batadv_tt_change_node *tt_change_node, *entry, *safe;
378
	struct batadv_tt_common_entry *common = &tt_local_entry->common;
379
	u8 flags = common->flags | event_flags;
380 381
	bool event_removed = false;
	bool del_op_requested, del_op_entry;
382 383 384 385 386

	tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
	if (!tt_change_node)
		return;

387
	tt_change_node->change.flags = flags;
388 389
	memset(tt_change_node->change.reserved, 0,
	       sizeof(tt_change_node->change.reserved));
390
	ether_addr_copy(tt_change_node->change.addr, common->addr);
391
	tt_change_node->change.vid = htons(common->vid);
392

393
	del_op_requested = flags & BATADV_TT_CLIENT_DEL;
394 395

	/* check for ADD+DEL or DEL+ADD events */
396 397
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
398
				 list) {
399
		if (!batadv_compare_eth(entry->change.addr, common->addr))
400 401 402 403 404 405 406 407 408
			continue;

		/* DEL+ADD in the same orig interval have no effect and can be
		 * removed to avoid silly behaviour on the receiver side. The
		 * other way around (ADD+DEL) can happen in case of roaming of
		 * a client still in the NEW state. Roaming of NEW clients is
		 * now possible due to automatically recognition of "temporary"
		 * clients
		 */
409
		del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
410 411 412 413
		if (!del_op_requested && del_op_entry)
			goto del;
		if (del_op_requested && !del_op_entry)
			goto del;
414 415 416 417 418 419 420

		/* this is a second add in the same originator interval. It
		 * means that flags have been changed: update them!
		 */
		if (!del_op_requested && !del_op_entry)
			entry->change.flags = flags;

421 422 423 424
		continue;
del:
		list_del(&entry->list);
		kfree(entry);
425
		kfree(tt_change_node);
426 427 428 429
		event_removed = true;
		goto unlock;
	}

430
	/* track the change in the OGMinterval list */
431
	list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
432 433

unlock:
434
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
435

436
	if (event_removed)
437
		atomic_dec(&bat_priv->tt.local_changes);
438
	else
439
		atomic_inc(&bat_priv->tt.local_changes);
440 441
}

442 443 444 445
/**
 * batadv_tt_len - compute length in bytes of given number of tt changes
 * @changes_num: number of tt changes
 *
446
 * Return: computed length in bytes.
447 448
 */
static int batadv_tt_len(int changes_num)
449
{
450
	return changes_num * sizeof(struct batadv_tvlv_tt_change);
451 452
}

453 454 455 456
/**
 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
 * @tt_len: available space
 *
457
 * Return: the number of entries.
458
 */
459
static u16 batadv_tt_entries(u16 tt_len)
460 461 462 463
{
	return tt_len / batadv_tt_len(1);
}

464 465 466 467 468
/**
 * batadv_tt_local_table_transmit_size - calculates the local translation table
 *  size when transmitted over the air
 * @bat_priv: the bat priv with all the soft interface information
 *
469
 * Return: local translation table size in bytes.
470 471 472
 */
static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
{
473 474
	u16 num_vlan = 0;
	u16 tt_local_entries = 0;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	struct batadv_softif_vlan *vlan;
	int hdr_size;

	rcu_read_lock();
	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
		num_vlan++;
		tt_local_entries += atomic_read(&vlan->tt.num_entries);
	}
	rcu_read_unlock();

	/* header size of tvlv encapsulated tt response payload */
	hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
	hdr_size += sizeof(struct batadv_tvlv_hdr);
	hdr_size += sizeof(struct batadv_tvlv_tt_data);
	hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);

	return hdr_size + batadv_tt_len(tt_local_entries);
}

494
static int batadv_tt_local_init(struct batadv_priv *bat_priv)
495
{
496
	if (bat_priv->tt.local_hash)
497
		return 0;
498

499
	bat_priv->tt.local_hash = batadv_hash_new(1024);
500

501
	if (!bat_priv->tt.local_hash)
502
		return -ENOMEM;
503

504 505 506
	batadv_hash_set_lock_class(bat_priv->tt.local_hash,
				   &batadv_tt_local_hash_lock_class_key);

507
	return 0;
508 509
}

510 511 512 513 514
static void batadv_tt_global_free(struct batadv_priv *bat_priv,
				  struct batadv_tt_global_entry *tt_global,
				  const char *message)
{
	batadv_dbg(BATADV_DBG_TT, bat_priv,
515 516 517
		   "Deleting global tt entry %pM (vid: %d): %s\n",
		   tt_global->common.addr,
		   BATADV_PRINT_VID(tt_global->common.vid), message);
518 519

	batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
520
			   batadv_choose_tt, &tt_global->common);
521 522 523
	batadv_tt_global_entry_free_ref(tt_global);
}

524 525 526 527 528 529 530 531
/**
 * batadv_tt_local_add - add a new client to the local table or update an
 *  existing client
 * @soft_iface: netdev struct of the mesh interface
 * @addr: the mac address of the client to add
 * @vid: VLAN identifier
 * @ifindex: index of the interface where the client is connected to (useful to
 *  identify wireless clients)
532 533
 * @mark: the value contained in the skb->mark field of the received packet (if
 *  any)
534
 *
535
 * Return: true if the client was successfully added, false otherwise.
536
 */
537 538
bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
			 unsigned short vid, int ifindex, u32 mark)
539
{
540
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
541
	struct batadv_tt_local_entry *tt_local;
542
	struct batadv_tt_global_entry *tt_global = NULL;
543
	struct batadv_softif_vlan *vlan;
544
	struct net_device *in_dev = NULL;
545
	struct hlist_head *head;
546
	struct batadv_tt_orig_list_entry *orig_entry;
547
	int hash_added, table_size, packet_size_max;
548 549
	bool ret = false;
	bool roamed_back = false;
550 551
	u8 remote_flags;
	u32 match_mark;
552

553 554 555
	if (ifindex != BATADV_NULL_IFINDEX)
		in_dev = dev_get_by_index(&init_net, ifindex);

556
	tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
557 558 559

	if (!is_multicast_ether_addr(addr))
		tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
560

561 562
	if (tt_local) {
		tt_local->last_seen = jiffies;
563 564
		if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
			batadv_dbg(BATADV_DBG_TT, bat_priv,
565 566
				   "Re-adding pending client %pM (vid: %d)\n",
				   addr, BATADV_PRINT_VID(vid));
567 568 569 570 571 572 573 574 575 576 577
			/* whatever the reason why the PENDING flag was set,
			 * this is a client which was enqueued to be removed in
			 * this orig_interval. Since it popped up again, the
			 * flag can be reset like it was never enqueued
			 */
			tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
			goto add_event;
		}

		if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
			batadv_dbg(BATADV_DBG_TT, bat_priv,
578 579
				   "Roaming client %pM (vid: %d) came back to its original location\n",
				   addr, BATADV_PRINT_VID(vid));
580 581 582 583 584 585 586 587 588
			/* the ROAM flag is set because this client roamed away
			 * and the node got a roaming_advertisement message. Now
			 * that the client popped up again at its original
			 * location such flag can be unset
			 */
			tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
			roamed_back = true;
		}
		goto check_roaming;
589 590
	}

591 592 593 594 595 596 597 598 599 600 601
	/* Ignore the client if we cannot send it in a full table response. */
	table_size = batadv_tt_local_table_transmit_size(bat_priv);
	table_size += batadv_tt_len(1);
	packet_size_max = atomic_read(&bat_priv->packet_size_max);
	if (table_size > packet_size_max) {
		net_ratelimited_function(batadv_info, soft_iface,
					 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
					 table_size, packet_size_max, addr);
		goto out;
	}

602 603
	tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
	if (!tt_local)
604
		goto out;
605

606 607
	/* increase the refcounter of the related vlan */
	vlan = batadv_softif_vlan_get(bat_priv, vid);
608
	if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
609 610 611
		 addr, BATADV_PRINT_VID(vid))) {
		kfree(tt_local);
		tt_local = NULL;
612
		goto out;
613
	}
614

615
	batadv_dbg(BATADV_DBG_TT, bat_priv,
616 617
		   "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
		   addr, BATADV_PRINT_VID(vid),
618
		   (u8)atomic_read(&bat_priv->tt.vn));
619

620
	ether_addr_copy(tt_local->common.addr, addr);
621 622 623 624 625
	/* The local entry has to be marked as NEW to avoid to send it in
	 * a full table response going out before the next ttvn increment
	 * (consistency check)
	 */
	tt_local->common.flags = BATADV_TT_CLIENT_NEW;
626
	tt_local->common.vid = vid;
627
	if (batadv_is_wifi_netdev(in_dev))
628 629 630 631
		tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
	atomic_set(&tt_local->common.refcount, 2);
	tt_local->last_seen = jiffies;
	tt_local->common.added_at = tt_local->last_seen;
632

633 634 635 636 637
	/* the batman interface mac and multicast addresses should never be
	 * purged
	 */
	if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
	    is_multicast_ether_addr(addr))
638
		tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
639

640
	hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
641
				     batadv_choose_tt, &tt_local->common,
642
				     &tt_local->common.hash_entry);
643 644 645

	if (unlikely(hash_added != 0)) {
		/* remove the reference for the hash */
646
		batadv_tt_local_entry_free_ref(tt_local);
647
		batadv_softif_vlan_free_ref(vlan);
648 649 650
		goto out;
	}

651
add_event:
652
	batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
653

654 655 656 657 658
check_roaming:
	/* Check whether it is a roaming, but don't do anything if the roaming
	 * process has already been handled
	 */
	if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
659
		/* These node are probably going to update their tt table */
660
		head = &tt_global->orig_list;
661
		rcu_read_lock();
662
		hlist_for_each_entry_rcu(orig_entry, head, list) {
663
			batadv_send_roam_adv(bat_priv, tt_global->common.addr,
664
					     tt_global->common.vid,
665
					     orig_entry->orig_node);
666 667
		}
		rcu_read_unlock();
668 669 670 671 672 673 674 675 676 677 678
		if (roamed_back) {
			batadv_tt_global_free(bat_priv, tt_global,
					      "Roaming canceled");
			tt_global = NULL;
		} else {
			/* The global entry has to be marked as ROAMING and
			 * has to be kept for consistency purpose
			 */
			tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
			tt_global->roam_at = jiffies;
		}
679
	}
680

681 682 683 684 685 686 687 688 689
	/* store the current remote flags before altering them. This helps
	 * understanding is flags are changing or not
	 */
	remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;

	if (batadv_is_wifi_netdev(in_dev))
		tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
	else
		tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
690

691 692 693 694 695 696 697 698 699 700 701
	/* check the mark in the skb: if it's equal to the configured
	 * isolation_mark, it means the packet is coming from an isolated
	 * non-mesh client
	 */
	match_mark = (mark & bat_priv->isolation_mark_mask);
	if (bat_priv->isolation_mark_mask &&
	    match_mark == bat_priv->isolation_mark)
		tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
	else
		tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;

702 703 704 705 706 707 708
	/* if any "dynamic" flag has been modified, resend an ADD event for this
	 * entry so that all the nodes can get the new flags
	 */
	if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
		batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);

	ret = true;
709
out:
710 711
	if (in_dev)
		dev_put(in_dev);
712 713 714 715
	if (tt_local)
		batadv_tt_local_entry_free_ref(tt_local);
	if (tt_global)
		batadv_tt_global_entry_free_ref(tt_global);
716
	return ret;
717 718
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
/**
 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
 *  within a TT Response directed to another node
 * @orig_node: originator for which the TT data has to be prepared
 * @tt_data: uninitialised pointer to the address of the TVLV buffer
 * @tt_change: uninitialised pointer to the address of the area where the TT
 *  changed can be stored
 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
 *  function reserves the amount of space needed to send the entire global TT
 *  table. In case of success the value is updated with the real amount of
 *  reserved bytes
 * Allocate the needed amount of memory for the entire TT TVLV and write its
 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
 * objects, one per active VLAN served by the originator node.
 *
734
 * Return: the size of the allocated buffer or 0 in case of failure.
735
 */
736
static u16
737 738 739
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,
740
				   s32 *tt_len)
741
{
742 743 744 745
	u16 num_vlan = 0;
	u16 num_entries = 0;
	u16 change_offset;
	u16 tvlv_len;
746 747
	struct batadv_tvlv_tt_vlan_data *tt_vlan;
	struct batadv_orig_node_vlan *vlan;
748
	u8 *tt_change_ptr;
749 750

	rcu_read_lock();
751
	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
		num_vlan++;
		num_entries += atomic_read(&vlan->tt.num_entries);
	}

	change_offset = sizeof(**tt_data);
	change_offset += num_vlan * sizeof(*tt_vlan);

	/* if tt_len is negative, allocate the space needed by the full table */
	if (*tt_len < 0)
		*tt_len = batadv_tt_len(num_entries);

	tvlv_len = *tt_len;
	tvlv_len += change_offset;

	*tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
	if (!*tt_data) {
		*tt_len = 0;
		goto out;
	}

	(*tt_data)->flags = BATADV_NO_FLAGS;
	(*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
	(*tt_data)->num_vlan = htons(num_vlan);

	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
777
	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
778 779 780 781 782 783
		tt_vlan->vid = htons(vlan->vid);
		tt_vlan->crc = htonl(vlan->tt.crc);

		tt_vlan++;
	}

784
	tt_change_ptr = (u8 *)*tt_data + change_offset;
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	*tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;

out:
	rcu_read_unlock();
	return tvlv_len;
}

/**
 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
 *  node
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: uninitialised pointer to the address of the TVLV buffer
 * @tt_change: uninitialised pointer to the address of the area where the TT
 *  changes can be stored
 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
 *  function reserves the amount of space needed to send the entire local TT
 *  table. In case of success the value is updated with the real amount of
 *  reserved bytes
 *
 * Allocate the needed amount of memory for the entire TT TVLV and write its
 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
 * objects, one per active VLAN.
 *
808
 * Return: the size of the allocated buffer or 0 in case of failure.
809
 */
810
static u16
811 812 813
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,
814
				  s32 *tt_len)
815 816 817
{
	struct batadv_tvlv_tt_vlan_data *tt_vlan;
	struct batadv_softif_vlan *vlan;
818 819 820
	u16 num_vlan = 0;
	u16 num_entries = 0;
	u16 tvlv_len;
821
	u8 *tt_change_ptr;
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	int change_offset;

	rcu_read_lock();
	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
		num_vlan++;
		num_entries += atomic_read(&vlan->tt.num_entries);
	}

	change_offset = sizeof(**tt_data);
	change_offset += num_vlan * sizeof(*tt_vlan);

	/* if tt_len is negative, allocate the space needed by the full table */
	if (*tt_len < 0)
		*tt_len = batadv_tt_len(num_entries);

	tvlv_len = *tt_len;
	tvlv_len += change_offset;

	*tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
	if (!*tt_data) {
		tvlv_len = 0;
		goto out;
	}

	(*tt_data)->flags = BATADV_NO_FLAGS;
	(*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
	(*tt_data)->num_vlan = htons(num_vlan);

	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
		tt_vlan->vid = htons(vlan->vid);
		tt_vlan->crc = htonl(vlan->tt.crc);

		tt_vlan++;
	}

858
	tt_change_ptr = (u8 *)*tt_data + change_offset;
859 860 861 862 863 864 865
	*tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;

out:
	rcu_read_unlock();
	return tvlv_len;
}

866 867 868 869 870 871
/**
 * batadv_tt_tvlv_container_update - update the translation table tvlv container
 *  after local tt changes have been committed
 * @bat_priv: the bat priv with all the soft interface information
 */
static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
872
{
873 874 875
	struct batadv_tt_change_node *entry, *safe;
	struct batadv_tvlv_tt_data *tt_data;
	struct batadv_tvlv_tt_change *tt_change;
876
	int tt_diff_len, tt_change_len = 0;
877 878
	int tt_diff_entries_num = 0;
	int tt_diff_entries_count = 0;
879
	u16 tvlv_len;
880

881 882
	tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
	tt_diff_len = batadv_tt_len(tt_diff_entries_num);
883 884 885 886

	/* if we have too many changes for one packet don't send any
	 * and wait for the tt table request which will be fragmented
	 */
887 888
	if (tt_diff_len > bat_priv->soft_iface->mtu)
		tt_diff_len = 0;
889

890 891 892
	tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
						     &tt_change, &tt_diff_len);
	if (!tvlv_len)
893
		return;
894

895
	tt_data->flags = BATADV_TT_OGM_DIFF;
896

897 898
	if (tt_diff_len == 0)
		goto container_register;
899

900 901
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	atomic_set(&bat_priv->tt.local_changes, 0);
902

903
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
904
				 list) {
905 906 907 908 909
		if (tt_diff_entries_count < tt_diff_entries_num) {
			memcpy(tt_change + tt_diff_entries_count,
			       &entry->change,
			       sizeof(struct batadv_tvlv_tt_change));
			tt_diff_entries_count++;
910
		}
911 912
		list_del(&entry->list);
		kfree(entry);
913
	}
914
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
915 916

	/* Keep the buffer for possible tt_request */
917 918 919 920
	spin_lock_bh(&bat_priv->tt.last_changeset_lock);
	kfree(bat_priv->tt.last_changeset);
	bat_priv->tt.last_changeset_len = 0;
	bat_priv->tt.last_changeset = NULL;
921
	tt_change_len = batadv_tt_len(tt_diff_entries_count);
922
	/* check whether this new OGM has no changes due to size problems */
923
	if (tt_diff_entries_count > 0) {
924
		/* if kmalloc() fails we will reply with the full table
925 926
		 * instead of providing the diff
		 */
927
		bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
928
		if (bat_priv->tt.last_changeset) {
929 930 931
			memcpy(bat_priv->tt.last_changeset,
			       tt_change, tt_change_len);
			bat_priv->tt.last_changeset_len = tt_diff_len;
932 933
		}
	}
934
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
935

936 937
container_register:
	batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
938
				       tvlv_len);
939
	kfree(tt_data);
940 941
}

942
int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
943 944
{
	struct net_device *net_dev = (struct net_device *)seq->private;
945
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
946
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
947
	struct batadv_tt_common_entry *tt_common_entry;
948
	struct batadv_tt_local_entry *tt_local;
949
	struct batadv_hard_iface *primary_if;
950
	struct batadv_softif_vlan *vlan;
951
	struct hlist_head *head;
952
	unsigned short vid;
953
	u32 i;
954 955 956 957
	int last_seen_secs;
	int last_seen_msecs;
	unsigned long last_seen_jiffies;
	bool no_purge;
958
	u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
959

960 961
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
962
		goto out;
963

964
	seq_printf(seq,
965
		   "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
966
		   net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
967
	seq_printf(seq, "       %-13s  %s %-8s %-9s (%-10s)\n", "Client", "VID",
968
		   "Flags", "Last seen", "CRC");
969 970 971 972

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

973
		rcu_read_lock();
974
		hlist_for_each_entry_rcu(tt_common_entry,
975
					 head, hash_entry) {
976 977 978
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
979
			vid = tt_common_entry->vid;
980 981 982 983 984 985 986
			last_seen_jiffies = jiffies - tt_local->last_seen;
			last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
			last_seen_secs = last_seen_msecs / 1000;
			last_seen_msecs = last_seen_msecs % 1000;

			no_purge = tt_common_entry->flags & np_flag;

987 988 989 990 991 992 993 994
			vlan = batadv_softif_vlan_get(bat_priv, vid);
			if (!vlan) {
				seq_printf(seq, "Cannot retrieve VLAN %d\n",
					   BATADV_PRINT_VID(vid));
				continue;
			}

			seq_printf(seq,
995
				   " * %pM %4i [%c%c%c%c%c%c] %3u.%03u   (%#.8x)\n",
996
				   tt_common_entry->addr,
997
				   BATADV_PRINT_VID(tt_common_entry->vid),
998 999
				   ((tt_common_entry->flags &
				     BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1000
				   no_purge ? 'P' : '.',
1001 1002 1003 1004 1005 1006 1007 1008
				   ((tt_common_entry->flags &
				     BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
				   ((tt_common_entry->flags &
				     BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
				   ((tt_common_entry->flags &
				     BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
				   ((tt_common_entry->flags &
				     BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1009
				   no_purge ? 0 : last_seen_secs,
1010 1011 1012 1013
				   no_purge ? 0 : last_seen_msecs,
				   vlan->tt.crc);

			batadv_softif_vlan_free_ref(vlan);
1014
		}
1015
		rcu_read_unlock();
1016
	}
1017 1018
out:
	if (primary_if)
1019
		batadv_hardif_free_ref(primary_if);
1020
	return 0;
1021 1022
}

1023 1024 1025
static void
batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
			    struct batadv_tt_local_entry *tt_local_entry,
1026
			    u16 flags, const char *message)
1027
{
1028
	batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1029

1030 1031
	/* The local client has to be marked as "pending to be removed" but has
	 * to be kept in the table in order to send it in a full table
1032 1033
	 * response issued before the net ttvn increment (consistency check)
	 */
1034
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1035

1036
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1037 1038 1039
		   "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
		   tt_local_entry->common.addr,
		   BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1040 1041
}

1042 1043 1044 1045
/**
 * batadv_tt_local_remove - logically remove an entry from the local table
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: the MAC address of the client to remove
1046
 * @vid: VLAN identifier
1047 1048 1049
 * @message: message to append to the log on deletion
 * @roaming: true if the deletion is due to a roaming event
 *
1050
 * Return: the flags assigned to the local entry before being deleted
1051
 */
1052 1053 1054
u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
			   unsigned short vid, const char *message,
			   bool roaming)
1055
{
1056
	struct batadv_tt_local_entry *tt_local_entry;
1057
	u16 flags, curr_flags = BATADV_NO_FLAGS;
1058
	struct batadv_softif_vlan *vlan;
1059
	void *tt_entry_exists;
1060

1061
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1062 1063 1064
	if (!tt_local_entry)
		goto out;

1065 1066
	curr_flags = tt_local_entry->common.flags;

1067
	flags = BATADV_TT_CLIENT_DEL;
1068 1069 1070 1071
	/* if this global entry addition is due to a roaming, the node has to
	 * mark the local entry as "roamed" in order to correctly reroute
	 * packets later
	 */
1072
	if (roaming) {
1073
		flags |= BATADV_TT_CLIENT_ROAM;
1074 1075 1076
		/* mark the local client as ROAMed */
		tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
	}
1077

1078 1079 1080 1081 1082 1083 1084 1085
	if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
		batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
					    message);
		goto out;
	}
	/* if this client has been added right now, it is possible to
	 * immediately purge it
	 */
1086
	batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1087 1088 1089 1090 1091 1092 1093 1094 1095

	tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
					     batadv_compare_tt,
					     batadv_choose_tt,
					     &tt_local_entry->common);
	if (!tt_entry_exists)
		goto out;

	/* extra call to free the local tt entry */
1096
	batadv_tt_local_entry_free_ref(tt_local_entry);
1097

1098 1099
	/* decrease the reference held for this vlan */
	vlan = batadv_softif_vlan_get(bat_priv, vid);
1100 1101 1102
	if (!vlan)
		goto out;

1103 1104 1105
	batadv_softif_vlan_free_ref(vlan);
	batadv_softif_vlan_free_ref(vlan);

1106 1107
out:
	if (tt_local_entry)
1108
		batadv_tt_local_entry_free_ref(tt_local_entry);
1109 1110

	return curr_flags;
1111 1112
}

1113 1114 1115 1116 1117 1118 1119
/**
 * batadv_tt_local_purge_list - purge inactive tt local entries
 * @bat_priv: the bat priv with all the soft interface information
 * @head: pointer to the list containing the local tt entries
 * @timeout: parameter deciding whether a given tt local entry is considered
 *  inactive or not
 */
1120
static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1121 1122
				       struct hlist_head *head,
				       int timeout)
1123
{
1124 1125
	struct batadv_tt_local_entry *tt_local_entry;
	struct batadv_tt_common_entry *tt_common_entry;
1126
	struct hlist_node *node_tmp;
1127

1128
	hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1129 1130
				  hash_entry) {
		tt_local_entry = container_of(tt_common_entry,
1131 1132
					      struct batadv_tt_local_entry,
					      common);
1133 1134 1135 1136 1137 1138 1139
		if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
			continue;

		/* entry already marked for deletion */
		if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
			continue;

1140
		if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1141 1142 1143 1144 1145 1146 1147
			continue;

		batadv_tt_local_set_pending(bat_priv, tt_local_entry,
					    BATADV_TT_CLIENT_DEL, "timed out");
	}
}

1148 1149 1150 1151 1152 1153 1154 1155
/**
 * batadv_tt_local_purge - purge inactive tt local entries
 * @bat_priv: the bat priv with all the soft interface information
 * @timeout: parameter deciding whether a given tt local entry is considered
 *  inactive or not
 */
static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
				  int timeout)
1156
{
1157
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1158
	struct hlist_head *head;
1159
	spinlock_t *list_lock; /* protects write access to the hash lists */
1160
	u32 i;
1161 1162 1163

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
1164
		list_lock = &hash->list_locks[i];
1165

1166
		spin_lock_bh(list_lock);
1167
		batadv_tt_local_purge_list(bat_priv, head, timeout);
1168
		spin_unlock_bh(list_lock);
1169 1170 1171
	}
}

1172
static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1173
{
1174
	struct batadv_hashtable *hash;
1175
	spinlock_t *list_lock; /* protects write access to the hash lists */
1176 1177
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local;
1178
	struct batadv_softif_vlan *vlan;
1179
	struct hlist_node *node_tmp;
1180
	struct hlist_head *head;
1181
	u32 i;
1182

1183
	if (!bat_priv->tt.local_hash)
1184 1185
		return;

1186
	hash = bat_priv->tt.local_hash;
1187 1188 1189 1190 1191 1192

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
		list_lock = &hash->list_locks[i];

		spin_lock_bh(list_lock);
1193
		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1194
					  head, hash_entry) {
1195
			hlist_del_rcu(&tt_common_entry->hash_entry);
1196 1197 1198
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
1199 1200 1201 1202

			/* decrease the reference held for this vlan */
			vlan = batadv_softif_vlan_get(bat_priv,
						      tt_common_entry->vid);
1203 1204 1205 1206
			if (vlan) {
				batadv_softif_vlan_free_ref(vlan);
				batadv_softif_vlan_free_ref(vlan);
			}
1207

1208
			batadv_tt_local_entry_free_ref(tt_local);
1209 1210 1211 1212
		}
		spin_unlock_bh(list_lock);
	}

1213
	batadv_hash_destroy(hash);
1214

1215
	bat_priv->tt.local_hash = NULL;
1216 1217
}

1218
static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1219
{
1220
	if (bat_priv->tt.global_hash)
1221
		return 0;
1222

1223
	bat_priv->tt.global_hash = batadv_hash_new(1024);
1224

1225
	if (!bat_priv->tt.global_hash)
1226
		return -ENOMEM;
1227

1228 1229 1230
	batadv_hash_set_lock_class(bat_priv->tt.global_hash,
				   &batadv_tt_global_hash_lock_class_key);

1231
	return 0;
1232 1233
}

1234
static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1235
{
1236
	struct batadv_tt_change_node *entry, *safe;
1237

1238
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
1239

1240
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1241 1242 1243 1244
				 list) {
		list_del(&entry->list);
		kfree(entry);
	}
1245

1246 1247
	atomic_set(&bat_priv->tt.local_changes, 0);
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1248
}
1249

1250
/**
1251 1252 1253 1254
 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
 * @entry: the TT global entry where the orig_list_entry has to be
 *  extracted from
 * @orig_node: the originator for which the orig_list_entry has to be found
1255
 *
1256
 * retrieve the orig_tt_list_entry belonging to orig_node from the
1257 1258
 * batadv_tt_global_entry list
 *
1259
 * Return: it with an increased refcounter, NULL if not found
1260
 */
1261 1262 1263
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)
1264
{
1265
	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1266 1267 1268 1269
	const struct hlist_head *head;

	rcu_read_lock();
	head = &entry->orig_list;
1270
	hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1271 1272 1273 1274 1275 1276 1277
		if (tmp_orig_entry->orig_node != orig_node)
			continue;
		if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
			continue;

		orig_entry = tmp_orig_entry;
		break;
1278 1279
	}
	rcu_read_unlock();
1280 1281 1282 1283

	return orig_entry;
}

1284
/**
1285 1286 1287 1288
 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
 *  by a given originator
 * @entry: the TT global entry to check
 * @orig_node: the originator to search in the list
1289 1290 1291 1292
 *
 * find out if an orig_node is already in the list of a tt_global_entry.
 *
 * Return: true if found, false otherwise
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
 */
static bool
batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
				const struct batadv_orig_node *orig_node)
{
	struct batadv_tt_orig_list_entry *orig_entry;
	bool found = false;

	orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
	if (orig_entry) {
		found = true;
		batadv_tt_orig_list_entry_free_ref(orig_entry);
	}

1307 1308 1309
	return found;
}

1310
static void
1311
batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1312
				struct batadv_orig_node *orig_node, int ttvn)
1313
{
1314
	struct batadv_tt_orig_list_entry *orig_entry;
1315

1316
	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1317 1318 1319 1320 1321
	if (orig_entry) {
		/* refresh the ttvn: the current value could be a bogus one that
		 * was added during a "temporary client detection"
		 */
		orig_entry->ttvn = ttvn;
1322
		goto out;
1323
	}
1324

1325 1326
	orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
	if (!orig_entry)
1327
		goto out;
1328 1329 1330

	INIT_HLIST_NODE(&orig_entry->list);
	atomic_inc(&orig_node->refcount);
1331
	batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1332 1333
	orig_entry->orig_node = orig_node;
	orig_entry->ttvn = ttvn;
1334
	atomic_set(&orig_entry->refcount, 2);
1335

1336
	spin_lock_bh(&tt_global->list_lock);
1337
	hlist_add_head_rcu(&orig_entry->list,
1338 1339
			   &tt_global->orig_list);
	spin_unlock_bh(&tt_global->list_lock);
1340 1341
	atomic_inc(&tt_global->orig_list_count);

1342 1343 1344
out:
	if (orig_entry)
		batadv_tt_orig_list_entry_free_ref(orig_entry);
1345 1346
}

1347 1348 1349 1350 1351
/**
 * batadv_tt_global_add - add a new TT global entry or update an existing one
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: the originator announcing the client
 * @tt_addr: the mac address of the non-mesh client
1352
 * @vid: VLAN identifier
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
 * @flags: TT flags that have to be set for this non-mesh client
 * @ttvn: the tt version number ever announcing this non-mesh client
 *
 * Add a new TT global entry for the given originator. If the entry already
 * exists add a new reference to the given originator (a global entry can have
 * references to multiple originators) and adjust the flags attribute to reflect
 * the function argument.
 * If a TT local entry exists for this non-mesh client remove it.
 *
 * The caller must hold orig_node refcount.
1363
 *
1364
 * Return: true if the new entry has been added, false otherwise
1365
 */
1366 1367
static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
1368
				 const unsigned char *tt_addr,
1369
				 unsigned short vid, u16 flags, u8 ttvn)
1370
{
1371 1372
	struct batadv_tt_global_entry *tt_global_entry;
	struct batadv_tt_local_entry *tt_local_entry;
1373
	bool ret = false;
1374
	int hash_added;
1375
	struct batadv_tt_common_entry *common;
1376
	u16 local_flags;
1377

1378 1379 1380 1381
	/* ignore global entries from backbone nodes */
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
		return true;

1382 1383
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1384 1385 1386 1387 1388 1389 1390 1391

	/* if the node already has a local client for this entry, it has to wait
	 * for a roaming advertisement instead of manually messing up the global
	 * table
	 */
	if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
	    !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
		goto out;
1392 1393

	if (!tt_global_entry) {
1394
		tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1395
		if (!tt_global_entry)
1396 1397
			goto out;

1398
		common = &tt_global_entry->common;
1399
		ether_addr_copy(common->addr, tt_addr);
1400
		common->vid = vid;
1401

1402
		common->flags = flags;
1403
		tt_global_entry->roam_at = 0;
1404 1405 1406 1407 1408 1409
		/* node must store current time in case of roaming. This is
		 * needed to purge this entry out on timeout (if nobody claims
		 * it)
		 */
		if (flags & BATADV_TT_CLIENT_ROAM)
			tt_global_entry->roam_at = jiffies;
1410
		atomic_set(&common->refcount, 2);
1411
		common->added_at = jiffies;
1412 1413

		INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1414
		atomic_set(&tt_global_entry->orig_list_count, 0);
1415
		spin_lock_init(&tt_global_entry->list_lock);
1416

1417
		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1418
					     batadv_compare_tt,
1419
					     batadv_choose_tt, common,
1420
					     &common->hash_entry);
1421 1422 1423

		if (unlikely(hash_added != 0)) {
			/* remove the reference for the hash */
1424
			batadv_tt_global_entry_free_ref(tt_global_entry);
1425 1426
			goto out_remove;
		}
1427
	} else {
1428
		common = &tt_global_entry->common;
1429 1430
		/* If there is already a global entry, we can use this one for
		 * our processing.
1431 1432 1433 1434 1435 1436 1437
		 * But if we are trying to add a temporary client then here are
		 * two options at this point:
		 * 1) the global client is not a temporary client: the global
		 *    client has to be left as it is, temporary information
		 *    should never override any already known client state
		 * 2) the global client is a temporary client: purge the
		 *    originator list and add the new one orig_entry
1438
		 */
1439 1440 1441 1442 1443 1444 1445 1446 1447
		if (flags & BATADV_TT_CLIENT_TEMP) {
			if (!(common->flags & BATADV_TT_CLIENT_TEMP))
				goto out;
			if (batadv_tt_global_entry_has_orig(tt_global_entry,
							    orig_node))
				goto out_remove;
			batadv_tt_global_del_orig_list(tt_global_entry);
			goto add_orig_entry;
		}
1448 1449

		/* if the client was temporary added before receiving the first
1450 1451 1452 1453
		 * OGM announcing it, we have to clear the TEMP flag. Also,
		 * remove the previous temporary orig node and re-add it
		 * if required. If the orig entry changed, the new one which
		 * is a non-temporary entry is preferred.
1454
		 */
1455 1456 1457 1458
		if (common->flags & BATADV_TT_CLIENT_TEMP) {
			batadv_tt_global_del_orig_list(tt_global_entry);
			common->flags &= ~BATADV_TT_CLIENT_TEMP;
		}
1459

1460 1461 1462 1463
		/* the change can carry possible "attribute" flags like the
		 * TT_CLIENT_WIFI, therefore they have to be copied in the
		 * client entry
		 */
1464
		common->flags |= flags;
1465

1466 1467
		/* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
		 * one originator left in the list and we previously received a
1468 1469 1470 1471 1472
		 * delete + roaming change for this originator.
		 *
		 * We should first delete the old originator before adding the
		 * new one.
		 */
1473
		if (common->flags & BATADV_TT_CLIENT_ROAM) {
1474
			batadv_tt_global_del_orig_list(tt_global_entry);
1475
			common->flags &= ~BATADV_TT_CLIENT_ROAM;
1476
			tt_global_entry->roam_at = 0;
1477 1478
		}
	}
1479
add_orig_entry:
1480
	/* add the new orig_entry (if needed) or update it */
1481
	batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1482

1483
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1484 1485 1486
		   "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
		   common->addr, BATADV_PRINT_VID(common->vid),
		   orig_node->orig);
1487
	ret = true;
1488

1489
out_remove:
1490 1491 1492 1493 1494
	/* Do not remove multicast addresses from the local hash on
	 * global additions
	 */
	if (is_multicast_ether_addr(tt_addr))
		goto out;
1495

1496
	/* remove address from local hash if present */
1497
	local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1498
					     "global tt received",
1499
					     flags & BATADV_TT_CLIENT_ROAM);
1500 1501
	tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;

1502 1503 1504 1505 1506 1507
	if (!(flags & BATADV_TT_CLIENT_ROAM))
		/* this is a normal global add. Therefore the client is not in a
		 * roaming state anymore.
		 */
		tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;

1508 1509
out:
	if (tt_global_entry)
1510
		batadv_tt_global_entry_free_ref(tt_global_entry);
1511 1512
	if (tt_local_entry)
		batadv_tt_local_entry_free_ref(tt_local_entry);
1513
	return ret;
1514 1515
}

1516 1517
/**
 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1518
 * @bat_priv: the bat priv with all the soft interface information
1519 1520 1521
 * @tt_global_entry: global translation table entry to be analyzed
 *
 * This functon assumes the caller holds rcu_read_lock().
1522
 * Return: best originator list entry or NULL on errors.
1523 1524
 */
static struct batadv_tt_orig_list_entry *
1525 1526
batadv_transtable_best_orig(struct batadv_priv *bat_priv,
			    struct batadv_tt_global_entry *tt_global_entry)
1527
{
1528 1529
	struct batadv_neigh_node *router, *best_router = NULL;
	struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1530 1531 1532 1533
	struct hlist_head *head;
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;

	head = &tt_global_entry->orig_list;
1534
	hlist_for_each_entry_rcu(orig_entry, head, list) {
1535 1536
		router = batadv_orig_router_get(orig_entry->orig_node,
						BATADV_IF_DEFAULT);
1537 1538 1539
		if (!router)
			continue;

1540
		if (best_router &&
1541 1542
		    bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
				       best_router, BATADV_IF_DEFAULT) <= 0) {
1543 1544
			batadv_neigh_node_free_ref(router);
			continue;
1545 1546
		}

1547 1548 1549 1550 1551 1552
		/* release the refcount for the "old" best */
		if (best_router)
			batadv_neigh_node_free_ref(best_router);

		best_entry = orig_entry;
		best_router = router;
1553 1554
	}

1555 1556 1557
	if (best_router)
		batadv_neigh_node_free_ref(best_router);

1558 1559 1560
	return best_entry;
}

1561 1562 1563
/**
 * batadv_tt_global_print_entry - print all orig nodes who announce the address
 *  for this global entry
1564
 * @bat_priv: the bat priv with all the soft interface information
1565 1566 1567 1568
 * @tt_global_entry: global translation table entry to be printed
 * @seq: debugfs table seq_file struct
 *
 * This functon assumes the caller holds rcu_read_lock().
1569
 */
1570
static void
1571 1572
batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
			     struct batadv_tt_global_entry *tt_global_entry,
1573
			     struct seq_file *seq)
1574
{
1575
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1576
	struct batadv_tt_common_entry *tt_common_entry;
1577 1578
	struct batadv_orig_node_vlan *vlan;
	struct hlist_head *head;
1579 1580
	u8 last_ttvn;
	u16 flags;
1581 1582

	tt_common_entry = &tt_global_entry->common;
1583 1584
	flags = tt_common_entry->flags;

1585
	best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1586
	if (best_entry) {
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
		vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
						 tt_common_entry->vid);
		if (!vlan) {
			seq_printf(seq,
				   " * Cannot retrieve VLAN %d for originator %pM\n",
				   BATADV_PRINT_VID(tt_common_entry->vid),
				   best_entry->orig_node->orig);
			goto print_list;
		}

1597
		last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1598
		seq_printf(seq,
1599
			   " %c %pM %4i   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1600
			   '*', tt_global_entry->common.addr,
1601
			   BATADV_PRINT_VID(tt_global_entry->common.vid),
1602
			   best_entry->ttvn, best_entry->orig_node->orig,
1603
			   last_ttvn, vlan->tt.crc,
1604 1605 1606 1607
			   ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
			   ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
			   ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
			   ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1608 1609

		batadv_orig_node_vlan_free_ref(vlan);
1610
	}
1611

1612
print_list:
1613 1614
	head = &tt_global_entry->orig_list;

1615
	hlist_for_each_entry_rcu(orig_entry, head, list) {
1616 1617 1618
		if (best_entry == orig_entry)
			continue;

1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
		vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
						 tt_common_entry->vid);
		if (!vlan) {
			seq_printf(seq,
				   " + Cannot retrieve VLAN %d for originator %pM\n",
				   BATADV_PRINT_VID(tt_common_entry->vid),
				   orig_entry->orig_node->orig);
			continue;
		}

1629
		last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1630
		seq_printf(seq,
1631
			   " %c %pM %4d   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1632
			   '+', tt_global_entry->common.addr,
1633
			   BATADV_PRINT_VID(tt_global_entry->common.vid),
1634
			   orig_entry->ttvn, orig_entry->orig_node->orig,
1635
			   last_ttvn, vlan->tt.crc,
1636 1637 1638 1639
			   ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
			   ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
			   ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
			   ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1640 1641

		batadv_orig_node_vlan_free_ref(vlan);
1642 1643 1644
	}
}

1645
int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1646 1647
{
	struct net_device *net_dev = (struct net_device *)seq->private;
1648
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1649
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1650 1651 1652
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
	struct batadv_hard_iface *primary_if;
1653
	struct hlist_head *head;
1654
	u32 i;
1655

1656 1657
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1658
		goto out;
1659

1660 1661
	seq_printf(seq,
		   "Globally announced TT entries received via the mesh %s\n",
1662
		   net_dev->name);
1663 1664 1665
	seq_printf(seq, "       %-13s  %s  %s       %-15s %s (%-10s) %s\n",
		   "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
		   "CRC", "Flags");
1666 1667 1668 1669

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

1670
		rcu_read_lock();
1671
		hlist_for_each_entry_rcu(tt_common_entry,
1672
					 head, hash_entry) {
1673 1674 1675
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
1676
			batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1677
		}
1678
		rcu_read_unlock();
1679
	}
1680 1681
out:
	if (primary_if)
1682
		batadv_hardif_free_ref(primary_if);
1683
	return 0;
1684 1685
}

1686
/**
1687
 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1688 1689 1690 1691 1692
 * @tt_global_entry: the global entry to remove the orig_entry from
 * @orig_entry: the orig entry to remove and free
 *
 * Remove an orig_entry from its list in the given tt_global_entry and
 * free this orig_entry afterwards.
1693 1694 1695
 *
 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
 * part of a list.
1696 1697
 */
static void
1698 1699
_batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
				 struct batadv_tt_orig_list_entry *orig_entry)
1700
{
1701 1702
	lockdep_assert_held(&tt_global_entry->list_lock);

1703 1704 1705
	batadv_tt_global_size_dec(orig_entry->orig_node,
				  tt_global_entry->common.vid);
	atomic_dec(&tt_global_entry->orig_list_count);
1706 1707 1708
	/* requires holding tt_global_entry->list_lock and orig_entry->list
	 * being part of a list
	 */
1709 1710 1711 1712
	hlist_del_rcu(&orig_entry->list);
	batadv_tt_orig_list_entry_free_ref(orig_entry);
}

1713
/* deletes the orig list of a tt_global_entry */
1714
static void
1715
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1716
{
1717
	struct hlist_head *head;
1718
	struct hlist_node *safe;
1719
	struct batadv_tt_orig_list_entry *orig_entry;
1720

1721 1722
	spin_lock_bh(&tt_global_entry->list_lock);
	head = &tt_global_entry->orig_list;
1723
	hlist_for_each_entry_safe(orig_entry, safe, head, list)
1724
		_batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1725 1726 1727
	spin_unlock_bh(&tt_global_entry->list_lock);
}

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
/**
 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_global_entry: the global entry to remove the orig_node from
 * @orig_node: the originator announcing the client
 * @message: message to append to the log on deletion
 *
 * Remove the given orig_node and its according orig_entry from the given
 * global tt entry.
 */
1738
static void
1739 1740 1741 1742
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)
1743 1744
{
	struct hlist_head *head;
1745
	struct hlist_node *safe;
1746
	struct batadv_tt_orig_list_entry *orig_entry;
1747
	unsigned short vid;
1748 1749 1750

	spin_lock_bh(&tt_global_entry->list_lock);
	head = &tt_global_entry->orig_list;
1751
	hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1752
		if (orig_entry->orig_node == orig_node) {
1753
			vid = tt_global_entry->common.vid;
1754
			batadv_dbg(BATADV_DBG_TT, bat_priv,
1755
				   "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1756
				   orig_node->orig,
1757 1758
				   tt_global_entry->common.addr,
				   BATADV_PRINT_VID(vid), message);
1759 1760
			_batadv_tt_global_del_orig_entry(tt_global_entry,
							 orig_entry);
1761 1762 1763 1764 1765 1766
		}
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
}

/* If the client is to be deleted, we check if it is the last origantor entry
1767 1768
 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
 * timer, otherwise we simply remove the originator scheduled for deletion.
1769
 */
1770
static void
1771 1772 1773 1774
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)
1775 1776 1777
{
	bool last_entry = true;
	struct hlist_head *head;
1778
	struct batadv_tt_orig_list_entry *orig_entry;
1779 1780 1781 1782 1783 1784 1785

	/* no local entry exists, case 1:
	 * Check if this is the last one or if other entries exist.
	 */

	rcu_read_lock();
	head = &tt_global_entry->orig_list;
1786
	hlist_for_each_entry_rcu(orig_entry, head, list) {
1787 1788 1789 1790 1791 1792 1793 1794 1795
		if (orig_entry->orig_node != orig_node) {
			last_entry = false;
			break;
		}
	}
	rcu_read_unlock();

	if (last_entry) {
		/* its the last one, mark for roaming. */
1796
		tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1797 1798 1799 1800 1801
		tt_global_entry->roam_at = jiffies;
	} else
		/* there is another entry, we can simply delete this
		 * one and can still use the other one.
		 */
1802 1803
		batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
					       orig_node, message);
1804 1805
}

1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
/**
 * batadv_tt_global_del - remove a client from the global table
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: an originator serving this client
 * @addr: the mac address of the client
 * @vid: VLAN identifier
 * @message: a message explaining the reason for deleting the client to print
 *  for debugging purpose
 * @roaming: true if the deletion has been triggered by a roaming event
 */
1816 1817
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
1818
				 const unsigned char *addr, unsigned short vid,
1819
				 const char *message, bool roaming)
1820
{
1821
	struct batadv_tt_global_entry *tt_global_entry;
1822
	struct batadv_tt_local_entry *local_entry = NULL;
1823

1824
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1825
	if (!tt_global_entry)
1826
		goto out;
1827

1828
	if (!roaming) {
1829 1830
		batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
					       orig_node, message);
1831 1832

		if (hlist_empty(&tt_global_entry->orig_list))
1833 1834
			batadv_tt_global_free(bat_priv, tt_global_entry,
					      message);
1835 1836 1837

		goto out;
	}
1838 1839 1840

	/* if we are deleting a global entry due to a roam
	 * event, there are two possibilities:
1841 1842
	 * 1) the client roamed from node A to node B => if there
	 *    is only one originator left for this client, we mark
1843
	 *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1844 1845
	 *    wait for node B to claim it. In case of timeout
	 *    the entry is purged.
1846 1847 1848
	 *
	 *    If there are other originators left, we directly delete
	 *    the originator.
1849
	 * 2) the client roamed to us => we can directly delete
1850 1851
	 *    the global entry, since it is useless now.
	 */
1852
	local_entry = batadv_tt_local_hash_find(bat_priv,
1853 1854
						tt_global_entry->common.addr,
						vid);
1855
	if (local_entry) {
1856
		/* local entry exists, case 2: client roamed to us. */
1857
		batadv_tt_global_del_orig_list(tt_global_entry);
1858
		batadv_tt_global_free(bat_priv, tt_global_entry, message);
1859 1860
	} else
		/* no local entry exists, case 1: check for roaming */
1861 1862
		batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
					     orig_node, message);
1863

1864
out:
1865
	if (tt_global_entry)
1866 1867 1868
		batadv_tt_global_entry_free_ref(tt_global_entry);
	if (local_entry)
		batadv_tt_local_entry_free_ref(local_entry);
1869 1870
}

1871 1872 1873 1874 1875 1876 1877 1878 1879
/**
 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
 *  given originator matching the provided vid
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: the originator owning the entries to remove
 * @match_vid: the VLAN identifier to match. If negative all the entries will be
 *  removed
 * @message: debug message to print as "reason"
 */
1880 1881
void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
			       struct batadv_orig_node *orig_node,
1882
			       s32 match_vid,
1883
			       const char *message)
1884
{
1885 1886
	struct batadv_tt_global_entry *tt_global;
	struct batadv_tt_common_entry *tt_common_entry;
1887
	u32 i;
1888
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1889
	struct hlist_node *safe;
1890
	struct hlist_head *head;
1891
	spinlock_t *list_lock; /* protects write access to the hash lists */
1892
	unsigned short vid;
1893

1894 1895 1896
	if (!hash)
		return;

1897 1898
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
1899
		list_lock = &hash->list_locks[i];
1900

1901
		spin_lock_bh(list_lock);
1902
		hlist_for_each_entry_safe(tt_common_entry, safe,
1903
					  head, hash_entry) {
1904 1905 1906 1907
			/* remove only matching entries */
			if (match_vid >= 0 && tt_common_entry->vid != match_vid)
				continue;

1908 1909 1910
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
1911

1912 1913
			batadv_tt_global_del_orig_node(bat_priv, tt_global,
						       orig_node, message);
1914

1915
			if (hlist_empty(&tt_global->orig_list)) {
1916
				vid = tt_global->common.vid;
1917
				batadv_dbg(BATADV_DBG_TT, bat_priv,
1918 1919 1920
					   "Deleting global tt entry %pM (vid: %d): %s\n",
					   tt_global->common.addr,
					   BATADV_PRINT_VID(vid), message);
1921
				hlist_del_rcu(&tt_common_entry->hash_entry);
1922
				batadv_tt_global_entry_free_ref(tt_global);
1923
			}
1924
		}
1925
		spin_unlock_bh(list_lock);
1926
	}
1927
	clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1928 1929
}

1930 1931
static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
				      char **msg)
1932
{
1933 1934 1935
	bool purge = false;
	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1936

1937 1938 1939 1940 1941
	if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
	    batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
		purge = true;
		*msg = "Roaming timeout\n";
	}
1942

1943 1944 1945 1946
	if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
	    batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
		purge = true;
		*msg = "Temporary client timeout\n";
1947
	}
1948 1949

	return purge;
1950 1951
}

1952
static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1953
{
1954
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1955
	struct hlist_head *head;
1956
	struct hlist_node *node_tmp;
1957
	spinlock_t *list_lock; /* protects write access to the hash lists */
1958
	u32 i;
1959 1960 1961
	char *msg = NULL;
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
1962 1963 1964

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
1965
		list_lock = &hash->list_locks[i];
1966

1967
		spin_lock_bh(list_lock);
1968
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
1969 1970 1971 1972 1973 1974 1975 1976 1977
					  hash_entry) {
			tt_global = container_of(tt_common,
						 struct batadv_tt_global_entry,
						 common);

			if (!batadv_tt_global_to_purge(tt_global, &msg))
				continue;

			batadv_dbg(BATADV_DBG_TT, bat_priv,
1978 1979 1980 1981
				   "Deleting global tt entry %pM (vid: %d): %s\n",
				   tt_global->common.addr,
				   BATADV_PRINT_VID(tt_global->common.vid),
				   msg);
1982

1983
			hlist_del_rcu(&tt_common->hash_entry);
1984 1985 1986

			batadv_tt_global_entry_free_ref(tt_global);
		}
1987
		spin_unlock_bh(list_lock);
1988 1989 1990
	}
}

1991
static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1992
{
1993
	struct batadv_hashtable *hash;
1994
	spinlock_t *list_lock; /* protects write access to the hash lists */
1995 1996
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
1997
	struct hlist_node *node_tmp;
1998
	struct hlist_head *head;
1999
	u32 i;
2000

2001
	if (!bat_priv->tt.global_hash)
2002 2003
		return;

2004
	hash = bat_priv->tt.global_hash;
2005 2006 2007 2008 2009 2010

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
		list_lock = &hash->list_locks[i];

		spin_lock_bh(list_lock);
2011
		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2012
					  head, hash_entry) {
2013
			hlist_del_rcu(&tt_common_entry->hash_entry);
2014 2015 2016 2017
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_entry_free_ref(tt_global);
2018 2019 2020 2021
		}
		spin_unlock_bh(list_lock);
	}

2022
	batadv_hash_destroy(hash);
2023

2024
	bat_priv->tt.global_hash = NULL;
2025 2026
}

2027 2028 2029
static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
		       struct batadv_tt_global_entry *tt_global_entry)
2030 2031 2032
{
	bool ret = false;

2033 2034
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2035 2036
		ret = true;

2037 2038 2039 2040 2041
	/* check if the two clients are marked as isolated */
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
	    tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
		ret = true;

2042 2043 2044
	return ret;
}

2045 2046 2047 2048 2049 2050 2051
/**
 * batadv_transtable_search - get the mesh destination for a given client
 * @bat_priv: the bat priv with all the soft interface information
 * @src: mac address of the source client
 * @addr: mac address of the destination client
 * @vid: VLAN identifier
 *
2052
 * Return: a pointer to the originator that was selected as destination in the
2053 2054 2055 2056 2057 2058
 * mesh for contacting the client 'addr', NULL otherwise.
 * In case of multiple originators serving the same client, the function returns
 * the best one (best in terms of metric towards the destination node).
 *
 * If the two clients are AP isolated the function returns NULL.
 */
2059
struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2060 2061
						  const u8 *src,
						  const u8 *addr,
2062
						  unsigned short vid)
2063
{
2064 2065 2066
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
	struct batadv_orig_node *orig_node = NULL;
2067
	struct batadv_tt_orig_list_entry *best_entry;
2068

2069
	if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2070
		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2071 2072
		if (!tt_local_entry ||
		    (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2073 2074
			goto out;
	}
2075

2076
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2077
	if (!tt_global_entry)
2078
		goto out;
2079

2080
	/* check whether the clients should not communicate due to AP
2081 2082
	 * isolation
	 */
2083 2084
	if (tt_local_entry &&
	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2085 2086
		goto out;

2087
	rcu_read_lock();
2088
	best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2089
	/* found anything? */
2090 2091
	if (best_entry)
		orig_node = best_entry->orig_node;
2092 2093 2094
	if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
		orig_node = NULL;
	rcu_read_unlock();
2095

2096
out:
2097
	if (tt_global_entry)
2098
		batadv_tt_global_entry_free_ref(tt_global_entry);
2099
	if (tt_local_entry)
2100
		batadv_tt_local_entry_free_ref(tt_local_entry);
2101

2102
	return orig_node;
2103
}
2104

2105 2106 2107 2108
/**
 * batadv_tt_global_crc - calculates the checksum of the local table belonging
 *  to the given orig_node
 * @bat_priv: the bat priv with all the soft interface information
2109
 * @orig_node: originator for which the CRC should be computed
2110
 * @vid: VLAN identifier for which the CRC32 has to be computed
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
 *
 * This function computes the checksum for the global table corresponding to a
 * specific originator. In particular, the checksum is computed as follows: For
 * each client connected to the originator the CRC32C of the MAC address and the
 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
 * together.
 *
 * The idea behind is that CRC32C should be used as much as possible in order to
 * produce a unique hash of the table, but since the order which is used to feed
 * the CRC32C function affects the result and since every node in the network
 * probably sorts the clients differently, the hash function cannot be directly
 * computed over the entire table. Hence the CRC32C is used only on
 * the single client entry, while all the results are then xor'ed together
 * because the XOR operation can combine them all while trying to reduce the
 * noise as much as possible.
 *
2127
 * Return: the checksum of the global table of a given originator.
2128
 */
2129 2130 2131
static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
				struct batadv_orig_node *orig_node,
				unsigned short vid)
2132
{
2133
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2134 2135
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
2136
	struct hlist_head *head;
2137 2138
	u32 i, crc_tmp, crc = 0;
	u8 flags;
2139
	__be16 tmp_vid;
2140 2141 2142 2143 2144

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
2145
		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2146 2147 2148
			tt_global = container_of(tt_common,
						 struct batadv_tt_global_entry,
						 common);
2149 2150 2151 2152 2153 2154
			/* compute the CRC only for entries belonging to the
			 * VLAN identified by the vid passed as parameter
			 */
			if (tt_common->vid != vid)
				continue;

2155 2156 2157 2158 2159
			/* Roaming clients are in the global table for
			 * consistency only. They don't have to be
			 * taken into account while computing the
			 * global crc
			 */
2160
			if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2161
				continue;
2162 2163 2164 2165 2166 2167
			/* Temporary clients have not been announced yet, so
			 * they have to be skipped while computing the global
			 * crc
			 */
			if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
				continue;
2168 2169 2170 2171

			/* find out if this global entry is announced by this
			 * originator
			 */
2172
			if (!batadv_tt_global_entry_has_orig(tt_global,
2173
							     orig_node))
2174 2175
				continue;

2176 2177 2178 2179 2180
			/* use network order to read the VID: this ensures that
			 * every node reads the bytes in the same order.
			 */
			tmp_vid = htons(tt_common->vid);
			crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2181 2182 2183 2184 2185 2186 2187

			/* compute the CRC on flags that have to be kept in sync
			 * among nodes
			 */
			flags = tt_common->flags & BATADV_TT_SYNC_MASK;
			crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));

2188
			crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2189 2190 2191 2192
		}
		rcu_read_unlock();
	}

2193
	return crc;
2194 2195
}

2196 2197 2198
/**
 * batadv_tt_local_crc - calculates the checksum of the local table
 * @bat_priv: the bat priv with all the soft interface information
2199
 * @vid: VLAN identifier for which the CRC32 has to be computed
2200 2201 2202 2203
 *
 * For details about the computation, please refer to the documentation for
 * batadv_tt_global_crc().
 *
2204
 * Return: the checksum of the local table
2205
 */
2206 2207
static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
			       unsigned short vid)
2208
{
2209
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2210
	struct batadv_tt_common_entry *tt_common;
2211
	struct hlist_head *head;
2212 2213
	u32 i, crc_tmp, crc = 0;
	u8 flags;
2214
	__be16 tmp_vid;
2215 2216 2217 2218 2219

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
2220
		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2221 2222 2223 2224 2225 2226
			/* compute the CRC only for entries belonging to the
			 * VLAN identified by vid
			 */
			if (tt_common->vid != vid)
				continue;

2227
			/* not yet committed clients have not to be taken into
2228 2229
			 * account while computing the CRC
			 */
2230
			if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2231
				continue;
2232

2233 2234 2235 2236 2237
			/* use network order to read the VID: this ensures that
			 * every node reads the bytes in the same order.
			 */
			tmp_vid = htons(tt_common->vid);
			crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2238 2239 2240 2241 2242 2243 2244

			/* compute the CRC on flags that have to be kept in sync
			 * among nodes
			 */
			flags = tt_common->flags & BATADV_TT_SYNC_MASK;
			crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));

2245
			crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2246 2247 2248 2249
		}
		rcu_read_unlock();
	}

2250
	return crc;
2251 2252
}

2253
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2254
{
2255 2256
	struct batadv_tt_req_node *node;
	struct hlist_node *safe;
2257

2258
	spin_lock_bh(&bat_priv->tt.req_list_lock);
2259

2260 2261
	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
		hlist_del_init(&node->list);
2262 2263 2264
		kfree(node);
	}

2265
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2266 2267
}

2268 2269
static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
				       struct batadv_orig_node *orig_node,
2270
				       const void *tt_buff,
2271
				       u16 tt_buff_len)
2272 2273
{
	/* Replace the old buffer only if I received something in the
2274 2275
	 * last OGM (the OGM could carry no changes)
	 */
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
	spin_lock_bh(&orig_node->tt_buff_lock);
	if (tt_buff_len > 0) {
		kfree(orig_node->tt_buff);
		orig_node->tt_buff_len = 0;
		orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
		if (orig_node->tt_buff) {
			memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
			orig_node->tt_buff_len = tt_buff_len;
		}
	}
	spin_unlock_bh(&orig_node->tt_buff_lock);
}

2289
static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2290
{
2291 2292
	struct batadv_tt_req_node *node;
	struct hlist_node *safe;
2293

2294
	spin_lock_bh(&bat_priv->tt.req_list_lock);
2295
	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2296 2297
		if (batadv_has_timed_out(node->issued_at,
					 BATADV_TT_REQUEST_TIMEOUT)) {
2298
			hlist_del_init(&node->list);
2299 2300 2301
			kfree(node);
		}
	}
2302
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2303 2304
}

2305 2306 2307 2308 2309
/**
 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: orig node this request is being issued for
 *
2310
 * Return: the pointer to the new tt_req_node struct if no request
2311
 * has already been issued for this orig_node, NULL otherwise.
2312
 */
2313
static struct batadv_tt_req_node *
2314
batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2315
		       struct batadv_orig_node *orig_node)
2316
{
2317
	struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2318

2319
	spin_lock_bh(&bat_priv->tt.req_list_lock);
2320
	hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2321 2322
		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2323
					  BATADV_TT_REQUEST_TIMEOUT))
2324 2325 2326 2327 2328 2329 2330
			goto unlock;
	}

	tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
	if (!tt_req_node)
		goto unlock;

2331
	ether_addr_copy(tt_req_node->addr, orig_node->orig);
2332 2333
	tt_req_node->issued_at = jiffies;

2334
	hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2335
unlock:
2336
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2337 2338 2339
	return tt_req_node;
}

2340 2341 2342 2343 2344
/**
 * batadv_tt_local_valid - verify that given tt entry is a valid one
 * @entry_ptr: to be checked local tt entry
 * @data_ptr: not used but definition required to satisfy the callback prototype
 *
2345
 * Return: 1 if the entry is a valid, 0 otherwise.
2346 2347
 */
static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2348
{
2349
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2350

2351
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2352 2353 2354 2355
		return 0;
	return 1;
}

2356 2357
static int batadv_tt_global_valid(const void *entry_ptr,
				  const void *data_ptr)
2358
{
2359 2360 2361
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
	const struct batadv_tt_global_entry *tt_global_entry;
	const struct batadv_orig_node *orig_node = data_ptr;
2362

2363 2364
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2365 2366
		return 0;

2367 2368
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
2369 2370
				       common);

2371
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2372 2373
}

2374
/**
2375 2376
 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
 *  specified tt hash
2377 2378 2379
 * @bat_priv: the bat priv with all the soft interface information
 * @hash: hash table containing the tt entries
 * @tt_len: expected tvlv tt data buffer length in number of bytes
2380
 * @tvlv_buff: pointer to the buffer to fill with the TT data
2381 2382 2383
 * @valid_cb: function to filter tt change entries
 * @cb_data: data passed to the filter function as argument
 */
2384 2385
static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
				    struct batadv_hashtable *hash,
2386
				    void *tvlv_buff, u16 tt_len,
2387 2388
				    int (*valid_cb)(const void *, const void *),
				    void *cb_data)
2389
{
2390
	struct batadv_tt_common_entry *tt_common_entry;
2391
	struct batadv_tvlv_tt_change *tt_change;
2392
	struct hlist_head *head;
2393 2394
	u16 tt_tot, tt_num_entries = 0;
	u32 i;
2395

2396
	tt_tot = batadv_tt_entries(tt_len);
2397
	tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2398 2399 2400 2401 2402

	rcu_read_lock();
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

2403
		hlist_for_each_entry_rcu(tt_common_entry,
2404
					 head, hash_entry) {
2405
			if (tt_tot == tt_num_entries)
2406 2407
				break;

2408
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2409 2410
				continue;

2411
			ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2412
			tt_change->flags = tt_common_entry->flags;
2413
			tt_change->vid = htons(tt_common_entry->vid);
2414 2415
			memset(tt_change->reserved, 0,
			       sizeof(tt_change->reserved));
2416

2417
			tt_num_entries++;
2418 2419 2420 2421
			tt_change++;
		}
	}
	rcu_read_unlock();
2422
}
2423

2424 2425 2426 2427 2428 2429
/**
 * batadv_tt_global_check_crc - check if all the CRCs are correct
 * @orig_node: originator for which the CRCs have to be checked
 * @tt_vlan: pointer to the first tvlv VLAN entry
 * @num_vlan: number of tvlv VLAN entries
 *
2430
 * Return: true if all the received CRCs match the locally stored ones, false
2431 2432 2433 2434
 * otherwise
 */
static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
				       struct batadv_tvlv_tt_vlan_data *tt_vlan,
2435
				       u16 num_vlan)
2436 2437 2438
{
	struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
	struct batadv_orig_node_vlan *vlan;
2439
	int i, orig_num_vlan;
2440
	u32 crc;
2441 2442 2443 2444 2445 2446 2447 2448 2449

	/* check if each received CRC matches the locally stored one */
	for (i = 0; i < num_vlan; i++) {
		tt_vlan_tmp = tt_vlan + i;

		/* if orig_node is a backbone node for this VLAN, don't check
		 * the CRC as we ignore all the global entries over it
		 */
		if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2450 2451
						   orig_node->orig,
						   ntohs(tt_vlan_tmp->vid)))
2452 2453 2454 2455 2456 2457 2458
			continue;

		vlan = batadv_orig_node_vlan_get(orig_node,
						 ntohs(tt_vlan_tmp->vid));
		if (!vlan)
			return false;

2459 2460 2461 2462
		crc = vlan->tt.crc;
		batadv_orig_node_vlan_free_ref(vlan);

		if (crc != ntohl(tt_vlan_tmp->crc))
2463 2464 2465
			return false;
	}

2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
	/* check if any excess VLANs exist locally for the originator
	 * which are not mentioned in the TVLV from the originator.
	 */
	rcu_read_lock();
	orig_num_vlan = 0;
	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
		orig_num_vlan++;
	rcu_read_unlock();

	if (orig_num_vlan > num_vlan)
		return false;

2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
	return true;
}

/**
 * batadv_tt_local_update_crc - update all the local CRCs
 * @bat_priv: the bat priv with all the soft interface information
 */
static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
{
	struct batadv_softif_vlan *vlan;

	/* recompute the global CRC for each VLAN */
	rcu_read_lock();
	hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
		vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
	}
	rcu_read_unlock();
}

/**
 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: the orig_node for which the CRCs have to be updated
 */
static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
					struct batadv_orig_node *orig_node)
{
	struct batadv_orig_node_vlan *vlan;
2506
	u32 crc;
2507 2508 2509

	/* recompute the global CRC for each VLAN */
	rcu_read_lock();
2510
	hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2511 2512 2513
		/* if orig_node is a backbone node for this VLAN, don't compute
		 * the CRC as we ignore all the global entries over it
		 */
2514 2515
		if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
						   vlan->vid))
2516 2517 2518 2519 2520 2521
			continue;

		crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
		vlan->tt.crc = crc;
	}
	rcu_read_unlock();
2522 2523
}

2524 2525 2526 2527 2528
/**
 * batadv_send_tt_request - send a TT Request message to a given node
 * @bat_priv: the bat priv with all the soft interface information
 * @dst_orig_node: the destination of the message
 * @ttvn: the version number that the source of the message is looking for
2529 2530
 * @tt_vlan: pointer to the first tvlv VLAN object to request
 * @num_vlan: number of tvlv VLAN entries
2531 2532
 * @full_table: ask for the entire translation table if true, while only for the
 *  last TT diff otherwise
2533 2534
 *
 * Return: true if the TT Request was sent, false otherwise
2535
 */
2536 2537
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *dst_orig_node,
2538
				  u8 ttvn,
2539
				  struct batadv_tvlv_tt_vlan_data *tt_vlan,
2540
				  u16 num_vlan, bool full_table)
2541
{
2542
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2543
	struct batadv_tt_req_node *tt_req_node = NULL;
2544 2545
	struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
	struct batadv_hard_iface *primary_if;
2546
	bool ret = false;
2547
	int i, size;
2548

2549
	primary_if = batadv_primary_if_get_selected(bat_priv);
2550 2551 2552 2553
	if (!primary_if)
		goto out;

	/* The new tt_req will be issued only if I'm not waiting for a
2554 2555
	 * reply from the same orig_node yet
	 */
2556
	tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2557 2558 2559
	if (!tt_req_node)
		goto out;

2560 2561
	size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
	tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2562
	if (!tvlv_tt_data)
2563 2564
		goto out;

2565 2566
	tvlv_tt_data->flags = BATADV_TT_REQUEST;
	tvlv_tt_data->ttvn = ttvn;
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
	tvlv_tt_data->num_vlan = htons(num_vlan);

	/* send all the CRCs within the request. This is needed by intermediate
	 * nodes to ensure they have the correct table before replying
	 */
	tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
	for (i = 0; i < num_vlan; i++) {
		tt_vlan_req->vid = tt_vlan->vid;
		tt_vlan_req->crc = tt_vlan->crc;

		tt_vlan_req++;
		tt_vlan++;
	}
2580 2581

	if (full_table)
2582
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2583

2584
	batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2585
		   dst_orig_node->orig, full_table ? 'F' : '.');
2586

2587
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2588 2589
	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
				 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2590
				 tvlv_tt_data, size);
2591
	ret = true;
2592 2593 2594

out:
	if (primary_if)
2595
		batadv_hardif_free_ref(primary_if);
2596
	if (ret && tt_req_node) {
2597
		spin_lock_bh(&bat_priv->tt.req_list_lock);
2598 2599
		/* hlist_del_init() verifies tt_req_node still is in the list */
		hlist_del_init(&tt_req_node->list);
2600
		spin_unlock_bh(&bat_priv->tt.req_list_lock);
2601 2602
		kfree(tt_req_node);
	}
2603
	kfree(tvlv_tt_data);
2604 2605 2606
	return ret;
}

2607 2608 2609 2610 2611 2612 2613 2614
/**
 * batadv_send_other_tt_response - send reply to tt request concerning another
 *  node's translation table
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @req_src: mac address of tt request sender
 * @req_dst: mac address of tt request recipient
 *
2615
 * Return: true if tt request reply was sent, false otherwise.
2616 2617 2618
 */
static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
					  struct batadv_tvlv_tt_data *tt_data,
2619
					  u8 *req_src, u8 *req_dst)
2620
{
2621
	struct batadv_orig_node *req_dst_orig_node;
2622
	struct batadv_orig_node *res_dst_orig_node = NULL;
2623
	struct batadv_tvlv_tt_change *tt_change;
2624
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2625
	struct batadv_tvlv_tt_vlan_data *tt_vlan;
2626
	bool ret = false, full_table;
2627 2628 2629
	u8 orig_ttvn, req_ttvn;
	u16 tvlv_len;
	s32 tt_len;
2630

2631
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2632
		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2633
		   req_src, tt_data->ttvn, req_dst,
2634
		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2635 2636

	/* Let's get the orig node of the REAL destination */
2637
	req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2638 2639 2640
	if (!req_dst_orig_node)
		goto out;

2641
	res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2642 2643 2644
	if (!res_dst_orig_node)
		goto out;

2645
	orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2646
	req_ttvn = tt_data->ttvn;
2647

2648
	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2649
	/* this node doesn't have the requested data */
2650
	if (orig_ttvn != req_ttvn ||
2651 2652
	    !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
					ntohs(tt_data->num_vlan)))
2653 2654
		goto out;

2655
	/* If the full table has been explicitly requested */
2656
	if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2657 2658 2659 2660 2661
	    !req_dst_orig_node->tt_buff)
		full_table = true;
	else
		full_table = false;

2662 2663
	/* TT fragmentation hasn't been implemented yet, so send as many
	 * TT entries fit a single packet as possible only
2664
	 */
2665 2666 2667 2668
	if (!full_table) {
		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
		tt_len = req_dst_orig_node->tt_buff_len;

2669 2670 2671 2672 2673
		tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
							      &tvlv_tt_data,
							      &tt_change,
							      &tt_len);
		if (!tt_len)
2674 2675 2676
			goto unlock;

		/* Copy the last orig_node's OGM buffer */
2677
		memcpy(tt_change, req_dst_orig_node->tt_buff,
2678 2679 2680
		       req_dst_orig_node->tt_buff_len);
		spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
	} else {
2681 2682 2683 2684 2685 2686 2687 2688 2689
		/* allocate the tvlv, put the tt_data and all the tt_vlan_data
		 * in the initial part
		 */
		tt_len = -1;
		tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
							      &tvlv_tt_data,
							      &tt_change,
							      &tt_len);
		if (!tt_len)
2690
			goto out;
2691 2692 2693 2694 2695 2696

		/* fill the rest of the tvlv with the real TT entries */
		batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
					tt_change, tt_len,
					batadv_tt_global_valid,
					req_dst_orig_node);
2697 2698
	}

2699 2700 2701 2702 2703 2704 2705 2706 2707
	/* Don't send the response, if larger than fragmented packet. */
	tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
	if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
		net_ratelimited_function(batadv_info, bat_priv->soft_iface,
					 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
					 res_dst_orig_node->orig);
		goto out;
	}

2708 2709
	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
	tvlv_tt_data->ttvn = req_ttvn;
2710 2711

	if (full_table)
2712
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2713

2714
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2715 2716 2717
		   "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
		   res_dst_orig_node->orig, req_dst_orig_node->orig,
		   full_table ? 'F' : '.', req_ttvn);
2718

2719
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2720

2721
	batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2722 2723
				 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
				 tvlv_len);
2724

2725
	ret = true;
2726 2727 2728 2729 2730 2731 2732
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
2733
		batadv_orig_node_free_ref(res_dst_orig_node);
2734
	if (req_dst_orig_node)
2735
		batadv_orig_node_free_ref(req_dst_orig_node);
2736
	kfree(tvlv_tt_data);
2737 2738
	return ret;
}
2739

2740 2741 2742 2743 2744 2745 2746
/**
 * batadv_send_my_tt_response - send reply to tt request concerning this node's
 *  translation table
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @req_src: mac address of tt request sender
 *
2747
 * Return: true if tt request reply was sent, false otherwise.
2748 2749 2750
 */
static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
				       struct batadv_tvlv_tt_data *tt_data,
2751
				       u8 *req_src)
2752
{
2753
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2754
	struct batadv_hard_iface *primary_if = NULL;
2755 2756
	struct batadv_tvlv_tt_change *tt_change;
	struct batadv_orig_node *orig_node;
2757 2758
	u8 my_ttvn, req_ttvn;
	u16 tvlv_len;
2759
	bool full_table;
2760
	s32 tt_len;
2761

2762
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2763
		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2764
		   req_src, tt_data->ttvn,
2765
		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2766

2767
	spin_lock_bh(&bat_priv->tt.commit_lock);
2768

2769
	my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2770
	req_ttvn = tt_data->ttvn;
2771

2772
	orig_node = batadv_orig_hash_find(bat_priv, req_src);
2773 2774 2775
	if (!orig_node)
		goto out;

2776
	primary_if = batadv_primary_if_get_selected(bat_priv);
2777 2778 2779 2780
	if (!primary_if)
		goto out;

	/* If the full table has been explicitly requested or the gap
2781 2782
	 * is too big send the whole local translation table
	 */
2783
	if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2784
	    !bat_priv->tt.last_changeset)
2785 2786 2787 2788
		full_table = true;
	else
		full_table = false;

2789 2790
	/* TT fragmentation hasn't been implemented yet, so send as many
	 * TT entries fit a single packet as possible only
2791
	 */
2792
	if (!full_table) {
2793
		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2794

2795 2796 2797 2798 2799 2800
		tt_len = bat_priv->tt.last_changeset_len;
		tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
							     &tvlv_tt_data,
							     &tt_change,
							     &tt_len);
		if (!tt_len)
2801 2802
			goto unlock;

2803
		/* Copy the last orig_node's OGM buffer */
2804
		memcpy(tt_change, bat_priv->tt.last_changeset,
2805 2806
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2807
	} else {
2808
		req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2809

2810 2811 2812 2813 2814 2815 2816 2817 2818
		/* allocate the tvlv, put the tt_data and all the tt_vlan_data
		 * in the initial part
		 */
		tt_len = -1;
		tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
							     &tvlv_tt_data,
							     &tt_change,
							     &tt_len);
		if (!tt_len)
2819
			goto out;
2820 2821 2822 2823 2824

		/* fill the rest of the tvlv with the real TT entries */
		batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
					tt_change, tt_len,
					batadv_tt_local_valid, NULL);
2825 2826
	}

2827 2828
	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
	tvlv_tt_data->ttvn = req_ttvn;
2829 2830

	if (full_table)
2831
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2832

2833
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2834 2835
		   "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
		   orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2836

2837
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2838

2839
	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2840 2841
				 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
				 tvlv_len);
2842

2843 2844 2845
	goto out;

unlock:
2846
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2847
out:
2848
	spin_unlock_bh(&bat_priv->tt.commit_lock);
2849
	if (orig_node)
2850
		batadv_orig_node_free_ref(orig_node);
2851
	if (primary_if)
2852
		batadv_hardif_free_ref(primary_if);
2853 2854
	kfree(tvlv_tt_data);
	/* The packet was for this host, so it doesn't need to be re-routed */
2855 2856 2857
	return true;
}

2858 2859 2860 2861 2862 2863 2864
/**
 * batadv_send_tt_response - send reply to tt request
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @req_src: mac address of tt request sender
 * @req_dst: mac address of tt request recipient
 *
2865
 * Return: true if tt request reply was sent, false otherwise.
2866 2867 2868
 */
static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
				    struct batadv_tvlv_tt_data *tt_data,
2869
				    u8 *req_src, u8 *req_dst)
2870
{
2871
	if (batadv_is_my_mac(bat_priv, req_dst))
2872
		return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2873 2874
	return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
					     req_dst);
2875 2876
}

2877 2878
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
2879
				      struct batadv_tvlv_tt_change *tt_change,
2880
				      u16 tt_num_changes, u8 ttvn)
2881 2882
{
	int i;
2883
	int roams;
2884 2885

	for (i = 0; i < tt_num_changes; i++) {
2886 2887
		if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
			roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2888 2889
			batadv_tt_global_del(bat_priv, orig_node,
					     (tt_change + i)->addr,
2890
					     ntohs((tt_change + i)->vid),
2891 2892
					     "tt removed by changes",
					     roams);
2893 2894
		} else {
			if (!batadv_tt_global_add(bat_priv, orig_node,
2895
						  (tt_change + i)->addr,
2896
						  ntohs((tt_change + i)->vid),
2897
						  (tt_change + i)->flags, ttvn))
2898 2899 2900 2901 2902 2903 2904
				/* In case of problem while storing a
				 * global_entry, we stop the updating
				 * procedure without committing the
				 * ttvn change. This will avoid to send
				 * corrupted data on tt_request
				 */
				return;
2905
		}
2906
	}
2907
	set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2908 2909
}

2910
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2911
				  struct batadv_tvlv_tt_change *tt_change,
2912 2913
				  u8 ttvn, u8 *resp_src,
				  u16 num_entries)
2914
{
2915
	struct batadv_orig_node *orig_node;
2916

2917
	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2918 2919 2920 2921
	if (!orig_node)
		goto out;

	/* Purge the old table first.. */
2922 2923
	batadv_tt_global_del_orig(bat_priv, orig_node, -1,
				  "Received full table");
2924

2925 2926
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
				  ttvn);
2927 2928 2929 2930 2931 2932 2933

	spin_lock_bh(&orig_node->tt_buff_lock);
	kfree(orig_node->tt_buff);
	orig_node->tt_buff_len = 0;
	orig_node->tt_buff = NULL;
	spin_unlock_bh(&orig_node->tt_buff_lock);

2934
	atomic_set(&orig_node->last_ttvn, ttvn);
2935 2936 2937

out:
	if (orig_node)
2938
		batadv_orig_node_free_ref(orig_node);
2939 2940
}

2941 2942
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
2943
				     u16 tt_num_changes, u8 ttvn,
2944
				     struct batadv_tvlv_tt_change *tt_change)
2945
{
2946 2947
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
2948

2949 2950
	batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
				   batadv_tt_len(tt_num_changes));
2951 2952 2953
	atomic_set(&orig_node->last_ttvn, ttvn);
}

2954 2955 2956
/**
 * batadv_is_my_client - check if a client is served by the local node
 * @bat_priv: the bat priv with all the soft interface information
2957
 * @addr: the mac address of the client to check
2958 2959
 * @vid: VLAN identifier
 *
2960
 * Return: true if the client is served by this node, false otherwise.
2961
 */
2962
bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
2963
			 unsigned short vid)
2964
{
2965
	struct batadv_tt_local_entry *tt_local_entry;
2966
	bool ret = false;
2967

2968
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2969 2970
	if (!tt_local_entry)
		goto out;
2971
	/* Check if the client has been logically deleted (but is kept for
2972 2973
	 * consistency purpose)
	 */
2974 2975
	if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
	    (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2976
		goto out;
2977 2978
	ret = true;
out:
2979
	if (tt_local_entry)
2980
		batadv_tt_local_entry_free_ref(tt_local_entry);
2981
	return ret;
2982 2983
}

2984 2985 2986 2987 2988 2989 2990 2991 2992
/**
 * batadv_handle_tt_response - process incoming tt reply
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @resp_src: mac address of tt reply sender
 * @num_entries: number of tt change entries appended to the tt data
 */
static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
				      struct batadv_tvlv_tt_data *tt_data,
2993
				      u8 *resp_src, u16 num_entries)
2994
{
2995 2996
	struct batadv_tt_req_node *node;
	struct hlist_node *safe;
2997
	struct batadv_orig_node *orig_node = NULL;
2998
	struct batadv_tvlv_tt_change *tt_change;
2999 3000
	u8 *tvlv_ptr = (u8 *)tt_data;
	u16 change_offset;
3001

3002
	batadv_dbg(BATADV_DBG_TT, bat_priv,
3003
		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3004
		   resp_src, tt_data->ttvn, num_entries,
3005
		   ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3006

3007
	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3008 3009 3010
	if (!orig_node)
		goto out;

3011 3012
	spin_lock_bh(&orig_node->tt_lock);

3013 3014 3015 3016 3017 3018
	change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
	change_offset *= ntohs(tt_data->num_vlan);
	change_offset += sizeof(*tt_data);
	tvlv_ptr += change_offset;

	tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3019
	if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3020 3021
		batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
				      resp_src, num_entries);
3022
	} else {
3023 3024
		batadv_tt_update_changes(bat_priv, orig_node, num_entries,
					 tt_data->ttvn, tt_change);
3025
	}
3026

3027
	/* Recalculate the CRC for this orig_node and store it */
3028
	batadv_tt_global_update_crc(bat_priv, orig_node);
3029 3030 3031

	spin_unlock_bh(&orig_node->tt_lock);

3032
	/* Delete the tt_req_node from pending tt_requests list */
3033
	spin_lock_bh(&bat_priv->tt.req_list_lock);
3034
	hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3035
		if (!batadv_compare_eth(node->addr, resp_src))
3036
			continue;
3037
		hlist_del_init(&node->list);
3038 3039
		kfree(node);
	}
3040

3041
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
3042 3043
out:
	if (orig_node)
3044
		batadv_orig_node_free_ref(orig_node);
3045 3046
}

3047
static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3048
{
3049
	struct batadv_tt_roam_node *node, *safe;
3050

3051
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3052

3053
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3054 3055 3056 3057
		list_del(&node->list);
		kfree(node);
	}

3058
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3059 3060
}

3061
static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3062
{
3063
	struct batadv_tt_roam_node *node, *safe;
3064

3065 3066
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3067 3068
		if (!batadv_has_timed_out(node->first_time,
					  BATADV_ROAMING_MAX_TIME))
3069 3070 3071 3072 3073
			continue;

		list_del(&node->list);
		kfree(node);
	}
3074
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3075 3076
}

3077
/**
3078 3079 3080
 * batadv_tt_check_roam_count - check if a client has roamed too frequently
 * @bat_priv: the bat priv with all the soft interface information
 * @client: mac address of the roaming client
3081 3082
 *
 * This function checks whether the client already reached the
3083 3084 3085
 * maximum number of possible roaming phases. In this case the ROAMING_ADV
 * will not be sent.
 *
3086
 * Return: true if the ROAMING_ADV can be sent, false otherwise
3087
 */
3088
static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3089
{
3090
	struct batadv_tt_roam_node *tt_roam_node;
3091 3092
	bool ret = false;

3093
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
3094
	/* The new tt_req will be issued only if I'm not waiting for a
3095 3096
	 * reply from the same orig_node yet
	 */
3097
	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3098
		if (!batadv_compare_eth(tt_roam_node->addr, client))
3099 3100
			continue;

3101
		if (batadv_has_timed_out(tt_roam_node->first_time,
3102
					 BATADV_ROAMING_MAX_TIME))
3103 3104
			continue;

3105
		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117
			/* Sorry, you roamed too many times! */
			goto unlock;
		ret = true;
		break;
	}

	if (!ret) {
		tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
		if (!tt_roam_node)
			goto unlock;

		tt_roam_node->first_time = jiffies;
3118 3119
		atomic_set(&tt_roam_node->counter,
			   BATADV_ROAMING_MAX_COUNT - 1);
3120
		ether_addr_copy(tt_roam_node->addr, client);
3121

3122
		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3123 3124 3125 3126
		ret = true;
	}

unlock:
3127
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3128 3129 3130
	return ret;
}

3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
/**
 * batadv_send_roam_adv - send a roaming advertisement message
 * @bat_priv: the bat priv with all the soft interface information
 * @client: mac address of the roaming client
 * @vid: VLAN identifier
 * @orig_node: message destination
 *
 * Send a ROAMING_ADV message to the node which was previously serving this
 * client. This is done to inform the node that from now on all traffic destined
 * for this particular roamed client has to be forwarded to the sender of the
 * roaming message.
 */
3143
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3144
				 unsigned short vid,
3145
				 struct batadv_orig_node *orig_node)
3146
{
3147
	struct batadv_hard_iface *primary_if;
3148 3149 3150 3151 3152
	struct batadv_tvlv_roam_adv tvlv_roam;

	primary_if = batadv_primary_if_get_selected(bat_priv);
	if (!primary_if)
		goto out;
3153 3154

	/* before going on we have to check whether the client has
3155 3156
	 * already roamed to us too many times
	 */
3157
	if (!batadv_tt_check_roam_count(bat_priv, client))
3158 3159
		goto out;

3160
	batadv_dbg(BATADV_DBG_TT, bat_priv,
3161 3162
		   "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
		   orig_node->orig, client, BATADV_PRINT_VID(vid));
3163

3164
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3165

3166
	memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3167
	tvlv_roam.vid = htons(vid);
3168 3169 3170 3171

	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
				 orig_node->orig, BATADV_TVLV_ROAM, 1,
				 &tvlv_roam, sizeof(tvlv_roam));
3172 3173

out:
3174 3175
	if (primary_if)
		batadv_hardif_free_ref(primary_if);
3176 3177
}

3178
static void batadv_tt_purge(struct work_struct *work)
3179
{
3180
	struct delayed_work *delayed_work;
3181
	struct batadv_priv_tt *priv_tt;
3182 3183 3184
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
3185 3186
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3187

3188
	batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3189
	batadv_tt_global_purge(bat_priv);
3190 3191
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
3192

3193 3194
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3195
}
3196

3197
void batadv_tt_free(struct batadv_priv *bat_priv)
3198
{
3199 3200 3201
	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);

3202
	cancel_delayed_work_sync(&bat_priv->tt.work);
3203

3204 3205 3206 3207 3208
	batadv_tt_local_table_free(bat_priv);
	batadv_tt_global_table_free(bat_priv);
	batadv_tt_req_list_free(bat_priv);
	batadv_tt_changes_list_free(bat_priv);
	batadv_tt_roam_list_free(bat_priv);
3209

3210
	kfree(bat_priv->tt.last_changeset);
3211
}
3212

3213 3214 3215 3216 3217 3218 3219
/**
 * batadv_tt_local_set_flags - set or unset the specified flags on the local
 *  table and possibly count them in the TT size
 * @bat_priv: the bat priv with all the soft interface information
 * @flags: the flag to switch
 * @enable: whether to set or unset the flag
 * @count: whether to increase the TT size by the number of changed entries
3220
 */
3221 3222
static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
				      bool enable, bool count)
3223
{
3224 3225
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
	struct batadv_tt_common_entry *tt_common_entry;
3226
	u16 changed_num = 0;
3227
	struct hlist_head *head;
3228
	u32 i;
3229 3230

	if (!hash)
3231
		return;
3232 3233 3234 3235 3236

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
3237
		hlist_for_each_entry_rcu(tt_common_entry,
3238
					 head, hash_entry) {
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
			if (enable) {
				if ((tt_common_entry->flags & flags) == flags)
					continue;
				tt_common_entry->flags |= flags;
			} else {
				if (!(tt_common_entry->flags & flags))
					continue;
				tt_common_entry->flags &= ~flags;
			}
			changed_num++;
3249 3250 3251 3252 3253 3254

			if (!count)
				continue;

			batadv_tt_local_size_inc(bat_priv,
						 tt_common_entry->vid);
3255 3256 3257 3258 3259
		}
		rcu_read_unlock();
	}
}

3260
/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3261
static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3262
{
3263
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3264 3265
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_local_entry *tt_local;
3266
	struct batadv_softif_vlan *vlan;
3267
	struct hlist_node *node_tmp;
3268 3269
	struct hlist_head *head;
	spinlock_t *list_lock; /* protects write access to the hash lists */
3270
	u32 i;
3271 3272 3273 3274 3275 3276 3277 3278 3279

	if (!hash)
		return;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
		list_lock = &hash->list_locks[i];

		spin_lock_bh(list_lock);
3280
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
3281 3282
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3283 3284
				continue;

3285
			batadv_dbg(BATADV_DBG_TT, bat_priv,
3286 3287 3288
				   "Deleting local tt entry (%pM, vid: %d): pending\n",
				   tt_common->addr,
				   BATADV_PRINT_VID(tt_common->vid));
3289

3290
			batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3291
			hlist_del_rcu(&tt_common->hash_entry);
3292 3293 3294
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
3295 3296 3297

			/* decrease the reference held for this vlan */
			vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3298 3299 3300 3301
			if (vlan) {
				batadv_softif_vlan_free_ref(vlan);
				batadv_softif_vlan_free_ref(vlan);
			}
3302

3303
			batadv_tt_local_entry_free_ref(tt_local);
3304 3305 3306 3307 3308
		}
		spin_unlock_bh(list_lock);
	}
}

3309
/**
3310 3311
 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
 *  which have been queued in the time since the last commit
3312
 * @bat_priv: the bat priv with all the soft interface information
3313 3314
 *
 * Caller must hold tt->commit_lock.
3315
 */
3316
static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3317
{
3318 3319
	lockdep_assert_held(&bat_priv->tt.commit_lock);

3320 3321 3322
	/* Update multicast addresses in local translation table */
	batadv_mcast_mla_update(bat_priv);

3323 3324 3325
	if (atomic_read(&bat_priv->tt.local_changes) < 1) {
		if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
			batadv_tt_tvlv_container_update(bat_priv);
3326
		return;
3327
	}
3328

3329
	batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3330

3331
	batadv_tt_local_purge_pending_clients(bat_priv);
3332
	batadv_tt_local_update_crc(bat_priv);
3333 3334

	/* Increment the TTVN only once per OGM interval */
3335
	atomic_inc(&bat_priv->tt.vn);
3336
	batadv_dbg(BATADV_DBG_TT, bat_priv,
3337
		   "Local changes committed, updating to ttvn %u\n",
3338
		   (u8)atomic_read(&bat_priv->tt.vn));
3339 3340

	/* reset the sending counter */
3341
	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3342
	batadv_tt_tvlv_container_update(bat_priv);
3343
}
3344

3345 3346 3347 3348 3349 3350 3351 3352 3353
/**
 * batadv_tt_local_commit_changes - commit all pending local tt changes which
 *  have been queued in the time since the last commit
 * @bat_priv: the bat priv with all the soft interface information
 */
void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
{
	spin_lock_bh(&bat_priv->tt.commit_lock);
	batadv_tt_local_commit_changes_nolock(bat_priv);
3354
	spin_unlock_bh(&bat_priv->tt.commit_lock);
3355
}
3356

3357 3358
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
			   unsigned short vid)
3359
{
3360 3361
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
3362
	struct batadv_softif_vlan *vlan;
3363
	bool ret = false;
3364

3365
	vlan = batadv_softif_vlan_get(bat_priv, vid);
3366 3367 3368 3369
	if (!vlan)
		return false;

	if (!atomic_read(&vlan->ap_isolation))
3370
		goto out;
3371

3372
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3373 3374 3375
	if (!tt_local_entry)
		goto out;

3376
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3377 3378 3379
	if (!tt_global_entry)
		goto out;

3380
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3381 3382
		goto out;

3383
	ret = true;
3384 3385

out:
3386
	batadv_softif_vlan_free_ref(vlan);
3387
	if (tt_global_entry)
3388
		batadv_tt_global_entry_free_ref(tt_global_entry);
3389
	if (tt_local_entry)
3390
		batadv_tt_local_entry_free_ref(tt_local_entry);
3391 3392
	return ret;
}
3393

3394 3395 3396 3397
/**
 * batadv_tt_update_orig - update global translation table with new tt
 *  information received via ogms
 * @bat_priv: the bat priv with all the soft interface information
3398 3399
 * @orig_node: the orig_node of the ogm
 * @tt_buff: pointer to the first tvlv VLAN entry
3400 3401
 * @tt_num_vlan: number of tvlv VLAN entries
 * @tt_change: pointer to the first entry in the TT buffer
3402 3403 3404 3405 3406
 * @tt_num_changes: number of tt changes inside the tt buffer
 * @ttvn: translation table version number of this changeset
 */
static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *orig_node,
3407
				  const void *tt_buff, u16 tt_num_vlan,
3408
				  struct batadv_tvlv_tt_change *tt_change,
3409
				  u16 tt_num_changes, u8 ttvn)
3410
{
3411
	u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3412
	struct batadv_tvlv_tt_vlan_data *tt_vlan;
3413
	bool full_table = true;
3414
	bool has_tt_init;
3415

3416
	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3417 3418
	has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
			       &orig_node->capa_initialized);
3419

3420
	/* orig table not initialised AND first diff is in the OGM OR the ttvn
3421 3422
	 * increased by one -> we can apply the attached changes
	 */
3423
	if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3424
		/* the OGM could not contain the changes due to their size or
3425 3426
		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
		 * times.
3427 3428
		 * In this case send a tt request
		 */
3429 3430 3431 3432 3433
		if (!tt_num_changes) {
			full_table = false;
			goto request_table;
		}

3434 3435
		spin_lock_bh(&orig_node->tt_lock);

3436
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3437
					 ttvn, tt_change);
3438 3439 3440

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
3441 3442
		 * in the global table
		 */
3443
		batadv_tt_global_update_crc(bat_priv, orig_node);
3444

3445 3446
		spin_unlock_bh(&orig_node->tt_lock);

3447 3448 3449 3450 3451 3452 3453
		/* The ttvn alone is not enough to guarantee consistency
		 * because a single value could represent different states
		 * (due to the wrap around). Thus a node has to check whether
		 * the resulting table (after applying the changes) is still
		 * consistent or not. E.g. a node could disconnect while its
		 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
		 * checking the CRC value is mandatory to detect the
3454 3455
		 * inconsistency
		 */
3456 3457
		if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
						tt_num_vlan))
3458 3459 3460
			goto request_table;
	} else {
		/* if we missed more than one change or our tables are not
3461 3462
		 * in sync anymore -> request fresh tt data
		 */
3463
		if (!has_tt_init || ttvn != orig_ttvn ||
3464 3465
		    !batadv_tt_global_check_crc(orig_node, tt_vlan,
						tt_num_vlan)) {
3466
request_table:
3467
			batadv_dbg(BATADV_DBG_TT, bat_priv,
3468 3469 3470
				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
				   orig_node->orig, ttvn, orig_ttvn,
				   tt_num_changes);
3471
			batadv_send_tt_request(bat_priv, orig_node, ttvn,
3472 3473
					       tt_vlan, tt_num_vlan,
					       full_table);
3474 3475 3476 3477
			return;
		}
	}
}
3478

3479 3480 3481 3482 3483 3484
/**
 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: the mac address of the client to check
 * @vid: VLAN identifier
 *
3485
 * Return: true if we know that the client has moved from its old originator
3486 3487
 * to another one. This entry is still kept for consistency purposes and will be
 * deleted later by a DEL or because of timeout
3488
 */
3489
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3490
					u8 *addr, unsigned short vid)
3491
{
3492
	struct batadv_tt_global_entry *tt_global_entry;
3493 3494
	bool ret = false;

3495
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3496 3497 3498
	if (!tt_global_entry)
		goto out;

3499
	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3500
	batadv_tt_global_entry_free_ref(tt_global_entry);
3501 3502 3503
out:
	return ret;
}
3504

3505 3506 3507
/**
 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
 * @bat_priv: the bat priv with all the soft interface information
3508 3509
 * @addr: the mac address of the local client to query
 * @vid: VLAN identifier
3510
 *
3511
 * Return: true if the local client is known to be roaming (it is not served by
3512 3513 3514 3515
 * this node anymore) or not. If yes, the client is still present in the table
 * to keep the latter consistent with the node TTVN
 */
bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3516
				       u8 *addr, unsigned short vid)
3517 3518 3519 3520
{
	struct batadv_tt_local_entry *tt_local_entry;
	bool ret = false;

3521
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3522 3523 3524 3525 3526 3527 3528 3529 3530
	if (!tt_local_entry)
		goto out;

	ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
	batadv_tt_local_entry_free_ref(tt_local_entry);
out:
	return ret;
}

3531 3532
bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
					  struct batadv_orig_node *orig_node,
3533
					  const unsigned char *addr,
3534
					  unsigned short vid)
3535 3536 3537
{
	bool ret = false;

3538
	if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3539 3540 3541 3542 3543
				  BATADV_TT_CLIENT_TEMP,
				  atomic_read(&orig_node->last_ttvn)))
		goto out;

	batadv_dbg(BATADV_DBG_TT, bat_priv,
3544 3545
		   "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
		   addr, BATADV_PRINT_VID(vid), orig_node->orig);
3546 3547 3548 3549
	ret = true;
out:
	return ret;
}
3550

3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
/**
 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
 *  maximum packet size that can be transported through the mesh
 * @soft_iface: netdev struct of the mesh interface
 *
 * Remove entries older than 'timeout' and half timeout if more entries need
 * to be removed.
 */
void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
{
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
	int packet_size_max = atomic_read(&bat_priv->packet_size_max);
	int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
	bool reduced = false;

	spin_lock_bh(&bat_priv->tt.commit_lock);

	while (true) {
		table_size = batadv_tt_local_table_transmit_size(bat_priv);
		if (packet_size_max >= table_size)
			break;

		batadv_tt_local_purge(bat_priv, timeout);
		batadv_tt_local_purge_pending_clients(bat_priv);

		timeout /= 2;
		reduced = true;
		net_ratelimited_function(batadv_info, soft_iface,
					 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
					 packet_size_max);
	}

	/* commit these changes immediately, to avoid synchronization problem
	 * with the TTVN
	 */
	if (reduced)
		batadv_tt_local_commit_changes_nolock(bat_priv);

	spin_unlock_bh(&bat_priv->tt.commit_lock);
}

3592 3593 3594 3595 3596 3597 3598 3599 3600 3601
/**
 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
 * @bat_priv: the bat priv with all the soft interface information
 * @orig: the orig_node of the ogm
 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
 * @tvlv_value: tvlv buffer containing the gateway data
 * @tvlv_value_len: tvlv buffer length
 */
static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
					  struct batadv_orig_node *orig,
3602 3603
					  u8 flags, void *tvlv_value,
					  u16 tvlv_value_len)
3604
{
3605 3606
	struct batadv_tvlv_tt_vlan_data *tt_vlan;
	struct batadv_tvlv_tt_change *tt_change;
3607
	struct batadv_tvlv_tt_data *tt_data;
3608
	u16 num_entries, num_vlan;
3609 3610 3611 3612 3613 3614 3615

	if (tvlv_value_len < sizeof(*tt_data))
		return;

	tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
	tvlv_value_len -= sizeof(*tt_data);

3616 3617 3618 3619 3620 3621 3622 3623 3624
	num_vlan = ntohs(tt_data->num_vlan);

	if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
		return;

	tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
	tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
	tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;

3625
	num_entries = batadv_tt_entries(tvlv_value_len);
3626

3627 3628
	batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
			      num_entries, tt_data->ttvn);
3629 3630
}

3631 3632 3633 3634 3635 3636 3637 3638 3639
/**
 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
 *  container
 * @bat_priv: the bat priv with all the soft interface information
 * @src: mac address of tt tvlv sender
 * @dst: mac address of tt tvlv recipient
 * @tvlv_value: tvlv buffer containing the tt data
 * @tvlv_value_len: tvlv buffer length
 *
3640
 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3641 3642 3643
 * otherwise.
 */
static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3644
					     u8 *src, u8 *dst,
3645
					     void *tvlv_value,
3646
					     u16 tvlv_value_len)
3647 3648
{
	struct batadv_tvlv_tt_data *tt_data;
3649
	u16 tt_vlan_len, tt_num_entries;
3650 3651 3652 3653 3654 3655 3656 3657 3658
	char tt_flag;
	bool ret;

	if (tvlv_value_len < sizeof(*tt_data))
		return NET_RX_SUCCESS;

	tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
	tvlv_value_len -= sizeof(*tt_data);

3659 3660 3661 3662 3663 3664 3665 3666
	tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
	tt_vlan_len *= ntohs(tt_data->num_vlan);

	if (tvlv_value_len < tt_vlan_len)
		return NET_RX_SUCCESS;

	tvlv_value_len -= tt_vlan_len;
	tt_num_entries = batadv_tt_entries(tvlv_value_len);
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693

	switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
	case BATADV_TT_REQUEST:
		batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);

		/* If this node cannot provide a TT response the tt_request is
		 * forwarded
		 */
		ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
		if (!ret) {
			if (tt_data->flags & BATADV_TT_FULL_TABLE)
				tt_flag = 'F';
			else
				tt_flag = '.';

			batadv_dbg(BATADV_DBG_TT, bat_priv,
				   "Routing TT_REQUEST to %pM [%c]\n",
				   dst, tt_flag);
			/* tvlv API will re-route the packet */
			return NET_RX_DROP;
		}
		break;
	case BATADV_TT_RESPONSE:
		batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);

		if (batadv_is_my_mac(bat_priv, dst)) {
			batadv_handle_tt_response(bat_priv, tt_data,
3694
						  src, tt_num_entries);
3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
			return NET_RX_SUCCESS;
		}

		if (tt_data->flags & BATADV_TT_FULL_TABLE)
			tt_flag =  'F';
		else
			tt_flag = '.';

		batadv_dbg(BATADV_DBG_TT, bat_priv,
			   "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);

		/* tvlv API will re-route the packet */
		return NET_RX_DROP;
	}

	return NET_RX_SUCCESS;
}

3713 3714 3715 3716 3717 3718 3719 3720
/**
 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
 * @bat_priv: the bat priv with all the soft interface information
 * @src: mac address of tt tvlv sender
 * @dst: mac address of tt tvlv recipient
 * @tvlv_value: tvlv buffer containing the tt data
 * @tvlv_value_len: tvlv buffer length
 *
3721
 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3722 3723 3724
 * otherwise.
 */
static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3725
					       u8 *src, u8 *dst,
3726
					       void *tvlv_value,
3727
					       u16 tvlv_value_len)
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753
{
	struct batadv_tvlv_roam_adv *roaming_adv;
	struct batadv_orig_node *orig_node = NULL;

	/* If this node is not the intended recipient of the
	 * roaming advertisement the packet is forwarded
	 * (the tvlv API will re-route the packet).
	 */
	if (!batadv_is_my_mac(bat_priv, dst))
		return NET_RX_DROP;

	if (tvlv_value_len < sizeof(*roaming_adv))
		goto out;

	orig_node = batadv_orig_hash_find(bat_priv, src);
	if (!orig_node)
		goto out;

	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
	roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;

	batadv_dbg(BATADV_DBG_TT, bat_priv,
		   "Received ROAMING_ADV from %pM (client %pM)\n",
		   src, roaming_adv->client);

	batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3754
			     ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3755 3756 3757 3758 3759 3760 3761 3762
			     atomic_read(&orig_node->last_ttvn) + 1);

out:
	if (orig_node)
		batadv_orig_node_free_ref(orig_node);
	return NET_RX_SUCCESS;
}

3763 3764 3765 3766
/**
 * batadv_tt_init - initialise the translation table internals
 * @bat_priv: the bat priv with all the soft interface information
 *
3767
 * Return: 0 on success or negative error number in case of failure.
3768 3769 3770 3771 3772
 */
int batadv_tt_init(struct batadv_priv *bat_priv)
{
	int ret;

3773 3774 3775
	/* synchronized flags must be remote */
	BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));

3776 3777 3778 3779 3780 3781 3782 3783 3784
	ret = batadv_tt_local_init(bat_priv);
	if (ret < 0)
		return ret;

	ret = batadv_tt_global_init(bat_priv);
	if (ret < 0)
		return ret;

	batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3785 3786
				     batadv_tt_tvlv_unicast_handler_v1,
				     BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3787

3788 3789 3790 3791
	batadv_tvlv_handler_register(bat_priv, NULL,
				     batadv_roam_tvlv_unicast_handler_v1,
				     BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);

3792 3793 3794 3795 3796 3797
	INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));

	return 1;
}
3798 3799 3800 3801 3802 3803 3804

/**
 * batadv_tt_global_is_isolated - check if a client is marked as isolated
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: the mac address of the client
 * @vid: the identifier of the VLAN where this client is connected
 *
3805
 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3806 3807 3808
 * otherwise
 */
bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3809
				  const u8 *addr, unsigned short vid)
3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823
{
	struct batadv_tt_global_entry *tt;
	bool ret;

	tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
	if (!tt)
		return false;

	ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;

	batadv_tt_global_entry_free_ref(tt);

	return ret;
}