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/errno.h>
26 #include <linux/types.h>
27 #include <linux/socket.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/string.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
37 #include <linux/skbuff.h>
39 #include <net/ip.h> /* For ip_rcv */
40 #include <asm/segment.h>
41 #include <asm/system.h>
42 #include <linux/fcntl.h>
44 #include <linux/interrupt.h>
47 static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
49 struct sk_buff *skbo, *skbn = skb;
52 sk->protinfo.x25->fraglen += skb->len;
53 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
54 skb_set_owner_r(skb, sk);
58 if (!more && sk->protinfo.x25->fraglen > 0) { /* End of fragment */
59 int len = sk->protinfo.x25->fraglen + skb->len;
61 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){
66 skb_queue_tail(&sk->protinfo.x25->fragment_queue, skb);
68 skbn->h.raw = skbn->data;
70 skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue);
71 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
74 while ((skbo = skb_dequeue(&sk->protinfo.x25->fragment_queue)) != NULL) {
75 skb_pull(skbo, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
76 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
80 sk->protinfo.x25->fraglen = 0;
83 skb_set_owner_r(skbn, sk);
84 skb_queue_tail(&sk->receive_queue, skbn);
86 sk->data_ready(sk,skbn->len);
92 * State machine for state 1, Awaiting Call Accepted State.
93 * The handling of the timer(s) is in file x25_timer.c.
94 * Handling of state 0 and connection release is in af_x25.c.
96 static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
98 x25_address source_addr, dest_addr;
102 case X25_CALL_ACCEPTED:
104 sk->protinfo.x25->condition = 0x00;
105 sk->protinfo.x25->vs = 0;
106 sk->protinfo.x25->va = 0;
107 sk->protinfo.x25->vr = 0;
108 sk->protinfo.x25->vl = 0;
109 sk->protinfo.x25->state = X25_STATE_3;
110 sk->state = TCP_ESTABLISHED;
112 * Parse the data in the frame.
114 skb_pull(skb, X25_STD_MIN_LEN);
115 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
116 skb_pull(skb, x25_parse_facilities(skb, &sk->protinfo.x25->facilities, &sk->protinfo.x25->vc_facil_mask));
118 * Copy any Call User Data.
121 memcpy(sk->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
122 sk->protinfo.x25->calluserdata.cudlength = skb->len;
125 sk->state_change(sk);
128 case X25_CLEAR_REQUEST:
129 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
130 x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
141 * State machine for state 2, Awaiting Clear Confirmation State.
142 * The handling of the timer(s) is in file x25_timer.c
143 * Handling of state 0 and connection release is in af_x25.c.
145 static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
149 case X25_CLEAR_REQUEST:
150 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
151 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
154 case X25_CLEAR_CONFIRMATION:
155 x25_disconnect(sk, 0, 0, 0);
166 * State machine for state 3, Connected State.
167 * The handling of the timer(s) is in file x25_timer.c
168 * Handling of state 0 and connection release is in af_x25.c.
170 static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
175 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
179 case X25_RESET_REQUEST:
180 x25_write_internal(sk, X25_RESET_CONFIRMATION);
182 sk->protinfo.x25->condition = 0x00;
183 sk->protinfo.x25->vs = 0;
184 sk->protinfo.x25->vr = 0;
185 sk->protinfo.x25->va = 0;
186 sk->protinfo.x25->vl = 0;
187 x25_requeue_frames(sk);
190 case X25_CLEAR_REQUEST:
191 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
192 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
197 if (!x25_validate_nr(sk, nr)) {
198 x25_clear_queues(sk);
199 x25_write_internal(sk, X25_RESET_REQUEST);
200 x25_start_t22timer(sk);
201 sk->protinfo.x25->condition = 0x00;
202 sk->protinfo.x25->vs = 0;
203 sk->protinfo.x25->vr = 0;
204 sk->protinfo.x25->va = 0;
205 sk->protinfo.x25->vl = 0;
206 sk->protinfo.x25->state = X25_STATE_4;
208 x25_frames_acked(sk, nr);
209 if (frametype == X25_RNR) {
210 sk->protinfo.x25->condition |= X25_COND_PEER_RX_BUSY;
212 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
217 case X25_DATA: /* XXX */
218 sk->protinfo.x25->condition &= ~X25_COND_PEER_RX_BUSY;
219 if ((ns!=sk->protinfo.x25->vr) ||
220 !x25_validate_nr(sk, nr)) {
221 x25_clear_queues(sk);
222 x25_write_internal(sk, X25_RESET_REQUEST);
223 x25_start_t22timer(sk);
224 sk->protinfo.x25->condition = 0x00;
225 sk->protinfo.x25->vs = 0;
226 sk->protinfo.x25->vr = 0;
227 sk->protinfo.x25->va = 0;
228 sk->protinfo.x25->vl = 0;
229 sk->protinfo.x25->state = X25_STATE_4;
232 x25_frames_acked(sk, nr);
233 if (ns == sk->protinfo.x25->vr) {
234 if (x25_queue_rx_frame(sk, skb, m) == 0) {
235 sk->protinfo.x25->vr = (sk->protinfo.x25->vr + 1) % modulus;
238 /* Should never happen */
239 x25_clear_queues(sk);
240 x25_write_internal(sk, X25_RESET_REQUEST);
241 x25_start_t22timer(sk);
242 sk->protinfo.x25->condition = 0x00;
243 sk->protinfo.x25->vs = 0;
244 sk->protinfo.x25->vr = 0;
245 sk->protinfo.x25->va = 0;
246 sk->protinfo.x25->vl = 0;
247 sk->protinfo.x25->state = X25_STATE_4;
250 if (atomic_read(&sk->rmem_alloc) > (sk->rcvbuf / 2))
251 sk->protinfo.x25->condition |= X25_COND_OWN_RX_BUSY;
254 * If the window is full Ack it immediately, else
255 * start the holdback timer.
257 if (((sk->protinfo.x25->vl + sk->protinfo.x25->facilities.winsize_in) % modulus) == sk->protinfo.x25->vr) {
258 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
260 x25_enquiry_response(sk);
262 sk->protinfo.x25->condition |= X25_COND_ACK_PENDING;
263 x25_start_t2timer(sk);
267 case X25_INTERRUPT_CONFIRMATION:
268 sk->protinfo.x25->intflag = 0;
273 queued = (sock_queue_rcv_skb(sk, skb) == 0);
275 skb_set_owner_r(skb, sk);
276 skb_queue_tail(&sk->protinfo.x25->interrupt_in_queue, skb);
281 kill_proc(sk->proc, SIGURG, 1);
283 kill_pg(-sk->proc, SIGURG, 1);
284 sock_wake_async(sk->socket, 3, POLL_PRI);
286 x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
290 printk(KERN_WARNING "x25: unknown %02X in state 3\n", frametype);
298 * State machine for state 4, Awaiting Reset Confirmation State.
299 * The handling of the timer(s) is in file x25_timer.c
300 * Handling of state 0 and connection release is in af_x25.c.
302 static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
306 case X25_RESET_REQUEST:
307 x25_write_internal(sk, X25_RESET_CONFIRMATION);
308 case X25_RESET_CONFIRMATION:
310 sk->protinfo.x25->condition = 0x00;
311 sk->protinfo.x25->va = 0;
312 sk->protinfo.x25->vr = 0;
313 sk->protinfo.x25->vs = 0;
314 sk->protinfo.x25->vl = 0;
315 sk->protinfo.x25->state = X25_STATE_3;
316 x25_requeue_frames(sk);
319 case X25_CLEAR_REQUEST:
320 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
321 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
331 /* Higher level upcall for a LAPB frame */
332 int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
334 int queued = 0, frametype, ns, nr, q, d, m;
336 if (sk->protinfo.x25->state == X25_STATE_0)
339 frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
341 switch (sk->protinfo.x25->state) {
343 queued = x25_state1_machine(sk, skb, frametype);
346 queued = x25_state2_machine(sk, skb, frametype);
349 queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
352 queued = x25_state4_machine(sk, skb, frametype);
361 int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
365 queued = x25_process_rx_frame(sk,skb);
366 if(!queued) kfree_skb(skb);