Linux Audio

Check our new training course

Loading...
v4.6
 
  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/slab.h>
 26#include <linux/socket.h>
 27#include <linux/kernel.h>
 28#include <linux/string.h>
 29#include <linux/skbuff.h>
 30#include <net/sock.h>
 31#include <net/x25.h>
 32
 33static int x25_pacsize_to_bytes(unsigned int pacsize)
 34{
 35	int bytes = 1;
 36
 37	if (!pacsize)
 38		return 128;
 39
 40	while (pacsize-- > 0)
 41		bytes *= 2;
 42
 43	return bytes;
 44}
 45
 46/*
 47 *	This is where all X.25 information frames pass.
 48 *
 49 *      Returns the amount of user data bytes sent on success
 50 *      or a negative error code on failure.
 51 */
 52int x25_output(struct sock *sk, struct sk_buff *skb)
 53{
 54	struct sk_buff *skbn;
 55	unsigned char header[X25_EXT_MIN_LEN];
 56	int err, frontlen, len;
 57	int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
 58	struct x25_sock *x25 = x25_sk(sk);
 59	int header_len = x25->neighbour->extended ? X25_EXT_MIN_LEN :
 60						    X25_STD_MIN_LEN;
 61	int max_len = x25_pacsize_to_bytes(x25->facilities.pacsize_out);
 62
 63	if (skb->len - header_len > max_len) {
 64		/* Save a copy of the Header */
 65		skb_copy_from_linear_data(skb, header, header_len);
 66		skb_pull(skb, header_len);
 67
 68		frontlen = skb_headroom(skb);
 69
 70		while (skb->len > 0) {
 71			release_sock(sk);
 72			skbn = sock_alloc_send_skb(sk, frontlen + max_len,
 73						   noblock, &err);
 74			lock_sock(sk);
 75			if (!skbn) {
 76				if (err == -EWOULDBLOCK && noblock){
 77					kfree_skb(skb);
 78					return sent;
 79				}
 80				SOCK_DEBUG(sk, "x25_output: fragment alloc"
 81					       " failed, err=%d, %d bytes "
 82					       "sent\n", err, sent);
 83				return err;
 84			}
 85
 86			skb_reserve(skbn, frontlen);
 87
 88			len = max_len > skb->len ? skb->len : max_len;
 89
 90			/* Copy the user data */
 91			skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
 92			skb_pull(skb, len);
 93
 94			/* Duplicate the Header */
 95			skb_push(skbn, header_len);
 96			skb_copy_to_linear_data(skbn, header, header_len);
 97
 98			if (skb->len > 0) {
 99				if (x25->neighbour->extended)
100					skbn->data[3] |= X25_EXT_M_BIT;
101				else
102					skbn->data[2] |= X25_STD_M_BIT;
103			}
104
105			skb_queue_tail(&sk->sk_write_queue, skbn);
106			sent += len;
107		}
108
109		kfree_skb(skb);
110	} else {
111		skb_queue_tail(&sk->sk_write_queue, skb);
112		sent = skb->len - header_len;
113	}
114	return sent;
115}
116
117/*
118 *	This procedure is passed a buffer descriptor for an iframe. It builds
119 *	the rest of the control part of the frame and then writes it out.
120 */
121static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
122{
123	struct x25_sock *x25 = x25_sk(sk);
124
125	if (!skb)
126		return;
127
128	if (x25->neighbour->extended) {
129		skb->data[2]  = (x25->vs << 1) & 0xFE;
130		skb->data[3] &= X25_EXT_M_BIT;
131		skb->data[3] |= (x25->vr << 1) & 0xFE;
132	} else {
133		skb->data[2] &= X25_STD_M_BIT;
134		skb->data[2] |= (x25->vs << 1) & 0x0E;
135		skb->data[2] |= (x25->vr << 5) & 0xE0;
136	}
137
138	x25_transmit_link(skb, x25->neighbour);
139}
140
141void x25_kick(struct sock *sk)
142{
143	struct sk_buff *skb, *skbn;
144	unsigned short start, end;
145	int modulus;
146	struct x25_sock *x25 = x25_sk(sk);
147
148	if (x25->state != X25_STATE_3)
149		return;
150
151	/*
152	 *	Transmit interrupt data.
153	 */
154	if (skb_peek(&x25->interrupt_out_queue) != NULL &&
155		!test_and_set_bit(X25_INTERRUPT_FLAG, &x25->flags)) {
156
157		skb = skb_dequeue(&x25->interrupt_out_queue);
158		x25_transmit_link(skb, x25->neighbour);
159	}
160
161	if (x25->condition & X25_COND_PEER_RX_BUSY)
162		return;
163
164	if (!skb_peek(&sk->sk_write_queue))
165		return;
166
167	modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
168
169	start   = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
170	end     = (x25->va + x25->facilities.winsize_out) % modulus;
171
172	if (start == end)
173		return;
174
175	x25->vs = start;
176
177	/*
178	 * Transmit data until either we're out of data to send or
179	 * the window is full.
180	 */
181
182	skb = skb_dequeue(&sk->sk_write_queue);
183
184	do {
185		if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
186			skb_queue_head(&sk->sk_write_queue, skb);
187			break;
188		}
189
190		skb_set_owner_w(skbn, sk);
191
192		/*
193		 * Transmit the frame copy.
194		 */
195		x25_send_iframe(sk, skbn);
196
197		x25->vs = (x25->vs + 1) % modulus;
198
199		/*
200		 * Requeue the original data frame.
201		 */
202		skb_queue_tail(&x25->ack_queue, skb);
203
204	} while (x25->vs != end &&
205		 (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
206
207	x25->vl         = x25->vr;
208	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
218void x25_enquiry_response(struct sock *sk)
219{
220	struct x25_sock *x25 = x25_sk(sk);
221
222	if (x25->condition & X25_COND_OWN_RX_BUSY)
223		x25_write_internal(sk, X25_RNR);
224	else
225		x25_write_internal(sk, X25_RR);
226
227	x25->vl         = x25->vr;
228	x25->condition &= ~X25_COND_ACK_PENDING;
229
230	x25_stop_timer(sk);
231}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *	X.25 Packet Layer release 002
  4 *
  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.
  8 *
  9 *	This code REQUIRES 2.1.15 or higher
 10 *
 
 
 
 
 
 
 11 *	History
 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.
 18 */
 19
 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>
 25#include <net/sock.h>
 26#include <net/x25.h>
 27
 28static int x25_pacsize_to_bytes(unsigned int pacsize)
 29{
 30	int bytes = 1;
 31
 32	if (!pacsize)
 33		return 128;
 34
 35	while (pacsize-- > 0)
 36		bytes *= 2;
 37
 38	return bytes;
 39}
 40
 41/*
 42 *	This is where all X.25 information frames pass.
 43 *
 44 *      Returns the amount of user data bytes sent on success
 45 *      or a negative error code on failure.
 46 */
 47int x25_output(struct sock *sk, struct sk_buff *skb)
 48{
 49	struct sk_buff *skbn;
 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 :
 55						    X25_STD_MIN_LEN;
 56	int max_len = x25_pacsize_to_bytes(x25->facilities.pacsize_out);
 57
 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);
 62
 63		frontlen = skb_headroom(skb);
 64
 65		while (skb->len > 0) {
 66			release_sock(sk);
 67			skbn = sock_alloc_send_skb(sk, frontlen + max_len,
 68						   noblock, &err);
 69			lock_sock(sk);
 70			if (!skbn) {
 71				if (err == -EWOULDBLOCK && noblock){
 72					kfree_skb(skb);
 73					return sent;
 74				}
 75				net_dbg_ratelimited("x25_output: fragment alloc"
 76					       " failed, err=%d, %d bytes "
 77					       "sent\n", err, sent);
 78				return err;
 79			}
 80
 81			skb_reserve(skbn, frontlen);
 82
 83			len = max_len > skb->len ? skb->len : max_len;
 84
 85			/* Copy the user data */
 86			skb_copy_from_linear_data(skb, skb_put(skbn, len), len);
 87			skb_pull(skb, len);
 88
 89			/* Duplicate the Header */
 90			skb_push(skbn, header_len);
 91			skb_copy_to_linear_data(skbn, header, header_len);
 92
 93			if (skb->len > 0) {
 94				if (x25->neighbour->extended)
 95					skbn->data[3] |= X25_EXT_M_BIT;
 96				else
 97					skbn->data[2] |= X25_STD_M_BIT;
 98			}
 99
100			skb_queue_tail(&sk->sk_write_queue, skbn);
101			sent += len;
102		}
103
104		kfree_skb(skb);
105	} else {
106		skb_queue_tail(&sk->sk_write_queue, skb);
107		sent = skb->len - header_len;
108	}
109	return sent;
110}
111
112/*
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.
115 */
116static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
117{
118	struct x25_sock *x25 = x25_sk(sk);
119
120	if (!skb)
121		return;
122
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;
127	} else {
128		skb->data[2] &= X25_STD_M_BIT;
129		skb->data[2] |= (x25->vs << 1) & 0x0E;
130		skb->data[2] |= (x25->vr << 5) & 0xE0;
131	}
132
133	x25_transmit_link(skb, x25->neighbour);
134}
135
136void x25_kick(struct sock *sk)
137{
138	struct sk_buff *skb, *skbn;
139	unsigned short start, end;
140	int modulus;
141	struct x25_sock *x25 = x25_sk(sk);
142
143	if (x25->state != X25_STATE_3)
144		return;
145
146	/*
147	 *	Transmit interrupt data.
148	 */
149	if (skb_peek(&x25->interrupt_out_queue) != NULL &&
150		!test_and_set_bit(X25_INTERRUPT_FLAG, &x25->flags)) {
151
152		skb = skb_dequeue(&x25->interrupt_out_queue);
153		x25_transmit_link(skb, x25->neighbour);
154	}
155
156	if (x25->condition & X25_COND_PEER_RX_BUSY)
157		return;
158
159	if (!skb_peek(&sk->sk_write_queue))
160		return;
161
162	modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
163
164	start   = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
165	end     = (x25->va + x25->facilities.winsize_out) % modulus;
166
167	if (start == end)
168		return;
169
170	x25->vs = start;
171
172	/*
173	 * Transmit data until either we're out of data to send or
174	 * the window is full.
175	 */
176
177	skb = skb_dequeue(&sk->sk_write_queue);
178
179	do {
180		if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
181			skb_queue_head(&sk->sk_write_queue, skb);
182			break;
183		}
184
185		skb_set_owner_w(skbn, sk);
186
187		/*
188		 * Transmit the frame copy.
189		 */
190		x25_send_iframe(sk, skbn);
191
192		x25->vs = (x25->vs + 1) % modulus;
193
194		/*
195		 * Requeue the original data frame.
196		 */
197		skb_queue_tail(&x25->ack_queue, skb);
198
199	} while (x25->vs != end &&
200		 (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
201
202	x25->vl         = x25->vr;
203	x25->condition &= ~X25_COND_ACK_PENDING;
204
205	x25_stop_timer(sk);
206}
207
208/*
209 * The following routines are taken from page 170 of the 7th ARRL Computer
210 * Networking Conference paper, as is the whole state machine.
211 */
212
213void x25_enquiry_response(struct sock *sk)
214{
215	struct x25_sock *x25 = x25_sk(sk);
216
217	if (x25->condition & X25_COND_OWN_RX_BUSY)
218		x25_write_internal(sk, X25_RNR);
219	else
220		x25_write_internal(sk, X25_RR);
221
222	x25->vl         = x25->vr;
223	x25->condition &= ~X25_COND_ACK_PENDING;
224
225	x25_stop_timer(sk);
226}