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