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