1 /*
2  *	ROSE release 003
3  *
4  *	This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *	This module:
7  *		This module is free software; you can redistribute it and/or
8  *		modify it under the terms of the GNU General Public License
9  *		as published by the Free Software Foundation; either version
10  *		2 of the License, or (at your option) any later version.
11  *
12  *	History
13  *	ROSE 001	Jonathan(G4KLX)	Cloned from nr_route.c.
14  *			Terry(VK2KTJ)	Added support for variable length
15  *					address masks.
16  *	ROSE 002	Jonathan(G4KLX)	Uprated through routing of packets.
17  *					Routing loop detection.
18  *	ROSE 003	Jonathan(G4KLX)	New timer architecture.
19  *					Added use count to neighbours.
20  */
21 
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
25 #include <linux/in.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/string.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
32 #include <net/ax25.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <net/arp.h>
36 #include <linux/if_arp.h>
37 #include <linux/skbuff.h>
38 #include <net/sock.h>
39 #include <asm/segment.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42 #include <linux/fcntl.h>
43 #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
44 #include <linux/mm.h>
45 #include <linux/interrupt.h>
46 #include <linux/notifier.h>
47 #include <linux/netfilter.h>
48 #include <linux/init.h>
49 #include <net/rose.h>
50 
51 static unsigned int rose_neigh_no = 1;
52 
53 static struct rose_node  *rose_node_list;
54 static struct rose_neigh *rose_neigh_list;
55 static struct rose_route *rose_route_list;
56 
57 struct rose_neigh *rose_loopback_neigh;
58 
59 static void rose_remove_neigh(struct rose_neigh *);
60 
61 /*
62  *	Add a new route to a node, and in the process add the node and the
63  *	neighbour if it is new.
64  */
rose_add_node(struct rose_route_struct * rose_route,struct net_device * dev)65 static int rose_add_node(struct rose_route_struct *rose_route, struct net_device *dev)
66 {
67 	struct rose_node  *rose_node, *rose_tmpn, *rose_tmpp;
68 	struct rose_neigh *rose_neigh;
69 	unsigned long flags;
70 	int i;
71 
72 	for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next)
73 		if ((rose_node->mask == rose_route->mask) && (rosecmpm(&rose_route->address, &rose_node->address, rose_route->mask) == 0))
74 			break;
75 
76 	if (rose_node != NULL && rose_node->loopback)
77 		return -EINVAL;
78 
79 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next)
80 		if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0 && rose_neigh->dev == dev)
81 			break;
82 
83 	if (rose_neigh == NULL) {
84 		if ((rose_neigh = kmalloc(sizeof(*rose_neigh), GFP_ATOMIC)) == NULL)
85 			return -ENOMEM;
86 
87 		rose_neigh->callsign  = rose_route->neighbour;
88 		rose_neigh->digipeat  = NULL;
89 		rose_neigh->ax25      = NULL;
90 		rose_neigh->dev       = dev;
91 		rose_neigh->count     = 0;
92 		rose_neigh->use       = 0;
93 		rose_neigh->dce_mode  = 0;
94 		rose_neigh->loopback  = 0;
95 		rose_neigh->number    = rose_neigh_no++;
96 		rose_neigh->restarted = 0;
97 
98 		skb_queue_head_init(&rose_neigh->queue);
99 
100 		init_timer(&rose_neigh->ftimer);
101 		init_timer(&rose_neigh->t0timer);
102 
103 		if (rose_route->ndigis != 0) {
104 			if ((rose_neigh->digipeat = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
105 				kfree(rose_neigh);
106 				return -ENOMEM;
107 			}
108 
109 			rose_neigh->digipeat->ndigi      = rose_route->ndigis;
110 			rose_neigh->digipeat->lastrepeat = -1;
111 
112 			for (i = 0; i < rose_route->ndigis; i++) {
113 				rose_neigh->digipeat->calls[i]    = rose_route->digipeaters[i];
114 				rose_neigh->digipeat->repeated[i] = 0;
115 			}
116 		}
117 
118 		save_flags(flags); cli();
119 		rose_neigh->next = rose_neigh_list;
120 		rose_neigh_list  = rose_neigh;
121 		restore_flags(flags);
122 	}
123 
124 	/*
125 	 * This is a new node to be inserted into the list. Find where it needs
126 	 * to be inserted into the list, and insert it. We want to be sure
127 	 * to order the list in descending order of mask size to ensure that
128 	 * later when we are searching this list the first match will be the
129 	 * best match.
130 	 */
131 	if (rose_node == NULL) {
132 		rose_tmpn = rose_node_list;
133 		rose_tmpp = NULL;
134 
135 		while (rose_tmpn != NULL) {
136 			if (rose_tmpn->mask > rose_route->mask) {
137 				rose_tmpp = rose_tmpn;
138 				rose_tmpn = rose_tmpn->next;
139 			} else {
140 				break;
141 			}
142 		}
143 
144 		/* create new node */
145 		if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL)
146 			return -ENOMEM;
147 
148 		rose_node->address      = rose_route->address;
149 		rose_node->mask         = rose_route->mask;
150 		rose_node->count        = 1;
151 		rose_node->loopback     = 0;
152 		rose_node->neighbour[0] = rose_neigh;
153 
154 		save_flags(flags); cli();
155 
156 		if (rose_tmpn == NULL) {
157 			if (rose_tmpp == NULL) {	/* Empty list */
158 				rose_node_list  = rose_node;
159 				rose_node->next = NULL;
160 			} else {
161 				rose_tmpp->next = rose_node;
162 				rose_node->next = NULL;
163 			}
164 		} else {
165 			if (rose_tmpp == NULL) {	/* 1st node */
166 				rose_node->next = rose_node_list;
167 				rose_node_list  = rose_node;
168 			} else {
169 				rose_tmpp->next = rose_node;
170 				rose_node->next = rose_tmpn;
171 			}
172 		}
173 
174 		restore_flags(flags);
175 
176 		rose_neigh->count++;
177 
178 		return 0;
179 	}
180 
181 	/* We have space, slot it in */
182 	if (rose_node->count < 3) {
183 		rose_node->neighbour[rose_node->count] = rose_neigh;
184 		rose_node->count++;
185 		rose_neigh->count++;
186 	}
187 
188 	return 0;
189 }
190 
rose_remove_node(struct rose_node * rose_node)191 static void rose_remove_node(struct rose_node *rose_node)
192 {
193 	struct rose_node *s;
194 	unsigned long flags;
195 
196 	save_flags(flags);
197 	cli();
198 
199 	if ((s = rose_node_list) == rose_node) {
200 		rose_node_list = rose_node->next;
201 		restore_flags(flags);
202 		kfree(rose_node);
203 		return;
204 	}
205 
206 	while (s != NULL && s->next != NULL) {
207 		if (s->next == rose_node) {
208 			s->next = rose_node->next;
209 			restore_flags(flags);
210 			kfree(rose_node);
211 			return;
212 		}
213 
214 		s = s->next;
215 	}
216 
217 	restore_flags(flags);
218 }
219 
rose_remove_neigh(struct rose_neigh * rose_neigh)220 static void rose_remove_neigh(struct rose_neigh *rose_neigh)
221 {
222 	struct rose_neigh *s;
223 	unsigned long flags;
224 
225 	rose_stop_ftimer(rose_neigh);
226 	rose_stop_t0timer(rose_neigh);
227 
228 	skb_queue_purge(&rose_neigh->queue);
229 
230 	save_flags(flags); cli();
231 
232 	if ((s = rose_neigh_list) == rose_neigh) {
233 		rose_neigh_list = rose_neigh->next;
234 		restore_flags(flags);
235 		if (rose_neigh->digipeat != NULL)
236 			kfree(rose_neigh->digipeat);
237 		kfree(rose_neigh);
238 		return;
239 	}
240 
241 	while (s != NULL && s->next != NULL) {
242 		if (s->next == rose_neigh) {
243 			s->next = rose_neigh->next;
244 			restore_flags(flags);
245 			if (rose_neigh->digipeat != NULL)
246 				kfree(rose_neigh->digipeat);
247 			kfree(rose_neigh);
248 			return;
249 		}
250 
251 		s = s->next;
252 	}
253 
254 	restore_flags(flags);
255 }
256 
rose_remove_route(struct rose_route * rose_route)257 static void rose_remove_route(struct rose_route *rose_route)
258 {
259 	struct rose_route *s;
260 	unsigned long flags;
261 
262 	if (rose_route->neigh1 != NULL)
263 		rose_route->neigh1->use--;
264 
265 	if (rose_route->neigh2 != NULL)
266 		rose_route->neigh2->use--;
267 
268 	save_flags(flags); cli();
269 
270 	if ((s = rose_route_list) == rose_route) {
271 		rose_route_list = rose_route->next;
272 		restore_flags(flags);
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 			restore_flags(flags);
281 			kfree(rose_route);
282 			return;
283 		}
284 
285 		s = s->next;
286 	}
287 
288 	restore_flags(flags);
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  */
rose_del_node(struct rose_route_struct * rose_route,struct net_device * dev)295 static int rose_del_node(struct rose_route_struct *rose_route, struct net_device *dev)
296 {
297 	struct rose_node  *rose_node;
298 	struct rose_neigh *rose_neigh;
299 	int i;
300 
301 	for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next)
302 		if ((rose_node->mask == rose_route->mask) && (rosecmpm(&rose_route->address, &rose_node->address, rose_route->mask) == 0))
303 			break;
304 
305 	if (rose_node == NULL) return -EINVAL;
306 
307 	if (rose_node->loopback) return -EINVAL;
308 
309 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next)
310 		if (ax25cmp(&rose_route->neighbour, &rose_neigh->callsign) == 0 && rose_neigh->dev == dev)
311 			break;
312 
313 	if (rose_neigh == NULL) return -EINVAL;
314 
315 	for (i = 0; i < rose_node->count; i++) {
316 		if (rose_node->neighbour[i] == rose_neigh) {
317 			rose_neigh->count--;
318 
319 			if (rose_neigh->count == 0 && rose_neigh->use == 0)
320 				rose_remove_neigh(rose_neigh);
321 
322 			rose_node->count--;
323 
324 			if (rose_node->count == 0) {
325 				rose_remove_node(rose_node);
326 			} else {
327 				switch (i) {
328 					case 0:
329 						rose_node->neighbour[0] = rose_node->neighbour[1];
330 					case 1:
331 						rose_node->neighbour[1] = rose_node->neighbour[2];
332 					case 2:
333 						break;
334 				}
335 			}
336 
337 			return 0;
338 		}
339 	}
340 
341 	return -EINVAL;
342 }
343 
344 /*
345  *	Add the loopback neighbour.
346  */
rose_add_loopback_neigh(void)347 int rose_add_loopback_neigh(void)
348 {
349 	unsigned long flags;
350 
351 	if ((rose_loopback_neigh = kmalloc(sizeof(struct rose_neigh), GFP_ATOMIC)) == NULL)
352 		return -ENOMEM;
353 
354 	rose_loopback_neigh->callsign  = null_ax25_address;
355 	rose_loopback_neigh->digipeat  = NULL;
356 	rose_loopback_neigh->ax25      = NULL;
357 	rose_loopback_neigh->dev       = NULL;
358 	rose_loopback_neigh->count     = 0;
359 	rose_loopback_neigh->use       = 0;
360 	rose_loopback_neigh->dce_mode  = 1;
361 	rose_loopback_neigh->loopback  = 1;
362 	rose_loopback_neigh->number    = rose_neigh_no++;
363 	rose_loopback_neigh->restarted = 1;
364 
365 	skb_queue_head_init(&rose_loopback_neigh->queue);
366 
367 	init_timer(&rose_loopback_neigh->ftimer);
368 	init_timer(&rose_loopback_neigh->t0timer);
369 
370 	save_flags(flags); cli();
371 	rose_loopback_neigh->next = rose_neigh_list;
372 	rose_neigh_list           = rose_loopback_neigh;
373 	restore_flags(flags);
374 
375 	return 0;
376 }
377 
378 /*
379  *	Add a loopback node.
380  */
rose_add_loopback_node(rose_address * address)381 int rose_add_loopback_node(rose_address *address)
382 {
383 	struct rose_node *rose_node;
384 	unsigned long flags;
385 
386 	for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next)
387 		if ((rose_node->mask == 10) && (rosecmpm(address, &rose_node->address, 10) == 0) && rose_node->loopback)
388 			break;
389 
390 	if (rose_node != NULL) return 0;
391 
392 	if ((rose_node = kmalloc(sizeof(*rose_node), GFP_ATOMIC)) == NULL)
393 		return -ENOMEM;
394 
395 	rose_node->address      = *address;
396 	rose_node->mask         = 10;
397 	rose_node->count        = 1;
398 	rose_node->loopback     = 1;
399 	rose_node->neighbour[0] = rose_loopback_neigh;
400 
401 	/* Insert at the head of list. Address is always mask=10 */
402 	save_flags(flags); cli();
403 	rose_node->next = rose_node_list;
404 	rose_node_list  = rose_node;
405 	restore_flags(flags);
406 
407 	rose_loopback_neigh->count++;
408 
409 	return 0;
410 }
411 
412 /*
413  *	Delete a loopback node.
414  */
rose_del_loopback_node(rose_address * address)415 void rose_del_loopback_node(rose_address *address)
416 {
417 	struct rose_node *rose_node;
418 
419 	for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next)
420 		if ((rose_node->mask == 10) && (rosecmpm(address, &rose_node->address, 10) == 0) && rose_node->loopback)
421 			break;
422 
423 	if (rose_node == NULL) return;
424 
425 	rose_remove_node(rose_node);
426 
427 	rose_loopback_neigh->count--;
428 }
429 
430 /*
431  *	A device has been removed. Remove its routes and neighbours.
432  */
rose_rt_device_down(struct net_device * dev)433 void rose_rt_device_down(struct net_device *dev)
434 {
435 	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
436 	struct rose_node  *t, *rose_node;
437 	int i;
438 
439 	while (rose_neigh != NULL) {
440 		s          = rose_neigh;
441 		rose_neigh = rose_neigh->next;
442 
443 		if (s->dev == dev) {
444 			rose_node = rose_node_list;
445 
446 			while (rose_node != NULL) {
447 				t         = rose_node;
448 				rose_node = rose_node->next;
449 
450 				for (i = 0; i < t->count; i++) {
451 					if (t->neighbour[i] == s) {
452 						t->count--;
453 
454 						switch (i) {
455 							case 0:
456 								t->neighbour[0] = t->neighbour[1];
457 							case 1:
458 								t->neighbour[1] = t->neighbour[2];
459 							case 2:
460 								break;
461 						}
462 					}
463 				}
464 
465 				if (t->count <= 0)
466 					rose_remove_node(t);
467 			}
468 
469 			rose_remove_neigh(s);
470 		}
471 	}
472 }
473 
474 /*
475  *	A device has been removed. Remove its links.
476  */
rose_route_device_down(struct net_device * dev)477 void rose_route_device_down(struct net_device *dev)
478 {
479 	struct rose_route *s, *rose_route = rose_route_list;
480 
481 	while (rose_route != NULL) {
482 		s          = rose_route;
483 		rose_route = rose_route->next;
484 
485 		if (s->neigh1->dev == dev || s->neigh2->dev == dev)
486 			rose_remove_route(s);
487 	}
488 }
489 
490 /*
491  *	Clear all nodes and neighbours out, except for neighbours with
492  *	active connections going through them.
493  *  Do not clear loopback neighbour and nodes.
494  */
rose_clear_routes(void)495 static int rose_clear_routes(void)
496 {
497 	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
498 	struct rose_node  *t, *rose_node  = rose_node_list;
499 
500 	while (rose_node != NULL) {
501 		t         = rose_node;
502 		rose_node = rose_node->next;
503 		if (!t->loopback)
504 			rose_remove_node(t);
505 	}
506 
507 	while (rose_neigh != NULL) {
508 		s          = rose_neigh;
509 		rose_neigh = rose_neigh->next;
510 
511 		if (s->use == 0 && !s->loopback) {
512 			s->count = 0;
513 			rose_remove_neigh(s);
514 		}
515 	}
516 
517 	return 0;
518 }
519 
520 /*
521  *	Check that the device given is a valid AX.25 interface that is "up".
522  */
rose_ax25_dev_get(char * devname)523 struct net_device *rose_ax25_dev_get(char *devname)
524 {
525 	struct net_device *dev;
526 
527 	if ((dev = dev_get_by_name(devname)) == NULL)
528 		return NULL;
529 
530 	if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
531 		return dev;
532 
533 	dev_put(dev);
534 	return NULL;
535 }
536 
537 /*
538  *	Find the first active ROSE device, usually "rose0".
539  */
rose_dev_first(void)540 struct net_device *rose_dev_first(void)
541 {
542 	struct net_device *dev, *first = NULL;
543 
544 	read_lock(&dev_base_lock);
545 	for (dev = dev_base; dev != NULL; dev = dev->next) {
546 		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE)
547 			if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
548 				first = dev;
549 	}
550 	read_unlock(&dev_base_lock);
551 
552 	return first;
553 }
554 
555 /*
556  *	Find the ROSE device for the given address.
557  */
rose_dev_get(rose_address * addr)558 struct net_device *rose_dev_get(rose_address *addr)
559 {
560 	struct net_device *dev;
561 
562 	read_lock(&dev_base_lock);
563 	for (dev = dev_base; dev != NULL; dev = dev->next) {
564 		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0) {
565 			dev_hold(dev);
566 			goto out;
567 		}
568 	}
569 out:
570 	read_unlock(&dev_base_lock);
571 	return dev;
572 }
573 
rose_dev_exists(rose_address * addr)574 static int rose_dev_exists(rose_address *addr)
575 {
576 	struct net_device *dev;
577 
578 	read_lock(&dev_base_lock);
579 	for (dev = dev_base; dev != NULL; dev = dev->next) {
580 		if ((dev->flags & IFF_UP) && dev->type == ARPHRD_ROSE && rosecmp(addr, (rose_address *)dev->dev_addr) == 0)
581 			goto out;
582 	}
583 out:
584 	read_unlock(&dev_base_lock);
585 	return dev != NULL;
586 }
587 
588 
589 
590 
rose_route_free_lci(unsigned int lci,struct rose_neigh * neigh)591 struct rose_route *rose_route_free_lci(unsigned int lci, struct rose_neigh *neigh)
592 {
593 	struct rose_route *rose_route;
594 
595 	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next)
596 		if ((rose_route->neigh1 == neigh && rose_route->lci1 == lci) ||
597 		    (rose_route->neigh2 == neigh && rose_route->lci2 == lci))
598 			return rose_route;
599 
600 	return NULL;
601 }
602 
603 /*
604  *	Find a neighbour given a ROSE address.
605  */
rose_get_neigh(rose_address * addr,unsigned char * cause,unsigned char * diagnostic)606 struct rose_neigh *rose_get_neigh(rose_address *addr, unsigned char *cause, unsigned char *diagnostic)
607 {
608 	struct rose_node *node;
609 	int failed = 0;
610 	int i;
611 
612 	for (node = rose_node_list; node != NULL; node = node->next) {
613 		if (rosecmpm(addr, &node->address, node->mask) == 0) {
614 			for (i = 0; i < node->count; i++) {
615 				if (!rose_ftimer_running(node->neighbour[i])) {
616 					return node->neighbour[i]; }
617 				else
618 					failed = 1;
619 			}
620 			break;
621 		}
622 	}
623 
624 	if (failed) {
625 		*cause      = ROSE_OUT_OF_ORDER;
626 		*diagnostic = 0;
627 	} else {
628 		*cause      = ROSE_NOT_OBTAINABLE;
629 		*diagnostic = 0;
630 	}
631 
632 	return NULL;
633 }
634 
635 /*
636  *	Handle the ioctls that control the routing functions.
637  */
rose_rt_ioctl(unsigned int cmd,void * arg)638 int rose_rt_ioctl(unsigned int cmd, void *arg)
639 {
640 	struct rose_route_struct rose_route;
641 	struct net_device *dev;
642 	int err;
643 
644 	switch (cmd) {
645 
646 		case SIOCADDRT:
647 			if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
648 				return -EFAULT;
649 			if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
650 				return -EINVAL;
651 			if (rose_dev_exists(&rose_route.address)) { /* Can't add routes to ourself */
652 				dev_put(dev);
653 				return -EINVAL;
654 			}
655 			if (rose_route.mask > 10) /* Mask can't be more than 10 digits */
656 				return -EINVAL;
657 
658 			if(rose_route.ndigis > 8) /* No more than 8 digipeats */
659 				return -EINVAL;
660 
661 			err = rose_add_node(&rose_route, dev);
662 			dev_put(dev);
663 			return err;
664 
665 		case SIOCDELRT:
666 			if (copy_from_user(&rose_route, arg, sizeof(struct rose_route_struct)))
667 				return -EFAULT;
668 			if ((dev = rose_ax25_dev_get(rose_route.device)) == NULL)
669 				return -EINVAL;
670 			err = rose_del_node(&rose_route, dev);
671 			dev_put(dev);
672 			return err;
673 
674 
675 		case SIOCRSCLRRT:
676 			return rose_clear_routes();
677 
678 		default:
679 			return -EINVAL;
680 	}
681 
682 	return 0;
683 }
684 
rose_del_route_by_neigh(struct rose_neigh * rose_neigh)685 static void rose_del_route_by_neigh(struct rose_neigh *rose_neigh)
686 {
687 	struct rose_route *rose_route, *s;
688 
689 	rose_neigh->restarted = 0;
690 
691 	rose_stop_t0timer(rose_neigh);
692 	rose_start_ftimer(rose_neigh);
693 
694 	skb_queue_purge(&rose_neigh->queue);
695 
696 	rose_route = rose_route_list;
697 
698 	while (rose_route != NULL) {
699 		if ((rose_route->neigh1 == rose_neigh && rose_route->neigh2 == rose_neigh) ||
700 		    (rose_route->neigh1 == rose_neigh && rose_route->neigh2 == NULL)       ||
701 		    (rose_route->neigh2 == rose_neigh && rose_route->neigh1 == NULL)) {
702 			s = rose_route->next;
703 			rose_remove_route(rose_route);
704 			rose_route = s;
705 			continue;
706 		}
707 
708 		if (rose_route->neigh1 == rose_neigh) {
709 			rose_route->neigh1->use--;
710 			rose_route->neigh1 = NULL;
711 			rose_transmit_clear_request(rose_route->neigh2, rose_route->lci2, ROSE_OUT_OF_ORDER, 0);
712 		}
713 
714 		if (rose_route->neigh2 == rose_neigh) {
715 			rose_route->neigh2->use--;
716 			rose_route->neigh2 = NULL;
717 			rose_transmit_clear_request(rose_route->neigh1, rose_route->lci1, ROSE_OUT_OF_ORDER, 0);
718 		}
719 
720 		rose_route = rose_route->next;
721 	}
722 }
723 
724 /*
725  * 	A level 2 link has timed out, therefore it appears to be a poor link,
726  *	then don't use that neighbour until it is reset. Blow away all through
727  *	routes and connections using this route.
728  */
rose_link_failed(ax25_cb * ax25,int reason)729 void rose_link_failed(ax25_cb *ax25, int reason)
730 {
731 	struct rose_neigh *rose_neigh;
732 
733 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next)
734 		if (rose_neigh->ax25 == ax25)
735 			break;
736 
737 	if (rose_neigh == NULL) return;
738 
739 	rose_neigh->ax25 = NULL;
740 
741 	rose_del_route_by_neigh(rose_neigh);
742 	rose_kill_by_neigh(rose_neigh);
743 }
744 
745 /*
746  * 	A device has been "downed" remove its link status. Blow away all
747  *	through routes and connections that use this device.
748  */
rose_link_device_down(struct net_device * dev)749 void rose_link_device_down(struct net_device *dev)
750 {
751 	struct rose_neigh *rose_neigh;
752 
753 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
754 		if (rose_neigh->dev == dev) {
755 			rose_del_route_by_neigh(rose_neigh);
756 			rose_kill_by_neigh(rose_neigh);
757 		}
758 	}
759 }
760 
761 /*
762  *	Route a frame to an appropriate AX.25 connection.
763  */
rose_route_frame(struct sk_buff * skb,ax25_cb * ax25)764 int rose_route_frame(struct sk_buff *skb, ax25_cb *ax25)
765 {
766 	struct rose_neigh *rose_neigh, *new_neigh;
767 	struct rose_route *rose_route;
768 	struct rose_facilities_struct facilities;
769 	rose_address *src_addr, *dest_addr;
770 	struct sock *sk;
771 	unsigned short frametype;
772 	unsigned int lci, new_lci;
773 	unsigned char cause, diagnostic;
774 	struct net_device *dev;
775 	unsigned long flags;
776 	int len;
777 
778 #if 0
779 	if (call_in_firewall(PF_ROSE, skb->dev, skb->data, NULL, &skb) != FW_ACCEPT)
780 		return 0;
781 #endif
782 
783 	frametype = skb->data[2];
784 	lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
785 	src_addr  = (rose_address *)(skb->data + 9);
786 	dest_addr = (rose_address *)(skb->data + 4);
787 
788 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next)
789 		if (ax25cmp(&ax25->dest_addr, &rose_neigh->callsign) == 0 && ax25->ax25_dev->dev == rose_neigh->dev)
790 			break;
791 
792 	if (rose_neigh == NULL) {
793 		printk("rose_route : unknown neighbour or device %s\n", ax2asc(&ax25->dest_addr));
794 		return 0;
795 	}
796 
797 	/*
798 	 *	Obviously the link is working, halt the ftimer.
799 	 */
800 	rose_stop_ftimer(rose_neigh);
801 
802 	/*
803 	 *	LCI of zero is always for us, and its always a restart
804 	 * 	frame.
805 	 */
806 	if (lci == 0) {
807 		rose_link_rx_restart(skb, rose_neigh, frametype);
808 		return 0;
809 	}
810 
811 	/*
812 	 *	Find an existing socket.
813 	 */
814 	if ((sk = rose_find_socket(lci, rose_neigh)) != NULL) {
815 		if (frametype == ROSE_CALL_REQUEST) {
816 			/* Remove an existing unused socket */
817 			rose_clear_queues(sk);
818 			sk->protinfo.rose->cause      = ROSE_NETWORK_CONGESTION;
819 			sk->protinfo.rose->diagnostic = 0;
820 			sk->protinfo.rose->neighbour->use--;
821 			sk->protinfo.rose->neighbour = NULL;
822 			sk->protinfo.rose->lci   = 0;
823 			sk->protinfo.rose->state = ROSE_STATE_0;
824 			sk->state                = TCP_CLOSE;
825 			sk->err                  = 0;
826 			sk->shutdown            |= SEND_SHUTDOWN;
827 			if (!sk->dead)
828 				sk->state_change(sk);
829 			sk->dead                 = 1;
830 		}
831 		else {
832 			skb->h.raw = skb->data;
833 			return rose_process_rx_frame(sk, skb);
834 		}
835 	}
836 
837 	/*
838 	 *	Is is a Call Request and is it for us ?
839 	 */
840 	if (frametype == ROSE_CALL_REQUEST)
841 		if ((dev = rose_dev_get(dest_addr)) != NULL) {
842 			int err = rose_rx_call_request(skb, dev, rose_neigh, lci);
843 			dev_put(dev);
844 			return err;
845 		}
846 
847 	if (!sysctl_rose_routing_control) {
848 		rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 0);
849 		return 0;
850 	}
851 
852 	/*
853 	 *	Route it to the next in line if we have an entry for it.
854 	 */
855 	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next) {
856 		if (rose_route->lci1 == lci && rose_route->neigh1 == rose_neigh) {
857 			if (frametype == ROSE_CALL_REQUEST) {
858 				/* F6FBB - Remove an existing unused route */
859 				rose_remove_route(rose_route);
860 				break;
861 			} else if (rose_route->neigh2 != NULL) {
862 				skb->data[0] &= 0xF0;
863 				skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
864 				skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
865 				rose_transmit_link(skb, rose_route->neigh2);
866 				if (frametype == ROSE_CLEAR_CONFIRMATION)
867 					rose_remove_route(rose_route);
868 				return 1;
869 			} else {
870 				if (frametype == ROSE_CLEAR_CONFIRMATION)
871 					rose_remove_route(rose_route);
872 				return 0;
873 			}
874 		}
875 		if (rose_route->lci2 == lci && rose_route->neigh2 == rose_neigh) {
876 			if (frametype == ROSE_CALL_REQUEST) {
877 				/* F6FBB - Remove an existing unused route */
878 				rose_remove_route(rose_route);
879 				break;
880 			} else if (rose_route->neigh1 != NULL) {
881 				skb->data[0] &= 0xF0;
882 				skb->data[0] |= (rose_route->lci1 >> 8) & 0x0F;
883 				skb->data[1]  = (rose_route->lci1 >> 0) & 0xFF;
884 				rose_transmit_link(skb, rose_route->neigh1);
885 				if (frametype == ROSE_CLEAR_CONFIRMATION)
886 					rose_remove_route(rose_route);
887 				return 1;
888 			} else {
889 				if (frametype == ROSE_CLEAR_CONFIRMATION)
890 					rose_remove_route(rose_route);
891 				return 0;
892 			}
893 		}
894 	}
895 
896 	/*
897 	 *	We know that:
898 	 *	1. The frame isn't for us,
899 	 *	2. It isn't "owned" by any existing route.
900 	 */
901 	if (frametype != ROSE_CALL_REQUEST)	/* XXX */
902 		return 0;
903 
904 	len  = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
905 	len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
906 
907 	memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
908 
909 	if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
910 		rose_transmit_clear_request(rose_neigh, lci, ROSE_INVALID_FACILITY, 76);
911 		return 0;
912 	}
913 
914 	/*
915 	 *	Check for routing loops.
916 	 */
917 	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next) {
918 		if (rose_route->rand == facilities.rand &&
919 		    rosecmp(src_addr, &rose_route->src_addr) == 0 &&
920 		    ax25cmp(&facilities.dest_call, &rose_route->src_call) == 0 &&
921 		    ax25cmp(&facilities.source_call, &rose_route->dest_call) == 0) {
922 			rose_transmit_clear_request(rose_neigh, lci, ROSE_NOT_OBTAINABLE, 120);
923 			return 0;
924 		}
925 	}
926 
927 	if ((new_neigh = rose_get_neigh(dest_addr, &cause, &diagnostic)) == NULL) {
928 		rose_transmit_clear_request(rose_neigh, lci, cause, diagnostic);
929 		return 0;
930 	}
931 
932 	if ((new_lci = rose_new_lci(new_neigh)) == 0) {
933 		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 71);
934 		return 0;
935 	}
936 
937 	if ((rose_route = kmalloc(sizeof(*rose_route), GFP_ATOMIC)) == NULL) {
938 		rose_transmit_clear_request(rose_neigh, lci, ROSE_NETWORK_CONGESTION, 120);
939 		return 0;
940 	}
941 
942 	rose_route->lci1      = lci;
943 	rose_route->src_addr  = *src_addr;
944 	rose_route->dest_addr = *dest_addr;
945 	rose_route->src_call  = facilities.dest_call;
946 	rose_route->dest_call = facilities.source_call;
947 	rose_route->rand      = facilities.rand;
948 	rose_route->neigh1    = rose_neigh;
949 	rose_route->lci2      = new_lci;
950 	rose_route->neigh2    = new_neigh;
951 
952 	rose_route->neigh1->use++;
953 	rose_route->neigh2->use++;
954 
955 	save_flags(flags); cli();
956 	rose_route->next = rose_route_list;
957 	rose_route_list  = rose_route;
958 	restore_flags(flags);
959 
960 	skb->data[0] &= 0xF0;
961 	skb->data[0] |= (rose_route->lci2 >> 8) & 0x0F;
962 	skb->data[1]  = (rose_route->lci2 >> 0) & 0xFF;
963 
964 	rose_transmit_link(skb, rose_route->neigh2);
965 
966 	return 1;
967 }
968 
rose_nodes_get_info(char * buffer,char ** start,off_t offset,int length)969 int rose_nodes_get_info(char *buffer, char **start, off_t offset, int length)
970 {
971 	struct rose_node *rose_node;
972 	int len     = 0;
973 	off_t pos   = 0;
974 	off_t begin = 0;
975 	int i;
976 
977 	cli();
978 
979 	len += sprintf(buffer, "address    mask n neigh neigh neigh\n");
980 
981 	for (rose_node = rose_node_list; rose_node != NULL; rose_node = rose_node->next) {
982 		/* if (rose_node->loopback) {
983 			len += sprintf(buffer + len, "%-10s %04d 1 loopback\n",
984 				rose2asc(&rose_node->address),
985 				rose_node->mask);
986 		} else { */
987 			len += sprintf(buffer + len, "%-10s %04d %d",
988 				rose2asc(&rose_node->address),
989 				rose_node->mask,
990 				rose_node->count);
991 
992 			for (i = 0; i < rose_node->count; i++)
993 				len += sprintf(buffer + len, " %05d",
994 					rose_node->neighbour[i]->number);
995 
996 			len += sprintf(buffer + len, "\n");
997 		/* } */
998 
999 		pos = begin + len;
1000 
1001 		if (pos < offset) {
1002 			len   = 0;
1003 			begin = pos;
1004 		}
1005 
1006 		if (pos > offset + length)
1007 			break;
1008 	}
1009 
1010 	sti();
1011 
1012 	*start = buffer + (offset - begin);
1013 	len   -= (offset - begin);
1014 
1015 	if (len > length) len = length;
1016 
1017 	return len;
1018 }
1019 
rose_neigh_get_info(char * buffer,char ** start,off_t offset,int length)1020 int rose_neigh_get_info(char *buffer, char **start, off_t offset, int length)
1021 {
1022 	struct rose_neigh *rose_neigh;
1023 	int len     = 0;
1024 	off_t pos   = 0;
1025 	off_t begin = 0;
1026 	int i;
1027 
1028 	cli();
1029 
1030 	len += sprintf(buffer, "addr  callsign  dev  count use mode restart  t0  tf digipeaters\n");
1031 
1032 	for (rose_neigh = rose_neigh_list; rose_neigh != NULL; rose_neigh = rose_neigh->next) {
1033 		/* if (!rose_neigh->loopback) { */
1034 			len += sprintf(buffer + len, "%05d %-9s %-4s   %3d %3d  %3s     %3s %3lu %3lu",
1035 				rose_neigh->number,
1036 				(rose_neigh->loopback) ? "RSLOOP-0" : ax2asc(&rose_neigh->callsign),
1037 				rose_neigh->dev ? rose_neigh->dev->name : "???",
1038 				rose_neigh->count,
1039 				rose_neigh->use,
1040 				(rose_neigh->dce_mode) ? "DCE" : "DTE",
1041 				(rose_neigh->restarted) ? "yes" : "no",
1042 				ax25_display_timer(&rose_neigh->t0timer) / HZ,
1043 				ax25_display_timer(&rose_neigh->ftimer)  / HZ);
1044 
1045 			if (rose_neigh->digipeat != NULL) {
1046 				for (i = 0; i < rose_neigh->digipeat->ndigi; i++)
1047 					len += sprintf(buffer + len, " %s", ax2asc(&rose_neigh->digipeat->calls[i]));
1048 			}
1049 
1050 			len += sprintf(buffer + len, "\n");
1051 
1052 			pos = begin + len;
1053 
1054 			if (pos < offset) {
1055 				len   = 0;
1056 				begin = pos;
1057 			}
1058 
1059 			if (pos > offset + length)
1060 				break;
1061 		/* } */
1062 	}
1063 
1064 	sti();
1065 
1066 	*start = buffer + (offset - begin);
1067 	len   -= (offset - begin);
1068 
1069 	if (len > length) len = length;
1070 
1071 	return len;
1072 }
1073 
rose_routes_get_info(char * buffer,char ** start,off_t offset,int length)1074 int rose_routes_get_info(char *buffer, char **start, off_t offset, int length)
1075 {
1076 	struct rose_route *rose_route;
1077 	int len     = 0;
1078 	off_t pos   = 0;
1079 	off_t begin = 0;
1080 
1081 	cli();
1082 
1083 	len += sprintf(buffer, "lci  address     callsign   neigh  <-> lci  address     callsign   neigh\n");
1084 
1085 	for (rose_route = rose_route_list; rose_route != NULL; rose_route = rose_route->next) {
1086 		if (rose_route->neigh1 != NULL) {
1087 			len += sprintf(buffer + len, "%3.3X  %-10s  %-9s  %05d      ",
1088 				rose_route->lci1,
1089 				rose2asc(&rose_route->src_addr),
1090 				ax2asc(&rose_route->src_call),
1091 				rose_route->neigh1->number);
1092 		} else {
1093 			len += sprintf(buffer + len, "000  *           *          00000      ");
1094 		}
1095 
1096 		if (rose_route->neigh2 != NULL) {
1097 			len += sprintf(buffer + len, "%3.3X  %-10s  %-9s  %05d\n",
1098 				rose_route->lci2,
1099 				rose2asc(&rose_route->dest_addr),
1100 				ax2asc(&rose_route->dest_call),
1101 				rose_route->neigh2->number);
1102 		} else {
1103 			len += sprintf(buffer + len, "000  *           *          00000\n");
1104 		}
1105 
1106 		pos = begin + len;
1107 
1108 		if (pos < offset) {
1109 			len   = 0;
1110 			begin = pos;
1111 		}
1112 
1113 		if (pos > offset + length)
1114 			break;
1115 	}
1116 
1117 	sti();
1118 
1119 	*start = buffer + (offset - begin);
1120 	len   -= (offset - begin);
1121 
1122 	if (len > length) len = length;
1123 
1124 	return len;
1125 }
1126 
1127 /*
1128  *	Release all memory associated with ROSE routing structures.
1129  */
rose_rt_free(void)1130 void __exit rose_rt_free(void)
1131 {
1132 	struct rose_neigh *s, *rose_neigh = rose_neigh_list;
1133 	struct rose_node  *t, *rose_node  = rose_node_list;
1134 	struct rose_route *u, *rose_route = rose_route_list;
1135 
1136 	while (rose_neigh != NULL) {
1137 		s          = rose_neigh;
1138 		rose_neigh = rose_neigh->next;
1139 
1140 		rose_remove_neigh(s);
1141 	}
1142 
1143 	while (rose_node != NULL) {
1144 		t         = rose_node;
1145 		rose_node = rose_node->next;
1146 
1147 		rose_remove_node(t);
1148 	}
1149 
1150 	while (rose_route != NULL) {
1151 		u          = rose_route;
1152 		rose_route = rose_route->next;
1153 
1154 		rose_remove_route(u);
1155 	}
1156 }
1157