Linux 2.6.19-rc1
[linux-2.6/next.git] / net / netrom / nr_route.c
blobc11737f472d6a3a8ac24dd1dbb27f5c793f592e3
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/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>
41 static unsigned int nr_neigh_no = 1;
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);
48 static struct nr_node *nr_node_get(ax25_address *callsign)
50 struct nr_node *found = NULL;
51 struct nr_node *nr_node;
52 struct hlist_node *node;
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;
61 spin_unlock_bh(&nr_node_list_lock);
62 return found;
65 static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
66 struct net_device *dev)
68 struct nr_neigh *found = NULL;
69 struct nr_neigh *nr_neigh;
70 struct hlist_node *node;
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;
80 spin_unlock_bh(&nr_neigh_list_lock);
81 return found;
84 static void nr_remove_neigh(struct nr_neigh *);
87 * Add a new route to a node, and in the process add the node and the
88 * neighbour if it is new.
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)
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;
99 if ((odev=nr_dev_get(nr)) != NULL) { /* Can't add routes to ourself */
100 dev_put(odev);
101 return -EINVAL;
104 nr_node = nr_node_get(nr);
106 nr_neigh = nr_neigh_get_dev(ax25, dev);
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).
114 if (nr_neigh != NULL && nr_neigh->failed != 0 && quality == 0) {
115 struct nr_node *nr_nodet;
116 struct hlist_node *node;
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);
127 spin_unlock_bh(&nr_node_list_lock);
130 if (nr_neigh != NULL)
131 nr_neigh->failed = 0;
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;
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;
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);
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;
164 memcpy(nr_neigh->digipeat, ax25_digi,
165 sizeof(*ax25_digi));
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);
174 if (quality != 0 && ax25cmp(nr, ax25) == 0 && !nr_neigh->locked)
175 nr_neigh->quality = quality;
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;
184 nr_node->callsign = *nr;
185 strcpy(nr_node->mnemonic, mnemonic);
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);
192 nr_node->routes[0].quality = quality;
193 nr_node->routes[0].obs_count = obs_count;
194 nr_node->routes[0].neighbour = nr_neigh;
196 nr_neigh_hold(nr_neigh);
197 nr_neigh->count++;
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);
204 return 0;
206 nr_node_lock(nr_node);
208 if (quality != 0)
209 strcpy(nr_node->mnemonic, mnemonic);
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;
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];
226 nr_node->routes[0].quality = quality;
227 nr_node->routes[0].obs_count = obs_count;
228 nr_node->routes[0].neighbour = nr_neigh;
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);
240 if (nr_node->routes[2].neighbour->count == 0 && !nr_node->routes[2].neighbour->locked)
241 nr_remove_neigh(nr_node->routes[2].neighbour);
243 nr_node->routes[2].quality = quality;
244 nr_node->routes[2].obs_count = obs_count;
245 nr_node->routes[2].neighbour = nr_neigh;
247 nr_neigh_hold(nr_neigh);
248 nr_neigh->count++;
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;
262 nr_route = nr_node->routes[0];
263 nr_node->routes[0] = nr_node->routes[1];
264 nr_node->routes[1] = nr_route;
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;
271 case 2: nr_node->which = 1;
272 break;
274 default:
275 break;
277 nr_route = nr_node->routes[1];
278 nr_node->routes[1] = nr_node->routes[2];
279 nr_node->routes[2] = nr_route;
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;
287 case 1: nr_node->which = 0;
288 break;
290 default: break;
292 nr_route = nr_node->routes[0];
293 nr_node->routes[0] = nr_node->routes[1];
294 nr_node->routes[1] = nr_route;
296 case 1:
297 break;
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;
308 nr_neigh_put(nr_neigh);
309 nr_node_unlock(nr_node);
310 nr_node_put(nr_node);
311 return 0;
314 static inline void __nr_remove_node(struct nr_node *nr_node)
316 hlist_del_init(&nr_node->node_node);
317 nr_node_put(nr_node);
320 #define nr_remove_node_locked(__node) \
321 __nr_remove_node(__node)
323 static void nr_remove_node(struct nr_node *nr_node)
325 spin_lock_bh(&nr_node_list_lock);
326 __nr_remove_node(nr_node);
327 spin_unlock_bh(&nr_node_list_lock);
330 static inline void __nr_remove_neigh(struct nr_neigh *nr_neigh)
332 hlist_del_init(&nr_neigh->neigh_node);
333 nr_neigh_put(nr_neigh);
336 #define nr_remove_neigh_locked(__neigh) \
337 __nr_remove_neigh(__neigh)
339 static void nr_remove_neigh(struct nr_neigh *nr_neigh)
341 spin_lock_bh(&nr_neigh_list_lock);
342 __nr_remove_neigh(nr_neigh);
343 spin_unlock_bh(&nr_neigh_list_lock);
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.
350 static int nr_del_node(ax25_address *callsign, ax25_address *neighbour, struct net_device *dev)
352 struct nr_node *nr_node;
353 struct nr_neigh *nr_neigh;
354 int i;
356 nr_node = nr_node_get(callsign);
358 if (nr_node == NULL)
359 return -EINVAL;
361 nr_neigh = nr_neigh_get_dev(neighbour, dev);
363 if (nr_neigh == NULL) {
364 nr_node_put(nr_node);
365 return -EINVAL;
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);
374 if (nr_neigh->count == 0 && !nr_neigh->locked)
375 nr_remove_neigh(nr_neigh);
376 nr_neigh_put(nr_neigh);
378 nr_node->count--;
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;
391 nr_node_put(nr_node);
393 nr_node_unlock(nr_node);
395 return 0;
398 nr_neigh_put(nr_neigh);
399 nr_node_unlock(nr_node);
400 nr_node_put(nr_node);
402 return -EINVAL;
406 * Lock a neighbour with a quality.
408 static int nr_add_neigh(ax25_address *callsign, ax25_digi *ax25_digi, struct net_device *dev, unsigned int quality)
410 struct nr_neigh *nr_neigh;
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;
420 if ((nr_neigh = kmalloc(sizeof(*nr_neigh), GFP_ATOMIC)) == NULL)
421 return -ENOMEM;
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);
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;
439 memcpy(nr_neigh->digipeat, ax25_digi, sizeof(*ax25_digi));
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);
447 return 0;
451 * "Delete" a neighbour. The neighbour is only removed if the number
452 * of nodes that may use it is zero.
454 static int nr_del_neigh(ax25_address *callsign, struct net_device *dev, unsigned int quality)
456 struct nr_neigh *nr_neigh;
458 nr_neigh = nr_neigh_get_dev(callsign, dev);
460 if (nr_neigh == NULL) return -EINVAL;
462 nr_neigh->quality = quality;
463 nr_neigh->locked = 0;
465 if (nr_neigh->count == 0)
466 nr_remove_neigh(nr_neigh);
467 nr_neigh_put(nr_neigh);
469 return 0;
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.
477 static int nr_dec_obs(void)
479 struct nr_neigh *nr_neigh;
480 struct nr_node *s;
481 struct hlist_node *node, *nodet;
482 int i;
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;
492 case 1: /* From 1 -> 0 */
493 nr_neigh = s->routes[i].neighbour;
495 nr_neigh->count--;
496 nr_neigh_put(nr_neigh);
498 if (nr_neigh->count == 0 && !nr_neigh->locked)
499 nr_remove_neigh(nr_neigh);
501 s->count--;
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;
511 break;
513 default:
514 s->routes[i].obs_count--;
515 break;
520 if (s->count <= 0)
521 nr_remove_node_locked(s);
522 nr_node_unlock(s);
524 spin_unlock_bh(&nr_node_list_lock);
526 return 0;
530 * A device has been removed. Remove its routes and neighbours.
532 void nr_rt_device_down(struct net_device *dev)
534 struct nr_neigh *s;
535 struct hlist_node *node, *nodet, *node2, *node2t;
536 struct nr_node *t;
537 int i;
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--;
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;
560 if (t->count <= 0)
561 nr_remove_node_locked(t);
562 nr_node_unlock(t);
564 spin_unlock_bh(&nr_node_list_lock);
566 nr_remove_neigh_locked(s);
569 spin_unlock_bh(&nr_neigh_list_lock);
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.
576 static struct net_device *nr_ax25_dev_get(char *devname)
578 struct net_device *dev;
580 if ((dev = dev_get_by_name(devname)) == NULL)
581 return NULL;
583 if ((dev->flags & IFF_UP) && dev->type == ARPHRD_AX25)
584 return dev;
586 dev_put(dev);
587 return NULL;
591 * Find the first active NET/ROM device, usually "nr0".
593 struct net_device *nr_dev_first(void)
595 struct net_device *dev, *first = NULL;
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;
603 if (first)
604 dev_hold(first);
605 read_unlock(&dev_base_lock);
607 return first;
611 * Find the NET/ROM device for the given callsign.
613 struct net_device *nr_dev_get(ax25_address *addr)
615 struct net_device *dev;
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;
624 out:
625 read_unlock(&dev_base_lock);
626 return dev;
629 static ax25_digi *nr_call_to_digi(int ndigis, ax25_address *digipeaters)
631 static ax25_digi ax25_digi;
632 int i;
634 if (ndigis == 0)
635 return NULL;
637 for (i = 0; i < ndigis; i++) {
638 ax25_digi.calls[i] = digipeaters[i];
639 ax25_digi.repeated[i] = 0;
642 ax25_digi.ndigi = ndigis;
643 ax25_digi.lastrepeat = -1;
645 return &ax25_digi;
649 * Handle the ioctls that control the routing functions.
651 int nr_rt_ioctl(unsigned int cmd, void __user *arg)
653 struct nr_route_struct nr_route;
654 struct net_device *dev;
655 int ret;
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;
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;
684 dev_put(dev);
685 return ret;
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;
704 dev_put(dev);
705 return ret;
707 case SIOCNRDECOBS:
708 return nr_dec_obs();
710 default:
711 return -EINVAL;
714 return 0;
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.
721 void nr_link_failed(ax25_cb *ax25, int reason)
723 struct nr_neigh *s, *nr_neigh = NULL;
724 struct hlist_node *node;
725 struct nr_node *nr_node = NULL;
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;
735 spin_unlock_bh(&nr_neigh_list_lock);
737 if (nr_neigh == NULL)
738 return;
740 nr_neigh->ax25 = NULL;
741 ax25_cb_put(ax25);
743 if (++nr_neigh->failed < sysctl_netrom_link_fails_count) {
744 nr_neigh_put(nr_neigh);
745 return;
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);
755 spin_unlock_bh(&nr_node_list_lock);
756 nr_neigh_put(nr_neigh);
760 * Route a frame to an appropriate AX.25 connection. A NULL ax25_cb
761 * indicates an internally generated frame.
763 int nr_route_frame(struct sk_buff *skb, ax25_cb *ax25)
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;
775 nr_src = (ax25_address *)(skb->data + 0);
776 nr_dest = (ax25_address *)(skb->data + 7);
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);
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;
791 if (!sysctl_netrom_routing_control && ax25 != NULL)
792 return 0;
794 /* Its Time-To-Live has expired */
795 if (skb->data[14] == 1) {
796 return 0;
799 nr_node = nr_node_get(nr_dest);
800 if (nr_node == NULL)
801 return 0;
802 nr_node_lock(nr_node);
804 if (nr_node->which >= nr_node->count) {
805 nr_node_unlock(nr_node);
806 nr_node_put(nr_node);
807 return 0;
810 nr_neigh = nr_node->routes[nr_node->which].neighbour;
812 if ((dev = nr_dev_first()) == NULL) {
813 nr_node_unlock(nr_node);
814 nr_node_put(nr_node);
815 return 0;
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;
827 kfree_skb(skb);
828 skb=skbn;
829 skb->data[14]--;
831 dptr = skb_push(skb, 1);
832 *dptr = AX25_P_NETROM;
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);
839 nr_neigh->ax25 = ax25s;
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;
848 #ifdef CONFIG_PROC_FS
850 static void *nr_node_start(struct seq_file *seq, loff_t *pos)
852 struct nr_node *nr_node;
853 struct hlist_node *node;
854 int i = 1;
856 spin_lock_bh(&nr_node_list_lock);
857 if (*pos == 0)
858 return SEQ_START_TOKEN;
860 nr_node_for_each(nr_node, node, &nr_node_list) {
861 if (i == *pos)
862 return nr_node;
863 ++i;
866 return NULL;
869 static void *nr_node_next(struct seq_file *seq, void *v, loff_t *pos)
871 struct hlist_node *node;
872 ++*pos;
874 node = (v == SEQ_START_TOKEN)
875 ? nr_node_list.first
876 : ((struct nr_node *)v)->node_node.next;
878 return hlist_entry(node, struct nr_node, node_node);
881 static void nr_node_stop(struct seq_file *seq, void *v)
883 spin_unlock_bh(&nr_node_list_lock);
886 static int nr_node_show(struct seq_file *seq, void *v)
888 char buf[11];
889 int i;
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);
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);
909 nr_node_unlock(nr_node);
911 seq_puts(seq, "\n");
913 return 0;
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,
923 static int nr_node_info_open(struct inode *inode, struct file *file)
925 return seq_open(file, &nr_node_seqops);
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,
936 static void *nr_neigh_start(struct seq_file *seq, loff_t *pos)
938 struct nr_neigh *nr_neigh;
939 struct hlist_node *node;
940 int i = 1;
942 spin_lock_bh(&nr_neigh_list_lock);
943 if (*pos == 0)
944 return SEQ_START_TOKEN;
946 nr_neigh_for_each(nr_neigh, node, &nr_neigh_list) {
947 if (i == *pos)
948 return nr_neigh;
950 return NULL;
953 static void *nr_neigh_next(struct seq_file *seq, void *v, loff_t *pos)
955 struct hlist_node *node;
956 ++*pos;
958 node = (v == SEQ_START_TOKEN)
959 ? nr_neigh_list.first
960 : ((struct nr_neigh *)v)->neigh_node.next;
962 return hlist_entry(node, struct nr_neigh, neigh_node);
965 static void nr_neigh_stop(struct seq_file *seq, void *v)
967 spin_unlock_bh(&nr_neigh_list_lock);
970 static int nr_neigh_show(struct seq_file *seq, void *v)
972 char buf[11];
973 int i;
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;
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);
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]));
995 seq_puts(seq, "\n");
997 return 0;
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,
1007 static int nr_neigh_info_open(struct inode *inode, struct file *file)
1009 return seq_open(file, &nr_neigh_seqops);
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,
1020 #endif
1023 * Free all memory associated with the nodes and routes lists.
1025 void __exit nr_rt_free(void)
1027 struct nr_neigh *s = NULL;
1028 struct nr_node *t = NULL;
1029 struct hlist_node *node, *nodet;
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);
1038 nr_neigh_for_each_safe(s, node, nodet, &nr_neigh_list) {
1039 while(s->count) {
1040 s->count--;
1041 nr_neigh_put(s);
1043 nr_remove_neigh_locked(s);
1045 spin_unlock_bh(&nr_node_list_lock);
1046 spin_unlock_bh(&nr_neigh_list_lock);