xref: /openbmc/linux/net/rose/rose_route.c (revision d003d772)
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 <linux/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 
39 static unsigned int rose_neigh_no = 1;
40 
41 static struct rose_node  *rose_node_list;
42 static DEFINE_SPINLOCK(rose_node_list_lock);
43 static struct rose_neigh *rose_neigh_list;
44 static DEFINE_SPINLOCK(rose_neigh_list_lock);
45 static struct rose_route *rose_route_list;
46 static DEFINE_SPINLOCK(rose_route_list_lock);
47 
48 struct 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  */
54 static 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 		timer_setup(&rose_neigh->ftimer, NULL, 0);
108 		timer_setup(&rose_neigh->t0timer, NULL, 0);
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 
195 out:
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  */
205 static 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  */
229 static 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  */
264 static 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  */
295 static 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 					/* fall through */
350 				case 1:
351 					rose_node->neighbour[1] =
352 						rose_node->neighbour[2];
353 				case 2:
354 					break;
355 				}
356 			}
357 			goto out;
358 		}
359 	}
360 	err = -EINVAL;
361 
362 out:
363 	spin_unlock_bh(&rose_neigh_list_lock);
364 	spin_unlock_bh(&rose_node_list_lock);
365 
366 	return err;
367 }
368 
369 /*
370  *	Add the loopback neighbour.
371  */
372 void rose_add_loopback_neigh(void)
373 {
374 	struct rose_neigh *sn;
375 
376 	rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_KERNEL);
377 	if (!rose_loopback_neigh)
378 		return;
379 	sn = rose_loopback_neigh;
380 
381 	sn->callsign  = null_ax25_address;
382 	sn->digipeat  = NULL;
383 	sn->ax25      = NULL;
384 	sn->dev       = NULL;
385 	sn->count     = 0;
386 	sn->use       = 0;
387 	sn->dce_mode  = 1;
388 	sn->loopback  = 1;
389 	sn->number    = rose_neigh_no++;
390 	sn->restarted = 1;
391 
392 	skb_queue_head_init(&sn->queue);
393 
394 	timer_setup(&sn->ftimer, NULL, 0);
395 	timer_setup(&sn->t0timer, NULL, 0);
396 
397 	spin_lock_bh(&rose_neigh_list_lock);
398 	sn->next = rose_neigh_list;
399 	rose_neigh_list           = sn;
400 	spin_unlock_bh(&rose_neigh_list_lock);
401 }
402 
403 /*
404  *	Add a loopback node.
405  */
406 int rose_add_loopback_node(rose_address *address)
407 {
408 	struct rose_node *rose_node;
409 	int err = 0;
410 
411 	spin_lock_bh(&rose_node_list_lock);
412 
413 	rose_node = rose_node_list;
414 	while (rose_node != NULL) {
415 		if ((rose_node->mask == 10) &&
416 		     (rosecmpm(address, &rose_node->address, 10) == 0) &&
417 		     rose_node->loopback)
418 			break;
419 		rose_node = rose_node->next;
420 	}
421 
422 	if (rose_node != NULL)
423 		goto out;
424 
425 	if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL) {
426 		err = -ENOMEM;
427 		goto out;
428 	}
429 
430 	rose_node->address      = *address;
431 	rose_node->mask         = 10;
432 	rose_node->count        = 1;
433 	rose_node->loopback     = 1;
434 	rose_node->neighbour[0] = rose_loopback_neigh;
435 
436 	/* Insert at the head of list. Address is always mask=10 */
437 	rose_node->next = rose_node_list;
438 	rose_node_list  = rose_node;
439 
440 	rose_loopback_neigh->count++;
441 
442 out:
443 	spin_unlock_bh(&rose_node_list_lock);
444 
445 	return err;
446 }
447 
448 /*
449  *	Delete a loopback node.
450  */
451 void rose_del_loopback_node(rose_address *address)
452 {
453 	struct rose_node *rose_node;
454 
455 	spin_lock_bh(&rose_node_list_lock);
456 
457 	rose_node = rose_node_list;
458 	while (rose_node != NULL) {
459 		if ((rose_node->mask == 10) &&
460 		    (rosecmpm(address, &rose_node->address, 10) == 0) &&
461 		    rose_node->loopback)
462 			break;
463 		rose_node = rose_node->next;
464 	}
465 
466 	if (rose_node == NULL)
467 		goto out;
468 
469 	rose_remove_node(rose_node);
470 
471 	rose_loopback_neigh->count--;
472 
473 out:
474 	spin_unlock_bh(&rose_node_list_lock);
475 }
476 
477 /*
478  *	A device has been removed. Remove its routes and neighbours.
479  */
480 void rose_rt_device_down(struct net_device *dev)
481 {
482 	struct rose_neigh *s, *rose_neigh;
483 	struct rose_node  *t, *rose_node;
484 	int i;
485 
486 	spin_lock_bh(&rose_node_list_lock);
487 	spin_lock_bh(&rose_neigh_list_lock);
488 	rose_neigh = rose_neigh_list;
489 	while (rose_neigh != NULL) {
490 		s          = rose_neigh;
491 		rose_neigh = rose_neigh->next;
492 
493 		if (s->dev != dev)
494 			continue;
495 
496 		rose_node = rose_node_list;
497 
498 		while (rose_node != NULL) {
499 			t         = rose_node;
500 			rose_node = rose_node->next;
501 
502 			for (i = 0; i < t->count; i++) {
503 				if (t->neighbour[i] != s)
504 					continue;
505 
506 				t->count--;
507 
508 				switch (i) {
509 				case 0:
510 					t->neighbour[0] = t->neighbour[1];
511 					/* fall through */
512 				case 1:
513 					t->neighbour[1] = t->neighbour[2];
514 				case 2:
515 					break;
516 				}
517 			}
518 
519 			if (t->count <= 0)
520 				rose_remove_node(t);
521 		}
522 
523 		rose_remove_neigh(s);
524 	}
525 	spin_unlock_bh(&rose_neigh_list_lock);
526 	spin_unlock_bh(&rose_node_list_lock);
527 }
528 
529 #if 0 /* Currently unused */
530 /*
531  *	A device has been removed. Remove its links.
532  */
533 void rose_route_device_down(struct net_device *dev)
534 {
535 	struct rose_route *s, *rose_route;
536 
537 	spin_lock_bh(&rose_route_list_lock);
538 	rose_route = rose_route_list;
539 	while (rose_route != NULL) {
540 		s          = rose_route;
541 		rose_route = rose_route->next;
542 
543 		if (s->neigh1->dev == dev || s->neigh2->dev == dev)
544 			rose_remove_route(s);
545 	}
546 	spin_unlock_bh(&rose_route_list_lock);
547 }
548 #endif
549 
550 /*
551  *	Clear all nodes and neighbours out, except for neighbours with
552  *	active connections going through them.
553  *  Do not clear loopback neighbour and nodes.
554  */
555 static int rose_clear_routes(void)
556 {
557 	struct rose_neigh *s, *rose_neigh;
558 	struct rose_node  *t, *rose_node;
559 
560 	spin_lock_bh(&rose_node_list_lock);
561 	spin_lock_bh(&rose_neigh_list_lock);
562 
563 	rose_neigh = rose_neigh_list;
564 	rose_node  = rose_node_list;
565 
566 	while (rose_node != NULL) {
567 		t         = rose_node;
568 		rose_node = rose_node->next;
569 		if (!t->loopback)
570 			rose_remove_node(t);
571 	}
572 
573 	while (rose_neigh != NULL) {
574 		s          = rose_neigh;
575 		rose_neigh = rose_neigh->next;
576 
577 		if (s->use == 0 && !s->loopback) {
578 			s->count = 0;
579 			rose_remove_neigh(s);
580 		}
581 	}
582 
583 	spin_unlock_bh(&rose_neigh_list_lock);
584 	spin_unlock_bh(&rose_node_list_lock);
585 
586 	return 0;
587 }
588 
589 /*
590  *	Check that the device given is a valid AX.25 interface that is "up".
591  * 	called with RTNL
592  */
593 static struct net_device *rose_ax25_dev_find(char *devname)
594 {
595 	struct net_device *dev;
596 
597 	if ((dev = __dev_get_by_name(&init_net, devname)) == NULL)
598 		return NULL;
599 
600 	if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
601 		return dev;
602 
603 	return NULL;
604 }
605 
606 /*
607  *	Find the first active ROSE device, usually "rose0".
608  */
609 struct net_device *rose_dev_first(void)
610 {
611 	struct net_device *dev, *first = NULL;
612 
613 	rcu_read_lock();
614 	for_each_netdev_rcu(&init_net, dev) {
615 		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
616 			if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
617 				first = dev;
618 	}
619 	rcu_read_unlock();
620 
621 	return first;
622 }
623 
624 /*
625  *	Find the ROSE device for the given address.
626  */
627 struct net_device *rose_dev_get(rose_address *addr)
628 {
629 	struct net_device *dev;
630 
631 	rcu_read_lock();
632 	for_each_netdev_rcu(&init_net, dev) {
633 		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
634 			dev_hold(dev);
635 			goto out;
636 		}
637 	}
638 	dev = NULL;
639 out:
640 	rcu_read_unlock();
641 	return dev;
642 }
643 
644 static int rose_dev_exists(rose_address *addr)
645 {
646 	struct net_device *dev;
647 
648 	rcu_read_lock();
649 	for_each_netdev_rcu(&init_net, dev) {
650 		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
651 			goto out;
652 	}
653 	dev = NULL;
654 out:
655 	rcu_read_unlock();
656 	return dev != NULL;
657 }
658 
659 
660 
661 
662 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
663 {
664 	struct rose_route *rose_route;
665 
666 	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
667 		if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
668 		    (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
669 			return rose_route;
670 
671 	return NULL;
672 }
673 
674 /*
675  *	Find a neighbour or a route given a ROSE address.
676  */
677 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause,
678 	unsigned char *diagnostic, int route_frame)
679 {
680 	struct rose_neigh *res = NULL;
681 	struct rose_node *node;
682 	int failed = 0;
683 	int i;
684 
685 	if (!route_frame) spin_lock_bh(&rose_node_list_lock);
686 	for (node = rose_node_list; node != NULL; node = node->next) {
687 		if (rosecmpm(addr, &node->address, node->mask) == 0) {
688 			for (i = 0; i < node->count; i++) {
689 				if (node->neighbour[i]->restarted) {
690 					res = node->neighbour[i];
691 					goto out;
692 				}
693 			}
694 		}
695 	}
696 	if (!route_frame) { /* connect request */
697 		for (node = rose_node_list; node != NULL; node = node->next) {
698 			if (rosecmpm(addr, &node->address, node->mask) == 0) {
699 				for (i = 0; i < node->count; i++) {
700 					if (!rose_ftimer_running(node->neighbour[i])) {
701 						res = node->neighbour[i];
702 						failed = 0;
703 						goto out;
704 					}
705 					failed = 1;
706 				}
707 			}
708 		}
709 	}
710 
711 	if (failed) {
712 		*cause      = ROSE_OUT_OF_ORDER;
713 		*diagnostic = 0;
714 	} else {
715 		*cause      = ROSE_NOT_OBTAINABLE;
716 		*diagnostic = 0;
717 	}
718 
719 out:
720 	if (!route_frame) spin_unlock_bh(&rose_node_list_lock);
721 	return res;
722 }
723 
724 /*
725  *	Handle the ioctls that control the routing functions.
726  */
727 int rose_rt_ioctl(unsigned int cmd, void __user *arg)
728 {
729 	struct rose_route_struct rose_route;
730 	struct net_device *dev;
731 	int err;
732 
733 	switch (cmd) {
734 	case SIOCADDRT:
735 		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
736 			return -EFAULT;
737 		if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
738 			return -EINVAL;
739 		if (rose_dev_exists(&rose_route.address)) /* Can't add routes to ourself */
740 			return -EINVAL;
741 		if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
742 			return -EINVAL;
743 		if (rose_route.ndigis > AX25_MAX_DIGIS)
744 			return -EINVAL;
745 		err = rose_add_node(&rose_route, dev);
746 		return err;
747 
748 	case SIOCDELRT:
749 		if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
750 			return -EFAULT;
751 		if ((dev = rose_ax25_dev_find(rose_route.device)) == NULL)
752 			return -EINVAL;
753 		err = rose_del_node(&rose_route, dev);
754 		return err;
755 
756 	case SIOCRSCLRRT:
757 		return rose_clear_routes();
758 
759 	default:
760 		return -EINVAL;
761 	}
762 
763 	return 0;
764 }
765 
766 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
767 {
768 	struct rose_route *rose_route, *s;
769 
770 	rose_neigh->restarted = 0;
771 
772 	rose_stop_t0timer(rose_neigh);
773 	rose_start_ftimer(rose_neigh);
774 
775 	skb_queue_purge(&rose_neigh->queue);
776 
777 	spin_lock_bh(&rose_route_list_lock);
778 
779 	rose_route = rose_route_list;
780 
781 	while (rose_route != NULL) {
782 		if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
783 		    (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL)       ||
784 		    (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
785 			s = rose_route->next;
786 			rose_remove_route(rose_route);
787 			rose_route = s;
788 			continue;
789 		}
790 
791 		if (rose_route->neigh1 == rose_neigh) {
792 			rose_route->neigh1->use--;
793 			rose_route->neigh1 = NULL;
794 			rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
795 		}
796 
797 		if (rose_route->neigh2 == rose_neigh) {
798 			rose_route->neigh2->use--;
799 			rose_route->neigh2 = NULL;
800 			rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
801 		}
802 
803 		rose_route = rose_route->next;
804 	}
805 	spin_unlock_bh(&rose_route_list_lock);
806 }
807 
808 /*
809  * 	A level 2 link has timed out, therefore it appears to be a poor link,
810  *	then don't use that neighbour until it is reset. Blow away all through
811  *	routes and connections using this route.
812  */
813 void rose_link_failed(ax25_cb *ax25, int reason)
814 {
815 	struct rose_neigh *rose_neigh;
816 
817 	spin_lock_bh(&rose_neigh_list_lock);
818 	rose_neigh = rose_neigh_list;
819 	while (rose_neigh != NULL) {
820 		if (rose_neigh->ax25 == ax25)
821 			break;
822 		rose_neigh = rose_neigh->next;
823 	}
824 
825 	if (rose_neigh != NULL) {
826 		rose_neigh->ax25 = NULL;
827 		ax25_cb_put(ax25);
828 
829 		rose_del_route_by_neigh(rose_neigh);
830 		rose_kill_by_neigh(rose_neigh);
831 	}
832 	spin_unlock_bh(&rose_neigh_list_lock);
833 }
834 
835 /*
836  * 	A device has been "downed" remove its link status. Blow away all
837  *	through routes and connections that use this device.
838  */
839 void rose_link_device_down(struct net_device *dev)
840 {
841 	struct rose_neigh *rose_neigh;
842 
843 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
844 		if (rose_neigh->dev == dev) {
845 			rose_del_route_by_neigh(rose_neigh);
846 			rose_kill_by_neigh(rose_neigh);
847 		}
848 	}
849 }
850 
851 /*
852  *	Route a frame to an appropriate AX.25 connection.
853  *	A NULL ax25_cb indicates an internally generated frame.
854  */
855 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
856 {
857 	struct rose_neigh *rose_neigh, *new_neigh;
858 	struct rose_route *rose_route;
859 	struct rose_facilities_struct facilities;
860 	rose_address *src_addr, *dest_addr;
861 	struct sock *sk;
862 	unsigned short frametype;
863 	unsigned int lci, new_lci;
864 	unsigned char cause, diagnostic;
865 	struct net_device *dev;
866 	int res = 0;
867 	char buf[11];
868 
869 	if (skb->len < ROSE_MIN_LEN)
870 		return res;
871 
872 	if (!ax25)
873 		return rose_loopback_queue(skb, NULL);
874 
875 	frametype = skb->data[2];
876 	lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
877 	if (frametype == ROSE_CALL_REQUEST &&
878 	    (skb->len <= ROSE_CALL_REQ_FACILITIES_OFF ||
879 	     skb->data[ROSE_CALL_REQ_ADDR_LEN_OFF] !=
880 	     ROSE_CALL_REQ_ADDR_LEN_VAL))
881 		return res;
882 	src_addr  = (rose_address *)(skb->data + ROSE_CALL_REQ_SRC_ADDR_OFF);
883 	dest_addr = (rose_address *)(skb->data + ROSE_CALL_REQ_DEST_ADDR_OFF);
884 
885 	spin_lock_bh(&rose_neigh_list_lock);
886 	spin_lock_bh(&rose_route_list_lock);
887 
888 	rose_neigh = rose_neigh_list;
889 	while (rose_neigh != NULL) {
890 		if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 &&
891 		    ax25->ax25_dev->dev == rose_neigh->dev)
892 			break;
893 		rose_neigh = rose_neigh->next;
894 	}
895 
896 	if (rose_neigh == NULL) {
897 		printk("rose_route : unknown neighbour or device %s\n",
898 		       ax2asc(buf, &ax25->dest_addr));
899 		goto out;
900 	}
901 
902 	/*
903 	 *	Obviously the link is working, halt the ftimer.
904 	 */
905 	rose_stop_ftimer(rose_neigh);
906 
907 	/*
908 	 *	LCI of zero is always for us, and its always a restart
909 	 * 	frame.
910 	 */
911 	if (lci == 0) {
912 		rose_link_rx_restart(skb, rose_neigh, frametype);
913 		goto out;
914 	}
915 
916 	/*
917 	 *	Find an existing socket.
918 	 */
919 	if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
920 		if (frametype == ROSE_CALL_REQUEST) {
921 			struct rose_sock *rose = rose_sk(sk);
922 
923 			/* Remove an existing unused socket */
924 			rose_clear_queues(sk);
925 			rose->cause	 = ROSE_NETWORK_CONGESTION;
926 			rose->diagnostic = 0;
927 			rose->neighbour->use--;
928 			rose->neighbour	 = NULL;
929 			rose->lci	 = 0;
930 			rose->state	 = ROSE_STATE_0;
931 			sk->sk_state	 = TCP_CLOSE;
932 			sk->sk_err	 = 0;
933 			sk->sk_shutdown	 |= SEND_SHUTDOWN;
934 			if (!sock_flag(sk, SOCK_DEAD)) {
935 				sk->sk_state_change(sk);
936 				sock_set_flag(sk, SOCK_DEAD);
937 			}
938 		}
939 		else {
940 			skb_reset_transport_header(skb);
941 			res = rose_process_rx_frame(sk, skb);
942 			goto out;
943 		}
944 	}
945 
946 	/*
947 	 *	Is is a Call Request and is it for us ?
948 	 */
949 	if (frametype == ROSE_CALL_REQUEST)
950 		if ((dev = rose_dev_get(dest_addr)) != NULL) {
951 			res = rose_rx_call_request(skb, dev, rose_neigh, lci);
952 			dev_put(dev);
953 			goto out;
954 		}
955 
956 	if (!sysctl_rose_routing_control) {
957 		rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
958 		goto out;
959 	}
960 
961 	/*
962 	 *	Route it to the next in line if we have an entry for it.
963 	 */
964 	rose_route = rose_route_list;
965 	while (rose_route != NULL) {
966 		if (rose_route->lci1 == lci &&
967 		    rose_route->neigh1 == rose_neigh) {
968 			if (frametype == ROSE_CALL_REQUEST) {
969 				/* F6FBB - Remove an existing unused route */
970 				rose_remove_route(rose_route);
971 				break;
972 			} else if (rose_route->neigh2 != NULL) {
973 				skb->data[0] &= 0xF0;
974 				skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
975 				skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
976 				rose_transmit_link(skb, rose_route->neigh2);
977 				if (frametype == ROSE_CLEAR_CONFIRMATION)
978 					rose_remove_route(rose_route);
979 				res = 1;
980 				goto out;
981 			} else {
982 				if (frametype == ROSE_CLEAR_CONFIRMATION)
983 					rose_remove_route(rose_route);
984 				goto out;
985 			}
986 		}
987 		if (rose_route->lci2 == lci &&
988 		    rose_route->neigh2 == rose_neigh) {
989 			if (frametype == ROSE_CALL_REQUEST) {
990 				/* F6FBB - Remove an existing unused route */
991 				rose_remove_route(rose_route);
992 				break;
993 			} else if (rose_route->neigh1 != NULL) {
994 				skb->data[0] &= 0xF0;
995 				skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
996 				skb->data[1]  = (rose_route->lci1 >> 0) & 0xFF;
997 				rose_transmit_link(skb, rose_route->neigh1);
998 				if (frametype == ROSE_CLEAR_CONFIRMATION)
999 					rose_remove_route(rose_route);
1000 				res = 1;
1001 				goto out;
1002 			} else {
1003 				if (frametype == ROSE_CLEAR_CONFIRMATION)
1004 					rose_remove_route(rose_route);
1005 				goto out;
1006 			}
1007 		}
1008 		rose_route = rose_route->next;
1009 	}
1010 
1011 	/*
1012 	 *	We know that:
1013 	 *	1. The frame isn't for us,
1014 	 *	2. It isn't "owned" by any existing route.
1015 	 */
1016 	if (frametype != ROSE_CALL_REQUEST) {	/* XXX */
1017 		res = 0;
1018 		goto out;
1019 	}
1020 
1021 	memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
1022 
1023 	if (!rose_parse_facilities(skb->data + ROSE_CALL_REQ_FACILITIES_OFF,
1024 				   skb->len - ROSE_CALL_REQ_FACILITIES_OFF,
1025 				   &facilities)) {
1026 		rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
1027 		goto out;
1028 	}
1029 
1030 	/*
1031 	 *	Check for routing loops.
1032 	 */
1033 	rose_route = rose_route_list;
1034 	while (rose_route != NULL) {
1035 		if (rose_route->rand == facilities.rand &&
1036 		    rosecmp(src_addr, &rose_route->src_addr) == 0 &&
1037 		    ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
1038 		    ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
1039 			rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
1040 			goto out;
1041 		}
1042 		rose_route = rose_route->next;
1043 	}
1044 
1045 	if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic, 1)) == NULL) {
1046 		rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
1047 		goto out;
1048 	}
1049 
1050 	if ((new_lci = rose_new_lci(new_neigh)) == 0) {
1051 		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
1052 		goto out;
1053 	}
1054 
1055 	if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
1056 		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
1057 		goto out;
1058 	}
1059 
1060 	rose_route->lci1      = lci;
1061 	rose_route->src_addr  = *src_addr;
1062 	rose_route->dest_addr = *dest_addr;
1063 	rose_route->src_call  = facilities.dest_call;
1064 	rose_route->dest_call = facilities.source_call;
1065 	rose_route->rand      = facilities.rand;
1066 	rose_route->neigh1    = rose_neigh;
1067 	rose_route->lci2      = new_lci;
1068 	rose_route->neigh2    = new_neigh;
1069 
1070 	rose_route->neigh1->use++;
1071 	rose_route->neigh2->use++;
1072 
1073 	rose_route->next = rose_route_list;
1074 	rose_route_list  = rose_route;
1075 
1076 	skb->data[0] &= 0xF0;
1077 	skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
1078 	skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
1079 
1080 	rose_transmit_link(skb, rose_route->neigh2);
1081 	res = 1;
1082 
1083 out:
1084 	spin_unlock_bh(&rose_route_list_lock);
1085 	spin_unlock_bh(&rose_neigh_list_lock);
1086 
1087 	return res;
1088 }
1089 
1090 #ifdef CONFIG_PROC_FS
1091 
1092 static void *rose_node_start(struct seq_file *seq, loff_t *pos)
1093 	__acquires(rose_node_list_lock)
1094 {
1095 	struct rose_node *rose_node;
1096 	int i = 1;
1097 
1098 	spin_lock_bh(&rose_node_list_lock);
1099 	if (*pos == 0)
1100 		return SEQ_START_TOKEN;
1101 
1102 	for (rose_node = rose_node_list; rose_node && i < *pos;
1103 	     rose_node = rose_node->next, ++i);
1104 
1105 	return (i == *pos) ? rose_node : NULL;
1106 }
1107 
1108 static void *rose_node_next(struct seq_file *seq, void *v, loff_t *pos)
1109 {
1110 	++*pos;
1111 
1112 	return (v == SEQ_START_TOKEN) ? rose_node_list
1113 		: ((struct rose_node *)v)->next;
1114 }
1115 
1116 static void rose_node_stop(struct seq_file *seq, void *v)
1117 	__releases(rose_node_list_lock)
1118 {
1119 	spin_unlock_bh(&rose_node_list_lock);
1120 }
1121 
1122 static int rose_node_show(struct seq_file *seq, void *v)
1123 {
1124 	char rsbuf[11];
1125 	int i;
1126 
1127 	if (v == SEQ_START_TOKEN)
1128 		seq_puts(seq, "address    mask n neigh neigh neigh\n");
1129 	else {
1130 		const struct rose_node *rose_node = v;
1131 		/* if (rose_node->loopback) {
1132 			seq_printf(seq, "%-10s %04d 1 loopback\n",
1133 				   rose2asc(rsbuf, &rose_node->address),
1134 				   rose_node->mask);
1135 		} else { */
1136 			seq_printf(seq, "%-10s %04d %d",
1137 				   rose2asc(rsbuf, &rose_node->address),
1138 				   rose_node->mask,
1139 				   rose_node->count);
1140 
1141 			for (i = 0; i < rose_node->count; i++)
1142 				seq_printf(seq, " %05d",
1143 					rose_node->neighbour[i]->number);
1144 
1145 			seq_puts(seq, "\n");
1146 		/* } */
1147 	}
1148 	return 0;
1149 }
1150 
1151 const struct seq_operations rose_node_seqops = {
1152 	.start = rose_node_start,
1153 	.next = rose_node_next,
1154 	.stop = rose_node_stop,
1155 	.show = rose_node_show,
1156 };
1157 
1158 static void *rose_neigh_start(struct seq_file *seq, loff_t *pos)
1159 	__acquires(rose_neigh_list_lock)
1160 {
1161 	struct rose_neigh *rose_neigh;
1162 	int i = 1;
1163 
1164 	spin_lock_bh(&rose_neigh_list_lock);
1165 	if (*pos == 0)
1166 		return SEQ_START_TOKEN;
1167 
1168 	for (rose_neigh = rose_neigh_list; rose_neigh && i < *pos;
1169 	     rose_neigh = rose_neigh->next, ++i);
1170 
1171 	return (i == *pos) ? rose_neigh : NULL;
1172 }
1173 
1174 static void *rose_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
1175 {
1176 	++*pos;
1177 
1178 	return (v == SEQ_START_TOKEN) ? rose_neigh_list
1179 		: ((struct rose_neigh *)v)->next;
1180 }
1181 
1182 static void rose_neigh_stop(struct seq_file *seq, void *v)
1183 	__releases(rose_neigh_list_lock)
1184 {
1185 	spin_unlock_bh(&rose_neigh_list_lock);
1186 }
1187 
1188 static int rose_neigh_show(struct seq_file *seq, void *v)
1189 {
1190 	char buf[11];
1191 	int i;
1192 
1193 	if (v == SEQ_START_TOKEN)
1194 		seq_puts(seq,
1195 			 "addr  callsign  dev  count use mode restart  t0  tf digipeaters\n");
1196 	else {
1197 		struct rose_neigh *rose_neigh = v;
1198 
1199 		/* if (!rose_neigh->loopback) { */
1200 		seq_printf(seq, "%05d %-9s %-4s   %3d %3d  %3s     %3s %3lu %3lu",
1201 			   rose_neigh->number,
1202 			   (rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(buf, &rose_neigh->callsign),
1203 			   rose_neigh->dev ? rose_neigh->dev->name : "???",
1204 			   rose_neigh->count,
1205 			   rose_neigh->use,
1206 			   (rose_neigh->dce_mode) ? "DCE" : "DTE",
1207 			   (rose_neigh->restarted) ? "yes" : "no",
1208 			   ax25_display_timer(&rose_neigh->t0timer) / HZ,
1209 			   ax25_display_timer(&rose_neigh->ftimer)  / HZ);
1210 
1211 		if (rose_neigh->digipeat != NULL) {
1212 			for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1213 				seq_printf(seq, " %s", ax2asc(buf, &rose_neigh->digipeat->calls[i]));
1214 		}
1215 
1216 		seq_puts(seq, "\n");
1217 	}
1218 	return 0;
1219 }
1220 
1221 
1222 const struct seq_operations rose_neigh_seqops = {
1223 	.start = rose_neigh_start,
1224 	.next = rose_neigh_next,
1225 	.stop = rose_neigh_stop,
1226 	.show = rose_neigh_show,
1227 };
1228 
1229 static void *rose_route_start(struct seq_file *seq, loff_t *pos)
1230 	__acquires(rose_route_list_lock)
1231 {
1232 	struct rose_route *rose_route;
1233 	int i = 1;
1234 
1235 	spin_lock_bh(&rose_route_list_lock);
1236 	if (*pos == 0)
1237 		return SEQ_START_TOKEN;
1238 
1239 	for (rose_route = rose_route_list; rose_route && i < *pos;
1240 	     rose_route = rose_route->next, ++i);
1241 
1242 	return (i == *pos) ? rose_route : NULL;
1243 }
1244 
1245 static void *rose_route_next(struct seq_file *seq, void *v, loff_t *pos)
1246 {
1247 	++*pos;
1248 
1249 	return (v == SEQ_START_TOKEN) ? rose_route_list
1250 		: ((struct rose_route *)v)->next;
1251 }
1252 
1253 static void rose_route_stop(struct seq_file *seq, void *v)
1254 	__releases(rose_route_list_lock)
1255 {
1256 	spin_unlock_bh(&rose_route_list_lock);
1257 }
1258 
1259 static int rose_route_show(struct seq_file *seq, void *v)
1260 {
1261 	char buf[11], rsbuf[11];
1262 
1263 	if (v == SEQ_START_TOKEN)
1264 		seq_puts(seq,
1265 			 "lci  address     callsign   neigh  <-> lci  address     callsign   neigh\n");
1266 	else {
1267 		struct rose_route *rose_route = v;
1268 
1269 		if (rose_route->neigh1)
1270 			seq_printf(seq,
1271 				   "%3.3X  %-10s  %-9s  %05d      ",
1272 				   rose_route->lci1,
1273 				   rose2asc(rsbuf, &rose_route->src_addr),
1274 				   ax2asc(buf, &rose_route->src_call),
1275 				   rose_route->neigh1->number);
1276 		else
1277 			seq_puts(seq,
1278 				 "000  *           *          00000      ");
1279 
1280 		if (rose_route->neigh2)
1281 			seq_printf(seq,
1282 				   "%3.3X  %-10s  %-9s  %05d\n",
1283 				   rose_route->lci2,
1284 				   rose2asc(rsbuf, &rose_route->dest_addr),
1285 				   ax2asc(buf, &rose_route->dest_call),
1286 				   rose_route->neigh2->number);
1287 		 else
1288 			 seq_puts(seq,
1289 				  "000  *           *          00000\n");
1290 		}
1291 	return 0;
1292 }
1293 
1294 struct seq_operations rose_route_seqops = {
1295 	.start = rose_route_start,
1296 	.next = rose_route_next,
1297 	.stop = rose_route_stop,
1298 	.show = rose_route_show,
1299 };
1300 #endif /* CONFIG_PROC_FS */
1301 
1302 /*
1303  *	Release all memory associated with ROSE routing structures.
1304  */
1305 void __exit rose_rt_free(void)
1306 {
1307 	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1308 	struct rose_node  *t, *rose_node  = rose_node_list;
1309 	struct rose_route *u, *rose_route = rose_route_list;
1310 
1311 	while (rose_neigh != NULL) {
1312 		s          = rose_neigh;
1313 		rose_neigh = rose_neigh->next;
1314 
1315 		rose_remove_neigh(s);
1316 	}
1317 
1318 	while (rose_node != NULL) {
1319 		t         = rose_node;
1320 		rose_node = rose_node->next;
1321 
1322 		rose_remove_node(t);
1323 	}
1324 
1325 	while (rose_route != NULL) {
1326 		u          = rose_route;
1327 		rose_route = rose_route->next;
1328 
1329 		rose_remove_route(u);
1330 	}
1331 }
1332