translation-table.c 76.3 KB
Newer Older
1
/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
2
 *
3
 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

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

30 31
#include <linux/crc16.h>

32 33 34 35
/* hash class keys */
static struct lock_class_key batadv_tt_local_hash_lock_class_key;
static struct lock_class_key batadv_tt_global_hash_lock_class_key;

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

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

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

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

	if (!hash)
		return NULL;

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

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

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

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

82
	return tt_common_entry_tmp;
83 84
}

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

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

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

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

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

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

125 126 127
	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);
128 129 130 131

	kfree(tt_global_entry);
}

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

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

146
	orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
147 148 149 150 151 152

	/* We are in an rcu callback here, therefore we cannot use
	 * batadv_orig_node_free_ref() and its call_rcu():
	 * An rcu_barrier() wouldn't wait for that to finish
	 */
	batadv_orig_node_free_ref_now(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 167 168 169 170 171
/**
 * batadv_tt_local_event - store a local TT event (ADD/DEL)
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_local_entry: the TT entry involved in the event
 * @event_flags: flags to store in the event structure
 */
172
static void batadv_tt_local_event(struct batadv_priv *bat_priv,
173 174
				  struct batadv_tt_local_entry *tt_local_entry,
				  uint8_t event_flags)
175
{
176
	struct batadv_tt_change_node *tt_change_node, *entry, *safe;
177 178
	struct batadv_tt_common_entry *common = &tt_local_entry->common;
	uint8_t flags = common->flags | event_flags;
179 180
	bool event_removed = false;
	bool del_op_requested, del_op_entry;
181 182 183 184 185

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

186
	tt_change_node->change.flags = flags;
187
	tt_change_node->change.reserved = 0;
188
	memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
189

190
	del_op_requested = flags & BATADV_TT_CLIENT_DEL;
191 192

	/* check for ADD+DEL or DEL+ADD events */
193 194
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
195
				 list) {
196
		if (!batadv_compare_eth(entry->change.addr, common->addr))
197 198 199 200 201 202 203 204 205
			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
		 */
206
		del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
207 208 209 210 211 212 213 214
		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);
215
		kfree(tt_change_node);
216 217 218 219
		event_removed = true;
		goto unlock;
	}

220
	/* track the change in the OGMinterval list */
221
	list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
222 223

unlock:
224
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
225

226
	if (event_removed)
227
		atomic_dec(&bat_priv->tt.local_changes);
228
	else
229
		atomic_inc(&bat_priv->tt.local_changes);
230 231
}

232
int batadv_tt_len(int changes_num)
233
{
234
	return changes_num * sizeof(struct batadv_tt_change);
235 236
}

237
static int batadv_tt_local_init(struct batadv_priv *bat_priv)
238
{
239
	if (bat_priv->tt.local_hash)
240
		return 0;
241

242
	bat_priv->tt.local_hash = batadv_hash_new(1024);
243

244
	if (!bat_priv->tt.local_hash)
245
		return -ENOMEM;
246

247 248 249
	batadv_hash_set_lock_class(bat_priv->tt.local_hash,
				   &batadv_tt_local_hash_lock_class_key);

250
	return 0;
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265
static void batadv_tt_global_free(struct batadv_priv *bat_priv,
				  struct batadv_tt_global_entry *tt_global,
				  const char *message)
{
	batadv_dbg(BATADV_DBG_TT, bat_priv,
		   "Deleting global tt entry %pM: %s\n",
		   tt_global->common.addr, message);

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

266 267
void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
			 int ifindex)
268
{
269
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
270 271
	struct batadv_tt_local_entry *tt_local;
	struct batadv_tt_global_entry *tt_global;
272
	struct hlist_head *head;
273
	struct batadv_tt_orig_list_entry *orig_entry;
274
	int hash_added;
275
	bool roamed_back = false;
276

277
	tt_local = batadv_tt_local_hash_find(bat_priv, addr);
278
	tt_global = batadv_tt_global_hash_find(bat_priv, addr);
279

280 281
	if (tt_local) {
		tt_local->last_seen = jiffies;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
			batadv_dbg(BATADV_DBG_TT, bat_priv,
				   "Re-adding pending client %pM\n", addr);
			/* whatever the reason why the PENDING flag was set,
			 * this is a client which was enqueued to be removed in
			 * this orig_interval. Since it popped up again, the
			 * flag can be reset like it was never enqueued
			 */
			tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
			goto add_event;
		}

		if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
			batadv_dbg(BATADV_DBG_TT, bat_priv,
				   "Roaming client %pM came back to its original location\n",
				   addr);
			/* the ROAM flag is set because this client roamed away
			 * and the node got a roaming_advertisement message. Now
			 * that the client popped up again at its original
			 * location such flag can be unset
			 */
			tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
			roamed_back = true;
		}
		goto check_roaming;
307 308
	}

309 310
	tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
	if (!tt_local)
311
		goto out;
312

313
	batadv_dbg(BATADV_DBG_TT, bat_priv,
314
		   "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
315
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
316

317
	memcpy(tt_local->common.addr, addr, ETH_ALEN);
318 319 320 321 322
	/* The local entry has to be marked as NEW to avoid to send it in
	 * a full table response going out before the next ttvn increment
	 * (consistency check)
	 */
	tt_local->common.flags = BATADV_TT_CLIENT_NEW;
323
	if (batadv_is_wifi_iface(ifindex))
324 325 326 327
		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;
328 329

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

333
	hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
334 335
				     batadv_choose_orig, &tt_local->common,
				     &tt_local->common.hash_entry);
336 337 338

	if (unlikely(hash_added != 0)) {
		/* remove the reference for the hash */
339
		batadv_tt_local_entry_free_ref(tt_local);
340 341 342
		goto out;
	}

343
add_event:
344
	batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
345

346 347 348 349 350
check_roaming:
	/* Check whether it is a roaming, but don't do anything if the roaming
	 * process has already been handled
	 */
	if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
351
		/* These node are probably going to update their tt table */
352
		head = &tt_global->orig_list;
353
		rcu_read_lock();
354
		hlist_for_each_entry_rcu(orig_entry, head, list) {
355
			batadv_send_roam_adv(bat_priv, tt_global->common.addr,
356
					     orig_entry->orig_node);
357 358
		}
		rcu_read_unlock();
359 360 361 362 363 364 365 366 367 368 369
		if (roamed_back) {
			batadv_tt_global_free(bat_priv, tt_global,
					      "Roaming canceled");
			tt_global = NULL;
		} else {
			/* The global entry has to be marked as ROAMING and
			 * has to be kept for consistency purpose
			 */
			tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
			tt_global->roam_at = jiffies;
		}
370
	}
371

372
out:
373 374 375 376
	if (tt_local)
		batadv_tt_local_entry_free_ref(tt_local);
	if (tt_global)
		batadv_tt_global_entry_free_ref(tt_global);
377 378
}

379 380 381 382 383 384
/**
 * batadv_tt_tvlv_container_update - update the translation table tvlv container
 *  after local tt changes have been committed
 * @bat_priv: the bat priv with all the soft interface information
 */
static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
385
{
386 387 388 389 390
	struct batadv_tt_change_node *entry, *safe;
	struct batadv_tvlv_tt_data *tt_data;
	struct batadv_tvlv_tt_change *tt_change;
	int tt_diff_len = 0, tt_change_len = 0;
	int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
391

392
	tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
393 394 395 396

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

400 401 402
	tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
	if (!tt_data)
		return;
403

404 405 406
	tt_data->flags = BATADV_TT_OGM_DIFF;
	tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
	tt_data->crc = htons(bat_priv->tt.local_crc);
407

408 409
	if (tt_diff_len == 0)
		goto container_register;
410

411
	tt_diff_entries_num = tt_diff_len / batadv_tt_len(1);
412

413 414
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	atomic_set(&bat_priv->tt.local_changes, 0);
415

416 417
	tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);

418
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
419
				 list) {
420 421 422 423 424
		if (tt_diff_entries_count < tt_diff_entries_num) {
			memcpy(tt_change + tt_diff_entries_count,
			       &entry->change,
			       sizeof(struct batadv_tvlv_tt_change));
			tt_diff_entries_count++;
425
		}
426 427
		list_del(&entry->list);
		kfree(entry);
428
	}
429
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
430 431

	/* Keep the buffer for possible tt_request */
432 433 434 435
	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;
436
	tt_change_len = batadv_tt_len(tt_diff_entries_count);
437
	/* check whether this new OGM has no changes due to size problems */
438
	if (tt_diff_entries_count > 0) {
439
		/* if kmalloc() fails we will reply with the full table
440 441
		 * instead of providing the diff
		 */
442
		bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
443
		if (bat_priv->tt.last_changeset) {
444 445 446
			memcpy(bat_priv->tt.last_changeset,
			       tt_change, tt_change_len);
			bat_priv->tt.last_changeset_len = tt_diff_len;
447 448
		}
	}
449
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
450

451 452 453 454
container_register:
	batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
				       sizeof(*tt_data) + tt_change_len);
	kfree(tt_data);
455 456
}

457
int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
458 459
{
	struct net_device *net_dev = (struct net_device *)seq->private;
460
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
461
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
462
	struct batadv_tt_common_entry *tt_common_entry;
463
	struct batadv_tt_local_entry *tt_local;
464
	struct batadv_hard_iface *primary_if;
465
	struct hlist_head *head;
466
	uint32_t i;
467 468 469 470 471
	int last_seen_secs;
	int last_seen_msecs;
	unsigned long last_seen_jiffies;
	bool no_purge;
	uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
472

473 474
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
475
		goto out;
476

477
	seq_printf(seq,
478 479 480
		   "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
		   net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
		   bat_priv->tt.local_crc);
481 482
	seq_printf(seq, "       %-13s %-7s %-10s\n", "Client", "Flags",
		   "Last seen");
483 484 485 486

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

487
		rcu_read_lock();
488
		hlist_for_each_entry_rcu(tt_common_entry,
489
					 head, hash_entry) {
490 491 492 493 494 495 496 497 498 499 500
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
			last_seen_jiffies = jiffies - tt_local->last_seen;
			last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
			last_seen_secs = last_seen_msecs / 1000;
			last_seen_msecs = last_seen_msecs % 1000;

			no_purge = tt_common_entry->flags & np_flag;

			seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
501 502
				   tt_common_entry->addr,
				   (tt_common_entry->flags &
503
				    BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
504
				   no_purge ? 'P' : '.',
505
				   (tt_common_entry->flags &
506
				    BATADV_TT_CLIENT_NEW ? 'N' : '.'),
507
				   (tt_common_entry->flags &
508
				    BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
509
				   (tt_common_entry->flags &
510
				    BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
511 512
				   no_purge ? 0 : last_seen_secs,
				   no_purge ? 0 : last_seen_msecs);
513
		}
514
		rcu_read_unlock();
515
	}
516 517
out:
	if (primary_if)
518
		batadv_hardif_free_ref(primary_if);
519
	return 0;
520 521
}

522 523 524 525
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)
526
{
527
	batadv_tt_local_event(bat_priv, tt_local_entry, flags);
528

529 530
	/* 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
531 532
	 * response issued before the net ttvn increment (consistency check)
	 */
533
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
534

535
	batadv_dbg(BATADV_DBG_TT, bat_priv,
536 537
		   "Local tt entry (%pM) pending to be removed: %s\n",
		   tt_local_entry->common.addr, message);
538 539
}

540 541 542 543 544 545 546 547 548 549 550 551
/**
 * 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)
552
{
553
	struct batadv_tt_local_entry *tt_local_entry;
554
	uint16_t flags, curr_flags = BATADV_NO_FLAGS;
555

556
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
557 558 559
	if (!tt_local_entry)
		goto out;

560 561
	curr_flags = tt_local_entry->common.flags;

562
	flags = BATADV_TT_CLIENT_DEL;
563 564 565 566
	/* if this global entry addition is due to a roaming, the node has to
	 * mark the local entry as "roamed" in order to correctly reroute
	 * packets later
	 */
567
	if (roaming) {
568
		flags |= BATADV_TT_CLIENT_ROAM;
569 570 571
		/* mark the local client as ROAMed */
		tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
	}
572

573 574 575 576 577 578 579 580
	if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
		batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
					    message);
		goto out;
	}
	/* if this client has been added right now, it is possible to
	 * immediately purge it
	 */
581
	batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
582 583
	hlist_del_rcu(&tt_local_entry->common.hash_entry);
	batadv_tt_local_entry_free_ref(tt_local_entry);
584

585 586
out:
	if (tt_local_entry)
587
		batadv_tt_local_entry_free_ref(tt_local_entry);
588 589

	return curr_flags;
590 591
}

592
static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
593
				       struct hlist_head *head)
594
{
595 596
	struct batadv_tt_local_entry *tt_local_entry;
	struct batadv_tt_common_entry *tt_common_entry;
597
	struct hlist_node *node_tmp;
598

599
	hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
600 601
				  hash_entry) {
		tt_local_entry = container_of(tt_common_entry,
602 603
					      struct batadv_tt_local_entry,
					      common);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
		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");
	}
}

620
static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
621
{
622
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
623
	struct hlist_head *head;
624
	spinlock_t *list_lock; /* protects write access to the hash lists */
625
	uint32_t i;
626 627 628

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

631
		spin_lock_bh(list_lock);
632
		batadv_tt_local_purge_list(bat_priv, head);
633
		spin_unlock_bh(list_lock);
634 635 636
	}
}

637
static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
638
{
639
	struct batadv_hashtable *hash;
640
	spinlock_t *list_lock; /* protects write access to the hash lists */
641 642
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local;
643
	struct hlist_node *node_tmp;
644
	struct hlist_head *head;
645
	uint32_t i;
646

647
	if (!bat_priv->tt.local_hash)
648 649
		return;

650
	hash = bat_priv->tt.local_hash;
651 652 653 654 655 656

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

		spin_lock_bh(list_lock);
657
		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
658
					  head, hash_entry) {
659
			hlist_del_rcu(&tt_common_entry->hash_entry);
660 661 662 663
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
664 665 666 667
		}
		spin_unlock_bh(list_lock);
	}

668
	batadv_hash_destroy(hash);
669

670
	bat_priv->tt.local_hash = NULL;
671 672
}

673
static int batadv_tt_global_init(struct batadv_priv *bat_priv)
674
{
675
	if (bat_priv->tt.global_hash)
676
		return 0;
677

678
	bat_priv->tt.global_hash = batadv_hash_new(1024);
679

680
	if (!bat_priv->tt.global_hash)
681
		return -ENOMEM;
682

683 684 685
	batadv_hash_set_lock_class(bat_priv->tt.global_hash,
				   &batadv_tt_global_hash_lock_class_key);

686
	return 0;
687 688
}

689
static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
690
{
691
	struct batadv_tt_change_node *entry, *safe;
692

693
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
694

695
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
696 697 698 699
				 list) {
		list_del(&entry->list);
		kfree(entry);
	}
700

701 702
	atomic_set(&bat_priv->tt.local_changes, 0);
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
703
}
704

705 706 707 708
/* 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
709
 */
710 711 712
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)
713
{
714
	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
715 716 717 718
	const struct hlist_head *head;

	rcu_read_lock();
	head = &entry->orig_list;
719
	hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
720 721 722 723 724 725 726
		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;
727 728
	}
	rcu_read_unlock();
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

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

749 750 751
	return found;
}

752
static void
753
batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
754
				struct batadv_orig_node *orig_node, int ttvn)
755
{
756
	struct batadv_tt_orig_list_entry *orig_entry;
757

758
	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
759 760 761 762 763
	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;
764
		goto out;
765
	}
766

767 768
	orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
	if (!orig_entry)
769
		goto out;
770 771 772 773 774 775

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

778
	spin_lock_bh(&tt_global->list_lock);
779
	hlist_add_head_rcu(&orig_entry->list,
780 781 782 783 784
			   &tt_global->orig_list);
	spin_unlock_bh(&tt_global->list_lock);
out:
	if (orig_entry)
		batadv_tt_orig_list_entry_free_ref(orig_entry);
785 786
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
/**
 * batadv_tt_global_add - add a new TT global entry or update an existing one
 * @bat_priv: the bat priv with all the soft interface information
 * @orig_node: the originator announcing the client
 * @tt_addr: the mac address of the non-mesh client
 * @flags: TT flags that have to be set for this non-mesh client
 * @ttvn: the tt version number ever announcing this non-mesh client
 *
 * Add a new TT global entry for the given originator. If the entry already
 * exists add a new reference to the given originator (a global entry can have
 * references to multiple originators) and adjust the flags attribute to reflect
 * the function argument.
 * If a TT local entry exists for this non-mesh client remove it.
 *
 * The caller must hold orig_node refcount.
 */
803 804
int batadv_tt_global_add(struct batadv_priv *bat_priv,
			 struct batadv_orig_node *orig_node,
805
			 const unsigned char *tt_addr, uint16_t flags,
806
			 uint8_t ttvn)
807
{
808 809
	struct batadv_tt_global_entry *tt_global_entry;
	struct batadv_tt_local_entry *tt_local_entry;
810
	int ret = 0;
811
	int hash_added;
812
	struct batadv_tt_common_entry *common;
813
	uint16_t local_flags;
814

815
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
816 817 818 819 820 821 822 823 824
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);

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

	if (!tt_global_entry) {
827
		tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
828
		if (!tt_global_entry)
829 830
			goto out;

831 832
		common = &tt_global_entry->common;
		memcpy(common->addr, tt_addr, ETH_ALEN);
833

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

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

848
		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
849 850 851
					     batadv_compare_tt,
					     batadv_choose_orig, common,
					     &common->hash_entry);
852 853 854

		if (unlikely(hash_added != 0)) {
			/* remove the reference for the hash */
855
			batadv_tt_global_entry_free_ref(tt_global_entry);
856 857
			goto out_remove;
		}
858
	} else {
859
		common = &tt_global_entry->common;
860 861
		/* If there is already a global entry, we can use this one for
		 * our processing.
862 863 864 865 866 867 868
		 * But if we are trying to add a temporary client then here are
		 * two options at this point:
		 * 1) the global client is not a temporary client: the global
		 *    client has to be left as it is, temporary information
		 *    should never override any already known client state
		 * 2) the global client is a temporary client: purge the
		 *    originator list and add the new one orig_entry
869
		 */
870 871 872 873 874 875 876 877 878
		if (flags & BATADV_TT_CLIENT_TEMP) {
			if (!(common->flags & BATADV_TT_CLIENT_TEMP))
				goto out;
			if (batadv_tt_global_entry_has_orig(tt_global_entry,
							    orig_node))
				goto out_remove;
			batadv_tt_global_del_orig_list(tt_global_entry);
			goto add_orig_entry;
		}
879 880 881 882

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

885 886 887 888 889 890
		/* 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;

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

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

913
out_remove:
914

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

921 922 923 924 925 926
	if (!(flags & BATADV_TT_CLIENT_ROAM))
		/* this is a normal global add. Therefore the client is not in a
		 * roaming state anymore.
		 */
		tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;

927 928
out:
	if (tt_global_entry)
929
		batadv_tt_global_entry_free_ref(tt_global_entry);
930 931
	if (tt_local_entry)
		batadv_tt_local_entry_free_ref(tt_local_entry);
932
	return ret;
933 934
}

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
/* batadv_transtable_best_orig - Get best originator list entry from tt entry
 * @tt_global_entry: global translation table entry to be analyzed
 *
 * This functon assumes the caller holds rcu_read_lock().
 * Returns best originator list entry or NULL on errors.
 */
static struct batadv_tt_orig_list_entry *
batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
{
	struct batadv_neigh_node *router = NULL;
	struct hlist_head *head;
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
	int best_tq = 0;

	head = &tt_global_entry->orig_list;
950
	hlist_for_each_entry_rcu(orig_entry, head, list) {
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
		router = batadv_orig_node_get_router(orig_entry->orig_node);
		if (!router)
			continue;

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

		batadv_neigh_node_free_ref(router);
	}

	return best_entry;
}

/* batadv_tt_global_print_entry - print all orig nodes who announce the address
 * for this global entry
 * @tt_global_entry: global translation table entry to be printed
 * @seq: debugfs table seq_file struct
 *
 * This functon assumes the caller holds rcu_read_lock().
972
 */
973
static void
974
batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
975
			     struct seq_file *seq)
976 977
{
	struct hlist_head *head;
978
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
979
	struct batadv_tt_common_entry *tt_common_entry;
980 981 982 983
	uint16_t flags;
	uint8_t last_ttvn;

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

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

	head = &tt_global_entry->orig_list;

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

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

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

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

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

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

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

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

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

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

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

/* If the client is to be deleted, we check if it is the last origantor entry
1100 1101
 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
 * timer, otherwise we simply remove the originator scheduled for deletion.
1102
 */
1103
static void
1104 1105 1106 1107
batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
			     struct batadv_tt_global_entry *tt_global_entry,
			     struct batadv_orig_node *orig_node,
			     const char *message)
1108 1109 1110
{
	bool last_entry = true;
	struct hlist_head *head;
1111
	struct batadv_tt_orig_list_entry *orig_entry;
1112 1113 1114 1115 1116 1117 1118

	/* 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;
1119
	hlist_for_each_entry_rcu(orig_entry, head, list) {
1120 1121 1122 1123 1124 1125 1126 1127 1128
		if (orig_entry->orig_node != orig_node) {
			last_entry = false;
			break;
		}
	}
	rcu_read_unlock();

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



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

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

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

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

		goto out;
	}
1163 1164 1165

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


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

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

1208 1209 1210
	if (!hash)
		return;

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

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

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

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

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

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

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

	return purge;
1258 1259
}

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

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

1275
		spin_lock_bh(list_lock);
1276
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
					  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);

1289
			hlist_del_rcu(&tt_common->hash_entry);
1290 1291 1292

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

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

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

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

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

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

1328
	batadv_hash_destroy(hash);
1329

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

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

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

	return ret;
}

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

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

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

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

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

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

1388
	return orig_node;
1389
}
1390 1391

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

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

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

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

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

	return total;
}

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

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

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

	return total;
}

1476
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1477
{
1478
	struct batadv_tt_req_node *node, *safe;
1479

1480
	spin_lock_bh(&bat_priv->tt.req_list_lock);
1481

1482
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1483 1484 1485 1486
		list_del(&node->list);
		kfree(node);
	}

1487
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1488 1489
}

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

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

1513
static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1514
{
1515
	struct batadv_tt_req_node *node, *safe;
1516

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

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

1537 1538
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1539 1540
		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1541
					  BATADV_TT_REQUEST_TIMEOUT))
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
			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;

1552
	list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1553
unlock:
1554
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1555 1556 1557
	return tt_req_node;
}

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

1564
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1565 1566 1567 1568
		return 0;
	return 1;
}

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

1576 1577
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1578 1579
		return 0;

1580 1581
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
1582 1583
				       common);

1584
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1585 1586
}

1587 1588
static struct sk_buff *
batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1589
			      struct batadv_hashtable *hash,
1590
			      struct batadv_priv *bat_priv,
1591 1592
			      int (*valid_cb)(const void *, const void *),
			      void *cb_data)
1593
{
1594
	struct batadv_tt_common_entry *tt_common_entry;
1595 1596
	struct batadv_tt_query_packet *tt_response;
	struct batadv_tt_change *tt_change;
1597 1598 1599
	struct hlist_head *head;
	struct sk_buff *skb = NULL;
	uint16_t tt_tot, tt_count;
1600
	ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1601
	uint32_t i;
1602
	size_t len;
1603

1604 1605
	if (tt_query_size + tt_len > bat_priv->soft_iface->mtu) {
		tt_len = bat_priv->soft_iface->mtu - tt_query_size;
1606
		tt_len -= tt_len % sizeof(struct batadv_tt_change);
1607
	}
1608
	tt_tot = tt_len / sizeof(struct batadv_tt_change);
1609

1610
	len = tt_query_size + tt_len;
1611
	skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
1612 1613 1614
	if (!skb)
		goto out;

1615
	skb->priority = TC_PRIO_CONTROL;
1616
	skb_reserve(skb, ETH_HLEN);
1617
	tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1618 1619
	tt_response->ttvn = ttvn;

1620
	tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1621 1622 1623 1624 1625 1626
	tt_count = 0;

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

1627
		hlist_for_each_entry_rcu(tt_common_entry,
1628 1629 1630 1631
					 head, hash_entry) {
			if (tt_count == tt_tot)
				break;

1632
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1633 1634
				continue;

1635 1636
			memcpy(tt_change->addr, tt_common_entry->addr,
			       ETH_ALEN);
1637
			tt_change->flags = tt_common_entry->flags;
1638 1639 1640 1641 1642 1643 1644

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

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

1650 1651 1652 1653
out:
	return skb;
}

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

1666
	primary_if = batadv_primary_if_get_selected(bat_priv);
1667 1668 1669 1670
	if (!primary_if)
		goto out;

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

1677
	skb = netdev_alloc_skb_ip_align(NULL, sizeof(*tt_request) + ETH_HLEN);
1678 1679 1680
	if (!skb)
		goto out;

1681
	skb->priority = TC_PRIO_CONTROL;
1682
	skb_reserve(skb, ETH_HLEN);
1683

1684 1685
	tt_req_len = sizeof(*tt_request);
	tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1686

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

	if (full_table)
1697
		tt_request->flags |= BATADV_TT_FULL_TABLE;
1698

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

1702
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1703

1704
	if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL) != NET_XMIT_DROP)
1705
		ret = 0;
1706 1707 1708

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

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

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

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

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

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

1754
	/* I don't have the requested data */
1755
	if (orig_ttvn != req_ttvn ||
1756
	    tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1757 1758
		goto out;

1759
	/* If the full table has been explicitly requested */
1760
	if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1761 1762 1763 1764 1765 1766
	    !req_dst_orig_node->tt_buff)
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1767 1768
	 * I'll send only one packet with as much TT entries as I can
	 */
1769 1770 1771
	if (!full_table) {
		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
		tt_len = req_dst_orig_node->tt_buff_len;
1772
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1773

1774
		len = sizeof(*tt_response) + tt_len;
1775
		skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
1776 1777 1778
		if (!skb)
			goto unlock;

1779
		skb->priority = TC_PRIO_CONTROL;
1780
		skb_reserve(skb, ETH_HLEN);
1781 1782
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1783 1784 1785
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1786
		tt_buff = skb->data + sizeof(*tt_response);
1787 1788 1789 1790 1791 1792
		/* 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 {
1793 1794
		tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
		tt_len *= sizeof(struct batadv_tt_change);
1795 1796
		ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);

1797
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1798
						    bat_priv->tt.global_hash,
1799
						    bat_priv,
1800 1801
						    batadv_tt_global_valid,
						    req_dst_orig_node);
1802 1803 1804
		if (!skb)
			goto out;

1805
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1806 1807
	}

1808
	tt_response->header.packet_type = BATADV_TT_QUERY;
1809
	tt_response->header.version = BATADV_COMPAT_VERSION;
1810
	tt_response->header.ttl = BATADV_TTL;
1811 1812
	memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1813
	tt_response->flags = BATADV_TT_RESPONSE;
1814 1815

	if (full_table)
1816
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1817

1818
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1819 1820
		   "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
		   res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
1821

1822
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1823

1824 1825
	res = batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL);
	if (res != NET_XMIT_DROP)
1826
		ret = true;
1827

1828 1829 1830 1831 1832 1833 1834
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
1835
		batadv_orig_node_free_ref(res_dst_orig_node);
1836
	if (req_dst_orig_node)
1837
		batadv_orig_node_free_ref(req_dst_orig_node);
1838 1839 1840 1841
	if (!ret)
		kfree_skb(skb);
	return ret;
}
1842 1843

static bool
1844
batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1845
			   struct batadv_tt_query_packet *tt_request)
1846
{
1847
	struct batadv_orig_node *orig_node;
1848
	struct batadv_hard_iface *primary_if = NULL;
1849 1850 1851 1852 1853 1854
	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;
1855
	struct batadv_tt_query_packet *tt_response;
1856
	uint8_t *packet_pos;
1857
	size_t len;
1858

1859
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1860 1861
		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
		   tt_request->src, tt_request->ttvn,
1862
		   (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1863 1864


1865
	my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1866 1867
	req_ttvn = tt_request->ttvn;

1868
	orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1869 1870 1871
	if (!orig_node)
		goto out;

1872
	primary_if = batadv_primary_if_get_selected(bat_priv);
1873 1874 1875 1876
	if (!primary_if)
		goto out;

	/* If the full table has been explicitly requested or the gap
1877 1878
	 * is too big send the whole local translation table
	 */
1879
	if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1880
	    !bat_priv->tt.last_changeset)
1881 1882 1883 1884 1885
		full_table = true;
	else
		full_table = false;

	/* In this version, fragmentation is not implemented, then
1886 1887
	 * I'll send only one packet with as much TT entries as I can
	 */
1888
	if (!full_table) {
1889 1890
		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
		tt_len = bat_priv->tt.last_changeset_len;
1891
		tt_tot = tt_len / sizeof(struct batadv_tt_change);
1892

1893
		len = sizeof(*tt_response) + tt_len;
1894
		skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
1895 1896 1897
		if (!skb)
			goto unlock;

1898
		skb->priority = TC_PRIO_CONTROL;
1899
		skb_reserve(skb, ETH_HLEN);
1900 1901
		packet_pos = skb_put(skb, len);
		tt_response = (struct batadv_tt_query_packet *)packet_pos;
1902 1903 1904
		tt_response->ttvn = req_ttvn;
		tt_response->tt_data = htons(tt_tot);

1905
		tt_buff = skb->data + sizeof(*tt_response);
1906 1907 1908
		memcpy(tt_buff, bat_priv->tt.last_changeset,
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1909
	} else {
1910
		tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1911
		tt_len *= sizeof(struct batadv_tt_change);
1912
		ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1913

1914
		skb = batadv_tt_response_fill_table(tt_len, ttvn,
1915
						    bat_priv->tt.local_hash,
1916
						    bat_priv,
1917 1918
						    batadv_tt_local_valid_entry,
						    NULL);
1919 1920 1921
		if (!skb)
			goto out;

1922
		tt_response = (struct batadv_tt_query_packet *)skb->data;
1923 1924
	}

1925
	tt_response->header.packet_type = BATADV_TT_QUERY;
1926
	tt_response->header.version = BATADV_COMPAT_VERSION;
1927
	tt_response->header.ttl = BATADV_TTL;
1928 1929
	memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
	memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1930
	tt_response->flags = BATADV_TT_RESPONSE;
1931 1932

	if (full_table)
1933
		tt_response->flags |= BATADV_TT_FULL_TABLE;
1934

1935
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1936 1937
		   "Sending TT_RESPONSE to %pM [%c]\n",
		   orig_node->orig,
1938
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1939

1940
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1941

1942
	if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
1943
		ret = true;
1944 1945 1946
	goto out;

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

1959
bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1960
			     struct batadv_tt_query_packet *tt_request)
1961
{
1962
	if (batadv_is_my_mac(bat_priv, tt_request->dst)) {
1963
		/* don't answer backbone gws! */
1964
		if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1965 1966
			return true;

1967
		return batadv_send_my_tt_response(bat_priv, tt_request);
1968
	} else {
1969
		return batadv_send_other_tt_response(bat_priv, tt_request);
1970
	}
1971 1972
}

1973 1974
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
1975
				      struct batadv_tt_change *tt_change,
1976
				      uint16_t tt_num_changes, uint8_t ttvn)
1977 1978
{
	int i;
1979
	int roams;
1980 1981

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

2004
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2005
				  struct batadv_tt_query_packet *tt_response)
2006
{
2007
	struct batadv_orig_node *orig_node;
2008

2009
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2010 2011 2012 2013
	if (!orig_node)
		goto out;

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

2016
	_batadv_tt_update_changes(bat_priv, orig_node,
2017
				  (struct batadv_tt_change *)(tt_response + 1),
2018 2019
				  ntohs(tt_response->tt_data),
				  tt_response->ttvn);
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030

	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)
2031
		batadv_orig_node_free_ref(orig_node);
2032 2033
}

2034 2035
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
2036
				     uint16_t tt_num_changes, uint8_t ttvn,
2037
				     struct batadv_tt_change *tt_change)
2038
{
2039 2040
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
2041

2042 2043
	batadv_tt_save_orig_buffer(bat_priv, orig_node,
				   (unsigned char *)tt_change, tt_num_changes);
2044 2045 2046
	atomic_set(&orig_node->last_ttvn, ttvn);
}

2047
bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2048
{
2049
	struct batadv_tt_local_entry *tt_local_entry;
2050
	bool ret = false;
2051

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

2068
void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2069
			       struct batadv_tt_query_packet *tt_response)
2070
{
2071 2072
	struct batadv_tt_req_node *node, *safe;
	struct batadv_orig_node *orig_node = NULL;
2073
	struct batadv_tt_change *tt_change;
2074

2075
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2076 2077 2078
		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
		   tt_response->src, tt_response->ttvn,
		   ntohs(tt_response->tt_data),
2079
		   (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2080

2081
	/* we should have never asked a backbone gw */
2082
	if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2083 2084
		goto out;

2085
	orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2086 2087 2088
	if (!orig_node)
		goto out;

2089
	if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2090
		batadv_tt_fill_gtable(bat_priv, tt_response);
2091 2092
	} else {
		tt_change = (struct batadv_tt_change *)(tt_response + 1);
2093 2094
		batadv_tt_update_changes(bat_priv, orig_node,
					 ntohs(tt_response->tt_data),
2095 2096
					 tt_response->ttvn, tt_change);
	}
2097 2098

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

	/* Recalculate the CRC for this orig_node and store it */
2109
	orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2110 2111
out:
	if (orig_node)
2112
		batadv_orig_node_free_ref(orig_node);
2113 2114
}

2115
static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2116
{
2117
	struct batadv_tt_roam_node *node, *safe;
2118

2119
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2120

2121
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2122 2123 2124 2125
		list_del(&node->list);
		kfree(node);
	}

2126
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2127 2128
}

2129
static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2130
{
2131
	struct batadv_tt_roam_node *node, *safe;
2132

2133 2134
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2135 2136
		if (!batadv_has_timed_out(node->first_time,
					  BATADV_ROAMING_MAX_TIME))
2137 2138 2139 2140 2141
			continue;

		list_del(&node->list);
		kfree(node);
	}
2142
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2143 2144 2145 2146 2147 2148
}

/* 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.
 *
2149 2150
 * returns true if the ROAMING_ADV can be sent, false otherwise
 */
2151
static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2152
				       uint8_t *client)
2153
{
2154
	struct batadv_tt_roam_node *tt_roam_node;
2155 2156
	bool ret = false;

2157
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2158
	/* The new tt_req will be issued only if I'm not waiting for a
2159 2160
	 * reply from the same orig_node yet
	 */
2161
	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2162
		if (!batadv_compare_eth(tt_roam_node->addr, client))
2163 2164
			continue;

2165
		if (batadv_has_timed_out(tt_roam_node->first_time,
2166
					 BATADV_ROAMING_MAX_TIME))
2167 2168
			continue;

2169
		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
			/* 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;
2182 2183
		atomic_set(&tt_roam_node->counter,
			   BATADV_ROAMING_MAX_COUNT - 1);
2184 2185
		memcpy(tt_roam_node->addr, client, ETH_ALEN);

2186
		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2187 2188 2189 2190
		ret = true;
	}

unlock:
2191
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2192 2193 2194
	return ret;
}

2195 2196
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node)
2197 2198
{
	struct sk_buff *skb = NULL;
2199
	struct batadv_roam_adv_packet *roam_adv_packet;
2200
	int ret = 1;
2201
	struct batadv_hard_iface *primary_if;
2202
	size_t len = sizeof(*roam_adv_packet);
2203 2204

	/* before going on we have to check whether the client has
2205 2206
	 * already roamed to us too many times
	 */
2207
	if (!batadv_tt_check_roam_count(bat_priv, client))
2208 2209
		goto out;

2210
	skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
2211 2212 2213
	if (!skb)
		goto out;

2214
	skb->priority = TC_PRIO_CONTROL;
2215
	skb_reserve(skb, ETH_HLEN);
2216

2217
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2218

2219
	roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2220
	roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2221
	roam_adv_packet->header.ttl = BATADV_TTL;
2222
	roam_adv_packet->reserved = 0;
2223
	primary_if = batadv_primary_if_get_selected(bat_priv);
2224 2225 2226
	if (!primary_if)
		goto out;
	memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2227
	batadv_hardif_free_ref(primary_if);
2228 2229 2230
	memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
	memcpy(roam_adv_packet->client, client, ETH_ALEN);

2231
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2232 2233
		   "Sending ROAMING_ADV to %pM (client %pM)\n",
		   orig_node->orig, client);
2234

2235
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2236

2237
	if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
2238
		ret = 0;
2239 2240

out:
2241
	if (ret && skb)
2242 2243
		kfree_skb(skb);
	return;
2244 2245
}

2246
static void batadv_tt_purge(struct work_struct *work)
2247
{
2248
	struct delayed_work *delayed_work;
2249
	struct batadv_priv_tt *priv_tt;
2250 2251 2252
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
2253 2254
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2255

2256
	batadv_tt_local_purge(bat_priv);
2257
	batadv_tt_global_purge(bat_priv);
2258 2259
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
2260

2261 2262
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2263
}
2264

2265
void batadv_tt_free(struct batadv_priv *bat_priv)
2266
{
2267 2268 2269
	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);

2270
	cancel_delayed_work_sync(&bat_priv->tt.work);
2271

2272 2273 2274 2275 2276
	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);
2277

2278
	kfree(bat_priv->tt.last_changeset);
2279
}
2280

2281
/* This function will enable or disable the specified flags for all the entries
2282 2283
 * in the given hash table and returns the number of modified entries
 */
2284 2285
static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
				    uint16_t flags, bool enable)
2286
{
2287
	uint32_t i;
2288
	uint16_t changed_num = 0;
2289
	struct hlist_head *head;
2290
	struct batadv_tt_common_entry *tt_common_entry;
2291 2292

	if (!hash)
2293
		goto out;
2294 2295 2296 2297 2298

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

		rcu_read_lock();
2299
		hlist_for_each_entry_rcu(tt_common_entry,
2300
					 head, hash_entry) {
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
			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++;
2311 2312 2313
		}
		rcu_read_unlock();
	}
2314 2315
out:
	return changed_num;
2316 2317
}

2318
/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2319
static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2320
{
2321
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2322 2323
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_local_entry *tt_local;
2324
	struct hlist_node *node_tmp;
2325 2326
	struct hlist_head *head;
	spinlock_t *list_lock; /* protects write access to the hash lists */
2327
	uint32_t i;
2328 2329 2330 2331 2332 2333 2334 2335 2336

	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);
2337
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
2338 2339
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2340 2341
				continue;

2342
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2343
				   "Deleting local tt entry (%pM): pending\n",
2344
				   tt_common->addr);
2345

2346
			atomic_dec(&bat_priv->tt.local_entry_num);
2347
			hlist_del_rcu(&tt_common->hash_entry);
2348 2349 2350 2351
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
2352 2353 2354 2355 2356
		}
		spin_unlock_bh(list_lock);
	}
}

2357 2358 2359 2360 2361 2362
/**
 * batadv_tt_local_commit_changes - commit all pending local tt changes which
 *  have been queued in the time since the last commit
 * @bat_priv: the bat priv with all the soft interface information
 */
void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
2363
{
2364 2365
	uint16_t changed_num = 0;

2366 2367 2368 2369 2370
	if (atomic_read(&bat_priv->tt.local_changes) < 1) {
		if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
			batadv_tt_tvlv_container_update(bat_priv);
		return;
	}
2371

2372
	changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2373
					  BATADV_TT_CLIENT_NEW, false);
2374 2375

	/* all reset entries have to be counted as local entries */
2376
	atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2377
	batadv_tt_local_purge_pending_clients(bat_priv);
2378
	bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2379 2380

	/* Increment the TTVN only once per OGM interval */
2381
	atomic_inc(&bat_priv->tt.vn);
2382
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2383
		   "Local changes committed, updating to ttvn %u\n",
2384
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
2385 2386

	/* reset the sending counter */
2387
	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2388
	batadv_tt_tvlv_container_update(bat_priv);
2389
}
2390

2391
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2392
			   uint8_t *dst)
2393
{
2394 2395
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
2396
	bool ret = false;
2397 2398

	if (!atomic_read(&bat_priv->ap_isolation))
2399
		goto out;
2400

2401
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2402 2403 2404
	if (!tt_local_entry)
		goto out;

2405
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2406 2407 2408
	if (!tt_global_entry)
		goto out;

2409
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2410 2411
		goto out;

2412
	ret = true;
2413 2414 2415

out:
	if (tt_global_entry)
2416
		batadv_tt_global_entry_free_ref(tt_global_entry);
2417
	if (tt_local_entry)
2418
		batadv_tt_local_entry_free_ref(tt_local_entry);
2419 2420
	return ret;
}
2421

2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
/**
 * batadv_tt_update_orig - update global translation table with new tt
 *  information received via ogms
 * @bat_priv: the bat priv with all the soft interface information
 * @orig: the orig_node of the ogm
 * @tt_buff: buffer holding the tt information
 * @tt_num_changes: number of tt changes inside the tt buffer
 * @ttvn: translation table version number of this changeset
 * @tt_crc: crc16 checksum of orig node's translation table
 */
static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *orig_node,
				  const unsigned char *tt_buff,
				  uint16_t tt_num_changes, uint8_t ttvn,
				  uint16_t tt_crc)
2437 2438 2439
{
	uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	bool full_table = true;
2440
	struct batadv_tt_change *tt_change;
2441

2442
	/* don't care about a backbone gateways updates. */
2443
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2444 2445
		return;

2446
	/* orig table not initialised AND first diff is in the OGM OR the ttvn
2447 2448
	 * increased by one -> we can apply the attached changes
	 */
2449 2450
	if ((!orig_node->tt_initialised && ttvn == 1) ||
	    ttvn - orig_ttvn == 1) {
2451
		/* the OGM could not contain the changes due to their size or
2452 2453
		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
		 * times.
2454 2455
		 * In this case send a tt request
		 */
2456 2457 2458 2459 2460
		if (!tt_num_changes) {
			full_table = false;
			goto request_table;
		}

2461
		tt_change = (struct batadv_tt_change *)tt_buff;
2462
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2463
					 ttvn, tt_change);
2464 2465 2466

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
2467 2468
		 * in the global table
		 */
2469
		orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2470 2471 2472 2473 2474 2475 2476 2477

		/* 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
2478 2479
		 * inconsistency
		 */
2480 2481 2482 2483
		if (orig_node->tt_crc != tt_crc)
			goto request_table;
	} else {
		/* if we missed more than one change or our tables are not
2484 2485
		 * in sync anymore -> request fresh tt data
		 */
2486 2487
		if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
		    orig_node->tt_crc != tt_crc) {
2488
request_table:
2489
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2490
				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2491 2492
				   orig_node->orig, ttvn, orig_ttvn, tt_crc,
				   orig_node->tt_crc, tt_num_changes);
2493 2494
			batadv_send_tt_request(bat_priv, orig_node, ttvn,
					       tt_crc, full_table);
2495 2496 2497 2498
			return;
		}
	}
}
2499 2500 2501 2502 2503

/* 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
 */
2504
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2505
					uint8_t *addr)
2506
{
2507
	struct batadv_tt_global_entry *tt_global_entry;
2508 2509
	bool ret = false;

2510
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2511 2512 2513
	if (!tt_global_entry)
		goto out;

2514
	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2515
	batadv_tt_global_entry_free_ref(tt_global_entry);
2516 2517 2518
out:
	return ret;
}
2519

2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
/**
 * 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;
}

2545 2546 2547 2548 2549 2550
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;

2551 2552 2553 2554 2555 2556 2557
	/* if the originator is a backbone node (meaning it belongs to the same
	 * LAN of this node) the temporary client must not be added because to
	 * reach such destination the node must use the LAN instead of the mesh
	 */
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
		goto out;

2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
	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;
}
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627

/**
 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
 * @bat_priv: the bat priv with all the soft interface information
 * @orig: the orig_node of the ogm
 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
 * @tvlv_value: tvlv buffer containing the gateway data
 * @tvlv_value_len: tvlv buffer length
 */
static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
					  struct batadv_orig_node *orig,
					  uint8_t flags,
					  void *tvlv_value,
					  uint16_t tvlv_value_len)
{
	struct batadv_tvlv_tt_data *tt_data;
	uint16_t num_entries;

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

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

	num_entries = tvlv_value_len / batadv_tt_len(1);

	batadv_tt_update_orig(bat_priv, orig,
			      (unsigned char *)(tt_data + 1),
			      num_entries, tt_data->ttvn, ntohs(tt_data->crc));
}

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

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

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

	batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
				     NULL, BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);

	INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));

	return 1;
}