Linux 4.19.168
[linux/fpc-iii.git] / net / netrom / nr_route.c
blob53ced34a1fdd212dbc2d6b31a5a9bf2a66b37737
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.
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)
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>
40 static unsigned int nr_neigh_no = 1;
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);
47 static struct nr_node *nr_node_get(ax25_address *callsign)
49 struct nr_node *found = NULL;
50 struct nr_node *nr_node;
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;
59 spin_unlock_bh(&nr_node_list_lock);
60 return found;
63 static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
64 struct net_device *dev)
66 struct nr_neigh *found = NULL;
67 struct nr_neigh *nr_neigh;
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;
77 spin_unlock_bh(&nr_neigh_list_lock);
78 return found;
81 static void nr_remove_neigh(struct nr_neigh *);
83 /* re-sort the routes in quality order. */
84 static void re_sort_routes(struct nr_node *nr_node, int x, int y)
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;
92 swap(nr_node->routes[x], nr_node->routes[y]);
97 * Add a new route to a node, and in the process add the node and the
98 * neighbour if it is new.
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)
104 struct nr_node *nr_node;
105 struct nr_neigh *nr_neigh;
106 int i, found;
107 struct net_device *odev;
109 if ((odev=nr_dev_get(nr)) != NULL) { /* Can't add routes to ourself */
110 dev_put(odev);
111 return -EINVAL;
114 nr_node = nr_node_get(nr);
116 nr_neigh = nr_neigh_get_dev(ax25, dev);
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).
124 if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
125 struct nr_node *nr_nodet;
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);
136 spin_unlock_bh(&nr_node_list_lock);
139 if (nr_neigh != NULL)
140 nr_neigh->failed = 0;
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;
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;
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);
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;
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);
184 if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
185 nr_neigh->quality = quality;
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;
194 nr_node->callsign = *nr;
195 strcpy(nr_node->mnemonic, mnemonic);
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);
202 nr_node->routes[0].quality = quality;
203 nr_node->routes[0].obs_count = obs_count;
204 nr_node->routes[0].neighbour = nr_neigh;
206 nr_neigh_hold(nr_neigh);
207 nr_neigh->count++;
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);
214 nr_neigh_put(nr_neigh);
215 return 0;
217 nr_node_lock(nr_node);
219 if (quality != 0)
220 strcpy(nr_node->mnemonic, mnemonic);
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;
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];
237 nr_node->routes[0].quality = quality;
238 nr_node->routes[0].obs_count = obs_count;
239 nr_node->routes[0].neighbour = nr_neigh;
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);
251 if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
252 nr_remove_neigh(nr_node->routes[2].neighbour);
254 nr_node->routes[2].quality = quality;
255 nr_node->routes[2].obs_count = obs_count;
256 nr_node->routes[2].neighbour = nr_neigh;
258 nr_neigh_hold(nr_neigh);
259 nr_neigh->count++;
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;
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;
284 nr_neigh_put(nr_neigh);
285 nr_node_unlock(nr_node);
286 nr_node_put(nr_node);
287 return 0;
290 static inline void __nr_remove_node(struct nr_node *nr_node)
292 hlist_del_init(&nr_node->node_node);
293 nr_node_put(nr_node);
296 #define nr_remove_node_locked(__node) \
297 __nr_remove_node(__node)
299 static void nr_remove_node(struct nr_node *nr_node)
301 spin_lock_bh(&nr_node_list_lock);
302 __nr_remove_node(nr_node);
303 spin_unlock_bh(&nr_node_list_lock);
306 static inline void __nr_remove_neigh(struct nr_neigh *nr_neigh)
308 hlist_del_init(&nr_neigh->neigh_node);
309 nr_neigh_put(nr_neigh);
312 #define nr_remove_neigh_locked(__neigh) \
313 __nr_remove_neigh(__neigh)
315 static void nr_remove_neigh(struct nr_neigh *nr_neigh)
317 spin_lock_bh(&nr_neigh_list_lock);
318 __nr_remove_neigh(nr_neigh);
319 spin_unlock_bh(&nr_neigh_list_lock);
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.
326 static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
328 struct nr_node *nr_node;
329 struct nr_neigh *nr_neigh;
330 int i;
332 nr_node = nr_node_get(callsign);
334 if (nr_node == NULL)
335 return -EINVAL;
337 nr_neigh = nr_neigh_get_dev(neighbour, dev);
339 if (nr_neigh == NULL) {
340 nr_node_put(nr_node);
341 return -EINVAL;
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);
350 if (nr_neigh->count == 0 && !nr_neigh->locked)
351 nr_remove_neigh(nr_neigh);
352 nr_neigh_put(nr_neigh);
354 nr_node->count--;
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;
368 nr_node_put(nr_node);
370 nr_node_unlock(nr_node);
372 return 0;
375 nr_neigh_put(nr_neigh);
376 nr_node_unlock(nr_node);
377 nr_node_put(nr_node);
379 return -EINVAL;
383 * Lock a neighbour with a quality.
385 static int __must_check nr_add_neigh(ax25_address *callsign,
386 ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
388 struct nr_neigh *nr_neigh;
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;
398 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
399 return -ENOMEM;
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);
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;
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);
426 return 0;
430 * "Delete" a neighbour. The neighbour is only removed if the number
431 * of nodes that may use it is zero.
433 static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
435 struct nr_neigh *nr_neigh;
437 nr_neigh = nr_neigh_get_dev(callsign, dev);
439 if (nr_neigh == NULL) return -EINVAL;
441 nr_neigh->quality = quality;
442 nr_neigh->locked = 0;
444 if (nr_neigh->count == 0)
445 nr_remove_neigh(nr_neigh);
446 nr_neigh_put(nr_neigh);
448 return 0;
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.
456 static int nr_dec_obs(void)
458 struct nr_neigh *nr_neigh;
459 struct nr_node *s;
460 struct hlist_node *nodet;
461 int i;
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;
471 case 1: /* From 1 -> 0 */
472 nr_neigh = s->routes[i].neighbour;
474 nr_neigh->count--;
475 nr_neigh_put(nr_neigh);
477 if (nr_neigh->count == 0 && !nr_neigh->locked)
478 nr_remove_neigh(nr_neigh);
480 s->count--;
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;
491 break;
493 default:
494 s->routes[i].obs_count--;
495 break;
500 if (s->count <= 0)
501 nr_remove_node_locked(s);
502 nr_node_unlock(s);
504 spin_unlock_bh(&nr_node_list_lock);
506 return 0;
510 * A device has been removed. Remove its routes and neighbours.
512 void nr_rt_device_down(struct net_device *dev)
514 struct nr_neigh *s;
515 struct hlist_node *nodet, *node2t;
516 struct nr_node *t;
517 int i;
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--;
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;
541 if (t->count <= 0)
542 nr_remove_node_locked(t);
543 nr_node_unlock(t);
545 spin_unlock_bh(&nr_node_list_lock);
547 nr_remove_neigh_locked(s);
550 spin_unlock_bh(&nr_neigh_list_lock);
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.
557 static struct net_device *nr_ax25_dev_get(char *devname)
559 struct net_device *dev;
561 if ((dev = dev_get_by_name(&init_net, devname)) == NULL)
562 return NULL;
564 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
565 return dev;
567 dev_put(dev);
568 return NULL;
572 * Find the first active NET/ROM device, usually "nr0".
574 struct net_device *nr_dev_first(void)
576 struct net_device *dev, *first = NULL;
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;
584 if (first)
585 dev_hold(first);
586 rcu_read_unlock();
588 return first;
592 * Find the NET/ROM device for the given callsign.
594 struct net_device *nr_dev_get(ax25_address *addr)
596 struct net_device *dev;
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;
606 dev = NULL;
607 out:
608 rcu_read_unlock();
609 return dev;
612 static ax25_digi *nr_call_to_digi(ax25_digi *digi, int ndigis,
613 ax25_address *digipeaters)
615 int i;
617 if (ndigis == 0)
618 return NULL;
620 for (i = 0; i < ndigis; i++) {
621 digi->calls[i] = digipeaters[i];
622 digi->repeated[i] = 0;
625 digi->ndigi = ndigis;
626 digi->lastrepeat = -1;
628 return digi;
632 * Handle the ioctls that control the routing functions.
634 int nr_rt_ioctl(unsigned int cmd, void __user *arg)
636 struct nr_route_struct nr_route;
637 struct net_device *dev;
638 ax25_digi digi;
639 int ret;
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;
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;
673 dev_put(dev);
674 return ret;
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;
693 dev_put(dev);
694 return ret;
696 case SIOCNRDECOBS:
697 return nr_dec_obs();
699 default:
700 return -EINVAL;
703 return 0;
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.
710 void nr_link_failed(ax25_cb *ax25, int reason)
712 struct nr_neigh *s, *nr_neigh = NULL;
713 struct nr_node *nr_node = NULL;
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;
723 spin_unlock_bh(&nr_neigh_list_lock);
725 if (nr_neigh == NULL)
726 return;
728 nr_neigh->ax25 = NULL;
729 ax25_cb_put(ax25);
731 if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
732 nr_neigh_put(nr_neigh);
733 return;
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);
743 spin_unlock_bh(&nr_node_list_lock);
744 nr_neigh_put(nr_neigh);
748 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
749 * indicates an internally generated frame.
751 int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
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;
763 nr_src = (ax25_address *)(skb->data + 0);
764 nr_dest = (ax25_address *)(skb->data + 7);
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;
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;
783 if (!sysctl_netrom_routing_control && ax25 != NULL)
784 return 0;
786 /* Its Time-To-Live has expired */
787 if (skb->data[14] == 1) {
788 return 0;
791 nr_node = nr_node_get(nr_dest);
792 if (nr_node == NULL)
793 return 0;
794 nr_node_lock(nr_node);
796 if (nr_node->which >= nr_node->count) {
797 nr_node_unlock(nr_node);
798 nr_node_put(nr_node);
799 return 0;
802 nr_neigh = nr_node->routes[nr_node->which].neighbour;
804 if ((dev = nr_dev_first()) == NULL) {
805 nr_node_unlock(nr_node);
806 nr_node_put(nr_node);
807 return 0;
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;
819 kfree_skb(skb);
820 skb=skbn;
821 skb->data[14]--;
823 dptr = skb_push(skb, 1);
824 *dptr = AX25_P_NETROM;
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);
834 dev_put(dev);
835 ret = (nr_neigh->ax25 != NULL);
836 nr_node_unlock(nr_node);
837 nr_node_put(nr_node);
839 return ret;
842 #ifdef CONFIG_PROC_FS
844 static void *nr_node_start(struct seq_file *seq, loff_t *pos)
846 spin_lock_bh(&nr_node_list_lock);
847 return seq_hlist_start_head(&nr_node_list, *pos);
850 static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
852 return seq_hlist_next(v, &nr_node_list, pos);
855 static void nr_node_stop(struct seq_file *seq, void *v)
857 spin_unlock_bh(&nr_node_list_lock);
860 static int nr_node_show(struct seq_file *seq, void *v)
862 char buf[11];
863 int i;
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);
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);
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);
885 nr_node_unlock(nr_node);
887 seq_puts(seq, "\n");
889 return 0;
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,
899 static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
901 spin_lock_bh(&nr_neigh_list_lock);
902 return seq_hlist_start_head(&nr_neigh_list, *pos);
905 static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
907 return seq_hlist_next(v, &nr_neigh_list, pos);
910 static void nr_neigh_stop(struct seq_file *seq, void *v)
912 spin_unlock_bh(&nr_neigh_list_lock);
915 static int nr_neigh_show(struct seq_file *seq, void *v)
917 char buf[11];
918 int i;
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;
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);
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]));
941 seq_puts(seq, "\n");
943 return 0;
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,
952 #endif
955 * Free all memory associated with the nodes and routes lists.
957 void nr_rt_free(void)
959 struct nr_neigh *s = NULL;
960 struct nr_node *t = NULL;
961 struct hlist_node *nodet;
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);
970 nr_neigh_for_each_safe(s, nodet, &nr_neigh_list) {
971 while(s->count) {
972 s->count--;
973 nr_neigh_put(s);
975 nr_remove_neigh_locked(s);
977 spin_unlock_bh(&nr_node_list_lock);
978 spin_unlock_bh(&nr_neigh_list_lock);