Import 2.3.13
[davej-history.git] / net / x25 / x25_in.c
blobad7adb7ea558d1a8f8783a022661b0803bd06f90
1 /*
2 * X.25 Packet Layer release 002
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.
17 * X.25 002 Jonathan Naylor Centralised disconnection code.
18 * New timer architecture.
21 #include <linux/config.h>
22 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/in.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/timer.h>
30 #include <linux/string.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <net/sock.h>
37 #include <net/ip.h> /* For ip_rcv */
38 #include <asm/segment.h>
39 #include <asm/system.h>
40 #include <linux/fcntl.h>
41 #include <linux/mm.h>
42 #include <linux/interrupt.h>
43 #include <net/x25.h>
45 static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
47 struct sk_buff *skbo, *skbn = skb;
49 if (more) {
50 sk->protinfo.x25->fraglen += skb->len;
51 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
52 skb_set_owner_r(skb, sk);
53 return 0;
56 if (!more && sk->protinfo.x25->fraglen > 0) { /* End of fragment */
57 int len = sk->protinfo.x25->fraglen + skb->len;
59 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){
60 kfree_skb(skb);
61 return 1;
64 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
66 skbn->h.raw = skbn->data;
68 skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue);
69 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
70 kfree_skb(skbo);
72 while ((skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue)) != NULL) {
73 skb_pull(skbo, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
74 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
75 kfree_skb(skbo);
78 sk->protinfo.x25->fraglen = 0;
81 skb_set_owner_r(skbn, sk);
82 skb_queue_tail(&sk->receive_queue, skbn);
83 if (!sk->dead)
84 sk->data_ready(sk,skbn->len);
86 return 0;
90 * State machine for state 1, Awaiting Call Accepted State.
91 * The handling of the timer(s) is in file x25_timer.c.
92 * Handling of state 0 and connection release is in af_x25.c.
94 static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
96 x25_address source_addr, dest_addr;
98 switch (frametype) {
100 case X25_CALL_ACCEPTED:
101 x25_stop_timer(sk);
102 sk->protinfo.x25->condition = 0x00;
103 sk->protinfo.x25->vs = 0;
104 sk->protinfo.x25->va = 0;
105 sk->protinfo.x25->vr = 0;
106 sk->protinfo.x25->vl = 0;
107 sk->protinfo.x25->state = X25_STATE_3;
108 sk->state = TCP_ESTABLISHED;
110 * Parse the data in the frame.
112 skb_pull(skb, X25_STD_MIN_LEN);
113 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
114 skb_pull(skb, x25_parse_facilities(skb, &sk->protinfo.x25->facilities));
116 * Copy any Call User Data.
118 if (skb->len >= 0) {
119 memcpy(sk->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
120 sk->protinfo.x25->calluserdata.cudlength = skb->len;
122 if (!sk->dead)
123 sk->state_change(sk);
124 break;
126 case X25_CLEAR_REQUEST:
127 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
128 x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
129 break;
131 default:
132 break;
135 return 0;
139 * State machine for state 2, Awaiting Clear Confirmation State.
140 * The handling of the timer(s) is in file x25_timer.c
141 * Handling of state 0 and connection release is in af_x25.c.
143 static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
145 switch (frametype) {
147 case X25_CLEAR_REQUEST:
148 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
149 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
150 break;
152 case X25_CLEAR_CONFIRMATION:
153 x25_disconnect(sk, 0, 0, 0);
154 break;
156 default:
157 break;
160 return 0;
164 * State machine for state 3, Connected State.
165 * The handling of the timer(s) is in file x25_timer.c
166 * Handling of state 0 and connection release is in af_x25.c.
168 static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
170 int queued = 0;
171 int modulus;
173 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
175 switch (frametype) {
177 case X25_RESET_REQUEST:
178 x25_write_internal(sk, X25_RESET_CONFIRMATION);
179 x25_stop_timer(sk);
180 sk->protinfo.x25->condition = 0x00;
181 sk->protinfo.x25->vs = 0;
182 sk->protinfo.x25->vr = 0;
183 sk->protinfo.x25->va = 0;
184 sk->protinfo.x25->vl = 0;
185 x25_requeue_frames(sk);
186 break;
188 case X25_CLEAR_REQUEST:
189 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
190 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
191 break;
193 case X25_RR:
194 case X25_RNR:
195 if (!x25_validate_nr(sk, nr)) {
196 x25_clear_queues(sk);
197 x25_write_internal(sk, X25_RESET_REQUEST);
198 x25_start_t22timer(sk);
199 sk->protinfo.x25->condition = 0x00;
200 sk->protinfo.x25->vs = 0;
201 sk->protinfo.x25->vr = 0;
202 sk->protinfo.x25->va = 0;
203 sk->protinfo.x25->vl = 0;
204 sk->protinfo.x25->state = X25_STATE_4;
205 } else {
206 x25_frames_acked(sk, nr);
207 if (frametype == X25_RNR) {
208 sk->protinfo.x25->condition |= X25_COND_PEER_RX_BUSY;
209 } else {
210 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
213 break;
215 case X25_DATA: /* XXX */
216 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
217 if (!x25_validate_nr(sk, nr)) {
218 x25_clear_queues(sk);
219 x25_write_internal(sk, X25_RESET_REQUEST);
220 x25_start_t22timer(sk);
221 sk->protinfo.x25->condition = 0x00;
222 sk->protinfo.x25->vs = 0;
223 sk->protinfo.x25->vr = 0;
224 sk->protinfo.x25->va = 0;
225 sk->protinfo.x25->vl = 0;
226 sk->protinfo.x25->state = X25_STATE_4;
227 break;
229 x25_frames_acked(sk, nr);
230 if (ns == sk->protinfo.x25->vr) {
231 if (x25_queue_rx_frame(sk, skb, m) == 0) {
232 sk->protinfo.x25->vr = (sk->protinfo.x25->vr + 1) % modulus;
233 queued = 1;
234 } else {
235 /* Should never happen */
236 x25_clear_queues(sk);
237 x25_write_internal(sk, X25_RESET_REQUEST);
238 x25_start_t22timer(sk);
239 sk->protinfo.x25->condition = 0x00;
240 sk->protinfo.x25->vs = 0;
241 sk->protinfo.x25->vr = 0;
242 sk->protinfo.x25->va = 0;
243 sk->protinfo.x25->vl = 0;
244 sk->protinfo.x25->state = X25_STATE_4;
245 break;
247 if (atomic_read(&sk->rmem_alloc) > (sk->rcvbuf / 2))
248 sk->protinfo.x25->condition |= X25_COND_OWN_RX_BUSY;
251 * If the window is full Ack it immediately, else
252 * start the holdback timer.
254 if (((sk->protinfo.x25->vl + sk->protinfo.x25->facilities.winsize_in) % modulus) == sk->protinfo.x25->vr) {
255 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
256 x25_stop_timer(sk);
257 x25_enquiry_response(sk);
258 } else {
259 sk->protinfo.x25->condition |= X25_COND_ACK_PENDING;
260 x25_start_t2timer(sk);
262 break;
264 case X25_INTERRUPT_CONFIRMATION:
265 sk->protinfo.x25->intflag = 0;
266 break;
268 case X25_INTERRUPT:
269 if (sk->urginline) {
270 queued = (sock_queue_rcv_skb(sk, skb) == 0);
271 } else {
272 skb_set_owner_r(skb, sk);
273 skb_queue_tail(&sk->protinfo.x25->interrupt_in_queue, skb);
274 queued = 1;
276 if (sk->proc != 0) {
277 if (sk->proc > 0)
278 kill_proc(sk->proc, SIGURG, 1);
279 else
280 kill_pg(-sk->proc, SIGURG, 1);
282 x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
283 break;
285 default:
286 printk(KERN_WARNING "x25: unknown %02X in state 3\n", frametype);
287 break;
290 return queued;
294 * State machine for state 4, Awaiting Reset Confirmation State.
295 * The handling of the timer(s) is in file x25_timer.c
296 * Handling of state 0 and connection release is in af_x25.c.
298 static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
300 switch (frametype) {
302 case X25_RESET_REQUEST:
303 x25_write_internal(sk, X25_RESET_CONFIRMATION);
304 case X25_RESET_CONFIRMATION:
305 x25_stop_timer(sk);
306 sk->protinfo.x25->condition = 0x00;
307 sk->protinfo.x25->va = 0;
308 sk->protinfo.x25->vr = 0;
309 sk->protinfo.x25->vs = 0;
310 sk->protinfo.x25->vl = 0;
311 sk->protinfo.x25->state = X25_STATE_3;
312 x25_requeue_frames(sk);
313 break;
315 case X25_CLEAR_REQUEST:
316 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
317 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
318 break;
320 default:
321 break;
324 return 0;
327 /* Higher level upcall for a LAPB frame */
328 int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
330 int queued = 0, frametype, ns, nr, q, d, m;
332 if (sk->protinfo.x25->state == X25_STATE_0)
333 return 0;
335 frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
337 switch (sk->protinfo.x25->state) {
338 case X25_STATE_1:
339 queued = x25_state1_machine(sk, skb, frametype);
340 break;
341 case X25_STATE_2:
342 queued = x25_state2_machine(sk, skb, frametype);
343 break;
344 case X25_STATE_3:
345 queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
346 break;
347 case X25_STATE_4:
348 queued = x25_state4_machine(sk, skb, frametype);
349 break;
352 x25_kick(sk);
354 return queued;
357 #endif