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 New timer architecture.
19 * 2000-09-04 Henner Eisen Prevented x25_output() skb leakage.
20 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
21 * 2000-11-10 Henner Eisen x25_send_iframe(): re-queued frames
22 * needed cleaned seq-number fields.
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 <asm/system.h>
40 #include <linux/fcntl.h>
42 #include <linux/interrupt.h>
45 static int x25_pacsize_to_bytes(unsigned int pacsize
)
59 * This is where all X.25 information frames pass.
61 * Returns the amount of user data bytes sent on success
62 * or a negative error code on failure.
64 int x25_output(struct sock
*sk
, struct sk_buff
*skb
)
67 unsigned char header
[X25_EXT_MIN_LEN
];
68 int err
, frontlen
, len
;
69 int sent
=0, noblock
= X25_SKB_CB(skb
)->flags
& MSG_DONTWAIT
;
70 struct x25_opt
*x25
= x25_sk(sk
);
71 int header_len
= x25
->neighbour
->extended
? X25_EXT_MIN_LEN
:
73 int max_len
= x25_pacsize_to_bytes(x25
->facilities
.pacsize_out
);
75 if (skb
->len
- header_len
> max_len
) {
76 /* Save a copy of the Header */
77 memcpy(header
, skb
->data
, header_len
);
78 skb_pull(skb
, header_len
);
80 frontlen
= skb_headroom(skb
);
82 while (skb
->len
> 0) {
83 if ((skbn
= sock_alloc_send_skb(sk
, frontlen
+ max_len
,
84 noblock
, &err
)) == NULL
){
85 if (err
== -EWOULDBLOCK
&& noblock
){
89 SOCK_DEBUG(sk
, "x25_output: fragment alloc"
90 " failed, err=%d, %d bytes "
95 skb_reserve(skbn
, frontlen
);
97 len
= max_len
> skb
->len
? skb
->len
: max_len
;
99 /* Copy the user data */
100 memcpy(skb_put(skbn
, len
), skb
->data
, len
);
103 /* Duplicate the Header */
104 skb_push(skbn
, header_len
);
105 memcpy(skbn
->data
, header
, header_len
);
108 if (x25
->neighbour
->extended
)
109 skbn
->data
[3] |= X25_EXT_M_BIT
;
111 skbn
->data
[2] |= X25_STD_M_BIT
;
114 skb_queue_tail(&sk
->sk_write_queue
, skbn
);
120 skb_queue_tail(&sk
->sk_write_queue
, skb
);
121 sent
= skb
->len
- header_len
;
127 * This procedure is passed a buffer descriptor for an iframe. It builds
128 * the rest of the control part of the frame and then writes it out.
130 static void x25_send_iframe(struct sock
*sk
, struct sk_buff
*skb
)
132 struct x25_opt
*x25
= x25_sk(sk
);
137 if (x25
->neighbour
->extended
) {
138 skb
->data
[2] = (x25
->vs
<< 1) & 0xFE;
139 skb
->data
[3] &= X25_EXT_M_BIT
;
140 skb
->data
[3] |= (x25
->vr
<< 1) & 0xFE;
142 skb
->data
[2] &= X25_STD_M_BIT
;
143 skb
->data
[2] |= (x25
->vs
<< 1) & 0x0E;
144 skb
->data
[2] |= (x25
->vr
<< 5) & 0xE0;
147 x25_transmit_link(skb
, x25
->neighbour
);
150 void x25_kick(struct sock
*sk
)
152 struct sk_buff
*skb
, *skbn
;
153 unsigned short start
, end
;
155 struct x25_opt
*x25
= x25_sk(sk
);
157 if (x25
->state
!= X25_STATE_3
)
161 * Transmit interrupt data.
163 if (!x25
->intflag
&& skb_peek(&x25
->interrupt_out_queue
) != NULL
) {
165 skb
= skb_dequeue(&x25
->interrupt_out_queue
);
166 x25_transmit_link(skb
, x25
->neighbour
);
169 if (x25
->condition
& X25_COND_PEER_RX_BUSY
)
172 if (!skb_peek(&sk
->sk_write_queue
))
175 modulus
= x25
->neighbour
->extended
? X25_EMODULUS
: X25_SMODULUS
;
177 start
= skb_peek(&x25
->ack_queue
) ? x25
->vs
: x25
->va
;
178 end
= (x25
->va
+ x25
->facilities
.winsize_out
) % modulus
;
186 * Transmit data until either we're out of data to send or
187 * the window is full.
190 skb
= skb_dequeue(&sk
->sk_write_queue
);
193 if ((skbn
= skb_clone(skb
, GFP_ATOMIC
)) == NULL
) {
194 skb_queue_head(&sk
->sk_write_queue
, skb
);
198 skb_set_owner_w(skbn
, sk
);
201 * Transmit the frame copy.
203 x25_send_iframe(sk
, skbn
);
205 x25
->vs
= (x25
->vs
+ 1) % modulus
;
208 * Requeue the original data frame.
210 skb_queue_tail(&x25
->ack_queue
, skb
);
212 } while (x25
->vs
!= end
&&
213 (skb
= skb_dequeue(&sk
->sk_write_queue
)) != NULL
);
216 x25
->condition
&= ~X25_COND_ACK_PENDING
;
222 * The following routines are taken from page 170 of the 7th ARRL Computer
223 * Networking Conference paper, as is the whole state machine.
226 void x25_enquiry_response(struct sock
*sk
)
228 struct x25_opt
*x25
= x25_sk(sk
);
230 if (x25
->condition
& X25_COND_OWN_RX_BUSY
)
231 x25_write_internal(sk
, X25_RNR
);
233 x25_write_internal(sk
, X25_RR
);
236 x25
->condition
&= ~X25_COND_ACK_PENDING
;