rose_route.c 30.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2 3 4 5
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Linus Torvalds's avatar
Linus Torvalds committed
6
 *
7 8
 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 */
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <net/ax25.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <net/arp.h>
#include <linux/if_arp.h>
#include <linux/skbuff.h>
#include <net/sock.h>
27
#include <net/tcp.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/fcntl.h>
#include <linux/termios.h>	/* For TIOCINQ/OUTQ */
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <linux/netfilter.h>
#include <linux/init.h>
#include <net/rose.h>

static unsigned int rose_neigh_no = 1;

static struct rose_node  *rose_node_list;
42
static spinlock_t rose_node_list_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
43
static struct rose_neigh *rose_neigh_list;
44
static spinlock_t rose_neigh_list_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
45
static struct rose_route *rose_route_list;
46
static spinlock_t rose_route_list_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49 50 51 52 53 54 55

struct rose_neigh *rose_loopback_neigh;

static void rose_remove_neigh(struct rose_neigh *);

/*
 *	Add a new route to a node, and in the process add the node and the
 *	neighbour if it is new.
 */
56 57
static int rose_add_node(struct rose_route_struct *rose_route,
	struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61
{
	struct rose_node  *rose_node, *rose_tmpn, *rose_tmpp;
	struct rose_neigh *rose_neigh;
	unsigned long flags;
62 63 64 65
	int i, res = 0;

	spin_lock_irqsave(&rose_node_list_lock, flags);
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
66

67 68 69 70 71
	rose_node = rose_node_list;
	while (rose_node != NULL) {
		if ((rose_node->mask == rose_route->mask) &&
		    (rosecmpm(&rose_route->address, &rose_node->address,
		              rose_route->mask) == 0))
Linus Torvalds's avatar
Linus Torvalds committed
72
			break;
73 74
		rose_node = rose_node->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
75

76 77 78 79
	if (rose_node != NULL && rose_node->loopback) {
		res = -EINVAL;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
80

81 82 83 84
	rose_neigh = rose_neigh_list;
	while (rose_neigh != NULL) {
		if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
		    && rose_neigh->dev == dev)
Linus Torvalds's avatar
Linus Torvalds committed
85
			break;
86 87
		rose_neigh = rose_neigh->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
88 89

	if (rose_neigh == NULL) {
90 91 92 93 94
		rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
		if (rose_neigh == NULL) {
			res = -ENOMEM;
			goto out;
		}
Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

		rose_neigh->callsign  = rose_route->neighbour;
		rose_neigh->digipeat  = NULL;
		rose_neigh->ax25      = NULL;
		rose_neigh->dev       = dev;
		rose_neigh->count     = 0;
		rose_neigh->use       = 0;
		rose_neigh->dce_mode  = 0;
		rose_neigh->loopback  = 0;
		rose_neigh->number    = rose_neigh_no++;
		rose_neigh->restarted = 0;

		skb_queue_head_init(&rose_neigh->queue);

		init_timer(&rose_neigh->ftimer);
		init_timer(&rose_neigh->t0timer);

		if (rose_route->ndigis != 0) {
			if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
				kfree(rose_neigh);
115 116
				res = -ENOMEM;
				goto out;
Linus Torvalds's avatar
Linus Torvalds committed
117 118 119 120 121 122
			}

			rose_neigh->digipeat->ndigi      = rose_route->ndigis;
			rose_neigh->digipeat->lastrepeat = -1;

			for (i = 0; i < rose_route->ndigis; i++) {
123 124
				rose_neigh->digipeat->calls[i]    =
					rose_route->digipeaters[i];
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
				rose_neigh->digipeat->repeated[i] = 0;
			}
		}

		rose_neigh->next = rose_neigh_list;
		rose_neigh_list  = rose_neigh;
	}

	/*
	 * This is a new node to be inserted into the list. Find where it needs
	 * to be inserted into the list, and insert it. We want to be sure
	 * to order the list in descending order of mask size to ensure that
	 * later when we are searching this list the first match will be the
	 * best match.
	 */
	if (rose_node == NULL) {
		rose_tmpn = rose_node_list;
		rose_tmpp = NULL;

		while (rose_tmpn != NULL) {
			if (rose_tmpn->mask > rose_route->mask) {
				rose_tmpp = rose_tmpn;
				rose_tmpn = rose_tmpn->next;
			} else {
				break;
			}
		}

		/* create new node */
154 155 156 157 158
		rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
		if (rose_node == NULL) {
			res = -ENOMEM;
			goto out;
		}
Linus Torvalds's avatar
Linus Torvalds committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

		rose_node->address      = rose_route->address;
		rose_node->mask         = rose_route->mask;
		rose_node->count        = 1;
		rose_node->loopback     = 0;
		rose_node->neighbour[0] = rose_neigh;

		if (rose_tmpn == NULL) {
			if (rose_tmpp == NULL) {	/* Empty list */
				rose_node_list  = rose_node;
				rose_node->next = NULL;
			} else {
				rose_tmpp->next = rose_node;
				rose_node->next = NULL;
			}
		} else {
			if (rose_tmpp == NULL) {	/* 1st node */
				rose_node->next = rose_node_list;
				rose_node_list  = rose_node;
			} else {
				rose_tmpp->next = rose_node;
				rose_node->next = rose_tmpn;
			}
		}
		rose_neigh->count++;

185
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
186 187 188 189 190 191 192 193 194
	}

	/* We have space, slot it in */
	if (rose_node->count < 3) {
		rose_node->neighbour[rose_node->count] = rose_neigh;
		rose_node->count++;
		rose_neigh->count++;
	}

195 196 197 198 199
out:
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
	spin_unlock_irqrestore(&rose_node_list_lock, flags);

	return res;
Linus Torvalds's avatar
Linus Torvalds committed
200 201
}

202 203 204
/*
 * Caller is holding rose_node_list_lock.
 */
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207
static void rose_remove_node(struct rose_node *rose_node)
{
	unsigned long flags;
208
	struct rose_node *s;
Linus Torvalds's avatar
Linus Torvalds committed
209

210
	spin_lock_irqsave(&rose_node_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	if ((s = rose_node_list) == rose_node) {
		rose_node_list = rose_node->next;
		kfree(rose_node);
		return;
	}

	while (s != NULL && s->next != NULL) {
		if (s->next == rose_node) {
			s->next = rose_node->next;
			kfree(rose_node);
			return;
		}

		s = s->next;
	}
}

228 229 230
/*
 * Caller is holding rose_neigh_list_lock.
 */
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235 236 237 238
static void rose_remove_neigh(struct rose_neigh *rose_neigh)
{
	struct rose_neigh *s;
	unsigned long flags;

	rose_stop_ftimer(rose_neigh);
	rose_stop_t0timer(rose_neigh);

Linus Torvalds's avatar
Linus Torvalds committed
239
	skb_queue_purge(&rose_neigh->queue);
Linus Torvalds's avatar
Linus Torvalds committed
240

241
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244

	if ((s = rose_neigh_list) == rose_neigh) {
		rose_neigh_list = rose_neigh->next;
245
		spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249 250 251 252 253 254
		if (rose_neigh->digipeat != NULL)
			kfree(rose_neigh->digipeat);
		kfree(rose_neigh);
		return;
	}

	while (s != NULL && s->next != NULL) {
		if (s->next == rose_neigh) {
			s->next = rose_neigh->next;
255
			spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259 260 261 262 263
			if (rose_neigh->digipeat != NULL)
				kfree(rose_neigh->digipeat);
			kfree(rose_neigh);
			return;
		}

		s = s->next;
	}
264
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
265 266
}

267 268 269
/*
 * Caller is holding rose_route_list_lock.
 */
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static void rose_remove_route(struct rose_route *rose_route)
{
	struct rose_route *s;

	if (rose_route->neigh1 != NULL)
		rose_route->neigh1->use--;

	if (rose_route->neigh2 != NULL)
		rose_route->neigh2->use--;

	if ((s = rose_route_list) == rose_route) {
		rose_route_list = rose_route->next;
		kfree(rose_route);
		return;
	}

	while (s != NULL && s->next != NULL) {
		if (s->next == rose_route) {
			s->next = rose_route->next;
			kfree(rose_route);
			return;
		}

		s = s->next;
	}
}

/*
 *	"Delete" a node. Strictly speaking remove a route to a node. The node
 *	is only deleted if no routes are left to it.
 */
301 302
static int rose_del_node(struct rose_route_struct *rose_route,
	struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305
{
	struct rose_node  *rose_node;
	struct rose_neigh *rose_neigh;
306 307
	unsigned long flags;
	int i, err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
308

309 310
	spin_lock_irqsave(&rose_node_list_lock, flags);
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
311

312 313 314 315 316 317 318 319
	rose_node = rose_node_list;
	while (rose_node != NULL) {
		if ((rose_node->mask == rose_route->mask) &&
		    (rosecmpm(&rose_route->address, &rose_node->address,
		              rose_route->mask) == 0))
			break;
		rose_node = rose_node->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
320

321 322 323 324
	if (rose_node == NULL || rose_node->loopback) {
		err = -EINVAL;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
325

326 327 328 329
	rose_neigh = rose_neigh_list;
	while (rose_neigh != NULL) {
		if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0
		    && rose_neigh->dev == dev)
Linus Torvalds's avatar
Linus Torvalds committed
330
			break;
331 332
		rose_neigh = rose_neigh->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
333

334 335 336 337
	if (rose_neigh == NULL) {
		err = -EINVAL;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351

	for (i = 0; i < rose_node->count; i++) {
		if (rose_node->neighbour[i] == rose_neigh) {
			rose_neigh->count--;

			if (rose_neigh->count == 0 && rose_neigh->use == 0)
				rose_remove_neigh(rose_neigh);

			rose_node->count--;

			if (rose_node->count == 0) {
				rose_remove_node(rose_node);
			} else {
				switch (i) {
352 353 354 355 356 357 358 359
				case 0:
					rose_node->neighbour[0] =
						rose_node->neighbour[1];
				case 1:
					rose_node->neighbour[1] =
						rose_node->neighbour[2];
				case 2:
					break;
Linus Torvalds's avatar
Linus Torvalds committed
360 361
				}
			}
362
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
363 364
		}
	}
365
	err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
366

367 368 369 370 371
out:
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
	spin_unlock_irqrestore(&rose_node_list_lock, flags);

	return err;
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
}

/*
 *	Add the loopback neighbour.
 */
int rose_add_loopback_neigh(void)
{
	unsigned long flags;

	if ((rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_ATOMIC)) == NULL)
		return -ENOMEM;

	rose_loopback_neigh->callsign  = null_ax25_address;
	rose_loopback_neigh->digipeat  = NULL;
	rose_loopback_neigh->ax25      = NULL;
	rose_loopback_neigh->dev       = NULL;
	rose_loopback_neigh->count     = 0;
	rose_loopback_neigh->use       = 0;
	rose_loopback_neigh->dce_mode  = 1;
	rose_loopback_neigh->loopback  = 1;
	rose_loopback_neigh->number    = rose_neigh_no++;
	rose_loopback_neigh->restarted = 1;

	skb_queue_head_init(&rose_loopback_neigh->queue);

	init_timer(&rose_loopback_neigh->ftimer);
	init_timer(&rose_loopback_neigh->t0timer);

400
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
401 402
	rose_loopback_neigh->next = rose_neigh_list;
	rose_neigh_list           = rose_loopback_neigh;
403
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408 409 410 411 412 413 414

	return 0;
}

/*
 *	Add a loopback node.
 */
int rose_add_loopback_node(rose_address *address)
{
	struct rose_node *rose_node;
	unsigned long flags;
415 416 417
	unsigned int err = 0;

	spin_lock_irqsave(&rose_node_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
418

419 420 421 422 423
	rose_node = rose_node_list;
	while (rose_node != NULL) {
		if ((rose_node->mask == 10) &&
		     (rosecmpm(address, &rose_node->address, 10) == 0) &&
		     rose_node->loopback)
Linus Torvalds's avatar
Linus Torvalds committed
424
			break;
425 426
		rose_node = rose_node->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
427

428 429 430 431 432 433 434
	if (rose_node != NULL)
		goto out;

	if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
		err = -ENOMEM;
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437 438 439 440 441 442 443 444 445 446 447

	rose_node->address      = *address;
	rose_node->mask         = 10;
	rose_node->count        = 1;
	rose_node->loopback     = 1;
	rose_node->neighbour[0] = rose_loopback_neigh;

	/* Insert at the head of list. Address is always mask=10 */
	rose_node->next = rose_node_list;
	rose_node_list  = rose_node;

	rose_loopback_neigh->count++;

448 449 450
out:
	spin_unlock_irqrestore(&rose_node_list_lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
451 452 453 454 455 456 457 458 459
	return 0;
}

/*
 *	Delete a loopback node.
 */
void rose_del_loopback_node(rose_address *address)
{
	struct rose_node *rose_node;
460
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
461

462 463 464 465 466 467 468
	spin_lock_irqsave(&rose_node_list_lock, flags);

	rose_node = rose_node_list;
	while (rose_node != NULL) {
		if ((rose_node->mask == 10) &&
		    (rosecmpm(address, &rose_node->address, 10) == 0) &&
		    rose_node->loopback)
Linus Torvalds's avatar
Linus Torvalds committed
469
			break;
470 471
		rose_node = rose_node->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
472

473 474
	if (rose_node == NULL)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
475 476 477 478

	rose_remove_node(rose_node);

	rose_loopback_neigh->count--;
479 480 481

out:
	spin_unlock_irqrestore(&rose_node_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
482 483 484 485 486 487 488
}

/*
 *	A device has been removed. Remove its routes and neighbours.
 */
void rose_rt_device_down(struct net_device *dev)
{
489
	struct rose_neigh *s, *rose_neigh;
Linus Torvalds's avatar
Linus Torvalds committed
490
	struct rose_node  *t, *rose_node;
491
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
492 493
	int i;

494 495 496
	spin_lock_irqsave(&rose_node_list_lock, flags);
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
	rose_neigh = rose_neigh_list;
Linus Torvalds's avatar
Linus Torvalds committed
497 498 499 500
	while (rose_neigh != NULL) {
		s          = rose_neigh;
		rose_neigh = rose_neigh->next;

501 502 503 504
		if (s->dev != dev)
			continue;

		rose_node = rose_node_list;
Linus Torvalds's avatar
Linus Torvalds committed
505

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		while (rose_node != NULL) {
			t         = rose_node;
			rose_node = rose_node->next;

			for (i = 0; i < t->count; i++) {
				if (t->neighbour[i] != s)
					continue;

				t->count--;

				switch (i) {
				case 0:
					t->neighbour[0] = t->neighbour[1];
				case 1:
					t->neighbour[1] = t->neighbour[2];
				case 2:
					break;
				}
Linus Torvalds's avatar
Linus Torvalds committed
524 525
			}

526 527
			if (t->count <= 0)
				rose_remove_node(t);
Linus Torvalds's avatar
Linus Torvalds committed
528
		}
529 530

		rose_remove_neigh(s);
Linus Torvalds's avatar
Linus Torvalds committed
531
	}
532 533
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
	spin_unlock_irqrestore(&rose_node_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
534 535
}

536
#if 0 /* Currently unused */
Linus Torvalds's avatar
Linus Torvalds committed
537 538 539 540 541
/*
 *	A device has been removed. Remove its links.
 */
void rose_route_device_down(struct net_device *dev)
{
542 543
	struct rose_route *s, *rose_route;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
544

545 546
	spin_lock_irqsave(&rose_route_list_lock, flags);
	rose_route = rose_route_list;
Linus Torvalds's avatar
Linus Torvalds committed
547 548 549 550 551 552 553
	while (rose_route != NULL) {
		s          = rose_route;
		rose_route = rose_route->next;

		if (s->neigh1->dev == dev || s->neigh2->dev == dev)
			rose_remove_route(s);
	}
554
	spin_unlock_irqrestore(&rose_route_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
555
}
556
#endif
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559 560 561 562 563 564

/*
 *	Clear all nodes and neighbours out, except for neighbours with
 *	active connections going through them.
 *  Do not clear loopback neighbour and nodes.
 */
static int rose_clear_routes(void)
{
565 566 567 568 569 570 571 572 573
	struct rose_neigh *s, *rose_neigh;
	struct rose_node  *t, *rose_node;
	unsigned long flags;

	spin_lock_irqsave(&rose_node_list_lock, flags);
	spin_lock_irqsave(&rose_neigh_list_lock, flags);

	rose_neigh = rose_neigh_list;
	rose_node  = rose_node_list;
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

	while (rose_node != NULL) {
		t         = rose_node;
		rose_node = rose_node->next;
		if (!t->loopback)
			rose_remove_node(t);
	}

	while (rose_neigh != NULL) {
		s          = rose_neigh;
		rose_neigh = rose_neigh->next;

		if (s->use == 0 && !s->loopback) {
			s->count = 0;
			rose_remove_neigh(s);
		}
	}

592 593 594
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
	spin_unlock_irqrestore(&rose_node_list_lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	return 0;
}

/*
 *	Check that the device given is a valid AX.25 interface that is "up".
 */
struct net_device *rose_ax25_dev_get(char *devname)
{
	struct net_device *dev;

	if ((dev = dev_get_by_name(devname)) == NULL)
		return NULL;

	if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
		return dev;

	dev_put(dev);
	return NULL;
}

/*
 *	Find the first active ROSE device, usually "rose0".
 */
struct net_device *rose_dev_first(void)
{
	struct net_device *dev, *first = NULL;

	read_lock(&dev_base_lock);
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
			if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
				first = dev;
	}
	read_unlock(&dev_base_lock);

	return first;
}

/*
 *	Find the ROSE device for the given address.
 */
struct net_device *rose_dev_get(rose_address *addr)
{
	struct net_device *dev;

	read_lock(&dev_base_lock);
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
			dev_hold(dev);
			goto out;
		}
	}
out:
	read_unlock(&dev_base_lock);
	return dev;
}

static int rose_dev_exists(rose_address *addr)
{
	struct net_device *dev;

	read_lock(&dev_base_lock);
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
			goto out;
	}
out:
	read_unlock(&dev_base_lock);
	return dev != NULL;
}




struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
{
	struct rose_route *rose_route;

	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
		if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
		    (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
			return rose_route;

	return NULL;
}

/*
 *	Find a neighbour given a ROSE address.
 */
684 685
struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
	unsigned char *diagnostic)
Linus Torvalds's avatar
Linus Torvalds committed
686
{
687
	struct rose_neigh *res = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
688
	struct rose_node *node;
689
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
690 691 692
	int failed = 0;
	int i;

693
	spin_lock_irqsave(&rose_node_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
694 695 696 697
	for (node = rose_node_list; node != NULL; node = node->next) {
		if (rosecmpm(addr, &node->address, node->mask) == 0) {
			for (i = 0; i < node->count; i++) {
				if (!rose_ftimer_running(node->neighbour[i])) {
698 699 700
					res = node->neighbour[i];
					goto out;
				} else
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714
					failed = 1;
			}
			break;
		}
	}

	if (failed) {
		*cause      = ROSE_OUT_OF_ORDER;
		*diagnostic = 0;
	} else {
		*cause      = ROSE_NOT_OBTAINABLE;
		*diagnostic = 0;
	}

715 716 717 718
out:
	spin_unlock_irqrestore(&rose_node_list_lock, flags);

	return res;
Linus Torvalds's avatar
Linus Torvalds committed
719 720 721 722 723 724 725 726 727 728 729 730
}

/*
 *	Handle the ioctls that control the routing functions.
 */
int rose_rt_ioctl(unsigned int cmd, void *arg)
{
	struct rose_route_struct rose_route;
	struct net_device *dev;
	int err;

	switch (cmd) {
731 732 733 734 735 736
	case SIOCADDRT:
		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
			return -EFAULT;
		if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
			return -EINVAL;
		if (rose_dev_exists(&rose_route.address)) { /* Can't add routes to ourself */
Linus Torvalds's avatar
Linus Torvalds committed
737
			dev_put(dev);
738 739 740 741
			return -EINVAL;
		}
		if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
742

743 744 745
		err = rose_add_node(&rose_route, dev);
		dev_put(dev);
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
746

747 748 749 750
	case SIOCDELRT:
		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
			return -EFAULT;
		if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
751
			return -EINVAL;
752 753 754 755 756 757 758 759 760
		err = rose_del_node(&rose_route, dev);
		dev_put(dev);
		return err;

	case SIOCRSCLRRT:
		return rose_clear_routes();

	default:
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
761 762 763 764 765 766 767 768
	}

	return 0;
}

static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
{
	struct rose_route *rose_route, *s;
769
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
770 771 772 773 774 775

	rose_neigh->restarted = 0;

	rose_stop_t0timer(rose_neigh);
	rose_start_ftimer(rose_neigh);

Linus Torvalds's avatar
Linus Torvalds committed
776
	skb_queue_purge(&rose_neigh->queue);
Linus Torvalds's avatar
Linus Torvalds committed
777

778 779
	spin_lock_irqsave(&rose_route_list_lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
	rose_route = rose_route_list;

	while (rose_route != NULL) {
		if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
		    (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL)       ||
		    (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
			s = rose_route->next;
			rose_remove_route(rose_route);
			rose_route = s;
			continue;
		}

		if (rose_route->neigh1 == rose_neigh) {
			rose_route->neigh1->use--;
			rose_route->neigh1 = NULL;
			rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
		}

		if (rose_route->neigh2 == rose_neigh) {
			rose_route->neigh2->use--;
			rose_route->neigh2 = NULL;
			rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
		}

		rose_route = rose_route->next;
	}
806
	spin_unlock_irqrestore(&rose_route_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
807 808 809 810 811 812 813 814 815 816
}

/*
 * 	A level 2 link has timed out, therefore it appears to be a poor link,
 *	then don't use that neighbour until it is reset. Blow away all through
 *	routes and connections using this route.
 */
void rose_link_failed(ax25_cb *ax25, int reason)
{
	struct rose_neigh *rose_neigh;
817
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
818

819 820 821
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
	rose_neigh = rose_neigh_list;
	while (rose_neigh != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
822 823
		if (rose_neigh->ax25 == ax25)
			break;
824 825
		rose_neigh = rose_neigh->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
826

827 828
	if (rose_neigh != NULL) {
		rose_neigh->ax25 = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
829

830 831 832 833
		rose_del_route_by_neigh(rose_neigh);
		rose_kill_by_neigh(rose_neigh);
	}
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
}

/*
 * 	A device has been "downed" remove its link status. Blow away all
 *	through routes and connections that use this device.
 */
void rose_link_device_down(struct net_device *dev)
{
	struct rose_neigh *rose_neigh;

	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
		if (rose_neigh->dev == dev) {
			rose_del_route_by_neigh(rose_neigh);
			rose_kill_by_neigh(rose_neigh);
		}
	}
}

/*
 *	Route a frame to an appropriate AX.25 connection.
 */
int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
{
	struct rose_neigh *rose_neigh, *new_neigh;
	struct rose_route *rose_route;
	struct rose_facilities_struct facilities;
	rose_address *src_addr, *dest_addr;
	struct sock *sk;
	unsigned short frametype;
	unsigned int lci, new_lci;
	unsigned char cause, diagnostic;
	struct net_device *dev;
	unsigned long flags;
867
	int len, res = 0;
Linus Torvalds's avatar
Linus Torvalds committed
868 869 870

#if 0
	if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
871
		return res;
Linus Torvalds's avatar
Linus Torvalds committed
872 873 874 875 876 877 878
#endif

	frametype = skb->data[2];
	lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
	src_addr  = (rose_address *)(skb->data + 9);
	dest_addr = (rose_address *)(skb->data + 4);

879 880 881 882 883 884 885 886
	spin_lock_irqsave(&rose_node_list_lock, flags);
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
	spin_lock_irqsave(&rose_route_list_lock, flags);

	rose_neigh = rose_neigh_list;
	while (rose_neigh != NULL) {
		if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
		    ax25->ax25_dev->dev == rose_neigh->dev)
Linus Torvalds's avatar
Linus Torvalds committed
887
			break;
888 889
		rose_neigh = rose_neigh->next;
	}
Linus Torvalds's avatar
Linus Torvalds committed
890 891

	if (rose_neigh == NULL) {
892 893 894
		printk("rose_route : unknown neighbour or device %s\n",
		       ax2asc(&ax25->dest_addr));
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897 898 899 900 901 902 903 904 905 906 907
	}

	/*
	 *	Obviously the link is working, halt the ftimer.
	 */
	rose_stop_ftimer(rose_neigh);

	/*
	 *	LCI of zero is always for us, and its always a restart
	 * 	frame.
	 */
	if (lci == 0) {
		rose_link_rx_restart(skb, rose_neigh, frametype);
908
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
909 910 911 912 913 914 915
	}

	/*
	 *	Find an existing socket.
	 */
	if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
		if (frametype == ROSE_CALL_REQUEST) {
916
			rose_cb *rose = rose_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
917 918
			/* Remove an existing unused socket */
			rose_clear_queues(sk);
919 920 921 922 923 924 925 926 927
			rose->cause	 = ROSE_NETWORK_CONGESTION;
			rose->diagnostic = 0;
			rose->neighbour->use--;
			rose->neighbour	 = NULL;
			rose->lci	 = 0;
			rose->state	 = ROSE_STATE_0;
			sk->state	 = TCP_CLOSE;
			sk->err		 = 0;
			sk->shutdown	 |= SEND_SHUTDOWN;
Linus Torvalds's avatar
Linus Torvalds committed
928 929
			if (!sk->dead)
				sk->state_change(sk);
930
			sk->dead         = 1;
Linus Torvalds's avatar
Linus Torvalds committed
931 932 933
		}
		else {
			skb->h.raw = skb->data;
934 935
			res = rose_process_rx_frame(sk, skb);
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
936 937 938 939 940 941 942 943
		}
	}

	/*
	 *	Is is a Call Request and is it for us ?
	 */
	if (frametype == ROSE_CALL_REQUEST)
		if ((dev = rose_dev_get(dest_addr)) != NULL) {
944
			res = rose_rx_call_request(skb, dev, rose_neigh, lci);
Linus Torvalds's avatar
Linus Torvalds committed
945
			dev_put(dev);
946
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950
		}

	if (!sysctl_rose_routing_control) {
		rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
951
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956
	}

	/*
	 *	Route it to the next in line if we have an entry for it.
	 */
957 958 959 960
	rose_route = rose_route_list;
	while (rose_route != NULL) {
		if (rose_route->lci1 == lci &&
		    rose_route->neigh1 == rose_neigh) {
Linus Torvalds's avatar
Linus Torvalds committed
961 962 963 964 965 966 967 968 969 970 971
			if (frametype == ROSE_CALL_REQUEST) {
				/* F6FBB - Remove an existing unused route */
				rose_remove_route(rose_route);
				break;
			} else if (rose_route->neigh2 != NULL) {
				skb->data[0] &= 0xF0;
				skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
				skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
				rose_transmit_link(skb, rose_route->neigh2);
				if (frametype == ROSE_CLEAR_CONFIRMATION)
					rose_remove_route(rose_route);
972 973
				res = 1;
				goto out;
Linus Torvalds's avatar
Linus Torvalds committed
974 975 976
			} else {
				if (frametype == ROSE_CLEAR_CONFIRMATION)
					rose_remove_route(rose_route);
977
				goto out;
Linus Torvalds's avatar
Linus Torvalds committed
978 979
			}
		}
980 981
		if (rose_route->lci2 == lci &&
		    rose_route->neigh2 == rose_neigh) {
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984 985 986 987 988 989 990 991 992
			if (frametype == ROSE_CALL_REQUEST) {
				/* F6FBB - Remove an existing unused route */
				rose_remove_route(rose_route);
				break;
			} else if (rose_route->neigh1 != NULL) {
				skb->data[0] &= 0xF0;
				skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
				skb->data[1]  = (rose_route->lci1 >> 0) & 0xFF;
				rose_transmit_link(skb, rose_route->neigh1);
				if (frametype == ROSE_CLEAR_CONFIRMATION)
					rose_remove_route(rose_route);
993 994
				res = 1;
				goto out;
Linus Torvalds's avatar
Linus Torvalds committed
995 996 997
			} else {
				if (frametype == ROSE_CLEAR_CONFIRMATION)
					rose_remove_route(rose_route);
998
				goto out;
Linus Torvalds's avatar
Linus Torvalds committed
999 1000
			}
		}
1001
		rose_route = rose_route->next;
Linus Torvalds's avatar
Linus Torvalds committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	}

	/*
	 *	We know that:
	 *	1. The frame isn't for us,
	 *	2. It isn't "owned" by any existing route.
	 */
	if (frametype != ROSE_CALL_REQUEST)	/* XXX */
		return 0;

	len  = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
	len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;

	memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1016

Linus Torvalds's avatar
Linus Torvalds committed
1017 1018
	if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
		rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1019
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1020 1021 1022 1023 1024
	}

	/*
	 *	Check for routing loops.
	 */
1025 1026
	rose_route = rose_route_list;
	while (rose_route != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028 1029 1030 1031
		if (rose_route->rand == facilities.rand &&
		    rosecmp(src_addr, &rose_route->src_addr) == 0 &&
		    ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
		    ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
			rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1032
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1033
		}
1034
		rose_route = rose_route->next;
Linus Torvalds's avatar
Linus Torvalds committed
1035 1036 1037 1038
	}

	if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic)) == NULL) {
		rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1039
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1040 1041 1042 1043
	}

	if ((new_lci = rose_new_lci(new_neigh)) == 0) {
		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1044
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1045 1046 1047 1048
	}

	if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1049
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	}

	rose_route->lci1      = lci;
	rose_route->src_addr  = *src_addr;
	rose_route->dest_addr = *dest_addr;
	rose_route->src_call  = facilities.dest_call;
	rose_route->dest_call = facilities.source_call;
	rose_route->rand      = facilities.rand;
	rose_route->neigh1    = rose_neigh;
	rose_route->lci2      = new_lci;
	rose_route->neigh2    = new_neigh;

	rose_route->neigh1->use++;
	rose_route->neigh2->use++;

	rose_route->next = rose_route_list;
	rose_route_list  = rose_route;

	skb->data[0] &= 0xF0;
	skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
	skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;

	rose_transmit_link(skb, rose_route->neigh2);
1073
	res = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1074

1075 1076 1077 1078 1079 1080
out:
	spin_unlock_irqrestore(&rose_route_list_lock, flags);
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
	spin_unlock_irqrestore(&rose_node_list_lock, flags);

	return res;
Linus Torvalds's avatar
Linus Torvalds committed
1081 1082 1083 1084 1085
}

int rose_nodes_get_info(char *buffer, char **start, off_t offset, int length)
{
	struct rose_node *rose_node;
1086
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1087 1088 1089 1090 1091
	int len     = 0;
	off_t pos   = 0;
	off_t begin = 0;
	int i;

1092
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	len += sprintf(buffer, "address    mask n neigh neigh neigh\n");

	for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) {
		/* if (rose_node->loopback) {
			len += sprintf(buffer + len, "%-10s %04d 1 loopback\n",
				rose2asc(&rose_node->address),
				rose_node->mask);
		} else { */
			len += sprintf(buffer + len, "%-10s %04d %d",
				rose2asc(&rose_node->address),
				rose_node->mask,
				rose_node->count);

			for (i = 0; i < rose_node->count; i++)
				len += sprintf(buffer + len, " %05d",
					rose_node->neighbour[i]->number);

			len += sprintf(buffer + len, "\n");
		/* } */

		pos = begin + len;

		if (pos < offset) {
			len   = 0;
			begin = pos;
		}

		if (pos > offset + length)
			break;
	}
1124
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1125 1126 1127 1128

	*start = buffer + (offset - begin);
	len   -= (offset - begin);

1129 1130
	if (len > length)
		len = length;
Linus Torvalds's avatar
Linus Torvalds committed
1131 1132

	return len;
1133
}
Linus Torvalds's avatar
Linus Torvalds committed
1134 1135 1136 1137

int rose_neigh_get_info(char *buffer, char **start, off_t offset, int length)
{
	struct rose_neigh *rose_neigh;
1138
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1139 1140 1141 1142 1143
	int len     = 0;
	off_t pos   = 0;
	off_t begin = 0;
	int i;

1144
	spin_lock_irqsave(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179

	len += sprintf(buffer, "addr  callsign  dev  count use mode restart  t0  tf digipeaters\n");

	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
		/* if (!rose_neigh->loopback) { */
			len += sprintf(buffer + len, "%05d %-9s %-4s   %3d %3d  %3s     %3s %3lu %3lu",
				rose_neigh->number,
				(rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(&rose_neigh->callsign),
				rose_neigh->dev ? rose_neigh->dev->name : "???",
				rose_neigh->count,
				rose_neigh->use,
				(rose_neigh->dce_mode) ? "DCE" : "DTE",
				(rose_neigh->restarted) ? "yes" : "no",
				ax25_display_timer(&rose_neigh->t0timer) / HZ,
				ax25_display_timer(&rose_neigh->ftimer)  / HZ);

			if (rose_neigh->digipeat != NULL) {
				for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
					len += sprintf(buffer + len, " %s", ax2asc(&rose_neigh->digipeat->calls[i]));
			}

			len += sprintf(buffer + len, "\n");

			pos = begin + len;

			if (pos < offset) {
				len   = 0;
				begin = pos;
			}

			if (pos > offset + length)
				break;
		/* } */
	}

1180
	spin_unlock_irqrestore(&rose_neigh_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183 1184

	*start = buffer + (offset - begin);
	len   -= (offset - begin);

1185 1186
	if (len > length)
		len = length;
Linus Torvalds's avatar
Linus Torvalds committed
1187 1188

	return len;
1189
}
Linus Torvalds's avatar
Linus Torvalds committed
1190 1191 1192 1193

int rose_routes_get_info(char *buffer, char **start, off_t offset, int length)
{
	struct rose_route *rose_route;
1194
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1195 1196 1197 1198
	int len     = 0;
	off_t pos   = 0;
	off_t begin = 0;

1199
	spin_lock_irqsave(&rose_route_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

	len += sprintf(buffer, "lci  address     callsign   neigh  <-> lci  address     callsign   neigh\n");

	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next) {
		if (rose_route->neigh1 != NULL) {
			len += sprintf(buffer + len, "%3.3X  %-10s  %-9s  %05d      ",
				rose_route->lci1,
				rose2asc(&rose_route->src_addr),
				ax2asc(&rose_route->src_call),
				rose_route->neigh1->number);
		} else {
			len += sprintf(buffer + len, "000  *           *          00000      ");
		}

		if (rose_route->neigh2 != NULL) {
			len += sprintf(buffer + len, "%3.3X  %-10s  %-9s  %05d\n",
				rose_route->lci2,
				rose2asc(&rose_route->dest_addr),
				ax2asc(&rose_route->dest_call),
				rose_route->neigh2->number);
		} else {
			len += sprintf(buffer + len, "000  *           *          00000\n");
		}

		pos = begin + len;

		if (pos < offset) {
			len   = 0;
			begin = pos;
		}

		if (pos > offset + length)
			break;
	}

1235
	spin_unlock_irqrestore(&rose_route_list_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1236 1237 1238 1239

	*start = buffer + (offset - begin);
	len   -= (offset - begin);

1240 1241
	if (len > length)
		len = length;
Linus Torvalds's avatar
Linus Torvalds committed
1242 1243

	return len;
1244
}
Linus Torvalds's avatar
Linus Torvalds committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

/*
 *	Release all memory associated with ROSE routing structures.
 */
void __exit rose_rt_free(void)
{
	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
	struct rose_node  *t, *rose_node  = rose_node_list;
	struct rose_route *u, *rose_route = rose_route_list;

	while (rose_neigh != NULL) {
		s          = rose_neigh;
		rose_neigh = rose_neigh->next;

		rose_remove_neigh(s);
	}

	while (rose_node != NULL) {
		t         = rose_node;
		rose_node = rose_node->next;

		rose_remove_node(t);
	}

	while (rose_route != NULL) {
		u          = rose_route;
		rose_route = rose_route->next;

		rose_remove_route(u);
	}
}