translation-table.c 69.4 KB
Newer Older
1
/* Copyright (C) 2007-2012 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
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node);
34 35
static void batadv_tt_purge(struct work_struct *work);
static void
36
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
37 38 39 40
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);
41

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

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

51
static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
52
{
53 54
	INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
55
			   msecs_to_jiffies(5000));
56 57
}

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

	if (!hash)
		return NULL;

70
	index = batadv_choose_orig(data, hash->size);
71 72 73
	head = &hash->table[index];

	rcu_read_lock();
74
	hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
75
		if (!batadv_compare_eth(tt_common_entry, data))
76 77
			continue;

78
		if (!atomic_inc_not_zero(&tt_common_entry->refcount))
79 80
			continue;

81
		tt_common_entry_tmp = tt_common_entry;
82 83 84 85
		break;
	}
	rcu_read_unlock();

86
	return tt_common_entry_tmp;
87 88
}

89 90
static struct batadv_tt_local_entry *
batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
91
{
92 93
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local_entry = NULL;
94

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

103 104
static struct batadv_tt_global_entry *
batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
105
{
106 107
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
108

109
	tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
110 111
	if (tt_common_entry)
		tt_global_entry = container_of(tt_common_entry,
112 113
					       struct batadv_tt_global_entry,
					       common);
114
	return tt_global_entry;
115 116 117

}

118
static void
119
batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
120
{
121 122
	if (atomic_dec_and_test(&tt_local_entry->common.refcount))
		kfree_rcu(tt_local_entry, common.rcu);
123 124
}

125
static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
126
{
127 128
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global_entry;
129

130 131 132
	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);
133 134 135 136

	kfree(tt_global_entry);
}

137
static void
138
batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
139
{
140
	if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
141
		batadv_tt_global_del_orig_list(tt_global_entry);
142
		call_rcu(&tt_global_entry->common.rcu,
143
			 batadv_tt_global_entry_free_rcu);
144 145 146
	}
}

147
static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
148
{
149
	struct batadv_tt_orig_list_entry *orig_entry;
150

151
	orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
152
	batadv_orig_node_free_ref(orig_entry->orig_node);
153 154 155
	kfree(orig_entry);
}

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

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

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

	if (!tt_change_node)
		return;

178
	tt_change_node->change.flags = flags;
179 180
	memcpy(tt_change_node->change.addr, addr, ETH_ALEN);

181
	del_op_requested = flags & BATADV_TT_CLIENT_DEL;
182 183

	/* check for ADD+DEL or DEL+ADD events */
184 185
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
186 187 188 189 190 191 192 193 194 195 196
				 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
		 */
197
		del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
198 199 200 201 202 203 204 205
		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);
206
		kfree(tt_change_node);
207 208 209 210
		event_removed = true;
		goto unlock;
	}

211
	/* track the change in the OGMinterval list */
212
	list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
213 214

unlock:
215
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
216

217
	if (event_removed)
218
		atomic_dec(&bat_priv->tt.local_changes);
219
	else
220
		atomic_inc(&bat_priv->tt.local_changes);
221 222
}

223
int batadv_tt_len(int changes_num)
224
{
225
	return changes_num * sizeof(struct batadv_tt_change);
226 227
}

228
static int batadv_tt_local_init(struct batadv_priv *bat_priv)
229
{
230
	if (bat_priv->tt.local_hash)
231
		return 0;
232

233
	bat_priv->tt.local_hash = batadv_hash_new(1024);
234

235
	if (!bat_priv->tt.local_hash)
236
		return -ENOMEM;
237

238
	return 0;
239 240
}

241 242
void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
			 int ifindex)
243
{
244 245 246
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
247 248
	struct hlist_head *head;
	struct hlist_node *node;
249
	struct batadv_tt_orig_list_entry *orig_entry;
250
	int hash_added;
251

252
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
253

254 255
	if (tt_local_entry) {
		tt_local_entry->last_seen = jiffies;
256 257
		/* possibly unset the BATADV_TT_CLIENT_PENDING flag */
		tt_local_entry->common.flags &= ~BATADV_TT_CLIENT_PENDING;
258
		goto out;
259 260
	}

261
	tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
262
	if (!tt_local_entry)
263
		goto out;
264

265
	batadv_dbg(BATADV_DBG_TT, bat_priv,
266
		   "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
267
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
268

269
	memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
270
	tt_local_entry->common.flags = BATADV_NO_FLAGS;
271
	if (batadv_is_wifi_iface(ifindex))
272
		tt_local_entry->common.flags |= BATADV_TT_CLIENT_WIFI;
273 274
	atomic_set(&tt_local_entry->common.refcount, 2);
	tt_local_entry->last_seen = jiffies;
275
	tt_local_entry->common.added_at = tt_local_entry->last_seen;
276 277

	/* the batman interface mac address should never be purged */
278
	if (batadv_compare_eth(addr, soft_iface->dev_addr))
279
		tt_local_entry->common.flags |= BATADV_TT_CLIENT_NOPURGE;
280

281 282
	/* 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
283 284
	 * (consistency check)
	 */
285
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_NEW;
286

287
	hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
288 289
				     batadv_choose_orig,
				     &tt_local_entry->common,
290
				     &tt_local_entry->common.hash_entry);
291 292 293

	if (unlikely(hash_added != 0)) {
		/* remove the reference for the hash */
294
		batadv_tt_local_entry_free_ref(tt_local_entry);
295 296 297
		goto out;
	}

298
	batadv_tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
299

300
	/* remove address from global hash if present */
301
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
302

303 304
	/* Check whether it is a roaming! */
	if (tt_global_entry) {
305 306 307 308 309 310
		/* These node are probably going to update their tt table */
		head = &tt_global_entry->orig_list;
		rcu_read_lock();
		hlist_for_each_entry_rcu(orig_entry, node, head, list) {
			orig_entry->orig_node->tt_poss_change = true;

311 312 313
			batadv_send_roam_adv(bat_priv,
					     tt_global_entry->common.addr,
					     orig_entry->orig_node);
314 315 316 317 318
		}
		rcu_read_unlock();
		/* The global entry has to be marked as ROAMING and
		 * has to be kept for consistency purpose
		 */
319
		tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
320
		tt_global_entry->roam_at = jiffies;
321 322 323
	}
out:
	if (tt_local_entry)
324
		batadv_tt_local_entry_free_ref(tt_local_entry);
325
	if (tt_global_entry)
326
		batadv_tt_global_entry_free_ref(tt_global_entry);
327 328
}

329 330 331 332
static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
					  int *packet_buff_len,
					  int min_packet_len,
					  int new_packet_len)
333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
	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;
	}
}

347
static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
348 349 350
					  unsigned char **packet_buff,
					  int *packet_buff_len,
					  int min_packet_len)
351
{
352
	struct batadv_hard_iface *primary_if;
353 354
	int req_len;

355
	primary_if = batadv_primary_if_get_selected(bat_priv);
356 357

	req_len = min_packet_len;
358
	req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
359 360 361 362 363 364 365

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

366 367
	batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
				      min_packet_len, req_len);
368 369

	if (primary_if)
370
		batadv_hardif_free_ref(primary_if);
371 372
}

373
static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
374 375 376
				       unsigned char **packet_buff,
				       int *packet_buff_len,
				       int min_packet_len)
377
{
378
	struct batadv_tt_change_node *entry, *safe;
379 380 381
	int count = 0, tot_changes = 0, new_len;
	unsigned char *tt_buff;

382 383
	batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
				      packet_buff_len, min_packet_len);
384

385 386 387 388
	new_len = *packet_buff_len - min_packet_len;
	tt_buff = *packet_buff + min_packet_len;

	if (new_len > 0)
389
		tot_changes = new_len / batadv_tt_len(1);
390

391 392
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	atomic_set(&bat_priv->tt.local_changes, 0);
393

394
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
395
				 list) {
396
		if (count < tot_changes) {
397
			memcpy(tt_buff + batadv_tt_len(count),
398
			       &entry->change, sizeof(struct batadv_tt_change));
399 400
			count++;
		}
401 402
		list_del(&entry->list);
		kfree(entry);
403
	}
404
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
405 406

	/* Keep the buffer for possible tt_request */
407 408 409 410
	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;
411 412 413
	/* 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
414 415
		 * instead of providing the diff
		 */
416 417 418 419
		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;
420 421
		}
	}
422
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
423

424
	return count;
425 426
}

427
int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
428 429
{
	struct net_device *net_dev = (struct net_device *)seq->private;
430
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
431
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
432 433
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_hard_iface *primary_if;
434
	struct hlist_node *node;
435
	struct hlist_head *head;
436 437
	uint32_t i;
	int ret = 0;
438

439
	primary_if = batadv_primary_if_get_selected(bat_priv);
440
	if (!primary_if) {
441 442
		ret = seq_printf(seq,
				 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
443 444 445
				 net_dev->name);
		goto out;
	}
446

447
	if (primary_if->if_status != BATADV_IF_ACTIVE) {
448 449
		ret = seq_printf(seq,
				 "BATMAN mesh %s disabled - primary interface not active\n",
450 451
				 net_dev->name);
		goto out;
452 453
	}

454 455
	seq_printf(seq,
		   "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
456
		   net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
457 458 459 460

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

461
		rcu_read_lock();
462
		hlist_for_each_entry_rcu(tt_common_entry, node,
463
					 head, hash_entry) {
464
			seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
465 466
				   tt_common_entry->addr,
				   (tt_common_entry->flags &
467
				    BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
468
				   (tt_common_entry->flags &
469
				    BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
470
				   (tt_common_entry->flags &
471
				    BATADV_TT_CLIENT_NEW ? 'N' : '.'),
472
				   (tt_common_entry->flags &
473
				    BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
474
				   (tt_common_entry->flags &
475
				    BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
476
		}
477
		rcu_read_unlock();
478
	}
479 480
out:
	if (primary_if)
481
		batadv_hardif_free_ref(primary_if);
482
	return ret;
483 484
}

485 486 487 488
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)
489
{
490 491
	batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
			      tt_local_entry->common.flags | flags);
492

493 494
	/* 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
495 496
	 * response issued before the net ttvn increment (consistency check)
	 */
497
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
498

499
	batadv_dbg(BATADV_DBG_TT, bat_priv,
500 501
		   "Local tt entry (%pM) pending to be removed: %s\n",
		   tt_local_entry->common.addr, message);
502 503
}

504
void batadv_tt_local_remove(struct batadv_priv *bat_priv, const uint8_t *addr,
505
			    const char *message, bool roaming)
506
{
507
	struct batadv_tt_local_entry *tt_local_entry = NULL;
508
	uint16_t flags;
509

510
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
511 512 513
	if (!tt_local_entry)
		goto out;

514
	flags = BATADV_TT_CLIENT_DEL;
515
	if (roaming)
516
		flags |= BATADV_TT_CLIENT_ROAM;
517 518

	batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
519 520
out:
	if (tt_local_entry)
521
		batadv_tt_local_entry_free_ref(tt_local_entry);
522 523
}

524
static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
525
				       struct hlist_head *head)
526
{
527 528
	struct batadv_tt_local_entry *tt_local_entry;
	struct batadv_tt_common_entry *tt_common_entry;
529
	struct hlist_node *node, *node_tmp;
530 531 532 533

	hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
				  hash_entry) {
		tt_local_entry = container_of(tt_common_entry,
534 535
					      struct batadv_tt_local_entry,
					      common);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
		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");
	}
}

552
static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
553
{
554
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
555
	struct hlist_head *head;
556
	spinlock_t *list_lock; /* protects write access to the hash lists */
557
	uint32_t i;
558 559 560

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

563
		spin_lock_bh(list_lock);
564
		batadv_tt_local_purge_list(bat_priv, head);
565
		spin_unlock_bh(list_lock);
566 567 568 569
	}

}

570
static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
571
{
572
	struct batadv_hashtable *hash;
573
	spinlock_t *list_lock; /* protects write access to the hash lists */
574 575
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local;
576 577
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
578
	uint32_t i;
579

580
	if (!bat_priv->tt.local_hash)
581 582
		return;

583
	hash = bat_priv->tt.local_hash;
584 585 586 587 588 589

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

		spin_lock_bh(list_lock);
590
		hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
591 592
					  head, hash_entry) {
			hlist_del_rcu(node);
593 594 595 596
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
597 598 599 600
		}
		spin_unlock_bh(list_lock);
	}

601
	batadv_hash_destroy(hash);
602

603
	bat_priv->tt.local_hash = NULL;
604 605
}

606
static int batadv_tt_global_init(struct batadv_priv *bat_priv)
607
{
608
	if (bat_priv->tt.global_hash)
609
		return 0;
610

611
	bat_priv->tt.global_hash = batadv_hash_new(1024);
612

613
	if (!bat_priv->tt.global_hash)
614
		return -ENOMEM;
615

616
	return 0;
617 618
}

619
static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
620
{
621
	struct batadv_tt_change_node *entry, *safe;
622

623
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
624

625
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
626 627 628 629
				 list) {
		list_del(&entry->list);
		kfree(entry);
	}
630

631 632
	atomic_set(&bat_priv->tt.local_changes, 0);
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
633
}
634

635 636 637 638
/* 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
639
 */
640 641 642
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)
643
{
644
	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
645 646 647 648 649 650
	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) {
651 652 653 654 655 656 657
		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;
658 659
	}
	rcu_read_unlock();
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

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

680 681 682
	return found;
}

683
static void
684
batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
685
				struct batadv_orig_node *orig_node, int ttvn)
686
{
687
	struct batadv_tt_orig_list_entry *orig_entry;
688

689
	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
690 691 692 693 694
	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;
695
		goto out;
696
	}
697

698 699
	orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
	if (!orig_entry)
700
		goto out;
701 702 703 704 705 706

	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;
707
	atomic_set(&orig_entry->refcount, 2);
708

709
	spin_lock_bh(&tt_global->list_lock);
710
	hlist_add_head_rcu(&orig_entry->list,
711 712 713 714 715
			   &tt_global->orig_list);
	spin_unlock_bh(&tt_global->list_lock);
out:
	if (orig_entry)
		batadv_tt_orig_list_entry_free_ref(orig_entry);
716 717
}

718
/* caller must hold orig_node refcount */
719 720
int batadv_tt_global_add(struct batadv_priv *bat_priv,
			 struct batadv_orig_node *orig_node,
721 722
			 const unsigned char *tt_addr, uint8_t flags,
			 uint8_t ttvn)
723
{
724
	struct batadv_tt_global_entry *tt_global_entry = NULL;
725
	int ret = 0;
726
	int hash_added;
727
	struct batadv_tt_common_entry *common;
728

729
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
730 731

	if (!tt_global_entry) {
732
		tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
733
		if (!tt_global_entry)
734 735
			goto out;

736 737
		common = &tt_global_entry->common;
		memcpy(common->addr, tt_addr, ETH_ALEN);
738

739
		common->flags = flags;
740
		tt_global_entry->roam_at = 0;
741
		atomic_set(&common->refcount, 2);
742
		common->added_at = jiffies;
743 744 745

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

747
		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
748 749 750
					     batadv_compare_tt,
					     batadv_choose_orig, common,
					     &common->hash_entry);
751 752 753

		if (unlikely(hash_added != 0)) {
			/* remove the reference for the hash */
754
			batadv_tt_global_entry_free_ref(tt_global_entry);
755 756
			goto out_remove;
		}
757
	} else {
758 759 760 761 762 763 764 765 766 767 768 769 770
		/* If there is already a global entry, we can use this one for
		 * our processing.
		 * But if we are trying to add a temporary client we can exit
		 * directly because the temporary information should never
		 * override any already known client state (whatever it is)
		 */
		if (flags & BATADV_TT_CLIENT_TEMP)
			goto out;

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

772 773 774 775 776 777
		/* 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;

778 779
		/* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
		 * one originator left in the list and we previously received a
780 781 782 783 784
		 * delete + roaming change for this originator.
		 *
		 * We should first delete the old originator before adding the
		 * new one.
		 */
785
		if (tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM) {
786
			batadv_tt_global_del_orig_list(tt_global_entry);
787
			tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
788
			tt_global_entry->roam_at = 0;
789 790
		}
	}
791
	/* add the new orig_entry (if needed) or update it */
792
	batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
793

794
	batadv_dbg(BATADV_DBG_TT, bat_priv,
795 796
		   "Creating new global tt entry: %pM (via %pM)\n",
		   tt_global_entry->common.addr, orig_node->orig);
797

798
out_remove:
799
	/* remove address from local hash if present */
800
	batadv_tt_local_remove(bat_priv, tt_global_entry->common.addr,
801 802
			       "global tt received",
			       flags & BATADV_TT_CLIENT_ROAM);
803 804 805
	ret = 1;
out:
	if (tt_global_entry)
806
		batadv_tt_global_entry_free_ref(tt_global_entry);
807
	return ret;
808 809
}

810 811 812
/* print all orig nodes who announce the address for this global entry.
 * it is assumed that the caller holds rcu_read_lock();
 */
813
static void
814
batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
815
			     struct seq_file *seq)
816 817 818
{
	struct hlist_head *head;
	struct hlist_node *node;
819 820
	struct batadv_tt_orig_list_entry *orig_entry;
	struct batadv_tt_common_entry *tt_common_entry;
821 822 823 824 825 826 827 828 829 830
	uint16_t flags;
	uint8_t last_ttvn;

	tt_common_entry = &tt_global_entry->common;

	head = &tt_global_entry->orig_list;

	hlist_for_each_entry_rcu(orig_entry, node, head, list) {
		flags = tt_common_entry->flags;
		last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
831
		seq_printf(seq,	" * %pM  (%3u) via %pM     (%3u)   [%c%c%c]\n",
832 833
			   tt_global_entry->common.addr, orig_entry->ttvn,
			   orig_entry->orig_node->orig, last_ttvn,
834
			   (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
835 836
			   (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
			   (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
837 838 839
	}
}

840
int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
841 842
{
	struct net_device *net_dev = (struct net_device *)seq->private;
843
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
844
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
845 846 847
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
	struct batadv_hard_iface *primary_if;
848
	struct hlist_node *node;
849
	struct hlist_head *head;
850 851
	uint32_t i;
	int ret = 0;
852

853
	primary_if = batadv_primary_if_get_selected(bat_priv);
854
	if (!primary_if) {
855 856
		ret = seq_printf(seq,
				 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
857 858 859
				 net_dev->name);
		goto out;
	}
860

861
	if (primary_if->if_status != BATADV_IF_ACTIVE) {
862 863
		ret = seq_printf(seq,
				 "BATMAN mesh %s disabled - primary interface not active\n",
864 865
				 net_dev->name);
		goto out;
866 867
	}

868 869
	seq_printf(seq,
		   "Globally announced TT entries received via the mesh %s\n",
870
		   net_dev->name);
871 872
	seq_printf(seq, "       %-13s %s       %-15s %s %s\n",
		   "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
873 874 875 876

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

877
		rcu_read_lock();
878
		hlist_for_each_entry_rcu(tt_common_entry, node,
879
					 head, hash_entry) {
880 881 882 883
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_print_entry(tt_global, seq);
884
		}
885
		rcu_read_unlock();
886
	}
887 888
out:
	if (primary_if)
889
		batadv_hardif_free_ref(primary_if);
890
	return ret;
891 892
}

893
/* deletes the orig list of a tt_global_entry */
894
static void
895
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
896
{
897 898
	struct hlist_head *head;
	struct hlist_node *node, *safe;
899
	struct batadv_tt_orig_list_entry *orig_entry;
900

901 902 903 904
	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);
905
		batadv_tt_orig_list_entry_free_ref(orig_entry);
906 907
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
908

909 910
}

911
static void
912 913 914
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,
915
				const char *message)
916 917 918
{
	struct hlist_head *head;
	struct hlist_node *node, *safe;
919
	struct batadv_tt_orig_list_entry *orig_entry;
920 921 922 923 924

	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) {
925
			batadv_dbg(BATADV_DBG_TT, bat_priv,
926 927 928
				   "Deleting %pM from global tt entry %pM: %s\n",
				   orig_node->orig,
				   tt_global_entry->common.addr, message);
929
			hlist_del_rcu(node);
930
			batadv_tt_orig_list_entry_free_ref(orig_entry);
931 932 933 934 935
		}
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
}

936 937 938 939
static void
batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
			    struct batadv_tt_global_entry *tt_global_entry,
			    const char *message)
940
{
941 942
	batadv_dbg(BATADV_DBG_TT, bat_priv,
		   "Deleting global tt entry %pM: %s\n",
943
		   tt_global_entry->common.addr, message);
944

945
	batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
946
			   batadv_choose_orig, tt_global_entry->common.addr);
947
	batadv_tt_global_entry_free_ref(tt_global_entry);
948

949 950
}

951
/* If the client is to be deleted, we check if it is the last origantor entry
952 953
 * 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.
954
 */
955
static void
956 957 958 959
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)
960 961 962 963
{
	bool last_entry = true;
	struct hlist_head *head;
	struct hlist_node *node;
964
	struct batadv_tt_orig_list_entry *orig_entry;
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

	/* 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. */
982
		tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
983 984 985 986 987
		tt_global_entry->roam_at = jiffies;
	} else
		/* there is another entry, we can simply delete this
		 * one and can still use the other one.
		 */
988 989
		batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
						orig_node, message);
990 991 992 993
}



994 995
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
996 997
				 const unsigned char *addr,
				 const char *message, bool roaming)
998
{
999 1000
	struct batadv_tt_global_entry *tt_global_entry = NULL;
	struct batadv_tt_local_entry *local_entry = NULL;
1001

1002
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1003
	if (!tt_global_entry)
1004
		goto out;
1005

1006
	if (!roaming) {
1007 1008
		batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
						orig_node, message);
1009 1010

		if (hlist_empty(&tt_global_entry->orig_list))
1011 1012
			batadv_tt_global_del_struct(bat_priv, tt_global_entry,
						    message);
1013 1014 1015

		goto out;
	}
1016 1017 1018

	/* if we are deleting a global entry due to a roam
	 * event, there are two possibilities:
1019 1020
	 * 1) the client roamed from node A to node B => if there
	 *    is only one originator left for this client, we mark
1021
	 *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1022 1023
	 *    wait for node B to claim it. In case of timeout
	 *    the entry is purged.
1024 1025 1026
	 *
	 *    If there are other originators left, we directly delete
	 *    the originator.
1027
	 * 2) the client roamed to us => we can directly delete
1028 1029
	 *    the global entry, since it is useless now.
	 */
1030 1031 1032
	local_entry = batadv_tt_local_hash_find(bat_priv,
						tt_global_entry->common.addr);
	if (local_entry) {
1033
		/* local entry exists, case 2: client roamed to us. */
1034 1035
		batadv_tt_global_del_orig_list(tt_global_entry);
		batadv_tt_global_del_struct(bat_priv, tt_global_entry, message);
1036 1037
	} else
		/* no local entry exists, case 1: check for roaming */
1038 1039
		batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
					     orig_node, message);
1040 1041


1042
out:
1043
	if (tt_global_entry)
1044 1045 1046
		batadv_tt_global_entry_free_ref(tt_global_entry);
	if (local_entry)
		batadv_tt_local_entry_free_ref(local_entry);
1047 1048
}

1049 1050 1051
void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
			       struct batadv_orig_node *orig_node,
			       const char *message)
1052
{
1053 1054
	struct batadv_tt_global_entry *tt_global;
	struct batadv_tt_common_entry *tt_common_entry;
1055
	uint32_t i;
1056
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1057 1058
	struct hlist_node *node, *safe;
	struct hlist_head *head;
1059
	spinlock_t *list_lock; /* protects write access to the hash lists */
1060

1061 1062 1063
	if (!hash)
		return;

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

1068
		spin_lock_bh(list_lock);
1069
		hlist_for_each_entry_safe(tt_common_entry, node, safe,
1070
					  head, hash_entry) {
1071 1072 1073
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
1074

1075
			batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1076
							orig_node, message);
1077

1078
			if (hlist_empty(&tt_global->orig_list)) {
1079
				batadv_dbg(BATADV_DBG_TT, bat_priv,
1080
					   "Deleting global tt entry %pM: %s\n",
1081
					   tt_global->common.addr, message);
1082
				hlist_del_rcu(node);
1083
				batadv_tt_global_entry_free_ref(tt_global);
1084
			}
1085
		}
1086
		spin_unlock_bh(list_lock);
1087
	}
1088
	orig_node->tt_initialised = false;
1089 1090
}

1091 1092
static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
				      char **msg)
1093
{
1094 1095 1096
	bool purge = false;
	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1097

1098 1099 1100 1101 1102
	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";
	}
1103

1104 1105 1106 1107
	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";
1108
	}
1109 1110

	return purge;
1111 1112
}

1113
static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1114
{
1115
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1116
	struct hlist_head *head;
1117
	struct hlist_node *node, *node_tmp;
1118
	spinlock_t *list_lock; /* protects write access to the hash lists */
1119
	uint32_t i;
1120 1121 1122
	char *msg = NULL;
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
1123 1124 1125

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

1128
		spin_lock_bh(list_lock);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		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);
		}
1146
		spin_unlock_bh(list_lock);
1147 1148 1149
	}
}

1150
static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1151
{
1152
	struct batadv_hashtable *hash;
1153
	spinlock_t *list_lock; /* protects write access to the hash lists */
1154 1155
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
1156 1157
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
1158
	uint32_t i;
1159

1160
	if (!bat_priv->tt.global_hash)
1161 1162
		return;

1163
	hash = bat_priv->tt.global_hash;
1164 1165 1166 1167 1168 1169

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

		spin_lock_bh(list_lock);
1170
		hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1171 1172
					  head, hash_entry) {
			hlist_del_rcu(node);
1173 1174 1175 1176
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_entry_free_ref(tt_global);
1177 1178 1179 1180
		}
		spin_unlock_bh(list_lock);
	}

1181
	batadv_hash_destroy(hash);
1182

1183
	bat_priv->tt.global_hash = NULL;
1184 1185
}

1186 1187 1188
static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
		       struct batadv_tt_global_entry *tt_global_entry)
1189 1190 1191
{
	bool ret = false;

1192 1193
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1194 1195 1196 1197 1198
		ret = true;

	return ret;
}

1199 1200 1201
struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
						  const uint8_t *src,
						  const uint8_t *addr)
1202
{
1203 1204 1205 1206
	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;
	struct batadv_neigh_node *router = NULL;
1207 1208
	struct hlist_head *head;
	struct hlist_node *node;
1209
	struct batadv_tt_orig_list_entry *orig_entry;
1210
	int best_tq;
1211

1212
	if (src && atomic_read(&bat_priv->ap_isolation)) {
1213
		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1214 1215 1216
		if (!tt_local_entry)
			goto out;
	}
1217

1218
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1219
	if (!tt_global_entry)
1220
		goto out;
1221

1222
	/* check whether the clients should not communicate due to AP
1223 1224
	 * isolation
	 */
1225 1226
	if (tt_local_entry &&
	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1227 1228
		goto out;

1229
	best_tq = 0;
1230

1231 1232 1233
	rcu_read_lock();
	head = &tt_global_entry->orig_list;
	hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1234
		router = batadv_orig_node_get_router(orig_entry->orig_node);
1235 1236
		if (!router)
			continue;
1237

1238 1239 1240 1241
		if (router->tq_avg > best_tq) {
			orig_node = orig_entry->orig_node;
			best_tq = router->tq_avg;
		}
1242
		batadv_neigh_node_free_ref(router);
1243 1244 1245 1246 1247
	}
	/* found anything? */
	if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
		orig_node = NULL;
	rcu_read_unlock();
1248
out:
1249
	if (tt_global_entry)
1250
		batadv_tt_global_entry_free_ref(tt_global_entry);
1251
	if (tt_local_entry)
1252
		batadv_tt_local_entry_free_ref(tt_local_entry);
1253

1254
	return orig_node;
1255
}
1256 1257

/* Calculates the checksum of the local table of a given orig_node */
1258 1259
static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node)
1260 1261
{
	uint16_t total = 0, total_one;
1262
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1263 1264
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
1265 1266
	struct hlist_node *node;
	struct hlist_head *head;
1267 1268
	uint32_t i;
	int j;
1269 1270 1271 1272 1273

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

		rcu_read_lock();
1274
		hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1275 1276 1277
			tt_global = container_of(tt_common,
						 struct batadv_tt_global_entry,
						 common);
1278 1279 1280 1281 1282
			/* Roaming clients are in the global table for
			 * consistency only. They don't have to be
			 * taken into account while computing the
			 * global crc
			 */
1283
			if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1284
				continue;
1285 1286 1287 1288 1289 1290
			/* 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;
1291 1292 1293 1294

			/* find out if this global entry is announced by this
			 * originator
			 */
1295
			if (!batadv_tt_global_entry_has_orig(tt_global,
1296
							     orig_node))
1297 1298 1299 1300 1301
				continue;

			total_one = 0;
			for (j = 0; j < ETH_ALEN; j++)
				total_one = crc16_byte(total_one,
1302
						       tt_common->addr[j]);
1303
			total ^= total_one;
1304 1305 1306 1307 1308 1309 1310 1311
		}
		rcu_read_unlock();
	}

	return total;
}

/* Calculates the checksum of the local table */
1312
static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1313 1314
{
	uint16_t total = 0, total_one;
1315
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1316
	struct batadv_tt_common_entry *tt_common;
1317 1318
	struct hlist_node *node;
	struct hlist_head *head;
1319 1320
	uint32_t i;
	int j;
1321 1322 1323 1324 1325

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

		rcu_read_lock();
1326
		hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
1327
			/* not yet committed clients have not to be taken into
1328 1329
			 * account while computing the CRC
			 */
1330
			if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1331
				continue;
1332 1333 1334
			total_one = 0;
			for (j = 0; j < ETH_ALEN; j++)
				total_one = crc16_byte(total_one,
1335
						       tt_common->addr[j]);
1336 1337 1338 1339 1340 1341 1342 1343
			total ^= total_one;
		}
		rcu_read_unlock();
	}

	return total;
}

1344
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1345
{
1346
	struct batadv_tt_req_node *node, *safe;
1347

1348
	spin_lock_bh(&bat_priv->tt.req_list_lock);
1349

1350
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1351 1352 1353 1354
		list_del(&node->list);
		kfree(node);
	}

1355
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1356 1357
}

1358 1359
static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
				       struct batadv_orig_node *orig_node,
1360 1361
				       const unsigned char *tt_buff,
				       uint8_t tt_num_changes)
1362
{
1363
	uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1364 1365

	/* Replace the old buffer only if I received something in the
1366 1367
	 * last OGM (the OGM could carry no changes)
	 */
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	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);
}

1381
static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1382
{
1383
	struct batadv_tt_req_node *node, *safe;
1384

1385 1386
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1387 1388
		if (batadv_has_timed_out(node->issued_at,
					 BATADV_TT_REQUEST_TIMEOUT)) {
1389 1390 1391 1392
			list_del(&node->list);
			kfree(node);
		}
	}
1393
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1394 1395 1396
}

/* returns the pointer to the new tt_req_node struct if no request
1397 1398
 * has already been issued for this orig_node, NULL otherwise
 */
1399 1400 1401
static struct batadv_tt_req_node *
batadv_new_tt_req_node(struct batadv_priv *bat_priv,
		       struct batadv_orig_node *orig_node)
1402
{
1403
	struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1404

1405 1406
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1407 1408
		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1409
					  BATADV_TT_REQUEST_TIMEOUT))
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
			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;

1420
	list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1421
unlock:
1422
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1423 1424 1425
	return tt_req_node;
}

1426
/* data_ptr is useless here, but has to be kept to respect the prototype */
1427 1428
static int batadv_tt_local_valid_entry(const void *entry_ptr,
				       const void *data_ptr)
1429
{
1430
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1431

1432
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1433 1434 1435 1436
		return 0;
	return 1;
}

1437 1438
static int batadv_tt_global_valid(const void *entry_ptr,
				  const void *data_ptr)
1439
{
1440 1441 1442
	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;
1443

1444 1445
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1446 1447
		return 0;

1448 1449
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
1450 1451
				       common);

1452
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1453 1454
}

1455 1456
static struct sk_buff *
batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1457
			      struct batadv_hashtable *hash,
1458
			      struct batadv_hard_iface *primary_if,
1459 1460
			      int (*valid_cb)(const void *, const void *),
			      void *cb_data)
1461
{
1462
	struct batadv_tt_common_entry *tt_common_entry;
1463 1464
	struct batadv_tt_query_packet *tt_response;
	struct batadv_tt_change *tt_change;
1465 1466 1467 1468
	struct hlist_node *node;
	struct hlist_head *head;
	struct sk_buff *skb = NULL;
	uint16_t tt_tot, tt_count;
1469
	ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1470
	uint32_t i;
1471
	size_t len;
1472 1473 1474

	if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
		tt_len = primary_if->soft_iface->mtu - tt_query_size;
1475
		tt_len -= tt_len % sizeof(struct batadv_tt_change);
1476
	}
1477
	tt_tot = tt_len / sizeof(struct batadv_tt_change);
1478

1479 1480
	len = tt_query_size + tt_len;
	skb = dev_alloc_skb(len + ETH_HLEN);
1481 1482 1483 1484
	if (!skb)
		goto out;

	skb_reserve(skb, ETH_HLEN);
1485
	tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1486 1487
	tt_response->ttvn = ttvn;

1488
	tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1489 1490 1491 1492 1493 1494
	tt_count = 0;

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

1495
		hlist_for_each_entry_rcu(tt_common_entry, node,
1496 1497 1498 1499
					 head, hash_entry) {
			if (tt_count == tt_tot)
				break;

1500
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1501 1502
				continue;

1503 1504
			memcpy(tt_change->addr, tt_common_entry->addr,
			       ETH_ALEN);
1505
			tt_change->flags = tt_common_entry->flags;
1506 1507 1508 1509 1510 1511 1512

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

1513
	/* store in the message the number of entries we have successfully
1514 1515
	 * copied
	 */
1516 1517
	tt_response->tt_data = htons(tt_count);

1518 1519 1520 1521
out:
	return skb;
}

1522 1523
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *dst_orig_node,
1524 1525
				  uint8_t ttvn, uint16_t tt_crc,
				  bool full_table)
1526 1527
{
	struct sk_buff *skb = NULL;
1528
	struct batadv_tt_query_packet *tt_request;
1529 1530 1531
	struct batadv_neigh_node *neigh_node = NULL;
	struct batadv_hard_iface *primary_if;
	struct batadv_tt_req_node *tt_req_node = NULL;
1532
	int ret = 1;
1533
	size_t tt_req_len;
1534

1535
	primary_if = batadv_primary_if_get_selected(bat_priv);
1536 1537 1538 1539
	if (!primary_if)
		goto out;

	/* The new tt_req will be issued only if I'm not waiting for a
1540 1541
	 * reply from the same orig_node yet
	 */
1542
	tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1543 1544 1545
	if (!tt_req_node)
		goto out;

1546
	skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN);
1547 1548 1549 1550 1551
	if (!skb)
		goto out;

	skb_reserve(skb, ETH_HLEN);

1552 1553
	tt_req_len = sizeof(*tt_request);
	tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1554

1555
	tt_request->header.packet_type = BATADV_TT_QUERY;
1556
	tt_request->header.version = BATADV_COMPAT_VERSION;
1557 1558
	memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1559
	tt_request->header.ttl = BATADV_TTL;
1560
	tt_request->ttvn = ttvn;
1561
	tt_request->tt_data = htons(tt_crc);
1562
	tt_request->flags = BATADV_TT_REQUEST;
1563 1564

	if (full_table)
1565
		tt_request->flags |= BATADV_TT_FULL_TABLE;
1566

1567
	neigh_node = batadv_orig_node_get_router(dst_orig_node);
1568 1569 1570
	if (!neigh_node)
		goto out;

1571
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1572 1573 1574
		   "Sending TT_REQUEST to %pM via %pM [%c]\n",
		   dst_orig_node->orig, neigh_node->addr,
		   (full_table ? 'F' : '.'));
1575

1576
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1577

1578
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1579 1580 1581 1582
	ret = 0;

out:
	if (neigh_node)
1583
		batadv_neigh_node_free_ref(neigh_node);
1584
	if (primary_if)
1585
		batadv_hardif_free_ref(primary_if);
1586 1587 1588
	if (ret)
		kfree_skb(skb);
	if (ret && tt_req_node) {
1589
		spin_lock_bh(&bat_priv->tt.req_list_lock);
1590
		list_del(&tt_req_node->list);
1591
		spin_unlock_bh(&bat_priv->tt.req_list_lock);
1592 1593 1594 1595 1596
		kfree(tt_req_node);
	}
	return ret;
}

1597
static bool
1598
batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1599
			      struct batadv_tt_query_packet *tt_request)
1600
{
1601 1602 1603 1604
	struct batadv_orig_node *req_dst_orig_node = NULL;
	struct batadv_orig_node *res_dst_orig_node = NULL;
	struct batadv_neigh_node *neigh_node = NULL;
	struct batadv_hard_iface *primary_if = NULL;
1605 1606 1607 1608 1609 1610
	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;
1611
	struct batadv_tt_query_packet *tt_response;
1612
	uint8_t *packet_pos;
1613
	size_t len;
1614

1615
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1616 1617
		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
		   tt_request->src, tt_request->ttvn, tt_request->dst,
1618
		   (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1619 1620

	/* Let's get the orig node of the REAL destination */
1621
	req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1622 1623 1624
	if (!req_dst_orig_node)
		goto out;

1625
	res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1626 1627 1628
	if (!res_dst_orig_node)
		goto out;

1629
	neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
1630 1631 1632
	if (!neigh_node)
		goto out;

1633
	primary_if = batadv_primary_if_get_selected(bat_priv);
1634 1635 1636 1637 1638 1639
	if (!primary_if)
		goto out;

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

1640
	/* I don't have the requested data */
1641
	if (orig_ttvn != req_ttvn ||
1642
	    tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1643 1644
		goto out;

1645
	/* If the full table has been explicitly requested */
1646
	if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1647 1648 1649 1650 1651 1652
	    !req_dst_orig_node->tt_buff)
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1653 1654
	 * I'll send only one packet with as much TT entries as I can
	 */
1655 1656 1657
	if (!full_table) {
		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
		tt_len = req_dst_orig_node->tt_buff_len;
1658
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1659

1660 1661
		len = sizeof(*tt_response) + tt_len;
		skb = dev_alloc_skb(len + ETH_HLEN);
1662 1663 1664 1665
		if (!skb)
			goto unlock;

		skb_reserve(skb, ETH_HLEN);
1666 1667
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1668 1669 1670
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1671
		tt_buff = skb->data + sizeof(*tt_response);
1672 1673 1674 1675 1676 1677
		/* 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 {
1678 1679
		tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
		tt_len *= sizeof(struct batadv_tt_change);
1680 1681
		ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);

1682
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1683
						    bat_priv->tt.global_hash,
1684 1685 1686
						    primary_if,
						    batadv_tt_global_valid,
						    req_dst_orig_node);
1687 1688 1689
		if (!skb)
			goto out;

1690
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1691 1692
	}

1693
	tt_response->header.packet_type = BATADV_TT_QUERY;
1694
	tt_response->header.version = BATADV_COMPAT_VERSION;
1695
	tt_response->header.ttl = BATADV_TTL;
1696 1697
	memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1698
	tt_response->flags = BATADV_TT_RESPONSE;
1699 1700

	if (full_table)
1701
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1702

1703
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1704 1705 1706
		   "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
		   res_dst_orig_node->orig, neigh_node->addr,
		   req_dst_orig_node->orig, req_ttvn);
1707

1708
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1709

1710
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1711 1712 1713 1714 1715 1716 1717 1718
	ret = true;
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
1719
		batadv_orig_node_free_ref(res_dst_orig_node);
1720
	if (req_dst_orig_node)
1721
		batadv_orig_node_free_ref(req_dst_orig_node);
1722
	if (neigh_node)
1723
		batadv_neigh_node_free_ref(neigh_node);
1724
	if (primary_if)
1725
		batadv_hardif_free_ref(primary_if);
1726 1727 1728 1729 1730
	if (!ret)
		kfree_skb(skb);
	return ret;

}
1731 1732

static bool
1733
batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1734
			   struct batadv_tt_query_packet *tt_request)
1735
{
1736 1737 1738
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *neigh_node = NULL;
	struct batadv_hard_iface *primary_if = NULL;
1739 1740 1741 1742 1743 1744
	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;
1745
	struct batadv_tt_query_packet *tt_response;
1746
	uint8_t *packet_pos;
1747
	size_t len;
1748

1749
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1750 1751
		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
		   tt_request->src, tt_request->ttvn,
1752
		   (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1753 1754


1755
	my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1756 1757
	req_ttvn = tt_request->ttvn;

1758
	orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1759 1760 1761
	if (!orig_node)
		goto out;

1762
	neigh_node = batadv_orig_node_get_router(orig_node);
1763 1764 1765
	if (!neigh_node)
		goto out;

1766
	primary_if = batadv_primary_if_get_selected(bat_priv);
1767 1768 1769 1770
	if (!primary_if)
		goto out;

	/* If the full table has been explicitly requested or the gap
1771 1772
	 * is too big send the whole local translation table
	 */
1773
	if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1774
	    !bat_priv->tt.last_changeset)
1775 1776 1777 1778 1779
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1780 1781
	 * I'll send only one packet with as much TT entries as I can
	 */
1782
	if (!full_table) {
1783 1784
		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
		tt_len = bat_priv->tt.last_changeset_len;
1785
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1786

1787 1788
		len = sizeof(*tt_response) + tt_len;
		skb = dev_alloc_skb(len + ETH_HLEN);
1789 1790 1791 1792
		if (!skb)
			goto unlock;

		skb_reserve(skb, ETH_HLEN);
1793 1794
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1795 1796 1797
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1798
		tt_buff = skb->data + sizeof(*tt_response);
1799 1800 1801
		memcpy(tt_buff, bat_priv->tt.last_changeset,
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1802
	} else {
1803
		tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1804
		tt_len *= sizeof(struct batadv_tt_change);
1805
		ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1806

1807
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1808
						    bat_priv->tt.local_hash,
1809 1810 1811
						    primary_if,
						    batadv_tt_local_valid_entry,
						    NULL);
1812 1813 1814
		if (!skb)
			goto out;

1815
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1816 1817
	}

1818
	tt_response->header.packet_type = BATADV_TT_QUERY;
1819
	tt_response->header.version = BATADV_COMPAT_VERSION;
1820
	tt_response->header.ttl = BATADV_TTL;
1821 1822
	memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1823
	tt_response->flags = BATADV_TT_RESPONSE;
1824 1825

	if (full_table)
1826
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1827

1828
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1829 1830
		   "Sending TT_RESPONSE to %pM via %pM [%c]\n",
		   orig_node->orig, neigh_node->addr,
1831
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1832

1833
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1834

1835
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1836 1837 1838 1839
	ret = true;
	goto out;

unlock:
1840
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1841 1842
out:
	if (orig_node)
1843
		batadv_orig_node_free_ref(orig_node);
1844
	if (neigh_node)
1845
		batadv_neigh_node_free_ref(neigh_node);
1846
	if (primary_if)
1847
		batadv_hardif_free_ref(primary_if);
1848 1849 1850 1851 1852 1853
	if (!ret)
		kfree_skb(skb);
	/* This packet was for me, so it doesn't need to be re-routed */
	return true;
}

1854
bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1855
			     struct batadv_tt_query_packet *tt_request)
1856
{
1857
	if (batadv_is_my_mac(tt_request->dst)) {
1858
		/* don't answer backbone gws! */
1859
		if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1860 1861
			return true;

1862
		return batadv_send_my_tt_response(bat_priv, tt_request);
1863
	} else {
1864
		return batadv_send_other_tt_response(bat_priv, tt_request);
1865
	}
1866 1867
}

1868 1869
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
1870
				      struct batadv_tt_change *tt_change,
1871
				      uint16_t tt_num_changes, uint8_t ttvn)
1872 1873
{
	int i;
1874
	int roams;
1875 1876

	for (i = 0; i < tt_num_changes; i++) {
1877 1878
		if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
			roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1879 1880
			batadv_tt_global_del(bat_priv, orig_node,
					     (tt_change + i)->addr,
1881 1882
					     "tt removed by changes",
					     roams);
1883 1884
		} else {
			if (!batadv_tt_global_add(bat_priv, orig_node,
1885 1886
						  (tt_change + i)->addr,
						  (tt_change + i)->flags, ttvn))
1887 1888 1889 1890 1891 1892 1893
				/* 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;
1894
		}
1895
	}
1896
	orig_node->tt_initialised = true;
1897 1898
}

1899
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1900
				  struct batadv_tt_query_packet *tt_response)
1901
{
1902
	struct batadv_orig_node *orig_node = NULL;
1903

1904
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1905 1906 1907 1908
	if (!orig_node)
		goto out;

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

1911
	_batadv_tt_update_changes(bat_priv, orig_node,
1912
				  (struct batadv_tt_change *)(tt_response + 1),
1913 1914
				  ntohs(tt_response->tt_data),
				  tt_response->ttvn);
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925

	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)
1926
		batadv_orig_node_free_ref(orig_node);
1927 1928
}

1929 1930
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
1931
				     uint16_t tt_num_changes, uint8_t ttvn,
1932
				     struct batadv_tt_change *tt_change)
1933
{
1934 1935
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
1936

1937 1938
	batadv_tt_save_orig_buffer(bat_priv, orig_node,
				   (unsigned char *)tt_change, tt_num_changes);
1939 1940 1941
	atomic_set(&orig_node->last_ttvn, ttvn);
}

1942
bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
1943
{
1944
	struct batadv_tt_local_entry *tt_local_entry = NULL;
1945
	bool ret = false;
1946

1947
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
1948 1949
	if (!tt_local_entry)
		goto out;
1950
	/* Check if the client has been logically deleted (but is kept for
1951 1952
	 * consistency purpose)
	 */
1953
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1954
		goto out;
1955 1956
	ret = true;
out:
1957
	if (tt_local_entry)
1958
		batadv_tt_local_entry_free_ref(tt_local_entry);
1959
	return ret;
1960 1961
}

1962
void batadv_handle_tt_response(struct batadv_priv *bat_priv,
1963
			       struct batadv_tt_query_packet *tt_response)
1964
{
1965 1966
	struct batadv_tt_req_node *node, *safe;
	struct batadv_orig_node *orig_node = NULL;
1967
	struct batadv_tt_change *tt_change;
1968

1969
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1970 1971 1972
		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
		   tt_response->src, tt_response->ttvn,
		   ntohs(tt_response->tt_data),
1973
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1974

1975
	/* we should have never asked a backbone gw */
1976
	if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1977 1978
		goto out;

1979
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1980 1981 1982
	if (!orig_node)
		goto out;

1983
	if (tt_response->flags & BATADV_TT_FULL_TABLE) {
1984
		batadv_tt_fill_gtable(bat_priv, tt_response);
1985 1986
	} else {
		tt_change = (struct batadv_tt_change *)(tt_response + 1);
1987 1988
		batadv_tt_update_changes(bat_priv, orig_node,
					 ntohs(tt_response->tt_data),
1989 1990
					 tt_response->ttvn, tt_change);
	}
1991 1992

	/* Delete the tt_req_node from pending tt_requests list */
1993 1994
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1995
		if (!batadv_compare_eth(node->addr, tt_response->src))
1996 1997 1998 1999
			continue;
		list_del(&node->list);
		kfree(node);
	}
2000
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2001 2002

	/* Recalculate the CRC for this orig_node and store it */
2003
	orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2004
	/* Roaming phase is over: tables are in sync again. I can
2005 2006
	 * unset the flag
	 */
2007
	orig_node->tt_poss_change = false;
2008 2009
out:
	if (orig_node)
2010
		batadv_orig_node_free_ref(orig_node);
2011 2012
}

2013
int batadv_tt_init(struct batadv_priv *bat_priv)
2014
{
2015
	int ret;
2016

2017
	ret = batadv_tt_local_init(bat_priv);
2018 2019 2020
	if (ret < 0)
		return ret;

2021
	ret = batadv_tt_global_init(bat_priv);
2022 2023
	if (ret < 0)
		return ret;
2024

2025
	batadv_tt_start_timer(bat_priv);
2026 2027 2028 2029

	return 1;
}

2030
static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2031
{
2032
	struct batadv_tt_roam_node *node, *safe;
2033

2034
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2035

2036
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2037 2038 2039 2040
		list_del(&node->list);
		kfree(node);
	}

2041
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2042 2043
}

2044
static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2045
{
2046
	struct batadv_tt_roam_node *node, *safe;
2047

2048 2049
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2050 2051
		if (!batadv_has_timed_out(node->first_time,
					  BATADV_ROAMING_MAX_TIME))
2052 2053 2054 2055 2056
			continue;

		list_del(&node->list);
		kfree(node);
	}
2057
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2058 2059 2060 2061 2062 2063
}

/* 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.
 *
2064 2065
 * returns true if the ROAMING_ADV can be sent, false otherwise
 */
2066
static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2067
				       uint8_t *client)
2068
{
2069
	struct batadv_tt_roam_node *tt_roam_node;
2070 2071
	bool ret = false;

2072
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2073
	/* The new tt_req will be issued only if I'm not waiting for a
2074 2075
	 * reply from the same orig_node yet
	 */
2076
	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2077
		if (!batadv_compare_eth(tt_roam_node->addr, client))
2078 2079
			continue;

2080
		if (batadv_has_timed_out(tt_roam_node->first_time,
2081
					 BATADV_ROAMING_MAX_TIME))
2082 2083
			continue;

2084
		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
			/* 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;
2097 2098
		atomic_set(&tt_roam_node->counter,
			   BATADV_ROAMING_MAX_COUNT - 1);
2099 2100
		memcpy(tt_roam_node->addr, client, ETH_ALEN);

2101
		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2102 2103 2104 2105
		ret = true;
	}

unlock:
2106
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2107 2108 2109
	return ret;
}

2110 2111
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node)
2112
{
2113
	struct batadv_neigh_node *neigh_node = NULL;
2114
	struct sk_buff *skb = NULL;
2115
	struct batadv_roam_adv_packet *roam_adv_packet;
2116
	int ret = 1;
2117
	struct batadv_hard_iface *primary_if;
2118
	size_t len = sizeof(*roam_adv_packet);
2119 2120

	/* before going on we have to check whether the client has
2121 2122
	 * already roamed to us too many times
	 */
2123
	if (!batadv_tt_check_roam_count(bat_priv, client))
2124 2125
		goto out;

2126
	skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN);
2127 2128 2129 2130 2131
	if (!skb)
		goto out;

	skb_reserve(skb, ETH_HLEN);

2132
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2133

2134
	roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2135
	roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2136
	roam_adv_packet->header.ttl = BATADV_TTL;
2137
	roam_adv_packet->reserved = 0;
2138
	primary_if = batadv_primary_if_get_selected(bat_priv);
2139 2140 2141
	if (!primary_if)
		goto out;
	memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2142
	batadv_hardif_free_ref(primary_if);
2143 2144 2145
	memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
	memcpy(roam_adv_packet->client, client, ETH_ALEN);

2146
	neigh_node = batadv_orig_node_get_router(orig_node);
2147 2148 2149
	if (!neigh_node)
		goto out;

2150
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2151 2152
		   "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
		   orig_node->orig, client, neigh_node->addr);
2153

2154
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2155

2156
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
2157 2158 2159 2160
	ret = 0;

out:
	if (neigh_node)
2161
		batadv_neigh_node_free_ref(neigh_node);
2162 2163 2164
	if (ret)
		kfree_skb(skb);
	return;
2165 2166
}

2167
static void batadv_tt_purge(struct work_struct *work)
2168
{
2169
	struct delayed_work *delayed_work;
2170
	struct batadv_priv_tt *priv_tt;
2171 2172 2173
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
2174 2175
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2176

2177
	batadv_tt_local_purge(bat_priv);
2178
	batadv_tt_global_purge(bat_priv);
2179 2180
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
2181

2182
	batadv_tt_start_timer(bat_priv);
2183
}
2184

2185
void batadv_tt_free(struct batadv_priv *bat_priv)
2186
{
2187
	cancel_delayed_work_sync(&bat_priv->tt.work);
2188

2189 2190 2191 2192 2193
	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);
2194

2195
	kfree(bat_priv->tt.last_changeset);
2196
}
2197

2198
/* This function will enable or disable the specified flags for all the entries
2199 2200
 * in the given hash table and returns the number of modified entries
 */
2201 2202
static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
				    uint16_t flags, bool enable)
2203
{
2204
	uint32_t i;
2205
	uint16_t changed_num = 0;
2206 2207
	struct hlist_head *head;
	struct hlist_node *node;
2208
	struct batadv_tt_common_entry *tt_common_entry;
2209 2210

	if (!hash)
2211
		goto out;
2212 2213 2214 2215 2216

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

		rcu_read_lock();
2217
		hlist_for_each_entry_rcu(tt_common_entry, node,
2218
					 head, hash_entry) {
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
			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++;
2229 2230 2231
		}
		rcu_read_unlock();
	}
2232 2233
out:
	return changed_num;
2234 2235
}

2236
/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2237
static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2238
{
2239
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2240 2241
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_local_entry *tt_local;
2242 2243 2244
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
	spinlock_t *list_lock; /* protects write access to the hash lists */
2245
	uint32_t i;
2246 2247 2248 2249 2250 2251 2252 2253 2254

	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);
2255 2256 2257
		hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2258 2259
				continue;

2260
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2261
				   "Deleting local tt entry (%pM): pending\n",
2262
				   tt_common->addr);
2263

2264
			atomic_dec(&bat_priv->tt.local_entry_num);
2265
			hlist_del_rcu(node);
2266 2267 2268 2269
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
2270 2271 2272 2273 2274 2275
		}
		spin_unlock_bh(list_lock);
	}

}

2276
static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2277 2278
				    unsigned char **packet_buff,
				    int *packet_buff_len, int packet_min_len)
2279
{
2280 2281
	uint16_t changed_num = 0;

2282
	if (atomic_read(&bat_priv->tt.local_changes) < 1)
2283 2284
		return -ENOENT;

2285
	changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2286
					  BATADV_TT_CLIENT_NEW, false);
2287 2288

	/* all reset entries have to be counted as local entries */
2289
	atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2290
	batadv_tt_local_purge_pending_clients(bat_priv);
2291
	bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2292 2293

	/* Increment the TTVN only once per OGM interval */
2294
	atomic_inc(&bat_priv->tt.vn);
2295
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2296
		   "Local changes committed, updating to ttvn %u\n",
2297 2298
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
	bat_priv->tt.poss_change = false;
2299 2300

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

2303 2304
	return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
					   packet_buff_len, packet_min_len);
2305 2306 2307
}

/* when calling this function (hard_iface == primary_if) has to be true */
2308
int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2309 2310 2311 2312 2313 2314
			  unsigned char **packet_buff, int *packet_buff_len,
			  int packet_min_len)
{
	int tt_num_changes;

	/* if at least one change happened */
2315 2316 2317
	tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
						  packet_buff_len,
						  packet_min_len);
2318 2319 2320

	/* if the changes have been sent often enough */
	if ((tt_num_changes < 0) &&
2321
	    (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2322 2323
		batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
					      packet_min_len, packet_min_len);
2324 2325 2326 2327
		tt_num_changes = 0;
	}

	return tt_num_changes;
2328
}
2329

2330
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2331
			   uint8_t *dst)
2332
{
2333 2334
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
2335
	bool ret = false;
2336 2337

	if (!atomic_read(&bat_priv->ap_isolation))
2338
		goto out;
2339

2340
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2341 2342 2343
	if (!tt_local_entry)
		goto out;

2344
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2345 2346 2347
	if (!tt_global_entry)
		goto out;

2348
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2349 2350
		goto out;

2351
	ret = true;
2352 2353 2354

out:
	if (tt_global_entry)
2355
		batadv_tt_global_entry_free_ref(tt_global_entry);
2356
	if (tt_local_entry)
2357
		batadv_tt_local_entry_free_ref(tt_local_entry);
2358 2359
	return ret;
}
2360

2361 2362
void batadv_tt_update_orig(struct batadv_priv *bat_priv,
			   struct batadv_orig_node *orig_node,
2363 2364
			   const unsigned char *tt_buff, uint8_t tt_num_changes,
			   uint8_t ttvn, uint16_t tt_crc)
2365 2366 2367
{
	uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	bool full_table = true;
2368
	struct batadv_tt_change *tt_change;
2369

2370
	/* don't care about a backbone gateways updates. */
2371
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2372 2373
		return;

2374
	/* orig table not initialised AND first diff is in the OGM OR the ttvn
2375 2376
	 * increased by one -> we can apply the attached changes
	 */
2377 2378
	if ((!orig_node->tt_initialised && ttvn == 1) ||
	    ttvn - orig_ttvn == 1) {
2379
		/* the OGM could not contain the changes due to their size or
2380 2381
		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
		 * times.
2382 2383
		 * In this case send a tt request
		 */
2384 2385 2386 2387 2388
		if (!tt_num_changes) {
			full_table = false;
			goto request_table;
		}

2389
		tt_change = (struct batadv_tt_change *)tt_buff;
2390
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2391
					 ttvn, tt_change);
2392 2393 2394

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
2395 2396
		 * in the global table
		 */
2397
		orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2398 2399 2400 2401 2402 2403 2404 2405

		/* 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
2406 2407
		 * inconsistency
		 */
2408 2409 2410 2411
		if (orig_node->tt_crc != tt_crc)
			goto request_table;

		/* Roaming phase is over: tables are in sync again. I can
2412 2413
		 * unset the flag
		 */
2414 2415 2416
		orig_node->tt_poss_change = false;
	} else {
		/* if we missed more than one change or our tables are not
2417 2418
		 * in sync anymore -> request fresh tt data
		 */
2419 2420
		if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
		    orig_node->tt_crc != tt_crc) {
2421
request_table:
2422
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2423 2424 2425
				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
				   orig_node->orig, ttvn, orig_ttvn, tt_crc,
				   orig_node->tt_crc, tt_num_changes);
2426 2427
			batadv_send_tt_request(bat_priv, orig_node, ttvn,
					       tt_crc, full_table);
2428 2429 2430 2431
			return;
		}
	}
}
2432 2433 2434 2435 2436

/* 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
 */
2437
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2438
					uint8_t *addr)
2439
{
2440
	struct batadv_tt_global_entry *tt_global_entry;
2441 2442
	bool ret = false;

2443
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2444 2445 2446
	if (!tt_global_entry)
		goto out;

2447
	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2448
	batadv_tt_global_entry_free_ref(tt_global_entry);
2449 2450 2451
out:
	return ret;
}
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470

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;

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