* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / x25 / x25_subr.c
blob8b055e40e0759452b8e964d37511de87cc4f9f48
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 processing.
20 #include <linux/config.h>
21 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
25 #include <linux/in.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/string.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <net/sock.h>
36 #include <asm/segment.h>
37 #include <asm/system.h>
38 #include <linux/fcntl.h>
39 #include <linux/mm.h>
40 #include <linux/interrupt.h>
41 #include <net/x25.h>
44 * This routine purges all of the queues of frames.
46 void x25_clear_queues(struct sock *sk)
48 struct sk_buff *skb;
50 while ((skb = skb_dequeue(&sk->write_queue)) != NULL)
51 kfree_skb(skb);
53 while ((skb = skb_dequeue(&sk->protinfo.x25->ack_queue)) != NULL)
54 kfree_skb(skb);
56 while ((skb = skb_dequeue(&sk->protinfo.x25->interrupt_in_queue)) != NULL)
57 kfree_skb(skb);
59 while ((skb = skb_dequeue(&sk->protinfo.x25->interrupt_out_queue)) != NULL)
60 kfree_skb(skb);
62 while ((skb = skb_dequeue(&sk->protinfo.x25->fragment_queue)) != NULL)
63 kfree_skb(skb);
68 * This routine purges the input queue of those frames that have been
69 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
70 * SDL diagram.
72 void x25_frames_acked(struct sock *sk, unsigned short nr)
74 struct sk_buff *skb;
75 int modulus;
77 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
80 * Remove all the ack-ed frames from the ack queue.
82 if (sk->protinfo.x25->va != nr) {
83 while (skb_peek(&sk->protinfo.x25->ack_queue) != NULL && sk->protinfo.x25->va != nr) {
84 skb = skb_dequeue(&sk->protinfo.x25->ack_queue);
85 kfree_skb(skb);
86 sk->protinfo.x25->va = (sk->protinfo.x25->va + 1) % modulus;
91 void x25_requeue_frames(struct sock *sk)
93 struct sk_buff *skb, *skb_prev = NULL;
96 * Requeue all the un-ack-ed frames on the output queue to be picked
97 * up by x25_kick. This arrangement handles the possibility of an empty
98 * output queue.
100 while ((skb = skb_dequeue(&sk->protinfo.x25->ack_queue)) != NULL) {
101 if (skb_prev == NULL)
102 skb_queue_head(&sk->write_queue, skb);
103 else
104 skb_append(skb_prev, skb);
105 skb_prev = skb;
110 * Validate that the value of nr is between va and vs. Return true or
111 * false for testing.
113 int x25_validate_nr(struct sock *sk, unsigned short nr)
115 unsigned short vc = sk->protinfo.x25->va;
116 int modulus;
118 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
120 while (vc != sk->protinfo.x25->vs) {
121 if (nr == vc) return 1;
122 vc = (vc + 1) % modulus;
125 if (nr == sk->protinfo.x25->vs) return 1;
127 return 0;
131 * This routine is called when the packet layer internally generates a
132 * control frame.
134 void x25_write_internal(struct sock *sk, int frametype)
136 struct sk_buff *skb;
137 unsigned char *dptr;
138 unsigned char facilities[X25_MAX_FAC_LEN];
139 unsigned char addresses[1 + X25_ADDR_LEN];
140 unsigned char lci1, lci2;
141 int len;
144 * Default safe frame size.
146 len = X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
149 * Adjust frame size.
151 switch (frametype) {
152 case X25_CALL_REQUEST:
153 len += 1 + X25_ADDR_LEN + X25_MAX_FAC_LEN + X25_MAX_CUD_LEN;
154 break;
155 case X25_CALL_ACCEPTED:
156 len += 1 + X25_MAX_FAC_LEN + X25_MAX_CUD_LEN;
157 break;
158 case X25_CLEAR_REQUEST:
159 case X25_RESET_REQUEST:
160 len += 2;
161 break;
162 case X25_RR:
163 case X25_RNR:
164 case X25_REJ:
165 case X25_CLEAR_CONFIRMATION:
166 case X25_INTERRUPT_CONFIRMATION:
167 case X25_RESET_CONFIRMATION:
168 break;
169 default:
170 printk(KERN_ERR "X.25: invalid frame type %02X\n", frametype);
171 return;
174 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
175 return;
178 * Space for Ethernet and 802.2 LLC headers.
180 skb_reserve(skb, X25_MAX_L2_LEN);
183 * Make space for the GFI and LCI, and fill them in.
185 dptr = skb_put(skb, 2);
187 lci1 = (sk->protinfo.x25->lci >> 8) & 0x0F;
188 lci2 = (sk->protinfo.x25->lci >> 0) & 0xFF;
190 if (sk->protinfo.x25->neighbour->extended) {
191 *dptr++ = lci1 | X25_GFI_EXTSEQ;
192 *dptr++ = lci2;
193 } else {
194 *dptr++ = lci1 | X25_GFI_STDSEQ;
195 *dptr++ = lci2;
199 * Now fill in the frame type specific information.
201 switch (frametype) {
203 case X25_CALL_REQUEST:
204 dptr = skb_put(skb, 1);
205 *dptr++ = X25_CALL_REQUEST;
206 len = x25_addr_aton(addresses, &sk->protinfo.x25->dest_addr, &sk->protinfo.x25->source_addr);
207 dptr = skb_put(skb, len);
208 memcpy(dptr, addresses, len);
209 len = x25_create_facilities(facilities, &sk->protinfo.x25->facilities);
210 dptr = skb_put(skb, len);
211 memcpy(dptr, facilities, len);
212 dptr = skb_put(skb, sk->protinfo.x25->calluserdata.cudlength);
213 memcpy(dptr, sk->protinfo.x25->calluserdata.cuddata, sk->protinfo.x25->calluserdata.cudlength);
214 sk->protinfo.x25->calluserdata.cudlength = 0;
215 break;
217 case X25_CALL_ACCEPTED:
218 dptr = skb_put(skb, 2);
219 *dptr++ = X25_CALL_ACCEPTED;
220 *dptr++ = 0x00; /* Address lengths */
221 len = x25_create_facilities(facilities, &sk->protinfo.x25->facilities);
222 dptr = skb_put(skb, len);
223 memcpy(dptr, facilities, len);
224 dptr = skb_put(skb, sk->protinfo.x25->calluserdata.cudlength);
225 memcpy(dptr, sk->protinfo.x25->calluserdata.cuddata, sk->protinfo.x25->calluserdata.cudlength);
226 sk->protinfo.x25->calluserdata.cudlength = 0;
227 break;
229 case X25_CLEAR_REQUEST:
230 case X25_RESET_REQUEST:
231 dptr = skb_put(skb, 3);
232 *dptr++ = frametype;
233 *dptr++ = 0x00; /* XXX */
234 *dptr++ = 0x00; /* XXX */
235 break;
237 case X25_RR:
238 case X25_RNR:
239 case X25_REJ:
240 if (sk->protinfo.x25->neighbour->extended) {
241 dptr = skb_put(skb, 2);
242 *dptr++ = frametype;
243 *dptr++ = (sk->protinfo.x25->vr << 1) & 0xFE;
244 } else {
245 dptr = skb_put(skb, 1);
246 *dptr = frametype;
247 *dptr++ |= (sk->protinfo.x25->vr << 5) & 0xE0;
249 break;
251 case X25_CLEAR_CONFIRMATION:
252 case X25_INTERRUPT_CONFIRMATION:
253 case X25_RESET_CONFIRMATION:
254 dptr = skb_put(skb, 1);
255 *dptr = frametype;
256 break;
259 x25_transmit_link(skb, sk->protinfo.x25->neighbour);
263 * Unpick the contents of the passed X.25 Packet Layer frame.
265 int x25_decode(struct sock *sk, struct sk_buff *skb, int *ns, int *nr, int *q, int *d, int *m)
267 unsigned char *frame;
269 frame = skb->data;
271 *ns = *nr = *q = *d = *m = 0;
273 switch (frame[2]) {
274 case X25_CALL_REQUEST:
275 case X25_CALL_ACCEPTED:
276 case X25_CLEAR_REQUEST:
277 case X25_CLEAR_CONFIRMATION:
278 case X25_INTERRUPT:
279 case X25_INTERRUPT_CONFIRMATION:
280 case X25_RESET_REQUEST:
281 case X25_RESET_CONFIRMATION:
282 case X25_RESTART_REQUEST:
283 case X25_RESTART_CONFIRMATION:
284 case X25_REGISTRATION_REQUEST:
285 case X25_REGISTRATION_CONFIRMATION:
286 case X25_DIAGNOSTIC:
287 return frame[2];
290 if (sk->protinfo.x25->neighbour->extended) {
291 if (frame[2] == X25_RR ||
292 frame[2] == X25_RNR ||
293 frame[2] == X25_REJ) {
294 *nr = (frame[3] >> 1) & 0x7F;
295 return frame[2];
297 } else {
298 if ((frame[2] & 0x1F) == X25_RR ||
299 (frame[2] & 0x1F) == X25_RNR ||
300 (frame[2] & 0x1F) == X25_REJ) {
301 *nr = (frame[2] >> 5) & 0x07;
302 return frame[2] & 0x1F;
306 if (sk->protinfo.x25->neighbour->extended) {
307 if ((frame[2] & 0x01) == X25_DATA) {
308 *q = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
309 *d = (frame[0] & X25_D_BIT) == X25_D_BIT;
310 *m = (frame[3] & X25_EXT_M_BIT) == X25_EXT_M_BIT;
311 *nr = (frame[3] >> 1) & 0x7F;
312 *ns = (frame[2] >> 1) & 0x7F;
313 return X25_DATA;
315 } else {
316 if ((frame[2] & 0x01) == X25_DATA) {
317 *q = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
318 *d = (frame[0] & X25_D_BIT) == X25_D_BIT;
319 *m = (frame[2] & X25_STD_M_BIT) == X25_STD_M_BIT;
320 *nr = (frame[2] >> 5) & 0x07;
321 *ns = (frame[2] >> 1) & 0x07;
322 return X25_DATA;
326 printk(KERN_DEBUG "X.25: invalid PLP frame %02X %02X %02X\n", frame[0], frame[1], frame[2]);
328 return X25_ILLEGAL;
331 void x25_disconnect(struct sock *sk, int reason, unsigned char cause, unsigned char diagnostic)
333 x25_clear_queues(sk);
334 x25_stop_timer(sk);
336 sk->protinfo.x25->lci = 0;
337 sk->protinfo.x25->state = X25_STATE_0;
339 sk->protinfo.x25->causediag.cause = cause;
340 sk->protinfo.x25->causediag.diagnostic = diagnostic;
342 sk->state = TCP_CLOSE;
343 sk->err = reason;
344 sk->shutdown |= SEND_SHUTDOWN;
346 if (!sk->dead)
347 sk->state_change(sk);
349 sk->dead = 1;
352 #endif