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
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.
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>
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>
35 #include <net/ip.h> /* For ip_rcv */
36 #include <asm/segment.h>
37 #include <asm/system.h>
38 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
43 static int x25_queue_rx_frame(struct sock
*sk
, struct sk_buff
*skb
, int more
)
45 struct sk_buff
*skbo
, *skbn
= skb
;
48 sk
->protinfo
.x25
->fraglen
+= skb
->len
;
49 skb_queue_tail(&sk
->protinfo
.x25
->fragment_queue
, skb
);
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
)
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
;
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.
109 memcpy(sk
->protinfo
.x25
->calluserdata
.cuddata
, skb
->data
, skb
->len
);
110 sk
->protinfo
.x25
->calluserdata
.cudlength
= skb
->len
;
113 sk
->state_change(sk
);
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
;
124 sk
->state_change(sk
);
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
)
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
;
151 sk
->shutdown
|= SEND_SHUTDOWN
;
153 sk
->state_change(sk
);
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
)
174 modulus
= (sk
->protinfo
.x25
->neighbour
->extended
) ? X25_EMODULUS
: X25_SMODULUS
;
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;
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
;
194 sk
->shutdown
|= SEND_SHUTDOWN
;
196 sk
->state_change(sk
);
202 if (frametype
== X25_RNR
) {
203 sk
->protinfo
.x25
->condition
|= X25_COND_PEER_RX_BUSY
;
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
;
218 if (sk
->protinfo
.x25
->condition
& X25_COND_PEER_RX_BUSY
) {
219 sk
->protinfo
.x25
->va
= nr
;
221 x25_check_iframes_acked(sk
, nr
);
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
;
240 if (sk
->protinfo
.x25
->condition
& X25_COND_PEER_RX_BUSY
) {
241 sk
->protinfo
.x25
->va
= nr
;
243 x25_check_iframes_acked(sk
, nr
);
245 if (sk
->protinfo
.x25
->condition
& X25_COND_OWN_RX_BUSY
)
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
;
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
);
264 sk
->protinfo
.x25
->condition
|= X25_COND_ACK_PENDING
;
265 sk
->protinfo
.x25
->timer
= sk
->protinfo
.x25
->t2
;
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);
287 x25_write_internal(sk
, X25_INTERRUPT_CONFIRMATION
);
291 printk(KERN_WARNING
"x25: unknown %02X in state 3\n", frametype
);
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
)
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
;
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
;
326 sk
->shutdown
|= SEND_SHUTDOWN
;
328 sk
->state_change(sk
);
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
)
347 del_timer(&sk
->timer
);
349 frametype
= x25_decode(sk
, skb
, &ns
, &nr
, &q
, &d
, &m
);
351 switch (sk
->protinfo
.x25
->state
) {
353 queued
= x25_state1_machine(sk
, skb
, frametype
);
356 queued
= x25_state2_machine(sk
, skb
, frametype
);
359 queued
= x25_state3_machine(sk
, skb
, frametype
, ns
, nr
, q
, d
, m
);
362 queued
= x25_state4_machine(sk
, skb
, frametype
);