ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / x25 / x25_out.c
1 /*
2  *      X.25 Packet Layer release 002
3  *
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. 
7  *
8  *      This code REQUIRES 2.1.15 or higher
9  *
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.
15  *
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.
23  */
24
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>
44
45 static int x25_pacsize_to_bytes(unsigned int pacsize)
46 {
47         int bytes = 1;
48
49         if (!pacsize)
50                 return 128;
51
52         while (pacsize-- > 0)
53                 bytes *= 2;
54
55         return bytes;
56 }
57
58 /*
59  *      This is where all X.25 information frames pass.
60  *
61  *      Returns the amount of user data bytes sent on success
62  *      or a negative error code on failure.
63  */
64 int x25_output(struct sock *sk, struct sk_buff *skb)
65 {
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);
74
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);
79
80                 frontlen = skb_headroom(skb);
81
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;
88                                 }
89                                 SOCK_DEBUG(sk, "x25_output: fragment alloc"
90                                                " failed, err=%d, %d bytes "
91                                                "sent\n", err, sent);
92                                 return err;
93                         }
94                                 
95                         skb_reserve(skbn, frontlen);
96
97                         len = max_len > skb->len ? skb->len : max_len;
98
99                         /* Copy the user data */
100                         memcpy(skb_put(skbn, len), skb->data, len);
101                         skb_pull(skb, len);
102
103                         /* Duplicate the Header */
104                         skb_push(skbn, header_len);
105                         memcpy(skbn->data, header, header_len);
106
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;
112                         }
113
114                         skb_queue_tail(&sk->sk_write_queue, skbn);
115                         sent += len;
116                 }
117                 
118                 kfree_skb(skb);
119         } else {
120                 skb_queue_tail(&sk->sk_write_queue, skb);
121                 sent = skb->len - header_len;
122         }
123         return sent;
124 }
125
126 /* 
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.
129  */
130 static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
131 {
132         struct x25_opt *x25 = x25_sk(sk);
133
134         if (!skb)
135                 return;
136
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;
145         }
146
147         x25_transmit_link(skb, x25->neighbour); 
148 }
149
150 void x25_kick(struct sock *sk)
151 {
152         struct sk_buff *skb, *skbn;
153         unsigned short start, end;
154         int modulus;
155         struct x25_opt *x25 = x25_sk(sk);
156
157         if (x25->state != X25_STATE_3)
158                 return;
159
160         /*
161          *      Transmit interrupt data.
162          */
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);
167         }
168
169         if (x25->condition & X25_COND_PEER_RX_BUSY)
170                 return;
171
172         if (!skb_peek(&sk->sk_write_queue))
173                 return;
174
175         modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
176
177         start   = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
178         end     = (x25->va + x25->facilities.winsize_out) % modulus;
179
180         if (start == end)
181                 return;
182
183         x25->vs = start;
184
185         /*
186          * Transmit data until either we're out of data to send or
187          * the window is full.
188          */
189
190         skb = skb_dequeue(&sk->sk_write_queue);
191
192         do {
193                 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
194                         skb_queue_head(&sk->sk_write_queue, skb);
195                         break;
196                 }
197
198                 skb_set_owner_w(skbn, sk);
199
200                 /*
201                  * Transmit the frame copy.
202                  */
203                 x25_send_iframe(sk, skbn);
204
205                 x25->vs = (x25->vs + 1) % modulus;
206
207                 /*
208                  * Requeue the original data frame.
209                  */
210                 skb_queue_tail(&x25->ack_queue, skb);
211
212         } while (x25->vs != end &&
213                  (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
214
215         x25->vl         = x25->vr;
216         x25->condition &= ~X25_COND_ACK_PENDING;
217
218         x25_stop_timer(sk);
219 }
220
221 /*
222  * The following routines are taken from page 170 of the 7th ARRL Computer
223  * Networking Conference paper, as is the whole state machine.
224  */
225
226 void x25_enquiry_response(struct sock *sk)
227 {
228         struct x25_opt *x25 = x25_sk(sk);
229
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);
234
235         x25->vl         = x25->vr;
236         x25->condition &= ~X25_COND_ACK_PENDING;
237
238         x25_stop_timer(sk);
239 }