1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
6 * Most of this code is based on the SDL diagrams published in the 7th ARRL
7 * Computer Networking Conference papers. The diagrams have mistakes in them,
8 * but are mostly correct. Before you modify the code could you read the SDL
9 * diagrams as the code is not obvious and probably very easy to break.
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.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>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
25 #include <net/tcp_states.h>
26 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
32 * State machine for state 1, Awaiting Call Accepted State.
33 * The handling of the timer(s) is in file rose_timer.c.
34 * Handling of state 0 and connection release is in af_rose.c.
36 static int rose_state1_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
38 struct rose_sock
*rose
= rose_sk(sk
);
41 case ROSE_CALL_ACCEPTED
:
43 rose_start_idletimer(sk
);
44 rose
->condition
= 0x00;
49 rose
->state
= ROSE_STATE_3
;
50 sk
->sk_state
= TCP_ESTABLISHED
;
51 if (!sock_flag(sk
, SOCK_DEAD
))
52 sk
->sk_state_change(sk
);
55 case ROSE_CLEAR_REQUEST
:
56 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
57 rose_disconnect(sk
, ECONNREFUSED
, skb
->data
[3], skb
->data
[4]);
58 rose
->neighbour
->use
--;
69 * State machine for state 2, Awaiting Clear Confirmation State.
70 * The handling of the timer(s) is in file rose_timer.c
71 * Handling of state 0 and connection release is in af_rose.c.
73 static int rose_state2_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
75 struct rose_sock
*rose
= rose_sk(sk
);
78 case ROSE_CLEAR_REQUEST
:
79 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
80 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
81 rose
->neighbour
->use
--;
84 case ROSE_CLEAR_CONFIRMATION
:
85 rose_disconnect(sk
, 0, -1, -1);
86 rose
->neighbour
->use
--;
97 * State machine for state 3, Connected State.
98 * The handling of the timer(s) is in file rose_timer.c
99 * Handling of state 0 and connection release is in af_rose.c.
101 static int rose_state3_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int q
, int d
, int m
)
103 struct rose_sock
*rose
= rose_sk(sk
);
107 case ROSE_RESET_REQUEST
:
109 rose_start_idletimer(sk
);
110 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
111 rose
->condition
= 0x00;
116 rose_requeue_frames(sk
);
119 case ROSE_CLEAR_REQUEST
:
120 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
121 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
122 rose
->neighbour
->use
--;
127 if (!rose_validate_nr(sk
, nr
)) {
128 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
129 rose
->condition
= 0x00;
134 rose
->state
= ROSE_STATE_4
;
135 rose_start_t2timer(sk
);
136 rose_stop_idletimer(sk
);
138 rose_frames_acked(sk
, nr
);
139 if (frametype
== ROSE_RNR
) {
140 rose
->condition
|= ROSE_COND_PEER_RX_BUSY
;
142 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
147 case ROSE_DATA
: /* XXX */
148 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
149 if (!rose_validate_nr(sk
, nr
)) {
150 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
151 rose
->condition
= 0x00;
156 rose
->state
= ROSE_STATE_4
;
157 rose_start_t2timer(sk
);
158 rose_stop_idletimer(sk
);
161 rose_frames_acked(sk
, nr
);
162 if (ns
== rose
->vr
) {
163 rose_start_idletimer(sk
);
164 if (sk_filter_trim_cap(sk
, skb
, ROSE_MIN_LEN
) == 0 &&
165 __sock_queue_rcv_skb(sk
, skb
) == 0) {
166 rose
->vr
= (rose
->vr
+ 1) % ROSE_MODULUS
;
169 /* Should never happen ! */
170 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
171 rose
->condition
= 0x00;
176 rose
->state
= ROSE_STATE_4
;
177 rose_start_t2timer(sk
);
178 rose_stop_idletimer(sk
);
181 if (atomic_read(&sk
->sk_rmem_alloc
) >
182 (sk
->sk_rcvbuf
>> 1))
183 rose
->condition
|= ROSE_COND_OWN_RX_BUSY
;
186 * If the window is full, ack the frame, else start the
187 * acknowledge hold back timer.
189 if (((rose
->vl
+ sysctl_rose_window_size
) % ROSE_MODULUS
) == rose
->vr
) {
190 rose
->condition
&= ~ROSE_COND_ACK_PENDING
;
192 rose_enquiry_response(sk
);
194 rose
->condition
|= ROSE_COND_ACK_PENDING
;
195 rose_start_hbtimer(sk
);
200 printk(KERN_WARNING
"ROSE: unknown %02X in state 3\n", frametype
);
208 * State machine for state 4, Awaiting Reset Confirmation State.
209 * The handling of the timer(s) is in file rose_timer.c
210 * Handling of state 0 and connection release is in af_rose.c.
212 static int rose_state4_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
214 struct rose_sock
*rose
= rose_sk(sk
);
217 case ROSE_RESET_REQUEST
:
218 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
220 case ROSE_RESET_CONFIRMATION
:
222 rose_start_idletimer(sk
);
223 rose
->condition
= 0x00;
228 rose
->state
= ROSE_STATE_3
;
229 rose_requeue_frames(sk
);
232 case ROSE_CLEAR_REQUEST
:
233 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
234 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
235 rose
->neighbour
->use
--;
246 * State machine for state 5, Awaiting Call Acceptance State.
247 * The handling of the timer(s) is in file rose_timer.c
248 * Handling of state 0 and connection release is in af_rose.c.
250 static int rose_state5_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
252 if (frametype
== ROSE_CLEAR_REQUEST
) {
253 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
254 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
255 rose_sk(sk
)->neighbour
->use
--;
261 /* Higher level upcall for a LAPB frame */
262 int rose_process_rx_frame(struct sock
*sk
, struct sk_buff
*skb
)
264 struct rose_sock
*rose
= rose_sk(sk
);
265 int queued
= 0, frametype
, ns
, nr
, q
, d
, m
;
267 if (rose
->state
== ROSE_STATE_0
)
270 frametype
= rose_decode(skb
, &ns
, &nr
, &q
, &d
, &m
);
272 switch (rose
->state
) {
274 queued
= rose_state1_machine(sk
, skb
, frametype
);
277 queued
= rose_state2_machine(sk
, skb
, frametype
);
280 queued
= rose_state3_machine(sk
, skb
, frametype
, ns
, nr
, q
, d
, m
);
283 queued
= rose_state4_machine(sk
, skb
, frametype
);
286 queued
= rose_state5_machine(sk
, skb
, frametype
);