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/sched.h>
22 #include <linux/timer.h>
23 #include <linux/string.h>
24 #include <linux/sockios.h>
25 #include <linux/net.h>
26 #include <linux/inet.h>
27 #include <linux/skbuff.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  */
lapb_clear_queues(lapb_cb * lapb)39 void lapb_clear_queues(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  */
lapb_frames_acked(lapb_cb * lapb,unsigned short nr)50 void lapb_frames_acked(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) != NULL && lapb->va != nr) {
62 		        skb = skb_dequeue(&lapb->ack_queue);
63 			kfree_skb(skb);
64 			lapb->va = (lapb->va + 1) % modulus;
65 		}
66 	}
67 }
68 
lapb_requeue_frames(lapb_cb * lapb)69 void lapb_requeue_frames(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 == NULL)
80 			skb_queue_head(&lapb->write_queue, skb);
81 		else
82 			skb_append(skb_prev, skb);
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  */
lapb_validate_nr(lapb_cb * lapb,unsigned short nr)91 int lapb_validate_nr(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) return 1;
100 		vc = (vc + 1) % modulus;
101 	}
102 
103 	if (nr == lapb->vs) return 1;
104 
105 	return 0;
106 }
107 
108 /*
109  *	This routine is the centralised routine for parsing the control
110  *	information for the different frame formats.
111  */
lapb_decode(lapb_cb * lapb,struct sk_buff * skb,struct lapb_frame * frame)112 void lapb_decode(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
113 {
114 	frame->type = LAPB_ILLEGAL;
115 
116 #if LAPB_DEBUG > 2
117 	printk(KERN_DEBUG "lapb: (%p) S%d RX %02X %02X %02X\n", lapb->token, lapb->state, skb->data[0], skb->data[1], skb->data[2]);
118 #endif
119 
120 	if (lapb->mode & LAPB_MLP) {
121 		if (lapb->mode & LAPB_DCE) {
122 			if (skb->data[0] == LAPB_ADDR_D)
123 				frame->cr = LAPB_COMMAND;
124 			if (skb->data[0] == LAPB_ADDR_C)
125 				frame->cr = LAPB_RESPONSE;
126 		} else {
127 			if (skb->data[0] == LAPB_ADDR_C)
128 				frame->cr = LAPB_COMMAND;
129 			if (skb->data[0] == LAPB_ADDR_D)
130 				frame->cr = LAPB_RESPONSE;
131 		}
132 	} else {
133 		if (lapb->mode & LAPB_DCE) {
134 			if (skb->data[0] == LAPB_ADDR_B)
135 				frame->cr = LAPB_COMMAND;
136 			if (skb->data[0] == LAPB_ADDR_A)
137 				frame->cr = LAPB_RESPONSE;
138 		} else {
139 			if (skb->data[0] == LAPB_ADDR_A)
140 				frame->cr = LAPB_COMMAND;
141 			if (skb->data[0] == LAPB_ADDR_B)
142 				frame->cr = LAPB_RESPONSE;
143 		}
144 	}
145 
146 	skb_pull(skb, 1);
147 
148 	if (lapb->mode & LAPB_EXTENDED) {
149 		if ((skb->data[0] & LAPB_S) == 0) {
150 			frame->type       = LAPB_I;			/* I frame - carries NR/NS/PF */
151 			frame->ns         = (skb->data[0] >> 1) & 0x7F;
152 			frame->nr         = (skb->data[1] >> 1) & 0x7F;
153 			frame->pf         = skb->data[1] & LAPB_EPF;
154 			frame->control[0] = skb->data[0];
155 			frame->control[1] = skb->data[1];
156 			skb_pull(skb, 2);
157 		} else if ((skb->data[0] & LAPB_U) == 1) { 	/* S frame - take out PF/NR */
158 			frame->type       = skb->data[0] & 0x0F;
159 			frame->nr         = (skb->data[1] >> 1) & 0x7F;
160 			frame->pf         = skb->data[1] & LAPB_EPF;
161 			frame->control[0] = skb->data[0];
162 			frame->control[1] = skb->data[1];
163 			skb_pull(skb, 2);
164 		} else if ((skb->data[0] & LAPB_U) == 3) { 	/* U frame - take out PF */
165 			frame->type       = skb->data[0] & ~LAPB_SPF;
166 			frame->pf         = skb->data[0] & LAPB_SPF;
167 			frame->control[0] = skb->data[0];
168 			frame->control[1] = 0x00;
169 			skb_pull(skb, 1);
170 		}
171 	} else {
172 		if ((skb->data[0] & LAPB_S) == 0) {
173 			frame->type = LAPB_I;			/* I frame - carries NR/NS/PF */
174 			frame->ns   = (skb->data[0] >> 1) & 0x07;
175 			frame->nr   = (skb->data[0] >> 5) & 0x07;
176 			frame->pf   = skb->data[0] & LAPB_SPF;
177 		} else if ((skb->data[0] & LAPB_U) == 1) { 	/* S frame - take out PF/NR */
178 			frame->type = skb->data[0] & 0x0F;
179 			frame->nr   = (skb->data[0] >> 5) & 0x07;
180 			frame->pf   = skb->data[0] & LAPB_SPF;
181 		} else if ((skb->data[0] & LAPB_U) == 3) { 	/* U frame - take out PF */
182 			frame->type = skb->data[0] & ~LAPB_SPF;
183 			frame->pf   = skb->data[0] & LAPB_SPF;
184 		}
185 
186 		frame->control[0] = skb->data[0];
187 
188 		skb_pull(skb, 1);
189 	}
190 }
191 
192 /*
193  *	This routine is called when the HDLC layer internally  generates a
194  *	command or  response  for  the remote machine ( eg. RR, UA etc. ).
195  *	Only supervisory or unnumbered frames are processed, FRMRs are handled
196  *	by lapb_transmit_frmr below.
197  */
lapb_send_control(lapb_cb * lapb,int frametype,int poll_bit,int type)198 void lapb_send_control(lapb_cb *lapb, int frametype, int poll_bit, int type)
199 {
200 	struct sk_buff *skb;
201 	unsigned char  *dptr;
202 
203 	if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
204 		return;
205 
206 	skb_reserve(skb, LAPB_HEADER_LEN + 1);
207 
208 	if (lapb->mode & LAPB_EXTENDED) {
209 		if ((frametype & LAPB_U) == LAPB_U) {
210 			dptr   = skb_put(skb, 1);
211 			*dptr  = frametype;
212 			*dptr |= (poll_bit) ? LAPB_SPF : 0;
213 		} else {
214 			dptr     = skb_put(skb, 2);
215 			dptr[0]  = frametype;
216 			dptr[1]  = (lapb->vr << 1);
217 			dptr[1] |= (poll_bit) ? LAPB_EPF : 0;
218 		}
219 	} else {
220 		dptr   = skb_put(skb, 1);
221 		*dptr  = frametype;
222 		*dptr |= (poll_bit) ? LAPB_SPF : 0;
223 		if ((frametype & LAPB_U) == LAPB_S)		/* S frames carry NR */
224 			*dptr |= (lapb->vr << 5);
225 	}
226 
227 	lapb_transmit_buffer(lapb, skb, type);
228 }
229 
230 /*
231  *	This routine generates FRMRs based on information previously stored in
232  *	the LAPB control block.
233  */
lapb_transmit_frmr(lapb_cb * lapb)234 void lapb_transmit_frmr(lapb_cb *lapb)
235 {
236 	struct sk_buff *skb;
237 	unsigned char  *dptr;
238 
239 	if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
240 		return;
241 
242 	skb_reserve(skb, LAPB_HEADER_LEN + 1);
243 
244 	if (lapb->mode & LAPB_EXTENDED) {
245 		dptr    = skb_put(skb, 6);
246 		*dptr++ = LAPB_FRMR;
247 		*dptr++ = lapb->frmr_data.control[0];
248 		*dptr++ = lapb->frmr_data.control[1];
249 		*dptr++ = (lapb->vs << 1) & 0xFE;
250 		*dptr   = (lapb->vr << 1) & 0xFE;
251 		if (lapb->frmr_data.cr == LAPB_RESPONSE)
252 			*dptr |= 0x01;
253 		dptr++;
254 		*dptr++ = lapb->frmr_type;
255 
256 #if LAPB_DEBUG > 1
257 	printk(KERN_DEBUG "lapb: (%p) S%d TX FRMR %02X %02X %02X %02X %02X\n", lapb->token, lapb->state, skb->data[1], skb->data[2], skb->data[3], skb->data[4], skb->data[5]);
258 #endif
259 	} else {
260 		dptr    = skb_put(skb, 4);
261 		*dptr++ = LAPB_FRMR;
262 		*dptr++ = lapb->frmr_data.control[0];
263 		*dptr   = (lapb->vs << 1) & 0x0E;
264 		*dptr  |= (lapb->vr << 5) & 0xE0;
265 		if (lapb->frmr_data.cr == LAPB_RESPONSE)
266 			*dptr |= 0x10;
267 		dptr++;
268 		*dptr++ = lapb->frmr_type;
269 
270 #if LAPB_DEBUG > 1
271 	printk(KERN_DEBUG "lapb: (%p) S%d TX FRMR %02X %02X %02X\n", lapb->token, lapb->state, skb->data[1], skb->data[2], skb->data[3]);
272 #endif
273 	}
274 
275 	lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);
276 }
277