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