1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * X.25 Packet Layer release 002
5 * This is ALPHA test software. This code may break your machine,
6 * randomly fail to work with new releases, misbehave and/or generally
7 * screw up. It might even work.
9 * This code REQUIRES 2.1.15 or higher
12 * X.25 001 Jonathan Naylor Started coding.
13 * X.25 002 Jonathan Naylor New timer architecture.
14 * 2000-09-04 Henner Eisen Prevented x25_output() skb leakage.
15 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
16 * 2000-11-10 Henner Eisen x25_send_iframe(): re-queued frames
17 * needed cleaned seq-number fields.
20 #include <linux/slab.h>
21 #include <linux/socket.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/skbuff.h>
28 static int x25_pacsize_to_bytes(unsigned int pacsize)
42 * This is where all X.25 information frames pass.
44 * Returns the amount of user data bytes sent on success
45 * or a negative error code on failure.
47 int x25_output(struct sock *sk, struct sk_buff *skb)
50 unsigned char header[X25_EXT_MIN_LEN];
51 int err, frontlen, len;
52 int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
53 struct x25_sock *x25 = x25_sk(sk);
54 int header_len = x25->neighbour->extended ? X25_EXT_MIN_LEN :
56 int max_len = x25_pacsize_to_bytes(x25->facilities.pacsize_out);
58 if (skb->len - header_len > max_len) {
59 /* Save a copy of the Header */
60 skb_copy_from_linear_data(skb, header, header_len);
61 skb_pull(skb, header_len);
63 frontlen = skb_headroom(skb);
65 while (skb->len > 0) {
67 skbn = sock_alloc_send_skb(sk, frontlen + max_len,
71 if (err == -EWOULDBLOCK && noblock){
75 SOCK_DEBUG(sk, "x25_output: fragment alloc"
76 " failed, err=%d, %d bytes "
81 skb_reserve(skbn, frontlen);
83 len = max_len > skb->len ? skb->len : max_len;
85 /* Copy the user data */
86 skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
89 /* Duplicate the Header */
90 skb_push(skbn, header_len);
91 skb_copy_to_linear_data(skbn, header, header_len);
94 if (x25->neighbour->extended)
95 skbn->data[3] |= X25_EXT_M_BIT;
97 skbn->data[2] |= X25_STD_M_BIT;
100 skb_queue_tail(&sk->sk_write_queue, skbn);
106 skb_queue_tail(&sk->sk_write_queue, skb);
107 sent = skb->len - header_len;
113 * This procedure is passed a buffer descriptor for an iframe. It builds
114 * the rest of the control part of the frame and then writes it out.
116 static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
118 struct x25_sock *x25 = x25_sk(sk);
123 if (x25->neighbour->extended) {
124 skb->data[2] = (x25->vs << 1) & 0xFE;
125 skb->data[3] &= X25_EXT_M_BIT;
126 skb->data[3] |= (x25->vr << 1) & 0xFE;
128 skb->data[2] &= X25_STD_M_BIT;
129 skb->data[2] |= (x25->vs << 1) & 0x0E;
130 skb->data[2] |= (x25->vr << 5) & 0xE0;
133 x25_transmit_link(skb, x25->neighbour);
136 void x25_kick(struct sock *sk)
138 struct sk_buff *skb, *skbn;
139 unsigned short start, end;
141 struct x25_sock *x25 = x25_sk(sk);
143 if (x25->state != X25_STATE_3)
147 * Transmit interrupt data.
149 if (skb_peek(&x25->interrupt_out_queue) != NULL &&
150 !test_and_set_bit(X25_INTERRUPT_FLAG, &x25->flags)) {
152 skb = skb_dequeue(&x25->interrupt_out_queue);
153 x25_transmit_link(skb, x25->neighbour);
156 if (x25->condition & X25_COND_PEER_RX_BUSY)
159 if (!skb_peek(&sk->sk_write_queue))
162 modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
164 start = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
165 end = (x25->va + x25->facilities.winsize_out) % modulus;
173 * Transmit data until either we're out of data to send or
174 * the window is full.
177 skb = skb_dequeue(&sk->sk_write_queue);
180 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
181 skb_queue_head(&sk->sk_write_queue, skb);
185 skb_set_owner_w(skbn, sk);
188 * Transmit the frame copy.
190 x25_send_iframe(sk, skbn);
192 x25->vs = (x25->vs + 1) % modulus;
195 * Requeue the original data frame.
197 skb_queue_tail(&x25->ack_queue, skb);
199 } while (x25->vs != end &&
200 (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
203 x25->condition &= ~X25_COND_ACK_PENDING;
209 * The following routines are taken from page 170 of the 7th ARRL Computer
210 * Networking Conference paper, as is the whole state machine.
213 void x25_enquiry_response(struct sock *sk)
215 struct x25_sock *x25 = x25_sk(sk);
217 if (x25->condition & X25_COND_OWN_RX_BUSY)
218 x25_write_internal(sk, X25_RNR);
220 x25_write_internal(sk, X25_RR);
223 x25->condition &= ~X25_COND_ACK_PENDING;