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