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