translation-table.c 80.2 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
#include <linux/crc32c.h>
31

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
	tt_data->flags = BATADV_TT_OGM_DIFF;
	tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
412
	tt_data->crc = htonl(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
		   "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
485 486
		   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
		seq_printf(seq,
996
			   " %c %pM  (%3u) via %pM     (%3u)   (%#.8x) [%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
	seq_printf(seq, "       %-13s %s       %-15s %s (%-10s) %s\n",
1041 1042
		   "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 1398 1399 1400 1401 1402
/**
 * batadv_tt_global_crc - calculates the checksum of the local table belonging
 *  to the given orig_node
 * @bat_priv: the bat priv with all the soft interface information
 */
static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1403
				     struct batadv_orig_node *orig_node)
1404
{
1405
	struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1406 1407
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_global_entry *tt_global;
1408
	struct hlist_head *head;
1409
	uint32_t i, crc = 0;
1410 1411 1412 1413 1414

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

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

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

1440
			crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
1441 1442 1443 1444
		}
		rcu_read_unlock();
	}

1445
	return crc;
1446 1447
}

1448 1449 1450 1451 1452
/**
 * batadv_tt_local_crc - calculates the checksum of the local table
 * @bat_priv: the bat priv with all the soft interface information
 */
static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1453
{
1454
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1455
	struct batadv_tt_common_entry *tt_common;
1456
	struct hlist_head *head;
1457
	uint32_t i, crc = 0;
1458 1459 1460 1461 1462

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

		rcu_read_lock();
1463
		hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1464
			/* not yet committed clients have not to be taken into
1465 1466
			 * account while computing the CRC
			 */
1467
			if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1468
				continue;
1469 1470

			crc ^= crc32c(0, tt_common->addr, ETH_ALEN);
1471 1472 1473 1474
		}
		rcu_read_unlock();
	}

1475
	return crc;
1476 1477
}

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

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

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

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

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

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

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

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

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

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

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

1560 1561 1562 1563 1564 1565 1566 1567
/**
 * 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)
1568
{
1569
	const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1570

1571
	if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1572 1573 1574 1575
		return 0;
	return 1;
}

1576 1577
static int batadv_tt_global_valid(const void *entry_ptr,
				  const void *data_ptr)
1578
{
1579 1580 1581
	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;
1582

1583 1584
	if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
	    tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1585 1586
		return 0;

1587 1588
	tt_global_entry = container_of(tt_common_entry,
				       struct batadv_tt_global_entry,
1589 1590
				       common);

1591
	return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1592 1593
}

1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
/**
 * 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)
1611
{
1612
	struct batadv_tt_common_entry *tt_common_entry;
1613 1614
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
	struct batadv_tvlv_tt_change *tt_change;
1615
	struct hlist_head *head;
1616 1617
	uint16_t tt_tot, tt_num_entries = 0;
	ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
1618
	uint32_t i;
1619

1620 1621 1622
	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);
1623 1624
	}

1625
	tt_tot = tt_len / sizeof(struct batadv_tvlv_tt_change);
1626

1627 1628 1629 1630
	tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
			       GFP_ATOMIC);
	if (!tvlv_tt_data)
		goto out;
1631

1632
	tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
1633 1634 1635 1636 1637

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

1638
		hlist_for_each_entry_rcu(tt_common_entry,
1639
					 head, hash_entry) {
1640
			if (tt_tot == tt_num_entries)
1641 1642
				break;

1643
			if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1644 1645
				continue;

1646 1647
			memcpy(tt_change->addr, tt_common_entry->addr,
			       ETH_ALEN);
1648
			tt_change->flags = tt_common_entry->flags;
1649
			tt_change->reserved = 0;
1650

1651
			tt_num_entries++;
1652 1653 1654 1655 1656 1657
			tt_change++;
		}
	}
	rcu_read_unlock();

out:
1658
	return tvlv_tt_data;
1659 1660
}

1661 1662 1663 1664 1665 1666 1667 1668 1669
/**
 * batadv_send_tt_request - send a TT Request message to a given node
 * @bat_priv: the bat priv with all the soft interface information
 * @dst_orig_node: the destination of the message
 * @ttvn: the version number that the source of the message is looking for
 * @tt_crc: the CRC associated with the version number
 * @full_table: ask for the entire translation table if true, while only for the
 *  last TT diff otherwise
 */
1670 1671
static int batadv_send_tt_request(struct batadv_priv *bat_priv,
				  struct batadv_orig_node *dst_orig_node,
1672
				  uint8_t ttvn, uint32_t tt_crc,
1673
				  bool full_table)
1674
{
1675
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1676 1677
	struct batadv_hard_iface *primary_if;
	struct batadv_tt_req_node *tt_req_node = NULL;
1678
	bool ret = false;
1679

1680
	primary_if = batadv_primary_if_get_selected(bat_priv);
1681 1682 1683 1684
	if (!primary_if)
		goto out;

	/* The new tt_req will be issued only if I'm not waiting for a
1685 1686
	 * reply from the same orig_node yet
	 */
1687
	tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1688 1689 1690
	if (!tt_req_node)
		goto out;

1691 1692
	tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
	if (!tvlv_tt_data)
1693 1694
		goto out;

1695 1696
	tvlv_tt_data->flags = BATADV_TT_REQUEST;
	tvlv_tt_data->ttvn = ttvn;
1697
	tvlv_tt_data->crc = htonl(tt_crc);
1698 1699

	if (full_table)
1700
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1701

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

1705
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1706 1707 1708 1709
	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;
1710 1711 1712

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

1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
/**
 * 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)
1737
{
1738
	struct batadv_orig_node *req_dst_orig_node;
1739
	struct batadv_orig_node *res_dst_orig_node = NULL;
1740 1741 1742 1743
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
	uint8_t orig_ttvn, req_ttvn;
	uint16_t tt_len;
	bool ret = false, full_table;
1744

1745
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1746
		   "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1747 1748
		   req_src, tt_data->ttvn, req_dst,
		   (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1749 1750

	/* Let's get the orig node of the REAL destination */
1751
	req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
1752 1753 1754
	if (!req_dst_orig_node)
		goto out;

1755
	res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
1756 1757 1758 1759
	if (!res_dst_orig_node)
		goto out;

	orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1760
	req_ttvn = tt_data->ttvn;
1761

1762
	/* this node doesn't have the requested data */
1763
	if (orig_ttvn != req_ttvn ||
1764
	    tt_data->crc != htonl(req_dst_orig_node->tt_crc))
1765 1766
		goto out;

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

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

1781 1782 1783
		tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
				       GFP_ATOMIC);
		if (!tvlv_tt_data)
1784 1785 1786
			goto unlock;

		/* Copy the last orig_node's OGM buffer */
1787
		memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
1788 1789 1790
		       req_dst_orig_node->tt_buff_len);
		spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
	} else {
1791
		tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1792 1793 1794 1795 1796 1797 1798 1799
		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)
1800 1801 1802
			goto out;
	}

1803 1804
	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
	tvlv_tt_data->ttvn = req_ttvn;
1805 1806

	if (full_table)
1807
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1808

1809
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1810 1811 1812
		   "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);
1813

1814
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1815

1816 1817 1818
	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);
1819

1820
	ret = true;
1821 1822 1823 1824 1825 1826 1827
	goto out;

unlock:
	spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);

out:
	if (res_dst_orig_node)
1828
		batadv_orig_node_free_ref(res_dst_orig_node);
1829
	if (req_dst_orig_node)
1830
		batadv_orig_node_free_ref(req_dst_orig_node);
1831
	kfree(tvlv_tt_data);
1832 1833
	return ret;
}
1834

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
/**
 * 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)
1847
{
1848
	struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1849
	struct batadv_orig_node *orig_node;
1850
	struct batadv_hard_iface *primary_if = NULL;
1851
	uint8_t my_ttvn, req_ttvn;
1852
	bool full_table;
1853
	uint16_t tt_len;
1854

1855
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1856
		   "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1857 1858
		   req_src, tt_data->ttvn,
		   (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1859 1860


1861
	my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1862
	req_ttvn = tt_data->ttvn;
1863

1864
	orig_node = batadv_orig_hash_find(bat_priv, req_src);
1865 1866 1867
	if (!orig_node)
		goto out;

1868
	primary_if = batadv_primary_if_get_selected(bat_priv);
1869 1870 1871 1872
	if (!primary_if)
		goto out;

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

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

1888 1889 1890
		tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
				       GFP_ATOMIC);
		if (!tvlv_tt_data)
1891 1892
			goto unlock;

1893 1894
		/* Copy the last orig_node's OGM buffer */
		memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
1895 1896
		       bat_priv->tt.last_changeset_len);
		spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1897
	} else {
1898
		tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1899 1900 1901 1902 1903 1904 1905 1906 1907
		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)
1908 1909 1910
			goto out;
	}

1911 1912
	tvlv_tt_data->flags = BATADV_TT_RESPONSE;
	tvlv_tt_data->ttvn = req_ttvn;
1913 1914

	if (full_table)
1915
		tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1916

1917
	batadv_dbg(BATADV_DBG_TT, bat_priv,
1918 1919
		   "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
		   orig_node->orig, full_table ? 'F' : '.', req_ttvn);
1920

1921
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1922

1923 1924 1925 1926
	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);

1927 1928 1929
	goto out;

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

1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
/**
 * 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)
1953
{
1954
	if (batadv_is_my_mac(bat_priv, req_dst)) {
1955
		/* don't answer backbone gws! */
1956
		if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
1957 1958
			return true;

1959
		return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
1960
	} else {
1961 1962
		return batadv_send_other_tt_response(bat_priv, tt_data,
						     req_src, req_dst);
1963
	}
1964 1965
}

1966 1967
static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
				      struct batadv_orig_node *orig_node,
1968
				      struct batadv_tvlv_tt_change *tt_change,
1969
				      uint16_t tt_num_changes, uint8_t ttvn)
1970 1971
{
	int i;
1972
	int roams;
1973 1974

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

1997
static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1998 1999
				  struct batadv_tvlv_tt_data *tt_data,
				  uint8_t *resp_src, uint16_t num_entries)
2000
{
2001
	struct batadv_orig_node *orig_node;
2002

2003
	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2004 2005 2006 2007
	if (!orig_node)
		goto out;

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

2010
	_batadv_tt_update_changes(bat_priv, orig_node,
2011 2012
				  (struct batadv_tvlv_tt_change *)(tt_data + 1),
				  num_entries, tt_data->ttvn);
2013 2014 2015 2016 2017 2018 2019

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

2020
	atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
2021 2022 2023

out:
	if (orig_node)
2024
		batadv_orig_node_free_ref(orig_node);
2025 2026
}

2027 2028
static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
				     struct batadv_orig_node *orig_node,
2029
				     uint16_t tt_num_changes, uint8_t ttvn,
2030
				     struct batadv_tvlv_tt_change *tt_change)
2031
{
2032 2033
	_batadv_tt_update_changes(bat_priv, orig_node, tt_change,
				  tt_num_changes, ttvn);
2034

2035 2036
	batadv_tt_save_orig_buffer(bat_priv, orig_node,
				   (unsigned char *)tt_change, tt_num_changes);
2037 2038 2039
	atomic_set(&orig_node->last_ttvn, ttvn);
}

2040
bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2041
{
2042
	struct batadv_tt_local_entry *tt_local_entry;
2043
	bool ret = false;
2044

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

2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
/**
 * 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)
2071
{
2072 2073
	struct batadv_tt_req_node *node, *safe;
	struct batadv_orig_node *orig_node = NULL;
2074
	struct batadv_tvlv_tt_change *tt_change;
2075

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

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

2085
	orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2086 2087 2088
	if (!orig_node)
		goto out;

2089 2090
	if (tt_data->flags & BATADV_TT_FULL_TABLE) {
		batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
2091
	} else {
2092 2093 2094
		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);
2095
	}
2096 2097

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2194 2195
static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
				 struct batadv_orig_node *orig_node)
2196
{
2197
	struct batadv_hard_iface *primary_if;
2198 2199 2200 2201 2202
	struct batadv_tvlv_roam_adv tvlv_roam;

	primary_if = batadv_primary_if_get_selected(bat_priv);
	if (!primary_if)
		goto out;
2203 2204

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

2210
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2211 2212
		   "Sending ROAMING_ADV to %pM (client %pM)\n",
		   orig_node->orig, client);
2213

2214
	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2215

2216 2217 2218 2219 2220 2221
	memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
	tvlv_roam.reserved = 0;

	batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
				 orig_node->orig, BATADV_TVLV_ROAM, 1,
				 &tvlv_roam, sizeof(tvlv_roam));
2222 2223

out:
2224 2225
	if (primary_if)
		batadv_hardif_free_ref(primary_if);
2226 2227
}

2228
static void batadv_tt_purge(struct work_struct *work)
2229
{
2230
	struct delayed_work *delayed_work;
2231
	struct batadv_priv_tt *priv_tt;
2232 2233 2234
	struct batadv_priv *bat_priv;

	delayed_work = container_of(work, struct delayed_work, work);
2235 2236
	priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
	bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2237

2238
	batadv_tt_local_purge(bat_priv);
2239
	batadv_tt_global_purge(bat_priv);
2240 2241
	batadv_tt_req_purge(bat_priv);
	batadv_tt_roam_purge(bat_priv);
2242

2243 2244
	queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
			   msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2245
}
2246

2247
void batadv_tt_free(struct batadv_priv *bat_priv)
2248
{
2249 2250 2251
	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);

2252
	cancel_delayed_work_sync(&bat_priv->tt.work);
2253

2254 2255 2256 2257 2258
	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);
2259

2260
	kfree(bat_priv->tt.last_changeset);
2261
}
2262

2263
/* This function will enable or disable the specified flags for all the entries
2264 2265
 * in the given hash table and returns the number of modified entries
 */
2266 2267
static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
				    uint16_t flags, bool enable)
2268
{
2269
	uint32_t i;
2270
	uint16_t changed_num = 0;
2271
	struct hlist_head *head;
2272
	struct batadv_tt_common_entry *tt_common_entry;
2273 2274

	if (!hash)
2275
		goto out;
2276 2277 2278 2279 2280

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

		rcu_read_lock();
2281
		hlist_for_each_entry_rcu(tt_common_entry,
2282
					 head, hash_entry) {
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
			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++;
2293 2294 2295
		}
		rcu_read_unlock();
	}
2296 2297
out:
	return changed_num;
2298 2299
}

2300
/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2301
static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2302
{
2303
	struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2304 2305
	struct batadv_tt_common_entry *tt_common;
	struct batadv_tt_local_entry *tt_local;
2306
	struct hlist_node *node_tmp;
2307 2308
	struct hlist_head *head;
	spinlock_t *list_lock; /* protects write access to the hash lists */
2309
	uint32_t i;
2310 2311 2312 2313 2314 2315 2316 2317 2318

	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);
2319
		hlist_for_each_entry_safe(tt_common, node_tmp, head,
2320 2321
					  hash_entry) {
			if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2322 2323
				continue;

2324
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2325
				   "Deleting local tt entry (%pM): pending\n",
2326
				   tt_common->addr);
2327

2328
			atomic_dec(&bat_priv->tt.local_entry_num);
2329
			hlist_del_rcu(&tt_common->hash_entry);
2330 2331 2332 2333
			tt_local = container_of(tt_common,
						struct batadv_tt_local_entry,
						common);
			batadv_tt_local_entry_free_ref(tt_local);
2334 2335 2336 2337 2338
		}
		spin_unlock_bh(list_lock);
	}
}

2339 2340 2341 2342 2343 2344
/**
 * 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)
2345
{
2346 2347
	uint16_t changed_num = 0;

2348 2349 2350 2351 2352
	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;
	}
2353

2354
	changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2355
					  BATADV_TT_CLIENT_NEW, false);
2356 2357

	/* all reset entries have to be counted as local entries */
2358
	atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2359
	batadv_tt_local_purge_pending_clients(bat_priv);
2360
	bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2361 2362

	/* Increment the TTVN only once per OGM interval */
2363
	atomic_inc(&bat_priv->tt.vn);
2364
	batadv_dbg(BATADV_DBG_TT, bat_priv,
2365
		   "Local changes committed, updating to ttvn %u\n",
2366
		   (uint8_t)atomic_read(&bat_priv->tt.vn));
2367 2368

	/* reset the sending counter */
2369
	atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2370
	batadv_tt_tvlv_container_update(bat_priv);
2371
}
2372

2373
bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2374
			   uint8_t *dst)
2375
{
2376 2377
	struct batadv_tt_local_entry *tt_local_entry = NULL;
	struct batadv_tt_global_entry *tt_global_entry = NULL;
2378
	bool ret = false;
2379 2380

	if (!atomic_read(&bat_priv->ap_isolation))
2381
		goto out;
2382

2383
	tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2384 2385 2386
	if (!tt_local_entry)
		goto out;

2387
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2388 2389 2390
	if (!tt_global_entry)
		goto out;

2391
	if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2392 2393
		goto out;

2394
	ret = true;
2395 2396 2397

out:
	if (tt_global_entry)
2398
		batadv_tt_global_entry_free_ref(tt_global_entry);
2399
	if (tt_local_entry)
2400
		batadv_tt_local_entry_free_ref(tt_local_entry);
2401 2402
	return ret;
}
2403

2404 2405 2406 2407 2408 2409 2410 2411
/**
 * 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
2412
 * @tt_crc: crc32 checksum of orig node's translation table
2413 2414 2415 2416 2417
 */
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,
2418
				  uint32_t tt_crc)
2419 2420 2421
{
	uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
	bool full_table = true;
2422
	struct batadv_tvlv_tt_change *tt_change;
2423

2424
	/* don't care about a backbone gateways updates. */
2425
	if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2426 2427
		return;

2428
	/* orig table not initialised AND first diff is in the OGM OR the ttvn
2429 2430
	 * increased by one -> we can apply the attached changes
	 */
2431 2432
	if ((!orig_node->tt_initialised && ttvn == 1) ||
	    ttvn - orig_ttvn == 1) {
2433
		/* the OGM could not contain the changes due to their size or
2434 2435
		 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
		 * times.
2436 2437
		 * In this case send a tt request
		 */
2438 2439 2440 2441 2442
		if (!tt_num_changes) {
			full_table = false;
			goto request_table;
		}

2443
		tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
2444
		batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2445
					 ttvn, tt_change);
2446 2447 2448

		/* Even if we received the precomputed crc with the OGM, we
		 * prefer to recompute it to spot any possible inconsistency
2449 2450
		 * in the global table
		 */
2451
		orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2452 2453 2454 2455 2456 2457 2458 2459

		/* 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
2460 2461
		 * inconsistency
		 */
2462 2463 2464 2465
		if (orig_node->tt_crc != tt_crc)
			goto request_table;
	} else {
		/* if we missed more than one change or our tables are not
2466 2467
		 * in sync anymore -> request fresh tt data
		 */
2468 2469
		if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
		    orig_node->tt_crc != tt_crc) {
2470
request_table:
2471
			batadv_dbg(BATADV_DBG_TT, bat_priv,
2472
				   "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.8x last_crc: %#.8x num_changes: %u)\n",
2473 2474
				   orig_node->orig, ttvn, orig_ttvn, tt_crc,
				   orig_node->tt_crc, tt_num_changes);
2475 2476
			batadv_send_tt_request(bat_priv, orig_node, ttvn,
					       tt_crc, full_table);
2477 2478 2479 2480
			return;
		}
	}
}
2481 2482 2483 2484 2485

/* 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
 */
2486
bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2487
					uint8_t *addr)
2488
{
2489
	struct batadv_tt_global_entry *tt_global_entry;
2490 2491
	bool ret = false;

2492
	tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2493 2494 2495
	if (!tt_global_entry)
		goto out;

2496
	ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2497
	batadv_tt_global_entry_free_ref(tt_global_entry);
2498 2499 2500
out:
	return ret;
}
2501

2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
/**
 * 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;
}

2527 2528 2529 2530 2531 2532
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;

2533 2534 2535 2536 2537 2538 2539
	/* 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;

2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
	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;
}
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579

/**
 * 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),
2580
			      num_entries, tt_data->ttvn, ntohl(tt_data->crc));
2581 2582
}

2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 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
/**
 * 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;
}

2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
/**
 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam 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 roam tvlv is to be re-routed, NET_RX_SUCCESS
 * otherwise.
 */
static int batadv_roam_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_roam_adv *roaming_adv;
	struct batadv_orig_node *orig_node = NULL;

	/* If this node is not the intended recipient of the
	 * roaming advertisement the packet is forwarded
	 * (the tvlv API will re-route the packet).
	 */
	if (!batadv_is_my_mac(bat_priv, dst))
		return NET_RX_DROP;

	/* check if it is a backbone gateway. we don't accept
	 * roaming advertisement from it, as it has the same
	 * entries as we have.
	 */
	if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
		goto out;

	if (tvlv_value_len < sizeof(*roaming_adv))
		goto out;

	orig_node = batadv_orig_hash_find(bat_priv, src);
	if (!orig_node)
		goto out;

	batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
	roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;

	batadv_dbg(BATADV_DBG_TT, bat_priv,
		   "Received ROAMING_ADV from %pM (client %pM)\n",
		   src, roaming_adv->client);

	batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
			     BATADV_TT_CLIENT_ROAM,
			     atomic_read(&orig_node->last_ttvn) + 1);

out:
	if (orig_node)
		batadv_orig_node_free_ref(orig_node);
	return NET_RX_SUCCESS;
}

2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733
/**
 * 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,
2734 2735
				     batadv_tt_tvlv_unicast_handler_v1,
				     BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
2736

2737 2738 2739 2740
	batadv_tvlv_handler_register(bat_priv, NULL,
				     batadv_roam_tvlv_unicast_handler_v1,
				     BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);

2741 2742 2743 2744 2745 2746
	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;
}