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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.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>
31 #include <asm/uaccess.h>
32 #include <linux/fcntl.h>
34 #include <linux/interrupt.h>
38 * This routine purges all the queues of frames.
40 void lapb_clear_queues(struct lapb_cb
*lapb
)
42 skb_queue_purge(&lapb
->write_queue
);
43 skb_queue_purge(&lapb
->ack_queue
);
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
51 void lapb_frames_acked(struct lapb_cb
*lapb
, unsigned short nr
)
56 modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
: LAPB_SMODULUS
;
59 * Remove all the ack-ed frames from the ack queue.
62 while (skb_peek(&lapb
->ack_queue
) && lapb
->va
!= nr
) {
63 skb
= skb_dequeue(&lapb
->ack_queue
);
65 lapb
->va
= (lapb
->va
+ 1) % modulus
;
69 void lapb_requeue_frames(struct 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
, &lapb
->write_queue
);
88 * Validate that the value of nr is between va and vs. Return true or
91 int lapb_validate_nr(struct 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
) {
101 vc
= (vc
+ 1) % modulus
;
104 return nr
== lapb
->vs
;
108 * This routine is the centralised routine for parsing the control
109 * information for the different frame formats.
111 int lapb_decode(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
112 struct lapb_frame
*frame
)
114 frame
->type
= LAPB_ILLEGAL
;
116 lapb_dbg(2, "(%p) S%d RX %3ph\n", lapb
->dev
, lapb
->state
, skb
->data
);
118 /* We always need to look at 2 bytes, sometimes we need
119 * to look at 3 and those cases are handled below.
121 if (!pskb_may_pull(skb
, 2))
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
;
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
;
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
;
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
;
152 if (lapb
->mode
& LAPB_EXTENDED
) {
153 if (!(skb
->data
[0] & LAPB_S
)) {
154 if (!pskb_may_pull(skb
, 2))
157 * I frame - carries NR/NS/PF
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];
166 } else if ((skb
->data
[0] & LAPB_U
) == 1) {
167 if (!pskb_may_pull(skb
, 2))
170 * S frame - take out PF/NR
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];
178 } else if ((skb
->data
[0] & LAPB_U
) == 3) {
180 * U frame - take out PF
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;
189 if (!(skb
->data
[0] & LAPB_S
)) {
191 * I frame - carries NR/NS/PF
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) {
199 * S frame - take out PF/NR
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) {
206 * U frame - take out PF
208 frame
->type
= skb
->data
[0] & ~LAPB_SPF
;
209 frame
->pf
= skb
->data
[0] & LAPB_SPF
;
212 frame
->control
[0] = skb
->data
[0];
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.
226 void lapb_send_control(struct lapb_cb
*lapb
, int frametype
,
227 int poll_bit
, int type
)
232 if ((skb
= alloc_skb(LAPB_HEADER_LEN
+ 3, GFP_ATOMIC
)) == NULL
)
235 skb_reserve(skb
, LAPB_HEADER_LEN
+ 1);
237 if (lapb
->mode
& LAPB_EXTENDED
) {
238 if ((frametype
& LAPB_U
) == LAPB_U
) {
239 dptr
= skb_put(skb
, 1);
241 *dptr
|= poll_bit
? LAPB_SPF
: 0;
243 dptr
= skb_put(skb
, 2);
245 dptr
[1] = (lapb
->vr
<< 1);
246 dptr
[1] |= poll_bit
? LAPB_EPF
: 0;
249 dptr
= skb_put(skb
, 1);
251 *dptr
|= poll_bit
? LAPB_SPF
: 0;
252 if ((frametype
& LAPB_U
) == LAPB_S
) /* S frames carry NR */
253 *dptr
|= (lapb
->vr
<< 5);
256 lapb_transmit_buffer(lapb
, skb
, type
);
260 * This routine generates FRMRs based on information previously stored in
261 * the LAPB control block.
263 void lapb_transmit_frmr(struct lapb_cb
*lapb
)
268 if ((skb
= alloc_skb(LAPB_HEADER_LEN
+ 7, GFP_ATOMIC
)) == NULL
)
271 skb_reserve(skb
, LAPB_HEADER_LEN
+ 1);
273 if (lapb
->mode
& LAPB_EXTENDED
) {
274 dptr
= skb_put(skb
, 6);
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
)
283 *dptr
++ = lapb
->frmr_type
;
285 lapb_dbg(1, "(%p) S%d TX FRMR %5ph\n",
286 lapb
->dev
, lapb
->state
,
289 dptr
= skb_put(skb
, 4);
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
)
297 *dptr
++ = lapb
->frmr_type
;
299 lapb_dbg(1, "(%p) S%d TX FRMR %3ph\n",
300 lapb
->dev
, lapb
->state
, &skb
->data
[1]);
303 lapb_transmit_buffer(lapb
, skb
, LAPB_RESPONSE
);