translation-table.c 15.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Marek Lindner, Simon Wunderlich
 *
 * 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"
25
#include "hard-interface.h"
26 27 28
#include "hash.h"
#include "originator.h"

29 30 31
static void tt_local_purge(struct work_struct *work);
static void _tt_global_del_orig(struct bat_priv *bat_priv,
				 struct tt_global_entry *tt_global_entry,
32 33
				 char *message);

34
/* returns 1 if they are the same mac addr */
35
static int compare_ltt(struct hlist_node *node, void *data2)
36
{
37
	void *data1 = container_of(node, struct tt_local_entry, hash_entry);
38 39 40 41 42

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

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

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

50
static void tt_local_start_timer(struct bat_priv *bat_priv)
51
{
52 53
	INIT_DELAYED_WORK(&bat_priv->tt_work, tt_local_purge);
	queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work, 10 * HZ);
54 55
}

56
static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
57 58
						   void *data)
{
59
	struct hashtable_t *hash = bat_priv->tt_local_hash;
60 61
	struct hlist_head *head;
	struct hlist_node *node;
62
	struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
63 64 65 66 67 68 69 70 71
	int index;

	if (!hash)
		return NULL;

	index = choose_orig(data, hash->size);
	head = &hash->table[index];

	rcu_read_lock();
72 73
	hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
		if (!compare_eth(tt_local_entry, data))
74 75
			continue;

76
		tt_local_entry_tmp = tt_local_entry;
77 78 79 80
		break;
	}
	rcu_read_unlock();

81
	return tt_local_entry_tmp;
82 83
}

84
static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
85 86
						     void *data)
{
87
	struct hashtable_t *hash = bat_priv->tt_global_hash;
88 89
	struct hlist_head *head;
	struct hlist_node *node;
90 91
	struct tt_global_entry *tt_global_entry;
	struct tt_global_entry *tt_global_entry_tmp = NULL;
92 93 94 95 96 97 98 99 100
	int index;

	if (!hash)
		return NULL;

	index = choose_orig(data, hash->size);
	head = &hash->table[index];

	rcu_read_lock();
101 102
	hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
		if (!compare_eth(tt_global_entry, data))
103 104
			continue;

105
		tt_global_entry_tmp = tt_global_entry;
106 107 108 109
		break;
	}
	rcu_read_unlock();

110
	return tt_global_entry_tmp;
111 112
}

113
int tt_local_init(struct bat_priv *bat_priv)
114
{
115
	if (bat_priv->tt_local_hash)
116 117
		return 1;

118
	bat_priv->tt_local_hash = hash_new(1024);
119

120
	if (!bat_priv->tt_local_hash)
121 122
		return 0;

123 124
	atomic_set(&bat_priv->tt_local_changed, 0);
	tt_local_start_timer(bat_priv);
125 126 127 128

	return 1;
}

129
void tt_local_add(struct net_device *soft_iface, uint8_t *addr)
130 131
{
	struct bat_priv *bat_priv = netdev_priv(soft_iface);
132 133
	struct tt_local_entry *tt_local_entry;
	struct tt_global_entry *tt_global_entry;
134 135
	int required_bytes;

136 137 138
	spin_lock_bh(&bat_priv->tt_lhash_lock);
	tt_local_entry = tt_local_hash_find(bat_priv, addr);
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
139

140 141
	if (tt_local_entry) {
		tt_local_entry->last_seen = jiffies;
142 143 144 145
		return;
	}

	/* only announce as many hosts as possible in the batman-packet and
146
	   space in batman_packet->num_tt That also should give a limit to
147
	   MAC-flooding. */
148
	required_bytes = (bat_priv->num_local_tt + 1) * ETH_ALEN;
149 150 151 152 153
	required_bytes += BAT_PACKET_LEN;

	if ((required_bytes > ETH_DATA_LEN) ||
	    (atomic_read(&bat_priv->aggregated_ogms) &&
	     required_bytes > MAX_AGGREGATION_BYTES) ||
154
	    (bat_priv->num_local_tt + 1 > 255)) {
155
		bat_dbg(DBG_ROUTES, bat_priv,
156 157
			"Can't add new local tt entry (%pM): "
			"number of local tt entries exceeds packet size\n",
158 159 160 161 162
			addr);
		return;
	}

	bat_dbg(DBG_ROUTES, bat_priv,
163
		"Creating new local tt entry: %pM\n", addr);
164

165 166
	tt_local_entry = kmalloc(sizeof(struct tt_local_entry), GFP_ATOMIC);
	if (!tt_local_entry)
167 168
		return;

169 170
	memcpy(tt_local_entry->addr, addr, ETH_ALEN);
	tt_local_entry->last_seen = jiffies;
171 172

	/* the batman interface mac address should never be purged */
173
	if (compare_eth(addr, soft_iface->dev_addr))
174
		tt_local_entry->never_purge = 1;
175
	else
176
		tt_local_entry->never_purge = 0;
177

178
	spin_lock_bh(&bat_priv->tt_lhash_lock);
179

180 181 182 183
	hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
		 tt_local_entry, &tt_local_entry->hash_entry);
	bat_priv->num_local_tt++;
	atomic_set(&bat_priv->tt_local_changed, 1);
184

185
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
186 187

	/* remove address from global hash if present */
188
	spin_lock_bh(&bat_priv->tt_ghash_lock);
189

190
	tt_global_entry = tt_global_hash_find(bat_priv, addr);
191

192 193 194
	if (tt_global_entry)
		_tt_global_del_orig(bat_priv, tt_global_entry,
				     "local tt received");
195

196
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
197 198
}

199
int tt_local_fill_buffer(struct bat_priv *bat_priv,
200 201
			  unsigned char *buff, int buff_len)
{
202 203
	struct hashtable_t *hash = bat_priv->tt_local_hash;
	struct tt_local_entry *tt_local_entry;
204
	struct hlist_node *node;
205
	struct hlist_head *head;
206
	int i, count = 0;
207

208
	spin_lock_bh(&bat_priv->tt_lhash_lock);
209 210 211 212

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

213
		rcu_read_lock();
214
		hlist_for_each_entry_rcu(tt_local_entry, node,
215
					 head, hash_entry) {
216 217 218
			if (buff_len < (count + 1) * ETH_ALEN)
				break;

219
			memcpy(buff + (count * ETH_ALEN), tt_local_entry->addr,
220 221 222 223
			       ETH_ALEN);

			count++;
		}
224
		rcu_read_unlock();
225 226
	}

227 228 229
	/* if we did not get all new local tts see you next time  ;-) */
	if (count == bat_priv->num_local_tt)
		atomic_set(&bat_priv->tt_local_changed, 0);
230

231
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
232
	return count;
233 234
}

235
int tt_local_seq_print_text(struct seq_file *seq, void *offset)
236 237 238
{
	struct net_device *net_dev = (struct net_device *)seq->private;
	struct bat_priv *bat_priv = netdev_priv(net_dev);
239 240
	struct hashtable_t *hash = bat_priv->tt_local_hash;
	struct tt_local_entry *tt_local_entry;
241
	struct hard_iface *primary_if;
242
	struct hlist_node *node;
243 244 245
	struct hlist_head *head;
	size_t buf_size, pos;
	char *buff;
246 247 248 249 250 251 252 253 254
	int i, ret = 0;

	primary_if = primary_if_get_selected(bat_priv);
	if (!primary_if) {
		ret = seq_printf(seq, "BATMAN mesh %s disabled - "
				 "please specify interfaces to enable it\n",
				 net_dev->name);
		goto out;
	}
255

256 257 258 259 260
	if (primary_if->if_status != IF_ACTIVE) {
		ret = seq_printf(seq, "BATMAN mesh %s disabled - "
				 "primary interface not active\n",
				 net_dev->name);
		goto out;
261 262 263
	}

	seq_printf(seq, "Locally retrieved addresses (from %s) "
264
		   "announced via TT:\n",
265 266
		   net_dev->name);

267
	spin_lock_bh(&bat_priv->tt_lhash_lock);
268 269 270 271 272 273

	buf_size = 1;
	/* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

274 275
		rcu_read_lock();
		__hlist_for_each_rcu(node, head)
276
			buf_size += 21;
277
		rcu_read_unlock();
278 279 280 281
	}

	buff = kmalloc(buf_size, GFP_ATOMIC);
	if (!buff) {
282
		spin_unlock_bh(&bat_priv->tt_lhash_lock);
283 284
		ret = -ENOMEM;
		goto out;
285
	}
286

287 288 289 290 291 292
	buff[0] = '\0';
	pos = 0;

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

293
		rcu_read_lock();
294
		hlist_for_each_entry_rcu(tt_local_entry, node,
295
					 head, hash_entry) {
296
			pos += snprintf(buff + pos, 22, " * %pM\n",
297
					tt_local_entry->addr);
298
		}
299
		rcu_read_unlock();
300 301
	}

302
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
303 304 305

	seq_printf(seq, "%s", buff);
	kfree(buff);
306 307 308 309
out:
	if (primary_if)
		hardif_free_ref(primary_if);
	return ret;
310 311
}

312
static void _tt_local_del(struct hlist_node *node, void *arg)
313 314
{
	struct bat_priv *bat_priv = (struct bat_priv *)arg;
315
	void *data = container_of(node, struct tt_local_entry, hash_entry);
316 317

	kfree(data);
318 319
	bat_priv->num_local_tt--;
	atomic_set(&bat_priv->tt_local_changed, 1);
320 321
}

322 323
static void tt_local_del(struct bat_priv *bat_priv,
			  struct tt_local_entry *tt_local_entry,
324 325
			  char *message)
{
326 327
	bat_dbg(DBG_ROUTES, bat_priv, "Deleting local tt entry (%pM): %s\n",
		tt_local_entry->addr, message);
328

329 330 331
	hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
		    tt_local_entry->addr);
	_tt_local_del(&tt_local_entry->hash_entry, bat_priv);
332 333
}

334
void tt_local_remove(struct bat_priv *bat_priv,
335 336
		      uint8_t *addr, char *message)
{
337
	struct tt_local_entry *tt_local_entry;
338

339
	spin_lock_bh(&bat_priv->tt_lhash_lock);
340

341
	tt_local_entry = tt_local_hash_find(bat_priv, addr);
342

343 344
	if (tt_local_entry)
		tt_local_del(bat_priv, tt_local_entry, message);
345

346
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
347 348
}

349
static void tt_local_purge(struct work_struct *work)
350 351 352 353
{
	struct delayed_work *delayed_work =
		container_of(work, struct delayed_work, work);
	struct bat_priv *bat_priv =
354 355 356
		container_of(delayed_work, struct bat_priv, tt_work);
	struct hashtable_t *hash = bat_priv->tt_local_hash;
	struct tt_local_entry *tt_local_entry;
357
	struct hlist_node *node, *node_tmp;
358 359
	struct hlist_head *head;
	unsigned long timeout;
360
	int i;
361

362
	spin_lock_bh(&bat_priv->tt_lhash_lock);
363 364 365 366

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

367
		hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
368
					  head, hash_entry) {
369
			if (tt_local_entry->never_purge)
370
				continue;
371

372 373
			timeout = tt_local_entry->last_seen;
			timeout += TT_LOCAL_TIMEOUT * HZ;
374

375 376 377
			if (time_before(jiffies, timeout))
				continue;

378
			tt_local_del(bat_priv, tt_local_entry,
379
				      "address timed out");
380 381 382
		}
	}

383 384
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
	tt_local_start_timer(bat_priv);
385 386
}

387
void tt_local_free(struct bat_priv *bat_priv)
388
{
389
	if (!bat_priv->tt_local_hash)
390 391
		return;

392 393 394
	cancel_delayed_work_sync(&bat_priv->tt_work);
	hash_delete(bat_priv->tt_local_hash, _tt_local_del, bat_priv);
	bat_priv->tt_local_hash = NULL;
395 396
}

397
int tt_global_init(struct bat_priv *bat_priv)
398
{
399
	if (bat_priv->tt_global_hash)
400 401
		return 1;

402
	bat_priv->tt_global_hash = hash_new(1024);
403

404
	if (!bat_priv->tt_global_hash)
405 406 407 408 409
		return 0;

	return 1;
}

410
void tt_global_add_orig(struct bat_priv *bat_priv,
411
			 struct orig_node *orig_node,
412
			 unsigned char *tt_buff, int tt_buff_len)
413
{
414 415 416 417
	struct tt_global_entry *tt_global_entry;
	struct tt_local_entry *tt_local_entry;
	int tt_buff_count = 0;
	unsigned char *tt_ptr;
418

419 420
	while ((tt_buff_count + 1) * ETH_ALEN <= tt_buff_len) {
		spin_lock_bh(&bat_priv->tt_ghash_lock);
421

422 423
		tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
		tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
424

425 426
		if (!tt_global_entry) {
			spin_unlock_bh(&bat_priv->tt_ghash_lock);
427

428 429
			tt_global_entry =
				kmalloc(sizeof(struct tt_global_entry),
430 431
					GFP_ATOMIC);

432
			if (!tt_global_entry)
433 434
				break;

435
			memcpy(tt_global_entry->addr, tt_ptr, ETH_ALEN);
436 437

			bat_dbg(DBG_ROUTES, bat_priv,
438
				"Creating new global tt entry: "
439
				"%pM (via %pM)\n",
440
				tt_global_entry->addr, orig_node->orig);
441

442 443 444 445
			spin_lock_bh(&bat_priv->tt_ghash_lock);
			hash_add(bat_priv->tt_global_hash, compare_gtt,
				 choose_orig, tt_global_entry,
				 &tt_global_entry->hash_entry);
446 447 448

		}

449 450
		tt_global_entry->orig_node = orig_node;
		spin_unlock_bh(&bat_priv->tt_ghash_lock);
451 452

		/* remove address from local hash if present */
453
		spin_lock_bh(&bat_priv->tt_lhash_lock);
454

455 456
		tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
		tt_local_entry = tt_local_hash_find(bat_priv, tt_ptr);
457

458 459 460
		if (tt_local_entry)
			tt_local_del(bat_priv, tt_local_entry,
				      "global tt received");
461

462
		spin_unlock_bh(&bat_priv->tt_lhash_lock);
463

464
		tt_buff_count++;
465 466 467
	}

	/* initialize, and overwrite if malloc succeeds */
468 469 470 471 472 473 474 475
	orig_node->tt_buff = NULL;
	orig_node->tt_buff_len = 0;

	if (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;
476 477 478 479
		}
	}
}

480
int tt_global_seq_print_text(struct seq_file *seq, void *offset)
481 482 483
{
	struct net_device *net_dev = (struct net_device *)seq->private;
	struct bat_priv *bat_priv = netdev_priv(net_dev);
484 485
	struct hashtable_t *hash = bat_priv->tt_global_hash;
	struct tt_global_entry *tt_global_entry;
486
	struct hard_iface *primary_if;
487
	struct hlist_node *node;
488 489 490
	struct hlist_head *head;
	size_t buf_size, pos;
	char *buff;
491 492 493 494 495 496 497 498 499
	int i, ret = 0;

	primary_if = primary_if_get_selected(bat_priv);
	if (!primary_if) {
		ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
				 "specify interfaces to enable it\n",
				 net_dev->name);
		goto out;
	}
500

501 502 503 504 505
	if (primary_if->if_status != IF_ACTIVE) {
		ret = seq_printf(seq, "BATMAN mesh %s disabled - "
				 "primary interface not active\n",
				 net_dev->name);
		goto out;
506 507
	}

508 509
	seq_printf(seq,
		   "Globally announced TT entries received via the mesh %s\n",
510 511
		   net_dev->name);

512
	spin_lock_bh(&bat_priv->tt_ghash_lock);
513 514 515 516 517 518

	buf_size = 1;
	/* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

519 520
		rcu_read_lock();
		__hlist_for_each_rcu(node, head)
521
			buf_size += 43;
522
		rcu_read_unlock();
523 524 525 526
	}

	buff = kmalloc(buf_size, GFP_ATOMIC);
	if (!buff) {
527
		spin_unlock_bh(&bat_priv->tt_ghash_lock);
528 529
		ret = -ENOMEM;
		goto out;
530 531 532 533 534 535 536
	}
	buff[0] = '\0';
	pos = 0;

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

537
		rcu_read_lock();
538
		hlist_for_each_entry_rcu(tt_global_entry, node,
539
					 head, hash_entry) {
540 541
			pos += snprintf(buff + pos, 44,
					" * %pM via %pM\n",
542 543
					tt_global_entry->addr,
					tt_global_entry->orig_node->orig);
544
		}
545
		rcu_read_unlock();
546 547
	}

548
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
549 550 551

	seq_printf(seq, "%s", buff);
	kfree(buff);
552 553 554 555
out:
	if (primary_if)
		hardif_free_ref(primary_if);
	return ret;
556 557
}

558 559
static void _tt_global_del_orig(struct bat_priv *bat_priv,
				 struct tt_global_entry *tt_global_entry,
560 561 562
				 char *message)
{
	bat_dbg(DBG_ROUTES, bat_priv,
563 564
		"Deleting global tt entry %pM (via %pM): %s\n",
		tt_global_entry->addr, tt_global_entry->orig_node->orig,
565 566
		message);

567 568 569
	hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
		    tt_global_entry->addr);
	kfree(tt_global_entry);
570 571
}

572
void tt_global_del_orig(struct bat_priv *bat_priv,
573 574
			 struct orig_node *orig_node, char *message)
{
575 576 577
	struct tt_global_entry *tt_global_entry;
	int tt_buff_count = 0;
	unsigned char *tt_ptr;
578

579
	if (orig_node->tt_buff_len == 0)
580 581
		return;

582
	spin_lock_bh(&bat_priv->tt_ghash_lock);
583

584 585 586
	while ((tt_buff_count + 1) * ETH_ALEN <= orig_node->tt_buff_len) {
		tt_ptr = orig_node->tt_buff + (tt_buff_count * ETH_ALEN);
		tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
587

588 589 590
		if ((tt_global_entry) &&
		    (tt_global_entry->orig_node == orig_node))
			_tt_global_del_orig(bat_priv, tt_global_entry,
591 592
					     message);

593
		tt_buff_count++;
594 595
	}

596
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
597

598 599 600
	orig_node->tt_buff_len = 0;
	kfree(orig_node->tt_buff);
	orig_node->tt_buff = NULL;
601 602
}

603
static void tt_global_del(struct hlist_node *node, void *arg)
604
{
605
	void *data = container_of(node, struct tt_global_entry, hash_entry);
606

607 608 609
	kfree(data);
}

610
void tt_global_free(struct bat_priv *bat_priv)
611
{
612
	if (!bat_priv->tt_global_hash)
613 614
		return;

615 616
	hash_delete(bat_priv->tt_global_hash, tt_global_del, NULL);
	bat_priv->tt_global_hash = NULL;
617 618 619 620
}

struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
{
621
	struct tt_global_entry *tt_global_entry;
622
	struct orig_node *orig_node = NULL;
623

624 625
	spin_lock_bh(&bat_priv->tt_ghash_lock);
	tt_global_entry = tt_global_hash_find(bat_priv, addr);
626

627
	if (!tt_global_entry)
628
		goto out;
629

630
	if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
631
		goto out;
632

633
	orig_node = tt_global_entry->orig_node;
634

635
out:
636
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
637
	return orig_node;
638
}