Import 2.1.42pre1
[davej-history.git] / net / x25 / x25_in.c
blob82e5c0817abd0e9872b8beaeec797f1e6d7a3faa
1 /*
2 * X.25 Packet Layer release 001
4 * This is ALPHA test software. This code may break your machine, randomly fail to work with new
5 * releases, misbehave and/or generally screw up. It might even work.
7 * This code REQUIRES 2.1.15 or higher
9 * This module:
10 * This module is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * History
16 * X.25 001 Jonathan Naylor Started coding.
19 #include <linux/config.h>
20 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/string.h>
29 #include <linux/sockios.h>
30 #include <linux/net.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <net/ip.h> /* For ip_rcv */
36 #include <asm/segment.h>
37 #include <asm/system.h>
38 #include <linux/fcntl.h>
39 #include <linux/mm.h>
40 #include <linux/interrupt.h>
41 #include <net/x25.h>
43 static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
45 struct sk_buff *skbo, *skbn = skb;
47 if (more) {
48 sk->protinfo.x25->fraglen += skb->len;
49 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
50 return 0;
53 if (!more && sk->protinfo.x25->fraglen > 0) { /* End of fragment */
54 sk->protinfo.x25->fraglen += skb->len;
55 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
57 if ((skbn = alloc_skb(sk->protinfo.x25->fraglen, GFP_ATOMIC)) == NULL)
58 return 1;
60 skb_set_owner_r(skbn, sk);
61 skbn->h.raw = skbn->data;
63 skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue);
64 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
65 kfree_skb(skbo, FREE_READ);
67 while ((skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue)) != NULL) {
68 skb_pull(skbo, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
69 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
70 kfree_skb(skbo, FREE_READ);
73 sk->protinfo.x25->fraglen = 0;
76 return sock_queue_rcv_skb(sk, skbn);
80 * State machine for state 1, Awaiting Call Accepted State.
81 * The handling of the timer(s) is in file x25_timer.c.
82 * Handling of state 0 and connection release is in af_x25.c.
84 static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
86 x25_address source_addr, dest_addr;
88 switch (frametype) {
90 case X25_CALL_ACCEPTED:
91 sk->protinfo.x25->condition = 0x00;
92 sk->protinfo.x25->timer = 0;
93 sk->protinfo.x25->vs = 0;
94 sk->protinfo.x25->va = 0;
95 sk->protinfo.x25->vr = 0;
96 sk->protinfo.x25->vl = 0;
97 sk->protinfo.x25->state = X25_STATE_3;
98 sk->state = TCP_ESTABLISHED;
100 * Parse the data in the frame.
102 skb_pull(skb, X25_STD_MIN_LEN);
103 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
104 skb_pull(skb, x25_parse_facilities(skb, &sk->protinfo.x25->facilities));
106 * Copy any Call User Data.
108 if (skb->len >= 0) {
109 memcpy(sk->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
110 sk->protinfo.x25->calluserdata.cudlength = skb->len;
112 if (!sk->dead)
113 sk->state_change(sk);
114 break;
116 case X25_CLEAR_REQUEST:
117 x25_clear_queues(sk);
118 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
119 sk->protinfo.x25->state = X25_STATE_0;
120 sk->state = TCP_CLOSE;
121 sk->err = ECONNREFUSED;
122 sk->shutdown |= SEND_SHUTDOWN;
123 if (!sk->dead)
124 sk->state_change(sk);
125 sk->dead = 1;
126 break;
128 default:
129 break;
132 return 0;
136 * State machine for state 2, Awaiting Clear Confirmation State.
137 * The handling of the timer(s) is in file x25_timer.c
138 * Handling of state 0 and connection release is in af_x25.c.
140 static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
142 switch (frametype) {
144 case X25_CLEAR_REQUEST:
145 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
146 case X25_CLEAR_CONFIRMATION:
147 x25_clear_queues(sk);
148 sk->protinfo.x25->state = X25_STATE_0;
149 sk->state = TCP_CLOSE;
150 sk->err = 0;
151 sk->shutdown |= SEND_SHUTDOWN;
152 if (!sk->dead)
153 sk->state_change(sk);
154 sk->dead = 1;
155 break;
157 default:
158 break;
161 return 0;
165 * State machine for state 3, Connected State.
166 * The handling of the timer(s) is in file x25_timer.c
167 * Handling of state 0 and connection release is in af_x25.c.
169 static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
171 int queued = 0;
172 int modulus;
174 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
176 switch (frametype) {
178 case X25_RESET_REQUEST:
179 x25_write_internal(sk, X25_RESET_CONFIRMATION);
180 sk->protinfo.x25->condition = 0x00;
181 sk->protinfo.x25->timer = 0;
182 sk->protinfo.x25->vs = 0;
183 sk->protinfo.x25->vr = 0;
184 sk->protinfo.x25->va = 0;
185 sk->protinfo.x25->vl = 0;
186 break;
188 case X25_CLEAR_REQUEST:
189 x25_clear_queues(sk);
190 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
191 sk->protinfo.x25->state = X25_STATE_0;
192 sk->state = TCP_CLOSE;
193 sk->err = 0;
194 sk->shutdown |= SEND_SHUTDOWN;
195 if (!sk->dead)
196 sk->state_change(sk);
197 sk->dead = 1;
198 break;
200 case X25_RR:
201 case X25_RNR:
202 if (frametype == X25_RNR) {
203 sk->protinfo.x25->condition |= X25_COND_PEER_RX_BUSY;
204 } else {
205 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
207 if (!x25_validate_nr(sk, nr)) {
208 x25_clear_queues(sk);
209 x25_write_internal(sk, X25_RESET_REQUEST);
210 sk->protinfo.x25->condition = 0x00;
211 sk->protinfo.x25->vs = 0;
212 sk->protinfo.x25->vr = 0;
213 sk->protinfo.x25->va = 0;
214 sk->protinfo.x25->vl = 0;
215 sk->protinfo.x25->state = X25_STATE_4;
216 sk->protinfo.x25->timer = sk->protinfo.x25->t22;
217 } else {
218 if (sk->protinfo.x25->condition & X25_COND_PEER_RX_BUSY) {
219 sk->protinfo.x25->va = nr;
220 } else {
221 x25_check_iframes_acked(sk, nr);
224 break;
226 case X25_DATA: /* XXX */
227 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
228 if (!x25_validate_nr(sk, nr)) {
229 x25_clear_queues(sk);
230 x25_write_internal(sk, X25_RESET_REQUEST);
231 sk->protinfo.x25->condition = 0x00;
232 sk->protinfo.x25->vs = 0;
233 sk->protinfo.x25->vr = 0;
234 sk->protinfo.x25->va = 0;
235 sk->protinfo.x25->vl = 0;
236 sk->protinfo.x25->state = X25_STATE_4;
237 sk->protinfo.x25->timer = sk->protinfo.x25->t22;
238 break;
240 if (sk->protinfo.x25->condition & X25_COND_PEER_RX_BUSY) {
241 sk->protinfo.x25->va = nr;
242 } else {
243 x25_check_iframes_acked(sk, nr);
245 if (sk->protinfo.x25->condition & X25_COND_OWN_RX_BUSY)
246 break;
247 if (ns == sk->protinfo.x25->vr) {
248 if (x25_queue_rx_frame(sk, skb, m) == 0) {
249 sk->protinfo.x25->vr = (sk->protinfo.x25->vr + 1) % modulus;
250 queued = 1;
251 } else {
252 sk->protinfo.x25->condition |= X25_COND_OWN_RX_BUSY;
256 * If the window is full Ack it immediately, else
257 * start the holdback timer.
259 if (((sk->protinfo.x25->vl + sk->protinfo.x25->facilities.winsize_in) % modulus) == sk->protinfo.x25->vr) {
260 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
261 sk->protinfo.x25->timer = 0;
262 x25_enquiry_response(sk);
263 } else {
264 sk->protinfo.x25->condition |= X25_COND_ACK_PENDING;
265 sk->protinfo.x25->timer = sk->protinfo.x25->t2;
267 break;
269 case X25_INTERRUPT_CONFIRMATION:
270 sk->protinfo.x25->intflag = 0;
271 break;
273 case X25_INTERRUPT:
274 if (sk->urginline) {
275 queued = (sock_queue_rcv_skb(sk, skb) == 0);
276 } else {
277 skb_set_owner_r(skb, sk);
278 skb_queue_tail(&sk->protinfo.x25->interrupt_in_queue, skb);
279 queued = 1;
281 if (sk->proc != 0) {
282 if (sk->proc > 0)
283 kill_proc(sk->proc, SIGURG, 1);
284 else
285 kill_pg(-sk->proc, SIGURG, 1);
287 x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
288 break;
290 default:
291 printk(KERN_WARNING "x25: unknown %02X in state 3\n", frametype);
292 break;
295 return queued;
299 * State machine for state 4, Awaiting Reset Confirmation State.
300 * The handling of the timer(s) is in file x25_timer.c
301 * Handling of state 0 and connection release is in af_x25.c.
303 static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
305 switch (frametype) {
307 case X25_RESET_REQUEST:
308 x25_write_internal(sk, X25_RESET_CONFIRMATION);
309 case X25_RESET_CONFIRMATION:
310 sk->protinfo.x25->timer = 0;
311 sk->protinfo.x25->condition = 0x00;
312 sk->protinfo.x25->va = 0;
313 sk->protinfo.x25->vr = 0;
314 sk->protinfo.x25->vs = 0;
315 sk->protinfo.x25->vl = 0;
316 sk->protinfo.x25->state = X25_STATE_3;
317 break;
319 case X25_CLEAR_REQUEST:
320 x25_clear_queues(sk);
321 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
322 sk->protinfo.x25->timer = 0;
323 sk->protinfo.x25->state = X25_STATE_0;
324 sk->state = TCP_CLOSE;
325 sk->err = 0;
326 sk->shutdown |= SEND_SHUTDOWN;
327 if (!sk->dead)
328 sk->state_change(sk);
329 sk->dead = 1;
330 break;
332 default:
333 break;
336 return 0;
339 /* Higher level upcall for a LAPB frame */
340 int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
342 int queued = 0, frametype, ns, nr, q, d, m;
344 if (sk->protinfo.x25->state == X25_STATE_0)
345 return 0;
347 del_timer(&sk->timer);
349 frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
351 switch (sk->protinfo.x25->state) {
352 case X25_STATE_1:
353 queued = x25_state1_machine(sk, skb, frametype);
354 break;
355 case X25_STATE_2:
356 queued = x25_state2_machine(sk, skb, frametype);
357 break;
358 case X25_STATE_3:
359 queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
360 break;
361 case X25_STATE_4:
362 queued = x25_state4_machine(sk, skb, frametype);
363 break;
366 x25_set_timer(sk);
368 return queued;
371 #endif