translation-table.c 69.9 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
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
245 246
	struct batadv_tt_local_entry *tt_local = NULL;
	struct batadv_tt_global_entry *tt_global = 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 = batadv_tt_local_hash_find(bat_priv, addr);
253

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

261 262
	tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
	if (!tt_local)
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 270
	memcpy(tt_local->common.addr, addr, ETH_ALEN);
	tt_local->common.flags = BATADV_NO_FLAGS;
271
	if (batadv_is_wifi_iface(ifindex))
272 273 274 275
		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;
276 277

	/* the batman interface mac address should never be purged */
278
	if (batadv_compare_eth(addr, soft_iface->dev_addr))
279
		tt_local->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->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->common,
				     &tt_local->common.hash_entry);
290 291 292

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

297
	batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
298

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

302
	/* Check whether it is a roaming! */
303
	if (tt_global) {
304
		/* These node are probably going to update their tt table */
305
		head = &tt_global->orig_list;
306 307
		rcu_read_lock();
		hlist_for_each_entry_rcu(orig_entry, node, head, list) {
308
			batadv_send_roam_adv(bat_priv, tt_global->common.addr,
309
					     orig_entry->orig_node);
310 311 312 313 314
		}
		rcu_read_unlock();
		/* The global entry has to be marked as ROAMING and
		 * has to be kept for consistency purpose
		 */
315 316
		tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
		tt_global->roam_at = jiffies;
317 318
	}
out:
319 320 321 322
	if (tt_local)
		batadv_tt_local_entry_free_ref(tt_local);
	if (tt_global)
		batadv_tt_global_entry_free_ref(tt_global);
323 324
}

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

343
static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
344 345 346
					  unsigned char **packet_buff,
					  int *packet_buff_len,
					  int min_packet_len)
347
{
348
	struct batadv_hard_iface *primary_if;
349 350
	int req_len;

351
	primary_if = batadv_primary_if_get_selected(bat_priv);
352 353

	req_len = min_packet_len;
354
	req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
355 356 357 358 359 360 361

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

362 363
	batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
				      min_packet_len, req_len);
364 365

	if (primary_if)
366
		batadv_hardif_free_ref(primary_if);
367 368
}

369
static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
370 371 372
				       unsigned char **packet_buff,
				       int *packet_buff_len,
				       int min_packet_len)
373
{
374
	struct batadv_tt_change_node *entry, *safe;
375 376 377
	int count = 0, tot_changes = 0, new_len;
	unsigned char *tt_buff;

378 379
	batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
				      packet_buff_len, min_packet_len);
380

381 382 383 384
	new_len = *packet_buff_len - min_packet_len;
	tt_buff = *packet_buff + min_packet_len;

	if (new_len > 0)
385
		tot_changes = new_len / batadv_tt_len(1);
386

387 388
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	atomic_set(&bat_priv->tt.local_changes, 0);
389

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

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

420
	return count;
421 422
}

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

434 435
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
436
		goto out;
437

438 439
	seq_printf(seq,
		   "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
440
		   net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
441 442 443 444

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

445
		rcu_read_lock();
446
		hlist_for_each_entry_rcu(tt_common_entry, node,
447
					 head, hash_entry) {
448
			seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
449 450
				   tt_common_entry->addr,
				   (tt_common_entry->flags &
451
				    BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
452
				   (tt_common_entry->flags &
453
				    BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
454
				   (tt_common_entry->flags &
455
				    BATADV_TT_CLIENT_NEW ? 'N' : '.'),
456
				   (tt_common_entry->flags &
457
				    BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
458
				   (tt_common_entry->flags &
459
				    BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
460
		}
461
		rcu_read_unlock();
462
	}
463 464
out:
	if (primary_if)
465
		batadv_hardif_free_ref(primary_if);
466
	return 0;
467 468
}

469 470 471 472
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)
473
{
474 475
	batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
			      tt_local_entry->common.flags | flags);
476

477 478
	/* 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
479 480
	 * response issued before the net ttvn increment (consistency check)
	 */
481
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
482

483
	batadv_dbg(BATADV_DBG_TT, bat_priv,
484 485
		   "Local tt entry (%pM) pending to be removed: %s\n",
		   tt_local_entry->common.addr, message);
486 487
}

488 489 490 491 492 493 494 495 496 497 498 499
/**
 * 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)
500
{
501
	struct batadv_tt_local_entry *tt_local_entry = NULL;
502
	uint16_t flags, curr_flags = BATADV_NO_FLAGS;
503

504
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
505 506 507
	if (!tt_local_entry)
		goto out;

508 509
	curr_flags = tt_local_entry->common.flags;

510
	flags = BATADV_TT_CLIENT_DEL;
511
	if (roaming) {
512
		flags |= BATADV_TT_CLIENT_ROAM;
513 514 515
		/* mark the local client as ROAMed */
		tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
	}
516 517

	batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, message);
518

519 520
out:
	if (tt_local_entry)
521
		batadv_tt_local_entry_free_ref(tt_local_entry);
522 523

	return curr_flags;
524 525
}

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

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

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

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

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

}

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

582
	if (!bat_priv->tt.local_hash)
583 584
		return;

585
	hash = bat_priv->tt.local_hash;
586 587 588 589 590 591

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

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

603
	batadv_hash_destroy(hash);
604

605
	bat_priv->tt.local_hash = NULL;
606 607
}

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

613
	bat_priv->tt.global_hash = batadv_hash_new(1024);
614

615
	if (!bat_priv->tt.global_hash)
616
		return -ENOMEM;
617

618
	return 0;
619 620
}

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

625
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
626

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

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

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

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

682 683 684
	return found;
}

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

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

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

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

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

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

732
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
733 734

	if (!tt_global_entry) {
735
		tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
736
		if (!tt_global_entry)
737 738
			goto out;

739 740
		common = &tt_global_entry->common;
		memcpy(common->addr, tt_addr, ETH_ALEN);
741

742
		common->flags = flags;
743
		tt_global_entry->roam_at = 0;
744 745 746 747 748 749
		/* 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;
750
		atomic_set(&common->refcount, 2);
751
		common->added_at = jiffies;
752 753 754

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

756
		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
757 758 759
					     batadv_compare_tt,
					     batadv_choose_orig, common,
					     &common->hash_entry);
760 761 762

		if (unlikely(hash_added != 0)) {
			/* remove the reference for the hash */
763
			batadv_tt_global_entry_free_ref(tt_global_entry);
764 765
			goto out_remove;
		}
766
	} else {
767 768 769 770 771 772 773 774 775 776 777 778 779
		/* 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;
780

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

797
	batadv_dbg(BATADV_DBG_TT, bat_priv,
798 799
		   "Creating new global tt entry: %pM (via %pM)\n",
		   tt_global_entry->common.addr, orig_node->orig);
800
	ret = 1;
801

802
out_remove:
803

804
	/* remove address from local hash if present */
805 806 807 808 809
	local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
					     "global tt received",
					     flags & BATADV_TT_CLIENT_ROAM);
	tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;

810 811
out:
	if (tt_global_entry)
812
		batadv_tt_global_entry_free_ref(tt_global_entry);
813
	return ret;
814 815
}

816 817 818
/* print all orig nodes who announce the address for this global entry.
 * it is assumed that the caller holds rcu_read_lock();
 */
819
static void
820
batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
821
			     struct seq_file *seq)
822 823 824
{
	struct hlist_head *head;
	struct hlist_node *node;
825 826
	struct batadv_tt_orig_list_entry *orig_entry;
	struct batadv_tt_common_entry *tt_common_entry;
827 828 829 830 831 832 833 834 835 836
	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);
837
		seq_printf(seq,	" * %pM  (%3u) via %pM     (%3u)   [%c%c%c]\n",
838 839
			   tt_global_entry->common.addr, orig_entry->ttvn,
			   orig_entry->orig_node->orig, last_ttvn,
840
			   (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
841 842
			   (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
			   (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
843 844 845
	}
}

846
int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
847 848
{
	struct net_device *net_dev = (struct net_device *)seq->private;
849
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
850
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
851 852 853
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
	struct batadv_hard_iface *primary_if;
854
	struct hlist_node *node;
855
	struct hlist_head *head;
856
	uint32_t i;
857

858 859
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
860
		goto out;
861

862 863
	seq_printf(seq,
		   "Globally announced TT entries received via the mesh %s\n",
864
		   net_dev->name);
865 866
	seq_printf(seq, "       %-13s %s       %-15s %s %s\n",
		   "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
867 868 869 870

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

871
		rcu_read_lock();
872
		hlist_for_each_entry_rcu(tt_common_entry, node,
873
					 head, hash_entry) {
874 875 876 877
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_print_entry(tt_global, seq);
878
		}
879
		rcu_read_unlock();
880
	}
881 882
out:
	if (primary_if)
883
		batadv_hardif_free_ref(primary_if);
884
	return 0;
885 886
}

887
/* deletes the orig list of a tt_global_entry */
888
static void
889
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
890
{
891 892
	struct hlist_head *head;
	struct hlist_node *node, *safe;
893
	struct batadv_tt_orig_list_entry *orig_entry;
894

895 896 897 898
	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);
899
		batadv_tt_orig_list_entry_free_ref(orig_entry);
900 901
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
902

903 904
}

905
static void
906 907 908
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,
909
				const char *message)
910 911 912
{
	struct hlist_head *head;
	struct hlist_node *node, *safe;
913
	struct batadv_tt_orig_list_entry *orig_entry;
914 915 916 917 918

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

930 931 932 933
static void
batadv_tt_global_del_struct(struct batadv_priv *bat_priv,
			    struct batadv_tt_global_entry *tt_global_entry,
			    const char *message)
934
{
935 936
	batadv_dbg(BATADV_DBG_TT, bat_priv,
		   "Deleting global tt entry %pM: %s\n",
937
		   tt_global_entry->common.addr, message);
938

939
	batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
940
			   batadv_choose_orig, tt_global_entry->common.addr);
941
	batadv_tt_global_entry_free_ref(tt_global_entry);
942

943 944
}

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

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



988 989
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
990 991
				 const unsigned char *addr,
				 const char *message, bool roaming)
992
{
993 994
	struct batadv_tt_global_entry *tt_global_entry = NULL;
	struct batadv_tt_local_entry *local_entry = NULL;
995

996
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
997
	if (!tt_global_entry)
998
		goto out;
999

1000
	if (!roaming) {
1001 1002
		batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
						orig_node, message);
1003 1004

		if (hlist_empty(&tt_global_entry->orig_list))
1005 1006
			batadv_tt_global_del_struct(bat_priv, tt_global_entry,
						    message);
1007 1008 1009

		goto out;
	}
1010 1011 1012

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


1036
out:
1037
	if (tt_global_entry)
1038 1039 1040
		batadv_tt_global_entry_free_ref(tt_global_entry);
	if (local_entry)
		batadv_tt_local_entry_free_ref(local_entry);
1041 1042
}

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

1055 1056 1057
	if (!hash)
		return;

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

1062
		spin_lock_bh(list_lock);
1063
		hlist_for_each_entry_safe(tt_common_entry, node, safe,
1064
					  head, hash_entry) {
1065 1066 1067
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
1068

1069
			batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1070
							orig_node, message);
1071

1072
			if (hlist_empty(&tt_global->orig_list)) {
1073
				batadv_dbg(BATADV_DBG_TT, bat_priv,
1074
					   "Deleting global tt entry %pM: %s\n",
1075
					   tt_global->common.addr, message);
1076
				hlist_del_rcu(node);
1077
				batadv_tt_global_entry_free_ref(tt_global);
1078
			}
1079
		}
1080
		spin_unlock_bh(list_lock);
1081
	}
1082
	orig_node->tt_initialised = false;
1083 1084
}

1085 1086
static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
				      char **msg)
1087
{
1088 1089 1090
	bool purge = false;
	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1091

1092 1093 1094 1095 1096
	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";
	}
1097

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

	return purge;
1105 1106
}

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

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

1122
		spin_lock_bh(list_lock);
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		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);
		}
1140
		spin_unlock_bh(list_lock);
1141 1142 1143
	}
}

1144
static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1145
{
1146
	struct batadv_hashtable *hash;
1147
	spinlock_t *list_lock; /* protects write access to the hash lists */
1148 1149
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
1150 1151
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
1152
	uint32_t i;
1153

1154
	if (!bat_priv->tt.global_hash)
1155 1156
		return;

1157
	hash = bat_priv->tt.global_hash;
1158 1159 1160 1161 1162 1163

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

		spin_lock_bh(list_lock);
1164
		hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
1165 1166
					  head, hash_entry) {
			hlist_del_rcu(node);
1167 1168 1169 1170
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_entry_free_ref(tt_global);
1171 1172 1173 1174
		}
		spin_unlock_bh(list_lock);
	}

1175
	batadv_hash_destroy(hash);
1176

1177
	bat_priv->tt.global_hash = NULL;
1178 1179
}

1180 1181 1182
static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
		       struct batadv_tt_global_entry *tt_global_entry)
1183 1184 1185
{
	bool ret = false;

1186 1187
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1188 1189 1190 1191 1192
		ret = true;

	return ret;
}

1193 1194 1195
struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
						  const uint8_t *src,
						  const uint8_t *addr)
1196
{
1197 1198 1199 1200
	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;
1201 1202
	struct hlist_head *head;
	struct hlist_node *node;
1203
	struct batadv_tt_orig_list_entry *orig_entry;
1204
	int best_tq;
1205

1206
	if (src && atomic_read(&bat_priv->ap_isolation)) {
1207
		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1208 1209 1210
		if (!tt_local_entry)
			goto out;
	}
1211

1212
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1213
	if (!tt_global_entry)
1214
		goto out;
1215

1216
	/* check whether the clients should not communicate due to AP
1217 1218
	 * isolation
	 */
1219 1220
	if (tt_local_entry &&
	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1221 1222
		goto out;

1223
	best_tq = 0;
1224

1225 1226 1227
	rcu_read_lock();
	head = &tt_global_entry->orig_list;
	hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1228
		router = batadv_orig_node_get_router(orig_entry->orig_node);
1229 1230
		if (!router)
			continue;
1231

1232 1233 1234 1235
		if (router->tq_avg > best_tq) {
			orig_node = orig_entry->orig_node;
			best_tq = router->tq_avg;
		}
1236
		batadv_neigh_node_free_ref(router);
1237 1238 1239 1240 1241
	}
	/* found anything? */
	if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
		orig_node = NULL;
	rcu_read_unlock();
1242
out:
1243
	if (tt_global_entry)
1244
		batadv_tt_global_entry_free_ref(tt_global_entry);
1245
	if (tt_local_entry)
1246
		batadv_tt_local_entry_free_ref(tt_local_entry);
1247

1248
	return orig_node;
1249
}
1250 1251

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

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

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

			/* find out if this global entry is announced by this
			 * originator
			 */
1289
			if (!batadv_tt_global_entry_has_orig(tt_global,
1290
							     orig_node))
1291 1292 1293 1294 1295
				continue;

			total_one = 0;
			for (j = 0; j < ETH_ALEN; j++)
				total_one = crc16_byte(total_one,
1296
						       tt_common->addr[j]);
1297
			total ^= total_one;
1298 1299 1300 1301 1302 1303 1304 1305
		}
		rcu_read_unlock();
	}

	return total;
}

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

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

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

	return total;
}

1338
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1339
{
1340
	struct batadv_tt_req_node *node, *safe;
1341

1342
	spin_lock_bh(&bat_priv->tt.req_list_lock);
1343

1344
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1345 1346 1347 1348
		list_del(&node->list);
		kfree(node);
	}

1349
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1350 1351
}

1352 1353
static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
				       struct batadv_orig_node *orig_node,
1354 1355
				       const unsigned char *tt_buff,
				       uint8_t tt_num_changes)
1356
{
1357
	uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1358 1359

	/* Replace the old buffer only if I received something in the
1360 1361
	 * last OGM (the OGM could carry no changes)
	 */
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	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);
}

1375
static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1376
{
1377
	struct batadv_tt_req_node *node, *safe;
1378

1379 1380
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1381 1382
		if (batadv_has_timed_out(node->issued_at,
					 BATADV_TT_REQUEST_TIMEOUT)) {
1383 1384 1385 1386
			list_del(&node->list);
			kfree(node);
		}
	}
1387
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1388 1389 1390
}

/* returns the pointer to the new tt_req_node struct if no request
1391 1392
 * has already been issued for this orig_node, NULL otherwise
 */
1393 1394 1395
static struct batadv_tt_req_node *
batadv_new_tt_req_node(struct batadv_priv *bat_priv,
		       struct batadv_orig_node *orig_node)
1396
{
1397
	struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1398

1399 1400
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1401 1402
		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1403
					  BATADV_TT_REQUEST_TIMEOUT))
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
			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;

1414
	list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1415
unlock:
1416
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1417 1418 1419
	return tt_req_node;
}

1420
/* data_ptr is useless here, but has to be kept to respect the prototype */
1421 1422
static int batadv_tt_local_valid_entry(const void *entry_ptr,
				       const void *data_ptr)
1423
{
1424
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1425

1426
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1427 1428 1429 1430
		return 0;
	return 1;
}

1431 1432
static int batadv_tt_global_valid(const void *entry_ptr,
				  const void *data_ptr)
1433
{
1434 1435 1436
	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;
1437

1438 1439
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1440 1441
		return 0;

1442 1443
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
1444 1445
				       common);

1446
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1447 1448
}

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

	if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
		tt_len = primary_if->soft_iface->mtu - tt_query_size;
1469
		tt_len -= tt_len % sizeof(struct batadv_tt_change);
1470
	}
1471
	tt_tot = tt_len / sizeof(struct batadv_tt_change);
1472

1473
	len = tt_query_size + tt_len;
1474
	skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1475 1476 1477
	if (!skb)
		goto out;

1478
	skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1479
	tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1480 1481
	tt_response->ttvn = ttvn;

1482
	tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1483 1484 1485 1486 1487 1488
	tt_count = 0;

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

1489
		hlist_for_each_entry_rcu(tt_common_entry, node,
1490 1491 1492 1493
					 head, hash_entry) {
			if (tt_count == tt_tot)
				break;

1494
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1495 1496
				continue;

1497 1498
			memcpy(tt_change->addr, tt_common_entry->addr,
			       ETH_ALEN);
1499
			tt_change->flags = BATADV_NO_FLAGS;
1500 1501 1502 1503 1504 1505 1506

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

1507
	/* store in the message the number of entries we have successfully
1508 1509
	 * copied
	 */
1510 1511
	tt_response->tt_data = htons(tt_count);

1512 1513 1514 1515
out:
	return skb;
}

1516 1517
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *dst_orig_node,
1518 1519
				  uint8_t ttvn, uint16_t tt_crc,
				  bool full_table)
1520 1521
{
	struct sk_buff *skb = NULL;
1522
	struct batadv_tt_query_packet *tt_request;
1523 1524 1525
	struct batadv_neigh_node *neigh_node = NULL;
	struct batadv_hard_iface *primary_if;
	struct batadv_tt_req_node *tt_req_node = NULL;
1526
	int ret = 1;
1527
	size_t tt_req_len;
1528

1529
	primary_if = batadv_primary_if_get_selected(bat_priv);
1530 1531 1532 1533
	if (!primary_if)
		goto out;

	/* The new tt_req will be issued only if I'm not waiting for a
1534 1535
	 * reply from the same orig_node yet
	 */
1536
	tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1537 1538 1539
	if (!tt_req_node)
		goto out;

1540
	skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1541 1542 1543
	if (!skb)
		goto out;

1544
	skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1545

1546 1547
	tt_req_len = sizeof(*tt_request);
	tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1548

1549
	tt_request->header.packet_type = BATADV_TT_QUERY;
1550
	tt_request->header.version = BATADV_COMPAT_VERSION;
1551 1552
	memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1553
	tt_request->header.ttl = BATADV_TTL;
1554
	tt_request->ttvn = ttvn;
1555
	tt_request->tt_data = htons(tt_crc);
1556
	tt_request->flags = BATADV_TT_REQUEST;
1557 1558

	if (full_table)
1559
		tt_request->flags |= BATADV_TT_FULL_TABLE;
1560

1561
	neigh_node = batadv_orig_node_get_router(dst_orig_node);
1562 1563 1564
	if (!neigh_node)
		goto out;

1565
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1566 1567 1568
		   "Sending TT_REQUEST to %pM via %pM [%c]\n",
		   dst_orig_node->orig, neigh_node->addr,
		   (full_table ? 'F' : '.'));
1569

1570
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1571

1572
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1573 1574 1575 1576
	ret = 0;

out:
	if (neigh_node)
1577
		batadv_neigh_node_free_ref(neigh_node);
1578
	if (primary_if)
1579
		batadv_hardif_free_ref(primary_if);
1580 1581 1582
	if (ret)
		kfree_skb(skb);
	if (ret && tt_req_node) {
1583
		spin_lock_bh(&bat_priv->tt.req_list_lock);
1584
		list_del(&tt_req_node->list);
1585
		spin_unlock_bh(&bat_priv->tt.req_list_lock);
1586 1587 1588 1589 1590
		kfree(tt_req_node);
	}
	return ret;
}

1591
static bool
1592
batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1593
			      struct batadv_tt_query_packet *tt_request)
1594
{
1595 1596 1597 1598
	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;
1599 1600 1601 1602 1603 1604
	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;
1605
	struct batadv_tt_query_packet *tt_response;
1606
	uint8_t *packet_pos;
1607
	size_t len;
1608

1609
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1610 1611
		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
		   tt_request->src, tt_request->ttvn, tt_request->dst,
1612
		   (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1613 1614

	/* Let's get the orig node of the REAL destination */
1615
	req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1616 1617 1618
	if (!req_dst_orig_node)
		goto out;

1619
	res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1620 1621 1622
	if (!res_dst_orig_node)
		goto out;

1623
	neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
1624 1625 1626
	if (!neigh_node)
		goto out;

1627
	primary_if = batadv_primary_if_get_selected(bat_priv);
1628 1629 1630 1631 1632 1633
	if (!primary_if)
		goto out;

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

1634
	/* I don't have the requested data */
1635
	if (orig_ttvn != req_ttvn ||
1636
	    tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1637 1638
		goto out;

1639
	/* If the full table has been explicitly requested */
1640
	if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1641 1642 1643 1644 1645 1646
	    !req_dst_orig_node->tt_buff)
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1647 1648
	 * I'll send only one packet with as much TT entries as I can
	 */
1649 1650 1651
	if (!full_table) {
		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
		tt_len = req_dst_orig_node->tt_buff_len;
1652
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1653

1654
		len = sizeof(*tt_response) + tt_len;
1655
		skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1656 1657 1658
		if (!skb)
			goto unlock;

1659
		skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1660 1661
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1662 1663 1664
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1665
		tt_buff = skb->data + sizeof(*tt_response);
1666 1667 1668 1669 1670 1671
		/* 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 {
1672 1673
		tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
		tt_len *= sizeof(struct batadv_tt_change);
1674 1675
		ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);

1676
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1677
						    bat_priv->tt.global_hash,
1678 1679 1680
						    primary_if,
						    batadv_tt_global_valid,
						    req_dst_orig_node);
1681 1682 1683
		if (!skb)
			goto out;

1684
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1685 1686
	}

1687
	tt_response->header.packet_type = BATADV_TT_QUERY;
1688
	tt_response->header.version = BATADV_COMPAT_VERSION;
1689
	tt_response->header.ttl = BATADV_TTL;
1690 1691
	memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1692
	tt_response->flags = BATADV_TT_RESPONSE;
1693 1694

	if (full_table)
1695
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1696

1697
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1698 1699 1700
		   "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);
1701

1702
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1703

1704
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1705 1706 1707 1708 1709 1710 1711 1712
	ret = true;
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
1713
		batadv_orig_node_free_ref(res_dst_orig_node);
1714
	if (req_dst_orig_node)
1715
		batadv_orig_node_free_ref(req_dst_orig_node);
1716
	if (neigh_node)
1717
		batadv_neigh_node_free_ref(neigh_node);
1718
	if (primary_if)
1719
		batadv_hardif_free_ref(primary_if);
1720 1721 1722 1723 1724
	if (!ret)
		kfree_skb(skb);
	return ret;

}
1725 1726

static bool
1727
batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1728
			   struct batadv_tt_query_packet *tt_request)
1729
{
1730 1731 1732
	struct batadv_orig_node *orig_node = NULL;
	struct batadv_neigh_node *neigh_node = NULL;
	struct batadv_hard_iface *primary_if = NULL;
1733 1734 1735 1736 1737 1738
	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;
1739
	struct batadv_tt_query_packet *tt_response;
1740
	uint8_t *packet_pos;
1741
	size_t len;
1742

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


1749
	my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1750 1751
	req_ttvn = tt_request->ttvn;

1752
	orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1753 1754 1755
	if (!orig_node)
		goto out;

1756
	neigh_node = batadv_orig_node_get_router(orig_node);
1757 1758 1759
	if (!neigh_node)
		goto out;

1760
	primary_if = batadv_primary_if_get_selected(bat_priv);
1761 1762 1763 1764
	if (!primary_if)
		goto out;

	/* If the full table has been explicitly requested or the gap
1765 1766
	 * is too big send the whole local translation table
	 */
1767
	if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1768
	    !bat_priv->tt.last_changeset)
1769 1770 1771 1772 1773
		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
	if (!full_table) {
1777 1778
		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
		tt_len = bat_priv->tt.last_changeset_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
		memcpy(tt_buff, bat_priv->tt.last_changeset,
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1796
	} else {
1797
		tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1798
		tt_len *= sizeof(struct batadv_tt_change);
1799
		ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1800

1801
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1802
						    bat_priv->tt.local_hash,
1803 1804 1805
						    primary_if,
						    batadv_tt_local_valid_entry,
						    NULL);
1806 1807 1808
		if (!skb)
			goto out;

1809
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1810 1811
	}

1812
	tt_response->header.packet_type = BATADV_TT_QUERY;
1813
	tt_response->header.version = BATADV_COMPAT_VERSION;
1814
	tt_response->header.ttl = BATADV_TTL;
1815 1816
	memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1817
	tt_response->flags = BATADV_TT_RESPONSE;
1818 1819

	if (full_table)
1820
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1821

1822
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1823 1824
		   "Sending TT_RESPONSE to %pM via %pM [%c]\n",
		   orig_node->orig, neigh_node->addr,
1825
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1826

1827
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1828

1829
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1830 1831 1832 1833
	ret = true;
	goto out;

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

1848
bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1849
			     struct batadv_tt_query_packet *tt_request)
1850
{
1851
	if (batadv_is_my_mac(tt_request->dst)) {
1852
		/* don't answer backbone gws! */
1853
		if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1854 1855
			return true;

1856
		return batadv_send_my_tt_response(bat_priv, tt_request);
1857
	} else {
1858
		return batadv_send_other_tt_response(bat_priv, tt_request);
1859
	}
1860 1861
}

1862 1863
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
1864
				      struct batadv_tt_change *tt_change,
1865
				      uint16_t tt_num_changes, uint8_t ttvn)
1866 1867
{
	int i;
1868
	int roams;
1869 1870

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

1893
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1894
				  struct batadv_tt_query_packet *tt_response)
1895
{
1896
	struct batadv_orig_node *orig_node = NULL;
1897

1898
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1899 1900 1901 1902
	if (!orig_node)
		goto out;

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

1905
	_batadv_tt_update_changes(bat_priv, orig_node,
1906
				  (struct batadv_tt_change *)(tt_response + 1),
1907 1908
				  ntohs(tt_response->tt_data),
				  tt_response->ttvn);
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919

	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)
1920
		batadv_orig_node_free_ref(orig_node);
1921 1922
}

1923 1924
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
1925
				     uint16_t tt_num_changes, uint8_t ttvn,
1926
				     struct batadv_tt_change *tt_change)
1927
{
1928 1929
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
1930

1931 1932
	batadv_tt_save_orig_buffer(bat_priv, orig_node,
				   (unsigned char *)tt_change, tt_num_changes);
1933 1934 1935
	atomic_set(&orig_node->last_ttvn, ttvn);
}

1936
bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
1937
{
1938
	struct batadv_tt_local_entry *tt_local_entry = NULL;
1939
	bool ret = false;
1940

1941
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
1942 1943
	if (!tt_local_entry)
		goto out;
1944
	/* Check if the client has been logically deleted (but is kept for
1945 1946
	 * consistency purpose)
	 */
1947 1948
	if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
	    (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
1949
		goto out;
1950 1951
	ret = true;
out:
1952
	if (tt_local_entry)
1953
		batadv_tt_local_entry_free_ref(tt_local_entry);
1954
	return ret;
1955 1956
}

1957
void batadv_handle_tt_response(struct batadv_priv *bat_priv,
1958
			       struct batadv_tt_query_packet *tt_response)
1959
{
1960 1961
	struct batadv_tt_req_node *node, *safe;
	struct batadv_orig_node *orig_node = NULL;
1962
	struct batadv_tt_change *tt_change;
1963

1964
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1965 1966 1967
		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
		   tt_response->src, tt_response->ttvn,
		   ntohs(tt_response->tt_data),
1968
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1969

1970
	/* we should have never asked a backbone gw */
1971
	if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
1972 1973
		goto out;

1974
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
1975 1976 1977
	if (!orig_node)
		goto out;

1978
	if (tt_response->flags & BATADV_TT_FULL_TABLE) {
1979
		batadv_tt_fill_gtable(bat_priv, tt_response);
1980 1981
	} else {
		tt_change = (struct batadv_tt_change *)(tt_response + 1);
1982 1983
		batadv_tt_update_changes(bat_priv, orig_node,
					 ntohs(tt_response->tt_data),
1984 1985
					 tt_response->ttvn, tt_change);
	}
1986 1987

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

	/* Recalculate the CRC for this orig_node and store it */
1998
	orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
1999 2000
out:
	if (orig_node)
2001
		batadv_orig_node_free_ref(orig_node);
2002 2003
}

2004
int batadv_tt_init(struct batadv_priv *bat_priv)
2005
{
2006
	int ret;
2007

2008
	ret = batadv_tt_local_init(bat_priv);
2009 2010 2011
	if (ret < 0)
		return ret;

2012
	ret = batadv_tt_global_init(bat_priv);
2013 2014
	if (ret < 0)
		return ret;
2015

2016
	batadv_tt_start_timer(bat_priv);
2017 2018 2019 2020

	return 1;
}

2021
static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2022
{
2023
	struct batadv_tt_roam_node *node, *safe;
2024

2025
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2026

2027
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2028 2029 2030 2031
		list_del(&node->list);
		kfree(node);
	}

2032
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2033 2034
}

2035
static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2036
{
2037
	struct batadv_tt_roam_node *node, *safe;
2038

2039 2040
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2041 2042
		if (!batadv_has_timed_out(node->first_time,
					  BATADV_ROAMING_MAX_TIME))
2043 2044 2045 2046 2047
			continue;

		list_del(&node->list);
		kfree(node);
	}
2048
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2049 2050 2051 2052 2053 2054
}

/* 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.
 *
2055 2056
 * returns true if the ROAMING_ADV can be sent, false otherwise
 */
2057
static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2058
				       uint8_t *client)
2059
{
2060
	struct batadv_tt_roam_node *tt_roam_node;
2061 2062
	bool ret = false;

2063
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2064
	/* The new tt_req will be issued only if I'm not waiting for a
2065 2066
	 * reply from the same orig_node yet
	 */
2067
	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2068
		if (!batadv_compare_eth(tt_roam_node->addr, client))
2069 2070
			continue;

2071
		if (batadv_has_timed_out(tt_roam_node->first_time,
2072
					 BATADV_ROAMING_MAX_TIME))
2073 2074
			continue;

2075
		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
			/* 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;
2088 2089
		atomic_set(&tt_roam_node->counter,
			   BATADV_ROAMING_MAX_COUNT - 1);
2090 2091
		memcpy(tt_roam_node->addr, client, ETH_ALEN);

2092
		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2093 2094 2095 2096
		ret = true;
	}

unlock:
2097
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2098 2099 2100
	return ret;
}

2101 2102
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node)
2103
{
2104
	struct batadv_neigh_node *neigh_node = NULL;
2105
	struct sk_buff *skb = NULL;
2106
	struct batadv_roam_adv_packet *roam_adv_packet;
2107
	int ret = 1;
2108
	struct batadv_hard_iface *primary_if;
2109
	size_t len = sizeof(*roam_adv_packet);
2110 2111

	/* before going on we have to check whether the client has
2112 2113
	 * already roamed to us too many times
	 */
2114
	if (!batadv_tt_check_roam_count(bat_priv, client))
2115 2116
		goto out;

2117
	skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2118 2119 2120
	if (!skb)
		goto out;

2121
	skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2122

2123
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2124

2125
	roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2126
	roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2127
	roam_adv_packet->header.ttl = BATADV_TTL;
2128
	roam_adv_packet->reserved = 0;
2129
	primary_if = batadv_primary_if_get_selected(bat_priv);
2130 2131 2132
	if (!primary_if)
		goto out;
	memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2133
	batadv_hardif_free_ref(primary_if);
2134 2135 2136
	memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
	memcpy(roam_adv_packet->client, client, ETH_ALEN);

2137
	neigh_node = batadv_orig_node_get_router(orig_node);
2138 2139 2140
	if (!neigh_node)
		goto out;

2141
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2142 2143
		   "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
		   orig_node->orig, client, neigh_node->addr);
2144

2145
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2146

2147
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
2148 2149 2150 2151
	ret = 0;

out:
	if (neigh_node)
2152
		batadv_neigh_node_free_ref(neigh_node);
2153 2154 2155
	if (ret)
		kfree_skb(skb);
	return;
2156 2157
}

2158
static void batadv_tt_purge(struct work_struct *work)
2159
{
2160
	struct delayed_work *delayed_work;
2161
	struct batadv_priv_tt *priv_tt;
2162 2163 2164
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
2165 2166
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2167

2168
	batadv_tt_local_purge(bat_priv);
2169
	batadv_tt_global_purge(bat_priv);
2170 2171
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
2172

2173
	batadv_tt_start_timer(bat_priv);
2174
}
2175

2176
void batadv_tt_free(struct batadv_priv *bat_priv)
2177
{
2178
	cancel_delayed_work_sync(&bat_priv->tt.work);
2179

2180 2181 2182 2183 2184
	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);
2185

2186
	kfree(bat_priv->tt.last_changeset);
2187
}
2188

2189
/* This function will enable or disable the specified flags for all the entries
2190 2191
 * in the given hash table and returns the number of modified entries
 */
2192 2193
static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
				    uint16_t flags, bool enable)
2194
{
2195
	uint32_t i;
2196
	uint16_t changed_num = 0;
2197 2198
	struct hlist_head *head;
	struct hlist_node *node;
2199
	struct batadv_tt_common_entry *tt_common_entry;
2200 2201

	if (!hash)
2202
		goto out;
2203 2204 2205 2206 2207

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

		rcu_read_lock();
2208
		hlist_for_each_entry_rcu(tt_common_entry, node,
2209
					 head, hash_entry) {
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
			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++;
2220 2221 2222
		}
		rcu_read_unlock();
	}
2223 2224
out:
	return changed_num;
2225 2226
}

2227
/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2228
static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2229
{
2230
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2231 2232
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_local_entry *tt_local;
2233 2234 2235
	struct hlist_node *node, *node_tmp;
	struct hlist_head *head;
	spinlock_t *list_lock; /* protects write access to the hash lists */
2236
	uint32_t i;
2237 2238 2239 2240 2241 2242 2243 2244 2245

	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);
2246 2247 2248
		hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2249 2250
				continue;

2251
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2252
				   "Deleting local tt entry (%pM): pending\n",
2253
				   tt_common->addr);
2254

2255
			atomic_dec(&bat_priv->tt.local_entry_num);
2256
			hlist_del_rcu(node);
2257 2258 2259 2260
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
2261 2262 2263 2264 2265 2266
		}
		spin_unlock_bh(list_lock);
	}

}

2267
static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2268 2269
				    unsigned char **packet_buff,
				    int *packet_buff_len, int packet_min_len)
2270
{
2271 2272
	uint16_t changed_num = 0;

2273
	if (atomic_read(&bat_priv->tt.local_changes) < 1)
2274 2275
		return -ENOENT;

2276
	changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2277
					  BATADV_TT_CLIENT_NEW, false);
2278 2279

	/* all reset entries have to be counted as local entries */
2280
	atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2281
	batadv_tt_local_purge_pending_clients(bat_priv);
2282
	bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2283 2284

	/* Increment the TTVN only once per OGM interval */
2285
	atomic_inc(&bat_priv->tt.vn);
2286
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2287
		   "Local changes committed, updating to ttvn %u\n",
2288
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
2289 2290

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

2293 2294
	return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
					   packet_buff_len, packet_min_len);
2295 2296 2297
}

/* when calling this function (hard_iface == primary_if) has to be true */
2298
int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2299 2300 2301 2302 2303 2304
			  unsigned char **packet_buff, int *packet_buff_len,
			  int packet_min_len)
{
	int tt_num_changes;

	/* if at least one change happened */
2305 2306 2307
	tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
						  packet_buff_len,
						  packet_min_len);
2308 2309 2310

	/* if the changes have been sent often enough */
	if ((tt_num_changes < 0) &&
2311
	    (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2312 2313
		batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
					      packet_min_len, packet_min_len);
2314 2315 2316 2317
		tt_num_changes = 0;
	}

	return tt_num_changes;
2318
}
2319

2320
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2321
			   uint8_t *dst)
2322
{
2323 2324
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
2325
	bool ret = false;
2326 2327

	if (!atomic_read(&bat_priv->ap_isolation))
2328
		goto out;
2329

2330
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2331 2332 2333
	if (!tt_local_entry)
		goto out;

2334
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2335 2336 2337
	if (!tt_global_entry)
		goto out;

2338
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2339 2340
		goto out;

2341
	ret = true;
2342 2343 2344

out:
	if (tt_global_entry)
2345
		batadv_tt_global_entry_free_ref(tt_global_entry);
2346
	if (tt_local_entry)
2347
		batadv_tt_local_entry_free_ref(tt_local_entry);
2348 2349
	return ret;
}
2350

2351 2352
void batadv_tt_update_orig(struct batadv_priv *bat_priv,
			   struct batadv_orig_node *orig_node,
2353 2354
			   const unsigned char *tt_buff, uint8_t tt_num_changes,
			   uint8_t ttvn, uint16_t tt_crc)
2355 2356 2357
{
	uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	bool full_table = true;
2358
	struct batadv_tt_change *tt_change;
2359

2360
	/* don't care about a backbone gateways updates. */
2361
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2362 2363
		return;

2364
	/* orig table not initialised AND first diff is in the OGM OR the ttvn
2365 2366
	 * increased by one -> we can apply the attached changes
	 */
2367 2368
	if ((!orig_node->tt_initialised && ttvn == 1) ||
	    ttvn - orig_ttvn == 1) {
2369
		/* the OGM could not contain the changes due to their size or
2370 2371
		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
		 * times.
2372 2373
		 * In this case send a tt request
		 */
2374 2375 2376 2377 2378
		if (!tt_num_changes) {
			full_table = false;
			goto request_table;
		}

2379
		tt_change = (struct batadv_tt_change *)tt_buff;
2380
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2381
					 ttvn, tt_change);
2382 2383 2384

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
2385 2386
		 * in the global table
		 */
2387
		orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2388 2389 2390 2391 2392 2393 2394 2395

		/* 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
2396 2397
		 * inconsistency
		 */
2398 2399 2400 2401
		if (orig_node->tt_crc != tt_crc)
			goto request_table;
	} else {
		/* if we missed more than one change or our tables are not
2402 2403
		 * in sync anymore -> request fresh tt data
		 */
2404 2405
		if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
		    orig_node->tt_crc != tt_crc) {
2406
request_table:
2407
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2408 2409 2410
				   "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);
2411 2412
			batadv_send_tt_request(bat_priv, orig_node, ttvn,
					       tt_crc, full_table);
2413 2414 2415 2416
			return;
		}
	}
}
2417 2418 2419 2420 2421

/* 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
 */
2422
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2423
					uint8_t *addr)
2424
{
2425
	struct batadv_tt_global_entry *tt_global_entry;
2426 2427
	bool ret = false;

2428
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2429 2430 2431
	if (!tt_global_entry)
		goto out;

2432
	ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2433
	batadv_tt_global_entry_free_ref(tt_global_entry);
2434 2435 2436
out:
	return ret;
}
2437

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
/**
 * 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;

}

2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
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;
}