Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *	LAPB release 002
  4 *
  5 *	This code REQUIRES 2.1.15 or higher/ NET3.038
  6 *
 
 
 
 
 
 
  7 *	History
  8 *	LAPB 001	Jonathan Naylor	Started Coding
  9 */
 10
 11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 12
 13#include <linux/errno.h>
 14#include <linux/types.h>
 15#include <linux/socket.h>
 16#include <linux/in.h>
 17#include <linux/kernel.h>
 18#include <linux/timer.h>
 19#include <linux/string.h>
 20#include <linux/sockios.h>
 21#include <linux/net.h>
 22#include <linux/inet.h>
 23#include <linux/skbuff.h>
 24#include <linux/slab.h>
 25#include <net/sock.h>
 26#include <linux/uaccess.h>
 27#include <linux/fcntl.h>
 28#include <linux/mm.h>
 29#include <linux/interrupt.h>
 30#include <net/lapb.h>
 31
 32/*
 33 *	This routine purges all the queues of frames.
 34 */
 35void lapb_clear_queues(struct lapb_cb *lapb)
 36{
 37	skb_queue_purge(&lapb->write_queue);
 38	skb_queue_purge(&lapb->ack_queue);
 39}
 40
 41/*
 42 * This routine purges the input queue of those frames that have been
 43 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
 44 * SDL diagram.
 45 */
 46void lapb_frames_acked(struct lapb_cb *lapb, unsigned short nr)
 47{
 48	struct sk_buff *skb;
 49	int modulus;
 50
 51	modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
 52
 53	/*
 54	 * Remove all the ack-ed frames from the ack queue.
 55	 */
 56	if (lapb->va != nr)
 57		while (skb_peek(&lapb->ack_queue) && lapb->va != nr) {
 58			skb = skb_dequeue(&lapb->ack_queue);
 59			kfree_skb(skb);
 60			lapb->va = (lapb->va + 1) % modulus;
 61		}
 62}
 63
 64void lapb_requeue_frames(struct lapb_cb *lapb)
 65{
 66	struct sk_buff *skb, *skb_prev = NULL;
 67
 68	/*
 69	 * Requeue all the un-ack-ed frames on the output queue to be picked
 70	 * up by lapb_kick called from the timer. This arrangement handles the
 71	 * possibility of an empty output queue.
 72	 */
 73	while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL) {
 74		if (!skb_prev)
 75			skb_queue_head(&lapb->write_queue, skb);
 76		else
 77			skb_append(skb_prev, skb, &lapb->write_queue);
 78		skb_prev = skb;
 79	}
 80}
 81
 82/*
 83 *	Validate that the value of nr is between va and vs. Return true or
 84 *	false for testing.
 85 */
 86int lapb_validate_nr(struct lapb_cb *lapb, unsigned short nr)
 87{
 88	unsigned short vc = lapb->va;
 89	int modulus;
 90
 91	modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
 92
 93	while (vc != lapb->vs) {
 94		if (nr == vc)
 95			return 1;
 96		vc = (vc + 1) % modulus;
 97	}
 98
 99	return nr == lapb->vs;
100}
101
102/*
103 *	This routine is the centralised routine for parsing the control
104 *	information for the different frame formats.
105 */
106int lapb_decode(struct lapb_cb *lapb, struct sk_buff *skb,
107		struct lapb_frame *frame)
108{
109	frame->type = LAPB_ILLEGAL;
110
111	lapb_dbg(2, "(%p) S%d RX %3ph\n", lapb->dev, lapb->state, skb->data);
 
 
112
113	/* We always need to look at 2 bytes, sometimes we need
114	 * to look at 3 and those cases are handled below.
115	 */
116	if (!pskb_may_pull(skb, 2))
117		return -1;
118
119	if (lapb->mode & LAPB_MLP) {
120		if (lapb->mode & LAPB_DCE) {
121			if (skb->data[0] == LAPB_ADDR_D)
122				frame->cr = LAPB_COMMAND;
123			if (skb->data[0] == LAPB_ADDR_C)
124				frame->cr = LAPB_RESPONSE;
125		} else {
126			if (skb->data[0] == LAPB_ADDR_C)
127				frame->cr = LAPB_COMMAND;
128			if (skb->data[0] == LAPB_ADDR_D)
129				frame->cr = LAPB_RESPONSE;
130		}
131	} else {
132		if (lapb->mode & LAPB_DCE) {
133			if (skb->data[0] == LAPB_ADDR_B)
134				frame->cr = LAPB_COMMAND;
135			if (skb->data[0] == LAPB_ADDR_A)
136				frame->cr = LAPB_RESPONSE;
137		} else {
138			if (skb->data[0] == LAPB_ADDR_A)
139				frame->cr = LAPB_COMMAND;
140			if (skb->data[0] == LAPB_ADDR_B)
141				frame->cr = LAPB_RESPONSE;
142		}
143	}
144
145	skb_pull(skb, 1);
146
147	if (lapb->mode & LAPB_EXTENDED) {
148		if (!(skb->data[0] & LAPB_S)) {
149			if (!pskb_may_pull(skb, 2))
150				return -1;
151			/*
152			 * I frame - carries NR/NS/PF
153			 */
154			frame->type       = LAPB_I;
155			frame->ns         = (skb->data[0] >> 1) & 0x7F;
156			frame->nr         = (skb->data[1] >> 1) & 0x7F;
157			frame->pf         = skb->data[1] & LAPB_EPF;
158			frame->control[0] = skb->data[0];
159			frame->control[1] = skb->data[1];
160			skb_pull(skb, 2);
161		} else if ((skb->data[0] & LAPB_U) == 1) {
162			if (!pskb_may_pull(skb, 2))
163				return -1;
164			/*
165			 * S frame - take out PF/NR
166			 */
167			frame->type       = skb->data[0] & 0x0F;
168			frame->nr         = (skb->data[1] >> 1) & 0x7F;
169			frame->pf         = skb->data[1] & LAPB_EPF;
170			frame->control[0] = skb->data[0];
171			frame->control[1] = skb->data[1];
172			skb_pull(skb, 2);
173		} else if ((skb->data[0] & LAPB_U) == 3) {
174			/*
175			 * U frame - take out PF
176			 */
177			frame->type       = skb->data[0] & ~LAPB_SPF;
178			frame->pf         = skb->data[0] & LAPB_SPF;
179			frame->control[0] = skb->data[0];
180			frame->control[1] = 0x00;
181			skb_pull(skb, 1);
182		}
183	} else {
184		if (!(skb->data[0] & LAPB_S)) {
185			/*
186			 * I frame - carries NR/NS/PF
187			 */
188			frame->type = LAPB_I;
189			frame->ns   = (skb->data[0] >> 1) & 0x07;
190			frame->nr   = (skb->data[0] >> 5) & 0x07;
191			frame->pf   = skb->data[0] & LAPB_SPF;
192		} else if ((skb->data[0] & LAPB_U) == 1) {
193			/*
194			 * S frame - take out PF/NR
195			 */
196			frame->type = skb->data[0] & 0x0F;
197			frame->nr   = (skb->data[0] >> 5) & 0x07;
198			frame->pf   = skb->data[0] & LAPB_SPF;
199		} else if ((skb->data[0] & LAPB_U) == 3) {
200			/*
201			 * U frame - take out PF
202			 */
203			frame->type = skb->data[0] & ~LAPB_SPF;
204			frame->pf   = skb->data[0] & LAPB_SPF;
205		}
206
207		frame->control[0] = skb->data[0];
208
209		skb_pull(skb, 1);
210	}
211
212	return 0;
213}
214
215/*
216 *	This routine is called when the HDLC layer internally  generates a
217 *	command or  response  for  the remote machine ( eg. RR, UA etc. ).
218 *	Only supervisory or unnumbered frames are processed, FRMRs are handled
219 *	by lapb_transmit_frmr below.
220 */
221void lapb_send_control(struct lapb_cb *lapb, int frametype,
222		       int poll_bit, int type)
223{
224	struct sk_buff *skb;
225	unsigned char  *dptr;
226
227	if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
228		return;
229
230	skb_reserve(skb, LAPB_HEADER_LEN + 1);
231
232	if (lapb->mode & LAPB_EXTENDED) {
233		if ((frametype & LAPB_U) == LAPB_U) {
234			dptr   = skb_put(skb, 1);
235			*dptr  = frametype;
236			*dptr |= poll_bit ? LAPB_SPF : 0;
237		} else {
238			dptr     = skb_put(skb, 2);
239			dptr[0]  = frametype;
240			dptr[1]  = (lapb->vr << 1);
241			dptr[1] |= poll_bit ? LAPB_EPF : 0;
242		}
243	} else {
244		dptr   = skb_put(skb, 1);
245		*dptr  = frametype;
246		*dptr |= poll_bit ? LAPB_SPF : 0;
247		if ((frametype & LAPB_U) == LAPB_S)	/* S frames carry NR */
248			*dptr |= (lapb->vr << 5);
249	}
250
251	lapb_transmit_buffer(lapb, skb, type);
252}
253
254/*
255 *	This routine generates FRMRs based on information previously stored in
256 *	the LAPB control block.
257 */
258void lapb_transmit_frmr(struct lapb_cb *lapb)
259{
260	struct sk_buff *skb;
261	unsigned char  *dptr;
262
263	if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
264		return;
265
266	skb_reserve(skb, LAPB_HEADER_LEN + 1);
267
268	if (lapb->mode & LAPB_EXTENDED) {
269		dptr    = skb_put(skb, 6);
270		*dptr++ = LAPB_FRMR;
271		*dptr++ = lapb->frmr_data.control[0];
272		*dptr++ = lapb->frmr_data.control[1];
273		*dptr++ = (lapb->vs << 1) & 0xFE;
274		*dptr   = (lapb->vr << 1) & 0xFE;
275		if (lapb->frmr_data.cr == LAPB_RESPONSE)
276			*dptr |= 0x01;
277		dptr++;
278		*dptr++ = lapb->frmr_type;
279
280		lapb_dbg(1, "(%p) S%d TX FRMR %5ph\n",
281			 lapb->dev, lapb->state,
282			 &skb->data[1]);
 
283	} else {
284		dptr    = skb_put(skb, 4);
285		*dptr++ = LAPB_FRMR;
286		*dptr++ = lapb->frmr_data.control[0];
287		*dptr   = (lapb->vs << 1) & 0x0E;
288		*dptr  |= (lapb->vr << 5) & 0xE0;
289		if (lapb->frmr_data.cr == LAPB_RESPONSE)
290			*dptr |= 0x10;
291		dptr++;
292		*dptr++ = lapb->frmr_type;
293
294		lapb_dbg(1, "(%p) S%d TX FRMR %3ph\n",
295			 lapb->dev, lapb->state, &skb->data[1]);
 
296	}
297
298	lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);
299}
v4.6
 
  1/*
  2 *	LAPB release 002
  3 *
  4 *	This code REQUIRES 2.1.15 or higher/ NET3.038
  5 *
  6 *	This module:
  7 *		This module is free software; you can redistribute it and/or
  8 *		modify it under the terms of the GNU General Public License
  9 *		as published by the Free Software Foundation; either version
 10 *		2 of the License, or (at your option) any later version.
 11 *
 12 *	History
 13 *	LAPB 001	Jonathan Naylor	Started Coding
 14 */
 15
 16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 17
 18#include <linux/errno.h>
 19#include <linux/types.h>
 20#include <linux/socket.h>
 21#include <linux/in.h>
 22#include <linux/kernel.h>
 23#include <linux/timer.h>
 24#include <linux/string.h>
 25#include <linux/sockios.h>
 26#include <linux/net.h>
 27#include <linux/inet.h>
 28#include <linux/skbuff.h>
 29#include <linux/slab.h>
 30#include <net/sock.h>
 31#include <asm/uaccess.h>
 32#include <linux/fcntl.h>
 33#include <linux/mm.h>
 34#include <linux/interrupt.h>
 35#include <net/lapb.h>
 36
 37/*
 38 *	This routine purges all the queues of frames.
 39 */
 40void lapb_clear_queues(struct lapb_cb *lapb)
 41{
 42	skb_queue_purge(&lapb->write_queue);
 43	skb_queue_purge(&lapb->ack_queue);
 44}
 45
 46/*
 47 * This routine purges the input queue of those frames that have been
 48 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
 49 * SDL diagram.
 50 */
 51void lapb_frames_acked(struct lapb_cb *lapb, unsigned short nr)
 52{
 53	struct sk_buff *skb;
 54	int modulus;
 55
 56	modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
 57
 58	/*
 59	 * Remove all the ack-ed frames from the ack queue.
 60	 */
 61	if (lapb->va != nr)
 62		while (skb_peek(&lapb->ack_queue) && lapb->va != nr) {
 63			skb = skb_dequeue(&lapb->ack_queue);
 64			kfree_skb(skb);
 65			lapb->va = (lapb->va + 1) % modulus;
 66		}
 67}
 68
 69void lapb_requeue_frames(struct lapb_cb *lapb)
 70{
 71	struct sk_buff *skb, *skb_prev = NULL;
 72
 73	/*
 74	 * Requeue all the un-ack-ed frames on the output queue to be picked
 75	 * up by lapb_kick called from the timer. This arrangement handles the
 76	 * possibility of an empty output queue.
 77	 */
 78	while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL) {
 79		if (!skb_prev)
 80			skb_queue_head(&lapb->write_queue, skb);
 81		else
 82			skb_append(skb_prev, skb, &lapb->write_queue);
 83		skb_prev = skb;
 84	}
 85}
 86
 87/*
 88 *	Validate that the value of nr is between va and vs. Return true or
 89 *	false for testing.
 90 */
 91int lapb_validate_nr(struct lapb_cb *lapb, unsigned short nr)
 92{
 93	unsigned short vc = lapb->va;
 94	int modulus;
 95
 96	modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
 97
 98	while (vc != lapb->vs) {
 99		if (nr == vc)
100			return 1;
101		vc = (vc + 1) % modulus;
102	}
103
104	return nr == lapb->vs;
105}
106
107/*
108 *	This routine is the centralised routine for parsing the control
109 *	information for the different frame formats.
110 */
111int lapb_decode(struct lapb_cb *lapb, struct sk_buff *skb,
112		struct lapb_frame *frame)
113{
114	frame->type = LAPB_ILLEGAL;
115
116	lapb_dbg(2, "(%p) S%d RX %02X %02X %02X\n",
117		 lapb->dev, lapb->state,
118		 skb->data[0], skb->data[1], skb->data[2]);
119
120	/* We always need to look at 2 bytes, sometimes we need
121	 * to look at 3 and those cases are handled below.
122	 */
123	if (!pskb_may_pull(skb, 2))
124		return -1;
125
126	if (lapb->mode & LAPB_MLP) {
127		if (lapb->mode & LAPB_DCE) {
128			if (skb->data[0] == LAPB_ADDR_D)
129				frame->cr = LAPB_COMMAND;
130			if (skb->data[0] == LAPB_ADDR_C)
131				frame->cr = LAPB_RESPONSE;
132		} else {
133			if (skb->data[0] == LAPB_ADDR_C)
134				frame->cr = LAPB_COMMAND;
135			if (skb->data[0] == LAPB_ADDR_D)
136				frame->cr = LAPB_RESPONSE;
137		}
138	} else {
139		if (lapb->mode & LAPB_DCE) {
140			if (skb->data[0] == LAPB_ADDR_B)
141				frame->cr = LAPB_COMMAND;
142			if (skb->data[0] == LAPB_ADDR_A)
143				frame->cr = LAPB_RESPONSE;
144		} else {
145			if (skb->data[0] == LAPB_ADDR_A)
146				frame->cr = LAPB_COMMAND;
147			if (skb->data[0] == LAPB_ADDR_B)
148				frame->cr = LAPB_RESPONSE;
149		}
150	}
151
152	skb_pull(skb, 1);
153
154	if (lapb->mode & LAPB_EXTENDED) {
155		if (!(skb->data[0] & LAPB_S)) {
156			if (!pskb_may_pull(skb, 2))
157				return -1;
158			/*
159			 * I frame - carries NR/NS/PF
160			 */
161			frame->type       = LAPB_I;
162			frame->ns         = (skb->data[0] >> 1) & 0x7F;
163			frame->nr         = (skb->data[1] >> 1) & 0x7F;
164			frame->pf         = skb->data[1] & LAPB_EPF;
165			frame->control[0] = skb->data[0];
166			frame->control[1] = skb->data[1];
167			skb_pull(skb, 2);
168		} else if ((skb->data[0] & LAPB_U) == 1) {
169			if (!pskb_may_pull(skb, 2))
170				return -1;
171			/*
172			 * S frame - take out PF/NR
173			 */
174			frame->type       = skb->data[0] & 0x0F;
175			frame->nr         = (skb->data[1] >> 1) & 0x7F;
176			frame->pf         = skb->data[1] & LAPB_EPF;
177			frame->control[0] = skb->data[0];
178			frame->control[1] = skb->data[1];
179			skb_pull(skb, 2);
180		} else if ((skb->data[0] & LAPB_U) == 3) {
181			/*
182			 * U frame - take out PF
183			 */
184			frame->type       = skb->data[0] & ~LAPB_SPF;
185			frame->pf         = skb->data[0] & LAPB_SPF;
186			frame->control[0] = skb->data[0];
187			frame->control[1] = 0x00;
188			skb_pull(skb, 1);
189		}
190	} else {
191		if (!(skb->data[0] & LAPB_S)) {
192			/*
193			 * I frame - carries NR/NS/PF
194			 */
195			frame->type = LAPB_I;
196			frame->ns   = (skb->data[0] >> 1) & 0x07;
197			frame->nr   = (skb->data[0] >> 5) & 0x07;
198			frame->pf   = skb->data[0] & LAPB_SPF;
199		} else if ((skb->data[0] & LAPB_U) == 1) {
200			/*
201			 * S frame - take out PF/NR
202			 */
203			frame->type = skb->data[0] & 0x0F;
204			frame->nr   = (skb->data[0] >> 5) & 0x07;
205			frame->pf   = skb->data[0] & LAPB_SPF;
206		} else if ((skb->data[0] & LAPB_U) == 3) {
207			/*
208			 * U frame - take out PF
209			 */
210			frame->type = skb->data[0] & ~LAPB_SPF;
211			frame->pf   = skb->data[0] & LAPB_SPF;
212		}
213
214		frame->control[0] = skb->data[0];
215
216		skb_pull(skb, 1);
217	}
218
219	return 0;
220}
221
222/*
223 *	This routine is called when the HDLC layer internally  generates a
224 *	command or  response  for  the remote machine ( eg. RR, UA etc. ).
225 *	Only supervisory or unnumbered frames are processed, FRMRs are handled
226 *	by lapb_transmit_frmr below.
227 */
228void lapb_send_control(struct lapb_cb *lapb, int frametype,
229		       int poll_bit, int type)
230{
231	struct sk_buff *skb;
232	unsigned char  *dptr;
233
234	if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
235		return;
236
237	skb_reserve(skb, LAPB_HEADER_LEN + 1);
238
239	if (lapb->mode & LAPB_EXTENDED) {
240		if ((frametype & LAPB_U) == LAPB_U) {
241			dptr   = skb_put(skb, 1);
242			*dptr  = frametype;
243			*dptr |= poll_bit ? LAPB_SPF : 0;
244		} else {
245			dptr     = skb_put(skb, 2);
246			dptr[0]  = frametype;
247			dptr[1]  = (lapb->vr << 1);
248			dptr[1] |= poll_bit ? LAPB_EPF : 0;
249		}
250	} else {
251		dptr   = skb_put(skb, 1);
252		*dptr  = frametype;
253		*dptr |= poll_bit ? LAPB_SPF : 0;
254		if ((frametype & LAPB_U) == LAPB_S)	/* S frames carry NR */
255			*dptr |= (lapb->vr << 5);
256	}
257
258	lapb_transmit_buffer(lapb, skb, type);
259}
260
261/*
262 *	This routine generates FRMRs based on information previously stored in
263 *	the LAPB control block.
264 */
265void lapb_transmit_frmr(struct lapb_cb *lapb)
266{
267	struct sk_buff *skb;
268	unsigned char  *dptr;
269
270	if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
271		return;
272
273	skb_reserve(skb, LAPB_HEADER_LEN + 1);
274
275	if (lapb->mode & LAPB_EXTENDED) {
276		dptr    = skb_put(skb, 6);
277		*dptr++ = LAPB_FRMR;
278		*dptr++ = lapb->frmr_data.control[0];
279		*dptr++ = lapb->frmr_data.control[1];
280		*dptr++ = (lapb->vs << 1) & 0xFE;
281		*dptr   = (lapb->vr << 1) & 0xFE;
282		if (lapb->frmr_data.cr == LAPB_RESPONSE)
283			*dptr |= 0x01;
284		dptr++;
285		*dptr++ = lapb->frmr_type;
286
287		lapb_dbg(1, "(%p) S%d TX FRMR %02X %02X %02X %02X %02X\n",
288			 lapb->dev, lapb->state,
289			 skb->data[1], skb->data[2], skb->data[3],
290			 skb->data[4], skb->data[5]);
291	} else {
292		dptr    = skb_put(skb, 4);
293		*dptr++ = LAPB_FRMR;
294		*dptr++ = lapb->frmr_data.control[0];
295		*dptr   = (lapb->vs << 1) & 0x0E;
296		*dptr  |= (lapb->vr << 5) & 0xE0;
297		if (lapb->frmr_data.cr == LAPB_RESPONSE)
298			*dptr |= 0x10;
299		dptr++;
300		*dptr++ = lapb->frmr_type;
301
302		lapb_dbg(1, "(%p) S%d TX FRMR %02X %02X %02X\n",
303			 lapb->dev, lapb->state, skb->data[1],
304			 skb->data[2], skb->data[3]);
305	}
306
307	lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);
308}