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