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/filter.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
16 #include <linux/kernel.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/sockios.h>
20 #include <linux/net.h>
22 #include <linux/inet.h>
23 #include <linux/netdevice.h>
24 #include <linux/skbuff.h>
26 #include <net/tcp_states.h>
27 #include <linux/fcntl.h>
29 #include <linux/interrupt.h>
33 * State machine for state 1, Awaiting Call Accepted State.
34 * The handling of the timer(s) is in file rose_timer.c.
35 * Handling of state 0 and connection release is in af_rose.c.
37 static int rose_state1_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
39 struct rose_sock
*rose
= rose_sk(sk
);
42 case ROSE_CALL_ACCEPTED
:
44 rose_start_idletimer(sk
);
45 rose
->condition
= 0x00;
50 rose
->state
= ROSE_STATE_3
;
51 sk
->sk_state
= TCP_ESTABLISHED
;
52 if (!sock_flag(sk
, SOCK_DEAD
))
53 sk
->sk_state_change(sk
);
56 case ROSE_CLEAR_REQUEST
:
57 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
58 rose_disconnect(sk
, ECONNREFUSED
, skb
->data
[3], skb
->data
[4]);
59 rose
->neighbour
->use
--;
70 * State machine for state 2, Awaiting Clear Confirmation State.
71 * The handling of the timer(s) is in file rose_timer.c
72 * Handling of state 0 and connection release is in af_rose.c.
74 static int rose_state2_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
76 struct rose_sock
*rose
= rose_sk(sk
);
79 case ROSE_CLEAR_REQUEST
:
80 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
81 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
82 rose
->neighbour
->use
--;
85 case ROSE_CLEAR_CONFIRMATION
:
86 rose_disconnect(sk
, 0, -1, -1);
87 rose
->neighbour
->use
--;
98 * State machine for state 3, Connected State.
99 * The handling of the timer(s) is in file rose_timer.c
100 * Handling of state 0 and connection release is in af_rose.c.
102 static int rose_state3_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int q
, int d
, int m
)
104 struct rose_sock
*rose
= rose_sk(sk
);
108 case ROSE_RESET_REQUEST
:
110 rose_start_idletimer(sk
);
111 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
112 rose
->condition
= 0x00;
117 rose_requeue_frames(sk
);
120 case ROSE_CLEAR_REQUEST
:
121 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
122 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
123 rose
->neighbour
->use
--;
128 if (!rose_validate_nr(sk
, nr
)) {
129 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
130 rose
->condition
= 0x00;
135 rose
->state
= ROSE_STATE_4
;
136 rose_start_t2timer(sk
);
137 rose_stop_idletimer(sk
);
139 rose_frames_acked(sk
, nr
);
140 if (frametype
== ROSE_RNR
) {
141 rose
->condition
|= ROSE_COND_PEER_RX_BUSY
;
143 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
148 case ROSE_DATA
: /* XXX */
149 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
150 if (!rose_validate_nr(sk
, nr
)) {
151 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
152 rose
->condition
= 0x00;
157 rose
->state
= ROSE_STATE_4
;
158 rose_start_t2timer(sk
);
159 rose_stop_idletimer(sk
);
162 rose_frames_acked(sk
, nr
);
163 if (ns
== rose
->vr
) {
164 rose_start_idletimer(sk
);
165 if (sk_filter_trim_cap(sk
, skb
, ROSE_MIN_LEN
) == 0 &&
166 __sock_queue_rcv_skb(sk
, skb
) == 0) {
167 rose
->vr
= (rose
->vr
+ 1) % ROSE_MODULUS
;
170 /* Should never happen ! */
171 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
172 rose
->condition
= 0x00;
177 rose
->state
= ROSE_STATE_4
;
178 rose_start_t2timer(sk
);
179 rose_stop_idletimer(sk
);
182 if (atomic_read(&sk
->sk_rmem_alloc
) >
183 (sk
->sk_rcvbuf
>> 1))
184 rose
->condition
|= ROSE_COND_OWN_RX_BUSY
;
187 * If the window is full, ack the frame, else start the
188 * acknowledge hold back timer.
190 if (((rose
->vl
+ sysctl_rose_window_size
) % ROSE_MODULUS
) == rose
->vr
) {
191 rose
->condition
&= ~ROSE_COND_ACK_PENDING
;
193 rose_enquiry_response(sk
);
195 rose
->condition
|= ROSE_COND_ACK_PENDING
;
196 rose_start_hbtimer(sk
);
201 printk(KERN_WARNING
"ROSE: unknown %02X in state 3\n", frametype
);
209 * State machine for state 4, Awaiting Reset Confirmation State.
210 * The handling of the timer(s) is in file rose_timer.c
211 * Handling of state 0 and connection release is in af_rose.c.
213 static int rose_state4_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
215 struct rose_sock
*rose
= rose_sk(sk
);
218 case ROSE_RESET_REQUEST
:
219 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
221 case ROSE_RESET_CONFIRMATION
:
223 rose_start_idletimer(sk
);
224 rose
->condition
= 0x00;
229 rose
->state
= ROSE_STATE_3
;
230 rose_requeue_frames(sk
);
233 case ROSE_CLEAR_REQUEST
:
234 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
235 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
236 rose
->neighbour
->use
--;
247 * State machine for state 5, Awaiting Call Acceptance State.
248 * The handling of the timer(s) is in file rose_timer.c
249 * Handling of state 0 and connection release is in af_rose.c.
251 static int rose_state5_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
253 if (frametype
== ROSE_CLEAR_REQUEST
) {
254 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
255 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
256 rose_sk(sk
)->neighbour
->use
--;
262 /* Higher level upcall for a LAPB frame */
263 int rose_process_rx_frame(struct sock
*sk
, struct sk_buff
*skb
)
265 struct rose_sock
*rose
= rose_sk(sk
);
266 int queued
= 0, frametype
, ns
, nr
, q
, d
, m
;
268 if (rose
->state
== ROSE_STATE_0
)
271 frametype
= rose_decode(skb
, &ns
, &nr
, &q
, &d
, &m
);
273 switch (rose
->state
) {
275 queued
= rose_state1_machine(sk
, skb
, frametype
);
278 queued
= rose_state2_machine(sk
, skb
, frametype
);
281 queued
= rose_state3_machine(sk
, skb
, frametype
, ns
, nr
, q
, d
, m
);
284 queued
= rose_state4_machine(sk
, skb
, frametype
);
287 queued
= rose_state5_machine(sk
, skb
, frametype
);