1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * This code REQUIRES 2.1.15 or higher/ NET3.038
8 * LAPB 001 Jonathan Naulor Started Coding
9 * LAPB 002 Jonathan Naylor New timer architecture.
10 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/socket.h>
19 #include <linux/kernel.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
24 #include <linux/inet.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/slab.h>
29 #include <linux/uaccess.h>
30 #include <linux/fcntl.h>
32 #include <linux/interrupt.h>
36 * State machine for state 0, Disconnected State.
37 * The handling of the timer(s) is in file lapb_timer.c.
39 static void lapb_state0_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
40 struct lapb_frame
*frame
)
42 switch (frame
->type
) {
44 lapb_dbg(1, "(%p) S0 RX SABM(%d)\n", lapb
->dev
, frame
->pf
);
45 if (lapb
->mode
& LAPB_EXTENDED
) {
46 lapb_dbg(1, "(%p) S0 TX DM(%d)\n",
47 lapb
->dev
, frame
->pf
);
48 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
51 lapb_dbg(1, "(%p) S0 TX UA(%d)\n",
52 lapb
->dev
, frame
->pf
);
53 lapb_dbg(0, "(%p) S0 -> S3\n", lapb
->dev
);
54 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
56 lapb_stop_t1timer(lapb
);
57 lapb_stop_t2timer(lapb
);
58 lapb
->state
= LAPB_STATE_3
;
59 lapb
->condition
= 0x00;
64 lapb_connect_indication(lapb
, LAPB_OK
);
69 lapb_dbg(1, "(%p) S0 RX SABME(%d)\n", lapb
->dev
, frame
->pf
);
70 if (lapb
->mode
& LAPB_EXTENDED
) {
71 lapb_dbg(1, "(%p) S0 TX UA(%d)\n",
72 lapb
->dev
, frame
->pf
);
73 lapb_dbg(0, "(%p) S0 -> S3\n", lapb
->dev
);
74 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
76 lapb_stop_t1timer(lapb
);
77 lapb_stop_t2timer(lapb
);
78 lapb
->state
= LAPB_STATE_3
;
79 lapb
->condition
= 0x00;
84 lapb_connect_indication(lapb
, LAPB_OK
);
86 lapb_dbg(1, "(%p) S0 TX DM(%d)\n",
87 lapb
->dev
, frame
->pf
);
88 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
94 lapb_dbg(1, "(%p) S0 RX DISC(%d)\n", lapb
->dev
, frame
->pf
);
95 lapb_dbg(1, "(%p) S0 TX UA(%d)\n", lapb
->dev
, frame
->pf
);
96 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
107 * State machine for state 1, Awaiting Connection State.
108 * The handling of the timer(s) is in file lapb_timer.c.
110 static void lapb_state1_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
111 struct lapb_frame
*frame
)
113 switch (frame
->type
) {
115 lapb_dbg(1, "(%p) S1 RX SABM(%d)\n", lapb
->dev
, frame
->pf
);
116 if (lapb
->mode
& LAPB_EXTENDED
) {
117 lapb_dbg(1, "(%p) S1 TX DM(%d)\n",
118 lapb
->dev
, frame
->pf
);
119 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
122 lapb_dbg(1, "(%p) S1 TX UA(%d)\n",
123 lapb
->dev
, frame
->pf
);
124 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
130 lapb_dbg(1, "(%p) S1 RX SABME(%d)\n", lapb
->dev
, frame
->pf
);
131 if (lapb
->mode
& LAPB_EXTENDED
) {
132 lapb_dbg(1, "(%p) S1 TX UA(%d)\n",
133 lapb
->dev
, frame
->pf
);
134 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
137 lapb_dbg(1, "(%p) S1 TX DM(%d)\n",
138 lapb
->dev
, frame
->pf
);
139 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
145 lapb_dbg(1, "(%p) S1 RX DISC(%d)\n", lapb
->dev
, frame
->pf
);
146 lapb_dbg(1, "(%p) S1 TX DM(%d)\n", lapb
->dev
, frame
->pf
);
147 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
151 lapb_dbg(1, "(%p) S1 RX UA(%d)\n", lapb
->dev
, frame
->pf
);
153 lapb_dbg(0, "(%p) S1 -> S3\n", lapb
->dev
);
154 lapb_stop_t1timer(lapb
);
155 lapb_stop_t2timer(lapb
);
156 lapb
->state
= LAPB_STATE_3
;
157 lapb
->condition
= 0x00;
162 lapb_connect_confirmation(lapb
, LAPB_OK
);
167 lapb_dbg(1, "(%p) S1 RX DM(%d)\n", lapb
->dev
, frame
->pf
);
169 lapb_dbg(0, "(%p) S1 -> S0\n", lapb
->dev
);
170 lapb_clear_queues(lapb
);
171 lapb
->state
= LAPB_STATE_0
;
172 lapb_start_t1timer(lapb
);
173 lapb_stop_t2timer(lapb
);
174 lapb_disconnect_indication(lapb
, LAPB_REFUSED
);
183 * State machine for state 2, Awaiting Release State.
184 * The handling of the timer(s) is in file lapb_timer.c
186 static void lapb_state2_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
187 struct lapb_frame
*frame
)
189 switch (frame
->type
) {
192 lapb_dbg(1, "(%p) S2 RX {SABM,SABME}(%d)\n",
193 lapb
->dev
, frame
->pf
);
194 lapb_dbg(1, "(%p) S2 TX DM(%d)\n", lapb
->dev
, frame
->pf
);
195 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
199 lapb_dbg(1, "(%p) S2 RX DISC(%d)\n", lapb
->dev
, frame
->pf
);
200 lapb_dbg(1, "(%p) S2 TX UA(%d)\n", lapb
->dev
, frame
->pf
);
201 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
205 lapb_dbg(1, "(%p) S2 RX UA(%d)\n", lapb
->dev
, frame
->pf
);
207 lapb_dbg(0, "(%p) S2 -> S0\n", lapb
->dev
);
208 lapb
->state
= LAPB_STATE_0
;
209 lapb_start_t1timer(lapb
);
210 lapb_stop_t2timer(lapb
);
211 lapb_disconnect_confirmation(lapb
, LAPB_OK
);
216 lapb_dbg(1, "(%p) S2 RX DM(%d)\n", lapb
->dev
, frame
->pf
);
218 lapb_dbg(0, "(%p) S2 -> S0\n", lapb
->dev
);
219 lapb
->state
= LAPB_STATE_0
;
220 lapb_start_t1timer(lapb
);
221 lapb_stop_t2timer(lapb
);
222 lapb_disconnect_confirmation(lapb
, LAPB_NOTCONNECTED
);
230 lapb_dbg(1, "(%p) S2 RX {I,REJ,RNR,RR}(%d)\n",
231 lapb
->dev
, frame
->pf
);
232 lapb_dbg(1, "(%p) S2 RX DM(%d)\n", lapb
->dev
, frame
->pf
);
234 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
243 * State machine for state 3, Connected State.
244 * The handling of the timer(s) is in file lapb_timer.c
246 static void lapb_state3_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
247 struct lapb_frame
*frame
)
250 int modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
:
253 switch (frame
->type
) {
255 lapb_dbg(1, "(%p) S3 RX SABM(%d)\n", lapb
->dev
, frame
->pf
);
256 if (lapb
->mode
& LAPB_EXTENDED
) {
257 lapb_dbg(1, "(%p) S3 TX DM(%d)\n",
258 lapb
->dev
, frame
->pf
);
259 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
262 lapb_dbg(1, "(%p) S3 TX UA(%d)\n",
263 lapb
->dev
, frame
->pf
);
264 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
266 lapb_stop_t1timer(lapb
);
267 lapb_stop_t2timer(lapb
);
268 lapb
->condition
= 0x00;
273 lapb_requeue_frames(lapb
);
278 lapb_dbg(1, "(%p) S3 RX SABME(%d)\n", lapb
->dev
, frame
->pf
);
279 if (lapb
->mode
& LAPB_EXTENDED
) {
280 lapb_dbg(1, "(%p) S3 TX UA(%d)\n",
281 lapb
->dev
, frame
->pf
);
282 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
284 lapb_stop_t1timer(lapb
);
285 lapb_stop_t2timer(lapb
);
286 lapb
->condition
= 0x00;
291 lapb_requeue_frames(lapb
);
293 lapb_dbg(1, "(%p) S3 TX DM(%d)\n",
294 lapb
->dev
, frame
->pf
);
295 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
301 lapb_dbg(1, "(%p) S3 RX DISC(%d)\n", lapb
->dev
, frame
->pf
);
302 lapb_dbg(0, "(%p) S3 -> S0\n", lapb
->dev
);
303 lapb_clear_queues(lapb
);
304 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
305 lapb_start_t1timer(lapb
);
306 lapb_stop_t2timer(lapb
);
307 lapb
->state
= LAPB_STATE_0
;
308 lapb_disconnect_indication(lapb
, LAPB_OK
);
312 lapb_dbg(1, "(%p) S3 RX DM(%d)\n", lapb
->dev
, frame
->pf
);
313 lapb_dbg(0, "(%p) S3 -> S0\n", lapb
->dev
);
314 lapb_clear_queues(lapb
);
315 lapb
->state
= LAPB_STATE_0
;
316 lapb_start_t1timer(lapb
);
317 lapb_stop_t2timer(lapb
);
318 lapb_disconnect_indication(lapb
, LAPB_NOTCONNECTED
);
322 lapb_dbg(1, "(%p) S3 RX RNR(%d) R%d\n",
323 lapb
->dev
, frame
->pf
, frame
->nr
);
324 lapb
->condition
|= LAPB_PEER_RX_BUSY_CONDITION
;
325 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
326 if (lapb_validate_nr(lapb
, frame
->nr
)) {
327 lapb_check_iframes_acked(lapb
, frame
->nr
);
329 lapb
->frmr_data
= *frame
;
330 lapb
->frmr_type
= LAPB_FRMR_Z
;
331 lapb_transmit_frmr(lapb
);
332 lapb_dbg(0, "(%p) S3 -> S4\n", lapb
->dev
);
333 lapb_start_t1timer(lapb
);
334 lapb_stop_t2timer(lapb
);
335 lapb
->state
= LAPB_STATE_4
;
341 lapb_dbg(1, "(%p) S3 RX RR(%d) R%d\n",
342 lapb
->dev
, frame
->pf
, frame
->nr
);
343 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
344 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
345 if (lapb_validate_nr(lapb
, frame
->nr
)) {
346 lapb_check_iframes_acked(lapb
, frame
->nr
);
348 lapb
->frmr_data
= *frame
;
349 lapb
->frmr_type
= LAPB_FRMR_Z
;
350 lapb_transmit_frmr(lapb
);
351 lapb_dbg(0, "(%p) S3 -> S4\n", lapb
->dev
);
352 lapb_start_t1timer(lapb
);
353 lapb_stop_t2timer(lapb
);
354 lapb
->state
= LAPB_STATE_4
;
360 lapb_dbg(1, "(%p) S3 RX REJ(%d) R%d\n",
361 lapb
->dev
, frame
->pf
, frame
->nr
);
362 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
363 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
364 if (lapb_validate_nr(lapb
, frame
->nr
)) {
365 lapb_frames_acked(lapb
, frame
->nr
);
366 lapb_stop_t1timer(lapb
);
368 lapb_requeue_frames(lapb
);
370 lapb
->frmr_data
= *frame
;
371 lapb
->frmr_type
= LAPB_FRMR_Z
;
372 lapb_transmit_frmr(lapb
);
373 lapb_dbg(0, "(%p) S3 -> S4\n", lapb
->dev
);
374 lapb_start_t1timer(lapb
);
375 lapb_stop_t2timer(lapb
);
376 lapb
->state
= LAPB_STATE_4
;
382 lapb_dbg(1, "(%p) S3 RX I(%d) S%d R%d\n",
383 lapb
->dev
, frame
->pf
, frame
->ns
, frame
->nr
);
384 if (!lapb_validate_nr(lapb
, frame
->nr
)) {
385 lapb
->frmr_data
= *frame
;
386 lapb
->frmr_type
= LAPB_FRMR_Z
;
387 lapb_transmit_frmr(lapb
);
388 lapb_dbg(0, "(%p) S3 -> S4\n", lapb
->dev
);
389 lapb_start_t1timer(lapb
);
390 lapb_stop_t2timer(lapb
);
391 lapb
->state
= LAPB_STATE_4
;
395 if (lapb
->condition
& LAPB_PEER_RX_BUSY_CONDITION
)
396 lapb_frames_acked(lapb
, frame
->nr
);
398 lapb_check_iframes_acked(lapb
, frame
->nr
);
400 if (frame
->ns
== lapb
->vr
) {
402 cn
= lapb_data_indication(lapb
, skb
);
405 * If upper layer has dropped the frame, we
406 * basically ignore any further protocol
407 * processing. This will cause the peer
408 * to re-transmit the frame later like
409 * a frame lost on the wire.
411 if (cn
== NET_RX_DROP
) {
412 pr_debug("rx congestion\n");
415 lapb
->vr
= (lapb
->vr
+ 1) % modulus
;
416 lapb
->condition
&= ~LAPB_REJECT_CONDITION
;
418 lapb_enquiry_response(lapb
);
420 if (!(lapb
->condition
&
421 LAPB_ACK_PENDING_CONDITION
)) {
422 lapb
->condition
|= LAPB_ACK_PENDING_CONDITION
;
423 lapb_start_t2timer(lapb
);
427 if (lapb
->condition
& LAPB_REJECT_CONDITION
) {
429 lapb_enquiry_response(lapb
);
431 lapb_dbg(1, "(%p) S3 TX REJ(%d) R%d\n",
432 lapb
->dev
, frame
->pf
, lapb
->vr
);
433 lapb
->condition
|= LAPB_REJECT_CONDITION
;
434 lapb_send_control(lapb
, LAPB_REJ
, frame
->pf
,
436 lapb
->condition
&= ~LAPB_ACK_PENDING_CONDITION
;
442 lapb_dbg(1, "(%p) S3 RX FRMR(%d) %5ph\n",
443 lapb
->dev
, frame
->pf
,
445 lapb_establish_data_link(lapb
);
446 lapb_dbg(0, "(%p) S3 -> S1\n", lapb
->dev
);
447 lapb_requeue_frames(lapb
);
448 lapb
->state
= LAPB_STATE_1
;
452 lapb_dbg(1, "(%p) S3 RX ILLEGAL(%d)\n", lapb
->dev
, frame
->pf
);
453 lapb
->frmr_data
= *frame
;
454 lapb
->frmr_type
= LAPB_FRMR_W
;
455 lapb_transmit_frmr(lapb
);
456 lapb_dbg(0, "(%p) S3 -> S4\n", lapb
->dev
);
457 lapb_start_t1timer(lapb
);
458 lapb_stop_t2timer(lapb
);
459 lapb
->state
= LAPB_STATE_4
;
469 * State machine for state 4, Frame Reject State.
470 * The handling of the timer(s) is in file lapb_timer.c.
472 static void lapb_state4_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
473 struct lapb_frame
*frame
)
475 switch (frame
->type
) {
477 lapb_dbg(1, "(%p) S4 RX SABM(%d)\n", lapb
->dev
, frame
->pf
);
478 if (lapb
->mode
& LAPB_EXTENDED
) {
479 lapb_dbg(1, "(%p) S4 TX DM(%d)\n",
480 lapb
->dev
, frame
->pf
);
481 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
484 lapb_dbg(1, "(%p) S4 TX UA(%d)\n",
485 lapb
->dev
, frame
->pf
);
486 lapb_dbg(0, "(%p) S4 -> S3\n", lapb
->dev
);
487 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
489 lapb_stop_t1timer(lapb
);
490 lapb_stop_t2timer(lapb
);
491 lapb
->state
= LAPB_STATE_3
;
492 lapb
->condition
= 0x00;
497 lapb_connect_indication(lapb
, LAPB_OK
);
502 lapb_dbg(1, "(%p) S4 RX SABME(%d)\n", lapb
->dev
, frame
->pf
);
503 if (lapb
->mode
& LAPB_EXTENDED
) {
504 lapb_dbg(1, "(%p) S4 TX UA(%d)\n",
505 lapb
->dev
, frame
->pf
);
506 lapb_dbg(0, "(%p) S4 -> S3\n", lapb
->dev
);
507 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
509 lapb_stop_t1timer(lapb
);
510 lapb_stop_t2timer(lapb
);
511 lapb
->state
= LAPB_STATE_3
;
512 lapb
->condition
= 0x00;
517 lapb_connect_indication(lapb
, LAPB_OK
);
519 lapb_dbg(1, "(%p) S4 TX DM(%d)\n",
520 lapb
->dev
, frame
->pf
);
521 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
531 * Process an incoming LAPB frame
533 void lapb_data_input(struct lapb_cb
*lapb
, struct sk_buff
*skb
)
535 struct lapb_frame frame
;
537 if (lapb_decode(lapb
, skb
, &frame
) < 0) {
542 switch (lapb
->state
) {
544 lapb_state0_machine(lapb
, skb
, &frame
); break;
546 lapb_state1_machine(lapb
, skb
, &frame
); break;
548 lapb_state2_machine(lapb
, skb
, &frame
); break;
550 lapb_state3_machine(lapb
, skb
, &frame
); break;
552 lapb_state4_machine(lapb
, skb
, &frame
); break;