2 * X.25 Packet Layer release 002
4 * This is ALPHA test software. This code may break your machine,
5 * randomly fail to work with new releases, misbehave and/or generally
6 * screw up. It might even work.
8 * This code REQUIRES 2.1.15 or higher
11 * This module is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * X.25 001 Jonathan Naylor Started coding.
18 * X.25 002 Jonathan Naylor Centralised disconnection processing.
19 * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
21 * jun/24/01 Arnaldo C. Melo use skb_queue_purge, cleanups
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/skbuff.h>
32 * This routine purges all of the queues of frames.
34 void x25_clear_queues(struct sock
*sk
)
36 struct x25_sock
*x25
= x25_sk(sk
);
38 skb_queue_purge(&sk
->sk_write_queue
);
39 skb_queue_purge(&x25
->ack_queue
);
40 skb_queue_purge(&x25
->interrupt_in_queue
);
41 skb_queue_purge(&x25
->interrupt_out_queue
);
42 skb_queue_purge(&x25
->fragment_queue
);
47 * This routine purges the input queue of those frames that have been
48 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
51 void x25_frames_acked(struct sock
*sk
, unsigned short nr
)
54 struct x25_sock
*x25
= x25_sk(sk
);
55 int modulus
= x25
->neighbour
->extended
? X25_EMODULUS
: X25_SMODULUS
;
58 * Remove all the ack-ed frames from the ack queue.
61 while (skb_peek(&x25
->ack_queue
) && x25
->va
!= nr
) {
62 skb
= skb_dequeue(&x25
->ack_queue
);
64 x25
->va
= (x25
->va
+ 1) % modulus
;
68 void x25_requeue_frames(struct sock
*sk
)
70 struct sk_buff
*skb
, *skb_prev
= NULL
;
73 * Requeue all the un-ack-ed frames on the output queue to be picked
74 * up by x25_kick. This arrangement handles the possibility of an empty
77 while ((skb
= skb_dequeue(&x25_sk(sk
)->ack_queue
)) != NULL
) {
79 skb_queue_head(&sk
->sk_write_queue
, skb
);
81 skb_append(skb_prev
, skb
);
87 * Validate that the value of nr is between va and vs. Return true or
90 int x25_validate_nr(struct sock
*sk
, unsigned short nr
)
92 struct x25_sock
*x25
= x25_sk(sk
);
93 unsigned short vc
= x25
->va
;
94 int modulus
= x25
->neighbour
->extended
? X25_EMODULUS
: X25_SMODULUS
;
96 while (vc
!= x25
->vs
) {
99 vc
= (vc
+ 1) % modulus
;
102 return nr
== x25
->vs
? 1 : 0;
106 * This routine is called when the packet layer internally generates a
109 void x25_write_internal(struct sock
*sk
, int frametype
)
111 struct x25_sock
*x25
= x25_sk(sk
);
114 unsigned char facilities
[X25_MAX_FAC_LEN
];
115 unsigned char addresses
[1 + X25_ADDR_LEN
];
116 unsigned char lci1
, lci2
;
118 * Default safe frame size.
120 int len
= X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
126 case X25_CALL_REQUEST
:
127 len
+= 1 + X25_ADDR_LEN
+ X25_MAX_FAC_LEN
+
130 case X25_CALL_ACCEPTED
:
131 len
+= 1 + X25_MAX_FAC_LEN
+ X25_MAX_CUD_LEN
;
133 case X25_CLEAR_REQUEST
:
134 case X25_RESET_REQUEST
:
140 case X25_CLEAR_CONFIRMATION
:
141 case X25_INTERRUPT_CONFIRMATION
:
142 case X25_RESET_CONFIRMATION
:
145 printk(KERN_ERR
"X.25: invalid frame type %02X\n",
150 if ((skb
= alloc_skb(len
, GFP_ATOMIC
)) == NULL
)
154 * Space for Ethernet and 802.2 LLC headers.
156 skb_reserve(skb
, X25_MAX_L2_LEN
);
159 * Make space for the GFI and LCI, and fill them in.
161 dptr
= skb_put(skb
, 2);
163 lci1
= (x25
->lci
>> 8) & 0x0F;
164 lci2
= (x25
->lci
>> 0) & 0xFF;
166 if (x25
->neighbour
->extended
) {
167 *dptr
++ = lci1
| X25_GFI_EXTSEQ
;
170 *dptr
++ = lci1
| X25_GFI_STDSEQ
;
175 * Now fill in the frame type specific information.
179 case X25_CALL_REQUEST
:
180 dptr
= skb_put(skb
, 1);
181 *dptr
++ = X25_CALL_REQUEST
;
182 len
= x25_addr_aton(addresses
, &x25
->dest_addr
,
184 dptr
= skb_put(skb
, len
);
185 memcpy(dptr
, addresses
, len
);
186 len
= x25_create_facilities(facilities
,
188 x25
->neighbour
->global_facil_mask
);
189 dptr
= skb_put(skb
, len
);
190 memcpy(dptr
, facilities
, len
);
191 dptr
= skb_put(skb
, x25
->calluserdata
.cudlength
);
192 memcpy(dptr
, x25
->calluserdata
.cuddata
,
193 x25
->calluserdata
.cudlength
);
194 x25
->calluserdata
.cudlength
= 0;
197 case X25_CALL_ACCEPTED
:
198 dptr
= skb_put(skb
, 2);
199 *dptr
++ = X25_CALL_ACCEPTED
;
200 *dptr
++ = 0x00; /* Address lengths */
201 len
= x25_create_facilities(facilities
,
204 dptr
= skb_put(skb
, len
);
205 memcpy(dptr
, facilities
, len
);
206 dptr
= skb_put(skb
, x25
->calluserdata
.cudlength
);
207 memcpy(dptr
, x25
->calluserdata
.cuddata
,
208 x25
->calluserdata
.cudlength
);
209 x25
->calluserdata
.cudlength
= 0;
212 case X25_CLEAR_REQUEST
:
213 case X25_RESET_REQUEST
:
214 dptr
= skb_put(skb
, 3);
216 *dptr
++ = 0x00; /* XXX */
217 *dptr
++ = 0x00; /* XXX */
223 if (x25
->neighbour
->extended
) {
224 dptr
= skb_put(skb
, 2);
226 *dptr
++ = (x25
->vr
<< 1) & 0xFE;
228 dptr
= skb_put(skb
, 1);
230 *dptr
++ |= (x25
->vr
<< 5) & 0xE0;
234 case X25_CLEAR_CONFIRMATION
:
235 case X25_INTERRUPT_CONFIRMATION
:
236 case X25_RESET_CONFIRMATION
:
237 dptr
= skb_put(skb
, 1);
242 x25_transmit_link(skb
, x25
->neighbour
);
246 * Unpick the contents of the passed X.25 Packet Layer frame.
248 int x25_decode(struct sock
*sk
, struct sk_buff
*skb
, int *ns
, int *nr
, int *q
,
251 struct x25_sock
*x25
= x25_sk(sk
);
252 unsigned char *frame
= skb
->data
;
254 *ns
= *nr
= *q
= *d
= *m
= 0;
257 case X25_CALL_REQUEST
:
258 case X25_CALL_ACCEPTED
:
259 case X25_CLEAR_REQUEST
:
260 case X25_CLEAR_CONFIRMATION
:
262 case X25_INTERRUPT_CONFIRMATION
:
263 case X25_RESET_REQUEST
:
264 case X25_RESET_CONFIRMATION
:
265 case X25_RESTART_REQUEST
:
266 case X25_RESTART_CONFIRMATION
:
267 case X25_REGISTRATION_REQUEST
:
268 case X25_REGISTRATION_CONFIRMATION
:
273 if (x25
->neighbour
->extended
) {
274 if (frame
[2] == X25_RR
||
275 frame
[2] == X25_RNR
||
276 frame
[2] == X25_REJ
) {
277 *nr
= (frame
[3] >> 1) & 0x7F;
281 if ((frame
[2] & 0x1F) == X25_RR
||
282 (frame
[2] & 0x1F) == X25_RNR
||
283 (frame
[2] & 0x1F) == X25_REJ
) {
284 *nr
= (frame
[2] >> 5) & 0x07;
285 return frame
[2] & 0x1F;
289 if (x25
->neighbour
->extended
) {
290 if ((frame
[2] & 0x01) == X25_DATA
) {
291 *q
= (frame
[0] & X25_Q_BIT
) == X25_Q_BIT
;
292 *d
= (frame
[0] & X25_D_BIT
) == X25_D_BIT
;
293 *m
= (frame
[3] & X25_EXT_M_BIT
) == X25_EXT_M_BIT
;
294 *nr
= (frame
[3] >> 1) & 0x7F;
295 *ns
= (frame
[2] >> 1) & 0x7F;
299 if ((frame
[2] & 0x01) == X25_DATA
) {
300 *q
= (frame
[0] & X25_Q_BIT
) == X25_Q_BIT
;
301 *d
= (frame
[0] & X25_D_BIT
) == X25_D_BIT
;
302 *m
= (frame
[2] & X25_STD_M_BIT
) == X25_STD_M_BIT
;
303 *nr
= (frame
[2] >> 5) & 0x07;
304 *ns
= (frame
[2] >> 1) & 0x07;
309 printk(KERN_DEBUG
"X.25: invalid PLP frame %02X %02X %02X\n",
310 frame
[0], frame
[1], frame
[2]);
315 void x25_disconnect(struct sock
*sk
, int reason
, unsigned char cause
,
316 unsigned char diagnostic
)
318 struct x25_sock
*x25
= x25_sk(sk
);
320 x25_clear_queues(sk
);
324 x25
->state
= X25_STATE_0
;
326 x25
->causediag
.cause
= cause
;
327 x25
->causediag
.diagnostic
= diagnostic
;
329 sk
->sk_state
= TCP_CLOSE
;
331 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
333 if (!sock_flag(sk
, SOCK_DEAD
)) {
334 sk
->sk_state_change(sk
);
335 sock_set_flag(sk
, SOCK_DEAD
);
340 * Clear an own-rx-busy condition and tell the peer about this, provided
341 * that there is a significant amount of free receive buffer space available.
343 void x25_check_rbuf(struct sock
*sk
)
345 struct x25_sock
*x25
= x25_sk(sk
);
347 if (atomic_read(&sk
->sk_rmem_alloc
) < (sk
->sk_rcvbuf
/ 2) &&
348 (x25
->condition
& X25_COND_OWN_RX_BUSY
)) {
349 x25
->condition
&= ~X25_COND_OWN_RX_BUSY
;
350 x25
->condition
&= ~X25_COND_ACK_PENDING
;
352 x25_write_internal(sk
, X25_RR
);
358 * Compare 2 calluserdata structures, used to find correct listening sockets
359 * when call user data is used.
361 int x25_check_calluserdata(struct x25_calluserdata
*ours
, struct x25_calluserdata
*theirs
)
364 if (ours
->cudlength
!= theirs
->cudlength
)
367 for (i
=0;i
<ours
->cudlength
;i
++) {
368 if (ours
->cuddata
[i
] != theirs
->cuddata
[i
]) {