patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / netrom / nr_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 Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8  * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9  * Copyright Tomi Manninen OH2BNS (oh2bns@sral.fi)
10  */
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.h>
14 #include <linux/in.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/sockios.h>
20 #include <linux/net.h>
21 #include <net/ax25.h>
22 #include <linux/inet.h>
23 #include <linux/netdevice.h>
24 #include <net/arp.h>
25 #include <linux/if_arp.h>
26 #include <linux/skbuff.h>
27 #include <net/sock.h>
28 #include <asm/uaccess.h>
29 #include <asm/system.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 <linux/spinlock.h>
38 #include <net/netrom.h>
39 #include <linux/seq_file.h>
40
41 static unsigned int nr_neigh_no = 1;
42
43 static HLIST_HEAD(nr_node_list);
44 static spinlock_t nr_node_list_lock = SPIN_LOCK_UNLOCKED;
45 static HLIST_HEAD(nr_neigh_list);
46 static spinlock_t nr_neigh_list_lock = SPIN_LOCK_UNLOCKED;
47
48 struct nr_node *nr_node_get(ax25_address *callsign)
49 {
50         struct nr_node *found = NULL;
51         struct nr_node *nr_node;
52         struct hlist_node *node;
53
54         spin_lock_bh(&nr_node_list_lock);
55         nr_node_for_each(nr_node, node, &nr_node_list)
56                 if (ax25cmp(callsign, &nr_node->callsign) == 0) {
57                         nr_node_hold(nr_node);
58                         found = nr_node;
59                         break;
60                 }
61         spin_unlock_bh(&nr_node_list_lock);
62         return found;
63 }
64
65 struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign, struct net_device *dev)
66 {
67         struct nr_neigh *found = NULL;
68         struct nr_neigh *nr_neigh;
69         struct hlist_node *node;
70
71         spin_lock_bh(&nr_neigh_list_lock);
72         nr_neigh_for_each(nr_neigh, node, &nr_neigh_list)
73                 if (ax25cmp(callsign, &nr_neigh->callsign) == 0 &&
74                     nr_neigh->dev == dev) {
75                         nr_neigh_hold(nr_neigh);
76                         found = nr_neigh;
77                         break;
78                 }
79         spin_unlock_bh(&nr_neigh_list_lock);
80         return found;
81 }
82
83 static void nr_remove_neigh(struct nr_neigh *);
84
85 /*
86  *      Add a new route to a node, and in the process add the node and the
87  *      neighbour if it is new.
88  */
89 static int nr_add_node(ax25_address *nr, const char *mnemonic, ax25_address *ax25,
90         ax25_digi *ax25_digi, struct net_device *dev, int quality, int obs_count)
91 {
92         struct nr_node  *nr_node;
93         struct nr_neigh *nr_neigh;
94         struct nr_route nr_route;
95         int i, found;
96         struct net_device *odev;
97
98         if ((odev=nr_dev_get(nr)) != NULL) {    /* Can't add routes to ourself */
99                 dev_put(odev);
100                 return -EINVAL;
101         }
102
103         nr_node = nr_node_get(nr);
104
105         nr_neigh = nr_neigh_get_dev(ax25, dev);
106
107         /*
108          * The L2 link to a neighbour has failed in the past
109          * and now a frame comes from this neighbour. We assume
110          * it was a temporary trouble with the link and reset the
111          * routes now (and not wait for a node broadcast).
112          */
113         if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
114                 struct nr_node *nr_nodet;
115                 struct hlist_node *node;
116
117                 spin_lock_bh(&nr_node_list_lock);
118                 nr_node_for_each(nr_nodet, node, &nr_node_list) {
119                         nr_node_lock(nr_nodet);
120                         for (i = 0; i < nr_nodet->count; i++)
121                                 if (nr_nodet->routes[i].neighbour == nr_neigh)
122                                         if (i < nr_nodet->which)
123                                                 nr_nodet->which = i;
124                         nr_node_unlock(nr_nodet);
125                 }
126                 spin_unlock_bh(&nr_node_list_lock);
127         }
128
129         if (nr_neigh != NULL)
130                 nr_neigh->failed = 0;
131
132         if (quality == 0 && nr_neigh != NULL && nr_node != NULL) {
133                 nr_neigh_put(nr_neigh);
134                 nr_node_put(nr_node);
135                 return 0;
136         }
137
138         if (nr_neigh == NULL) {
139                 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL) {
140                         if (nr_node)
141                                 nr_node_put(nr_node);
142                         return -ENOMEM;
143                 }
144
145                 nr_neigh->callsign = *ax25;
146                 nr_neigh->digipeat = NULL;
147                 nr_neigh->ax25     = NULL;
148                 nr_neigh->dev      = dev;
149                 nr_neigh->quality  = sysctl_netrom_default_path_quality;
150                 nr_neigh->locked   = 0;
151                 nr_neigh->count    = 0;
152                 nr_neigh->number   = nr_neigh_no++;
153                 nr_neigh->failed   = 0;
154                 atomic_set(&nr_neigh->refcount, 1);
155
156                 if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
157                         if ((nr_neigh->digipeat = kmalloc(sizeof(*ax25_digi), GFP_KERNEL)) == NULL) {
158                                 kfree(nr_neigh);
159                                 if (nr_node)
160                                         nr_node_put(nr_node);
161                                 return -ENOMEM;
162                         }
163                         memcpy(nr_neigh->digipeat, ax25_digi,
164                                         sizeof(*ax25_digi));
165                 }
166
167                 spin_lock_bh(&nr_neigh_list_lock);
168                 hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
169                 nr_neigh_hold(nr_neigh);
170                 spin_unlock_bh(&nr_neigh_list_lock);
171         }
172
173         if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
174                 nr_neigh->quality = quality;
175
176         if (nr_node == NULL) {
177                 if ((nr_node = kmalloc(sizeof(*nr_node), GFP_ATOMIC)) == NULL) {
178                         if (nr_neigh)
179                                 nr_neigh_put(nr_neigh);
180                         return -ENOMEM;
181                 }
182
183                 nr_node->callsign = *nr;
184                 strcpy(nr_node->mnemonic, mnemonic);
185
186                 nr_node->which = 0;
187                 nr_node->count = 1;
188                 atomic_set(&nr_node->refcount, 1);
189                 nr_node->node_lock = SPIN_LOCK_UNLOCKED;
190
191                 nr_node->routes[0].quality   = quality;
192                 nr_node->routes[0].obs_count = obs_count;
193                 nr_node->routes[0].neighbour = nr_neigh;
194
195                 nr_neigh_hold(nr_neigh);
196                 nr_neigh->count++;
197
198                 spin_lock_bh(&nr_node_list_lock);
199                 hlist_add_head(&nr_node->node_node, &nr_node_list);
200                 /* refcount initialized at 1 */
201                 spin_unlock_bh(&nr_node_list_lock);
202
203                 return 0;
204         }
205         nr_node_lock(nr_node);
206
207         if (quality != 0)
208                 strcpy(nr_node->mnemonic, mnemonic);
209
210         for (found = 0, i = 0; i < nr_node->count; i++) {
211                 if (nr_node->routes[i].neighbour == nr_neigh) {
212                         nr_node->routes[i].quality   = quality;
213                         nr_node->routes[i].obs_count = obs_count;
214                         found = 1;
215                         break;
216                 }
217         }
218
219         if (!found) {
220                 /* We have space at the bottom, slot it in */
221                 if (nr_node->count < 3) {
222                         nr_node->routes[2] = nr_node->routes[1];
223                         nr_node->routes[1] = nr_node->routes[0];
224
225                         nr_node->routes[0].quality   = quality;
226                         nr_node->routes[0].obs_count = obs_count;
227                         nr_node->routes[0].neighbour = nr_neigh;
228
229                         nr_node->which++;
230                         nr_node->count++;
231                         nr_neigh_hold(nr_neigh);
232                         nr_neigh->count++;
233                 } else {
234                         /* It must be better than the worst */
235                         if (quality > nr_node->routes[2].quality) {
236                                 nr_node->routes[2].neighbour->count--;
237                                 nr_neigh_put(nr_node->routes[2].neighbour);
238
239                                 if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
240                                         nr_remove_neigh(nr_node->routes[2].neighbour);
241
242                                 nr_node->routes[2].quality   = quality;
243                                 nr_node->routes[2].obs_count = obs_count;
244                                 nr_node->routes[2].neighbour = nr_neigh;
245
246                                 nr_neigh_hold(nr_neigh);
247                                 nr_neigh->count++;
248                         }
249                 }
250         }
251
252         /* Now re-sort the routes in quality order */
253         switch (nr_node->count) {
254         case 3:
255                 if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
256                         switch (nr_node->which) {
257                                 case 0:  nr_node->which = 1; break;
258                                 case 1:  nr_node->which = 0; break;
259                                 default: break;
260                         }
261                         nr_route           = nr_node->routes[0];
262                         nr_node->routes[0] = nr_node->routes[1];
263                         nr_node->routes[1] = nr_route;
264                 }
265                 if (nr_node->routes[2].quality > nr_node->routes[1].quality) {
266                         switch (nr_node->which) {
267                         case 1:  nr_node->which = 2;
268                                 break;
269
270                         case 2:  nr_node->which = 1;
271                                 break;
272
273                         default:
274                                 break;
275                         }
276                         nr_route           = nr_node->routes[1];
277                         nr_node->routes[1] = nr_node->routes[2];
278                         nr_node->routes[2] = nr_route;
279                 }
280         case 2:
281                 if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
282                         switch (nr_node->which) {
283                         case 0:  nr_node->which = 1;
284                                 break;
285
286                         case 1:  nr_node->which = 0;
287                                 break;
288
289                         default: break;
290                         }
291                         nr_route           = nr_node->routes[0];
292                         nr_node->routes[0] = nr_node->routes[1];
293                         nr_node->routes[1] = nr_route;
294                         }
295         case 1:
296                 break;
297         }
298
299         for (i = 0; i < nr_node->count; i++) {
300                 if (nr_node->routes[i].neighbour == nr_neigh) {
301                         if (i < nr_node->which)
302                                 nr_node->which = i;
303                         break;
304                 }
305         }
306
307         nr_neigh_put(nr_neigh);
308         nr_node_unlock(nr_node);
309         nr_node_put(nr_node);
310         return 0;
311 }
312
313 static inline void __nr_remove_node(struct nr_node *nr_node)
314 {
315         hlist_del_init(&nr_node->node_node);
316         nr_node_put(nr_node);
317 }
318
319 #define nr_remove_node_locked(__node) \
320         __nr_remove_node(__node)
321
322 static void nr_remove_node(struct nr_node *nr_node)
323 {
324         spin_lock_bh(&nr_node_list_lock);
325         __nr_remove_node(nr_node);
326         spin_unlock_bh(&nr_node_list_lock);
327 }
328
329 static inline void __nr_remove_neigh(struct nr_neigh *nr_neigh)
330 {
331         hlist_del_init(&nr_neigh->neigh_node);
332         nr_neigh_put(nr_neigh);
333 }
334
335 #define nr_remove_neigh_locked(__neigh) \
336         __nr_remove_neigh(__neigh)
337
338 static void nr_remove_neigh(struct nr_neigh *nr_neigh)
339 {
340         spin_lock_bh(&nr_neigh_list_lock);
341         __nr_remove_neigh(nr_neigh);
342         spin_unlock_bh(&nr_neigh_list_lock);
343 }
344
345 /*
346  *      "Delete" a node. Strictly speaking remove a route to a node. The node
347  *      is only deleted if no routes are left to it.
348  */
349 static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
350 {
351         struct nr_node  *nr_node;
352         struct nr_neigh *nr_neigh;
353         int i;
354
355         nr_node = nr_node_get(callsign);
356
357         if (nr_node == NULL)
358                 return -EINVAL;
359
360         nr_neigh = nr_neigh_get_dev(neighbour, dev);
361
362         if (nr_neigh == NULL) {
363                 nr_node_put(nr_node);
364                 return -EINVAL;
365         }
366
367         nr_node_lock(nr_node);
368         for (i = 0; i < nr_node->count; i++) {
369                 if (nr_node->routes[i].neighbour == nr_neigh) {
370                         nr_neigh->count--;
371                         nr_neigh_put(nr_neigh);
372
373                         if (nr_neigh->count == 0 && !nr_neigh->locked)
374                                 nr_remove_neigh(nr_neigh);
375                         nr_neigh_put(nr_neigh);
376
377                         nr_node->count--;
378
379                         if (nr_node->count == 0) {
380                                 nr_remove_node(nr_node);
381                         } else {
382                                 switch (i) {
383                                 case 0:
384                                         nr_node->routes[0] = nr_node->routes[1];
385                                 case 1:
386                                         nr_node->routes[1] = nr_node->routes[2];
387                                 case 2:
388                                         break;
389                                 }
390                                 nr_node_put(nr_node);
391                         }
392                         nr_node_unlock(nr_node);
393
394                         return 0;
395                 }
396         }
397         nr_neigh_put(nr_neigh);
398         nr_node_unlock(nr_node);
399         nr_node_put(nr_node);
400
401         return -EINVAL;
402 }
403
404 /*
405  *      Lock a neighbour with a quality.
406  */
407 static int nr_add_neigh(ax25_address *callsign, ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
408 {
409         struct nr_neigh *nr_neigh;
410
411         nr_neigh = nr_neigh_get_dev(callsign, dev);
412         if (nr_neigh) {
413                 nr_neigh->quality = quality;
414                 nr_neigh->locked  = 1;
415                 nr_neigh_put(nr_neigh);
416                 return 0;
417         }
418
419         if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
420                 return -ENOMEM;
421
422         nr_neigh->callsign = *callsign;
423         nr_neigh->digipeat = NULL;
424         nr_neigh->ax25     = NULL;
425         nr_neigh->dev      = dev;
426         nr_neigh->quality  = quality;
427         nr_neigh->locked   = 1;
428         nr_neigh->count    = 0;
429         nr_neigh->number   = nr_neigh_no++;
430         nr_neigh->failed   = 0;
431         atomic_set(&nr_neigh->refcount, 1);
432
433         if (ax25_digi != NULL && ax25_digi->ndigi > 0) {
434                 if ((nr_neigh->digipeat = kmalloc(sizeof(*ax25_digi), GFP_KERNEL)) == NULL) {
435                         kfree(nr_neigh);
436                         return -ENOMEM;
437                 }
438                 memcpy(nr_neigh->digipeat, ax25_digi, sizeof(*ax25_digi));
439         }
440
441         spin_lock_bh(&nr_neigh_list_lock);
442         hlist_add_head(&nr_neigh->neigh_node, &nr_neigh_list);
443         /* refcount is initialized at 1 */
444         spin_unlock_bh(&nr_neigh_list_lock);
445
446         return 0;
447 }
448
449 /*
450  *      "Delete" a neighbour. The neighbour is only removed if the number
451  *      of nodes that may use it is zero.
452  */
453 static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
454 {
455         struct nr_neigh *nr_neigh;
456
457         nr_neigh = nr_neigh_get_dev(callsign, dev);
458
459         if (nr_neigh == NULL) return -EINVAL;
460
461         nr_neigh->quality = quality;
462         nr_neigh->locked  = 0;
463
464         if (nr_neigh->count == 0)
465                 nr_remove_neigh(nr_neigh);
466         nr_neigh_put(nr_neigh);
467
468         return 0;
469 }
470
471 /*
472  *      Decrement the obsolescence count by one. If a route is reduced to a
473  *      count of zero, remove it. Also remove any unlocked neighbours with
474  *      zero nodes routing via it.
475  */
476 static int nr_dec_obs(void)
477 {
478         struct nr_neigh *nr_neigh;
479         struct nr_node  *s;
480         struct hlist_node *node, *nodet;
481         int i;
482
483         spin_lock_bh(&nr_node_list_lock);
484         nr_node_for_each_safe(s, node, nodet, &nr_node_list) {
485                 nr_node_lock(s);
486                 for (i = 0; i < s->count; i++) {
487                         switch (s->routes[i].obs_count) {
488                         case 0:         /* A locked entry */
489                                 break;
490
491                         case 1:         /* From 1 -> 0 */
492                                 nr_neigh = s->routes[i].neighbour;
493
494                                 nr_neigh->count--;
495                                 nr_neigh_put(nr_neigh);
496
497                                 if (nr_neigh->count == 0 && !nr_neigh->locked)
498                                         nr_remove_neigh(nr_neigh);
499
500                                 s->count--;
501
502                                 switch (i) {
503                                         case 0:
504                                                 s->routes[0] = s->routes[1];
505                                         case 1:
506                                                 s->routes[1] = s->routes[2];
507                                         case 2:
508                                                 break;
509                                 }
510                                 break;
511
512                         default:
513                                 s->routes[i].obs_count--;
514                                 break;
515
516                         }
517                 }
518
519                 if (s->count <= 0)
520                         nr_remove_node_locked(s);
521                 nr_node_unlock(s);
522         }
523         spin_unlock_bh(&nr_node_list_lock);
524
525         return 0;
526 }
527
528 /*
529  *      A device has been removed. Remove its routes and neighbours.
530  */
531 void nr_rt_device_down(struct net_device *dev)
532 {
533         struct nr_neigh *s;
534         struct hlist_node *node, *nodet, *node2, *node2t;
535         struct nr_node  *t;
536         int i;
537
538         spin_lock_bh(&nr_neigh_list_lock);
539         nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
540                 if (s->dev == dev) {
541                         spin_lock_bh(&nr_node_list_lock);
542                         nr_node_for_each_safe(t, node2, node2t, &nr_node_list) {
543                                 nr_node_lock(t);
544                                 for (i = 0; i < t->count; i++) {
545                                         if (t->routes[i].neighbour == s) {
546                                                 t->count--;
547
548                                                 switch (i) {
549                                                 case 0:
550                                                         t->routes[0] = t->routes[1];
551                                                 case 1:
552                                                         t->routes[1] = t->routes[2];
553                                                 case 2:
554                                                         break;
555                                                 }
556                                         }
557                                 }
558
559                                 if (t->count <= 0)
560                                         nr_remove_node_locked(t);
561                                 nr_node_unlock(t);
562                         }
563                         spin_unlock_bh(&nr_node_list_lock);
564
565                         nr_remove_neigh_locked(s);
566                 }
567         }
568         spin_unlock_bh(&nr_neigh_list_lock);
569 }
570
571 /*
572  *      Check that the device given is a valid AX.25 interface that is "up".
573  *      Or a valid ethernet interface with an AX.25 callsign binding.
574  */
575 static struct net_device *nr_ax25_dev_get(char *devname)
576 {
577         struct net_device *dev;
578
579         if ((dev = dev_get_by_name(devname)) == NULL)
580                 return NULL;
581
582         if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
583                 return dev;
584
585         dev_put(dev);
586         return NULL;
587 }
588
589 /*
590  *      Find the first active NET/ROM device, usually "nr0".
591  */
592 struct net_device *nr_dev_first(void)
593 {
594         struct net_device *dev, *first = NULL;
595
596         read_lock(&dev_base_lock);
597         for (dev = dev_base; dev != NULL; dev = dev->next) {
598                 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM)
599                         if (first == NULL || strncmp(dev->name, first->name, 3) < 0)
600                                 first = dev;
601         }
602         if (first)
603                 dev_hold(first);
604         read_unlock(&dev_base_lock);
605
606         return first;
607 }
608
609 /*
610  *      Find the NET/ROM device for the given callsign.
611  */
612 struct net_device *nr_dev_get(ax25_address *addr)
613 {
614         struct net_device *dev;
615
616         read_lock(&dev_base_lock);
617         for (dev = dev_base; dev != NULL; dev = dev->next) {
618                 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_NETROM && ax25cmp(addr, (ax25_address *)dev->dev_addr) == 0) {
619                         dev_hold(dev);
620                         goto out;
621                 }
622         }
623 out:
624         read_unlock(&dev_base_lock);
625         return dev;
626 }
627
628 static ax25_digi *nr_call_to_digi(int ndigis, ax25_address *digipeaters)
629 {
630         static ax25_digi ax25_digi;
631         int i;
632
633         if (ndigis == 0)
634                 return NULL;
635
636         for (i = 0; i < ndigis; i++) {
637                 ax25_digi.calls[i]    = digipeaters[i];
638                 ax25_digi.repeated[i] = 0;
639         }
640
641         ax25_digi.ndigi      = ndigis;
642         ax25_digi.lastrepeat = -1;
643
644         return &ax25_digi;
645 }
646
647 /*
648  *      Handle the ioctls that control the routing functions.
649  */
650 int nr_rt_ioctl(unsigned int cmd, void __user *arg)
651 {
652         struct nr_route_struct nr_route;
653         struct net_device *dev;
654         int ret;
655
656         switch (cmd) {
657         case SIOCADDRT:
658                 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
659                         return -EFAULT;
660                 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
661                         return -EINVAL;
662                 if (nr_route.ndigis < 0 || nr_route.ndigis > AX25_MAX_DIGIS) {
663                         dev_put(dev);
664                         return -EINVAL;
665                 }
666                 switch (nr_route.type) {
667                 case NETROM_NODE:
668                         ret = nr_add_node(&nr_route.callsign,
669                                 nr_route.mnemonic,
670                                 &nr_route.neighbour,
671                                 nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
672                                 dev, nr_route.quality,
673                                 nr_route.obs_count);
674                         break;
675                 case NETROM_NEIGH:
676                         ret = nr_add_neigh(&nr_route.callsign,
677                                 nr_call_to_digi(nr_route.ndigis, nr_route.digipeaters),
678                                 dev, nr_route.quality);
679                         break;
680                 default:
681                         ret = -EINVAL;
682                 }
683                 dev_put(dev);
684                 return ret;
685
686         case SIOCDELRT:
687                 if (copy_from_user(&nr_route, arg, sizeof(struct nr_route_struct)))
688                         return -EFAULT;
689                 if ((dev = nr_ax25_dev_get(nr_route.device)) == NULL)
690                         return -EINVAL;
691                 switch (nr_route.type) {
692                 case NETROM_NODE:
693                         ret = nr_del_node(&nr_route.callsign,
694                                 &nr_route.neighbour, dev);
695                         break;
696                 case NETROM_NEIGH:
697                         ret = nr_del_neigh(&nr_route.callsign,
698                                 dev, nr_route.quality);
699                         break;
700                 default:
701                         ret = -EINVAL;
702                 }
703                 dev_put(dev);
704                 return ret;
705
706         case SIOCNRDECOBS:
707                 return nr_dec_obs();
708
709         default:
710                 return -EINVAL;
711         }
712
713         return 0;
714 }
715
716 /*
717  *      A level 2 link has timed out, therefore it appears to be a poor link,
718  *      then don't use that neighbour until it is reset.
719  */
720 void nr_link_failed(ax25_cb *ax25, int reason)
721 {
722         struct nr_neigh *s, *nr_neigh = NULL;
723         struct hlist_node *node;
724         struct nr_node  *nr_node = NULL;
725
726         spin_lock_bh(&nr_neigh_list_lock);
727         nr_neigh_for_each(s, node, &nr_neigh_list)
728                 if (s->ax25 == ax25) {
729                         nr_neigh_hold(s);
730                         nr_neigh = s;
731                         break;
732                 }
733         spin_unlock_bh(&nr_neigh_list_lock);
734
735         if (nr_neigh == NULL) return;
736
737         nr_neigh->ax25 = NULL;
738         ax25_cb_put(ax25);
739
740         if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
741                 nr_neigh_put(nr_neigh);
742                 return;
743         }
744         spin_lock_bh(&nr_node_list_lock);
745         nr_node_for_each(nr_node, node, &nr_node_list)
746                 nr_node_lock(nr_node);
747                 if (nr_node->which < nr_node->count && nr_node->routes[nr_node->which].neighbour == nr_neigh)
748                         nr_node->which++;
749                 nr_node_unlock(nr_node);
750         spin_unlock_bh(&nr_node_list_lock);
751         nr_neigh_put(nr_neigh);
752 }
753
754 /*
755  *      Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
756  *      indicates an internally generated frame.
757  */
758 int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
759 {
760         ax25_address *nr_src, *nr_dest;
761         struct nr_neigh *nr_neigh;
762         struct nr_node  *nr_node;
763         struct net_device *dev;
764         unsigned char *dptr;
765         ax25_cb *ax25s;
766         int ret;
767         struct sk_buff *skbn;
768
769
770         nr_src  = (ax25_address *)(skb->data + 0);
771         nr_dest = (ax25_address *)(skb->data + 7);
772
773         if (ax25 != NULL)
774                 nr_add_node(nr_src, "", &ax25->dest_addr, ax25->digipeat,
775                             ax25->ax25_dev->dev, 0, sysctl_netrom_obsolescence_count_initialiser);
776
777         if ((dev = nr_dev_get(nr_dest)) != NULL) {      /* Its for me */
778                 if (ax25 == NULL)                       /* Its from me */
779                         ret = nr_loopback_queue(skb);
780                 else
781                         ret = nr_rx_frame(skb, dev);
782                 dev_put(dev);
783                 return ret;
784         }
785
786         if (!sysctl_netrom_routing_control && ax25 != NULL)
787                 return 0;
788
789         /* Its Time-To-Live has expired */
790         if (skb->data[14] == 1) {
791                 return 0;
792         }
793
794         nr_node = nr_node_get(nr_dest);
795         if (nr_node == NULL)
796                 return 0;
797         nr_node_lock(nr_node);
798
799         if (nr_node->which >= nr_node->count) {
800                 nr_node_unlock(nr_node);
801                 nr_node_put(nr_node);
802                 return 0;
803         }
804
805         nr_neigh = nr_node->routes[nr_node->which].neighbour;
806
807         if ((dev = nr_dev_first()) == NULL) {
808                 nr_node_unlock(nr_node);
809                 nr_node_put(nr_node);
810                 return 0;
811         }
812
813         /* We are going to change the netrom headers so we should get our
814            own skb, we also did not know until now how much header space
815            we had to reserve... - RXQ */
816         if ((skbn=skb_copy_expand(skb, dev->hard_header_len, 0, GFP_ATOMIC)) == NULL) {
817                 nr_node_unlock(nr_node);
818                 nr_node_put(nr_node);
819                 dev_put(dev);
820                 return 0;
821         }
822         kfree_skb(skb);
823         skb=skbn;
824         skb->data[14]--;
825
826         dptr  = skb_push(skb, 1);
827         *dptr = AX25_P_NETROM;
828
829         ax25s = ax25_send_frame(skb, 256, (ax25_address *)dev->dev_addr, &nr_neigh->callsign, nr_neigh->digipeat, nr_neigh->dev);
830         if (nr_neigh->ax25 && ax25s) {
831                 /* We were already holding this ax25_cb */
832                 ax25_cb_put(ax25s);
833         }
834         nr_neigh->ax25 = ax25s;
835
836         dev_put(dev);
837         ret = (nr_neigh->ax25 != NULL);
838         nr_node_unlock(nr_node);
839         nr_node_put(nr_node);
840         return ret;
841 }
842
843 #ifdef CONFIG_PROC_FS
844
845 static void *nr_node_start(struct seq_file *seq, loff_t *pos)
846 {
847         struct nr_node *nr_node;
848         struct hlist_node *node;
849         int i = 1;
850  
851         spin_lock_bh(&nr_node_list_lock);
852         if (*pos == 0)
853                 return SEQ_START_TOKEN;
854
855         nr_node_for_each(nr_node, node, &nr_node_list) {
856                 if (i == *pos)
857                         return nr_node;
858                 ++i;
859         }
860
861         return NULL;
862 }
863
864 static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
865 {
866         struct hlist_node *node;
867         ++*pos;
868         
869         node = (v == SEQ_START_TOKEN)  
870                 ? nr_node_list.first
871                 : ((struct nr_node *)v)->node_node.next;
872
873         return hlist_entry(node, struct nr_node, node_node);
874 }
875
876 static void nr_node_stop(struct seq_file *seq, void *v)
877 {
878         spin_unlock_bh(&nr_node_list_lock);
879 }
880
881 static int nr_node_show(struct seq_file *seq, void *v)
882 {
883         int i;
884
885         if (v == SEQ_START_TOKEN)
886                 seq_puts(seq,
887                          "callsign  mnemonic w n qual obs neigh qual obs neigh qual obs neigh\n");
888         else {
889                 struct nr_node *nr_node = v;
890                 nr_node_lock(nr_node);
891                 seq_printf(seq, "%-9s %-7s  %d %d",
892                         ax2asc(&nr_node->callsign),
893                         (nr_node->mnemonic[0] == '\0') ? "*" : nr_node->mnemonic,
894                         nr_node->which + 1,
895                         nr_node->count);
896
897                 for (i = 0; i < nr_node->count; i++) {
898                         seq_printf(seq, "  %3d   %d %05d",
899                                 nr_node->routes[i].quality,
900                                 nr_node->routes[i].obs_count,
901                                 nr_node->routes[i].neighbour->number);
902                 }
903                 nr_node_unlock(nr_node);
904
905                 seq_puts(seq, "\n");
906         }
907         return 0;
908 }
909
910 static struct seq_operations nr_node_seqops = {
911         .start = nr_node_start,
912         .next = nr_node_next,
913         .stop = nr_node_stop,
914         .show = nr_node_show,
915 };
916
917 static int nr_node_info_open(struct inode *inode, struct file *file)
918 {
919         return seq_open(file, &nr_node_seqops);
920 }
921
922 struct file_operations nr_nodes_fops = {
923         .owner = THIS_MODULE,
924         .open = nr_node_info_open,
925         .read = seq_read,
926         .llseek = seq_lseek,
927         .release = seq_release,
928 };
929
930 static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
931 {
932         struct nr_neigh *nr_neigh;
933         struct hlist_node *node;
934         int i = 1;
935
936         spin_lock_bh(&nr_neigh_list_lock);
937         if (*pos == 0)
938                 return SEQ_START_TOKEN;
939
940         nr_neigh_for_each(nr_neigh, node, &nr_neigh_list) {
941                 if (i == *pos)
942                         return nr_neigh;
943         }
944         return NULL;
945 }
946
947 static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
948 {
949         struct hlist_node *node;
950         ++*pos;
951         
952         node = (v == SEQ_START_TOKEN)  
953                 ? nr_neigh_list.first
954                 : ((struct nr_neigh *)v)->neigh_node.next;
955
956         return hlist_entry(node, struct nr_neigh, neigh_node);
957 }
958
959 static void nr_neigh_stop(struct seq_file *seq, void *v)
960 {
961         spin_unlock_bh(&nr_neigh_list_lock);
962 }
963
964 static int nr_neigh_show(struct seq_file *seq, void *v)
965 {
966         int i;
967
968         if (v == SEQ_START_TOKEN)
969                 seq_puts(seq, "addr  callsign  dev  qual lock count failed digipeaters\n");
970         else {
971                 struct nr_neigh *nr_neigh = v;
972
973                 seq_printf(seq, "%05d %-9s %-4s  %3d    %d   %3d    %3d",
974                         nr_neigh->number,
975                         ax2asc(&nr_neigh->callsign),
976                         nr_neigh->dev ? nr_neigh->dev->name : "???",
977                         nr_neigh->quality,
978                         nr_neigh->locked,
979                         nr_neigh->count,
980                         nr_neigh->failed);
981
982                 if (nr_neigh->digipeat != NULL) {
983                         for (i = 0; i < nr_neigh->digipeat->ndigi; i++)
984                                 seq_printf(seq, " %s", 
985                                            ax2asc(&nr_neigh->digipeat->calls[i]));
986                 }
987
988                 seq_puts(seq, "\n");
989         }
990         return 0;
991 }
992
993 static struct seq_operations nr_neigh_seqops = {
994         .start = nr_neigh_start,
995         .next = nr_neigh_next,
996         .stop = nr_neigh_stop,
997         .show = nr_neigh_show,
998 };
999
1000 static int nr_neigh_info_open(struct inode *inode, struct file *file)
1001 {
1002         return seq_open(file, &nr_neigh_seqops);
1003 }
1004
1005 struct file_operations nr_neigh_fops = {
1006         .owner = THIS_MODULE,
1007         .open = nr_neigh_info_open,
1008         .read = seq_read,
1009         .llseek = seq_lseek,
1010         .release = seq_release,
1011 };
1012
1013 #endif
1014
1015 /*
1016  *      Free all memory associated with the nodes and routes lists.
1017  */
1018 void __exit nr_rt_free(void)
1019 {
1020         struct nr_neigh *s = NULL;
1021         struct nr_node  *t = NULL;
1022         struct hlist_node *node, *nodet;
1023
1024         spin_lock_bh(&nr_neigh_list_lock);
1025         spin_lock_bh(&nr_node_list_lock);
1026         nr_node_for_each_safe(t, node, nodet, &nr_node_list) {
1027                 nr_node_lock(t);
1028                 nr_remove_node_locked(t);
1029                 nr_node_unlock(t);
1030         }
1031         nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
1032                 while(s->count) {
1033                         s->count--;
1034                         nr_neigh_put(s);
1035                 }
1036                 nr_remove_neigh_locked(s);
1037         }
1038         spin_unlock_bh(&nr_node_list_lock);
1039         spin_unlock_bh(&nr_neigh_list_lock);
1040 }