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/sched.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
29 #include <net/ip.h> /* For ip_rcv */
31 #include <asm/system.h>
32 #include <linux/fcntl.h>
34 #include <linux/interrupt.h>
38 * State machine for state 1, Awaiting Call Accepted State.
39 * The handling of the timer(s) is in file rose_timer.c.
40 * Handling of state 0 and connection release is in af_rose.c.
42 static int rose_state1_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
44 struct rose_sock
*rose
= rose_sk(sk
);
47 case ROSE_CALL_ACCEPTED
:
49 rose_start_idletimer(sk
);
50 rose
->condition
= 0x00;
55 rose
->state
= ROSE_STATE_3
;
56 sk
->sk_state
= TCP_ESTABLISHED
;
57 if (!sock_flag(sk
, SOCK_DEAD
))
58 sk
->sk_state_change(sk
);
61 case ROSE_CLEAR_REQUEST
:
62 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
63 rose_disconnect(sk
, ECONNREFUSED
, skb
->data
[3], skb
->data
[4]);
64 rose
->neighbour
->use
--;
75 * State machine for state 2, Awaiting Clear Confirmation State.
76 * The handling of the timer(s) is in file rose_timer.c
77 * Handling of state 0 and connection release is in af_rose.c.
79 static int rose_state2_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
81 struct rose_sock
*rose
= rose_sk(sk
);
84 case ROSE_CLEAR_REQUEST
:
85 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
86 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
87 rose
->neighbour
->use
--;
90 case ROSE_CLEAR_CONFIRMATION
:
91 rose_disconnect(sk
, 0, -1, -1);
92 rose
->neighbour
->use
--;
103 * State machine for state 3, Connected State.
104 * The handling of the timer(s) is in file rose_timer.c
105 * Handling of state 0 and connection release is in af_rose.c.
107 static int rose_state3_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
, int ns
, int nr
, int q
, int d
, int m
)
109 struct rose_sock
*rose
= rose_sk(sk
);
113 case ROSE_RESET_REQUEST
:
115 rose_start_idletimer(sk
);
116 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
117 rose
->condition
= 0x00;
122 rose_requeue_frames(sk
);
125 case ROSE_CLEAR_REQUEST
:
126 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
127 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
128 rose
->neighbour
->use
--;
133 if (!rose_validate_nr(sk
, nr
)) {
134 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
135 rose
->condition
= 0x00;
140 rose
->state
= ROSE_STATE_4
;
141 rose_start_t2timer(sk
);
142 rose_stop_idletimer(sk
);
144 rose_frames_acked(sk
, nr
);
145 if (frametype
== ROSE_RNR
) {
146 rose
->condition
|= ROSE_COND_PEER_RX_BUSY
;
148 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
153 case ROSE_DATA
: /* XXX */
154 rose
->condition
&= ~ROSE_COND_PEER_RX_BUSY
;
155 if (!rose_validate_nr(sk
, nr
)) {
156 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
157 rose
->condition
= 0x00;
162 rose
->state
= ROSE_STATE_4
;
163 rose_start_t2timer(sk
);
164 rose_stop_idletimer(sk
);
167 rose_frames_acked(sk
, nr
);
168 if (ns
== rose
->vr
) {
169 rose_start_idletimer(sk
);
170 if (sock_queue_rcv_skb(sk
, skb
) == 0) {
171 rose
->vr
= (rose
->vr
+ 1) % ROSE_MODULUS
;
174 /* Should never happen ! */
175 rose_write_internal(sk
, ROSE_RESET_REQUEST
);
176 rose
->condition
= 0x00;
181 rose
->state
= ROSE_STATE_4
;
182 rose_start_t2timer(sk
);
183 rose_stop_idletimer(sk
);
186 if (atomic_read(&sk
->sk_rmem_alloc
) >
188 rose
->condition
|= ROSE_COND_OWN_RX_BUSY
;
191 * If the window is full, ack the frame, else start the
192 * acknowledge hold back timer.
194 if (((rose
->vl
+ sysctl_rose_window_size
) % ROSE_MODULUS
) == rose
->vr
) {
195 rose
->condition
&= ~ROSE_COND_ACK_PENDING
;
197 rose_enquiry_response(sk
);
199 rose
->condition
|= ROSE_COND_ACK_PENDING
;
200 rose_start_hbtimer(sk
);
205 printk(KERN_WARNING
"ROSE: unknown %02X in state 3\n", frametype
);
213 * State machine for state 4, Awaiting Reset Confirmation State.
214 * The handling of the timer(s) is in file rose_timer.c
215 * Handling of state 0 and connection release is in af_rose.c.
217 static int rose_state4_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
219 struct rose_sock
*rose
= rose_sk(sk
);
222 case ROSE_RESET_REQUEST
:
223 rose_write_internal(sk
, ROSE_RESET_CONFIRMATION
);
224 case ROSE_RESET_CONFIRMATION
:
226 rose_start_idletimer(sk
);
227 rose
->condition
= 0x00;
232 rose
->state
= ROSE_STATE_3
;
233 rose_requeue_frames(sk
);
236 case ROSE_CLEAR_REQUEST
:
237 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
238 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
239 rose
->neighbour
->use
--;
250 * State machine for state 5, Awaiting Call Acceptance State.
251 * The handling of the timer(s) is in file rose_timer.c
252 * Handling of state 0 and connection release is in af_rose.c.
254 static int rose_state5_machine(struct sock
*sk
, struct sk_buff
*skb
, int frametype
)
256 if (frametype
== ROSE_CLEAR_REQUEST
) {
257 rose_write_internal(sk
, ROSE_CLEAR_CONFIRMATION
);
258 rose_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
259 rose_sk(sk
)->neighbour
->use
--;
265 /* Higher level upcall for a LAPB frame */
266 int rose_process_rx_frame(struct sock
*sk
, struct sk_buff
*skb
)
268 struct rose_sock
*rose
= rose_sk(sk
);
269 int queued
= 0, frametype
, ns
, nr
, q
, d
, m
;
271 if (rose
->state
== ROSE_STATE_0
)
274 frametype
= rose_decode(skb
, &ns
, &nr
, &q
, &d
, &m
);
276 switch (rose
->state
) {
278 queued
= rose_state1_machine(sk
, skb
, frametype
);
281 queued
= rose_state2_machine(sk
, skb
, frametype
);
284 queued
= rose_state3_machine(sk
, skb
, frametype
, ns
, nr
, q
, d
, m
);
287 queued
= rose_state4_machine(sk
, skb
, frametype
);
290 queued
= rose_state5_machine(sk
, skb
, frametype
);