1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
7 #include <linux/errno.h>
8 #include <linux/types.h>
9 #include <linux/socket.h>
11 #include <linux/kernel.h>
12 #include <linux/timer.h>
13 #include <linux/string.h>
14 #include <linux/sockios.h>
15 #include <linux/net.h>
16 #include <linux/slab.h>
18 #include <linux/inet.h>
19 #include <linux/netdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/skbuff.h>
24 #include <net/tcp_states.h>
25 #include <linux/uaccess.h>
26 #include <linux/fcntl.h>
27 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
29 #include <linux/interrupt.h>
30 #include <linux/notifier.h>
31 #include <linux/init.h>
33 #include <linux/seq_file.h>
34 #include <linux/export.h>
36 static unsigned int rose_neigh_no
= 1;
38 static struct rose_node
*rose_node_list
;
39 static DEFINE_SPINLOCK(rose_node_list_lock
);
40 static struct rose_neigh
*rose_neigh_list
;
41 static DEFINE_SPINLOCK(rose_neigh_list_lock
);
42 static struct rose_route
*rose_route_list
;
43 static DEFINE_SPINLOCK(rose_route_list_lock
);
45 struct rose_neigh
*rose_loopback_neigh
;
48 * Add a new route to a node, and in the process add the node and the
49 * neighbour if it is new.
51 static int __must_check
rose_add_node(struct rose_route_struct
*rose_route
,
52 struct net_device
*dev
)
54 struct rose_node
*rose_node
, *rose_tmpn
, *rose_tmpp
;
55 struct rose_neigh
*rose_neigh
;
58 spin_lock_bh(&rose_node_list_lock
);
59 spin_lock_bh(&rose_neigh_list_lock
);
61 rose_node
= rose_node_list
;
62 while (rose_node
!= NULL
) {
63 if ((rose_node
->mask
== rose_route
->mask
) &&
64 (rosecmpm(&rose_route
->address
, &rose_node
->address
,
65 rose_route
->mask
) == 0))
67 rose_node
= rose_node
->next
;
70 if (rose_node
!= NULL
&& rose_node
->loopback
) {
75 rose_neigh
= rose_neigh_list
;
76 while (rose_neigh
!= NULL
) {
77 if (ax25cmp(&rose_route
->neighbour
,
78 &rose_neigh
->callsign
) == 0 &&
79 rose_neigh
->dev
== dev
)
81 rose_neigh
= rose_neigh
->next
;
84 if (rose_neigh
== NULL
) {
85 rose_neigh
= kmalloc(sizeof(*rose_neigh
), GFP_ATOMIC
);
86 if (rose_neigh
== NULL
) {
91 rose_neigh
->callsign
= rose_route
->neighbour
;
92 rose_neigh
->digipeat
= NULL
;
93 rose_neigh
->ax25
= NULL
;
94 rose_neigh
->dev
= dev
;
95 rose_neigh
->count
= 0;
97 rose_neigh
->dce_mode
= 0;
98 rose_neigh
->loopback
= 0;
99 rose_neigh
->number
= rose_neigh_no
++;
100 rose_neigh
->restarted
= 0;
102 skb_queue_head_init(&rose_neigh
->queue
);
104 timer_setup(&rose_neigh
->ftimer
, NULL
, 0);
105 timer_setup(&rose_neigh
->t0timer
, NULL
, 0);
107 if (rose_route
->ndigis
!= 0) {
108 rose_neigh
->digipeat
=
109 kmalloc(sizeof(ax25_digi
), GFP_ATOMIC
);
110 if (rose_neigh
->digipeat
== NULL
) {
116 rose_neigh
->digipeat
->ndigi
= rose_route
->ndigis
;
117 rose_neigh
->digipeat
->lastrepeat
= -1;
119 for (i
= 0; i
< rose_route
->ndigis
; i
++) {
120 rose_neigh
->digipeat
->calls
[i
] =
121 rose_route
->digipeaters
[i
];
122 rose_neigh
->digipeat
->repeated
[i
] = 0;
126 rose_neigh
->next
= rose_neigh_list
;
127 rose_neigh_list
= rose_neigh
;
131 * This is a new node to be inserted into the list. Find where it needs
132 * to be inserted into the list, and insert it. We want to be sure
133 * to order the list in descending order of mask size to ensure that
134 * later when we are searching this list the first match will be the
137 if (rose_node
== NULL
) {
138 rose_tmpn
= rose_node_list
;
141 while (rose_tmpn
!= NULL
) {
142 if (rose_tmpn
->mask
> rose_route
->mask
) {
143 rose_tmpp
= rose_tmpn
;
144 rose_tmpn
= rose_tmpn
->next
;
150 /* create new node */
151 rose_node
= kmalloc(sizeof(*rose_node
), GFP_ATOMIC
);
152 if (rose_node
== NULL
) {
157 rose_node
->address
= rose_route
->address
;
158 rose_node
->mask
= rose_route
->mask
;
159 rose_node
->count
= 1;
160 rose_node
->loopback
= 0;
161 rose_node
->neighbour
[0] = rose_neigh
;
163 if (rose_tmpn
== NULL
) {
164 if (rose_tmpp
== NULL
) { /* Empty list */
165 rose_node_list
= rose_node
;
166 rose_node
->next
= NULL
;
168 rose_tmpp
->next
= rose_node
;
169 rose_node
->next
= NULL
;
172 if (rose_tmpp
== NULL
) { /* 1st node */
173 rose_node
->next
= rose_node_list
;
174 rose_node_list
= rose_node
;
176 rose_tmpp
->next
= rose_node
;
177 rose_node
->next
= rose_tmpn
;
185 /* We have space, slot it in */
186 if (rose_node
->count
< 3) {
187 rose_node
->neighbour
[rose_node
->count
] = rose_neigh
;
193 spin_unlock_bh(&rose_neigh_list_lock
);
194 spin_unlock_bh(&rose_node_list_lock
);
200 * Caller is holding rose_node_list_lock.
202 static void rose_remove_node(struct rose_node
*rose_node
)
206 if ((s
= rose_node_list
) == rose_node
) {
207 rose_node_list
= rose_node
->next
;
212 while (s
!= NULL
&& s
->next
!= NULL
) {
213 if (s
->next
== rose_node
) {
214 s
->next
= rose_node
->next
;
224 * Caller is holding rose_neigh_list_lock.
226 static void rose_remove_neigh(struct rose_neigh
*rose_neigh
)
228 struct rose_neigh
*s
;
230 del_timer_sync(&rose_neigh
->ftimer
);
231 del_timer_sync(&rose_neigh
->t0timer
);
233 skb_queue_purge(&rose_neigh
->queue
);
235 if ((s
= rose_neigh_list
) == rose_neigh
) {
236 rose_neigh_list
= rose_neigh
->next
;
237 if (rose_neigh
->ax25
)
238 ax25_cb_put(rose_neigh
->ax25
);
239 kfree(rose_neigh
->digipeat
);
244 while (s
!= NULL
&& s
->next
!= NULL
) {
245 if (s
->next
== rose_neigh
) {
246 s
->next
= rose_neigh
->next
;
247 if (rose_neigh
->ax25
)
248 ax25_cb_put(rose_neigh
->ax25
);
249 kfree(rose_neigh
->digipeat
);
259 * Caller is holding rose_route_list_lock.
261 static void rose_remove_route(struct rose_route
*rose_route
)
263 struct rose_route
*s
;
265 if (rose_route
->neigh1
!= NULL
)
266 rose_route
->neigh1
->use
--;
268 if (rose_route
->neigh2
!= NULL
)
269 rose_route
->neigh2
->use
--;
271 if ((s
= rose_route_list
) == rose_route
) {
272 rose_route_list
= rose_route
->next
;
277 while (s
!= NULL
&& s
->next
!= NULL
) {
278 if (s
->next
== rose_route
) {
279 s
->next
= rose_route
->next
;
289 * "Delete" a node. Strictly speaking remove a route to a node. The node
290 * is only deleted if no routes are left to it.
292 static int rose_del_node(struct rose_route_struct
*rose_route
,
293 struct net_device
*dev
)
295 struct rose_node
*rose_node
;
296 struct rose_neigh
*rose_neigh
;
299 spin_lock_bh(&rose_node_list_lock
);
300 spin_lock_bh(&rose_neigh_list_lock
);
302 rose_node
= rose_node_list
;
303 while (rose_node
!= NULL
) {
304 if ((rose_node
->mask
== rose_route
->mask
) &&
305 (rosecmpm(&rose_route
->address
, &rose_node
->address
,
306 rose_route
->mask
) == 0))
308 rose_node
= rose_node
->next
;
311 if (rose_node
== NULL
|| rose_node
->loopback
) {
316 rose_neigh
= rose_neigh_list
;
317 while (rose_neigh
!= NULL
) {
318 if (ax25cmp(&rose_route
->neighbour
,
319 &rose_neigh
->callsign
) == 0 &&
320 rose_neigh
->dev
== dev
)
322 rose_neigh
= rose_neigh
->next
;
325 if (rose_neigh
== NULL
) {
330 for (i
= 0; i
< rose_node
->count
; i
++) {
331 if (rose_node
->neighbour
[i
] == rose_neigh
) {
334 if (rose_neigh
->count
== 0 && rose_neigh
->use
== 0)
335 rose_remove_neigh(rose_neigh
);
339 if (rose_node
->count
== 0) {
340 rose_remove_node(rose_node
);
344 rose_node
->neighbour
[0] =
345 rose_node
->neighbour
[1];
348 rose_node
->neighbour
[1] =
349 rose_node
->neighbour
[2];
361 spin_unlock_bh(&rose_neigh_list_lock
);
362 spin_unlock_bh(&rose_node_list_lock
);
368 * Add the loopback neighbour.
370 void rose_add_loopback_neigh(void)
372 struct rose_neigh
*sn
;
374 rose_loopback_neigh
= kmalloc(sizeof(struct rose_neigh
), GFP_KERNEL
);
375 if (!rose_loopback_neigh
)
377 sn
= rose_loopback_neigh
;
379 sn
->callsign
= null_ax25_address
;
387 sn
->number
= rose_neigh_no
++;
390 skb_queue_head_init(&sn
->queue
);
392 timer_setup(&sn
->ftimer
, NULL
, 0);
393 timer_setup(&sn
->t0timer
, NULL
, 0);
395 spin_lock_bh(&rose_neigh_list_lock
);
396 sn
->next
= rose_neigh_list
;
397 rose_neigh_list
= sn
;
398 spin_unlock_bh(&rose_neigh_list_lock
);
402 * Add a loopback node.
404 int rose_add_loopback_node(const rose_address
*address
)
406 struct rose_node
*rose_node
;
409 spin_lock_bh(&rose_node_list_lock
);
411 rose_node
= rose_node_list
;
412 while (rose_node
!= NULL
) {
413 if ((rose_node
->mask
== 10) &&
414 (rosecmpm(address
, &rose_node
->address
, 10) == 0) &&
417 rose_node
= rose_node
->next
;
420 if (rose_node
!= NULL
)
423 if ((rose_node
= kmalloc(sizeof(*rose_node
), GFP_ATOMIC
)) == NULL
) {
428 rose_node
->address
= *address
;
429 rose_node
->mask
= 10;
430 rose_node
->count
= 1;
431 rose_node
->loopback
= 1;
432 rose_node
->neighbour
[0] = rose_loopback_neigh
;
434 /* Insert at the head of list. Address is always mask=10 */
435 rose_node
->next
= rose_node_list
;
436 rose_node_list
= rose_node
;
438 rose_loopback_neigh
->count
++;
441 spin_unlock_bh(&rose_node_list_lock
);
447 * Delete a loopback node.
449 void rose_del_loopback_node(const rose_address
*address
)
451 struct rose_node
*rose_node
;
453 spin_lock_bh(&rose_node_list_lock
);
455 rose_node
= rose_node_list
;
456 while (rose_node
!= NULL
) {
457 if ((rose_node
->mask
== 10) &&
458 (rosecmpm(address
, &rose_node
->address
, 10) == 0) &&
461 rose_node
= rose_node
->next
;
464 if (rose_node
== NULL
)
467 rose_remove_node(rose_node
);
469 rose_loopback_neigh
->count
--;
472 spin_unlock_bh(&rose_node_list_lock
);
476 * A device has been removed. Remove its routes and neighbours.
478 void rose_rt_device_down(struct net_device
*dev
)
480 struct rose_neigh
*s
, *rose_neigh
;
481 struct rose_node
*t
, *rose_node
;
484 spin_lock_bh(&rose_node_list_lock
);
485 spin_lock_bh(&rose_neigh_list_lock
);
486 rose_neigh
= rose_neigh_list
;
487 while (rose_neigh
!= NULL
) {
489 rose_neigh
= rose_neigh
->next
;
494 rose_node
= rose_node_list
;
496 while (rose_node
!= NULL
) {
498 rose_node
= rose_node
->next
;
500 for (i
= 0; i
< t
->count
; i
++) {
501 if (t
->neighbour
[i
] != s
)
508 t
->neighbour
[0] = t
->neighbour
[1];
511 t
->neighbour
[1] = t
->neighbour
[2];
522 rose_remove_neigh(s
);
524 spin_unlock_bh(&rose_neigh_list_lock
);
525 spin_unlock_bh(&rose_node_list_lock
);
528 #if 0 /* Currently unused */
530 * A device has been removed. Remove its links.
532 void rose_route_device_down(struct net_device
*dev
)
534 struct rose_route
*s
, *rose_route
;
536 spin_lock_bh(&rose_route_list_lock
);
537 rose_route
= rose_route_list
;
538 while (rose_route
!= NULL
) {
540 rose_route
= rose_route
->next
;
542 if (s
->neigh1
->dev
== dev
|| s
->neigh2
->dev
== dev
)
543 rose_remove_route(s
);
545 spin_unlock_bh(&rose_route_list_lock
);
550 * Clear all nodes and neighbours out, except for neighbours with
551 * active connections going through them.
552 * Do not clear loopback neighbour and nodes.
554 static int rose_clear_routes(void)
556 struct rose_neigh
*s
, *rose_neigh
;
557 struct rose_node
*t
, *rose_node
;
559 spin_lock_bh(&rose_node_list_lock
);
560 spin_lock_bh(&rose_neigh_list_lock
);
562 rose_neigh
= rose_neigh_list
;
563 rose_node
= rose_node_list
;
565 while (rose_node
!= NULL
) {
567 rose_node
= rose_node
->next
;
572 while (rose_neigh
!= NULL
) {
574 rose_neigh
= rose_neigh
->next
;
576 if (s
->use
== 0 && !s
->loopback
) {
578 rose_remove_neigh(s
);
582 spin_unlock_bh(&rose_neigh_list_lock
);
583 spin_unlock_bh(&rose_node_list_lock
);
589 * Check that the device given is a valid AX.25 interface that is "up".
592 static struct net_device
*rose_ax25_dev_find(char *devname
)
594 struct net_device
*dev
;
596 if ((dev
= __dev_get_by_name(&init_net
, devname
)) == NULL
)
599 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_AX25
)
606 * Find the first active ROSE device, usually "rose0".
608 struct net_device
*rose_dev_first(void)
610 struct net_device
*dev
, *first
= NULL
;
613 for_each_netdev_rcu(&init_net
, dev
) {
614 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_ROSE
)
615 if (first
== NULL
|| strncmp(dev
->name
, first
->name
, 3) < 0)
626 * Find the ROSE device for the given address.
628 struct net_device
*rose_dev_get(rose_address
*addr
)
630 struct net_device
*dev
;
633 for_each_netdev_rcu(&init_net
, dev
) {
634 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_ROSE
&&
635 rosecmp(addr
, (const rose_address
*)dev
->dev_addr
) == 0) {
646 static int rose_dev_exists(rose_address
*addr
)
648 struct net_device
*dev
;
651 for_each_netdev_rcu(&init_net
, dev
) {
652 if ((dev
->flags
& IFF_UP
) && dev
->type
== ARPHRD_ROSE
&&
653 rosecmp(addr
, (const rose_address
*)dev
->dev_addr
) == 0)
665 struct rose_route
*rose_route_free_lci(unsigned int lci
, struct rose_neigh
*neigh
)
667 struct rose_route
*rose_route
;
669 for (rose_route
= rose_route_list
; rose_route
!= NULL
; rose_route
= rose_route
->next
)
670 if ((rose_route
->neigh1
== neigh
&& rose_route
->lci1
== lci
) ||
671 (rose_route
->neigh2
== neigh
&& rose_route
->lci2
== lci
))
678 * Find a neighbour or a route given a ROSE address.
680 struct rose_neigh
*rose_get_neigh(rose_address
*addr
, unsigned char *cause
,
681 unsigned char *diagnostic
, int route_frame
)
683 struct rose_neigh
*res
= NULL
;
684 struct rose_node
*node
;
688 if (!route_frame
) spin_lock_bh(&rose_node_list_lock
);
689 for (node
= rose_node_list
; node
!= NULL
; node
= node
->next
) {
690 if (rosecmpm(addr
, &node
->address
, node
->mask
) == 0) {
691 for (i
= 0; i
< node
->count
; i
++) {
692 if (node
->neighbour
[i
]->restarted
) {
693 res
= node
->neighbour
[i
];
699 if (!route_frame
) { /* connect request */
700 for (node
= rose_node_list
; node
!= NULL
; node
= node
->next
) {
701 if (rosecmpm(addr
, &node
->address
, node
->mask
) == 0) {
702 for (i
= 0; i
< node
->count
; i
++) {
703 if (!rose_ftimer_running(node
->neighbour
[i
])) {
704 res
= node
->neighbour
[i
];
714 *cause
= ROSE_OUT_OF_ORDER
;
717 *cause
= ROSE_NOT_OBTAINABLE
;
722 if (!route_frame
) spin_unlock_bh(&rose_node_list_lock
);
727 * Handle the ioctls that control the routing functions.
729 int rose_rt_ioctl(unsigned int cmd
, void __user
*arg
)
731 struct rose_route_struct rose_route
;
732 struct net_device
*dev
;
737 if (copy_from_user(&rose_route
, arg
, sizeof(struct rose_route_struct
)))
739 if ((dev
= rose_ax25_dev_find(rose_route
.device
)) == NULL
)
741 if (rose_dev_exists(&rose_route
.address
)) /* Can't add routes to ourself */
743 if (rose_route
.mask
> 10) /* Mask can't be more than 10 digits */
745 if (rose_route
.ndigis
> AX25_MAX_DIGIS
)
747 err
= rose_add_node(&rose_route
, dev
);
751 if (copy_from_user(&rose_route
, arg
, sizeof(struct rose_route_struct
)))
753 if ((dev
= rose_ax25_dev_find(rose_route
.device
)) == NULL
)
755 err
= rose_del_node(&rose_route
, dev
);
759 return rose_clear_routes();
768 static void rose_del_route_by_neigh(struct rose_neigh
*rose_neigh
)
770 struct rose_route
*rose_route
, *s
;
772 rose_neigh
->restarted
= 0;
774 rose_stop_t0timer(rose_neigh
);
775 rose_start_ftimer(rose_neigh
);
777 skb_queue_purge(&rose_neigh
->queue
);
779 spin_lock_bh(&rose_route_list_lock
);
781 rose_route
= rose_route_list
;
783 while (rose_route
!= NULL
) {
784 if ((rose_route
->neigh1
== rose_neigh
&& rose_route
->neigh2
== rose_neigh
) ||
785 (rose_route
->neigh1
== rose_neigh
&& rose_route
->neigh2
== NULL
) ||
786 (rose_route
->neigh2
== rose_neigh
&& rose_route
->neigh1
== NULL
)) {
787 s
= rose_route
->next
;
788 rose_remove_route(rose_route
);
793 if (rose_route
->neigh1
== rose_neigh
) {
794 rose_route
->neigh1
->use
--;
795 rose_route
->neigh1
= NULL
;
796 rose_transmit_clear_request(rose_route
->neigh2
, rose_route
->lci2
, ROSE_OUT_OF_ORDER
, 0);
799 if (rose_route
->neigh2
== rose_neigh
) {
800 rose_route
->neigh2
->use
--;
801 rose_route
->neigh2
= NULL
;
802 rose_transmit_clear_request(rose_route
->neigh1
, rose_route
->lci1
, ROSE_OUT_OF_ORDER
, 0);
805 rose_route
= rose_route
->next
;
807 spin_unlock_bh(&rose_route_list_lock
);
811 * A level 2 link has timed out, therefore it appears to be a poor link,
812 * then don't use that neighbour until it is reset. Blow away all through
813 * routes and connections using this route.
815 void rose_link_failed(ax25_cb
*ax25
, int reason
)
817 struct rose_neigh
*rose_neigh
;
819 spin_lock_bh(&rose_neigh_list_lock
);
820 rose_neigh
= rose_neigh_list
;
821 while (rose_neigh
!= NULL
) {
822 if (rose_neigh
->ax25
== ax25
)
824 rose_neigh
= rose_neigh
->next
;
827 if (rose_neigh
!= NULL
) {
828 rose_neigh
->ax25
= NULL
;
831 rose_del_route_by_neigh(rose_neigh
);
832 rose_kill_by_neigh(rose_neigh
);
834 spin_unlock_bh(&rose_neigh_list_lock
);
838 * A device has been "downed" remove its link status. Blow away all
839 * through routes and connections that use this device.
841 void rose_link_device_down(struct net_device
*dev
)
843 struct rose_neigh
*rose_neigh
;
845 for (rose_neigh
= rose_neigh_list
; rose_neigh
!= NULL
; rose_neigh
= rose_neigh
->next
) {
846 if (rose_neigh
->dev
== dev
) {
847 rose_del_route_by_neigh(rose_neigh
);
848 rose_kill_by_neigh(rose_neigh
);
854 * Route a frame to an appropriate AX.25 connection.
855 * A NULL ax25_cb indicates an internally generated frame.
857 int rose_route_frame(struct sk_buff
*skb
, ax25_cb
*ax25
)
859 struct rose_neigh
*rose_neigh
, *new_neigh
;
860 struct rose_route
*rose_route
;
861 struct rose_facilities_struct facilities
;
862 rose_address
*src_addr
, *dest_addr
;
864 unsigned short frametype
;
865 unsigned int lci
, new_lci
;
866 unsigned char cause
, diagnostic
;
867 struct net_device
*dev
;
871 if (skb
->len
< ROSE_MIN_LEN
)
875 return rose_loopback_queue(skb
, NULL
);
877 frametype
= skb
->data
[2];
878 lci
= ((skb
->data
[0] << 8) & 0xF00) + ((skb
->data
[1] << 0) & 0x0FF);
879 if (frametype
== ROSE_CALL_REQUEST
&&
880 (skb
->len
<= ROSE_CALL_REQ_FACILITIES_OFF
||
881 skb
->data
[ROSE_CALL_REQ_ADDR_LEN_OFF
] !=
882 ROSE_CALL_REQ_ADDR_LEN_VAL
))
884 src_addr
= (rose_address
*)(skb
->data
+ ROSE_CALL_REQ_SRC_ADDR_OFF
);
885 dest_addr
= (rose_address
*)(skb
->data
+ ROSE_CALL_REQ_DEST_ADDR_OFF
);
887 spin_lock_bh(&rose_neigh_list_lock
);
888 spin_lock_bh(&rose_route_list_lock
);
890 rose_neigh
= rose_neigh_list
;
891 while (rose_neigh
!= NULL
) {
892 if (ax25cmp(&ax25
->dest_addr
, &rose_neigh
->callsign
) == 0 &&
893 ax25
->ax25_dev
->dev
== rose_neigh
->dev
)
895 rose_neigh
= rose_neigh
->next
;
898 if (rose_neigh
== NULL
) {
899 printk("rose_route : unknown neighbour or device %s\n",
900 ax2asc(buf
, &ax25
->dest_addr
));
905 * Obviously the link is working, halt the ftimer.
907 rose_stop_ftimer(rose_neigh
);
910 * LCI of zero is always for us, and its always a restart
914 rose_link_rx_restart(skb
, rose_neigh
, frametype
);
919 * Find an existing socket.
921 if ((sk
= rose_find_socket(lci
, rose_neigh
)) != NULL
) {
922 if (frametype
== ROSE_CALL_REQUEST
) {
923 struct rose_sock
*rose
= rose_sk(sk
);
925 /* Remove an existing unused socket */
926 rose_clear_queues(sk
);
927 rose
->cause
= ROSE_NETWORK_CONGESTION
;
928 rose
->diagnostic
= 0;
929 rose
->neighbour
->use
--;
930 rose
->neighbour
= NULL
;
932 rose
->state
= ROSE_STATE_0
;
933 sk
->sk_state
= TCP_CLOSE
;
935 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
936 if (!sock_flag(sk
, SOCK_DEAD
)) {
937 sk
->sk_state_change(sk
);
938 sock_set_flag(sk
, SOCK_DEAD
);
942 skb_reset_transport_header(skb
);
943 res
= rose_process_rx_frame(sk
, skb
);
949 * Is is a Call Request and is it for us ?
951 if (frametype
== ROSE_CALL_REQUEST
)
952 if ((dev
= rose_dev_get(dest_addr
)) != NULL
) {
953 res
= rose_rx_call_request(skb
, dev
, rose_neigh
, lci
);
958 if (!sysctl_rose_routing_control
) {
959 rose_transmit_clear_request(rose_neigh
, lci
, ROSE_NOT_OBTAINABLE
, 0);
964 * Route it to the next in line if we have an entry for it.
966 rose_route
= rose_route_list
;
967 while (rose_route
!= NULL
) {
968 if (rose_route
->lci1
== lci
&&
969 rose_route
->neigh1
== rose_neigh
) {
970 if (frametype
== ROSE_CALL_REQUEST
) {
971 /* F6FBB - Remove an existing unused route */
972 rose_remove_route(rose_route
);
974 } else if (rose_route
->neigh2
!= NULL
) {
975 skb
->data
[0] &= 0xF0;
976 skb
->data
[0] |= (rose_route
->lci2
>> 8) & 0x0F;
977 skb
->data
[1] = (rose_route
->lci2
>> 0) & 0xFF;
978 rose_transmit_link(skb
, rose_route
->neigh2
);
979 if (frametype
== ROSE_CLEAR_CONFIRMATION
)
980 rose_remove_route(rose_route
);
984 if (frametype
== ROSE_CLEAR_CONFIRMATION
)
985 rose_remove_route(rose_route
);
989 if (rose_route
->lci2
== lci
&&
990 rose_route
->neigh2
== rose_neigh
) {
991 if (frametype
== ROSE_CALL_REQUEST
) {
992 /* F6FBB - Remove an existing unused route */
993 rose_remove_route(rose_route
);
995 } else if (rose_route
->neigh1
!= NULL
) {
996 skb
->data
[0] &= 0xF0;
997 skb
->data
[0] |= (rose_route
->lci1
>> 8) & 0x0F;
998 skb
->data
[1] = (rose_route
->lci1
>> 0) & 0xFF;
999 rose_transmit_link(skb
, rose_route
->neigh1
);
1000 if (frametype
== ROSE_CLEAR_CONFIRMATION
)
1001 rose_remove_route(rose_route
);
1005 if (frametype
== ROSE_CLEAR_CONFIRMATION
)
1006 rose_remove_route(rose_route
);
1010 rose_route
= rose_route
->next
;
1015 * 1. The frame isn't for us,
1016 * 2. It isn't "owned" by any existing route.
1018 if (frametype
!= ROSE_CALL_REQUEST
) { /* XXX */
1023 memset(&facilities
, 0x00, sizeof(struct rose_facilities_struct
));
1025 if (!rose_parse_facilities(skb
->data
+ ROSE_CALL_REQ_FACILITIES_OFF
,
1026 skb
->len
- ROSE_CALL_REQ_FACILITIES_OFF
,
1028 rose_transmit_clear_request(rose_neigh
, lci
, ROSE_INVALID_FACILITY
, 76);
1033 * Check for routing loops.
1035 rose_route
= rose_route_list
;
1036 while (rose_route
!= NULL
) {
1037 if (rose_route
->rand
== facilities
.rand
&&
1038 rosecmp(src_addr
, &rose_route
->src_addr
) == 0 &&
1039 ax25cmp(&facilities
.dest_call
, &rose_route
->src_call
) == 0 &&
1040 ax25cmp(&facilities
.source_call
, &rose_route
->dest_call
) == 0) {
1041 rose_transmit_clear_request(rose_neigh
, lci
, ROSE_NOT_OBTAINABLE
, 120);
1044 rose_route
= rose_route
->next
;
1047 if ((new_neigh
= rose_get_neigh(dest_addr
, &cause
, &diagnostic
, 1)) == NULL
) {
1048 rose_transmit_clear_request(rose_neigh
, lci
, cause
, diagnostic
);
1052 if ((new_lci
= rose_new_lci(new_neigh
)) == 0) {
1053 rose_transmit_clear_request(rose_neigh
, lci
, ROSE_NETWORK_CONGESTION
, 71);
1057 if ((rose_route
= kmalloc(sizeof(*rose_route
), GFP_ATOMIC
)) == NULL
) {
1058 rose_transmit_clear_request(rose_neigh
, lci
, ROSE_NETWORK_CONGESTION
, 120);
1062 rose_route
->lci1
= lci
;
1063 rose_route
->src_addr
= *src_addr
;
1064 rose_route
->dest_addr
= *dest_addr
;
1065 rose_route
->src_call
= facilities
.dest_call
;
1066 rose_route
->dest_call
= facilities
.source_call
;
1067 rose_route
->rand
= facilities
.rand
;
1068 rose_route
->neigh1
= rose_neigh
;
1069 rose_route
->lci2
= new_lci
;
1070 rose_route
->neigh2
= new_neigh
;
1072 rose_route
->neigh1
->use
++;
1073 rose_route
->neigh2
->use
++;
1075 rose_route
->next
= rose_route_list
;
1076 rose_route_list
= rose_route
;
1078 skb
->data
[0] &= 0xF0;
1079 skb
->data
[0] |= (rose_route
->lci2
>> 8) & 0x0F;
1080 skb
->data
[1] = (rose_route
->lci2
>> 0) & 0xFF;
1082 rose_transmit_link(skb
, rose_route
->neigh2
);
1086 spin_unlock_bh(&rose_route_list_lock
);
1087 spin_unlock_bh(&rose_neigh_list_lock
);
1092 #ifdef CONFIG_PROC_FS
1094 static void *rose_node_start(struct seq_file
*seq
, loff_t
*pos
)
1095 __acquires(rose_node_list_lock
)
1097 struct rose_node
*rose_node
;
1100 spin_lock_bh(&rose_node_list_lock
);
1102 return SEQ_START_TOKEN
;
1104 for (rose_node
= rose_node_list
; rose_node
&& i
< *pos
;
1105 rose_node
= rose_node
->next
, ++i
);
1107 return (i
== *pos
) ? rose_node
: NULL
;
1110 static void *rose_node_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1114 return (v
== SEQ_START_TOKEN
) ? rose_node_list
1115 : ((struct rose_node
*)v
)->next
;
1118 static void rose_node_stop(struct seq_file
*seq
, void *v
)
1119 __releases(rose_node_list_lock
)
1121 spin_unlock_bh(&rose_node_list_lock
);
1124 static int rose_node_show(struct seq_file
*seq
, void *v
)
1129 if (v
== SEQ_START_TOKEN
)
1130 seq_puts(seq
, "address mask n neigh neigh neigh\n");
1132 const struct rose_node
*rose_node
= v
;
1133 seq_printf(seq
, "%-10s %04d %d",
1134 rose2asc(rsbuf
, &rose_node
->address
),
1138 for (i
= 0; i
< rose_node
->count
; i
++)
1139 seq_printf(seq
, " %05d", rose_node
->neighbour
[i
]->number
);
1141 seq_puts(seq
, "\n");
1146 const struct seq_operations rose_node_seqops
= {
1147 .start
= rose_node_start
,
1148 .next
= rose_node_next
,
1149 .stop
= rose_node_stop
,
1150 .show
= rose_node_show
,
1153 static void *rose_neigh_start(struct seq_file
*seq
, loff_t
*pos
)
1154 __acquires(rose_neigh_list_lock
)
1156 struct rose_neigh
*rose_neigh
;
1159 spin_lock_bh(&rose_neigh_list_lock
);
1161 return SEQ_START_TOKEN
;
1163 for (rose_neigh
= rose_neigh_list
; rose_neigh
&& i
< *pos
;
1164 rose_neigh
= rose_neigh
->next
, ++i
);
1166 return (i
== *pos
) ? rose_neigh
: NULL
;
1169 static void *rose_neigh_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1173 return (v
== SEQ_START_TOKEN
) ? rose_neigh_list
1174 : ((struct rose_neigh
*)v
)->next
;
1177 static void rose_neigh_stop(struct seq_file
*seq
, void *v
)
1178 __releases(rose_neigh_list_lock
)
1180 spin_unlock_bh(&rose_neigh_list_lock
);
1183 static int rose_neigh_show(struct seq_file
*seq
, void *v
)
1188 if (v
== SEQ_START_TOKEN
)
1190 "addr callsign dev count use mode restart t0 tf digipeaters\n");
1192 struct rose_neigh
*rose_neigh
= v
;
1194 /* if (!rose_neigh->loopback) { */
1195 seq_printf(seq
, "%05d %-9s %-4s %3d %3d %3s %3s %3lu %3lu",
1197 (rose_neigh
->loopback
) ? "RSLOOP-0" : ax2asc(buf
, &rose_neigh
->callsign
),
1198 rose_neigh
->dev
? rose_neigh
->dev
->name
: "???",
1201 (rose_neigh
->dce_mode
) ? "DCE" : "DTE",
1202 (rose_neigh
->restarted
) ? "yes" : "no",
1203 ax25_display_timer(&rose_neigh
->t0timer
) / HZ
,
1204 ax25_display_timer(&rose_neigh
->ftimer
) / HZ
);
1206 if (rose_neigh
->digipeat
!= NULL
) {
1207 for (i
= 0; i
< rose_neigh
->digipeat
->ndigi
; i
++)
1208 seq_printf(seq
, " %s", ax2asc(buf
, &rose_neigh
->digipeat
->calls
[i
]));
1211 seq_puts(seq
, "\n");
1217 const struct seq_operations rose_neigh_seqops
= {
1218 .start
= rose_neigh_start
,
1219 .next
= rose_neigh_next
,
1220 .stop
= rose_neigh_stop
,
1221 .show
= rose_neigh_show
,
1224 static void *rose_route_start(struct seq_file
*seq
, loff_t
*pos
)
1225 __acquires(rose_route_list_lock
)
1227 struct rose_route
*rose_route
;
1230 spin_lock_bh(&rose_route_list_lock
);
1232 return SEQ_START_TOKEN
;
1234 for (rose_route
= rose_route_list
; rose_route
&& i
< *pos
;
1235 rose_route
= rose_route
->next
, ++i
);
1237 return (i
== *pos
) ? rose_route
: NULL
;
1240 static void *rose_route_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1244 return (v
== SEQ_START_TOKEN
) ? rose_route_list
1245 : ((struct rose_route
*)v
)->next
;
1248 static void rose_route_stop(struct seq_file
*seq
, void *v
)
1249 __releases(rose_route_list_lock
)
1251 spin_unlock_bh(&rose_route_list_lock
);
1254 static int rose_route_show(struct seq_file
*seq
, void *v
)
1256 char buf
[11], rsbuf
[11];
1258 if (v
== SEQ_START_TOKEN
)
1260 "lci address callsign neigh <-> lci address callsign neigh\n");
1262 struct rose_route
*rose_route
= v
;
1264 if (rose_route
->neigh1
)
1266 "%3.3X %-10s %-9s %05d ",
1268 rose2asc(rsbuf
, &rose_route
->src_addr
),
1269 ax2asc(buf
, &rose_route
->src_call
),
1270 rose_route
->neigh1
->number
);
1275 if (rose_route
->neigh2
)
1277 "%3.3X %-10s %-9s %05d\n",
1279 rose2asc(rsbuf
, &rose_route
->dest_addr
),
1280 ax2asc(buf
, &rose_route
->dest_call
),
1281 rose_route
->neigh2
->number
);
1289 struct seq_operations rose_route_seqops
= {
1290 .start
= rose_route_start
,
1291 .next
= rose_route_next
,
1292 .stop
= rose_route_stop
,
1293 .show
= rose_route_show
,
1295 #endif /* CONFIG_PROC_FS */
1298 * Release all memory associated with ROSE routing structures.
1300 void __exit
rose_rt_free(void)
1302 struct rose_neigh
*s
, *rose_neigh
= rose_neigh_list
;
1303 struct rose_node
*t
, *rose_node
= rose_node_list
;
1304 struct rose_route
*u
, *rose_route
= rose_route_list
;
1306 while (rose_neigh
!= NULL
) {
1308 rose_neigh
= rose_neigh
->next
;
1310 rose_remove_neigh(s
);
1313 while (rose_node
!= NULL
) {
1315 rose_node
= rose_node
->next
;
1317 rose_remove_node(t
);
1320 while (rose_route
!= NULL
) {
1322 rose_route
= rose_route
->next
;
1324 rose_remove_route(u
);