4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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.
13 * LAPB 001 Jonathan Naylor Started Coding
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/socket.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>
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
31 #include <linux/fcntl.h>
33 #include <linux/interrupt.h>
37 * This routine purges all the queues of frames.
39 void lapb_clear_queues(lapb_cb *lapb)
41 skb_queue_purge(&lapb->write_queue);
42 skb_queue_purge(&lapb->ack_queue);
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
50 void lapb_frames_acked(lapb_cb *lapb, unsigned short nr)
55 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
58 * Remove all the ack-ed frames from the ack queue.
61 while (skb_peek(&lapb->ack_queue) != NULL && lapb->va != nr) {
62 skb = skb_dequeue(&lapb->ack_queue);
64 lapb->va = (lapb->va + 1) % modulus;
69 void lapb_requeue_frames(lapb_cb *lapb)
71 struct sk_buff *skb, *skb_prev = NULL;
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.
78 while ((skb = skb_dequeue(&lapb->ack_queue)) != NULL) {
80 skb_queue_head(&lapb->write_queue, skb);
82 skb_append(skb_prev, skb);
88 * Validate that the value of nr is between va and vs. Return true or
91 int lapb_validate_nr(lapb_cb *lapb, unsigned short nr)
93 unsigned short vc = lapb->va;
96 modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS : LAPB_SMODULUS;
98 while (vc != lapb->vs) {
99 if (nr == vc) return 1;
100 vc = (vc + 1) % modulus;
103 if (nr == lapb->vs) return 1;
109 * This routine is the centralised routine for parsing the control
110 * information for the different frame formats.
112 void lapb_decode(lapb_cb *lapb, struct sk_buff *skb, struct lapb_frame *frame)
114 frame->type = LAPB_ILLEGAL;
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]);
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;
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;
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;
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;
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];
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];
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;
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;
186 frame->control[0] = skb->data[0];
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.
198 void lapb_send_control(lapb_cb *lapb, int frametype, int poll_bit, int type)
203 if ((skb = alloc_skb(LAPB_HEADER_LEN + 3, GFP_ATOMIC)) == NULL)
206 skb_reserve(skb, LAPB_HEADER_LEN + 1);
208 if (lapb->mode & LAPB_EXTENDED) {
209 if ((frametype & LAPB_U) == LAPB_U) {
210 dptr = skb_put(skb, 1);
212 *dptr |= (poll_bit) ? LAPB_SPF : 0;
214 dptr = skb_put(skb, 2);
216 dptr[1] = (lapb->vr << 1);
217 dptr[1] |= (poll_bit) ? LAPB_EPF : 0;
220 dptr = skb_put(skb, 1);
222 *dptr |= (poll_bit) ? LAPB_SPF : 0;
223 if ((frametype & LAPB_U) == LAPB_S) /* S frames carry NR */
224 *dptr |= (lapb->vr << 5);
227 lapb_transmit_buffer(lapb, skb, type);
231 * This routine generates FRMRs based on information previously stored in
232 * the LAPB control block.
234 void lapb_transmit_frmr(lapb_cb *lapb)
239 if ((skb = alloc_skb(LAPB_HEADER_LEN + 7, GFP_ATOMIC)) == NULL)
242 skb_reserve(skb, LAPB_HEADER_LEN + 1);
244 if (lapb->mode & LAPB_EXTENDED) {
245 dptr = skb_put(skb, 6);
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)
254 *dptr++ = lapb->frmr_type;
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]);
260 dptr = skb_put(skb, 4);
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)
268 *dptr++ = lapb->frmr_type;
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]);
275 lapb_transmit_buffer(lapb, skb, LAPB_RESPONSE);