Including config.h once is enough ...
[linux-2.6/linux-mips.git] / net / lapb / lapb_subr.c
blob611eba6f18a28fe0ba301338e99bfbe2d286dbfb
1 /*
2 * LAPB release 002
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
12 * History
13 * LAPB 001 Jonathan Naylor Started Coding
16 #include <linux/config.h>
17 #if defined(CONFIG_LAPB) || defined(CONFIG_LAPB_MODULE)
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/sched.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/inet.h>
29 #include <linux/skbuff.h>
30 #include <net/sock.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/fcntl.h>
34 #include <linux/mm.h>
35 #include <linux/interrupt.h>
36 #include <net/lapb.h>
39 * This routine purges all the queues of frames.
41 void lapb_clear_queues(lapb_cb *lapb)
43 struct sk_buff *skb;
45 while ((skb = skb_dequeue(&lapb->write_queue)) != NULL)
46 kfree_skb(skb);
48 while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL)
49 kfree_skb(skb);
53 * This routine purges the input queue of those frames that have been
54 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
55 * SDL diagram.
57 void lapb_frames_acked(lapb_cb *lapb, unsigned short nr)
59 struct sk_buff *skb;
60 int modulus;
62 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
65 * Remove all the ack-ed frames from the ack queue.
67 if (lapb->va != nr) {
68 while (skb_peek(&lapb->ack_queue) != NULL && lapb->va != nr) {
69 skb = skb_dequeue(&lapb->ack_queue);
70 kfree_skb(skb);
71 lapb->va = (lapb->va + 1) % modulus;
76 void lapb_requeue_frames(lapb_cb *lapb)
78 struct sk_buff *skb, *skb_prev = NULL;
81 * Requeue all the un-ack-ed frames on the output queue to be picked
82 * up by lapb_kick called from the timer. This arrangement handles the
83 * possibility of an empty output queue.
85 while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL) {
86 if (skb_prev == NULL)
87 skb_queue_head(&lapb->write_queue, skb);
88 else
89 skb_append(skb_prev, skb);
90 skb_prev = skb;
95 * Validate that the value of nr is between va and vs. Return true or
96 * false for testing.
98 int lapb_validate_nr(lapb_cb *lapb, unsigned short nr)
100 unsigned short vc = lapb->va;
101 int modulus;
103 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
105 while (vc != lapb->vs) {
106 if (nr == vc) return 1;
107 vc = (vc + 1) % modulus;
110 if (nr == lapb->vs) return 1;
112 return 0;
116 * This routine is the centralised routine for parsing the control
117 * information for the different frame formats.
119 void lapb_decode(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
121 frame->type = LAPB_ILLEGAL;
123 #if LAPB_DEBUG > 2
124 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]);
125 #endif
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;
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;
153 skb_pull(skb, 1);
155 if (lapb->mode & LAPB_EXTENDED) {
156 if ((skb->data[0] & LAPB_S) == 0) {
157 frame->type = LAPB_I; /* I frame - carries NR/NS/PF */
158 frame->ns = (skb->data[0] >> 1) & 0x7F;
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) == 1) { /* S frame - take out PF/NR */
165 frame->type = skb->data[0] & 0x0F;
166 frame->nr = (skb->data[1] >> 1) & 0x7F;
167 frame->pf = skb->data[1] & LAPB_EPF;
168 frame->control[0] = skb->data[0];
169 frame->control[1] = skb->data[1];
170 skb_pull(skb, 2);
171 } else if ((skb->data[0] & LAPB_U) == 3) { /* U frame - take out PF */
172 frame->type = skb->data[0] & ~LAPB_SPF;
173 frame->pf = skb->data[0] & LAPB_SPF;
174 frame->control[0] = skb->data[0];
175 frame->control[1] = 0x00;
176 skb_pull(skb, 1);
178 } else {
179 if ((skb->data[0] & LAPB_S) == 0) {
180 frame->type = LAPB_I; /* I frame - carries NR/NS/PF */
181 frame->ns = (skb->data[0] >> 1) & 0x07;
182 frame->nr = (skb->data[0] >> 5) & 0x07;
183 frame->pf = skb->data[0] & LAPB_SPF;
184 } else if ((skb->data[0] & LAPB_U) == 1) { /* S frame - take out PF/NR */
185 frame->type = skb->data[0] & 0x0F;
186 frame->nr = (skb->data[0] >> 5) & 0x07;
187 frame->pf = skb->data[0] & LAPB_SPF;
188 } else if ((skb->data[0] & LAPB_U) == 3) { /* U frame - take out PF */
189 frame->type = skb->data[0] & ~LAPB_SPF;
190 frame->pf = skb->data[0] & LAPB_SPF;
193 frame->control[0] = skb->data[0];
195 skb_pull(skb, 1);
200 * This routine is called when the HDLC layer internally generates a
201 * command or response for the remote machine ( eg. RR, UA etc. ).
202 * Only supervisory or unnumbered frames are processed, FRMRs are handled
203 * by lapb_transmit_frmr below.
205 void lapb_send_control(lapb_cb *lapb, int frametype, int poll_bit, int type)
207 struct sk_buff *skb;
208 unsigned char *dptr;
210 if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
211 return;
213 skb_reserve(skb, LAPB_HEADER_LEN + 1);
215 if (lapb->mode & LAPB_EXTENDED) {
216 if ((frametype & LAPB_U) == LAPB_U) {
217 dptr = skb_put(skb, 1);
218 *dptr = frametype;
219 *dptr |= (poll_bit) ? LAPB_SPF : 0;
220 } else {
221 dptr = skb_put(skb, 2);
222 dptr[0] = frametype;
223 dptr[1] = (lapb->vr << 1);
224 dptr[1] |= (poll_bit) ? LAPB_EPF : 0;
226 } else {
227 dptr = skb_put(skb, 1);
228 *dptr = frametype;
229 *dptr |= (poll_bit) ? LAPB_SPF : 0;
230 if ((frametype & LAPB_U) == LAPB_S) /* S frames carry NR */
231 *dptr |= (lapb->vr << 5);
234 lapb_transmit_buffer(lapb, skb, type);
238 * This routine generates FRMRs based on information previously stored in
239 * the LAPB control block.
241 void lapb_transmit_frmr(lapb_cb *lapb)
243 struct sk_buff *skb;
244 unsigned char *dptr;
246 if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
247 return;
249 skb_reserve(skb, LAPB_HEADER_LEN + 1);
251 if (lapb->mode & LAPB_EXTENDED) {
252 dptr = skb_put(skb, 6);
253 *dptr++ = LAPB_FRMR;
254 *dptr++ = lapb->frmr_data.control[0];
255 *dptr++ = lapb->frmr_data.control[1];
256 *dptr++ = (lapb->vs << 1) & 0xFE;
257 *dptr = (lapb->vr << 1) & 0xFE;
258 if (lapb->frmr_data.cr == LAPB_RESPONSE)
259 *dptr |= 0x01;
260 dptr++;
261 *dptr++ = lapb->frmr_type;
263 #if LAPB_DEBUG > 1
264 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]);
265 #endif
266 } else {
267 dptr = skb_put(skb, 4);
268 *dptr++ = LAPB_FRMR;
269 *dptr++ = lapb->frmr_data.control[0];
270 *dptr = (lapb->vs << 1) & 0x0E;
271 *dptr |= (lapb->vr << 5) & 0xE0;
272 if (lapb->frmr_data.cr == LAPB_RESPONSE)
273 *dptr |= 0x10;
274 dptr++;
275 *dptr++ = lapb->frmr_type;
277 #if LAPB_DEBUG > 1
278 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]);
279 #endif
282 lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);
285 #endif