Linux Audio

Check our new training course

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