translation-table.c 15.7 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
				 const char *message);
33

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

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

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

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

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

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

	if (!hash)
		return NULL;

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

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

78
		tt_local_entry_tmp = tt_local_entry;
79 80 81 82
		break;
	}
	rcu_read_unlock();

83
	return tt_local_entry_tmp;
84 85
}

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

	if (!hash)
		return NULL;

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

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

107
		tt_global_entry_tmp = tt_global_entry;
108 109 110 111
		break;
	}
	rcu_read_unlock();

112
	return tt_global_entry_tmp;
113 114
}

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

120
	bat_priv->tt_local_hash = hash_new(1024);
121

122
	if (!bat_priv->tt_local_hash)
123 124
		return 0;

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

	return 1;
}

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

138 139 140
	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);
141

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

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

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

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

167 168
	tt_local_entry = kmalloc(sizeof(struct tt_local_entry), GFP_ATOMIC);
	if (!tt_local_entry)
169 170
		return;

171 172
	memcpy(tt_local_entry->addr, addr, ETH_ALEN);
	tt_local_entry->last_seen = jiffies;
173 174

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

180
	spin_lock_bh(&bat_priv->tt_lhash_lock);
181

182 183 184 185
	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);
186

187
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
188 189

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

192
	tt_global_entry = tt_global_hash_find(bat_priv, addr);
193

194 195 196
	if (tt_global_entry)
		_tt_global_del_orig(bat_priv, tt_global_entry,
				     "local tt received");
197

198
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
199 200
}

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

210
	spin_lock_bh(&bat_priv->tt_lhash_lock);
211 212 213 214

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

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

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

			count++;
		}
226
		rcu_read_unlock();
227 228
	}

229 230 231
	/* 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);
232

233
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
234
	return count;
235 236
}

237
int tt_local_seq_print_text(struct seq_file *seq, void *offset)
238 239 240
{
	struct net_device *net_dev = (struct net_device *)seq->private;
	struct bat_priv *bat_priv = netdev_priv(net_dev);
241 242
	struct hashtable_t *hash = bat_priv->tt_local_hash;
	struct tt_local_entry *tt_local_entry;
243
	struct hard_iface *primary_if;
244
	struct hlist_node *node;
245 246 247
	struct hlist_head *head;
	size_t buf_size, pos;
	char *buff;
248 249 250 251 252 253 254 255 256
	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;
	}
257

258 259 260 261 262
	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;
263 264 265
	}

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

269
	spin_lock_bh(&bat_priv->tt_lhash_lock);
270 271 272 273 274 275

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

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

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

289 290 291 292 293 294
	buff[0] = '\0';
	pos = 0;

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

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

304
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
305 306 307

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

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

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

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

331 332 333
	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);
334 335
}

336
void tt_local_remove(struct bat_priv *bat_priv,
337
		     const uint8_t *addr, const char *message)
338
{
339
	struct tt_local_entry *tt_local_entry;
340

341
	spin_lock_bh(&bat_priv->tt_lhash_lock);
342

343
	tt_local_entry = tt_local_hash_find(bat_priv, addr);
344

345 346
	if (tt_local_entry)
		tt_local_del(bat_priv, tt_local_entry, message);
347

348
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
349 350
}

351
static void tt_local_purge(struct work_struct *work)
352 353 354 355
{
	struct delayed_work *delayed_work =
		container_of(work, struct delayed_work, work);
	struct bat_priv *bat_priv =
356 357 358
		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;
359
	struct hlist_node *node, *node_tmp;
360 361
	struct hlist_head *head;
	unsigned long timeout;
362
	int i;
363

364
	spin_lock_bh(&bat_priv->tt_lhash_lock);
365 366 367 368

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

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

374 375
			timeout = tt_local_entry->last_seen;
			timeout += TT_LOCAL_TIMEOUT * HZ;
376

377 378 379
			if (time_before(jiffies, timeout))
				continue;

380
			tt_local_del(bat_priv, tt_local_entry,
381
				      "address timed out");
382 383 384
		}
	}

385 386
	spin_unlock_bh(&bat_priv->tt_lhash_lock);
	tt_local_start_timer(bat_priv);
387 388
}

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

394 395 396
	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;
397 398
}

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

404
	bat_priv->tt_global_hash = hash_new(1024);
405

406
	if (!bat_priv->tt_global_hash)
407 408 409 410 411
		return 0;

	return 1;
}

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

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

424 425
		tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
		tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
426

427 428
		if (!tt_global_entry) {
			spin_unlock_bh(&bat_priv->tt_ghash_lock);
429

430 431
			tt_global_entry =
				kmalloc(sizeof(struct tt_global_entry),
432 433
					GFP_ATOMIC);

434
			if (!tt_global_entry)
435 436
				break;

437
			memcpy(tt_global_entry->addr, tt_ptr, ETH_ALEN);
438 439

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

444 445 446 447
			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);
448 449 450

		}

451 452
		tt_global_entry->orig_node = orig_node;
		spin_unlock_bh(&bat_priv->tt_ghash_lock);
453 454

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

457 458
		tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
		tt_local_entry = tt_local_hash_find(bat_priv, tt_ptr);
459

460 461 462
		if (tt_local_entry)
			tt_local_del(bat_priv, tt_local_entry,
				      "global tt received");
463

464
		spin_unlock_bh(&bat_priv->tt_lhash_lock);
465

466
		tt_buff_count++;
467 468 469
	}

	/* initialize, and overwrite if malloc succeeds */
470 471 472 473 474 475 476 477
	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;
478 479 480 481
		}
	}
}

482
int tt_global_seq_print_text(struct seq_file *seq, void *offset)
483 484 485
{
	struct net_device *net_dev = (struct net_device *)seq->private;
	struct bat_priv *bat_priv = netdev_priv(net_dev);
486 487
	struct hashtable_t *hash = bat_priv->tt_global_hash;
	struct tt_global_entry *tt_global_entry;
488
	struct hard_iface *primary_if;
489
	struct hlist_node *node;
490 491 492
	struct hlist_head *head;
	size_t buf_size, pos;
	char *buff;
493 494 495 496 497 498 499 500 501
	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;
	}
502

503 504 505 506 507
	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;
508 509
	}

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

514
	spin_lock_bh(&bat_priv->tt_ghash_lock);
515 516 517 518 519 520

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

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

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

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

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

550
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
551 552 553

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

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

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

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

581
	if (orig_node->tt_buff_len == 0)
582 583
		return;

584
	spin_lock_bh(&bat_priv->tt_ghash_lock);
585

586 587 588
	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);
589

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

595
		tt_buff_count++;
596 597
	}

598
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
599

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

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

609 610 611
	kfree(data);
}

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

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

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

627 628
	spin_lock_bh(&bat_priv->tt_ghash_lock);
	tt_global_entry = tt_global_hash_find(bat_priv, addr);
629

630
	if (!tt_global_entry)
631
		goto out;
632

633
	if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
634
		goto out;
635

636
	orig_node = tt_global_entry->orig_node;
637

638
out:
639
	spin_unlock_bh(&bat_priv->tt_ghash_lock);
640
	return orig_node;
641
}