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 Naulor Started Coding
14 * LAPB 002 Jonathan Naylor New timer architecture.
15 * 2000-10-29 Henner Eisen lapb_data_indication() return status.
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/netdevice.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 * State machine for state 0, Disconnected State.
40 * The handling of the timer(s) is in file lapb_timer.c.
42 static void lapb_state0_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
43 struct lapb_frame
*frame
)
45 switch (frame
->type
) {
48 printk(KERN_DEBUG
"lapb: (%p) S0 RX SABM(%d)\n",
49 lapb
->dev
, frame
->pf
);
51 if (lapb
->mode
& LAPB_EXTENDED
) {
53 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n",
54 lapb
->dev
, frame
->pf
);
56 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
60 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
61 lapb
->dev
, frame
->pf
);
64 printk(KERN_DEBUG
"lapb: (%p) S0 -> S3\n",
67 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
69 lapb_stop_t1timer(lapb
);
70 lapb_stop_t2timer(lapb
);
71 lapb
->state
= LAPB_STATE_3
;
72 lapb
->condition
= 0x00;
77 lapb_connect_indication(lapb
, LAPB_OK
);
83 printk(KERN_DEBUG
"lapb: (%p) S0 RX SABME(%d)\n",
84 lapb
->dev
, frame
->pf
);
86 if (lapb
->mode
& LAPB_EXTENDED
) {
88 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
89 lapb
->dev
, frame
->pf
);
92 printk(KERN_DEBUG
"lapb: (%p) S0 -> S3\n",
95 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
97 lapb_stop_t1timer(lapb
);
98 lapb_stop_t2timer(lapb
);
99 lapb
->state
= LAPB_STATE_3
;
100 lapb
->condition
= 0x00;
105 lapb_connect_indication(lapb
, LAPB_OK
);
108 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n",
109 lapb
->dev
, frame
->pf
);
111 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
118 printk(KERN_DEBUG
"lapb: (%p) S0 RX DISC(%d)\n",
119 lapb
->dev
, frame
->pf
);
120 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
121 lapb
->dev
, frame
->pf
);
123 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
135 * State machine for state 1, Awaiting Connection State.
136 * The handling of the timer(s) is in file lapb_timer.c.
138 static void lapb_state1_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
139 struct lapb_frame
*frame
)
141 switch (frame
->type
) {
144 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABM(%d)\n",
145 lapb
->dev
, frame
->pf
);
147 if (lapb
->mode
& LAPB_EXTENDED
) {
149 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
150 lapb
->dev
, frame
->pf
);
152 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
156 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n",
157 lapb
->dev
, frame
->pf
);
159 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
166 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABME(%d)\n",
167 lapb
->dev
, frame
->pf
);
169 if (lapb
->mode
& LAPB_EXTENDED
) {
171 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n",
172 lapb
->dev
, frame
->pf
);
174 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
178 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
179 lapb
->dev
, frame
->pf
);
181 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
188 printk(KERN_DEBUG
"lapb: (%p) S1 RX DISC(%d)\n",
189 lapb
->dev
, frame
->pf
);
190 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
191 lapb
->dev
, frame
->pf
);
193 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
199 printk(KERN_DEBUG
"lapb: (%p) S1 RX UA(%d)\n",
200 lapb
->dev
, frame
->pf
);
204 printk(KERN_DEBUG
"lapb: (%p) S1 -> S3\n",
207 lapb_stop_t1timer(lapb
);
208 lapb_stop_t2timer(lapb
);
209 lapb
->state
= LAPB_STATE_3
;
210 lapb
->condition
= 0x00;
215 lapb_connect_confirmation(lapb
, LAPB_OK
);
221 printk(KERN_DEBUG
"lapb: (%p) S1 RX DM(%d)\n",
222 lapb
->dev
, frame
->pf
);
226 printk(KERN_DEBUG
"lapb: (%p) S1 -> S0\n",
229 lapb_clear_queues(lapb
);
230 lapb
->state
= LAPB_STATE_0
;
231 lapb_start_t1timer(lapb
);
232 lapb_stop_t2timer(lapb
);
233 lapb_disconnect_indication(lapb
, LAPB_REFUSED
);
242 * State machine for state 2, Awaiting Release State.
243 * The handling of the timer(s) is in file lapb_timer.c
245 static void lapb_state2_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
246 struct lapb_frame
*frame
)
248 switch (frame
->type
) {
252 printk(KERN_DEBUG
"lapb: (%p) S2 RX {SABM,SABME}(%d)\n",
253 lapb
->dev
, frame
->pf
);
254 printk(KERN_DEBUG
"lapb: (%p) S2 TX DM(%d)\n",
255 lapb
->dev
, frame
->pf
);
257 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
263 printk(KERN_DEBUG
"lapb: (%p) S2 RX DISC(%d)\n",
264 lapb
->dev
, frame
->pf
);
265 printk(KERN_DEBUG
"lapb: (%p) S2 TX UA(%d)\n",
266 lapb
->dev
, frame
->pf
);
268 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
274 printk(KERN_DEBUG
"lapb: (%p) S2 RX UA(%d)\n",
275 lapb
->dev
, frame
->pf
);
279 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n",
282 lapb
->state
= LAPB_STATE_0
;
283 lapb_start_t1timer(lapb
);
284 lapb_stop_t2timer(lapb
);
285 lapb_disconnect_confirmation(lapb
, LAPB_OK
);
291 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n",
292 lapb
->dev
, frame
->pf
);
296 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n",
299 lapb
->state
= LAPB_STATE_0
;
300 lapb_start_t1timer(lapb
);
301 lapb_stop_t2timer(lapb
);
302 lapb_disconnect_confirmation(lapb
,
312 printk(KERN_DEBUG
"lapb: (%p) S2 RX {I,REJ,RNR,RR}"
313 "(%d)\n", lapb
->dev
, frame
->pf
);
314 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n",
315 lapb
->dev
, frame
->pf
);
318 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
327 * State machine for state 3, Connected State.
328 * The handling of the timer(s) is in file lapb_timer.c
330 static void lapb_state3_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
331 struct lapb_frame
*frame
)
334 int modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
:
337 switch (frame
->type
) {
340 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABM(%d)\n",
341 lapb
->dev
, frame
->pf
);
343 if (lapb
->mode
& LAPB_EXTENDED
) {
345 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n",
346 lapb
->dev
, frame
->pf
);
348 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
352 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n",
353 lapb
->dev
, frame
->pf
);
355 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
357 lapb_stop_t1timer(lapb
);
358 lapb_stop_t2timer(lapb
);
359 lapb
->condition
= 0x00;
364 lapb_requeue_frames(lapb
);
370 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABME(%d)\n",
371 lapb
->dev
, frame
->pf
);
373 if (lapb
->mode
& LAPB_EXTENDED
) {
375 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n",
376 lapb
->dev
, frame
->pf
);
378 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
380 lapb_stop_t1timer(lapb
);
381 lapb_stop_t2timer(lapb
);
382 lapb
->condition
= 0x00;
387 lapb_requeue_frames(lapb
);
390 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n",
391 lapb
->dev
, frame
->pf
);
393 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
400 printk(KERN_DEBUG
"lapb: (%p) S3 RX DISC(%d)\n",
401 lapb
->dev
, frame
->pf
);
404 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n",
407 lapb_clear_queues(lapb
);
408 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
410 lapb_start_t1timer(lapb
);
411 lapb_stop_t2timer(lapb
);
412 lapb
->state
= LAPB_STATE_0
;
413 lapb_disconnect_indication(lapb
, LAPB_OK
);
418 printk(KERN_DEBUG
"lapb: (%p) S3 RX DM(%d)\n",
419 lapb
->dev
, frame
->pf
);
422 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n",
425 lapb_clear_queues(lapb
);
426 lapb
->state
= LAPB_STATE_0
;
427 lapb_start_t1timer(lapb
);
428 lapb_stop_t2timer(lapb
);
429 lapb_disconnect_indication(lapb
, LAPB_NOTCONNECTED
);
434 printk(KERN_DEBUG
"lapb: (%p) S3 RX RNR(%d) R%d\n",
435 lapb
->dev
, frame
->pf
, frame
->nr
);
437 lapb
->condition
|= LAPB_PEER_RX_BUSY_CONDITION
;
438 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
439 if (lapb_validate_nr(lapb
, frame
->nr
)) {
440 lapb_check_iframes_acked(lapb
, frame
->nr
);
442 lapb
->frmr_data
= *frame
;
443 lapb
->frmr_type
= LAPB_FRMR_Z
;
444 lapb_transmit_frmr(lapb
);
446 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
449 lapb_start_t1timer(lapb
);
450 lapb_stop_t2timer(lapb
);
451 lapb
->state
= LAPB_STATE_4
;
458 printk(KERN_DEBUG
"lapb: (%p) S3 RX RR(%d) R%d\n",
459 lapb
->dev
, frame
->pf
, frame
->nr
);
461 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
462 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
463 if (lapb_validate_nr(lapb
, frame
->nr
)) {
464 lapb_check_iframes_acked(lapb
, frame
->nr
);
466 lapb
->frmr_data
= *frame
;
467 lapb
->frmr_type
= LAPB_FRMR_Z
;
468 lapb_transmit_frmr(lapb
);
470 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
473 lapb_start_t1timer(lapb
);
474 lapb_stop_t2timer(lapb
);
475 lapb
->state
= LAPB_STATE_4
;
482 printk(KERN_DEBUG
"lapb: (%p) S3 RX REJ(%d) R%d\n",
483 lapb
->dev
, frame
->pf
, frame
->nr
);
485 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
486 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
487 if (lapb_validate_nr(lapb
, frame
->nr
)) {
488 lapb_frames_acked(lapb
, frame
->nr
);
489 lapb_stop_t1timer(lapb
);
491 lapb_requeue_frames(lapb
);
493 lapb
->frmr_data
= *frame
;
494 lapb
->frmr_type
= LAPB_FRMR_Z
;
495 lapb_transmit_frmr(lapb
);
497 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
500 lapb_start_t1timer(lapb
);
501 lapb_stop_t2timer(lapb
);
502 lapb
->state
= LAPB_STATE_4
;
509 printk(KERN_DEBUG
"lapb: (%p) S3 RX I(%d) S%d R%d\n",
510 lapb
->dev
, frame
->pf
, frame
->ns
, frame
->nr
);
512 if (!lapb_validate_nr(lapb
, frame
->nr
)) {
513 lapb
->frmr_data
= *frame
;
514 lapb
->frmr_type
= LAPB_FRMR_Z
;
515 lapb_transmit_frmr(lapb
);
517 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
520 lapb_start_t1timer(lapb
);
521 lapb_stop_t2timer(lapb
);
522 lapb
->state
= LAPB_STATE_4
;
526 if (lapb
->condition
& LAPB_PEER_RX_BUSY_CONDITION
)
527 lapb_frames_acked(lapb
, frame
->nr
);
529 lapb_check_iframes_acked(lapb
, frame
->nr
);
531 if (frame
->ns
== lapb
->vr
) {
533 cn
= lapb_data_indication(lapb
, skb
);
536 * If upper layer has dropped the frame, we
537 * basically ignore any further protocol
538 * processing. This will cause the peer
539 * to re-transmit the frame later like
540 * a frame lost on the wire.
542 if (cn
== NET_RX_DROP
) {
544 "LAPB: rx congestion\n");
547 lapb
->vr
= (lapb
->vr
+ 1) % modulus
;
548 lapb
->condition
&= ~LAPB_REJECT_CONDITION
;
550 lapb_enquiry_response(lapb
);
552 if (!(lapb
->condition
&
553 LAPB_ACK_PENDING_CONDITION
)) {
554 lapb
->condition
|= LAPB_ACK_PENDING_CONDITION
;
555 lapb_start_t2timer(lapb
);
559 if (lapb
->condition
& LAPB_REJECT_CONDITION
) {
561 lapb_enquiry_response(lapb
);
565 "lapb: (%p) S3 TX REJ(%d) R%d\n",
566 lapb
->dev
, frame
->pf
, lapb
->vr
);
568 lapb
->condition
|= LAPB_REJECT_CONDITION
;
569 lapb_send_control(lapb
, LAPB_REJ
,
572 lapb
->condition
&= ~LAPB_ACK_PENDING_CONDITION
;
579 printk(KERN_DEBUG
"lapb: (%p) S3 RX FRMR(%d) %02X "
580 "%02X %02X %02X %02X\n", lapb
->dev
, frame
->pf
,
581 skb
->data
[0], skb
->data
[1], skb
->data
[2],
582 skb
->data
[3], skb
->data
[4]);
584 lapb_establish_data_link(lapb
);
586 printk(KERN_DEBUG
"lapb: (%p) S3 -> S1\n",
589 lapb_requeue_frames(lapb
);
590 lapb
->state
= LAPB_STATE_1
;
595 printk(KERN_DEBUG
"lapb: (%p) S3 RX ILLEGAL(%d)\n",
596 lapb
->dev
, frame
->pf
);
598 lapb
->frmr_data
= *frame
;
599 lapb
->frmr_type
= LAPB_FRMR_W
;
600 lapb_transmit_frmr(lapb
);
602 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
604 lapb_start_t1timer(lapb
);
605 lapb_stop_t2timer(lapb
);
606 lapb
->state
= LAPB_STATE_4
;
616 * State machine for state 4, Frame Reject State.
617 * The handling of the timer(s) is in file lapb_timer.c.
619 static void lapb_state4_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
620 struct lapb_frame
*frame
)
622 switch (frame
->type
) {
625 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABM(%d)\n",
626 lapb
->dev
, frame
->pf
);
628 if (lapb
->mode
& LAPB_EXTENDED
) {
630 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n",
631 lapb
->dev
, frame
->pf
);
633 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
637 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n",
638 lapb
->dev
, frame
->pf
);
641 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n",
644 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
646 lapb_stop_t1timer(lapb
);
647 lapb_stop_t2timer(lapb
);
648 lapb
->state
= LAPB_STATE_3
;
649 lapb
->condition
= 0x00;
654 lapb_connect_indication(lapb
, LAPB_OK
);
660 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABME(%d)\n",
661 lapb
->dev
, frame
->pf
);
663 if (lapb
->mode
& LAPB_EXTENDED
) {
665 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n",
666 lapb
->dev
, frame
->pf
);
669 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n",
672 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
674 lapb_stop_t1timer(lapb
);
675 lapb_stop_t2timer(lapb
);
676 lapb
->state
= LAPB_STATE_3
;
677 lapb
->condition
= 0x00;
682 lapb_connect_indication(lapb
, LAPB_OK
);
685 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n",
686 lapb
->dev
, frame
->pf
);
688 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
698 * Process an incoming LAPB frame
700 void lapb_data_input(struct lapb_cb
*lapb
, struct sk_buff
*skb
)
702 struct lapb_frame frame
;
704 if (lapb_decode(lapb
, skb
, &frame
) < 0) {
709 switch (lapb
->state
) {
711 lapb_state0_machine(lapb
, skb
, &frame
); break;
713 lapb_state1_machine(lapb
, skb
, &frame
); break;
715 lapb_state2_machine(lapb
, skb
, &frame
); break;
717 lapb_state3_machine(lapb
, skb
, &frame
); break;
719 lapb_state4_machine(lapb
, skb
, &frame
); break;