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
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.
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
21 * 2000-11-10 Henner Eisen Check and reset for out-of-sequence
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>
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>
41 #include <net/ip.h> /* For ip_rcv */
42 #include <asm/segment.h>
43 #include <asm/system.h>
44 #include <linux/fcntl.h>
46 #include <linux/interrupt.h>
49 static int x25_queue_rx_frame(struct sock
*sk
, struct sk_buff
*skb
, int more
)
51 struct sk_buff
*skbo
, *skbn
= skb
;
54 sk
->protinfo
.x25
->fraglen
+= skb
->len
;
55 skb_queue_tail(&sk
->protinfo
.x25
->fragment_queue
, skb
);
56 skb_set_owner_r(skb
, sk
);
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
){
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
);
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
);
82 sk
->protinfo
.x25
->fraglen
= 0;
85 skb_set_owner_r(skbn
, sk
);
86 skb_queue_tail(&sk
->receive_queue
, skbn
);
88 sk
->data_ready(sk
,skbn
->len
);
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
;
104 case X25_CALL_ACCEPTED
:
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.
123 memcpy(sk
->protinfo
.x25
->calluserdata
.cuddata
, skb
->data
, skb
->len
);
124 sk
->protinfo
.x25
->calluserdata
.cudlength
= skb
->len
;
127 sk
->state_change(sk
);
130 case X25_CLEAR_REQUEST
:
131 x25_write_internal(sk
, X25_CLEAR_CONFIRMATION
);
132 x25_disconnect(sk
, ECONNREFUSED
, skb
->data
[3], skb
->data
[4]);
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
)
151 case X25_CLEAR_REQUEST
:
152 x25_write_internal(sk
, X25_CLEAR_CONFIRMATION
);
153 x25_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
156 case X25_CLEAR_CONFIRMATION
:
157 x25_disconnect(sk
, 0, 0, 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
)
177 modulus
= (sk
->protinfo
.x25
->neighbour
->extended
) ? X25_EMODULUS
: X25_SMODULUS
;
181 case X25_RESET_REQUEST
:
182 x25_write_internal(sk
, X25_RESET_CONFIRMATION
);
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
);
192 case X25_CLEAR_REQUEST
:
193 x25_write_internal(sk
, X25_CLEAR_CONFIRMATION
);
194 x25_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
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
;
210 x25_frames_acked(sk
, nr
);
211 if (frametype
== X25_RNR
) {
212 sk
->protinfo
.x25
->condition
|= X25_COND_PEER_RX_BUSY
;
214 sk
->protinfo
.x25
->condition
&= ~X25_COND_PEER_RX_BUSY
;
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
;
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
;
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
;
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
;
262 x25_enquiry_response(sk
);
264 sk
->protinfo
.x25
->condition
|= X25_COND_ACK_PENDING
;
265 x25_start_t2timer(sk
);
269 case X25_INTERRUPT_CONFIRMATION
:
270 sk
->protinfo
.x25
->intflag
= 0;
275 queued
= (sock_queue_rcv_skb(sk
, skb
) == 0);
277 skb_set_owner_r(skb
, sk
);
278 skb_queue_tail(&sk
->protinfo
.x25
->interrupt_in_queue
, skb
);
283 kill_proc(sk
->proc
, SIGURG
, 1);
285 kill_pg(-sk
->proc
, SIGURG
, 1);
286 sock_wake_async(sk
->socket
, 3, POLL_PRI
);
288 x25_write_internal(sk
, X25_INTERRUPT_CONFIRMATION
);
292 printk(KERN_WARNING
"x25: unknown %02X in state 3\n", frametype
);
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
)
308 case X25_RESET_REQUEST
:
309 x25_write_internal(sk
, X25_RESET_CONFIRMATION
);
310 case X25_RESET_CONFIRMATION
:
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
);
321 case X25_CLEAR_REQUEST
:
322 x25_write_internal(sk
, X25_CLEAR_CONFIRMATION
);
323 x25_disconnect(sk
, 0, skb
->data
[3], skb
->data
[4]);
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
)
341 frametype
= x25_decode(sk
, skb
, &ns
, &nr
, &q
, &d
, &m
);
343 switch (sk
->protinfo
.x25
->state
) {
345 queued
= x25_state1_machine(sk
, skb
, frametype
);
348 queued
= x25_state2_machine(sk
, skb
, frametype
);
351 queued
= x25_state3_machine(sk
, skb
, frametype
, ns
, nr
, q
, d
, m
);
354 queued
= x25_state4_machine(sk
, skb
, frametype
);
363 int x25_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
367 queued
= x25_process_rx_frame(sk
,skb
);
368 if(!queued
) kfree_skb(skb
);