Import 2.3.10pre3
[davej-history.git] / net / rose / rose_in.c
blobbe86c9e16725bfb7c5277fb13cc213fada370c3d
1 /*
2 * ROSE release 003
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
6 * This module:
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Most of this code is based on the SDL diagrams published in the 7th
13 * ARRL Computer Networking Conference papers. The diagrams have mistakes
14 * in them, but are mostly correct. Before you modify the code could you
15 * read the SDL diagrams as the code is not obvious and probably very
16 * easy to break;
18 * History
19 * ROSE 001 Jonathan(G4KLX) Cloned from nr_in.c
20 * ROSE 002 Jonathan(G4KLX) Return cause and diagnostic codes from Clear Requests.
21 * ROSE 003 Jonathan(G4KLX) New timer architecture.
22 * Removed M bit processing.
25 #include <linux/config.h>
26 #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/socket.h>
30 #include <linux/in.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/string.h>
35 #include <linux/sockios.h>
36 #include <linux/net.h>
37 #include <net/ax25.h>
38 #include <linux/inet.h>
39 #include <linux/netdevice.h>
40 #include <linux/skbuff.h>
41 #include <net/sock.h>
42 #include <net/ip.h> /* For ip_rcv */
43 #include <asm/segment.h>
44 #include <asm/system.h>
45 #include <linux/fcntl.h>
46 #include <linux/mm.h>
47 #include <linux/interrupt.h>
48 #include <net/rose.h>
51 * State machine for state 1, Awaiting Call Accepted State.
52 * The handling of the timer(s) is in file rose_timer.c.
53 * Handling of state 0 and connection release is in af_rose.c.
55 static int rose_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
57 switch (frametype) {
59 case ROSE_CALL_ACCEPTED:
60 rose_stop_timer(sk);
61 rose_start_idletimer(sk);
62 sk->protinfo.rose->condition = 0x00;
63 sk->protinfo.rose->vs = 0;
64 sk->protinfo.rose->va = 0;
65 sk->protinfo.rose->vr = 0;
66 sk->protinfo.rose->vl = 0;
67 sk->protinfo.rose->state = ROSE_STATE_3;
68 sk->state = TCP_ESTABLISHED;
69 if (!sk->dead)
70 sk->state_change(sk);
71 break;
73 case ROSE_CLEAR_REQUEST:
74 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
75 rose_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
76 sk->protinfo.rose->neighbour->use--;
77 break;
79 default:
80 break;
83 return 0;
87 * State machine for state 2, Awaiting Clear Confirmation State.
88 * The handling of the timer(s) is in file rose_timer.c
89 * Handling of state 0 and connection release is in af_rose.c.
91 static int rose_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
93 switch (frametype) {
95 case ROSE_CLEAR_REQUEST:
96 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
97 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
98 sk->protinfo.rose->neighbour->use--;
99 break;
101 case ROSE_CLEAR_CONFIRMATION:
102 rose_disconnect(sk, 0, -1, -1);
103 sk->protinfo.rose->neighbour->use--;
104 break;
106 default:
107 break;
110 return 0;
114 * State machine for state 3, Connected State.
115 * The handling of the timer(s) is in file rose_timer.c
116 * Handling of state 0 and connection release is in af_rose.c.
118 static int rose_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
120 int queued = 0;
122 switch (frametype) {
124 case ROSE_RESET_REQUEST:
125 rose_stop_timer(sk);
126 rose_start_idletimer(sk);
127 rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
128 sk->protinfo.rose->condition = 0x00;
129 sk->protinfo.rose->vs = 0;
130 sk->protinfo.rose->vr = 0;
131 sk->protinfo.rose->va = 0;
132 sk->protinfo.rose->vl = 0;
133 rose_requeue_frames(sk);
134 break;
136 case ROSE_CLEAR_REQUEST:
137 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
138 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
139 sk->protinfo.rose->neighbour->use--;
140 break;
142 case ROSE_RR:
143 case ROSE_RNR:
144 if (!rose_validate_nr(sk, nr)) {
145 rose_write_internal(sk, ROSE_RESET_REQUEST);
146 sk->protinfo.rose->condition = 0x00;
147 sk->protinfo.rose->vs = 0;
148 sk->protinfo.rose->vr = 0;
149 sk->protinfo.rose->va = 0;
150 sk->protinfo.rose->vl = 0;
151 sk->protinfo.rose->state = ROSE_STATE_4;
152 rose_start_t2timer(sk);
153 rose_stop_idletimer(sk);
154 } else {
155 rose_frames_acked(sk, nr);
156 if (frametype == ROSE_RNR) {
157 sk->protinfo.rose->condition |= ROSE_COND_PEER_RX_BUSY;
158 } else {
159 sk->protinfo.rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
162 break;
164 case ROSE_DATA: /* XXX */
165 sk->protinfo.rose->condition &= ~ROSE_COND_PEER_RX_BUSY;
166 if (!rose_validate_nr(sk, nr)) {
167 rose_write_internal(sk, ROSE_RESET_REQUEST);
168 sk->protinfo.rose->condition = 0x00;
169 sk->protinfo.rose->vs = 0;
170 sk->protinfo.rose->vr = 0;
171 sk->protinfo.rose->va = 0;
172 sk->protinfo.rose->vl = 0;
173 sk->protinfo.rose->state = ROSE_STATE_4;
174 rose_start_t2timer(sk);
175 rose_stop_idletimer(sk);
176 break;
178 rose_frames_acked(sk, nr);
179 if (ns == sk->protinfo.rose->vr) {
180 rose_start_idletimer(sk);
181 if (sock_queue_rcv_skb(sk, skb) == 0) {
182 sk->protinfo.rose->vr = (sk->protinfo.rose->vr + 1) % ROSE_MODULUS;
183 queued = 1;
184 } else {
185 /* Should never happen ! */
186 rose_write_internal(sk, ROSE_RESET_REQUEST);
187 sk->protinfo.rose->condition = 0x00;
188 sk->protinfo.rose->vs = 0;
189 sk->protinfo.rose->vr = 0;
190 sk->protinfo.rose->va = 0;
191 sk->protinfo.rose->vl = 0;
192 sk->protinfo.rose->state = ROSE_STATE_4;
193 rose_start_t2timer(sk);
194 rose_stop_idletimer(sk);
195 break;
197 if (atomic_read(&sk->rmem_alloc) > (sk->rcvbuf / 2))
198 sk->protinfo.rose->condition |= ROSE_COND_OWN_RX_BUSY;
201 * If the window is full, ack the frame, else start the
202 * acknowledge hold back timer.
204 if (((sk->protinfo.rose->vl + sysctl_rose_window_size) % ROSE_MODULUS) == sk->protinfo.rose->vr) {
205 sk->protinfo.rose->condition &= ~ROSE_COND_ACK_PENDING;
206 rose_stop_timer(sk);
207 rose_enquiry_response(sk);
208 } else {
209 sk->protinfo.rose->condition |= ROSE_COND_ACK_PENDING;
210 rose_start_hbtimer(sk);
212 break;
214 default:
215 printk(KERN_WARNING "ROSE: unknown %02X in state 3\n", frametype);
216 break;
219 return queued;
223 * State machine for state 4, Awaiting Reset Confirmation State.
224 * The handling of the timer(s) is in file rose_timer.c
225 * Handling of state 0 and connection release is in af_rose.c.
227 static int rose_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
229 switch (frametype) {
231 case ROSE_RESET_REQUEST:
232 rose_write_internal(sk, ROSE_RESET_CONFIRMATION);
233 case ROSE_RESET_CONFIRMATION:
234 rose_stop_timer(sk);
235 rose_start_idletimer(sk);
236 sk->protinfo.rose->condition = 0x00;
237 sk->protinfo.rose->va = 0;
238 sk->protinfo.rose->vr = 0;
239 sk->protinfo.rose->vs = 0;
240 sk->protinfo.rose->vl = 0;
241 sk->protinfo.rose->state = ROSE_STATE_3;
242 rose_requeue_frames(sk);
243 break;
245 case ROSE_CLEAR_REQUEST:
246 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
247 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
248 sk->protinfo.rose->neighbour->use--;
249 break;
251 default:
252 break;
255 return 0;
259 * State machine for state 5, Awaiting Call Acceptance State.
260 * The handling of the timer(s) is in file rose_timer.c
261 * Handling of state 0 and connection release is in af_rose.c.
263 static int rose_state5_machine(struct sock *sk, struct sk_buff *skb, int frametype)
265 if (frametype == ROSE_CLEAR_REQUEST) {
266 rose_write_internal(sk, ROSE_CLEAR_CONFIRMATION);
267 rose_disconnect(sk, 0, skb->data[3], skb->data[4]);
268 sk->protinfo.rose->neighbour->use--;
271 return 0;
274 /* Higher level upcall for a LAPB frame */
275 int rose_process_rx_frame(struct sock *sk, struct sk_buff *skb)
277 int queued = 0, frametype, ns, nr, q, d, m;
279 if (sk->protinfo.rose->state == ROSE_STATE_0)
280 return 0;
282 frametype = rose_decode(skb, &ns, &nr, &q, &d, &m);
284 switch (sk->protinfo.rose->state) {
285 case ROSE_STATE_1:
286 queued = rose_state1_machine(sk, skb, frametype);
287 break;
288 case ROSE_STATE_2:
289 queued = rose_state2_machine(sk, skb, frametype);
290 break;
291 case ROSE_STATE_3:
292 queued = rose_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
293 break;
294 case ROSE_STATE_4:
295 queued = rose_state4_machine(sk, skb, frametype);
296 break;
297 case ROSE_STATE_5:
298 queued = rose_state5_machine(sk, skb, frametype);
299 break;
302 rose_kick(sk);
304 return queued;
307 #endif