2 * X.25 Packet Layer release 002
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.
7 * This code REQUIRES 2.1.15 or higher
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.
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.
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.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>
38 #include <asm/segment.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, header_len, max_len;
69 int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
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);
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);
79 frontlen = skb_headroom(skb);
81 while (skb->len > 0) {
82 if ((skbn = sock_alloc_send_skb(sk, frontlen + max_len, noblock, &err)) == NULL){
83 if(err == -EWOULDBLOCK && noblock){
87 SOCK_DEBUG(sk, "x25_output: fragment allocation failed, err=%d, %d bytes sent\n", err, sent);
91 skb_reserve(skbn, frontlen);
93 len = (max_len > skb->len) ? skb->len : max_len;
95 /* Copy the user data */
96 memcpy(skb_put(skbn, len), skb->data, len);
99 /* Duplicate the Header */
100 skb_push(skbn, header_len);
101 memcpy(skbn->data, header, header_len);
104 if (sk->protinfo.x25->neighbour->extended)
105 skbn->data[3] |= X25_EXT_M_BIT;
107 skbn->data[2] |= X25_STD_M_BIT;
110 skb_queue_tail(&sk->write_queue, skbn);
116 skb_queue_tail(&sk->write_queue, skb);
117 sent = skb->len - header_len;
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.
126 static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
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;
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;
141 x25_transmit_link(skb, sk->protinfo.x25->neighbour);
144 void x25_kick(struct sock *sk)
146 struct sk_buff *skb, *skbn;
147 unsigned short start, end;
150 if (sk->protinfo.x25->state != X25_STATE_3)
154 * Transmit interrupt data.
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);
162 if (sk->protinfo.x25->condition & X25_COND_PEER_RX_BUSY)
165 if (skb_peek(&sk->write_queue) == NULL)
168 modulus = (sk->protinfo.x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
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;
176 sk->protinfo.x25->vs = start;
179 * Transmit data until either we're out of data to send or
180 * the window is full.
183 skb = skb_dequeue(&sk->write_queue);
186 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
187 skb_queue_head(&sk->write_queue, skb);
191 skb_set_owner_w(skbn, sk);
194 * Transmit the frame copy.
196 x25_send_iframe(sk, skbn);
198 sk->protinfo.x25->vs = (sk->protinfo.x25->vs + 1) % modulus;
201 * Requeue the original data frame.
203 skb_queue_tail(&sk->protinfo.x25->ack_queue, skb);
205 } while (sk->protinfo.x25->vs != end && (skb = skb_dequeue(&sk->write_queue)) != NULL);
207 sk->protinfo.x25->vl = sk->protinfo.x25->vr;
208 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;
214 * The following routines are taken from page 170 of the 7th ARRL Computer
215 * Networking Conference paper, as is the whole state machine.
218 void x25_enquiry_response(struct sock *sk)
220 if (sk->protinfo.x25->condition & X25_COND_OWN_RX_BUSY)
221 x25_write_internal(sk, X25_RNR);
223 x25_write_internal(sk, X25_RR);
225 sk->protinfo.x25->vl = sk->protinfo.x25->vr;
226 sk->protinfo.x25->condition &= ~X25_COND_ACK_PENDING;