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