initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / x25 / x25_out.c
blobed6b6d84b3d0d64e3f91c85e043e597448ca95cd
1 /*
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
10 * This module:
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.
16 * History
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>
28 #include <linux/in.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>
38 #include <net/sock.h>
39 #include <asm/system.h>
40 #include <linux/fcntl.h>
41 #include <linux/mm.h>
42 #include <linux/interrupt.h>
43 #include <net/x25.h>
45 static int x25_pacsize_to_bytes(unsigned int pacsize)
47 int bytes = 1;
49 if (!pacsize)
50 return 128;
52 while (pacsize-- > 0)
53 bytes *= 2;
55 return bytes;
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)
66 struct sk_buff *skbn;
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 :
72 X25_STD_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){
86 kfree_skb(skb);
87 return sent;
89 SOCK_DEBUG(sk, "x25_output: fragment alloc"
90 " failed, err=%d, %d bytes "
91 "sent\n", err, sent);
92 return err;
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);
101 skb_pull(skb, len);
103 /* Duplicate the Header */
104 skb_push(skbn, header_len);
105 memcpy(skbn->data, header, header_len);
107 if (skb->len > 0) {
108 if (x25->neighbour->extended)
109 skbn->data[3] |= X25_EXT_M_BIT;
110 else
111 skbn->data[2] |= X25_STD_M_BIT;
114 skb_queue_tail(&sk->sk_write_queue, skbn);
115 sent += len;
118 kfree_skb(skb);
119 } else {
120 skb_queue_tail(&sk->sk_write_queue, skb);
121 sent = skb->len - header_len;
123 return sent;
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);
134 if (!skb)
135 return;
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;
141 } else {
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;
154 int modulus;
155 struct x25_opt *x25 = x25_sk(sk);
157 if (x25->state != X25_STATE_3)
158 return;
161 * Transmit interrupt data.
163 if (!x25->intflag && skb_peek(&x25->interrupt_out_queue) != NULL) {
164 x25->intflag = 1;
165 skb = skb_dequeue(&x25->interrupt_out_queue);
166 x25_transmit_link(skb, x25->neighbour);
169 if (x25->condition & X25_COND_PEER_RX_BUSY)
170 return;
172 if (!skb_peek(&sk->sk_write_queue))
173 return;
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;
180 if (start == end)
181 return;
183 x25->vs = start;
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);
192 do {
193 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
194 skb_queue_head(&sk->sk_write_queue, skb);
195 break;
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);
215 x25->vl = x25->vr;
216 x25->condition &= ~X25_COND_ACK_PENDING;
218 x25_stop_timer(sk);
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);
232 else
233 x25_write_internal(sk, X25_RR);
235 x25->vl = x25->vr;
236 x25->condition &= ~X25_COND_ACK_PENDING;
238 x25_stop_timer(sk);