1 /*
2 * X.25 Packet Layer release 002
3 *
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.
6 *
7 * This code REQUIRES 2.1.15 or higher
8 *
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.
14 *
15 * History
16 * X.25 001 Jonathan Naylor Started coding.
17 * X.25 002 Jonathan Naylor New timer architecture.
18 * 2000-09-04 Henner Eisen Prevented x25_output() skb leakage.
19 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
20 * 2000-11-10 Henner Eisen x25_send_iframe(): re-queued frames
21 * needed cleaned seq-number fields.
22 */
23
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/string.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/inet.h>
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <net/sock.h>
38 #include <asm/segment.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
x25_pacsize_to_bytes(unsigned int pacsize)45 static int x25_pacsize_to_bytes(unsigned int pacsize)
46 {
47 int bytes = 1;
48
49 if (pacsize == 0)
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 */
x25_output(struct sock * sk,struct sk_buff * skb)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, header_len, max_len;
69 int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
70
71 header_len = (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
72 max_len = x25_pacsize_to_bytes(sk->protinfo.x25->facilities.pacsize_out);
73
74 if (skb->len - header_len > max_len) {
75 /* Save a copy of the Header */
76 memcpy(header, skb->data, header_len);
77 skb_pull(skb, header_len);
78
79 frontlen = skb_headroom(skb);
80
81 while (skb->len > 0) {
82 if ((skbn = sock_alloc_send_skb(sk, frontlen + max_len, noblock, &err)) == NULL){
83 if(err == -EWOULDBLOCK && noblock){
84 kfree_skb(skb);
85 return sent;
86 }
87 SOCK_DEBUG(sk, "x25_output: fragment allocation failed, err=%d, %d bytes sent\n", err, sent);
88 return err;
89 }
90
91 skb_reserve(skbn, frontlen);
92
93 len = (max_len > skb->len) ? skb->len : max_len;
94
95 /* Copy the user data */
96 memcpy(skb_put(skbn, len), skb->data, len);
97 skb_pull(skb, len);
98
99 /* Duplicate the Header */
100 skb_push(skbn, header_len);
101 memcpy(skbn->data, header, header_len);
102
103 if (skb->len > 0) {
104 if (sk->protinfo.x25->neighbour->extended)
105 skbn->data[3] |= X25_EXT_M_BIT;
106 else
107 skbn->data[2] |= X25_STD_M_BIT;
108 }
109
110 skb_queue_tail(&sk->write_queue, skbn);
111 sent += len;
112 }
113
114 kfree_skb(skb);
115 } else {
116 skb_queue_tail(&sk->write_queue, skb);
117 sent = skb->len - header_len;
118 }
119 return sent;
120 }
121
122 /*
123 * This procedure is passed a buffer descriptor for an iframe. It builds
124 * the rest of the control part of the frame and then writes it out.
125 */
x25_send_iframe(struct sock * sk,struct sk_buff * skb)126 static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
127 {
128 if (skb == NULL)
129 return;
130
131 if (sk->protinfo.x25->neighbour->extended) {
132 skb->data[2] = (sk->protinfo.x25->vs << 1) & 0xFE;
133 skb->data[3] &= X25_EXT_M_BIT;
134 skb->data[3] |= (sk->protinfo.x25->vr << 1) & 0xFE;
135 } else {
136 skb->data[2] &= X25_STD_M_BIT;
137 skb->data[2] |= (sk->protinfo.x25->vs << 1) & 0x0E;
138 skb->data[2] |= (sk->protinfo.x25->vr << 5) & 0xE0;
139 }
140
141 x25_transmit_link(skb, sk->protinfo.x25->neighbour);
142 }
143
x25_kick(struct sock * sk)144 void x25_kick(struct sock *sk)
145 {
146 struct sk_buff *skb, *skbn;
147 unsigned short start, end;
148 int modulus;
149
150 if (sk->protinfo.x25->state != X25_STATE_3)
151 return;
152
153 /*
154 * Transmit interrupt data.
155 */
156 if (!sk->protinfo.x25->intflag && skb_peek(&sk->protinfo.x25->interrupt_out_queue) != NULL) {
157 sk->protinfo.x25->intflag = 1;
158 skb = skb_dequeue(&sk->protinfo.x25->interrupt_out_queue);
159 x25_transmit_link(skb, sk->protinfo.x25->neighbour);
160 }
161
162 if (sk->protinfo.x25->condition & X25_COND_PEER_RX_BUSY)
163 return;
164
165 if (skb_peek(&sk->write_queue) == NULL)
166 return;
167
168 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
169
170 start = (skb_peek(&sk->protinfo.x25->ack_queue) == NULL) ? sk->protinfo.x25->va : sk->protinfo.x25->vs;
171 end = (sk->protinfo.x25->va + sk->protinfo.x25->facilities.winsize_out) % modulus;
172
173 if (start == end)
174 return;
175
176 sk->protinfo.x25->vs = start;
177
178 /*
179 * Transmit data until either we're out of data to send or
180 * the window is full.
181 */
182
183 skb = skb_dequeue(&sk->write_queue);
184
185 do {
186 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
187 skb_queue_head(&sk->write_queue, skb);
188 break;
189 }
190
191 skb_set_owner_w(skbn, sk);
192
193 /*
194 * Transmit the frame copy.
195 */
196 x25_send_iframe(sk, skbn);
197
198 sk->protinfo.x25->vs = (sk->protinfo.x25->vs + 1) % modulus;
199
200 /*
201 * Requeue the original data frame.
202 */
203 skb_queue_tail(&sk->protinfo.x25->ack_queue, skb);
204
205 } while (sk->protinfo.x25->vs != end && (skb = skb_dequeue(&sk->write_queue)) != NULL);
206
207 sk->protinfo.x25->vl = sk->protinfo.x25->vr;
208 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
209
210 x25_stop_timer(sk);
211 }
212
213 /*
214 * The following routines are taken from page 170 of the 7th ARRL Computer
215 * Networking Conference paper, as is the whole state machine.
216 */
217
x25_enquiry_response(struct sock * sk)218 void x25_enquiry_response(struct sock *sk)
219 {
220 if (sk->protinfo.x25->condition & X25_COND_OWN_RX_BUSY)
221 x25_write_internal(sk, X25_RNR);
222 else
223 x25_write_internal(sk, X25_RR);
224
225 sk->protinfo.x25->vl = sk->protinfo.x25->vr;
226 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
227
228 x25_stop_timer(sk);
229 }
230