translation-table.c 78.6 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 233 234 235 236 237 238
/**
 * batadv_tt_len - compute length in bytes of given number of tt changes
 * @changes_num: number of tt changes
 *
 * Returns computed length in bytes.
 */
static int batadv_tt_len(int changes_num)
239
{
240
	return changes_num * sizeof(struct batadv_tvlv_tt_change);
241 242
}

243
static int batadv_tt_local_init(struct batadv_priv *bat_priv)
244
{
245
	if (bat_priv->tt.local_hash)
246
		return 0;
247

248
	bat_priv->tt.local_hash = batadv_hash_new(1024);
249

250
	if (!bat_priv->tt.local_hash)
251
		return -ENOMEM;
252

253 254 255
	batadv_hash_set_lock_class(bat_priv->tt.local_hash,
				   &batadv_tt_local_hash_lock_class_key);

256
	return 0;
257 258
}

259 260 261 262 263 264 265 266 267 268 269 270 271
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);
}

272 273
void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
			 int ifindex)
274
{
275
	struct batadv_priv *bat_priv = netdev_priv(soft_iface);
276 277
	struct batadv_tt_local_entry *tt_local;
	struct batadv_tt_global_entry *tt_global;
278
	struct hlist_head *head;
279
	struct batadv_tt_orig_list_entry *orig_entry;
280
	int hash_added;
281
	bool roamed_back = false;
282

283
	tt_local = batadv_tt_local_hash_find(bat_priv, addr);
284
	tt_global = batadv_tt_global_hash_find(bat_priv, addr);
285

286 287
	if (tt_local) {
		tt_local->last_seen = jiffies;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		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;
313 314
	}

315 316
	tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
	if (!tt_local)
317
		goto out;
318

319
	batadv_dbg(BATADV_DBG_TT, bat_priv,
320
		   "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
321
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
322

323
	memcpy(tt_local->common.addr, addr, ETH_ALEN);
324 325 326 327 328
	/* 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;
329
	if (batadv_is_wifi_iface(ifindex))
330 331 332 333
		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;
334 335

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

339
	hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
340 341
				     batadv_choose_orig, &tt_local->common,
				     &tt_local->common.hash_entry);
342 343 344

	if (unlikely(hash_added != 0)) {
		/* remove the reference for the hash */
345
		batadv_tt_local_entry_free_ref(tt_local);
346 347 348
		goto out;
	}

349
add_event:
350
	batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
351

352 353 354 355 356
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)) {
357
		/* These node are probably going to update their tt table */
358
		head = &tt_global->orig_list;
359
		rcu_read_lock();
360
		hlist_for_each_entry_rcu(orig_entry, head, list) {
361
			batadv_send_roam_adv(bat_priv, tt_global->common.addr,
362
					     orig_entry->orig_node);
363 364
		}
		rcu_read_unlock();
365 366 367 368 369 370 371 372 373 374 375
		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;
		}
376
	}
377

378
out:
379 380 381 382
	if (tt_local)
		batadv_tt_local_entry_free_ref(tt_local);
	if (tt_global)
		batadv_tt_global_entry_free_ref(tt_global);
383 384
}

385 386 387 388 389 390
/**
 * 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)
391
{
392 393 394 395 396
	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;
397

398
	tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
399 400 401 402

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

406 407 408
	tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
	if (!tt_data)
		return;
409

410 411 412
	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);
413

414 415
	if (tt_diff_len == 0)
		goto container_register;
416

417
	tt_diff_entries_num = tt_diff_len / batadv_tt_len(1);
418

419 420
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
	atomic_set(&bat_priv->tt.local_changes, 0);
421

422 423
	tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);

424
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
425
				 list) {
426 427 428 429 430
		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++;
431
		}
432 433
		list_del(&entry->list);
		kfree(entry);
434
	}
435
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
436 437

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

457 458 459 460
container_register:
	batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
				       sizeof(*tt_data) + tt_change_len);
	kfree(tt_data);
461 462
}

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

479 480
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
481
		goto out;
482

483
	seq_printf(seq,
484 485 486
		   "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);
487 488
	seq_printf(seq, "       %-13s %-7s %-10s\n", "Client", "Flags",
		   "Last seen");
489 490 491 492

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

493
		rcu_read_lock();
494
		hlist_for_each_entry_rcu(tt_common_entry,
495
					 head, hash_entry) {
496 497 498 499 500 501 502 503 504 505 506
			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",
507 508
				   tt_common_entry->addr,
				   (tt_common_entry->flags &
509
				    BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
510
				   no_purge ? 'P' : '.',
511
				   (tt_common_entry->flags &
512
				    BATADV_TT_CLIENT_NEW ? 'N' : '.'),
513
				   (tt_common_entry->flags &
514
				    BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
515
				   (tt_common_entry->flags &
516
				    BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
517 518
				   no_purge ? 0 : last_seen_secs,
				   no_purge ? 0 : last_seen_msecs);
519
		}
520
		rcu_read_unlock();
521
	}
522 523
out:
	if (primary_if)
524
		batadv_hardif_free_ref(primary_if);
525
	return 0;
526 527
}

528 529 530 531
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)
532
{
533
	batadv_tt_local_event(bat_priv, tt_local_entry, flags);
534

535 536
	/* 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
537 538
	 * response issued before the net ttvn increment (consistency check)
	 */
539
	tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
540

541
	batadv_dbg(BATADV_DBG_TT, bat_priv,
542 543
		   "Local tt entry (%pM) pending to be removed: %s\n",
		   tt_local_entry->common.addr, message);
544 545
}

546 547 548 549 550 551 552 553 554 555 556 557
/**
 * 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)
558
{
559
	struct batadv_tt_local_entry *tt_local_entry;
560
	uint16_t flags, curr_flags = BATADV_NO_FLAGS;
561

562
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
563 564 565
	if (!tt_local_entry)
		goto out;

566 567
	curr_flags = tt_local_entry->common.flags;

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

579 580 581 582 583 584 585 586
	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
	 */
587
	batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
588 589
	hlist_del_rcu(&tt_local_entry->common.hash_entry);
	batadv_tt_local_entry_free_ref(tt_local_entry);
590

591 592
out:
	if (tt_local_entry)
593
		batadv_tt_local_entry_free_ref(tt_local_entry);
594 595

	return curr_flags;
596 597
}

598
static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
599
				       struct hlist_head *head)
600
{
601 602
	struct batadv_tt_local_entry *tt_local_entry;
	struct batadv_tt_common_entry *tt_common_entry;
603
	struct hlist_node *node_tmp;
604

605
	hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
606 607
				  hash_entry) {
		tt_local_entry = container_of(tt_common_entry,
608 609
					      struct batadv_tt_local_entry,
					      common);
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		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");
	}
}

626
static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
627
{
628
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
629
	struct hlist_head *head;
630
	spinlock_t *list_lock; /* protects write access to the hash lists */
631
	uint32_t i;
632 633 634

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

637
		spin_lock_bh(list_lock);
638
		batadv_tt_local_purge_list(bat_priv, head);
639
		spin_unlock_bh(list_lock);
640 641 642
	}
}

643
static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
644
{
645
	struct batadv_hashtable *hash;
646
	spinlock_t *list_lock; /* protects write access to the hash lists */
647 648
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_local_entry *tt_local;
649
	struct hlist_node *node_tmp;
650
	struct hlist_head *head;
651
	uint32_t i;
652

653
	if (!bat_priv->tt.local_hash)
654 655
		return;

656
	hash = bat_priv->tt.local_hash;
657 658 659 660 661 662

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

		spin_lock_bh(list_lock);
663
		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
664
					  head, hash_entry) {
665
			hlist_del_rcu(&tt_common_entry->hash_entry);
666 667 668 669
			tt_local = container_of(tt_common_entry,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
670 671 672 673
		}
		spin_unlock_bh(list_lock);
	}

674
	batadv_hash_destroy(hash);
675

676
	bat_priv->tt.local_hash = NULL;
677 678
}

679
static int batadv_tt_global_init(struct batadv_priv *bat_priv)
680
{
681
	if (bat_priv->tt.global_hash)
682
		return 0;
683

684
	bat_priv->tt.global_hash = batadv_hash_new(1024);
685

686
	if (!bat_priv->tt.global_hash)
687
		return -ENOMEM;
688

689 690 691
	batadv_hash_set_lock_class(bat_priv->tt.global_hash,
				   &batadv_tt_global_hash_lock_class_key);

692
	return 0;
693 694
}

695
static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
696
{
697
	struct batadv_tt_change_node *entry, *safe;
698

699
	spin_lock_bh(&bat_priv->tt.changes_list_lock);
700

701
	list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
702 703 704 705
				 list) {
		list_del(&entry->list);
		kfree(entry);
	}
706

707 708
	atomic_set(&bat_priv->tt.local_changes, 0);
	spin_unlock_bh(&bat_priv->tt.changes_list_lock);
709
}
710

711 712 713 714
/* 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
715
 */
716 717 718
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)
719
{
720
	struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
721 722 723 724
	const struct hlist_head *head;

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

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

755 756 757
	return found;
}

758
static void
759
batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
760
				struct batadv_orig_node *orig_node, int ttvn)
761
{
762
	struct batadv_tt_orig_list_entry *orig_entry;
763

764
	orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
765 766 767 768 769
	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;
770
		goto out;
771
	}
772

773 774
	orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
	if (!orig_entry)
775
		goto out;
776 777 778 779 780 781

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

784
	spin_lock_bh(&tt_global->list_lock);
785
	hlist_add_head_rcu(&orig_entry->list,
786 787 788 789 790
			   &tt_global->orig_list);
	spin_unlock_bh(&tt_global->list_lock);
out:
	if (orig_entry)
		batadv_tt_orig_list_entry_free_ref(orig_entry);
791 792
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
/**
 * 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.
 */
809 810
int batadv_tt_global_add(struct batadv_priv *bat_priv,
			 struct batadv_orig_node *orig_node,
811
			 const unsigned char *tt_addr, uint16_t flags,
812
			 uint8_t ttvn)
813
{
814 815
	struct batadv_tt_global_entry *tt_global_entry;
	struct batadv_tt_local_entry *tt_local_entry;
816
	int ret = 0;
817
	int hash_added;
818
	struct batadv_tt_common_entry *common;
819
	uint16_t local_flags;
820

821
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
822 823 824 825 826 827 828 829 830
	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;
831 832

	if (!tt_global_entry) {
833
		tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
834
		if (!tt_global_entry)
835 836
			goto out;

837 838
		common = &tt_global_entry->common;
		memcpy(common->addr, tt_addr, ETH_ALEN);
839

840
		common->flags = flags;
841
		tt_global_entry->roam_at = 0;
842 843 844 845 846 847
		/* 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;
848
		atomic_set(&common->refcount, 2);
849
		common->added_at = jiffies;
850 851 852

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

854
		hash_added = batadv_hash_add(bat_priv->tt.global_hash,
855 856 857
					     batadv_compare_tt,
					     batadv_choose_orig, common,
					     &common->hash_entry);
858 859 860

		if (unlikely(hash_added != 0)) {
			/* remove the reference for the hash */
861
			batadv_tt_global_entry_free_ref(tt_global_entry);
862 863
			goto out_remove;
		}
864
	} else {
865
		common = &tt_global_entry->common;
866 867
		/* If there is already a global entry, we can use this one for
		 * our processing.
868 869 870 871 872 873 874
		 * 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
875
		 */
876 877 878 879 880 881 882 883 884
		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;
		}
885 886 887 888

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

891 892 893 894 895 896
		/* 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;

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

914
	batadv_dbg(BATADV_DBG_TT, bat_priv,
915
		   "Creating new global tt entry: %pM (via %pM)\n",
916
		   common->addr, orig_node->orig);
917
	ret = 1;
918

919
out_remove:
920

921
	/* remove address from local hash if present */
922 923
	local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
					     "global tt received",
924
					     flags & BATADV_TT_CLIENT_ROAM);
925 926
	tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;

927 928 929 930 931 932
	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;

933 934
out:
	if (tt_global_entry)
935
		batadv_tt_global_entry_free_ref(tt_global_entry);
936 937
	if (tt_local_entry)
		batadv_tt_local_entry_free_ref(tt_local_entry);
938
	return ret;
939 940
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
/* 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;
956
	hlist_for_each_entry_rcu(orig_entry, head, list) {
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
		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().
978
 */
979
static void
980
batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
981
			     struct seq_file *seq)
982 983
{
	struct hlist_head *head;
984
	struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
985
	struct batadv_tt_common_entry *tt_common_entry;
986 987 988 989
	uint16_t flags;
	uint8_t last_ttvn;

	tt_common_entry = &tt_global_entry->common;
990 991 992 993 994
	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);
995 996
		seq_printf(seq,
			   " %c %pM  (%3u) via %pM     (%3u)   (%#.4x) [%c%c%c]\n",
997 998
			   '*', tt_global_entry->common.addr,
			   best_entry->ttvn, best_entry->orig_node->orig,
999
			   last_ttvn, best_entry->orig_node->tt_crc,
1000 1001 1002 1003
			   (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
			   (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
			   (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
	}
1004 1005 1006

	head = &tt_global_entry->orig_list;

1007
	hlist_for_each_entry_rcu(orig_entry, head, list) {
1008 1009 1010
		if (best_entry == orig_entry)
			continue;

1011
		last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1012 1013 1014 1015
		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,
1016
			   (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1017 1018
			   (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
			   (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1019 1020 1021
	}
}

1022
int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1023 1024
{
	struct net_device *net_dev = (struct net_device *)seq->private;
1025
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1026
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1027 1028 1029
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
	struct batadv_hard_iface *primary_if;
1030
	struct hlist_head *head;
1031
	uint32_t i;
1032

1033 1034
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1035
		goto out;
1036

1037 1038
	seq_printf(seq,
		   "Globally announced TT entries received via the mesh %s\n",
1039
		   net_dev->name);
1040 1041 1042
	seq_printf(seq, "       %-13s %s       %-15s %s (%-6s) %s\n",
		   "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
		   "Flags");
1043 1044 1045 1046

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

1047
		rcu_read_lock();
1048
		hlist_for_each_entry_rcu(tt_common_entry,
1049
					 head, hash_entry) {
1050 1051 1052 1053
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_print_entry(tt_global, seq);
1054
		}
1055
		rcu_read_unlock();
1056
	}
1057 1058
out:
	if (primary_if)
1059
		batadv_hardif_free_ref(primary_if);
1060
	return 0;
1061 1062
}

1063
/* deletes the orig list of a tt_global_entry */
1064
static void
1065
batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1066
{
1067
	struct hlist_head *head;
1068
	struct hlist_node *safe;
1069
	struct batadv_tt_orig_list_entry *orig_entry;
1070

1071 1072
	spin_lock_bh(&tt_global_entry->list_lock);
	head = &tt_global_entry->orig_list;
1073 1074
	hlist_for_each_entry_safe(orig_entry, safe, head, list) {
		hlist_del_rcu(&orig_entry->list);
1075
		batadv_tt_orig_list_entry_free_ref(orig_entry);
1076 1077 1078 1079
	}
	spin_unlock_bh(&tt_global_entry->list_lock);
}

1080
static void
1081 1082 1083
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,
1084
				const char *message)
1085 1086
{
	struct hlist_head *head;
1087
	struct hlist_node *safe;
1088
	struct batadv_tt_orig_list_entry *orig_entry;
1089 1090 1091

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

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

	/* 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;
1125
	hlist_for_each_entry_rcu(orig_entry, head, list) {
1126 1127 1128 1129 1130 1131 1132 1133 1134
		if (orig_entry->orig_node != orig_node) {
			last_entry = false;
			break;
		}
	}
	rcu_read_unlock();

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



1147 1148
static void batadv_tt_global_del(struct batadv_priv *bat_priv,
				 struct batadv_orig_node *orig_node,
1149 1150
				 const unsigned char *addr,
				 const char *message, bool roaming)
1151
{
1152
	struct batadv_tt_global_entry *tt_global_entry;
1153
	struct batadv_tt_local_entry *local_entry = NULL;
1154

1155
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1156
	if (!tt_global_entry)
1157
		goto out;
1158

1159
	if (!roaming) {
1160 1161
		batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
						orig_node, message);
1162 1163

		if (hlist_empty(&tt_global_entry->orig_list))
1164 1165
			batadv_tt_global_free(bat_priv, tt_global_entry,
					      message);
1166 1167 1168

		goto out;
	}
1169 1170 1171

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


1195
out:
1196
	if (tt_global_entry)
1197 1198 1199
		batadv_tt_global_entry_free_ref(tt_global_entry);
	if (local_entry)
		batadv_tt_local_entry_free_ref(local_entry);
1200 1201
}

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

1214 1215 1216
	if (!hash)
		return;

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

1221
		spin_lock_bh(list_lock);
1222
		hlist_for_each_entry_safe(tt_common_entry, safe,
1223
					  head, hash_entry) {
1224 1225 1226
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
1227

1228
			batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1229
							orig_node, message);
1230

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

1244 1245
static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
				      char **msg)
1246
{
1247 1248 1249
	bool purge = false;
	unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
	unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1250

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

1257 1258 1259 1260
	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";
1261
	}
1262 1263

	return purge;
1264 1265
}

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

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

1281
		spin_lock_bh(list_lock);
1282
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
					  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);

1295
			hlist_del_rcu(&tt_common->hash_entry);
1296 1297 1298

			batadv_tt_global_entry_free_ref(tt_global);
		}
1299
		spin_unlock_bh(list_lock);
1300 1301 1302
	}
}

1303
static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1304
{
1305
	struct batadv_hashtable *hash;
1306
	spinlock_t *list_lock; /* protects write access to the hash lists */
1307 1308
	struct batadv_tt_common_entry *tt_common_entry;
	struct batadv_tt_global_entry *tt_global;
1309
	struct hlist_node *node_tmp;
1310
	struct hlist_head *head;
1311
	uint32_t i;
1312

1313
	if (!bat_priv->tt.global_hash)
1314 1315
		return;

1316
	hash = bat_priv->tt.global_hash;
1317 1318 1319 1320 1321 1322

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

		spin_lock_bh(list_lock);
1323
		hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1324
					  head, hash_entry) {
1325
			hlist_del_rcu(&tt_common_entry->hash_entry);
1326 1327 1328 1329
			tt_global = container_of(tt_common_entry,
						 struct batadv_tt_global_entry,
						 common);
			batadv_tt_global_entry_free_ref(tt_global);
1330 1331 1332 1333
		}
		spin_unlock_bh(list_lock);
	}

1334
	batadv_hash_destroy(hash);
1335

1336
	bat_priv->tt.global_hash = NULL;
1337 1338
}

1339 1340 1341
static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
		       struct batadv_tt_global_entry *tt_global_entry)
1342 1343 1344
{
	bool ret = false;

1345 1346
	if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
	    tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1347 1348 1349 1350 1351
		ret = true;

	return ret;
}

1352 1353 1354
struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
						  const uint8_t *src,
						  const uint8_t *addr)
1355
{
1356 1357 1358
	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;
1359
	struct batadv_tt_orig_list_entry *best_entry;
1360

1361
	if (src && atomic_read(&bat_priv->ap_isolation)) {
1362
		tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1363 1364
		if (!tt_local_entry ||
		    (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1365 1366
			goto out;
	}
1367

1368
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1369
	if (!tt_global_entry)
1370
		goto out;
1371

1372
	/* check whether the clients should not communicate due to AP
1373 1374
	 * isolation
	 */
1375 1376
	if (tt_local_entry &&
	    _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1377 1378
		goto out;

1379
	rcu_read_lock();
1380
	best_entry = batadv_transtable_best_orig(tt_global_entry);
1381
	/* found anything? */
1382 1383
	if (best_entry)
		orig_node = best_entry->orig_node;
1384 1385 1386
	if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
		orig_node = NULL;
	rcu_read_unlock();
1387

1388
out:
1389
	if (tt_global_entry)
1390
		batadv_tt_global_entry_free_ref(tt_global_entry);
1391
	if (tt_local_entry)
1392
		batadv_tt_local_entry_free_ref(tt_local_entry);
1393

1394
	return orig_node;
1395
}
1396 1397

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

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

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

			/* find out if this global entry is announced by this
			 * originator
			 */
1434
			if (!batadv_tt_global_entry_has_orig(tt_global,
1435
							     orig_node))
1436 1437 1438 1439 1440
				continue;

			total_one = 0;
			for (j = 0; j < ETH_ALEN; j++)
				total_one = crc16_byte(total_one,
1441
						       tt_common->addr[j]);
1442
			total ^= total_one;
1443 1444 1445 1446 1447 1448 1449 1450
		}
		rcu_read_unlock();
	}

	return total;
}

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

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

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

	return total;
}

1482
static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1483
{
1484
	struct batadv_tt_req_node *node, *safe;
1485

1486
	spin_lock_bh(&bat_priv->tt.req_list_lock);
1487

1488
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1489 1490 1491 1492
		list_del(&node->list);
		kfree(node);
	}

1493
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1494 1495
}

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

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

1519
static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1520
{
1521
	struct batadv_tt_req_node *node, *safe;
1522

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

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

1543 1544
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1545 1546
		if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
		    !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1547
					  BATADV_TT_REQUEST_TIMEOUT))
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
			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;

1558
	list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1559
unlock:
1560
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
1561 1562 1563
	return tt_req_node;
}

1564 1565 1566 1567 1568 1569 1570 1571
/**
 * batadv_tt_local_valid - verify that given tt entry is a valid one
 * @entry_ptr: to be checked local tt entry
 * @data_ptr: not used but definition required to satisfy the callback prototype
 *
 * Returns 1 if the entry is a valid, 0 otherwise.
 */
static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
1572
{
1573
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1574

1575
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1576 1577 1578 1579
		return 0;
	return 1;
}

1580 1581
static int batadv_tt_global_valid(const void *entry_ptr,
				  const void *data_ptr)
1582
{
1583 1584 1585
	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;
1586

1587 1588
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1589 1590
		return 0;

1591 1592
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
1593 1594
				       common);

1595
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1596 1597
}

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
/**
 * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
 *  tt entries from the specified tt hash
 * @bat_priv: the bat priv with all the soft interface information
 * @hash: hash table containing the tt entries
 * @tt_len: expected tvlv tt data buffer length in number of bytes
 * @valid_cb: function to filter tt change entries
 * @cb_data: data passed to the filter function as argument
 *
 * Returns pointer to allocated tvlv tt data buffer if operation was
 * successful or NULL otherwise.
 */
static struct batadv_tvlv_tt_data *
batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
			struct batadv_hashtable *hash, uint16_t tt_len,
			int (*valid_cb)(const void *, const void *),
			void *cb_data)
1615
{
1616
	struct batadv_tt_common_entry *tt_common_entry;
1617 1618
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
	struct batadv_tvlv_tt_change *tt_change;
1619
	struct hlist_head *head;
1620 1621
	uint16_t tt_tot, tt_num_entries = 0;
	ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
1622
	uint32_t i;
1623

1624 1625 1626
	if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
		tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
		tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
1627 1628
	}

1629
	tt_tot = tt_len / sizeof(struct batadv_tvlv_tt_change);
1630

1631 1632 1633 1634
	tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
			       GFP_ATOMIC);
	if (!tvlv_tt_data)
		goto out;
1635

1636
	tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
1637 1638 1639 1640 1641

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

1642
		hlist_for_each_entry_rcu(tt_common_entry,
1643
					 head, hash_entry) {
1644
			if (tt_tot == tt_num_entries)
1645 1646
				break;

1647
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1648 1649
				continue;

1650 1651
			memcpy(tt_change->addr, tt_common_entry->addr,
			       ETH_ALEN);
1652
			tt_change->flags = tt_common_entry->flags;
1653
			tt_change->reserved = 0;
1654

1655
			tt_num_entries++;
1656 1657 1658 1659 1660 1661
			tt_change++;
		}
	}
	rcu_read_unlock();

out:
1662
	return tvlv_tt_data;
1663 1664
}

1665 1666
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *dst_orig_node,
1667 1668
				  uint8_t ttvn, uint16_t tt_crc,
				  bool full_table)
1669
{
1670
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1671 1672
	struct batadv_hard_iface *primary_if;
	struct batadv_tt_req_node *tt_req_node = NULL;
1673
	bool ret = false;
1674

1675
	primary_if = batadv_primary_if_get_selected(bat_priv);
1676 1677 1678 1679
	if (!primary_if)
		goto out;

	/* The new tt_req will be issued only if I'm not waiting for a
1680 1681
	 * reply from the same orig_node yet
	 */
1682
	tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1683 1684 1685
	if (!tt_req_node)
		goto out;

1686 1687
	tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
	if (!tvlv_tt_data)
1688 1689
		goto out;

1690 1691 1692
	tvlv_tt_data->flags = BATADV_TT_REQUEST;
	tvlv_tt_data->ttvn = ttvn;
	tvlv_tt_data->crc = htons(tt_crc);
1693 1694

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

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

1700
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1701 1702 1703 1704
	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
				 dst_orig_node->orig, BATADV_TVLV_TT, 1,
				 tvlv_tt_data, sizeof(*tvlv_tt_data));
	ret = true;
1705 1706 1707

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

1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
/**
 * batadv_send_other_tt_response - send reply to tt request concerning another
 *  node's translation table
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @req_src: mac address of tt request sender
 * @req_dst: mac address of tt request recipient
 *
 * Returns true if tt request reply was sent, false otherwise.
 */
static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
					  struct batadv_tvlv_tt_data *tt_data,
					  uint8_t *req_src, uint8_t *req_dst)
1732
{
1733
	struct batadv_orig_node *req_dst_orig_node;
1734
	struct batadv_orig_node *res_dst_orig_node = NULL;
1735 1736 1737 1738
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
	uint8_t orig_ttvn, req_ttvn;
	uint16_t tt_len;
	bool ret = false, full_table;
1739

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

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

1750
	res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
1751 1752 1753 1754
	if (!res_dst_orig_node)
		goto out;

	orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1755
	req_ttvn = tt_data->ttvn;
1756

1757
	/* this node doesn't have the requested data */
1758
	if (orig_ttvn != req_ttvn ||
1759
	    tt_data->crc != htons(req_dst_orig_node->tt_crc))
1760 1761
		goto out;

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

1769 1770
	/* TT fragmentation hasn't been implemented yet, so send as many
	 * TT entries fit a single packet as possible only
1771
	 */
1772 1773 1774 1775
	if (!full_table) {
		spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
		tt_len = req_dst_orig_node->tt_buff_len;

1776 1777 1778
		tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
				       GFP_ATOMIC);
		if (!tvlv_tt_data)
1779 1780 1781
			goto unlock;

		/* Copy the last orig_node's OGM buffer */
1782
		memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
1783 1784 1785
		       req_dst_orig_node->tt_buff_len);
		spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
	} else {
1786
		tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1787 1788 1789 1790 1791 1792 1793 1794
		tt_len = batadv_tt_len(tt_len);

		tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
						       bat_priv->tt.global_hash,
						       tt_len,
						       batadv_tt_global_valid,
						       req_dst_orig_node);
		if (!tvlv_tt_data)
1795 1796 1797
			goto out;
	}

1798 1799
	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
	tvlv_tt_data->ttvn = req_ttvn;
1800 1801

	if (full_table)
1802
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1803

1804
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1805 1806 1807
		   "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
		   res_dst_orig_node->orig, req_dst_orig_node->orig,
		   full_table ? 'F' : '.', req_ttvn);
1808

1809
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1810

1811 1812 1813
	batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
				 req_src, BATADV_TVLV_TT, 1,
				 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
1814

1815
	ret = true;
1816 1817 1818 1819 1820 1821 1822
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
1823
		batadv_orig_node_free_ref(res_dst_orig_node);
1824
	if (req_dst_orig_node)
1825
		batadv_orig_node_free_ref(req_dst_orig_node);
1826
	kfree(tvlv_tt_data);
1827 1828
	return ret;
}
1829

1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
/**
 * batadv_send_my_tt_response - send reply to tt request concerning this node's
 *  translation table
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @req_src: mac address of tt request sender
 *
 * Returns true if tt request reply was sent, false otherwise.
 */
static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
				       struct batadv_tvlv_tt_data *tt_data,
				       uint8_t *req_src)
1842
{
1843
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1844
	struct batadv_orig_node *orig_node;
1845
	struct batadv_hard_iface *primary_if = NULL;
1846
	uint8_t my_ttvn, req_ttvn;
1847
	bool full_table;
1848
	uint16_t tt_len;
1849

1850
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1851
		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1852 1853
		   req_src, tt_data->ttvn,
		   (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1854 1855


1856
	my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1857
	req_ttvn = tt_data->ttvn;
1858

1859
	orig_node = batadv_orig_hash_find(bat_priv, req_src);
1860 1861 1862
	if (!orig_node)
		goto out;

1863
	primary_if = batadv_primary_if_get_selected(bat_priv);
1864 1865 1866 1867
	if (!primary_if)
		goto out;

	/* If the full table has been explicitly requested or the gap
1868 1869
	 * is too big send the whole local translation table
	 */
1870
	if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1871
	    !bat_priv->tt.last_changeset)
1872 1873 1874 1875
		full_table = true;
	else
		full_table = false;

1876 1877
	/* TT fragmentation hasn't been implemented yet, so send as many
	 * TT entries fit a single packet as possible only
1878
	 */
1879
	if (!full_table) {
1880 1881
		spin_lock_bh(&bat_priv->tt.last_changeset_lock);
		tt_len = bat_priv->tt.last_changeset_len;
1882

1883 1884 1885
		tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
				       GFP_ATOMIC);
		if (!tvlv_tt_data)
1886 1887
			goto unlock;

1888 1889
		/* Copy the last orig_node's OGM buffer */
		memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
1890 1891
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1892
	} else {
1893
		tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1894 1895 1896 1897 1898 1899 1900 1901 1902
		tt_len = batadv_tt_len(tt_len);
		req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);

		tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
						       bat_priv->tt.local_hash,
						       tt_len,
						       batadv_tt_local_valid,
						       NULL);
		if (!tvlv_tt_data)
1903 1904 1905
			goto out;
	}

1906 1907
	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
	tvlv_tt_data->ttvn = req_ttvn;
1908 1909

	if (full_table)
1910
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1911

1912
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1913 1914
		   "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
		   orig_node->orig, full_table ? 'F' : '.', req_ttvn);
1915

1916
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1917

1918 1919 1920 1921
	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
				 req_src, BATADV_TVLV_TT, 1,
				 tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);

1922 1923 1924
	goto out;

unlock:
1925
	spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1926 1927
out:
	if (orig_node)
1928
		batadv_orig_node_free_ref(orig_node);
1929
	if (primary_if)
1930
		batadv_hardif_free_ref(primary_if);
1931 1932
	kfree(tvlv_tt_data);
	/* The packet was for this host, so it doesn't need to be re-routed */
1933 1934 1935
	return true;
}

1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
/**
 * batadv_send_tt_response - send reply to tt request
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @req_src: mac address of tt request sender
 * @req_dst: mac address of tt request recipient
 *
 * Returns true if tt request reply was sent, false otherwise.
 */
static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
				    struct batadv_tvlv_tt_data *tt_data,
				    uint8_t *req_src, uint8_t *req_dst)
1948
{
1949
	if (batadv_is_my_mac(bat_priv, req_dst)) {
1950
		/* don't answer backbone gws! */
1951
		if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
1952 1953
			return true;

1954
		return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
1955
	} else {
1956 1957
		return batadv_send_other_tt_response(bat_priv, tt_data,
						     req_src, req_dst);
1958
	}
1959 1960
}

1961 1962
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
1963
				      struct batadv_tvlv_tt_change *tt_change,
1964
				      uint16_t tt_num_changes, uint8_t ttvn)
1965 1966
{
	int i;
1967
	int roams;
1968 1969

	for (i = 0; i < tt_num_changes; i++) {
1970 1971
		if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
			roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1972 1973
			batadv_tt_global_del(bat_priv, orig_node,
					     (tt_change + i)->addr,
1974 1975
					     "tt removed by changes",
					     roams);
1976 1977
		} else {
			if (!batadv_tt_global_add(bat_priv, orig_node,
1978 1979
						  (tt_change + i)->addr,
						  (tt_change + i)->flags, ttvn))
1980 1981 1982 1983 1984 1985 1986
				/* 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;
1987
		}
1988
	}
1989
	orig_node->tt_initialised = true;
1990 1991
}

1992
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1993 1994
				  struct batadv_tvlv_tt_data *tt_data,
				  uint8_t *resp_src, uint16_t num_entries)
1995
{
1996
	struct batadv_orig_node *orig_node;
1997

1998
	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
1999 2000 2001 2002
	if (!orig_node)
		goto out;

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

2005
	_batadv_tt_update_changes(bat_priv, orig_node,
2006 2007
				  (struct batadv_tvlv_tt_change *)(tt_data + 1),
				  num_entries, tt_data->ttvn);
2008 2009 2010 2011 2012 2013 2014

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

2015
	atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
2016 2017 2018

out:
	if (orig_node)
2019
		batadv_orig_node_free_ref(orig_node);
2020 2021
}

2022 2023
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
2024
				     uint16_t tt_num_changes, uint8_t ttvn,
2025
				     struct batadv_tvlv_tt_change *tt_change)
2026
{
2027 2028
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
2029

2030 2031
	batadv_tt_save_orig_buffer(bat_priv, orig_node,
				   (unsigned char *)tt_change, tt_num_changes);
2032 2033 2034
	atomic_set(&orig_node->last_ttvn, ttvn);
}

2035
bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2036
{
2037
	struct batadv_tt_local_entry *tt_local_entry;
2038
	bool ret = false;
2039

2040
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2041 2042
	if (!tt_local_entry)
		goto out;
2043
	/* Check if the client has been logically deleted (but is kept for
2044 2045
	 * consistency purpose)
	 */
2046 2047
	if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
	    (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2048
		goto out;
2049 2050
	ret = true;
out:
2051
	if (tt_local_entry)
2052
		batadv_tt_local_entry_free_ref(tt_local_entry);
2053
	return ret;
2054 2055
}

2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
/**
 * batadv_handle_tt_response - process incoming tt reply
 * @bat_priv: the bat priv with all the soft interface information
 * @tt_data: tt data containing the tt request information
 * @resp_src: mac address of tt reply sender
 * @num_entries: number of tt change entries appended to the tt data
 */
static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
				      struct batadv_tvlv_tt_data *tt_data,
				      uint8_t *resp_src, uint16_t num_entries)
2066
{
2067 2068
	struct batadv_tt_req_node *node, *safe;
	struct batadv_orig_node *orig_node = NULL;
2069
	struct batadv_tvlv_tt_change *tt_change;
2070

2071
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2072
		   "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2073 2074
		   resp_src, tt_data->ttvn, num_entries,
		   (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2075

2076
	/* we should have never asked a backbone gw */
2077
	if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
2078 2079
		goto out;

2080
	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2081 2082 2083
	if (!orig_node)
		goto out;

2084 2085
	if (tt_data->flags & BATADV_TT_FULL_TABLE) {
		batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
2086
	} else {
2087 2088 2089
		tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
		batadv_tt_update_changes(bat_priv, orig_node, num_entries,
					 tt_data->ttvn, tt_change);
2090
	}
2091 2092

	/* Delete the tt_req_node from pending tt_requests list */
2093 2094
	spin_lock_bh(&bat_priv->tt.req_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2095
		if (!batadv_compare_eth(node->addr, resp_src))
2096 2097 2098 2099
			continue;
		list_del(&node->list);
		kfree(node);
	}
2100
	spin_unlock_bh(&bat_priv->tt.req_list_lock);
2101 2102

	/* Recalculate the CRC for this orig_node and store it */
2103
	orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2104 2105
out:
	if (orig_node)
2106
		batadv_orig_node_free_ref(orig_node);
2107 2108
}

2109
static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2110
{
2111
	struct batadv_tt_roam_node *node, *safe;
2112

2113
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2114

2115
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2116 2117 2118 2119
		list_del(&node->list);
		kfree(node);
	}

2120
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2121 2122
}

2123
static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2124
{
2125
	struct batadv_tt_roam_node *node, *safe;
2126

2127 2128
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
	list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2129 2130
		if (!batadv_has_timed_out(node->first_time,
					  BATADV_ROAMING_MAX_TIME))
2131 2132 2133 2134 2135
			continue;

		list_del(&node->list);
		kfree(node);
	}
2136
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2137 2138 2139 2140 2141 2142
}

/* 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.
 *
2143 2144
 * returns true if the ROAMING_ADV can be sent, false otherwise
 */
2145
static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2146
				       uint8_t *client)
2147
{
2148
	struct batadv_tt_roam_node *tt_roam_node;
2149 2150
	bool ret = false;

2151
	spin_lock_bh(&bat_priv->tt.roam_list_lock);
2152
	/* The new tt_req will be issued only if I'm not waiting for a
2153 2154
	 * reply from the same orig_node yet
	 */
2155
	list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2156
		if (!batadv_compare_eth(tt_roam_node->addr, client))
2157 2158
			continue;

2159
		if (batadv_has_timed_out(tt_roam_node->first_time,
2160
					 BATADV_ROAMING_MAX_TIME))
2161 2162
			continue;

2163
		if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
			/* 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;
2176 2177
		atomic_set(&tt_roam_node->counter,
			   BATADV_ROAMING_MAX_COUNT - 1);
2178 2179
		memcpy(tt_roam_node->addr, client, ETH_ALEN);

2180
		list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2181 2182 2183 2184
		ret = true;
	}

unlock:
2185
	spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2186 2187 2188
	return ret;
}

2189 2190
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node)
2191 2192
{
	struct sk_buff *skb = NULL;
2193
	struct batadv_roam_adv_packet *roam_adv_packet;
2194
	int ret = 1;
2195
	struct batadv_hard_iface *primary_if;
2196
	size_t len = sizeof(*roam_adv_packet);
2197 2198

	/* before going on we have to check whether the client has
2199 2200
	 * already roamed to us too many times
	 */
2201
	if (!batadv_tt_check_roam_count(bat_priv, client))
2202 2203
		goto out;

2204
	skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
2205 2206 2207
	if (!skb)
		goto out;

2208
	skb->priority = TC_PRIO_CONTROL;
2209
	skb_reserve(skb, ETH_HLEN);
2210

2211
	roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2212

2213
	roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2214
	roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2215
	roam_adv_packet->header.ttl = BATADV_TTL;
2216
	roam_adv_packet->reserved = 0;
2217
	primary_if = batadv_primary_if_get_selected(bat_priv);
2218 2219 2220
	if (!primary_if)
		goto out;
	memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2221
	batadv_hardif_free_ref(primary_if);
2222 2223 2224
	memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
	memcpy(roam_adv_packet->client, client, ETH_ALEN);

2225
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2226 2227
		   "Sending ROAMING_ADV to %pM (client %pM)\n",
		   orig_node->orig, client);
2228

2229
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2230

2231
	if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
2232
		ret = 0;
2233 2234

out:
2235
	if (ret && skb)
2236 2237
		kfree_skb(skb);
	return;
2238 2239
}

2240
static void batadv_tt_purge(struct work_struct *work)
2241
{
2242
	struct delayed_work *delayed_work;
2243
	struct batadv_priv_tt *priv_tt;
2244 2245 2246
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
2247 2248
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2249

2250
	batadv_tt_local_purge(bat_priv);
2251
	batadv_tt_global_purge(bat_priv);
2252 2253
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
2254

2255 2256
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2257
}
2258

2259
void batadv_tt_free(struct batadv_priv *bat_priv)
2260
{
2261 2262 2263
	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);

2264
	cancel_delayed_work_sync(&bat_priv->tt.work);
2265

2266 2267 2268 2269 2270
	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);
2271

2272
	kfree(bat_priv->tt.last_changeset);
2273
}
2274

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

	if (!hash)
2287
		goto out;
2288 2289 2290 2291 2292

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

		rcu_read_lock();
2293
		hlist_for_each_entry_rcu(tt_common_entry,
2294
					 head, hash_entry) {
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
			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++;
2305 2306 2307
		}
		rcu_read_unlock();
	}
2308 2309
out:
	return changed_num;
2310 2311
}

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

	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);
2331
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
2332 2333
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2334 2335
				continue;

2336
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2337
				   "Deleting local tt entry (%pM): pending\n",
2338
				   tt_common->addr);
2339

2340
			atomic_dec(&bat_priv->tt.local_entry_num);
2341
			hlist_del_rcu(&tt_common->hash_entry);
2342 2343 2344 2345
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
2346 2347 2348 2349 2350
		}
		spin_unlock_bh(list_lock);
	}
}

2351 2352 2353 2354 2355 2356
/**
 * 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)
2357
{
2358 2359
	uint16_t changed_num = 0;

2360 2361 2362 2363 2364
	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;
	}
2365

2366
	changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2367
					  BATADV_TT_CLIENT_NEW, false);
2368 2369

	/* all reset entries have to be counted as local entries */
2370
	atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2371
	batadv_tt_local_purge_pending_clients(bat_priv);
2372
	bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2373 2374

	/* Increment the TTVN only once per OGM interval */
2375
	atomic_inc(&bat_priv->tt.vn);
2376
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2377
		   "Local changes committed, updating to ttvn %u\n",
2378
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
2379 2380

	/* reset the sending counter */
2381
	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2382
	batadv_tt_tvlv_container_update(bat_priv);
2383
}
2384

2385
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2386
			   uint8_t *dst)
2387
{
2388 2389
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
2390
	bool ret = false;
2391 2392

	if (!atomic_read(&bat_priv->ap_isolation))
2393
		goto out;
2394

2395
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2396 2397 2398
	if (!tt_local_entry)
		goto out;

2399
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2400 2401 2402
	if (!tt_global_entry)
		goto out;

2403
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2404 2405
		goto out;

2406
	ret = true;
2407 2408 2409

out:
	if (tt_global_entry)
2410
		batadv_tt_global_entry_free_ref(tt_global_entry);
2411
	if (tt_local_entry)
2412
		batadv_tt_local_entry_free_ref(tt_local_entry);
2413 2414
	return ret;
}
2415

2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
/**
 * 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)
2431 2432 2433
{
	uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	bool full_table = true;
2434
	struct batadv_tvlv_tt_change *tt_change;
2435

2436
	/* don't care about a backbone gateways updates. */
2437
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2438 2439
		return;

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

2455
		tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
2456
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2457
					 ttvn, tt_change);
2458 2459 2460

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
2461 2462
		 * in the global table
		 */
2463
		orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2464 2465 2466 2467 2468 2469 2470 2471

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

/* 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
 */
2498
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2499
					uint8_t *addr)
2500
{
2501
	struct batadv_tt_global_entry *tt_global_entry;
2502 2503
	bool ret = false;

2504
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2505 2506 2507
	if (!tt_global_entry)
		goto out;

2508
	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2509
	batadv_tt_global_entry_free_ref(tt_global_entry);
2510 2511 2512
out:
	return ret;
}
2513

2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
/**
 * 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;
}

2539 2540 2541 2542 2543 2544
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;

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

2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
	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;
}
2564 2565 2566 2567 2568 2569 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

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

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 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
/**
 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
 *  container
 * @bat_priv: the bat priv with all the soft interface information
 * @src: mac address of tt tvlv sender
 * @dst: mac address of tt tvlv recipient
 * @tvlv_value: tvlv buffer containing the tt data
 * @tvlv_value_len: tvlv buffer length
 *
 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
 * otherwise.
 */
static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
					     uint8_t *src, uint8_t *dst,
					     void *tvlv_value,
					     uint16_t tvlv_value_len)
{
	struct batadv_tvlv_tt_data *tt_data;
	uint16_t num_entries;
	char tt_flag;
	bool ret;

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

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

	num_entries = tvlv_value_len / batadv_tt_len(1);

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

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

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

		if (batadv_is_my_mac(bat_priv, dst)) {
			batadv_handle_tt_response(bat_priv, tt_data,
						  src, num_entries);
			return NET_RX_SUCCESS;
		}

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

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

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

	return NET_RX_SUCCESS;
}

2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
/**
 * 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,
2689 2690
				     batadv_tt_tvlv_unicast_handler_v1,
				     BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
2691 2692 2693 2694 2695 2696 2697

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