Linux Audio

Check our new training course

Loading...
v4.6
 
   1/*
   2 * This program is free software; you can redistribute it and/or modify
   3 * it under the terms of the GNU General Public License as published by
   4 * the Free Software Foundation; either version 2 of the License, or
   5 * (at your option) any later version.
   6 *
   7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
   8 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
   9 */
  10#include <linux/errno.h>
  11#include <linux/types.h>
  12#include <linux/socket.h>
  13#include <linux/in.h>
  14#include <linux/kernel.h>
  15#include <linux/timer.h>
  16#include <linux/string.h>
  17#include <linux/sockios.h>
  18#include <linux/net.h>
  19#include <linux/slab.h>
  20#include <net/ax25.h>
  21#include <linux/inet.h>
  22#include <linux/netdevice.h>
  23#include <net/arp.h>
  24#include <linux/if_arp.h>
  25#include <linux/skbuff.h>
  26#include <net/sock.h>
  27#include <net/tcp_states.h>
  28#include <asm/uaccess.h>
  29#include <linux/fcntl.h>
  30#include <linux/termios.h>	/* For TIOCINQ/OUTQ */
  31#include <linux/mm.h>
  32#include <linux/interrupt.h>
  33#include <linux/notifier.h>
  34#include <linux/init.h>
  35#include <net/rose.h>
  36#include <linux/seq_file.h>
  37#include <linux/export.h>
  38
  39static unsigned int rose_neigh_no = 1;
  40
  41static struct rose_node  *rose_node_list;
  42static DEFINE_SPINLOCK(rose_node_list_lock);
  43static struct rose_neigh *rose_neigh_list;
  44static DEFINE_SPINLOCK(rose_neigh_list_lock);
  45static struct rose_route *rose_route_list;
  46static DEFINE_SPINLOCK(rose_route_list_lock);
  47
  48struct rose_neigh *rose_loopback_neigh;
  49
  50/*
  51 *	Add a new route to a node, and in the process add the node and the
  52 *	neighbour if it is new.
  53 */
  54static int __must_check rose_add_node(struct rose_route_struct *rose_route,
  55	struct net_device *dev)
  56{
  57	struct rose_node  *rose_node, *rose_tmpn, *rose_tmpp;
  58	struct rose_neigh *rose_neigh;
  59	int i, res = 0;
  60
  61	spin_lock_bh(&rose_node_list_lock);
  62	spin_lock_bh(&rose_neigh_list_lock);
  63
  64	rose_node = rose_node_list;
  65	while (rose_node != NULL) {
  66		if ((rose_node->mask == rose_route->mask) &&
  67		    (rosecmpm(&rose_route->address, &rose_node->address,
  68			      rose_route->mask) == 0))
  69			break;
  70		rose_node = rose_node->next;
  71	}
  72
  73	if (rose_node != NULL && rose_node->loopback) {
  74		res = -EINVAL;
  75		goto out;
  76	}
  77
  78	rose_neigh = rose_neigh_list;
  79	while (rose_neigh != NULL) {
  80		if (ax25cmp(&rose_route->neighbour,
  81			    &rose_neigh->callsign) == 0 &&
  82		    rose_neigh->dev == dev)
  83			break;
  84		rose_neigh = rose_neigh->next;
  85	}
  86
  87	if (rose_neigh == NULL) {
  88		rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
  89		if (rose_neigh == NULL) {
  90			res = -ENOMEM;
  91			goto out;
  92		}
  93
  94		rose_neigh->callsign  = rose_route->neighbour;
  95		rose_neigh->digipeat  = NULL;
  96		rose_neigh->ax25      = NULL;
  97		rose_neigh->dev       = dev;
  98		rose_neigh->count     = 0;
  99		rose_neigh->use       = 0;
 100		rose_neigh->dce_mode  = 0;
 101		rose_neigh->loopback  = 0;
 102		rose_neigh->number    = rose_neigh_no++;
 103		rose_neigh->restarted = 0;
 104
 105		skb_queue_head_init(&rose_neigh->queue);
 106
 107		init_timer(&rose_neigh->ftimer);
 108		init_timer(&rose_neigh->t0timer);
 109
 110		if (rose_route->ndigis != 0) {
 111			rose_neigh->digipeat =
 112				kmalloc(sizeof(ax25_digi), GFP_ATOMIC);
 113			if (rose_neigh->digipeat == NULL) {
 114				kfree(rose_neigh);
 115				res = -ENOMEM;
 116				goto out;
 117			}
 118
 119			rose_neigh->digipeat->ndigi      = rose_route->ndigis;
 120			rose_neigh->digipeat->lastrepeat = -1;
 121
 122			for (i = 0; i < rose_route->ndigis; i++) {
 123				rose_neigh->digipeat->calls[i]    =
 124					rose_route->digipeaters[i];
 125				rose_neigh->digipeat->repeated[i] = 0;
 126			}
 127		}
 128
 129		rose_neigh->next = rose_neigh_list;
 130		rose_neigh_list  = rose_neigh;
 131	}
 132
 133	/*
 134	 * This is a new node to be inserted into the list. Find where it needs
 135	 * to be inserted into the list, and insert it. We want to be sure
 136	 * to order the list in descending order of mask size to ensure that
 137	 * later when we are searching this list the first match will be the
 138	 * best match.
 139	 */
 140	if (rose_node == NULL) {
 141		rose_tmpn = rose_node_list;
 142		rose_tmpp = NULL;
 143
 144		while (rose_tmpn != NULL) {
 145			if (rose_tmpn->mask > rose_route->mask) {
 146				rose_tmpp = rose_tmpn;
 147				rose_tmpn = rose_tmpn->next;
 148			} else {
 149				break;
 150			}
 151		}
 152
 153		/* create new node */
 154		rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
 155		if (rose_node == NULL) {
 156			res = -ENOMEM;
 157			goto out;
 158		}
 159
 160		rose_node->address      = rose_route->address;
 161		rose_node->mask         = rose_route->mask;
 162		rose_node->count        = 1;
 163		rose_node->loopback     = 0;
 164		rose_node->neighbour[0] = rose_neigh;
 165
 166		if (rose_tmpn == NULL) {
 167			if (rose_tmpp == NULL) {	/* Empty list */
 168				rose_node_list  = rose_node;
 169				rose_node->next = NULL;
 170			} else {
 171				rose_tmpp->next = rose_node;
 172				rose_node->next = NULL;
 173			}
 174		} else {
 175			if (rose_tmpp == NULL) {	/* 1st node */
 176				rose_node->next = rose_node_list;
 177				rose_node_list  = rose_node;
 178			} else {
 179				rose_tmpp->next = rose_node;
 180				rose_node->next = rose_tmpn;
 181			}
 182		}
 183		rose_neigh->count++;
 184
 185		goto out;
 186	}
 187
 188	/* We have space, slot it in */
 189	if (rose_node->count < 3) {
 190		rose_node->neighbour[rose_node->count] = rose_neigh;
 191		rose_node->count++;
 192		rose_neigh->count++;
 193	}
 194
 195out:
 196	spin_unlock_bh(&rose_neigh_list_lock);
 197	spin_unlock_bh(&rose_node_list_lock);
 198
 199	return res;
 200}
 201
 202/*
 203 * Caller is holding rose_node_list_lock.
 204 */
 205static void rose_remove_node(struct rose_node *rose_node)
 206{
 207	struct rose_node *s;
 208
 209	if ((s = rose_node_list) == rose_node) {
 210		rose_node_list = rose_node->next;
 211		kfree(rose_node);
 212		return;
 213	}
 214
 215	while (s != NULL && s->next != NULL) {
 216		if (s->next == rose_node) {
 217			s->next = rose_node->next;
 218			kfree(rose_node);
 219			return;
 220		}
 221
 222		s = s->next;
 223	}
 224}
 225
 226/*
 227 * Caller is holding rose_neigh_list_lock.
 228 */
 229static void rose_remove_neigh(struct rose_neigh *rose_neigh)
 230{
 231	struct rose_neigh *s;
 232
 233	rose_stop_ftimer(rose_neigh);
 234	rose_stop_t0timer(rose_neigh);
 235
 236	skb_queue_purge(&rose_neigh->queue);
 237
 238	if ((s = rose_neigh_list) == rose_neigh) {
 239		rose_neigh_list = rose_neigh->next;
 240		if (rose_neigh->ax25)
 241			ax25_cb_put(rose_neigh->ax25);
 242		kfree(rose_neigh->digipeat);
 243		kfree(rose_neigh);
 244		return;
 245	}
 246
 247	while (s != NULL && s->next != NULL) {
 248		if (s->next == rose_neigh) {
 249			s->next = rose_neigh->next;
 250			if (rose_neigh->ax25)
 251				ax25_cb_put(rose_neigh->ax25);
 252			kfree(rose_neigh->digipeat);
 253			kfree(rose_neigh);
 254			return;
 255		}
 256
 257		s = s->next;
 258	}
 259}
 260
 261/*
 262 * Caller is holding rose_route_list_lock.
 263 */
 264static void rose_remove_route(struct rose_route *rose_route)
 265{
 266	struct rose_route *s;
 267
 268	if (rose_route->neigh1 != NULL)
 269		rose_route->neigh1->use--;
 270
 271	if (rose_route->neigh2 != NULL)
 272		rose_route->neigh2->use--;
 273
 274	if ((s = rose_route_list) == rose_route) {
 275		rose_route_list = rose_route->next;
 276		kfree(rose_route);
 277		return;
 278	}
 279
 280	while (s != NULL && s->next != NULL) {
 281		if (s->next == rose_route) {
 282			s->next = rose_route->next;
 283			kfree(rose_route);
 284			return;
 285		}
 286
 287		s = s->next;
 288	}
 289}
 290
 291/*
 292 *	"Delete" a node. Strictly speaking remove a route to a node. The node
 293 *	is only deleted if no routes are left to it.
 294 */
 295static int rose_del_node(struct rose_route_struct *rose_route,
 296	struct net_device *dev)
 297{
 298	struct rose_node  *rose_node;
 299	struct rose_neigh *rose_neigh;
 300	int i, err = 0;
 301
 302	spin_lock_bh(&rose_node_list_lock);
 303	spin_lock_bh(&rose_neigh_list_lock);
 304
 305	rose_node = rose_node_list;
 306	while (rose_node != NULL) {
 307		if ((rose_node->mask == rose_route->mask) &&
 308		    (rosecmpm(&rose_route->address, &rose_node->address,
 309			      rose_route->mask) == 0))
 310			break;
 311		rose_node = rose_node->next;
 312	}
 313
 314	if (rose_node == NULL || rose_node->loopback) {
 315		err = -EINVAL;
 316		goto out;
 317	}
 318
 319	rose_neigh = rose_neigh_list;
 320	while (rose_neigh != NULL) {
 321		if (ax25cmp(&rose_route->neighbour,
 322			    &rose_neigh->callsign) == 0 &&
 323		    rose_neigh->dev == dev)
 324			break;
 325		rose_neigh = rose_neigh->next;
 326	}
 327
 328	if (rose_neigh == NULL) {
 329		err = -EINVAL;
 330		goto out;
 331	}
 332
 333	for (i = 0; i < rose_node->count; i++) {
 334		if (rose_node->neighbour[i] == rose_neigh) {
 335			rose_neigh->count--;
 336
 337			if (rose_neigh->count == 0 && rose_neigh->use == 0)
 338				rose_remove_neigh(rose_neigh);
 339
 340			rose_node->count--;
 341
 342			if (rose_node->count == 0) {
 343				rose_remove_node(rose_node);
 344			} else {
 345				switch (i) {
 346				case 0:
 347					rose_node->neighbour[0] =
 348						rose_node->neighbour[1];
 
 349				case 1:
 350					rose_node->neighbour[1] =
 351						rose_node->neighbour[2];
 
 352				case 2:
 353					break;
 354				}
 355			}
 356			goto out;
 357		}
 358	}
 359	err = -EINVAL;
 360
 361out:
 362	spin_unlock_bh(&rose_neigh_list_lock);
 363	spin_unlock_bh(&rose_node_list_lock);
 364
 365	return err;
 366}
 367
 368/*
 369 *	Add the loopback neighbour.
 370 */
 371void rose_add_loopback_neigh(void)
 372{
 373	struct rose_neigh *sn;
 374
 375	rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
 376	if (!rose_loopback_neigh)
 377		return;
 378	sn = rose_loopback_neigh;
 379
 380	sn->callsign  = null_ax25_address;
 381	sn->digipeat  = NULL;
 382	sn->ax25      = NULL;
 383	sn->dev       = NULL;
 384	sn->count     = 0;
 385	sn->use       = 0;
 386	sn->dce_mode  = 1;
 387	sn->loopback  = 1;
 388	sn->number    = rose_neigh_no++;
 389	sn->restarted = 1;
 390
 391	skb_queue_head_init(&sn->queue);
 392
 393	init_timer(&sn->ftimer);
 394	init_timer(&sn->t0timer);
 395
 396	spin_lock_bh(&rose_neigh_list_lock);
 397	sn->next = rose_neigh_list;
 398	rose_neigh_list           = sn;
 399	spin_unlock_bh(&rose_neigh_list_lock);
 400}
 401
 402/*
 403 *	Add a loopback node.
 404 */
 405int rose_add_loopback_node(rose_address *address)
 406{
 407	struct rose_node *rose_node;
 408	int err = 0;
 409
 410	spin_lock_bh(&rose_node_list_lock);
 411
 412	rose_node = rose_node_list;
 413	while (rose_node != NULL) {
 414		if ((rose_node->mask == 10) &&
 415		     (rosecmpm(address, &rose_node->address, 10) == 0) &&
 416		     rose_node->loopback)
 417			break;
 418		rose_node = rose_node->next;
 419	}
 420
 421	if (rose_node != NULL)
 422		goto out;
 423
 424	if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
 425		err = -ENOMEM;
 426		goto out;
 427	}
 428
 429	rose_node->address      = *address;
 430	rose_node->mask         = 10;
 431	rose_node->count        = 1;
 432	rose_node->loopback     = 1;
 433	rose_node->neighbour[0] = rose_loopback_neigh;
 434
 435	/* Insert at the head of list. Address is always mask=10 */
 436	rose_node->next = rose_node_list;
 437	rose_node_list  = rose_node;
 438
 439	rose_loopback_neigh->count++;
 440
 441out:
 442	spin_unlock_bh(&rose_node_list_lock);
 443
 444	return err;
 445}
 446
 447/*
 448 *	Delete a loopback node.
 449 */
 450void rose_del_loopback_node(rose_address *address)
 451{
 452	struct rose_node *rose_node;
 453
 454	spin_lock_bh(&rose_node_list_lock);
 455
 456	rose_node = rose_node_list;
 457	while (rose_node != NULL) {
 458		if ((rose_node->mask == 10) &&
 459		    (rosecmpm(address, &rose_node->address, 10) == 0) &&
 460		    rose_node->loopback)
 461			break;
 462		rose_node = rose_node->next;
 463	}
 464
 465	if (rose_node == NULL)
 466		goto out;
 467
 468	rose_remove_node(rose_node);
 469
 470	rose_loopback_neigh->count--;
 471
 472out:
 473	spin_unlock_bh(&rose_node_list_lock);
 474}
 475
 476/*
 477 *	A device has been removed. Remove its routes and neighbours.
 478 */
 479void rose_rt_device_down(struct net_device *dev)
 480{
 481	struct rose_neigh *s, *rose_neigh;
 482	struct rose_node  *t, *rose_node;
 483	int i;
 484
 485	spin_lock_bh(&rose_node_list_lock);
 486	spin_lock_bh(&rose_neigh_list_lock);
 487	rose_neigh = rose_neigh_list;
 488	while (rose_neigh != NULL) {
 489		s          = rose_neigh;
 490		rose_neigh = rose_neigh->next;
 491
 492		if (s->dev != dev)
 493			continue;
 494
 495		rose_node = rose_node_list;
 496
 497		while (rose_node != NULL) {
 498			t         = rose_node;
 499			rose_node = rose_node->next;
 500
 501			for (i = 0; i < t->count; i++) {
 502				if (t->neighbour[i] != s)
 503					continue;
 504
 505				t->count--;
 506
 507				switch (i) {
 508				case 0:
 509					t->neighbour[0] = t->neighbour[1];
 
 510				case 1:
 511					t->neighbour[1] = t->neighbour[2];
 
 512				case 2:
 513					break;
 514				}
 515			}
 516
 517			if (t->count <= 0)
 518				rose_remove_node(t);
 519		}
 520
 521		rose_remove_neigh(s);
 522	}
 523	spin_unlock_bh(&rose_neigh_list_lock);
 524	spin_unlock_bh(&rose_node_list_lock);
 525}
 526
 527#if 0 /* Currently unused */
 528/*
 529 *	A device has been removed. Remove its links.
 530 */
 531void rose_route_device_down(struct net_device *dev)
 532{
 533	struct rose_route *s, *rose_route;
 534
 535	spin_lock_bh(&rose_route_list_lock);
 536	rose_route = rose_route_list;
 537	while (rose_route != NULL) {
 538		s          = rose_route;
 539		rose_route = rose_route->next;
 540
 541		if (s->neigh1->dev == dev || s->neigh2->dev == dev)
 542			rose_remove_route(s);
 543	}
 544	spin_unlock_bh(&rose_route_list_lock);
 545}
 546#endif
 547
 548/*
 549 *	Clear all nodes and neighbours out, except for neighbours with
 550 *	active connections going through them.
 551 *  Do not clear loopback neighbour and nodes.
 552 */
 553static int rose_clear_routes(void)
 554{
 555	struct rose_neigh *s, *rose_neigh;
 556	struct rose_node  *t, *rose_node;
 557
 558	spin_lock_bh(&rose_node_list_lock);
 559	spin_lock_bh(&rose_neigh_list_lock);
 560
 561	rose_neigh = rose_neigh_list;
 562	rose_node  = rose_node_list;
 563
 564	while (rose_node != NULL) {
 565		t         = rose_node;
 566		rose_node = rose_node->next;
 567		if (!t->loopback)
 568			rose_remove_node(t);
 569	}
 570
 571	while (rose_neigh != NULL) {
 572		s          = rose_neigh;
 573		rose_neigh = rose_neigh->next;
 574
 575		if (s->use == 0 && !s->loopback) {
 576			s->count = 0;
 577			rose_remove_neigh(s);
 578		}
 579	}
 580
 581	spin_unlock_bh(&rose_neigh_list_lock);
 582	spin_unlock_bh(&rose_node_list_lock);
 583
 584	return 0;
 585}
 586
 587/*
 588 *	Check that the device given is a valid AX.25 interface that is "up".
 589 * 	called with RTNL
 590 */
 591static struct net_device *rose_ax25_dev_find(char *devname)
 592{
 593	struct net_device *dev;
 594
 595	if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
 596		return NULL;
 597
 598	if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
 599		return dev;
 600
 601	return NULL;
 602}
 603
 604/*
 605 *	Find the first active ROSE device, usually "rose0".
 606 */
 607struct net_device *rose_dev_first(void)
 608{
 609	struct net_device *dev, *first = NULL;
 610
 611	rcu_read_lock();
 612	for_each_netdev_rcu(&init_net, dev) {
 613		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
 614			if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
 615				first = dev;
 616	}
 
 
 617	rcu_read_unlock();
 618
 619	return first;
 620}
 621
 622/*
 623 *	Find the ROSE device for the given address.
 624 */
 625struct net_device *rose_dev_get(rose_address *addr)
 626{
 627	struct net_device *dev;
 628
 629	rcu_read_lock();
 630	for_each_netdev_rcu(&init_net, dev) {
 631		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
 
 632			dev_hold(dev);
 633			goto out;
 634		}
 635	}
 636	dev = NULL;
 637out:
 638	rcu_read_unlock();
 639	return dev;
 640}
 641
 642static int rose_dev_exists(rose_address *addr)
 643{
 644	struct net_device *dev;
 645
 646	rcu_read_lock();
 647	for_each_netdev_rcu(&init_net, dev) {
 648		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
 
 649			goto out;
 650	}
 651	dev = NULL;
 652out:
 653	rcu_read_unlock();
 654	return dev != NULL;
 655}
 656
 657
 658
 659
 660struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
 661{
 662	struct rose_route *rose_route;
 663
 664	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
 665		if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
 666		    (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
 667			return rose_route;
 668
 669	return NULL;
 670}
 671
 672/*
 673 *	Find a neighbour or a route given a ROSE address.
 674 */
 675struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
 676	unsigned char *diagnostic, int route_frame)
 677{
 678	struct rose_neigh *res = NULL;
 679	struct rose_node *node;
 680	int failed = 0;
 681	int i;
 682
 683	if (!route_frame) spin_lock_bh(&rose_node_list_lock);
 684	for (node = rose_node_list; node != NULL; node = node->next) {
 685		if (rosecmpm(addr, &node->address, node->mask) == 0) {
 686			for (i = 0; i < node->count; i++) {
 687				if (node->neighbour[i]->restarted) {
 688					res = node->neighbour[i];
 689					goto out;
 690				}
 691			}
 692		}
 693	}
 694	if (!route_frame) { /* connect request */
 695		for (node = rose_node_list; node != NULL; node = node->next) {
 696			if (rosecmpm(addr, &node->address, node->mask) == 0) {
 697				for (i = 0; i < node->count; i++) {
 698					if (!rose_ftimer_running(node->neighbour[i])) {
 699						res = node->neighbour[i];
 700						failed = 0;
 701						goto out;
 702					}
 703					failed = 1;
 704				}
 705			}
 706		}
 707	}
 708
 709	if (failed) {
 710		*cause      = ROSE_OUT_OF_ORDER;
 711		*diagnostic = 0;
 712	} else {
 713		*cause      = ROSE_NOT_OBTAINABLE;
 714		*diagnostic = 0;
 715	}
 716
 717out:
 718	if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
 719	return res;
 720}
 721
 722/*
 723 *	Handle the ioctls that control the routing functions.
 724 */
 725int rose_rt_ioctl(unsigned int cmd, void __user *arg)
 726{
 727	struct rose_route_struct rose_route;
 728	struct net_device *dev;
 729	int err;
 730
 731	switch (cmd) {
 732	case SIOCADDRT:
 733		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
 734			return -EFAULT;
 735		if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
 736			return -EINVAL;
 737		if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
 738			return -EINVAL;
 739		if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
 740			return -EINVAL;
 741		if (rose_route.ndigis > AX25_MAX_DIGIS)
 742			return -EINVAL;
 743		err = rose_add_node(&rose_route, dev);
 744		return err;
 745
 746	case SIOCDELRT:
 747		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
 748			return -EFAULT;
 749		if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
 750			return -EINVAL;
 751		err = rose_del_node(&rose_route, dev);
 752		return err;
 753
 754	case SIOCRSCLRRT:
 755		return rose_clear_routes();
 756
 757	default:
 758		return -EINVAL;
 759	}
 760
 761	return 0;
 762}
 763
 764static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
 765{
 766	struct rose_route *rose_route, *s;
 767
 768	rose_neigh->restarted = 0;
 769
 770	rose_stop_t0timer(rose_neigh);
 771	rose_start_ftimer(rose_neigh);
 772
 773	skb_queue_purge(&rose_neigh->queue);
 774
 775	spin_lock_bh(&rose_route_list_lock);
 776
 777	rose_route = rose_route_list;
 778
 779	while (rose_route != NULL) {
 780		if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
 781		    (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL)       ||
 782		    (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
 783			s = rose_route->next;
 784			rose_remove_route(rose_route);
 785			rose_route = s;
 786			continue;
 787		}
 788
 789		if (rose_route->neigh1 == rose_neigh) {
 790			rose_route->neigh1->use--;
 791			rose_route->neigh1 = NULL;
 792			rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
 793		}
 794
 795		if (rose_route->neigh2 == rose_neigh) {
 796			rose_route->neigh2->use--;
 797			rose_route->neigh2 = NULL;
 798			rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
 799		}
 800
 801		rose_route = rose_route->next;
 802	}
 803	spin_unlock_bh(&rose_route_list_lock);
 804}
 805
 806/*
 807 * 	A level 2 link has timed out, therefore it appears to be a poor link,
 808 *	then don't use that neighbour until it is reset. Blow away all through
 809 *	routes and connections using this route.
 810 */
 811void rose_link_failed(ax25_cb *ax25, int reason)
 812{
 813	struct rose_neigh *rose_neigh;
 814
 815	spin_lock_bh(&rose_neigh_list_lock);
 816	rose_neigh = rose_neigh_list;
 817	while (rose_neigh != NULL) {
 818		if (rose_neigh->ax25 == ax25)
 819			break;
 820		rose_neigh = rose_neigh->next;
 821	}
 822
 823	if (rose_neigh != NULL) {
 824		rose_neigh->ax25 = NULL;
 825		ax25_cb_put(ax25);
 826
 827		rose_del_route_by_neigh(rose_neigh);
 828		rose_kill_by_neigh(rose_neigh);
 829	}
 830	spin_unlock_bh(&rose_neigh_list_lock);
 831}
 832
 833/*
 834 * 	A device has been "downed" remove its link status. Blow away all
 835 *	through routes and connections that use this device.
 836 */
 837void rose_link_device_down(struct net_device *dev)
 838{
 839	struct rose_neigh *rose_neigh;
 840
 841	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
 842		if (rose_neigh->dev == dev) {
 843			rose_del_route_by_neigh(rose_neigh);
 844			rose_kill_by_neigh(rose_neigh);
 845		}
 846	}
 847}
 848
 849/*
 850 *	Route a frame to an appropriate AX.25 connection.
 
 851 */
 852int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
 853{
 854	struct rose_neigh *rose_neigh, *new_neigh;
 855	struct rose_route *rose_route;
 856	struct rose_facilities_struct facilities;
 857	rose_address *src_addr, *dest_addr;
 858	struct sock *sk;
 859	unsigned short frametype;
 860	unsigned int lci, new_lci;
 861	unsigned char cause, diagnostic;
 862	struct net_device *dev;
 863	int res = 0;
 864	char buf[11];
 865
 866	if (skb->len < ROSE_MIN_LEN)
 867		return res;
 
 
 
 
 868	frametype = skb->data[2];
 869	lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
 870	if (frametype == ROSE_CALL_REQUEST &&
 871	    (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
 872	     skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
 873	     ROSE_CALL_REQ_ADDR_LEN_VAL))
 874		return res;
 875	src_addr  = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
 876	dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
 877
 878	spin_lock_bh(&rose_neigh_list_lock);
 879	spin_lock_bh(&rose_route_list_lock);
 880
 881	rose_neigh = rose_neigh_list;
 882	while (rose_neigh != NULL) {
 883		if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
 884		    ax25->ax25_dev->dev == rose_neigh->dev)
 885			break;
 886		rose_neigh = rose_neigh->next;
 887	}
 888
 889	if (rose_neigh == NULL) {
 890		printk("rose_route : unknown neighbour or device %s\n",
 891		       ax2asc(buf, &ax25->dest_addr));
 892		goto out;
 893	}
 894
 895	/*
 896	 *	Obviously the link is working, halt the ftimer.
 897	 */
 898	rose_stop_ftimer(rose_neigh);
 899
 900	/*
 901	 *	LCI of zero is always for us, and its always a restart
 902	 * 	frame.
 903	 */
 904	if (lci == 0) {
 905		rose_link_rx_restart(skb, rose_neigh, frametype);
 906		goto out;
 907	}
 908
 909	/*
 910	 *	Find an existing socket.
 911	 */
 912	if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
 913		if (frametype == ROSE_CALL_REQUEST) {
 914			struct rose_sock *rose = rose_sk(sk);
 915
 916			/* Remove an existing unused socket */
 917			rose_clear_queues(sk);
 918			rose->cause	 = ROSE_NETWORK_CONGESTION;
 919			rose->diagnostic = 0;
 920			rose->neighbour->use--;
 921			rose->neighbour	 = NULL;
 922			rose->lci	 = 0;
 923			rose->state	 = ROSE_STATE_0;
 924			sk->sk_state	 = TCP_CLOSE;
 925			sk->sk_err	 = 0;
 926			sk->sk_shutdown	 |= SEND_SHUTDOWN;
 927			if (!sock_flag(sk, SOCK_DEAD)) {
 928				sk->sk_state_change(sk);
 929				sock_set_flag(sk, SOCK_DEAD);
 930			}
 931		}
 932		else {
 933			skb_reset_transport_header(skb);
 934			res = rose_process_rx_frame(sk, skb);
 935			goto out;
 936		}
 937	}
 938
 939	/*
 940	 *	Is is a Call Request and is it for us ?
 941	 */
 942	if (frametype == ROSE_CALL_REQUEST)
 943		if ((dev = rose_dev_get(dest_addr)) != NULL) {
 944			res = rose_rx_call_request(skb, dev, rose_neigh, lci);
 945			dev_put(dev);
 946			goto out;
 947		}
 948
 949	if (!sysctl_rose_routing_control) {
 950		rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
 951		goto out;
 952	}
 953
 954	/*
 955	 *	Route it to the next in line if we have an entry for it.
 956	 */
 957	rose_route = rose_route_list;
 958	while (rose_route != NULL) {
 959		if (rose_route->lci1 == lci &&
 960		    rose_route->neigh1 == rose_neigh) {
 961			if (frametype == ROSE_CALL_REQUEST) {
 962				/* F6FBB - Remove an existing unused route */
 963				rose_remove_route(rose_route);
 964				break;
 965			} else if (rose_route->neigh2 != NULL) {
 966				skb->data[0] &= 0xF0;
 967				skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
 968				skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
 969				rose_transmit_link(skb, rose_route->neigh2);
 970				if (frametype == ROSE_CLEAR_CONFIRMATION)
 971					rose_remove_route(rose_route);
 972				res = 1;
 973				goto out;
 974			} else {
 975				if (frametype == ROSE_CLEAR_CONFIRMATION)
 976					rose_remove_route(rose_route);
 977				goto out;
 978			}
 979		}
 980		if (rose_route->lci2 == lci &&
 981		    rose_route->neigh2 == rose_neigh) {
 982			if (frametype == ROSE_CALL_REQUEST) {
 983				/* F6FBB - Remove an existing unused route */
 984				rose_remove_route(rose_route);
 985				break;
 986			} else if (rose_route->neigh1 != NULL) {
 987				skb->data[0] &= 0xF0;
 988				skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
 989				skb->data[1]  = (rose_route->lci1 >> 0) & 0xFF;
 990				rose_transmit_link(skb, rose_route->neigh1);
 991				if (frametype == ROSE_CLEAR_CONFIRMATION)
 992					rose_remove_route(rose_route);
 993				res = 1;
 994				goto out;
 995			} else {
 996				if (frametype == ROSE_CLEAR_CONFIRMATION)
 997					rose_remove_route(rose_route);
 998				goto out;
 999			}
1000		}
1001		rose_route = rose_route->next;
1002	}
1003
1004	/*
1005	 *	We know that:
1006	 *	1. The frame isn't for us,
1007	 *	2. It isn't "owned" by any existing route.
1008	 */
1009	if (frametype != ROSE_CALL_REQUEST) {	/* XXX */
1010		res = 0;
1011		goto out;
1012	}
1013
1014	memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1015
1016	if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
1017				   skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
1018				   &facilities)) {
1019		rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1020		goto out;
1021	}
1022
1023	/*
1024	 *	Check for routing loops.
1025	 */
1026	rose_route = rose_route_list;
1027	while (rose_route != NULL) {
1028		if (rose_route->rand == facilities.rand &&
1029		    rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1030		    ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1031		    ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1032			rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1033			goto out;
1034		}
1035		rose_route = rose_route->next;
1036	}
1037
1038	if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1039		rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1040		goto out;
1041	}
1042
1043	if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1044		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1045		goto out;
1046	}
1047
1048	if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1049		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1050		goto out;
1051	}
1052
1053	rose_route->lci1      = lci;
1054	rose_route->src_addr  = *src_addr;
1055	rose_route->dest_addr = *dest_addr;
1056	rose_route->src_call  = facilities.dest_call;
1057	rose_route->dest_call = facilities.source_call;
1058	rose_route->rand      = facilities.rand;
1059	rose_route->neigh1    = rose_neigh;
1060	rose_route->lci2      = new_lci;
1061	rose_route->neigh2    = new_neigh;
1062
1063	rose_route->neigh1->use++;
1064	rose_route->neigh2->use++;
1065
1066	rose_route->next = rose_route_list;
1067	rose_route_list  = rose_route;
1068
1069	skb->data[0] &= 0xF0;
1070	skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1071	skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
1072
1073	rose_transmit_link(skb, rose_route->neigh2);
1074	res = 1;
1075
1076out:
1077	spin_unlock_bh(&rose_route_list_lock);
1078	spin_unlock_bh(&rose_neigh_list_lock);
1079
1080	return res;
1081}
1082
1083#ifdef CONFIG_PROC_FS
1084
1085static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1086	__acquires(rose_node_list_lock)
1087{
1088	struct rose_node *rose_node;
1089	int i = 1;
1090
1091	spin_lock_bh(&rose_node_list_lock);
1092	if (*pos == 0)
1093		return SEQ_START_TOKEN;
1094
1095	for (rose_node = rose_node_list; rose_node && i < *pos;
1096	     rose_node = rose_node->next, ++i);
1097
1098	return (i == *pos) ? rose_node : NULL;
1099}
1100
1101static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1102{
1103	++*pos;
1104
1105	return (v == SEQ_START_TOKEN) ? rose_node_list
1106		: ((struct rose_node *)v)->next;
1107}
1108
1109static void rose_node_stop(struct seq_file *seq, void *v)
1110	__releases(rose_node_list_lock)
1111{
1112	spin_unlock_bh(&rose_node_list_lock);
1113}
1114
1115static int rose_node_show(struct seq_file *seq, void *v)
1116{
1117	char rsbuf[11];
1118	int i;
1119
1120	if (v == SEQ_START_TOKEN)
1121		seq_puts(seq, "address    mask n neigh neigh neigh\n");
1122	else {
1123		const struct rose_node *rose_node = v;
1124		/* if (rose_node->loopback) {
1125			seq_printf(seq, "%-10s %04d 1 loopback\n",
1126				   rose2asc(rsbuf, &rose_node->address),
1127				   rose_node->mask);
1128		} else { */
1129			seq_printf(seq, "%-10s %04d %d",
1130				   rose2asc(rsbuf, &rose_node->address),
1131				   rose_node->mask,
1132				   rose_node->count);
1133
1134			for (i = 0; i < rose_node->count; i++)
1135				seq_printf(seq, " %05d",
1136					rose_node->neighbour[i]->number);
1137
1138			seq_puts(seq, "\n");
1139		/* } */
1140	}
1141	return 0;
1142}
1143
1144static const struct seq_operations rose_node_seqops = {
1145	.start = rose_node_start,
1146	.next = rose_node_next,
1147	.stop = rose_node_stop,
1148	.show = rose_node_show,
1149};
1150
1151static int rose_nodes_open(struct inode *inode, struct file *file)
1152{
1153	return seq_open(file, &rose_node_seqops);
1154}
1155
1156const struct file_operations rose_nodes_fops = {
1157	.owner = THIS_MODULE,
1158	.open = rose_nodes_open,
1159	.read = seq_read,
1160	.llseek = seq_lseek,
1161	.release = seq_release,
1162};
1163
1164static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1165	__acquires(rose_neigh_list_lock)
1166{
1167	struct rose_neigh *rose_neigh;
1168	int i = 1;
1169
1170	spin_lock_bh(&rose_neigh_list_lock);
1171	if (*pos == 0)
1172		return SEQ_START_TOKEN;
1173
1174	for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1175	     rose_neigh = rose_neigh->next, ++i);
1176
1177	return (i == *pos) ? rose_neigh : NULL;
1178}
1179
1180static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1181{
1182	++*pos;
1183
1184	return (v == SEQ_START_TOKEN) ? rose_neigh_list
1185		: ((struct rose_neigh *)v)->next;
1186}
1187
1188static void rose_neigh_stop(struct seq_file *seq, void *v)
1189	__releases(rose_neigh_list_lock)
1190{
1191	spin_unlock_bh(&rose_neigh_list_lock);
1192}
1193
1194static int rose_neigh_show(struct seq_file *seq, void *v)
1195{
1196	char buf[11];
1197	int i;
1198
1199	if (v == SEQ_START_TOKEN)
1200		seq_puts(seq,
1201			 "addr  callsign  dev  count use mode restart  t0  tf digipeaters\n");
1202	else {
1203		struct rose_neigh *rose_neigh = v;
1204
1205		/* if (!rose_neigh->loopback) { */
1206		seq_printf(seq, "%05d %-9s %-4s   %3d %3d  %3s     %3s %3lu %3lu",
1207			   rose_neigh->number,
1208			   (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1209			   rose_neigh->dev ? rose_neigh->dev->name : "???",
1210			   rose_neigh->count,
1211			   rose_neigh->use,
1212			   (rose_neigh->dce_mode) ? "DCE" : "DTE",
1213			   (rose_neigh->restarted) ? "yes" : "no",
1214			   ax25_display_timer(&rose_neigh->t0timer) / HZ,
1215			   ax25_display_timer(&rose_neigh->ftimer)  / HZ);
1216
1217		if (rose_neigh->digipeat != NULL) {
1218			for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1219				seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1220		}
1221
1222		seq_puts(seq, "\n");
1223	}
1224	return 0;
1225}
1226
1227
1228static const struct seq_operations rose_neigh_seqops = {
1229	.start = rose_neigh_start,
1230	.next = rose_neigh_next,
1231	.stop = rose_neigh_stop,
1232	.show = rose_neigh_show,
1233};
1234
1235static int rose_neigh_open(struct inode *inode, struct file *file)
1236{
1237	return seq_open(file, &rose_neigh_seqops);
1238}
1239
1240const struct file_operations rose_neigh_fops = {
1241	.owner = THIS_MODULE,
1242	.open = rose_neigh_open,
1243	.read = seq_read,
1244	.llseek = seq_lseek,
1245	.release = seq_release,
1246};
1247
1248
1249static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1250	__acquires(rose_route_list_lock)
1251{
1252	struct rose_route *rose_route;
1253	int i = 1;
1254
1255	spin_lock_bh(&rose_route_list_lock);
1256	if (*pos == 0)
1257		return SEQ_START_TOKEN;
1258
1259	for (rose_route = rose_route_list; rose_route && i < *pos;
1260	     rose_route = rose_route->next, ++i);
1261
1262	return (i == *pos) ? rose_route : NULL;
1263}
1264
1265static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1266{
1267	++*pos;
1268
1269	return (v == SEQ_START_TOKEN) ? rose_route_list
1270		: ((struct rose_route *)v)->next;
1271}
1272
1273static void rose_route_stop(struct seq_file *seq, void *v)
1274	__releases(rose_route_list_lock)
1275{
1276	spin_unlock_bh(&rose_route_list_lock);
1277}
1278
1279static int rose_route_show(struct seq_file *seq, void *v)
1280{
1281	char buf[11], rsbuf[11];
1282
1283	if (v == SEQ_START_TOKEN)
1284		seq_puts(seq,
1285			 "lci  address     callsign   neigh  <-> lci  address     callsign   neigh\n");
1286	else {
1287		struct rose_route *rose_route = v;
1288
1289		if (rose_route->neigh1)
1290			seq_printf(seq,
1291				   "%3.3X  %-10s  %-9s  %05d      ",
1292				   rose_route->lci1,
1293				   rose2asc(rsbuf, &rose_route->src_addr),
1294				   ax2asc(buf, &rose_route->src_call),
1295				   rose_route->neigh1->number);
1296		else
1297			seq_puts(seq,
1298				 "000  *           *          00000      ");
1299
1300		if (rose_route->neigh2)
1301			seq_printf(seq,
1302				   "%3.3X  %-10s  %-9s  %05d\n",
1303				   rose_route->lci2,
1304				   rose2asc(rsbuf, &rose_route->dest_addr),
1305				   ax2asc(buf, &rose_route->dest_call),
1306				   rose_route->neigh2->number);
1307		 else
1308			 seq_puts(seq,
1309				  "000  *           *          00000\n");
1310		}
1311	return 0;
1312}
1313
1314static const struct seq_operations rose_route_seqops = {
1315	.start = rose_route_start,
1316	.next = rose_route_next,
1317	.stop = rose_route_stop,
1318	.show = rose_route_show,
1319};
1320
1321static int rose_route_open(struct inode *inode, struct file *file)
1322{
1323	return seq_open(file, &rose_route_seqops);
1324}
1325
1326const struct file_operations rose_routes_fops = {
1327	.owner = THIS_MODULE,
1328	.open = rose_route_open,
1329	.read = seq_read,
1330	.llseek = seq_lseek,
1331	.release = seq_release,
1332};
1333
1334#endif /* CONFIG_PROC_FS */
1335
1336/*
1337 *	Release all memory associated with ROSE routing structures.
1338 */
1339void __exit rose_rt_free(void)
1340{
1341	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1342	struct rose_node  *t, *rose_node  = rose_node_list;
1343	struct rose_route *u, *rose_route = rose_route_list;
1344
1345	while (rose_neigh != NULL) {
1346		s          = rose_neigh;
1347		rose_neigh = rose_neigh->next;
1348
1349		rose_remove_neigh(s);
1350	}
1351
1352	while (rose_node != NULL) {
1353		t         = rose_node;
1354		rose_node = rose_node->next;
1355
1356		rose_remove_node(t);
1357	}
1358
1359	while (rose_route != NULL) {
1360		u          = rose_route;
1361		rose_route = rose_route->next;
1362
1363		rose_remove_route(u);
1364	}
1365}
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
 
 
 
 
   3 *
   4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
   5 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
   6 */
   7#include <linux/errno.h>
   8#include <linux/types.h>
   9#include <linux/socket.h>
  10#include <linux/in.h>
  11#include <linux/kernel.h>
  12#include <linux/timer.h>
  13#include <linux/string.h>
  14#include <linux/sockios.h>
  15#include <linux/net.h>
  16#include <linux/slab.h>
  17#include <net/ax25.h>
  18#include <linux/inet.h>
  19#include <linux/netdevice.h>
  20#include <net/arp.h>
  21#include <linux/if_arp.h>
  22#include <linux/skbuff.h>
  23#include <net/sock.h>
  24#include <net/tcp_states.h>
  25#include <linux/uaccess.h>
  26#include <linux/fcntl.h>
  27#include <linux/termios.h>	/* For TIOCINQ/OUTQ */
  28#include <linux/mm.h>
  29#include <linux/interrupt.h>
  30#include <linux/notifier.h>
  31#include <linux/init.h>
  32#include <net/rose.h>
  33#include <linux/seq_file.h>
  34#include <linux/export.h>
  35
  36static unsigned int rose_neigh_no = 1;
  37
  38static struct rose_node  *rose_node_list;
  39static DEFINE_SPINLOCK(rose_node_list_lock);
  40static struct rose_neigh *rose_neigh_list;
  41static DEFINE_SPINLOCK(rose_neigh_list_lock);
  42static struct rose_route *rose_route_list;
  43static DEFINE_SPINLOCK(rose_route_list_lock);
  44
  45struct rose_neigh *rose_loopback_neigh;
  46
  47/*
  48 *	Add a new route to a node, and in the process add the node and the
  49 *	neighbour if it is new.
  50 */
  51static int __must_check rose_add_node(struct rose_route_struct *rose_route,
  52	struct net_device *dev)
  53{
  54	struct rose_node  *rose_node, *rose_tmpn, *rose_tmpp;
  55	struct rose_neigh *rose_neigh;
  56	int i, res = 0;
  57
  58	spin_lock_bh(&rose_node_list_lock);
  59	spin_lock_bh(&rose_neigh_list_lock);
  60
  61	rose_node = rose_node_list;
  62	while (rose_node != NULL) {
  63		if ((rose_node->mask == rose_route->mask) &&
  64		    (rosecmpm(&rose_route->address, &rose_node->address,
  65			      rose_route->mask) == 0))
  66			break;
  67		rose_node = rose_node->next;
  68	}
  69
  70	if (rose_node != NULL && rose_node->loopback) {
  71		res = -EINVAL;
  72		goto out;
  73	}
  74
  75	rose_neigh = rose_neigh_list;
  76	while (rose_neigh != NULL) {
  77		if (ax25cmp(&rose_route->neighbour,
  78			    &rose_neigh->callsign) == 0 &&
  79		    rose_neigh->dev == dev)
  80			break;
  81		rose_neigh = rose_neigh->next;
  82	}
  83
  84	if (rose_neigh == NULL) {
  85		rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC);
  86		if (rose_neigh == NULL) {
  87			res = -ENOMEM;
  88			goto out;
  89		}
  90
  91		rose_neigh->callsign  = rose_route->neighbour;
  92		rose_neigh->digipeat  = NULL;
  93		rose_neigh->ax25      = NULL;
  94		rose_neigh->dev       = dev;
  95		rose_neigh->count     = 0;
  96		rose_neigh->use       = 0;
  97		rose_neigh->dce_mode  = 0;
  98		rose_neigh->loopback  = 0;
  99		rose_neigh->number    = rose_neigh_no++;
 100		rose_neigh->restarted = 0;
 101
 102		skb_queue_head_init(&rose_neigh->queue);
 103
 104		timer_setup(&rose_neigh->ftimer, NULL, 0);
 105		timer_setup(&rose_neigh->t0timer, NULL, 0);
 106
 107		if (rose_route->ndigis != 0) {
 108			rose_neigh->digipeat =
 109				kmalloc(sizeof(ax25_digi), GFP_ATOMIC);
 110			if (rose_neigh->digipeat == NULL) {
 111				kfree(rose_neigh);
 112				res = -ENOMEM;
 113				goto out;
 114			}
 115
 116			rose_neigh->digipeat->ndigi      = rose_route->ndigis;
 117			rose_neigh->digipeat->lastrepeat = -1;
 118
 119			for (i = 0; i < rose_route->ndigis; i++) {
 120				rose_neigh->digipeat->calls[i]    =
 121					rose_route->digipeaters[i];
 122				rose_neigh->digipeat->repeated[i] = 0;
 123			}
 124		}
 125
 126		rose_neigh->next = rose_neigh_list;
 127		rose_neigh_list  = rose_neigh;
 128	}
 129
 130	/*
 131	 * This is a new node to be inserted into the list. Find where it needs
 132	 * to be inserted into the list, and insert it. We want to be sure
 133	 * to order the list in descending order of mask size to ensure that
 134	 * later when we are searching this list the first match will be the
 135	 * best match.
 136	 */
 137	if (rose_node == NULL) {
 138		rose_tmpn = rose_node_list;
 139		rose_tmpp = NULL;
 140
 141		while (rose_tmpn != NULL) {
 142			if (rose_tmpn->mask > rose_route->mask) {
 143				rose_tmpp = rose_tmpn;
 144				rose_tmpn = rose_tmpn->next;
 145			} else {
 146				break;
 147			}
 148		}
 149
 150		/* create new node */
 151		rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC);
 152		if (rose_node == NULL) {
 153			res = -ENOMEM;
 154			goto out;
 155		}
 156
 157		rose_node->address      = rose_route->address;
 158		rose_node->mask         = rose_route->mask;
 159		rose_node->count        = 1;
 160		rose_node->loopback     = 0;
 161		rose_node->neighbour[0] = rose_neigh;
 162
 163		if (rose_tmpn == NULL) {
 164			if (rose_tmpp == NULL) {	/* Empty list */
 165				rose_node_list  = rose_node;
 166				rose_node->next = NULL;
 167			} else {
 168				rose_tmpp->next = rose_node;
 169				rose_node->next = NULL;
 170			}
 171		} else {
 172			if (rose_tmpp == NULL) {	/* 1st node */
 173				rose_node->next = rose_node_list;
 174				rose_node_list  = rose_node;
 175			} else {
 176				rose_tmpp->next = rose_node;
 177				rose_node->next = rose_tmpn;
 178			}
 179		}
 180		rose_neigh->count++;
 181
 182		goto out;
 183	}
 184
 185	/* We have space, slot it in */
 186	if (rose_node->count < 3) {
 187		rose_node->neighbour[rose_node->count] = rose_neigh;
 188		rose_node->count++;
 189		rose_neigh->count++;
 190	}
 191
 192out:
 193	spin_unlock_bh(&rose_neigh_list_lock);
 194	spin_unlock_bh(&rose_node_list_lock);
 195
 196	return res;
 197}
 198
 199/*
 200 * Caller is holding rose_node_list_lock.
 201 */
 202static void rose_remove_node(struct rose_node *rose_node)
 203{
 204	struct rose_node *s;
 205
 206	if ((s = rose_node_list) == rose_node) {
 207		rose_node_list = rose_node->next;
 208		kfree(rose_node);
 209		return;
 210	}
 211
 212	while (s != NULL && s->next != NULL) {
 213		if (s->next == rose_node) {
 214			s->next = rose_node->next;
 215			kfree(rose_node);
 216			return;
 217		}
 218
 219		s = s->next;
 220	}
 221}
 222
 223/*
 224 * Caller is holding rose_neigh_list_lock.
 225 */
 226static void rose_remove_neigh(struct rose_neigh *rose_neigh)
 227{
 228	struct rose_neigh *s;
 229
 230	del_timer_sync(&rose_neigh->ftimer);
 231	del_timer_sync(&rose_neigh->t0timer);
 232
 233	skb_queue_purge(&rose_neigh->queue);
 234
 235	if ((s = rose_neigh_list) == rose_neigh) {
 236		rose_neigh_list = rose_neigh->next;
 237		if (rose_neigh->ax25)
 238			ax25_cb_put(rose_neigh->ax25);
 239		kfree(rose_neigh->digipeat);
 240		kfree(rose_neigh);
 241		return;
 242	}
 243
 244	while (s != NULL && s->next != NULL) {
 245		if (s->next == rose_neigh) {
 246			s->next = rose_neigh->next;
 247			if (rose_neigh->ax25)
 248				ax25_cb_put(rose_neigh->ax25);
 249			kfree(rose_neigh->digipeat);
 250			kfree(rose_neigh);
 251			return;
 252		}
 253
 254		s = s->next;
 255	}
 256}
 257
 258/*
 259 * Caller is holding rose_route_list_lock.
 260 */
 261static void rose_remove_route(struct rose_route *rose_route)
 262{
 263	struct rose_route *s;
 264
 265	if (rose_route->neigh1 != NULL)
 266		rose_route->neigh1->use--;
 267
 268	if (rose_route->neigh2 != NULL)
 269		rose_route->neigh2->use--;
 270
 271	if ((s = rose_route_list) == rose_route) {
 272		rose_route_list = rose_route->next;
 273		kfree(rose_route);
 274		return;
 275	}
 276
 277	while (s != NULL && s->next != NULL) {
 278		if (s->next == rose_route) {
 279			s->next = rose_route->next;
 280			kfree(rose_route);
 281			return;
 282		}
 283
 284		s = s->next;
 285	}
 286}
 287
 288/*
 289 *	"Delete" a node. Strictly speaking remove a route to a node. The node
 290 *	is only deleted if no routes are left to it.
 291 */
 292static int rose_del_node(struct rose_route_struct *rose_route,
 293	struct net_device *dev)
 294{
 295	struct rose_node  *rose_node;
 296	struct rose_neigh *rose_neigh;
 297	int i, err = 0;
 298
 299	spin_lock_bh(&rose_node_list_lock);
 300	spin_lock_bh(&rose_neigh_list_lock);
 301
 302	rose_node = rose_node_list;
 303	while (rose_node != NULL) {
 304		if ((rose_node->mask == rose_route->mask) &&
 305		    (rosecmpm(&rose_route->address, &rose_node->address,
 306			      rose_route->mask) == 0))
 307			break;
 308		rose_node = rose_node->next;
 309	}
 310
 311	if (rose_node == NULL || rose_node->loopback) {
 312		err = -EINVAL;
 313		goto out;
 314	}
 315
 316	rose_neigh = rose_neigh_list;
 317	while (rose_neigh != NULL) {
 318		if (ax25cmp(&rose_route->neighbour,
 319			    &rose_neigh->callsign) == 0 &&
 320		    rose_neigh->dev == dev)
 321			break;
 322		rose_neigh = rose_neigh->next;
 323	}
 324
 325	if (rose_neigh == NULL) {
 326		err = -EINVAL;
 327		goto out;
 328	}
 329
 330	for (i = 0; i < rose_node->count; i++) {
 331		if (rose_node->neighbour[i] == rose_neigh) {
 332			rose_neigh->count--;
 333
 334			if (rose_neigh->count == 0 && rose_neigh->use == 0)
 335				rose_remove_neigh(rose_neigh);
 336
 337			rose_node->count--;
 338
 339			if (rose_node->count == 0) {
 340				rose_remove_node(rose_node);
 341			} else {
 342				switch (i) {
 343				case 0:
 344					rose_node->neighbour[0] =
 345						rose_node->neighbour[1];
 346					fallthrough;
 347				case 1:
 348					rose_node->neighbour[1] =
 349						rose_node->neighbour[2];
 350					break;
 351				case 2:
 352					break;
 353				}
 354			}
 355			goto out;
 356		}
 357	}
 358	err = -EINVAL;
 359
 360out:
 361	spin_unlock_bh(&rose_neigh_list_lock);
 362	spin_unlock_bh(&rose_node_list_lock);
 363
 364	return err;
 365}
 366
 367/*
 368 *	Add the loopback neighbour.
 369 */
 370void rose_add_loopback_neigh(void)
 371{
 372	struct rose_neigh *sn;
 373
 374	rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
 375	if (!rose_loopback_neigh)
 376		return;
 377	sn = rose_loopback_neigh;
 378
 379	sn->callsign  = null_ax25_address;
 380	sn->digipeat  = NULL;
 381	sn->ax25      = NULL;
 382	sn->dev       = NULL;
 383	sn->count     = 0;
 384	sn->use       = 0;
 385	sn->dce_mode  = 1;
 386	sn->loopback  = 1;
 387	sn->number    = rose_neigh_no++;
 388	sn->restarted = 1;
 389
 390	skb_queue_head_init(&sn->queue);
 391
 392	timer_setup(&sn->ftimer, NULL, 0);
 393	timer_setup(&sn->t0timer, NULL, 0);
 394
 395	spin_lock_bh(&rose_neigh_list_lock);
 396	sn->next = rose_neigh_list;
 397	rose_neigh_list           = sn;
 398	spin_unlock_bh(&rose_neigh_list_lock);
 399}
 400
 401/*
 402 *	Add a loopback node.
 403 */
 404int rose_add_loopback_node(const rose_address *address)
 405{
 406	struct rose_node *rose_node;
 407	int err = 0;
 408
 409	spin_lock_bh(&rose_node_list_lock);
 410
 411	rose_node = rose_node_list;
 412	while (rose_node != NULL) {
 413		if ((rose_node->mask == 10) &&
 414		     (rosecmpm(address, &rose_node->address, 10) == 0) &&
 415		     rose_node->loopback)
 416			break;
 417		rose_node = rose_node->next;
 418	}
 419
 420	if (rose_node != NULL)
 421		goto out;
 422
 423	if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
 424		err = -ENOMEM;
 425		goto out;
 426	}
 427
 428	rose_node->address      = *address;
 429	rose_node->mask         = 10;
 430	rose_node->count        = 1;
 431	rose_node->loopback     = 1;
 432	rose_node->neighbour[0] = rose_loopback_neigh;
 433
 434	/* Insert at the head of list. Address is always mask=10 */
 435	rose_node->next = rose_node_list;
 436	rose_node_list  = rose_node;
 437
 438	rose_loopback_neigh->count++;
 439
 440out:
 441	spin_unlock_bh(&rose_node_list_lock);
 442
 443	return err;
 444}
 445
 446/*
 447 *	Delete a loopback node.
 448 */
 449void rose_del_loopback_node(const rose_address *address)
 450{
 451	struct rose_node *rose_node;
 452
 453	spin_lock_bh(&rose_node_list_lock);
 454
 455	rose_node = rose_node_list;
 456	while (rose_node != NULL) {
 457		if ((rose_node->mask == 10) &&
 458		    (rosecmpm(address, &rose_node->address, 10) == 0) &&
 459		    rose_node->loopback)
 460			break;
 461		rose_node = rose_node->next;
 462	}
 463
 464	if (rose_node == NULL)
 465		goto out;
 466
 467	rose_remove_node(rose_node);
 468
 469	rose_loopback_neigh->count--;
 470
 471out:
 472	spin_unlock_bh(&rose_node_list_lock);
 473}
 474
 475/*
 476 *	A device has been removed. Remove its routes and neighbours.
 477 */
 478void rose_rt_device_down(struct net_device *dev)
 479{
 480	struct rose_neigh *s, *rose_neigh;
 481	struct rose_node  *t, *rose_node;
 482	int i;
 483
 484	spin_lock_bh(&rose_node_list_lock);
 485	spin_lock_bh(&rose_neigh_list_lock);
 486	rose_neigh = rose_neigh_list;
 487	while (rose_neigh != NULL) {
 488		s          = rose_neigh;
 489		rose_neigh = rose_neigh->next;
 490
 491		if (s->dev != dev)
 492			continue;
 493
 494		rose_node = rose_node_list;
 495
 496		while (rose_node != NULL) {
 497			t         = rose_node;
 498			rose_node = rose_node->next;
 499
 500			for (i = 0; i < t->count; i++) {
 501				if (t->neighbour[i] != s)
 502					continue;
 503
 504				t->count--;
 505
 506				switch (i) {
 507				case 0:
 508					t->neighbour[0] = t->neighbour[1];
 509					fallthrough;
 510				case 1:
 511					t->neighbour[1] = t->neighbour[2];
 512					break;
 513				case 2:
 514					break;
 515				}
 516			}
 517
 518			if (t->count <= 0)
 519				rose_remove_node(t);
 520		}
 521
 522		rose_remove_neigh(s);
 523	}
 524	spin_unlock_bh(&rose_neigh_list_lock);
 525	spin_unlock_bh(&rose_node_list_lock);
 526}
 527
 528#if 0 /* Currently unused */
 529/*
 530 *	A device has been removed. Remove its links.
 531 */
 532void rose_route_device_down(struct net_device *dev)
 533{
 534	struct rose_route *s, *rose_route;
 535
 536	spin_lock_bh(&rose_route_list_lock);
 537	rose_route = rose_route_list;
 538	while (rose_route != NULL) {
 539		s          = rose_route;
 540		rose_route = rose_route->next;
 541
 542		if (s->neigh1->dev == dev || s->neigh2->dev == dev)
 543			rose_remove_route(s);
 544	}
 545	spin_unlock_bh(&rose_route_list_lock);
 546}
 547#endif
 548
 549/*
 550 *	Clear all nodes and neighbours out, except for neighbours with
 551 *	active connections going through them.
 552 *  Do not clear loopback neighbour and nodes.
 553 */
 554static int rose_clear_routes(void)
 555{
 556	struct rose_neigh *s, *rose_neigh;
 557	struct rose_node  *t, *rose_node;
 558
 559	spin_lock_bh(&rose_node_list_lock);
 560	spin_lock_bh(&rose_neigh_list_lock);
 561
 562	rose_neigh = rose_neigh_list;
 563	rose_node  = rose_node_list;
 564
 565	while (rose_node != NULL) {
 566		t         = rose_node;
 567		rose_node = rose_node->next;
 568		if (!t->loopback)
 569			rose_remove_node(t);
 570	}
 571
 572	while (rose_neigh != NULL) {
 573		s          = rose_neigh;
 574		rose_neigh = rose_neigh->next;
 575
 576		if (s->use == 0 && !s->loopback) {
 577			s->count = 0;
 578			rose_remove_neigh(s);
 579		}
 580	}
 581
 582	spin_unlock_bh(&rose_neigh_list_lock);
 583	spin_unlock_bh(&rose_node_list_lock);
 584
 585	return 0;
 586}
 587
 588/*
 589 *	Check that the device given is a valid AX.25 interface that is "up".
 590 * 	called with RTNL
 591 */
 592static struct net_device *rose_ax25_dev_find(char *devname)
 593{
 594	struct net_device *dev;
 595
 596	if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
 597		return NULL;
 598
 599	if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
 600		return dev;
 601
 602	return NULL;
 603}
 604
 605/*
 606 *	Find the first active ROSE device, usually "rose0".
 607 */
 608struct net_device *rose_dev_first(void)
 609{
 610	struct net_device *dev, *first = NULL;
 611
 612	rcu_read_lock();
 613	for_each_netdev_rcu(&init_net, dev) {
 614		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
 615			if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
 616				first = dev;
 617	}
 618	if (first)
 619		dev_hold(first);
 620	rcu_read_unlock();
 621
 622	return first;
 623}
 624
 625/*
 626 *	Find the ROSE device for the given address.
 627 */
 628struct net_device *rose_dev_get(rose_address *addr)
 629{
 630	struct net_device *dev;
 631
 632	rcu_read_lock();
 633	for_each_netdev_rcu(&init_net, dev) {
 634		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE &&
 635		    rosecmp(addr, (const rose_address *)dev->dev_addr) == 0) {
 636			dev_hold(dev);
 637			goto out;
 638		}
 639	}
 640	dev = NULL;
 641out:
 642	rcu_read_unlock();
 643	return dev;
 644}
 645
 646static int rose_dev_exists(rose_address *addr)
 647{
 648	struct net_device *dev;
 649
 650	rcu_read_lock();
 651	for_each_netdev_rcu(&init_net, dev) {
 652		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE &&
 653		    rosecmp(addr, (const rose_address *)dev->dev_addr) == 0)
 654			goto out;
 655	}
 656	dev = NULL;
 657out:
 658	rcu_read_unlock();
 659	return dev != NULL;
 660}
 661
 662
 663
 664
 665struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
 666{
 667	struct rose_route *rose_route;
 668
 669	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
 670		if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
 671		    (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
 672			return rose_route;
 673
 674	return NULL;
 675}
 676
 677/*
 678 *	Find a neighbour or a route given a ROSE address.
 679 */
 680struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
 681	unsigned char *diagnostic, int route_frame)
 682{
 683	struct rose_neigh *res = NULL;
 684	struct rose_node *node;
 685	int failed = 0;
 686	int i;
 687
 688	if (!route_frame) spin_lock_bh(&rose_node_list_lock);
 689	for (node = rose_node_list; node != NULL; node = node->next) {
 690		if (rosecmpm(addr, &node->address, node->mask) == 0) {
 691			for (i = 0; i < node->count; i++) {
 692				if (node->neighbour[i]->restarted) {
 693					res = node->neighbour[i];
 694					goto out;
 695				}
 696			}
 697		}
 698	}
 699	if (!route_frame) { /* connect request */
 700		for (node = rose_node_list; node != NULL; node = node->next) {
 701			if (rosecmpm(addr, &node->address, node->mask) == 0) {
 702				for (i = 0; i < node->count; i++) {
 703					if (!rose_ftimer_running(node->neighbour[i])) {
 704						res = node->neighbour[i];
 
 705						goto out;
 706					}
 707					failed = 1;
 708				}
 709			}
 710		}
 711	}
 712
 713	if (failed) {
 714		*cause      = ROSE_OUT_OF_ORDER;
 715		*diagnostic = 0;
 716	} else {
 717		*cause      = ROSE_NOT_OBTAINABLE;
 718		*diagnostic = 0;
 719	}
 720
 721out:
 722	if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
 723	return res;
 724}
 725
 726/*
 727 *	Handle the ioctls that control the routing functions.
 728 */
 729int rose_rt_ioctl(unsigned int cmd, void __user *arg)
 730{
 731	struct rose_route_struct rose_route;
 732	struct net_device *dev;
 733	int err;
 734
 735	switch (cmd) {
 736	case SIOCADDRT:
 737		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
 738			return -EFAULT;
 739		if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
 740			return -EINVAL;
 741		if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
 742			return -EINVAL;
 743		if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
 744			return -EINVAL;
 745		if (rose_route.ndigis > AX25_MAX_DIGIS)
 746			return -EINVAL;
 747		err = rose_add_node(&rose_route, dev);
 748		return err;
 749
 750	case SIOCDELRT:
 751		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
 752			return -EFAULT;
 753		if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
 754			return -EINVAL;
 755		err = rose_del_node(&rose_route, dev);
 756		return err;
 757
 758	case SIOCRSCLRRT:
 759		return rose_clear_routes();
 760
 761	default:
 762		return -EINVAL;
 763	}
 764
 765	return 0;
 766}
 767
 768static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
 769{
 770	struct rose_route *rose_route, *s;
 771
 772	rose_neigh->restarted = 0;
 773
 774	rose_stop_t0timer(rose_neigh);
 775	rose_start_ftimer(rose_neigh);
 776
 777	skb_queue_purge(&rose_neigh->queue);
 778
 779	spin_lock_bh(&rose_route_list_lock);
 780
 781	rose_route = rose_route_list;
 782
 783	while (rose_route != NULL) {
 784		if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
 785		    (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL)       ||
 786		    (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
 787			s = rose_route->next;
 788			rose_remove_route(rose_route);
 789			rose_route = s;
 790			continue;
 791		}
 792
 793		if (rose_route->neigh1 == rose_neigh) {
 794			rose_route->neigh1->use--;
 795			rose_route->neigh1 = NULL;
 796			rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
 797		}
 798
 799		if (rose_route->neigh2 == rose_neigh) {
 800			rose_route->neigh2->use--;
 801			rose_route->neigh2 = NULL;
 802			rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
 803		}
 804
 805		rose_route = rose_route->next;
 806	}
 807	spin_unlock_bh(&rose_route_list_lock);
 808}
 809
 810/*
 811 * 	A level 2 link has timed out, therefore it appears to be a poor link,
 812 *	then don't use that neighbour until it is reset. Blow away all through
 813 *	routes and connections using this route.
 814 */
 815void rose_link_failed(ax25_cb *ax25, int reason)
 816{
 817	struct rose_neigh *rose_neigh;
 818
 819	spin_lock_bh(&rose_neigh_list_lock);
 820	rose_neigh = rose_neigh_list;
 821	while (rose_neigh != NULL) {
 822		if (rose_neigh->ax25 == ax25)
 823			break;
 824		rose_neigh = rose_neigh->next;
 825	}
 826
 827	if (rose_neigh != NULL) {
 828		rose_neigh->ax25 = NULL;
 829		ax25_cb_put(ax25);
 830
 831		rose_del_route_by_neigh(rose_neigh);
 832		rose_kill_by_neigh(rose_neigh);
 833	}
 834	spin_unlock_bh(&rose_neigh_list_lock);
 835}
 836
 837/*
 838 * 	A device has been "downed" remove its link status. Blow away all
 839 *	through routes and connections that use this device.
 840 */
 841void rose_link_device_down(struct net_device *dev)
 842{
 843	struct rose_neigh *rose_neigh;
 844
 845	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
 846		if (rose_neigh->dev == dev) {
 847			rose_del_route_by_neigh(rose_neigh);
 848			rose_kill_by_neigh(rose_neigh);
 849		}
 850	}
 851}
 852
 853/*
 854 *	Route a frame to an appropriate AX.25 connection.
 855 *	A NULL ax25_cb indicates an internally generated frame.
 856 */
 857int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
 858{
 859	struct rose_neigh *rose_neigh, *new_neigh;
 860	struct rose_route *rose_route;
 861	struct rose_facilities_struct facilities;
 862	rose_address *src_addr, *dest_addr;
 863	struct sock *sk;
 864	unsigned short frametype;
 865	unsigned int lci, new_lci;
 866	unsigned char cause, diagnostic;
 867	struct net_device *dev;
 868	int res = 0;
 869	char buf[11];
 870
 871	if (skb->len < ROSE_MIN_LEN)
 872		return res;
 873
 874	if (!ax25)
 875		return rose_loopback_queue(skb, NULL);
 876
 877	frametype = skb->data[2];
 878	lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
 879	if (frametype == ROSE_CALL_REQUEST &&
 880	    (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
 881	     skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
 882	     ROSE_CALL_REQ_ADDR_LEN_VAL))
 883		return res;
 884	src_addr  = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
 885	dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
 886
 887	spin_lock_bh(&rose_neigh_list_lock);
 888	spin_lock_bh(&rose_route_list_lock);
 889
 890	rose_neigh = rose_neigh_list;
 891	while (rose_neigh != NULL) {
 892		if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
 893		    ax25->ax25_dev->dev == rose_neigh->dev)
 894			break;
 895		rose_neigh = rose_neigh->next;
 896	}
 897
 898	if (rose_neigh == NULL) {
 899		printk("rose_route : unknown neighbour or device %s\n",
 900		       ax2asc(buf, &ax25->dest_addr));
 901		goto out;
 902	}
 903
 904	/*
 905	 *	Obviously the link is working, halt the ftimer.
 906	 */
 907	rose_stop_ftimer(rose_neigh);
 908
 909	/*
 910	 *	LCI of zero is always for us, and its always a restart
 911	 * 	frame.
 912	 */
 913	if (lci == 0) {
 914		rose_link_rx_restart(skb, rose_neigh, frametype);
 915		goto out;
 916	}
 917
 918	/*
 919	 *	Find an existing socket.
 920	 */
 921	if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
 922		if (frametype == ROSE_CALL_REQUEST) {
 923			struct rose_sock *rose = rose_sk(sk);
 924
 925			/* Remove an existing unused socket */
 926			rose_clear_queues(sk);
 927			rose->cause	 = ROSE_NETWORK_CONGESTION;
 928			rose->diagnostic = 0;
 929			rose->neighbour->use--;
 930			rose->neighbour	 = NULL;
 931			rose->lci	 = 0;
 932			rose->state	 = ROSE_STATE_0;
 933			sk->sk_state	 = TCP_CLOSE;
 934			sk->sk_err	 = 0;
 935			sk->sk_shutdown	 |= SEND_SHUTDOWN;
 936			if (!sock_flag(sk, SOCK_DEAD)) {
 937				sk->sk_state_change(sk);
 938				sock_set_flag(sk, SOCK_DEAD);
 939			}
 940		}
 941		else {
 942			skb_reset_transport_header(skb);
 943			res = rose_process_rx_frame(sk, skb);
 944			goto out;
 945		}
 946	}
 947
 948	/*
 949	 *	Is is a Call Request and is it for us ?
 950	 */
 951	if (frametype == ROSE_CALL_REQUEST)
 952		if ((dev = rose_dev_get(dest_addr)) != NULL) {
 953			res = rose_rx_call_request(skb, dev, rose_neigh, lci);
 954			dev_put(dev);
 955			goto out;
 956		}
 957
 958	if (!sysctl_rose_routing_control) {
 959		rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
 960		goto out;
 961	}
 962
 963	/*
 964	 *	Route it to the next in line if we have an entry for it.
 965	 */
 966	rose_route = rose_route_list;
 967	while (rose_route != NULL) {
 968		if (rose_route->lci1 == lci &&
 969		    rose_route->neigh1 == rose_neigh) {
 970			if (frametype == ROSE_CALL_REQUEST) {
 971				/* F6FBB - Remove an existing unused route */
 972				rose_remove_route(rose_route);
 973				break;
 974			} else if (rose_route->neigh2 != NULL) {
 975				skb->data[0] &= 0xF0;
 976				skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
 977				skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
 978				rose_transmit_link(skb, rose_route->neigh2);
 979				if (frametype == ROSE_CLEAR_CONFIRMATION)
 980					rose_remove_route(rose_route);
 981				res = 1;
 982				goto out;
 983			} else {
 984				if (frametype == ROSE_CLEAR_CONFIRMATION)
 985					rose_remove_route(rose_route);
 986				goto out;
 987			}
 988		}
 989		if (rose_route->lci2 == lci &&
 990		    rose_route->neigh2 == rose_neigh) {
 991			if (frametype == ROSE_CALL_REQUEST) {
 992				/* F6FBB - Remove an existing unused route */
 993				rose_remove_route(rose_route);
 994				break;
 995			} else if (rose_route->neigh1 != NULL) {
 996				skb->data[0] &= 0xF0;
 997				skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
 998				skb->data[1]  = (rose_route->lci1 >> 0) & 0xFF;
 999				rose_transmit_link(skb, rose_route->neigh1);
1000				if (frametype == ROSE_CLEAR_CONFIRMATION)
1001					rose_remove_route(rose_route);
1002				res = 1;
1003				goto out;
1004			} else {
1005				if (frametype == ROSE_CLEAR_CONFIRMATION)
1006					rose_remove_route(rose_route);
1007				goto out;
1008			}
1009		}
1010		rose_route = rose_route->next;
1011	}
1012
1013	/*
1014	 *	We know that:
1015	 *	1. The frame isn't for us,
1016	 *	2. It isn't "owned" by any existing route.
1017	 */
1018	if (frametype != ROSE_CALL_REQUEST) {	/* XXX */
1019		res = 0;
1020		goto out;
1021	}
1022
1023	memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1024
1025	if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
1026				   skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
1027				   &facilities)) {
1028		rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1029		goto out;
1030	}
1031
1032	/*
1033	 *	Check for routing loops.
1034	 */
1035	rose_route = rose_route_list;
1036	while (rose_route != NULL) {
1037		if (rose_route->rand == facilities.rand &&
1038		    rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1039		    ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1040		    ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1041			rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1042			goto out;
1043		}
1044		rose_route = rose_route->next;
1045	}
1046
1047	if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1048		rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1049		goto out;
1050	}
1051
1052	if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1053		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1054		goto out;
1055	}
1056
1057	if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1058		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1059		goto out;
1060	}
1061
1062	rose_route->lci1      = lci;
1063	rose_route->src_addr  = *src_addr;
1064	rose_route->dest_addr = *dest_addr;
1065	rose_route->src_call  = facilities.dest_call;
1066	rose_route->dest_call = facilities.source_call;
1067	rose_route->rand      = facilities.rand;
1068	rose_route->neigh1    = rose_neigh;
1069	rose_route->lci2      = new_lci;
1070	rose_route->neigh2    = new_neigh;
1071
1072	rose_route->neigh1->use++;
1073	rose_route->neigh2->use++;
1074
1075	rose_route->next = rose_route_list;
1076	rose_route_list  = rose_route;
1077
1078	skb->data[0] &= 0xF0;
1079	skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1080	skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
1081
1082	rose_transmit_link(skb, rose_route->neigh2);
1083	res = 1;
1084
1085out:
1086	spin_unlock_bh(&rose_route_list_lock);
1087	spin_unlock_bh(&rose_neigh_list_lock);
1088
1089	return res;
1090}
1091
1092#ifdef CONFIG_PROC_FS
1093
1094static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1095	__acquires(rose_node_list_lock)
1096{
1097	struct rose_node *rose_node;
1098	int i = 1;
1099
1100	spin_lock_bh(&rose_node_list_lock);
1101	if (*pos == 0)
1102		return SEQ_START_TOKEN;
1103
1104	for (rose_node = rose_node_list; rose_node && i < *pos;
1105	     rose_node = rose_node->next, ++i);
1106
1107	return (i == *pos) ? rose_node : NULL;
1108}
1109
1110static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1111{
1112	++*pos;
1113
1114	return (v == SEQ_START_TOKEN) ? rose_node_list
1115		: ((struct rose_node *)v)->next;
1116}
1117
1118static void rose_node_stop(struct seq_file *seq, void *v)
1119	__releases(rose_node_list_lock)
1120{
1121	spin_unlock_bh(&rose_node_list_lock);
1122}
1123
1124static int rose_node_show(struct seq_file *seq, void *v)
1125{
1126	char rsbuf[11];
1127	int i;
1128
1129	if (v == SEQ_START_TOKEN)
1130		seq_puts(seq, "address    mask n neigh neigh neigh\n");
1131	else {
1132		const struct rose_node *rose_node = v;
1133		seq_printf(seq, "%-10s %04d %d",
1134			   rose2asc(rsbuf, &rose_node->address),
1135			   rose_node->mask,
1136			   rose_node->count);
1137
1138		for (i = 0; i < rose_node->count; i++)
1139			seq_printf(seq, " %05d", rose_node->neighbour[i]->number);
 
 
 
 
 
 
1140
1141		seq_puts(seq, "\n");
 
1142	}
1143	return 0;
1144}
1145
1146const struct seq_operations rose_node_seqops = {
1147	.start = rose_node_start,
1148	.next = rose_node_next,
1149	.stop = rose_node_stop,
1150	.show = rose_node_show,
1151};
1152
 
 
 
 
 
 
 
 
 
 
 
 
 
1153static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1154	__acquires(rose_neigh_list_lock)
1155{
1156	struct rose_neigh *rose_neigh;
1157	int i = 1;
1158
1159	spin_lock_bh(&rose_neigh_list_lock);
1160	if (*pos == 0)
1161		return SEQ_START_TOKEN;
1162
1163	for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1164	     rose_neigh = rose_neigh->next, ++i);
1165
1166	return (i == *pos) ? rose_neigh : NULL;
1167}
1168
1169static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1170{
1171	++*pos;
1172
1173	return (v == SEQ_START_TOKEN) ? rose_neigh_list
1174		: ((struct rose_neigh *)v)->next;
1175}
1176
1177static void rose_neigh_stop(struct seq_file *seq, void *v)
1178	__releases(rose_neigh_list_lock)
1179{
1180	spin_unlock_bh(&rose_neigh_list_lock);
1181}
1182
1183static int rose_neigh_show(struct seq_file *seq, void *v)
1184{
1185	char buf[11];
1186	int i;
1187
1188	if (v == SEQ_START_TOKEN)
1189		seq_puts(seq,
1190			 "addr  callsign  dev  count use mode restart  t0  tf digipeaters\n");
1191	else {
1192		struct rose_neigh *rose_neigh = v;
1193
1194		/* if (!rose_neigh->loopback) { */
1195		seq_printf(seq, "%05d %-9s %-4s   %3d %3d  %3s     %3s %3lu %3lu",
1196			   rose_neigh->number,
1197			   (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1198			   rose_neigh->dev ? rose_neigh->dev->name : "???",
1199			   rose_neigh->count,
1200			   rose_neigh->use,
1201			   (rose_neigh->dce_mode) ? "DCE" : "DTE",
1202			   (rose_neigh->restarted) ? "yes" : "no",
1203			   ax25_display_timer(&rose_neigh->t0timer) / HZ,
1204			   ax25_display_timer(&rose_neigh->ftimer)  / HZ);
1205
1206		if (rose_neigh->digipeat != NULL) {
1207			for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1208				seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1209		}
1210
1211		seq_puts(seq, "\n");
1212	}
1213	return 0;
1214}
1215
1216
1217const struct seq_operations rose_neigh_seqops = {
1218	.start = rose_neigh_start,
1219	.next = rose_neigh_next,
1220	.stop = rose_neigh_stop,
1221	.show = rose_neigh_show,
1222};
1223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1224static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1225	__acquires(rose_route_list_lock)
1226{
1227	struct rose_route *rose_route;
1228	int i = 1;
1229
1230	spin_lock_bh(&rose_route_list_lock);
1231	if (*pos == 0)
1232		return SEQ_START_TOKEN;
1233
1234	for (rose_route = rose_route_list; rose_route && i < *pos;
1235	     rose_route = rose_route->next, ++i);
1236
1237	return (i == *pos) ? rose_route : NULL;
1238}
1239
1240static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1241{
1242	++*pos;
1243
1244	return (v == SEQ_START_TOKEN) ? rose_route_list
1245		: ((struct rose_route *)v)->next;
1246}
1247
1248static void rose_route_stop(struct seq_file *seq, void *v)
1249	__releases(rose_route_list_lock)
1250{
1251	spin_unlock_bh(&rose_route_list_lock);
1252}
1253
1254static int rose_route_show(struct seq_file *seq, void *v)
1255{
1256	char buf[11], rsbuf[11];
1257
1258	if (v == SEQ_START_TOKEN)
1259		seq_puts(seq,
1260			 "lci  address     callsign   neigh  <-> lci  address     callsign   neigh\n");
1261	else {
1262		struct rose_route *rose_route = v;
1263
1264		if (rose_route->neigh1)
1265			seq_printf(seq,
1266				   "%3.3X  %-10s  %-9s  %05d      ",
1267				   rose_route->lci1,
1268				   rose2asc(rsbuf, &rose_route->src_addr),
1269				   ax2asc(buf, &rose_route->src_call),
1270				   rose_route->neigh1->number);
1271		else
1272			seq_puts(seq,
1273				 "000  *           *          00000      ");
1274
1275		if (rose_route->neigh2)
1276			seq_printf(seq,
1277				   "%3.3X  %-10s  %-9s  %05d\n",
1278				   rose_route->lci2,
1279				   rose2asc(rsbuf, &rose_route->dest_addr),
1280				   ax2asc(buf, &rose_route->dest_call),
1281				   rose_route->neigh2->number);
1282		 else
1283			 seq_puts(seq,
1284				  "000  *           *          00000\n");
1285		}
1286	return 0;
1287}
1288
1289struct seq_operations rose_route_seqops = {
1290	.start = rose_route_start,
1291	.next = rose_route_next,
1292	.stop = rose_route_stop,
1293	.show = rose_route_show,
1294};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1295#endif /* CONFIG_PROC_FS */
1296
1297/*
1298 *	Release all memory associated with ROSE routing structures.
1299 */
1300void __exit rose_rt_free(void)
1301{
1302	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1303	struct rose_node  *t, *rose_node  = rose_node_list;
1304	struct rose_route *u, *rose_route = rose_route_list;
1305
1306	while (rose_neigh != NULL) {
1307		s          = rose_neigh;
1308		rose_neigh = rose_neigh->next;
1309
1310		rose_remove_neigh(s);
1311	}
1312
1313	while (rose_node != NULL) {
1314		t         = rose_node;
1315		rose_node = rose_node->next;
1316
1317		rose_remove_node(t);
1318	}
1319
1320	while (rose_route != NULL) {
1321		u          = rose_route;
1322		rose_route = rose_route->next;
1323
1324		rose_remove_route(u);
1325	}
1326}