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 <asm/system.h>
30 #include <linux/fcntl.h>
32 #include <linux/interrupt.h>
36 * State machine for state 1, Awaiting Call Accepted State.
37 * The handling of the timer(s) is in file rose_timer.c.
38 * Handling of state 0 and connection release is in af_rose.c.
40 static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
42 struct rose_sock *rose = rose_sk(sk);
45 case ROSE_CALL_ACCEPTED:
47 rose_start_idletimer(sk);
48 rose->condition = 0x00;
53 rose->state = ROSE_STATE_3;
54 sk->sk_state = TCP_ESTABLISHED;
55 if (!sock_flag(sk, SOCK_DEAD))
56 sk->sk_state_change(sk);
59 case ROSE_CLEAR_REQUEST:
60 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
61 rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
62 rose->neighbour->use--;
73 * State machine for state 2, Awaiting Clear Confirmation State.
74 * The handling of the timer(s) is in file rose_timer.c
75 * Handling of state 0 and connection release is in af_rose.c.
77 static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
79 struct rose_sock *rose = rose_sk(sk);
82 case ROSE_CLEAR_REQUEST:
83 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
84 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
85 rose->neighbour->use--;
88 case ROSE_CLEAR_CONFIRMATION:
89 rose_disconnect(sk, 0, -1, -1);
90 rose->neighbour->use--;
101 * State machine for state 3, Connected State.
102 * The handling of the timer(s) is in file rose_timer.c
103 * Handling of state 0 and connection release is in af_rose.c.
105 static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
107 struct rose_sock *rose = rose_sk(sk);
111 case ROSE_RESET_REQUEST:
113 rose_start_idletimer(sk);
114 rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
115 rose->condition = 0x00;
120 rose_requeue_frames(sk);
123 case ROSE_CLEAR_REQUEST:
124 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
125 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
126 rose->neighbour->use--;
131 if (!rose_validate_nr(sk, nr)) {
132 rose_write_internal(sk, ROSE_RESET_REQUEST);
133 rose->condition = 0x00;
138 rose->state = ROSE_STATE_4;
139 rose_start_t2timer(sk);
140 rose_stop_idletimer(sk);
142 rose_frames_acked(sk, nr);
143 if (frametype == ROSE_RNR) {
144 rose->condition |= ROSE_COND_PEER_RX_BUSY;
146 rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
151 case ROSE_DATA: /* XXX */
152 rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
153 if (!rose_validate_nr(sk, nr)) {
154 rose_write_internal(sk, ROSE_RESET_REQUEST);
155 rose->condition = 0x00;
160 rose->state = ROSE_STATE_4;
161 rose_start_t2timer(sk);
162 rose_stop_idletimer(sk);
165 rose_frames_acked(sk, nr);
166 if (ns == rose->vr) {
167 rose_start_idletimer(sk);
168 if (sk_filter_trim_cap(sk, skb, ROSE_MIN_LEN) == 0 &&
169 __sock_queue_rcv_skb(sk, skb) == 0) {
170 rose->vr = (rose->vr + 1) % ROSE_MODULUS;
173 /* Should never happen ! */
174 rose_write_internal(sk, ROSE_RESET_REQUEST);
175 rose->condition = 0x00;
180 rose->state = ROSE_STATE_4;
181 rose_start_t2timer(sk);
182 rose_stop_idletimer(sk);
185 if (atomic_read(&sk->sk_rmem_alloc) >
186 (sk->sk_rcvbuf >> 1))
187 rose->condition |= ROSE_COND_OWN_RX_BUSY;
190 * If the window is full, ack the frame, else start the
191 * acknowledge hold back timer.
193 if (((rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == rose->vr) {
194 rose->condition &= ~ROSE_COND_ACK_PENDING;
196 rose_enquiry_response(sk);
198 rose->condition |= ROSE_COND_ACK_PENDING;
199 rose_start_hbtimer(sk);
204 printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
212 * State machine for state 4, Awaiting Reset Confirmation State.
213 * The handling of the timer(s) is in file rose_timer.c
214 * Handling of state 0 and connection release is in af_rose.c.
216 static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
218 struct rose_sock *rose = rose_sk(sk);
221 case ROSE_RESET_REQUEST:
222 rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
223 case ROSE_RESET_CONFIRMATION:
225 rose_start_idletimer(sk);
226 rose->condition = 0x00;
231 rose->state = ROSE_STATE_3;
232 rose_requeue_frames(sk);
235 case ROSE_CLEAR_REQUEST:
236 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
237 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
238 rose->neighbour->use--;
249 * State machine for state 5, Awaiting Call Acceptance State.
250 * The handling of the timer(s) is in file rose_timer.c
251 * Handling of state 0 and connection release is in af_rose.c.
253 static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
255 if (frametype == ROSE_CLEAR_REQUEST) {
256 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
257 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
258 rose_sk(sk)->neighbour->use--;
264 /* Higher level upcall for a LAPB frame */
265 int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
267 struct rose_sock *rose = rose_sk(sk);
268 int queued = 0, frametype, ns, nr, q, d, m;
270 if (rose->state == ROSE_STATE_0)
273 frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
275 switch (rose->state) {
277 queued = rose_state1_machine(sk, skb, frametype);
280 queued = rose_state2_machine(sk, skb, frametype);
283 queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
286 queued = rose_state4_machine(sk, skb, frametype);
289 queued = rose_state5_machine(sk, skb, frametype);