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/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>
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>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/fcntl.h>
35 #include <linux/interrupt.h>
39 * This routine purges all the queues of frames.
41 void lapb_clear_queues(lapb_cb
*lapb
)
45 while ((skb
= skb_dequeue(&lapb
->write_queue
)) != NULL
)
48 while ((skb
= skb_dequeue(&lapb
->ack_queue
)) != NULL
)
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
57 void lapb_frames_acked(lapb_cb
*lapb
, unsigned short nr
)
62 modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
: LAPB_SMODULUS
;
65 * Remove all the ack-ed frames from the ack queue.
68 while (skb_peek(&lapb
->ack_queue
) != NULL
&& lapb
->va
!= nr
) {
69 skb
= skb_dequeue(&lapb
->ack_queue
);
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
) {
87 skb_queue_head(&lapb
->write_queue
, skb
);
89 skb_append(skb_prev
, skb
);
95 * Validate that the value of nr is between va and vs. Return true or
98 int lapb_validate_nr(lapb_cb
*lapb
, unsigned short nr
)
100 unsigned short vc
= lapb
->va
;
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;
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
;
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]);
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
;
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
;
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
;
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
;
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];
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];
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;
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];
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
)
210 if ((skb
= alloc_skb(LAPB_HEADER_LEN
+ 3, GFP_ATOMIC
)) == NULL
)
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);
219 *dptr
|= (poll_bit
) ? LAPB_SPF
: 0;
221 dptr
= skb_put(skb
, 2);
223 dptr
[1] = (lapb
->vr
<< 1);
224 dptr
[1] |= (poll_bit
) ? LAPB_EPF
: 0;
227 dptr
= skb_put(skb
, 1);
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
)
246 if ((skb
= alloc_skb(LAPB_HEADER_LEN
+ 7, GFP_ATOMIC
)) == NULL
)
249 skb_reserve(skb
, LAPB_HEADER_LEN
+ 1);
251 if (lapb
->mode
& LAPB_EXTENDED
) {
252 dptr
= skb_put(skb
, 6);
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
)
261 *dptr
++ = lapb
->frmr_type
;
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]);
267 dptr
= skb_put(skb
, 4);
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
)
275 *dptr
++ = lapb
->frmr_type
;
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]);
282 lapb_transmit_buffer(lapb
, skb
, LAPB_RESPONSE
);