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