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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9 * Most of this code is based on the SDL diagrams published in the 7th ARRL
10 * Computer Networking Conference papers. The diagrams have mistakes in them,
11 * but are mostly correct. Before you modify the code could you read the SDL
12 * diagrams as the code is not obvious and probably very easy to break.
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
18 #include <linux/kernel.h>
19 #include <linux/timer.h>
20 #include <linux/string.h>
21 #include <linux/sockios.h>
22 #include <linux/net.h>
24 #include <linux/inet.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
28 #include <net/tcp_states.h>
29 #include <linux/fcntl.h>
31 #include <linux/interrupt.h>
35 * State machine for state 1, Awaiting Call Accepted State.
36 * The handling of the timer(s) is in file rose_timer.c.
37 * Handling of state 0 and connection release is in af_rose.c.
39 static int rose_state1_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
41 struct rose_sock
*rose
= rose_sk(sk
);
44 case ROSE_CALL_ACCEPTED
:
46 rose_start_idletimer(sk
);
47 rose
->condition
= 0x00;
52 rose
->state
= ROSE_STATE_3
;
53 sk
->sk_state
= TCP_ESTABLISHED
;
54 if (!sock_flag(sk
, SOCK_DEAD
))
55 sk
->sk_state_change(sk
);
58 case ROSE_CLEAR_REQUEST
:
59 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
60 rose_disconnect(sk
, ECONNREFUSED
, skb
->data
[3], skb
->data
[4]);
61 rose
->neighbour
->use
--;
72 * State machine for state 2, Awaiting Clear Confirmation State.
73 * The handling of the timer(s) is in file rose_timer.c
74 * Handling of state 0 and connection release is in af_rose.c.
76 static int rose_state2_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
78 struct rose_sock
*rose
= rose_sk(sk
);
81 case ROSE_CLEAR_REQUEST
:
82 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
83 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
84 rose
->neighbour
->use
--;
87 case ROSE_CLEAR_CONFIRMATION
:
88 rose_disconnect(sk
, 0, -1, -1);
89 rose
->neighbour
->use
--;
100 * State machine for state 3, Connected State.
101 * The handling of the timer(s) is in file rose_timer.c
102 * Handling of state 0 and connection release is in af_rose.c.
104 static int rose_state3_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int q
, int d
, int m
)
106 struct rose_sock
*rose
= rose_sk(sk
);
110 case ROSE_RESET_REQUEST
:
112 rose_start_idletimer(sk
);
113 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
114 rose
->condition
= 0x00;
119 rose_requeue_frames(sk
);
122 case ROSE_CLEAR_REQUEST
:
123 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
124 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
125 rose
->neighbour
->use
--;
130 if (!rose_validate_nr(sk
, nr
)) {
131 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
132 rose
->condition
= 0x00;
137 rose
->state
= ROSE_STATE_4
;
138 rose_start_t2timer(sk
);
139 rose_stop_idletimer(sk
);
141 rose_frames_acked(sk
, nr
);
142 if (frametype
== ROSE_RNR
) {
143 rose
->condition
|= ROSE_COND_PEER_RX_BUSY
;
145 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
150 case ROSE_DATA
: /* XXX */
151 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
152 if (!rose_validate_nr(sk
, nr
)) {
153 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
154 rose
->condition
= 0x00;
159 rose
->state
= ROSE_STATE_4
;
160 rose_start_t2timer(sk
);
161 rose_stop_idletimer(sk
);
164 rose_frames_acked(sk
, nr
);
165 if (ns
== rose
->vr
) {
166 rose_start_idletimer(sk
);
167 if (sock_queue_rcv_skb(sk
, skb
) == 0) {
168 rose
->vr
= (rose
->vr
+ 1) % ROSE_MODULUS
;
171 /* Should never happen ! */
172 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
173 rose
->condition
= 0x00;
178 rose
->state
= ROSE_STATE_4
;
179 rose_start_t2timer(sk
);
180 rose_stop_idletimer(sk
);
183 if (atomic_read(&sk
->sk_rmem_alloc
) >
184 (sk
->sk_rcvbuf
>> 1))
185 rose
->condition
|= ROSE_COND_OWN_RX_BUSY
;
188 * If the window is full, ack the frame, else start the
189 * acknowledge hold back timer.
191 if (((rose
->vl
+ sysctl_rose_window_size
) % ROSE_MODULUS
) == rose
->vr
) {
192 rose
->condition
&= ~ROSE_COND_ACK_PENDING
;
194 rose_enquiry_response(sk
);
196 rose
->condition
|= ROSE_COND_ACK_PENDING
;
197 rose_start_hbtimer(sk
);
202 printk(KERN_WARNING
"ROSE: unknown %02X in state 3\n", frametype
);
210 * State machine for state 4, Awaiting Reset Confirmation State.
211 * The handling of the timer(s) is in file rose_timer.c
212 * Handling of state 0 and connection release is in af_rose.c.
214 static int rose_state4_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
216 struct rose_sock
*rose
= rose_sk(sk
);
219 case ROSE_RESET_REQUEST
:
220 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
);