translation-table.c 74.4 KB
Newer Older
1
/* Copyright (C) 2007-2013 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 15 16 17 18 19 20 21 22
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
#include "translation-table.h"
#include "soft-interface.h"
23
#include "hard-interface.h"
24
#include "send.h"
25 26
#include "hash.h"
#include "originator.h"
27
#include "routing.h"
28
#include "bridge_loop_avoidance.h"
29

30 31
#include <linux/crc16.h>

32 33 34 35
/* 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;

36 37
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node);
38 39
static void batadv_tt_purge(struct work_struct *work);
static void
40
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
41 42 43 44
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
				 const unsigned char *addr,
				 const char *message, bool roaming);
45

46
/* returns 1 if they are the same mac addr */
47
static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
48
{
49
	const void *data1 = container_of(node, struct batadv_tt_common_entry,
50
					 hash_entry);
51 52 53 54

	return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
}

55
static struct batadv_tt_common_entry *
56
batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
57 58 59
{
	struct hlist_head *head;
	struct hlist_node *node;
60 61
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
62
	uint32_t index;
63 64 65 66

	if (!hash)
		return NULL;

67
	index = batadv_choose_orig(data, hash->size);
68 69 70
	head = &hash->table[index];

	rcu_read_lock();
71
	hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
72
		if (!batadv_compare_eth(tt_common_entry, data))
73 74
			continue;

75
		if (!atomic_inc_not_zero(&tt_common_entry->refcount))
76 77
			continue;

78
		tt_common_entry_tmp = tt_common_entry;
79 80 81 82
		break;
	}
	rcu_read_unlock();

83
	return tt_common_entry_tmp;
84 85
}

86 87
static struct batadv_tt_local_entry *
batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
88
{
89 90
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local_entry = NULL;
91

92
	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
93 94
	if (tt_common_entry)
		tt_local_entry = container_of(tt_common_entry,
95 96
					      struct batadv_tt_local_entry,
					      common);
97 98
	return tt_local_entry;
}
99

100 101
static struct batadv_tt_global_entry *
batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
102
{
103 104
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
105

106
	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
107 108
	if (tt_common_entry)
		tt_global_entry = container_of(tt_common_entry,
109 110
					       struct batadv_tt_global_entry,
					       common);
111
	return tt_global_entry;
112 113
}

114
static void
115
batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
116
{
117 118
	if (atomic_dec_and_test(&tt_local_entry->common.refcount))
		kfree_rcu(tt_local_entry, common.rcu);
119 120
}

121
static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
122
{
123 124
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global_entry;
125

126 127 128
	tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry, common);
129 130 131 132

	kfree(tt_global_entry);
}

133
static void
134
batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
135
{
136
	if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
137
		batadv_tt_global_del_orig_list(tt_global_entry);
138
		call_rcu(&tt_global_entry->common.rcu,
139
			 batadv_tt_global_entry_free_rcu);
140 141 142
	}
}

143
static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
144
{
145
	struct batadv_tt_orig_list_entry *orig_entry;
146

147
	orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
148
	batadv_orig_node_free_ref(orig_entry->orig_node);
149 150 151
	kfree(orig_entry);
}

152
static void
153
batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
154
{
155 156
	if (!atomic_dec_and_test(&orig_entry->refcount))
		return;
157 158
	/* to avoid race conditions, immediately decrease the tt counter */
	atomic_dec(&orig_entry->orig_node->tt_size);
159
	call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
160 161
}

162
static void batadv_tt_local_event(struct batadv_priv *bat_priv,
163
				  const uint8_t *addr, uint8_t flags)
164
{
165
	struct batadv_tt_change_node *tt_change_node, *entry, *safe;
166 167
	bool event_removed = false;
	bool del_op_requested, del_op_entry;
168 169 170 171 172 173

	tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);

	if (!tt_change_node)
		return;

174
	tt_change_node->change.flags = flags;
175 176
	memcpy(tt_change_node->change.addr, addr, ETH_ALEN);

177
	del_op_requested = flags & BATADV_TT_CLIENT_DEL;
178 179

	/* check for ADD+DEL or DEL+ADD events */
180 181
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
182 183 184 185 186 187 188 189 190 191 192
				 list) {
		if (!batadv_compare_eth(entry->change.addr, addr))
			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
		 */
193
		del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
194 195 196 197 198 199 200 201
		if (!del_op_requested && del_op_entry)
			goto del;
		if (del_op_requested && !del_op_entry)
			goto del;
		continue;
del:
		list_del(&entry->list);
		kfree(entry);
202
		kfree(tt_change_node);
203 204 205 206
		event_removed = true;
		goto unlock;
	}

207
	/* track the change in the OGMinterval list */
208
	list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
209 210

unlock:
211
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
212

213
	if (event_removed)
214
		atomic_dec(&bat_priv->tt.local_changes);
215
	else
216
		atomic_inc(&bat_priv->tt.local_changes);
217 218
}

219
int batadv_tt_len(int changes_num)
220
{
221
	return changes_num * sizeof(struct batadv_tt_change);
222 223
}

224
static int batadv_tt_local_init(struct batadv_priv *bat_priv)
225
{
226
	if (bat_priv->tt.local_hash)
227
		return 0;
228

229
	bat_priv->tt.local_hash = batadv_hash_new(1024);
230

231
	if (!bat_priv->tt.local_hash)
232
		return -ENOMEM;
233

234 235 236
	batadv_hash_set_lock_class(bat_priv->tt.local_hash,
				   &batadv_tt_local_hash_lock_class_key);

237
	return 0;
238 239
}

240 241 242 243 244 245 246 247 248 249 250 251 252
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,
		   "Deleting global tt entry %pM: %s\n",
		   tt_global->common.addr, message);

	batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
			   batadv_choose_orig, tt_global->common.addr);
	batadv_tt_global_entry_free_ref(tt_global);
}

253 254
void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
			 int ifindex)
255
{
256
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
257 258
	struct batadv_tt_local_entry *tt_local;
	struct batadv_tt_global_entry *tt_global;
259 260
	struct hlist_head *head;
	struct hlist_node *node;
261
	struct batadv_tt_orig_list_entry *orig_entry;
262
	int hash_added;
263
	bool roamed_back = false;
264

265
	tt_local = batadv_tt_local_hash_find(bat_priv, addr);
266
	tt_global = batadv_tt_global_hash_find(bat_priv, addr);
267

268 269
	if (tt_local) {
		tt_local->last_seen = jiffies;
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
		if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
			batadv_dbg(BATADV_DBG_TT, bat_priv,
				   "Re-adding pending client %pM\n", addr);
			/* 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,
				   "Roaming client %pM came back to its original location\n",
				   addr);
			/* 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;
295 296
	}

297 298
	tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
	if (!tt_local)
299
		goto out;
300

301
	batadv_dbg(BATADV_DBG_TT, bat_priv,
302
		   "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
303
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
304

305
	memcpy(tt_local->common.addr, addr, ETH_ALEN);
306 307 308 309 310
	/* 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;
311
	if (batadv_is_wifi_iface(ifindex))
312 313 314 315
		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;
316 317

	/* the batman interface mac address should never be purged */
318
	if (batadv_compare_eth(addr, soft_iface->dev_addr))
319
		tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
320

321
	hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
322 323
				     batadv_choose_orig, &tt_local->common,
				     &tt_local->common.hash_entry);
324 325 326

	if (unlikely(hash_added != 0)) {
		/* remove the reference for the hash */
327
		batadv_tt_local_entry_free_ref(tt_local);
328 329 330
		goto out;
	}

331
add_event:
332
	batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
333

334 335 336 337 338
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)) {
339
		/* These node are probably going to update their tt table */
340
		head = &tt_global->orig_list;
341 342
		rcu_read_lock();
		hlist_for_each_entry_rcu(orig_entry, node, head, list) {
343
			batadv_send_roam_adv(bat_priv, tt_global->common.addr,
344
					     orig_entry->orig_node);
345 346
		}
		rcu_read_unlock();
347 348 349 350 351 352 353 354 355 356 357
		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;
		}
358
	}
359

360
out:
361 362 363 364
	if (tt_local)
		batadv_tt_local_entry_free_ref(tt_local);
	if (tt_global)
		batadv_tt_global_entry_free_ref(tt_global);
365 366
}

367 368 369 370
static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
					  int *packet_buff_len,
					  int min_packet_len,
					  int new_packet_len)
371 372 373 374 375 376 377 378 379 380 381 382 383 384
{
	unsigned char *new_buff;

	new_buff = kmalloc(new_packet_len, GFP_ATOMIC);

	/* keep old buffer if kmalloc should fail */
	if (new_buff) {
		memcpy(new_buff, *packet_buff, min_packet_len);
		kfree(*packet_buff);
		*packet_buff = new_buff;
		*packet_buff_len = new_packet_len;
	}
}

385
static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
386 387 388
					  unsigned char **packet_buff,
					  int *packet_buff_len,
					  int min_packet_len)
389
{
390
	struct batadv_hard_iface *primary_if;
391 392
	int req_len;

393
	primary_if = batadv_primary_if_get_selected(bat_priv);
394 395

	req_len = min_packet_len;
396
	req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
397 398 399 400 401 402 403

	/* if we have too many changes for one packet don't send any
	 * and wait for the tt table request which will be fragmented
	 */
	if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
		req_len = min_packet_len;

404 405
	batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
				      min_packet_len, req_len);
406 407

	if (primary_if)
408
		batadv_hardif_free_ref(primary_if);
409 410
}

411
static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
412 413 414
				       unsigned char **packet_buff,
				       int *packet_buff_len,
				       int min_packet_len)
415
{
416
	struct batadv_tt_change_node *entry, *safe;
417 418 419
	int count = 0, tot_changes = 0, new_len;
	unsigned char *tt_buff;

420 421
	batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
				      packet_buff_len, min_packet_len);
422

423 424 425 426
	new_len = *packet_buff_len - min_packet_len;
	tt_buff = *packet_buff + min_packet_len;

	if (new_len > 0)
427
		tot_changes = new_len / batadv_tt_len(1);
428

429 430
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	atomic_set(&bat_priv->tt.local_changes, 0);
431

432
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
433
				 list) {
434
		if (count < tot_changes) {
435
			memcpy(tt_buff + batadv_tt_len(count),
436
			       &entry->change, sizeof(struct batadv_tt_change));
437 438
			count++;
		}
439 440
		list_del(&entry->list);
		kfree(entry);
441
	}
442
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
443 444

	/* Keep the buffer for possible tt_request */
445 446 447 448
	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;
449 450 451
	/* check whether this new OGM has no changes due to size problems */
	if (new_len > 0) {
		/* if kmalloc() fails we will reply with the full table
452 453
		 * instead of providing the diff
		 */
454 455 456 457
		bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
		if (bat_priv->tt.last_changeset) {
			memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
			bat_priv->tt.last_changeset_len = new_len;
458 459
		}
	}
460
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
461

462
	return count;
463 464
}

465
int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
466 467
{
	struct net_device *net_dev = (struct net_device *)seq->private;
468
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
469
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
470
	struct batadv_tt_common_entry *tt_common_entry;
471
	struct batadv_tt_local_entry *tt_local;
472
	struct batadv_hard_iface *primary_if;
473
	struct hlist_node *node;
474
	struct hlist_head *head;
475
	uint32_t i;
476 477 478 479 480
	int last_seen_secs;
	int last_seen_msecs;
	unsigned long last_seen_jiffies;
	bool no_purge;
	uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
481

482 483
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
484
		goto out;
485

486
	seq_printf(seq,
487 488 489
		   "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
		   net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
		   bat_priv->tt.local_crc);
490 491
	seq_printf(seq, "       %-13s %-7s %-10s\n", "Client", "Flags",
		   "Last seen");
492 493 494 495

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

496
		rcu_read_lock();
497
		hlist_for_each_entry_rcu(tt_common_entry, node,
498
					 head, hash_entry) {
499 500 501 502 503 504 505 506 507 508 509
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
			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;

			seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
510 511
				   tt_common_entry->addr,
				   (tt_common_entry->flags &
512
				    BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
513
				   no_purge ? 'P' : '.',
514
				   (tt_common_entry->flags &
515
				    BATADV_TT_CLIENT_NEW ? 'N' : '.'),
516
				   (tt_common_entry->flags &
517
				    BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
518
				   (tt_common_entry->flags &
519
				    BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
520 521
				   no_purge ? 0 : last_seen_secs,
				   no_purge ? 0 : last_seen_msecs);
522
		}
523
		rcu_read_unlock();
524
	}
525 526
out:
	if (primary_if)
527
		batadv_hardif_free_ref(primary_if);
528
	return 0;
529 530
}

531 532 533 534
static void
batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
			    struct batadv_tt_local_entry *tt_local_entry,
			    uint16_t flags, const char *message)
535
{
536 537
	batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
			      tt_local_entry->common.flags | flags);
538

539 540
	/* 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
541 542
	 * response issued before the net ttvn increment (consistency check)
	 */
543
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
544

545
	batadv_dbg(BATADV_DBG_TT, bat_priv,
546 547
		   "Local tt entry (%pM) pending to be removed: %s\n",
		   tt_local_entry->common.addr, message);
548 549
}

550 551 552 553 554 555 556 557 558 559 560 561
/**
 * 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
 * @message: message to append to the log on deletion
 * @roaming: true if the deletion is due to a roaming event
 *
 * Returns the flags assigned to the local entry before being deleted
 */
uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
				const uint8_t *addr, const char *message,
				bool roaming)
562
{
563
	struct batadv_tt_local_entry *tt_local_entry;
564
	uint16_t flags, curr_flags = BATADV_NO_FLAGS;
565

566
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
567 568 569
	if (!tt_local_entry)
		goto out;

570 571
	curr_flags = tt_local_entry->common.flags;

572
	flags = BATADV_TT_CLIENT_DEL;
573 574 575 576
	/* 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
	 */
577
	if (roaming) {
578
		flags |= BATADV_TT_CLIENT_ROAM;
579 580 581
		/* mark the local client as ROAMed */
		tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
	}
582

583 584 585 586 587 588 589 590 591 592 593 594
	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
	 */
	batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
			      curr_flags | BATADV_TT_CLIENT_DEL);
	hlist_del_rcu(&tt_local_entry->common.hash_entry);
	batadv_tt_local_entry_free_ref(tt_local_entry);
595

596 597
out:
	if (tt_local_entry)
598
		batadv_tt_local_entry_free_ref(tt_local_entry);
599 600

	return curr_flags;
601 602
}

603
static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
604
				       struct hlist_head *head)
605
{
606 607
	struct batadv_tt_local_entry *tt_local_entry;
	struct batadv_tt_common_entry *tt_common_entry;
608
	struct hlist_node *node, *node_tmp;
609 610 611 612

	hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
				  hash_entry) {
		tt_local_entry = container_of(tt_common_entry,
613 614
					      struct batadv_tt_local_entry,
					      common);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
		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;

		if (!batadv_has_timed_out(tt_local_entry->last_seen,
					  BATADV_TT_LOCAL_TIMEOUT))
			continue;

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

631
static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
632
{
633
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
634
	struct hlist_head *head;
635
	spinlock_t *list_lock; /* protects write access to the hash lists */
636
	uint32_t i;
637 638 639

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

642
		spin_lock_bh(list_lock);
643
		batadv_tt_local_purge_list(bat_priv, head);
644
		spin_unlock_bh(list_lock);
645 646 647
	}
}

648
static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
649
{
650
	struct batadv_hashtable *hash;
651
	spinlock_t *list_lock; /* protects write access to the hash lists */
652 653
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local;
654 655
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
656
	uint32_t i;
657

658
	if (!bat_priv->tt.local_hash)
659 660
		return;

661
	hash = bat_priv->tt.local_hash;
662 663 664 665 666 667

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

		spin_lock_bh(list_lock);
668
		hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
669 670
					  head, hash_entry) {
			hlist_del_rcu(node);
671 672 673 674
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
675 676 677 678
		}
		spin_unlock_bh(list_lock);
	}

679
	batadv_hash_destroy(hash);
680

681
	bat_priv->tt.local_hash = NULL;
682 683
}

684
static int batadv_tt_global_init(struct batadv_priv *bat_priv)
685
{
686
	if (bat_priv->tt.global_hash)
687
		return 0;
688

689
	bat_priv->tt.global_hash = batadv_hash_new(1024);
690

691
	if (!bat_priv->tt.global_hash)
692
		return -ENOMEM;
693

694 695 696
	batadv_hash_set_lock_class(bat_priv->tt.global_hash,
				   &batadv_tt_global_hash_lock_class_key);

697
	return 0;
698 699
}

700
static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
701
{
702
	struct batadv_tt_change_node *entry, *safe;
703

704
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
705

706
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
707 708 709 710
				 list) {
		list_del(&entry->list);
		kfree(entry);
	}
711

712 713
	atomic_set(&bat_priv->tt.local_changes, 0);
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
714
}
715

716 717 718 719
/* retrieves the orig_tt_list_entry belonging to orig_node from the
 * batadv_tt_global_entry list
 *
 * returns it with an increased refcounter, NULL if not found
720
 */
721 722 723
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)
724
{
725
	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
726 727 728 729 730 731
	const struct hlist_head *head;
	struct hlist_node *node;

	rcu_read_lock();
	head = &entry->orig_list;
	hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
732 733 734 735 736 737 738
		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;
739 740
	}
	rcu_read_unlock();
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

	return orig_entry;
}

/* find out if an orig_node is already in the list of a tt_global_entry.
 * returns true if found, false otherwise
 */
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);
	}

761 762 763
	return found;
}

764
static void
765
batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
766
				struct batadv_orig_node *orig_node, int ttvn)
767
{
768
	struct batadv_tt_orig_list_entry *orig_entry;
769

770
	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
771 772 773 774 775
	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;
776
		goto out;
777
	}
778

779 780
	orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
	if (!orig_entry)
781
		goto out;
782 783 784 785 786 787

	INIT_HLIST_NODE(&orig_entry->list);
	atomic_inc(&orig_node->refcount);
	atomic_inc(&orig_node->tt_size);
	orig_entry->orig_node = orig_node;
	orig_entry->ttvn = ttvn;
788
	atomic_set(&orig_entry->refcount, 2);
789

790
	spin_lock_bh(&tt_global->list_lock);
791
	hlist_add_head_rcu(&orig_entry->list,
792 793 794 795 796
			   &tt_global->orig_list);
	spin_unlock_bh(&tt_global->list_lock);
out:
	if (orig_entry)
		batadv_tt_orig_list_entry_free_ref(orig_entry);
797 798
}

799
/* caller must hold orig_node refcount */
800 801
int batadv_tt_global_add(struct batadv_priv *bat_priv,
			 struct batadv_orig_node *orig_node,
802 803
			 const unsigned char *tt_addr, uint8_t flags,
			 uint8_t ttvn)
804
{
805 806
	struct batadv_tt_global_entry *tt_global_entry;
	struct batadv_tt_local_entry *tt_local_entry;
807
	int ret = 0;
808
	int hash_added;
809
	struct batadv_tt_common_entry *common;
810
	uint16_t local_flags;
811

812
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
813 814 815 816 817 818 819 820 821
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);

	/* 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;
822 823

	if (!tt_global_entry) {
824
		tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
825
		if (!tt_global_entry)
826 827
			goto out;

828 829
		common = &tt_global_entry->common;
		memcpy(common->addr, tt_addr, ETH_ALEN);
830

831
		common->flags = flags;
832
		tt_global_entry->roam_at = 0;
833 834 835 836 837 838
		/* 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;
839
		atomic_set(&common->refcount, 2);
840
		common->added_at = jiffies;
841 842 843

		INIT_HLIST_HEAD(&tt_global_entry->orig_list);
		spin_lock_init(&tt_global_entry->list_lock);
844

845
		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
846 847 848
					     batadv_compare_tt,
					     batadv_choose_orig, common,
					     &common->hash_entry);
849 850 851

		if (unlikely(hash_added != 0)) {
			/* remove the reference for the hash */
852
			batadv_tt_global_entry_free_ref(tt_global_entry);
853 854
			goto out_remove;
		}
855
	} else {
856
		common = &tt_global_entry->common;
857 858
		/* If there is already a global entry, we can use this one for
		 * our processing.
859 860 861 862 863 864 865
		 * 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
866
		 */
867 868 869 870 871 872 873 874 875
		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;
		}
876 877 878 879

		/* if the client was temporary added before receiving the first
		 * OGM announcing it, we have to clear the TEMP flag
		 */
880
		common->flags &= ~BATADV_TT_CLIENT_TEMP;
881

882 883 884 885 886 887
		/* the change can carry possible "attribute" flags like the
		 * TT_CLIENT_WIFI, therefore they have to be copied in the
		 * client entry
		 */
		tt_global_entry->common.flags |= flags;

888 889
		/* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
		 * one originator left in the list and we previously received a
890 891 892 893 894
		 * delete + roaming change for this originator.
		 *
		 * We should first delete the old originator before adding the
		 * new one.
		 */
895
		if (common->flags & BATADV_TT_CLIENT_ROAM) {
896
			batadv_tt_global_del_orig_list(tt_global_entry);
897
			common->flags &= ~BATADV_TT_CLIENT_ROAM;
898
			tt_global_entry->roam_at = 0;
899 900
		}
	}
901
add_orig_entry:
902
	/* add the new orig_entry (if needed) or update it */
903
	batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
904

905
	batadv_dbg(BATADV_DBG_TT, bat_priv,
906
		   "Creating new global tt entry: %pM (via %pM)\n",
907
		   common->addr, orig_node->orig);
908
	ret = 1;
909

910
out_remove:
911

912
	/* remove address from local hash if present */
913 914
	local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
					     "global tt received",
915
					     !!(flags & BATADV_TT_CLIENT_ROAM));
916 917
	tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;

918 919 920 921 922 923
	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;

924 925
out:
	if (tt_global_entry)
926
		batadv_tt_global_entry_free_ref(tt_global_entry);
927 928
	if (tt_local_entry)
		batadv_tt_local_entry_free_ref(tt_local_entry);
929
	return ret;
930 931
}

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
/* batadv_transtable_best_orig - Get best originator list entry from tt entry
 * @tt_global_entry: global translation table entry to be analyzed
 *
 * This functon assumes the caller holds rcu_read_lock().
 * Returns best originator list entry or NULL on errors.
 */
static struct batadv_tt_orig_list_entry *
batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
{
	struct batadv_neigh_node *router = NULL;
	struct hlist_head *head;
	struct hlist_node *node;
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
	int best_tq = 0;

	head = &tt_global_entry->orig_list;
	hlist_for_each_entry_rcu(orig_entry, node, head, list) {
		router = batadv_orig_node_get_router(orig_entry->orig_node);
		if (!router)
			continue;

		if (router->tq_avg > best_tq) {
			best_entry = orig_entry;
			best_tq = router->tq_avg;
		}

		batadv_neigh_node_free_ref(router);
	}

	return best_entry;
}

/* batadv_tt_global_print_entry - print all orig nodes who announce the address
 * for this global entry
 * @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().
970
 */
971
static void
972
batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
973
			     struct seq_file *seq)
974 975 976
{
	struct hlist_head *head;
	struct hlist_node *node;
977
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
978
	struct batadv_tt_common_entry *tt_common_entry;
979 980 981 982
	uint16_t flags;
	uint8_t last_ttvn;

	tt_common_entry = &tt_global_entry->common;
983 984 985 986 987
	flags = tt_common_entry->flags;

	best_entry = batadv_transtable_best_orig(tt_global_entry);
	if (best_entry) {
		last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
988 989
		seq_printf(seq,
			   " %c %pM  (%3u) via %pM     (%3u)   (%#.4x) [%c%c%c]\n",
990 991
			   '*', tt_global_entry->common.addr,
			   best_entry->ttvn, best_entry->orig_node->orig,
992
			   last_ttvn, best_entry->orig_node->tt_crc,
993 994 995 996
			   (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
			   (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
			   (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
	}
997 998 999 1000

	head = &tt_global_entry->orig_list;

	hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1001 1002 1003
		if (best_entry == orig_entry)
			continue;

1004
		last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1005 1006 1007 1008
		seq_printf(seq,	" %c %pM  (%3u) via %pM     (%3u)   [%c%c%c]\n",
			   '+', tt_global_entry->common.addr,
			   orig_entry->ttvn, orig_entry->orig_node->orig,
			   last_ttvn,
1009
			   (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1010 1011
			   (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
			   (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1012 1013 1014
	}
}

1015
int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1016 1017
{
	struct net_device *net_dev = (struct net_device *)seq->private;
1018
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1019
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1020 1021 1022
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
	struct batadv_hard_iface *primary_if;
1023
	struct hlist_node *node;
1024
	struct hlist_head *head;
1025
	uint32_t i;
1026

1027 1028
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1029
		goto out;
1030

1031 1032
	seq_printf(seq,
		   "Globally announced TT entries received via the mesh %s\n",
1033
		   net_dev->name);
1034 1035 1036
	seq_printf(seq, "       %-13s %s       %-15s %s (%-6s) %s\n",
		   "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
		   "Flags");
1037 1038 1039 1040

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

1041
		rcu_read_lock();
1042
		hlist_for_each_entry_rcu(tt_common_entry, node,
1043
					 head, hash_entry) {
1044 1045 1046 1047
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_print_entry(tt_global, seq);
1048
		}
1049
		rcu_read_unlock();
1050
	}
1051 1052
out:
	if (primary_if)
1053
		batadv_hardif_free_ref(primary_if);
1054
	return 0;
1055 1056
}

1057
/* deletes the orig list of a tt_global_entry */
1058
static void
1059
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1060
{
1061 1062
	struct hlist_head *head;
	struct hlist_node *node, *safe;
1063
	struct batadv_tt_orig_list_entry *orig_entry;
1064

1065 1066 1067 1068
	spin_lock_bh(&tt_global_entry->list_lock);
	head = &tt_global_entry->orig_list;
	hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
		hlist_del_rcu(node);
1069
		batadv_tt_orig_list_entry_free_ref(orig_entry);
1070 1071 1072 1073
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
}

1074
static void
1075 1076 1077
batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
				struct batadv_tt_global_entry *tt_global_entry,
				struct batadv_orig_node *orig_node,
1078
				const char *message)
1079 1080 1081
{
	struct hlist_head *head;
	struct hlist_node *node, *safe;
1082
	struct batadv_tt_orig_list_entry *orig_entry;
1083 1084 1085 1086 1087

	spin_lock_bh(&tt_global_entry->list_lock);
	head = &tt_global_entry->orig_list;
	hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
		if (orig_entry->orig_node == orig_node) {
1088
			batadv_dbg(BATADV_DBG_TT, bat_priv,
1089 1090 1091
				   "Deleting %pM from global tt entry %pM: %s\n",
				   orig_node->orig,
				   tt_global_entry->common.addr, message);
1092
			hlist_del_rcu(node);
1093
			batadv_tt_orig_list_entry_free_ref(orig_entry);
1094 1095 1096 1097 1098 1099
		}
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
}

/* If the client is to be deleted, we check if it is the last origantor entry
1100 1101
 * 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.
1102
 */
1103
static void
1104 1105 1106 1107
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)
1108 1109 1110 1111
{
	bool last_entry = true;
	struct hlist_head *head;
	struct hlist_node *node;
1112
	struct batadv_tt_orig_list_entry *orig_entry;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

	/* 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;
	hlist_for_each_entry_rcu(orig_entry, node, head, list) {
		if (orig_entry->orig_node != orig_node) {
			last_entry = false;
			break;
		}
	}
	rcu_read_unlock();

	if (last_entry) {
		/* its the last one, mark for roaming. */
1130
		tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1131 1132 1133 1134 1135
		tt_global_entry->roam_at = jiffies;
	} else
		/* there is another entry, we can simply delete this
		 * one and can still use the other one.
		 */
1136 1137
		batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
						orig_node, message);
1138 1139 1140 1141
}



1142 1143
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
1144 1145
				 const unsigned char *addr,
				 const char *message, bool roaming)
1146
{
1147
	struct batadv_tt_global_entry *tt_global_entry;
1148
	struct batadv_tt_local_entry *local_entry = NULL;
1149

1150
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1151
	if (!tt_global_entry)
1152
		goto out;
1153

1154
	if (!roaming) {
1155 1156
		batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
						orig_node, message);
1157 1158

		if (hlist_empty(&tt_global_entry->orig_list))
1159 1160
			batadv_tt_global_free(bat_priv, tt_global_entry,
					      message);
1161 1162 1163

		goto out;
	}
1164 1165 1166

	/* if we are deleting a global entry due to a roam
	 * event, there are two possibilities:
1167 1168
	 * 1) the client roamed from node A to node B => if there
	 *    is only one originator left for this client, we mark
1169
	 *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1170 1171
	 *    wait for node B to claim it. In case of timeout
	 *    the entry is purged.
1172 1173 1174
	 *
	 *    If there are other originators left, we directly delete
	 *    the originator.
1175
	 * 2) the client roamed to us => we can directly delete
1176 1177
	 *    the global entry, since it is useless now.
	 */
1178 1179 1180
	local_entry = batadv_tt_local_hash_find(bat_priv,
						tt_global_entry->common.addr);
	if (local_entry) {
1181
		/* local entry exists, case 2: client roamed to us. */
1182
		batadv_tt_global_del_orig_list(tt_global_entry);
1183
		batadv_tt_global_free(bat_priv, tt_global_entry, message);
1184 1185
	} else
		/* no local entry exists, case 1: check for roaming */
1186 1187
		batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
					     orig_node, message);
1188 1189


1190
out:
1191
	if (tt_global_entry)
1192 1193 1194
		batadv_tt_global_entry_free_ref(tt_global_entry);
	if (local_entry)
		batadv_tt_local_entry_free_ref(local_entry);
1195 1196
}

1197 1198 1199
void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
			       struct batadv_orig_node *orig_node,
			       const char *message)
1200
{
1201 1202
	struct batadv_tt_global_entry *tt_global;
	struct batadv_tt_common_entry *tt_common_entry;
1203
	uint32_t i;
1204
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1205 1206
	struct hlist_node *node, *safe;
	struct hlist_head *head;
1207
	spinlock_t *list_lock; /* protects write access to the hash lists */
1208

1209 1210 1211
	if (!hash)
		return;

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

1216
		spin_lock_bh(list_lock);
1217
		hlist_for_each_entry_safe(tt_common_entry, node, safe,
1218
					  head, hash_entry) {
1219 1220 1221
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
1222

1223
			batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1224
							orig_node, message);
1225

1226
			if (hlist_empty(&tt_global->orig_list)) {
1227
				batadv_dbg(BATADV_DBG_TT, bat_priv,
1228
					   "Deleting global tt entry %pM: %s\n",
1229
					   tt_global->common.addr, message);
1230
				hlist_del_rcu(node);
1231
				batadv_tt_global_entry_free_ref(tt_global);
1232
			}
1233
		}
1234
		spin_unlock_bh(list_lock);
1235
	}
1236
	orig_node->tt_initialised = false;
1237 1238
}

1239 1240
static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
				      char **msg)
1241
{
1242 1243 1244
	bool purge = false;
	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1245

1246 1247 1248 1249 1250
	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";
	}
1251

1252 1253 1254 1255
	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";
1256
	}
1257 1258

	return purge;
1259 1260
}

1261
static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1262
{
1263
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1264
	struct hlist_head *head;
1265
	struct hlist_node *node, *node_tmp;
1266
	spinlock_t *list_lock; /* protects write access to the hash lists */
1267
	uint32_t i;
1268 1269 1270
	char *msg = NULL;
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
1271 1272 1273

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

1276
		spin_lock_bh(list_lock);
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
		hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
					  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,
				   "Deleting global tt entry (%pM): %s\n",
				   tt_global->common.addr, msg);

			hlist_del_rcu(node);

			batadv_tt_global_entry_free_ref(tt_global);
		}
1294
		spin_unlock_bh(list_lock);
1295 1296 1297
	}
}

1298
static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1299
{
1300
	struct batadv_hashtable *hash;
1301
	spinlock_t *list_lock; /* protects write access to the hash lists */
1302 1303
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
1304 1305
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
1306
	uint32_t i;
1307

1308
	if (!bat_priv->tt.global_hash)
1309 1310
		return;

1311
	hash = bat_priv->tt.global_hash;
1312 1313 1314 1315 1316 1317

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

		spin_lock_bh(list_lock);
1318
		hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1319 1320
					  head, hash_entry) {
			hlist_del_rcu(node);
1321 1322 1323 1324
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_entry_free_ref(tt_global);
1325 1326 1327 1328
		}
		spin_unlock_bh(list_lock);
	}

1329
	batadv_hash_destroy(hash);
1330

1331
	bat_priv->tt.global_hash = NULL;
1332 1333
}

1334 1335 1336
static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
		       struct batadv_tt_global_entry *tt_global_entry)
1337 1338 1339
{
	bool ret = false;

1340 1341
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1342 1343 1344 1345 1346
		ret = true;

	return ret;
}

1347 1348 1349
struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
						  const uint8_t *src,
						  const uint8_t *addr)
1350
{
1351 1352 1353
	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;
1354
	struct batadv_tt_orig_list_entry *best_entry;
1355

1356
	if (src && atomic_read(&bat_priv->ap_isolation)) {
1357
		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1358 1359
		if (!tt_local_entry ||
		    (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1360 1361
			goto out;
	}
1362

1363
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1364
	if (!tt_global_entry)
1365
		goto out;
1366

1367
	/* check whether the clients should not communicate due to AP
1368 1369
	 * isolation
	 */
1370 1371
	if (tt_local_entry &&
	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1372 1373
		goto out;

1374
	rcu_read_lock();
1375
	best_entry = batadv_transtable_best_orig(tt_global_entry);
1376
	/* found anything? */
1377 1378
	if (best_entry)
		orig_node = best_entry->orig_node;
1379 1380 1381
	if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
		orig_node = NULL;
	rcu_read_unlock();
1382

1383
out:
1384
	if (tt_global_entry)
1385
		batadv_tt_global_entry_free_ref(tt_global_entry);
1386
	if (tt_local_entry)
1387
		batadv_tt_local_entry_free_ref(tt_local_entry);
1388

1389
	return orig_node;
1390
}
1391 1392

/* Calculates the checksum of the local table of a given orig_node */
1393 1394
static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node)
1395 1396
{
	uint16_t total = 0, total_one;
1397
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1398 1399
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
1400 1401
	struct hlist_node *node;
	struct hlist_head *head;
1402 1403
	uint32_t i;
	int j;
1404 1405 1406 1407 1408

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

		rcu_read_lock();
1409
		hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1410 1411 1412
			tt_global = container_of(tt_common,
						 struct batadv_tt_global_entry,
						 common);
1413 1414 1415 1416 1417
			/* Roaming clients are in the global table for
			 * consistency only. They don't have to be
			 * taken into account while computing the
			 * global crc
			 */
1418
			if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1419
				continue;
1420 1421 1422 1423 1424 1425
			/* 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;
1426 1427 1428 1429

			/* find out if this global entry is announced by this
			 * originator
			 */
1430
			if (!batadv_tt_global_entry_has_orig(tt_global,
1431
							     orig_node))
1432 1433 1434 1435 1436
				continue;

			total_one = 0;
			for (j = 0; j < ETH_ALEN; j++)
				total_one = crc16_byte(total_one,
1437
						       tt_common->addr[j]);
1438
			total ^= total_one;
1439 1440 1441 1442 1443 1444 1445 1446
		}
		rcu_read_unlock();
	}

	return total;
}

/* Calculates the checksum of the local table */
1447
static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1448 1449
{
	uint16_t total = 0, total_one;
1450
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1451
	struct batadv_tt_common_entry *tt_common;
1452 1453
	struct hlist_node *node;
	struct hlist_head *head;
1454 1455
	uint32_t i;
	int j;
1456 1457 1458 1459 1460

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

		rcu_read_lock();
1461
		hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1462
			/* not yet committed clients have not to be taken into
1463 1464
			 * account while computing the CRC
			 */
1465
			if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1466
				continue;
1467 1468 1469
			total_one = 0;
			for (j = 0; j < ETH_ALEN; j++)
				total_one = crc16_byte(total_one,
1470
						       tt_common->addr[j]);
1471 1472 1473 1474 1475 1476 1477 1478
			total ^= total_one;
		}
		rcu_read_unlock();
	}

	return total;
}

1479
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1480
{
1481
	struct batadv_tt_req_node *node, *safe;
1482

1483
	spin_lock_bh(&bat_priv->tt.req_list_lock);
1484

1485
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1486 1487 1488 1489
		list_del(&node->list);
		kfree(node);
	}

1490
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1491 1492
}

1493 1494
static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
				       struct batadv_orig_node *orig_node,
1495 1496
				       const unsigned char *tt_buff,
				       uint8_t tt_num_changes)
1497
{
1498
	uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1499 1500

	/* Replace the old buffer only if I received something in the
1501 1502
	 * last OGM (the OGM could carry no changes)
	 */
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
	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);
}

1516
static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1517
{
1518
	struct batadv_tt_req_node *node, *safe;
1519

1520 1521
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1522 1523
		if (batadv_has_timed_out(node->issued_at,
					 BATADV_TT_REQUEST_TIMEOUT)) {
1524 1525 1526 1527
			list_del(&node->list);
			kfree(node);
		}
	}
1528
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1529 1530 1531
}

/* returns the pointer to the new tt_req_node struct if no request
1532 1533
 * has already been issued for this orig_node, NULL otherwise
 */
1534 1535 1536
static struct batadv_tt_req_node *
batadv_new_tt_req_node(struct batadv_priv *bat_priv,
		       struct batadv_orig_node *orig_node)
1537
{
1538
	struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1539

1540 1541
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1542 1543
		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1544
					  BATADV_TT_REQUEST_TIMEOUT))
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
			goto unlock;
	}

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

	memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
	tt_req_node->issued_at = jiffies;

1555
	list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1556
unlock:
1557
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1558 1559 1560
	return tt_req_node;
}

1561
/* data_ptr is useless here, but has to be kept to respect the prototype */
1562 1563
static int batadv_tt_local_valid_entry(const void *entry_ptr,
				       const void *data_ptr)
1564
{
1565
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1566

1567
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1568 1569 1570 1571
		return 0;
	return 1;
}

1572 1573
static int batadv_tt_global_valid(const void *entry_ptr,
				  const void *data_ptr)
1574
{
1575 1576 1577
	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;
1578

1579 1580
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1581 1582
		return 0;

1583 1584
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
1585 1586
				       common);

1587
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1588 1589
}

1590 1591
static struct sk_buff *
batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1592
			      struct batadv_hashtable *hash,
1593
			      struct batadv_hard_iface *primary_if,
1594 1595
			      int (*valid_cb)(const void *, const void *),
			      void *cb_data)
1596
{
1597
	struct batadv_tt_common_entry *tt_common_entry;
1598 1599
	struct batadv_tt_query_packet *tt_response;
	struct batadv_tt_change *tt_change;
1600 1601 1602 1603
	struct hlist_node *node;
	struct hlist_head *head;
	struct sk_buff *skb = NULL;
	uint16_t tt_tot, tt_count;
1604
	ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1605
	uint32_t i;
1606
	size_t len;
1607 1608 1609

	if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
		tt_len = primary_if->soft_iface->mtu - tt_query_size;
1610
		tt_len -= tt_len % sizeof(struct batadv_tt_change);
1611
	}
1612
	tt_tot = tt_len / sizeof(struct batadv_tt_change);
1613

1614
	len = tt_query_size + tt_len;
1615
	skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1616 1617 1618
	if (!skb)
		goto out;

1619
	skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1620
	tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1621 1622
	tt_response->ttvn = ttvn;

1623
	tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1624 1625 1626 1627 1628 1629
	tt_count = 0;

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

1630
		hlist_for_each_entry_rcu(tt_common_entry, node,
1631 1632 1633 1634
					 head, hash_entry) {
			if (tt_count == tt_tot)
				break;

1635
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1636 1637
				continue;

1638 1639
			memcpy(tt_change->addr, tt_common_entry->addr,
			       ETH_ALEN);
1640
			tt_change->flags = tt_common_entry->flags;
1641 1642 1643 1644 1645 1646 1647

			tt_count++;
			tt_change++;
		}
	}
	rcu_read_unlock();

1648
	/* store in the message the number of entries we have successfully
1649 1650
	 * copied
	 */
1651 1652
	tt_response->tt_data = htons(tt_count);

1653 1654 1655 1656
out:
	return skb;
}

1657 1658
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *dst_orig_node,
1659 1660
				  uint8_t ttvn, uint16_t tt_crc,
				  bool full_table)
1661 1662
{
	struct sk_buff *skb = NULL;
1663
	struct batadv_tt_query_packet *tt_request;
1664 1665
	struct batadv_hard_iface *primary_if;
	struct batadv_tt_req_node *tt_req_node = NULL;
1666
	int ret = 1;
1667
	size_t tt_req_len;
1668

1669
	primary_if = batadv_primary_if_get_selected(bat_priv);
1670 1671 1672 1673
	if (!primary_if)
		goto out;

	/* The new tt_req will be issued only if I'm not waiting for a
1674 1675
	 * reply from the same orig_node yet
	 */
1676
	tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1677 1678 1679
	if (!tt_req_node)
		goto out;

1680
	skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1681 1682 1683
	if (!skb)
		goto out;

1684
	skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1685

1686 1687
	tt_req_len = sizeof(*tt_request);
	tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1688

1689
	tt_request->header.packet_type = BATADV_TT_QUERY;
1690
	tt_request->header.version = BATADV_COMPAT_VERSION;
1691 1692
	memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1693
	tt_request->header.ttl = BATADV_TTL;
1694
	tt_request->ttvn = ttvn;
1695
	tt_request->tt_data = htons(tt_crc);
1696
	tt_request->flags = BATADV_TT_REQUEST;
1697 1698

	if (full_table)
1699
		tt_request->flags |= BATADV_TT_FULL_TABLE;
1700

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

1704
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1705

1706 1707
	if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
		ret = 0;
1708 1709 1710

out:
	if (primary_if)
1711
		batadv_hardif_free_ref(primary_if);
1712 1713 1714
	if (ret)
		kfree_skb(skb);
	if (ret && tt_req_node) {
1715
		spin_lock_bh(&bat_priv->tt.req_list_lock);
1716
		list_del(&tt_req_node->list);
1717
		spin_unlock_bh(&bat_priv->tt.req_list_lock);
1718 1719 1720 1721 1722
		kfree(tt_req_node);
	}
	return ret;
}

1723
static bool
1724
batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1725
			      struct batadv_tt_query_packet *tt_request)
1726
{
1727
	struct batadv_orig_node *req_dst_orig_node;
1728 1729
	struct batadv_orig_node *res_dst_orig_node = NULL;
	struct batadv_hard_iface *primary_if = NULL;
1730 1731 1732 1733 1734 1735
	uint8_t orig_ttvn, req_ttvn, ttvn;
	int ret = false;
	unsigned char *tt_buff;
	bool full_table;
	uint16_t tt_len, tt_tot;
	struct sk_buff *skb = NULL;
1736
	struct batadv_tt_query_packet *tt_response;
1737
	uint8_t *packet_pos;
1738
	size_t len;
1739

1740
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1741 1742
		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
		   tt_request->src, tt_request->ttvn, tt_request->dst,
1743
		   (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1744 1745

	/* Let's get the orig node of the REAL destination */
1746
	req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1747 1748 1749
	if (!req_dst_orig_node)
		goto out;

1750
	res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1751 1752 1753
	if (!res_dst_orig_node)
		goto out;

1754
	primary_if = batadv_primary_if_get_selected(bat_priv);
1755 1756 1757 1758 1759 1760
	if (!primary_if)
		goto out;

	orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
	req_ttvn = tt_request->ttvn;

1761
	/* I don't have the requested data */
1762
	if (orig_ttvn != req_ttvn ||
1763
	    tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1764 1765
		goto out;

1766
	/* If the full table has been explicitly requested */
1767
	if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1768 1769 1770 1771 1772 1773
	    !req_dst_orig_node->tt_buff)
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1774 1775
	 * I'll send only one packet with as much TT entries as I can
	 */
1776 1777 1778
	if (!full_table) {
		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
		tt_len = req_dst_orig_node->tt_buff_len;
1779
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1780

1781
		len = sizeof(*tt_response) + tt_len;
1782
		skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1783 1784 1785
		if (!skb)
			goto unlock;

1786
		skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1787 1788
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1789 1790 1791
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1792
		tt_buff = skb->data + sizeof(*tt_response);
1793 1794 1795 1796 1797 1798
		/* Copy the last orig_node's OGM buffer */
		memcpy(tt_buff, req_dst_orig_node->tt_buff,
		       req_dst_orig_node->tt_buff_len);

		spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
	} else {
1799 1800
		tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
		tt_len *= sizeof(struct batadv_tt_change);
1801 1802
		ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);

1803
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1804
						    bat_priv->tt.global_hash,
1805 1806 1807
						    primary_if,
						    batadv_tt_global_valid,
						    req_dst_orig_node);
1808 1809 1810
		if (!skb)
			goto out;

1811
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1812 1813
	}

1814
	tt_response->header.packet_type = BATADV_TT_QUERY;
1815
	tt_response->header.version = BATADV_COMPAT_VERSION;
1816
	tt_response->header.ttl = BATADV_TTL;
1817 1818
	memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1819
	tt_response->flags = BATADV_TT_RESPONSE;
1820 1821

	if (full_table)
1822
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1823

1824
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1825 1826
		   "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
		   res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
1827

1828
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1829

1830 1831
	if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
		ret = true;
1832 1833 1834 1835 1836 1837 1838
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
1839
		batadv_orig_node_free_ref(res_dst_orig_node);
1840
	if (req_dst_orig_node)
1841
		batadv_orig_node_free_ref(req_dst_orig_node);
1842
	if (primary_if)
1843
		batadv_hardif_free_ref(primary_if);
1844 1845 1846 1847
	if (!ret)
		kfree_skb(skb);
	return ret;
}
1848 1849

static bool
1850
batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1851
			   struct batadv_tt_query_packet *tt_request)
1852
{
1853
	struct batadv_orig_node *orig_node;
1854
	struct batadv_hard_iface *primary_if = NULL;
1855 1856 1857 1858 1859 1860
	uint8_t my_ttvn, req_ttvn, ttvn;
	int ret = false;
	unsigned char *tt_buff;
	bool full_table;
	uint16_t tt_len, tt_tot;
	struct sk_buff *skb = NULL;
1861
	struct batadv_tt_query_packet *tt_response;
1862
	uint8_t *packet_pos;
1863
	size_t len;
1864

1865
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1866 1867
		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
		   tt_request->src, tt_request->ttvn,
1868
		   (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1869 1870


1871
	my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1872 1873
	req_ttvn = tt_request->ttvn;

1874
	orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1875 1876 1877
	if (!orig_node)
		goto out;

1878
	primary_if = batadv_primary_if_get_selected(bat_priv);
1879 1880 1881 1882
	if (!primary_if)
		goto out;

	/* If the full table has been explicitly requested or the gap
1883 1884
	 * is too big send the whole local translation table
	 */
1885
	if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1886
	    !bat_priv->tt.last_changeset)
1887 1888 1889 1890 1891
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1892 1893
	 * I'll send only one packet with as much TT entries as I can
	 */
1894
	if (!full_table) {
1895 1896
		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
		tt_len = bat_priv->tt.last_changeset_len;
1897
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1898

1899
		len = sizeof(*tt_response) + tt_len;
1900
		skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1901 1902 1903
		if (!skb)
			goto unlock;

1904
		skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1905 1906
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1907 1908 1909
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1910
		tt_buff = skb->data + sizeof(*tt_response);
1911 1912 1913
		memcpy(tt_buff, bat_priv->tt.last_changeset,
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1914
	} else {
1915
		tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1916
		tt_len *= sizeof(struct batadv_tt_change);
1917
		ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1918

1919
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1920
						    bat_priv->tt.local_hash,
1921 1922 1923
						    primary_if,
						    batadv_tt_local_valid_entry,
						    NULL);
1924 1925 1926
		if (!skb)
			goto out;

1927
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1928 1929
	}

1930
	tt_response->header.packet_type = BATADV_TT_QUERY;
1931
	tt_response->header.version = BATADV_COMPAT_VERSION;
1932
	tt_response->header.ttl = BATADV_TTL;
1933 1934
	memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1935
	tt_response->flags = BATADV_TT_RESPONSE;
1936 1937

	if (full_table)
1938
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1939

1940
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1941 1942
		   "Sending TT_RESPONSE to %pM [%c]\n",
		   orig_node->orig,
1943
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1944

1945
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1946

1947 1948
	if (batadv_send_skb_to_orig(skb, orig_node, NULL))
		ret = true;
1949 1950 1951
	goto out;

unlock:
1952
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1953 1954
out:
	if (orig_node)
1955
		batadv_orig_node_free_ref(orig_node);
1956
	if (primary_if)
1957
		batadv_hardif_free_ref(primary_if);
1958 1959 1960 1961 1962 1963
	if (!ret)
		kfree_skb(skb);
	/* This packet was for me, so it doesn't need to be re-routed */
	return true;
}

1964
bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1965
			     struct batadv_tt_query_packet *tt_request)
1966
{
1967
	if (batadv_is_my_mac(tt_request->dst)) {
1968
		/* don't answer backbone gws! */
1969
		if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1970 1971
			return true;

1972
		return batadv_send_my_tt_response(bat_priv, tt_request);
1973
	} else {
1974
		return batadv_send_other_tt_response(bat_priv, tt_request);
1975
	}
1976 1977
}

1978 1979
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
1980
				      struct batadv_tt_change *tt_change,
1981
				      uint16_t tt_num_changes, uint8_t ttvn)
1982 1983
{
	int i;
1984
	int roams;
1985 1986

	for (i = 0; i < tt_num_changes; i++) {
1987 1988
		if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
			roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1989 1990
			batadv_tt_global_del(bat_priv, orig_node,
					     (tt_change + i)->addr,
1991 1992
					     "tt removed by changes",
					     roams);
1993 1994
		} else {
			if (!batadv_tt_global_add(bat_priv, orig_node,
1995 1996
						  (tt_change + i)->addr,
						  (tt_change + i)->flags, ttvn))
1997 1998 1999 2000 2001 2002 2003
				/* 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;
2004
		}
2005
	}
2006
	orig_node->tt_initialised = true;
2007 2008
}

2009
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2010
				  struct batadv_tt_query_packet *tt_response)
2011
{
2012
	struct batadv_orig_node *orig_node;
2013

2014
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2015 2016 2017 2018
	if (!orig_node)
		goto out;

	/* Purge the old table first.. */
2019
	batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
2020

2021
	_batadv_tt_update_changes(bat_priv, orig_node,
2022
				  (struct batadv_tt_change *)(tt_response + 1),
2023 2024
				  ntohs(tt_response->tt_data),
				  tt_response->ttvn);
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035

	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);

	atomic_set(&orig_node->last_ttvn, tt_response->ttvn);

out:
	if (orig_node)
2036
		batadv_orig_node_free_ref(orig_node);
2037 2038
}

2039 2040
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
2041
				     uint16_t tt_num_changes, uint8_t ttvn,
2042
				     struct batadv_tt_change *tt_change)
2043
{
2044 2045
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
2046

2047 2048
	batadv_tt_save_orig_buffer(bat_priv, orig_node,
				   (unsigned char *)tt_change, tt_num_changes);
2049 2050 2051
	atomic_set(&orig_node->last_ttvn, ttvn);
}

2052
bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2053
{
2054
	struct batadv_tt_local_entry *tt_local_entry;
2055
	bool ret = false;
2056

2057
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2058 2059
	if (!tt_local_entry)
		goto out;
2060
	/* Check if the client has been logically deleted (but is kept for
2061 2062
	 * consistency purpose)
	 */
2063 2064
	if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
	    (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2065
		goto out;
2066 2067
	ret = true;
out:
2068
	if (tt_local_entry)
2069
		batadv_tt_local_entry_free_ref(tt_local_entry);
2070
	return ret;
2071 2072
}

2073
void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2074
			       struct batadv_tt_query_packet *tt_response)
2075
{
2076 2077
	struct batadv_tt_req_node *node, *safe;
	struct batadv_orig_node *orig_node = NULL;
2078
	struct batadv_tt_change *tt_change;
2079

2080
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2081 2082 2083
		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
		   tt_response->src, tt_response->ttvn,
		   ntohs(tt_response->tt_data),
2084
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2085

2086
	/* we should have never asked a backbone gw */
2087
	if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2088 2089
		goto out;

2090
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2091 2092 2093
	if (!orig_node)
		goto out;

2094
	if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2095
		batadv_tt_fill_gtable(bat_priv, tt_response);
2096 2097
	} else {
		tt_change = (struct batadv_tt_change *)(tt_response + 1);
2098 2099
		batadv_tt_update_changes(bat_priv, orig_node,
					 ntohs(tt_response->tt_data),
2100 2101
					 tt_response->ttvn, tt_change);
	}
2102 2103

	/* Delete the tt_req_node from pending tt_requests list */
2104 2105
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2106
		if (!batadv_compare_eth(node->addr, tt_response->src))
2107 2108 2109 2110
			continue;
		list_del(&node->list);
		kfree(node);
	}
2111
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2112 2113

	/* Recalculate the CRC for this orig_node and store it */
2114
	orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2115 2116
out:
	if (orig_node)
2117
		batadv_orig_node_free_ref(orig_node);
2118 2119
}

2120
int batadv_tt_init(struct batadv_priv *bat_priv)
2121
{
2122
	int ret;
2123

2124
	ret = batadv_tt_local_init(bat_priv);
2125 2126 2127
	if (ret < 0)
		return ret;

2128
	ret = batadv_tt_global_init(bat_priv);
2129 2130
	if (ret < 0)
		return ret;
2131

2132 2133 2134
	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));
2135 2136 2137 2138

	return 1;
}

2139
static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2140
{
2141
	struct batadv_tt_roam_node *node, *safe;
2142

2143
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2144

2145
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2146 2147 2148 2149
		list_del(&node->list);
		kfree(node);
	}

2150
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2151 2152
}

2153
static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2154
{
2155
	struct batadv_tt_roam_node *node, *safe;
2156

2157 2158
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2159 2160
		if (!batadv_has_timed_out(node->first_time,
					  BATADV_ROAMING_MAX_TIME))
2161 2162 2163 2164 2165
			continue;

		list_del(&node->list);
		kfree(node);
	}
2166
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2167 2168 2169 2170 2171 2172
}

/* This function checks whether the client already reached the
 * maximum number of possible roaming phases. In this case the ROAMING_ADV
 * will not be sent.
 *
2173 2174
 * returns true if the ROAMING_ADV can be sent, false otherwise
 */
2175
static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2176
				       uint8_t *client)
2177
{
2178
	struct batadv_tt_roam_node *tt_roam_node;
2179 2180
	bool ret = false;

2181
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2182
	/* The new tt_req will be issued only if I'm not waiting for a
2183 2184
	 * reply from the same orig_node yet
	 */
2185
	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2186
		if (!batadv_compare_eth(tt_roam_node->addr, client))
2187 2188
			continue;

2189
		if (batadv_has_timed_out(tt_roam_node->first_time,
2190
					 BATADV_ROAMING_MAX_TIME))
2191 2192
			continue;

2193
		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
			/* 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;
2206 2207
		atomic_set(&tt_roam_node->counter,
			   BATADV_ROAMING_MAX_COUNT - 1);
2208 2209
		memcpy(tt_roam_node->addr, client, ETH_ALEN);

2210
		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2211 2212 2213 2214
		ret = true;
	}

unlock:
2215
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2216 2217 2218
	return ret;
}

2219 2220
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node)
2221 2222
{
	struct sk_buff *skb = NULL;
2223
	struct batadv_roam_adv_packet *roam_adv_packet;
2224
	int ret = 1;
2225
	struct batadv_hard_iface *primary_if;
2226
	size_t len = sizeof(*roam_adv_packet);
2227 2228

	/* before going on we have to check whether the client has
2229 2230
	 * already roamed to us too many times
	 */
2231
	if (!batadv_tt_check_roam_count(bat_priv, client))
2232 2233
		goto out;

2234
	skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2235 2236 2237
	if (!skb)
		goto out;

2238
	skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2239

2240
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2241

2242
	roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2243
	roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2244
	roam_adv_packet->header.ttl = BATADV_TTL;
2245
	roam_adv_packet->reserved = 0;
2246
	primary_if = batadv_primary_if_get_selected(bat_priv);
2247 2248 2249
	if (!primary_if)
		goto out;
	memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2250
	batadv_hardif_free_ref(primary_if);
2251 2252 2253
	memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
	memcpy(roam_adv_packet->client, client, ETH_ALEN);

2254
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2255 2256
		   "Sending ROAMING_ADV to %pM (client %pM)\n",
		   orig_node->orig, client);
2257

2258
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2259

2260 2261
	if (batadv_send_skb_to_orig(skb, orig_node, NULL))
		ret = 0;
2262 2263

out:
2264
	if (ret && skb)
2265 2266
		kfree_skb(skb);
	return;
2267 2268
}

2269
static void batadv_tt_purge(struct work_struct *work)
2270
{
2271
	struct delayed_work *delayed_work;
2272
	struct batadv_priv_tt *priv_tt;
2273 2274 2275
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
2276 2277
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2278

2279
	batadv_tt_local_purge(bat_priv);
2280
	batadv_tt_global_purge(bat_priv);
2281 2282
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
2283

2284 2285
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2286
}
2287

2288
void batadv_tt_free(struct batadv_priv *bat_priv)
2289
{
2290
	cancel_delayed_work_sync(&bat_priv->tt.work);
2291

2292 2293 2294 2295 2296
	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);
2297

2298
	kfree(bat_priv->tt.last_changeset);
2299
}
2300

2301
/* This function will enable or disable the specified flags for all the entries
2302 2303
 * in the given hash table and returns the number of modified entries
 */
2304 2305
static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
				    uint16_t flags, bool enable)
2306
{
2307
	uint32_t i;
2308
	uint16_t changed_num = 0;
2309 2310
	struct hlist_head *head;
	struct hlist_node *node;
2311
	struct batadv_tt_common_entry *tt_common_entry;
2312 2313

	if (!hash)
2314
		goto out;
2315 2316 2317 2318 2319

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

		rcu_read_lock();
2320
		hlist_for_each_entry_rcu(tt_common_entry, node,
2321
					 head, hash_entry) {
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
			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++;
2332 2333 2334
		}
		rcu_read_unlock();
	}
2335 2336
out:
	return changed_num;
2337 2338
}

2339
/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2340
static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2341
{
2342
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2343 2344
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_local_entry *tt_local;
2345 2346 2347
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
	spinlock_t *list_lock; /* protects write access to the hash lists */
2348
	uint32_t i;
2349 2350 2351 2352 2353 2354 2355 2356 2357

	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);
2358 2359 2360
		hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2361 2362
				continue;

2363
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2364
				   "Deleting local tt entry (%pM): pending\n",
2365
				   tt_common->addr);
2366

2367
			atomic_dec(&bat_priv->tt.local_entry_num);
2368
			hlist_del_rcu(node);
2369 2370 2371 2372
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
2373 2374 2375 2376 2377
		}
		spin_unlock_bh(list_lock);
	}
}

2378
static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2379 2380
				    unsigned char **packet_buff,
				    int *packet_buff_len, int packet_min_len)
2381
{
2382 2383
	uint16_t changed_num = 0;

2384
	if (atomic_read(&bat_priv->tt.local_changes) < 1)
2385 2386
		return -ENOENT;

2387
	changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2388
					  BATADV_TT_CLIENT_NEW, false);
2389 2390

	/* all reset entries have to be counted as local entries */
2391
	atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2392
	batadv_tt_local_purge_pending_clients(bat_priv);
2393
	bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2394 2395

	/* Increment the TTVN only once per OGM interval */
2396
	atomic_inc(&bat_priv->tt.vn);
2397
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2398
		   "Local changes committed, updating to ttvn %u\n",
2399
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
2400 2401

	/* reset the sending counter */
2402
	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2403

2404 2405
	return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
					   packet_buff_len, packet_min_len);
2406 2407 2408
}

/* when calling this function (hard_iface == primary_if) has to be true */
2409
int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2410 2411 2412 2413 2414 2415
			  unsigned char **packet_buff, int *packet_buff_len,
			  int packet_min_len)
{
	int tt_num_changes;

	/* if at least one change happened */
2416 2417 2418
	tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
						  packet_buff_len,
						  packet_min_len);
2419 2420 2421

	/* if the changes have been sent often enough */
	if ((tt_num_changes < 0) &&
2422
	    (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2423 2424
		batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
					      packet_min_len, packet_min_len);
2425 2426 2427 2428
		tt_num_changes = 0;
	}

	return tt_num_changes;
2429
}
2430

2431
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2432
			   uint8_t *dst)
2433
{
2434 2435
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
2436
	bool ret = false;
2437 2438

	if (!atomic_read(&bat_priv->ap_isolation))
2439
		goto out;
2440

2441
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2442 2443 2444
	if (!tt_local_entry)
		goto out;

2445
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2446 2447 2448
	if (!tt_global_entry)
		goto out;

2449
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2450 2451
		goto out;

2452
	ret = true;
2453 2454 2455

out:
	if (tt_global_entry)
2456
		batadv_tt_global_entry_free_ref(tt_global_entry);
2457
	if (tt_local_entry)
2458
		batadv_tt_local_entry_free_ref(tt_local_entry);
2459 2460
	return ret;
}
2461

2462 2463
void batadv_tt_update_orig(struct batadv_priv *bat_priv,
			   struct batadv_orig_node *orig_node,
2464 2465
			   const unsigned char *tt_buff, uint8_t tt_num_changes,
			   uint8_t ttvn, uint16_t tt_crc)
2466 2467 2468
{
	uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	bool full_table = true;
2469
	struct batadv_tt_change *tt_change;
2470

2471
	/* don't care about a backbone gateways updates. */
2472
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2473 2474
		return;

2475
	/* orig table not initialised AND first diff is in the OGM OR the ttvn
2476 2477
	 * increased by one -> we can apply the attached changes
	 */
2478 2479
	if ((!orig_node->tt_initialised && ttvn == 1) ||
	    ttvn - orig_ttvn == 1) {
2480
		/* the OGM could not contain the changes due to their size or
2481 2482
		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
		 * times.
2483 2484
		 * In this case send a tt request
		 */
2485 2486 2487 2488 2489
		if (!tt_num_changes) {
			full_table = false;
			goto request_table;
		}

2490
		tt_change = (struct batadv_tt_change *)tt_buff;
2491
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2492
					 ttvn, tt_change);
2493 2494 2495

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
2496 2497
		 * in the global table
		 */
2498
		orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2499 2500 2501 2502 2503 2504 2505 2506

		/* 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
2507 2508
		 * inconsistency
		 */
2509 2510 2511 2512
		if (orig_node->tt_crc != tt_crc)
			goto request_table;
	} else {
		/* if we missed more than one change or our tables are not
2513 2514
		 * in sync anymore -> request fresh tt data
		 */
2515 2516
		if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
		    orig_node->tt_crc != tt_crc) {
2517
request_table:
2518
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2519
				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2520 2521
				   orig_node->orig, ttvn, orig_ttvn, tt_crc,
				   orig_node->tt_crc, tt_num_changes);
2522 2523
			batadv_send_tt_request(bat_priv, orig_node, ttvn,
					       tt_crc, full_table);
2524 2525 2526 2527
			return;
		}
	}
}
2528 2529 2530 2531 2532

/* returns true whether we know that the client has moved from its old
 * originator to another one. This entry is kept is still kept for consistency
 * purposes
 */
2533
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2534
					uint8_t *addr)
2535
{
2536
	struct batadv_tt_global_entry *tt_global_entry;
2537 2538
	bool ret = false;

2539
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2540 2541 2542
	if (!tt_global_entry)
		goto out;

2543
	ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2544
	batadv_tt_global_entry_free_ref(tt_global_entry);
2545 2546 2547
out:
	return ret;
}
2548

2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
/**
 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
 * @bat_priv: the bat priv with all the soft interface information
 * @addr: the MAC address of the local client to query
 *
 * Returns true if the local client is known to be roaming (it is not served by
 * 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,
				       uint8_t *addr)
{
	struct batadv_tt_local_entry *tt_local_entry;
	bool ret = false;

	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
	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;
}

2574 2575 2576 2577 2578 2579
bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
					  struct batadv_orig_node *orig_node,
					  const unsigned char *addr)
{
	bool ret = false;

2580 2581 2582 2583 2584 2585 2586
	/* if the originator is a backbone node (meaning it belongs to the same
	 * LAN of this node) the temporary client must not be added because to
	 * reach such destination the node must use the LAN instead of the mesh
	 */
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
		goto out;

2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
	if (!batadv_tt_global_add(bat_priv, orig_node, addr,
				  BATADV_TT_CLIENT_TEMP,
				  atomic_read(&orig_node->last_ttvn)))
		goto out;

	batadv_dbg(BATADV_DBG_TT, bat_priv,
		   "Added temporary global client (addr: %pM orig: %pM)\n",
		   addr, orig_node->orig);
	ret = true;
out:
	return ret;
}