- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / net / x25 / x25_in.c
blobbcb5f1cf49b6528e6ec6e8c1d668ba67f2ff960e
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.
19 * 2000-03-20 Daniela Squassoni Disabling/enabling of facilities
20 * negotiation.
21 * 2000-11-10 Henner Eisen Check and reset for out-of-sequence
22 * i-frames.
25 #include <linux/config.h>
26 #if defined(CONFIG_X25) || defined(CONFIG_X25_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 <linux/inet.h>
38 #include <linux/netdevice.h>
39 #include <linux/skbuff.h>
40 #include <net/sock.h>
41 #include <net/ip.h> /* For ip_rcv */
42 #include <asm/segment.h>
43 #include <asm/system.h>
44 #include <linux/fcntl.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <net/x25.h>
49 static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
51 struct sk_buff *skbo, *skbn = skb;
53 if (more) {
54 sk->protinfo.x25->fraglen += skb->len;
55 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
56 skb_set_owner_r(skb, sk);
57 return 0;
60 if (!more && sk->protinfo.x25->fraglen > 0) { /* End of fragment */
61 int len = sk->protinfo.x25->fraglen + skb->len;
63 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){
64 kfree_skb(skb);
65 return 1;
68 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
70 skbn->h.raw = skbn->data;
72 skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue);
73 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
74 kfree_skb(skbo);
76 while ((skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue)) != NULL) {
77 skb_pull(skbo, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
78 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
79 kfree_skb(skbo);
82 sk->protinfo.x25->fraglen = 0;
85 skb_set_owner_r(skbn, sk);
86 skb_queue_tail(&sk->receive_queue, skbn);
87 if (!sk->dead)
88 sk->data_ready(sk,skbn->len);
90 return 0;
94 * State machine for state 1, Awaiting Call Accepted State.
95 * The handling of the timer(s) is in file x25_timer.c.
96 * Handling of state 0 and connection release is in af_x25.c.
98 static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
100 x25_address source_addr, dest_addr;
102 switch (frametype) {
104 case X25_CALL_ACCEPTED:
105 x25_stop_timer(sk);
106 sk->protinfo.x25->condition = 0x00;
107 sk->protinfo.x25->vs = 0;
108 sk->protinfo.x25->va = 0;
109 sk->protinfo.x25->vr = 0;
110 sk->protinfo.x25->vl = 0;
111 sk->protinfo.x25->state = X25_STATE_3;
112 sk->state = TCP_ESTABLISHED;
114 * Parse the data in the frame.
116 skb_pull(skb, X25_STD_MIN_LEN);
117 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
118 skb_pull(skb, x25_parse_facilities(skb, &sk->protinfo.x25->facilities, &sk->protinfo.x25->vc_facil_mask));
120 * Copy any Call User Data.
122 if (skb->len >= 0) {
123 memcpy(sk->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
124 sk->protinfo.x25->calluserdata.cudlength = skb->len;
126 if (!sk->dead)
127 sk->state_change(sk);
128 break;
130 case X25_CLEAR_REQUEST:
131 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
132 x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
133 break;
135 default:
136 break;
139 return 0;
143 * State machine for state 2, Awaiting Clear Confirmation State.
144 * The handling of the timer(s) is in file x25_timer.c
145 * Handling of state 0 and connection release is in af_x25.c.
147 static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
149 switch (frametype) {
151 case X25_CLEAR_REQUEST:
152 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
153 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
154 break;
156 case X25_CLEAR_CONFIRMATION:
157 x25_disconnect(sk, 0, 0, 0);
158 break;
160 default:
161 break;
164 return 0;
168 * State machine for state 3, Connected State.
169 * The handling of the timer(s) is in file x25_timer.c
170 * Handling of state 0 and connection release is in af_x25.c.
172 static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
174 int queued = 0;
175 int modulus;
177 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
179 switch (frametype) {
181 case X25_RESET_REQUEST:
182 x25_write_internal(sk, X25_RESET_CONFIRMATION);
183 x25_stop_timer(sk);
184 sk->protinfo.x25->condition = 0x00;
185 sk->protinfo.x25->vs = 0;
186 sk->protinfo.x25->vr = 0;
187 sk->protinfo.x25->va = 0;
188 sk->protinfo.x25->vl = 0;
189 x25_requeue_frames(sk);
190 break;
192 case X25_CLEAR_REQUEST:
193 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
194 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
195 break;
197 case X25_RR:
198 case X25_RNR:
199 if (!x25_validate_nr(sk, nr)) {
200 x25_clear_queues(sk);
201 x25_write_internal(sk, X25_RESET_REQUEST);
202 x25_start_t22timer(sk);
203 sk->protinfo.x25->condition = 0x00;
204 sk->protinfo.x25->vs = 0;
205 sk->protinfo.x25->vr = 0;
206 sk->protinfo.x25->va = 0;
207 sk->protinfo.x25->vl = 0;
208 sk->protinfo.x25->state = X25_STATE_4;
209 } else {
210 x25_frames_acked(sk, nr);
211 if (frametype == X25_RNR) {
212 sk->protinfo.x25->condition |= X25_COND_PEER_RX_BUSY;
213 } else {
214 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
217 break;
219 case X25_DATA: /* XXX */
220 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
221 if ((ns!=sk->protinfo.x25->vr) ||
222 !x25_validate_nr(sk, nr)) {
223 x25_clear_queues(sk);
224 x25_write_internal(sk, X25_RESET_REQUEST);
225 x25_start_t22timer(sk);
226 sk->protinfo.x25->condition = 0x00;
227 sk->protinfo.x25->vs = 0;
228 sk->protinfo.x25->vr = 0;
229 sk->protinfo.x25->va = 0;
230 sk->protinfo.x25->vl = 0;
231 sk->protinfo.x25->state = X25_STATE_4;
232 break;
234 x25_frames_acked(sk, nr);
235 if (ns == sk->protinfo.x25->vr) {
236 if (x25_queue_rx_frame(sk, skb, m) == 0) {
237 sk->protinfo.x25->vr = (sk->protinfo.x25->vr + 1) % modulus;
238 queued = 1;
239 } else {
240 /* Should never happen */
241 x25_clear_queues(sk);
242 x25_write_internal(sk, X25_RESET_REQUEST);
243 x25_start_t22timer(sk);
244 sk->protinfo.x25->condition = 0x00;
245 sk->protinfo.x25->vs = 0;
246 sk->protinfo.x25->vr = 0;
247 sk->protinfo.x25->va = 0;
248 sk->protinfo.x25->vl = 0;
249 sk->protinfo.x25->state = X25_STATE_4;
250 break;
252 if (atomic_read(&sk->rmem_alloc) > (sk->rcvbuf / 2))
253 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 x25_stop_timer(sk);
262 x25_enquiry_response(sk);
263 } else {
264 sk->protinfo.x25->condition |= X25_COND_ACK_PENDING;
265 x25_start_t2timer(sk);
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);
286 sock_wake_async(sk->socket, 3, POLL_PRI);
288 x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
289 break;
291 default:
292 printk(KERN_WARNING "x25: unknown %02X in state 3\n", frametype);
293 break;
296 return queued;
300 * State machine for state 4, Awaiting Reset Confirmation State.
301 * The handling of the timer(s) is in file x25_timer.c
302 * Handling of state 0 and connection release is in af_x25.c.
304 static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
306 switch (frametype) {
308 case X25_RESET_REQUEST:
309 x25_write_internal(sk, X25_RESET_CONFIRMATION);
310 case X25_RESET_CONFIRMATION:
311 x25_stop_timer(sk);
312 sk->protinfo.x25->condition = 0x00;
313 sk->protinfo.x25->va = 0;
314 sk->protinfo.x25->vr = 0;
315 sk->protinfo.x25->vs = 0;
316 sk->protinfo.x25->vl = 0;
317 sk->protinfo.x25->state = X25_STATE_3;
318 x25_requeue_frames(sk);
319 break;
321 case X25_CLEAR_REQUEST:
322 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
323 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
324 break;
326 default:
327 break;
330 return 0;
333 /* Higher level upcall for a LAPB frame */
334 int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
336 int queued = 0, frametype, ns, nr, q, d, m;
338 if (sk->protinfo.x25->state == X25_STATE_0)
339 return 0;
341 frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
343 switch (sk->protinfo.x25->state) {
344 case X25_STATE_1:
345 queued = x25_state1_machine(sk, skb, frametype);
346 break;
347 case X25_STATE_2:
348 queued = x25_state2_machine(sk, skb, frametype);
349 break;
350 case X25_STATE_3:
351 queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
352 break;
353 case X25_STATE_4:
354 queued = x25_state4_machine(sk, skb, frametype);
355 break;
358 x25_kick(sk);
360 return queued;
363 int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
365 int queued;
367 queued = x25_process_rx_frame(sk,skb);
368 if(!queued) kfree_skb(skb);
370 return 0;
373 #endif