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/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/netdevice.h>
30 #include <linux/skbuff.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/fcntl.h>
36 #include <linux/interrupt.h>
40 * State machine for state 0, Disconnected State.
41 * The handling of the timer(s) is in file lapb_timer.c.
43 static void lapb_state0_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
44 struct lapb_frame
*frame
)
46 switch (frame
->type
) {
49 printk(KERN_DEBUG
"lapb: (%p) S0 RX SABM(%d)\n",
50 lapb
->dev
, frame
->pf
);
52 if (lapb
->mode
& LAPB_EXTENDED
) {
54 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n",
55 lapb
->dev
, frame
->pf
);
57 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
61 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
62 lapb
->dev
, frame
->pf
);
65 printk(KERN_DEBUG
"lapb: (%p) S0 -> S3\n",
68 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
70 lapb_stop_t1timer(lapb
);
71 lapb_stop_t2timer(lapb
);
72 lapb
->state
= LAPB_STATE_3
;
73 lapb
->condition
= 0x00;
78 lapb_connect_indication(lapb
, LAPB_OK
);
84 printk(KERN_DEBUG
"lapb: (%p) S0 RX SABME(%d)\n",
85 lapb
->dev
, frame
->pf
);
87 if (lapb
->mode
& LAPB_EXTENDED
) {
89 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
90 lapb
->dev
, frame
->pf
);
93 printk(KERN_DEBUG
"lapb: (%p) S0 -> S3\n",
96 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
98 lapb_stop_t1timer(lapb
);
99 lapb_stop_t2timer(lapb
);
100 lapb
->state
= LAPB_STATE_3
;
101 lapb
->condition
= 0x00;
106 lapb_connect_indication(lapb
, LAPB_OK
);
109 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n",
110 lapb
->dev
, frame
->pf
);
112 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
119 printk(KERN_DEBUG
"lapb: (%p) S0 RX DISC(%d)\n",
120 lapb
->dev
, frame
->pf
);
121 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
122 lapb
->dev
, frame
->pf
);
124 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
136 * State machine for state 1, Awaiting Connection State.
137 * The handling of the timer(s) is in file lapb_timer.c.
139 static void lapb_state1_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
140 struct lapb_frame
*frame
)
142 switch (frame
->type
) {
145 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABM(%d)\n",
146 lapb
->dev
, frame
->pf
);
148 if (lapb
->mode
& LAPB_EXTENDED
) {
150 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
151 lapb
->dev
, frame
->pf
);
153 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
157 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n",
158 lapb
->dev
, frame
->pf
);
160 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
167 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABME(%d)\n",
168 lapb
->dev
, frame
->pf
);
170 if (lapb
->mode
& LAPB_EXTENDED
) {
172 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n",
173 lapb
->dev
, frame
->pf
);
175 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
179 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
180 lapb
->dev
, frame
->pf
);
182 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
189 printk(KERN_DEBUG
"lapb: (%p) S1 RX DISC(%d)\n",
190 lapb
->dev
, frame
->pf
);
191 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
192 lapb
->dev
, frame
->pf
);
194 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
200 printk(KERN_DEBUG
"lapb: (%p) S1 RX UA(%d)\n",
201 lapb
->dev
, frame
->pf
);
205 printk(KERN_DEBUG
"lapb: (%p) S1 -> S3\n",
208 lapb_stop_t1timer(lapb
);
209 lapb_stop_t2timer(lapb
);
210 lapb
->state
= LAPB_STATE_3
;
211 lapb
->condition
= 0x00;
216 lapb_connect_confirmation(lapb
, LAPB_OK
);
222 printk(KERN_DEBUG
"lapb: (%p) S1 RX DM(%d)\n",
223 lapb
->dev
, frame
->pf
);
227 printk(KERN_DEBUG
"lapb: (%p) S1 -> S0\n",
230 lapb_clear_queues(lapb
);
231 lapb
->state
= LAPB_STATE_0
;
232 lapb_start_t1timer(lapb
);
233 lapb_stop_t2timer(lapb
);
234 lapb_disconnect_indication(lapb
, LAPB_REFUSED
);
243 * State machine for state 2, Awaiting Release State.
244 * The handling of the timer(s) is in file lapb_timer.c
246 static void lapb_state2_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
247 struct lapb_frame
*frame
)
249 switch (frame
->type
) {
253 printk(KERN_DEBUG
"lapb: (%p) S2 RX {SABM,SABME}(%d)\n",
254 lapb
->dev
, frame
->pf
);
255 printk(KERN_DEBUG
"lapb: (%p) S2 TX DM(%d)\n",
256 lapb
->dev
, frame
->pf
);
258 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
264 printk(KERN_DEBUG
"lapb: (%p) S2 RX DISC(%d)\n",
265 lapb
->dev
, frame
->pf
);
266 printk(KERN_DEBUG
"lapb: (%p) S2 TX UA(%d)\n",
267 lapb
->dev
, frame
->pf
);
269 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
275 printk(KERN_DEBUG
"lapb: (%p) S2 RX UA(%d)\n",
276 lapb
->dev
, frame
->pf
);
280 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n",
283 lapb
->state
= LAPB_STATE_0
;
284 lapb_start_t1timer(lapb
);
285 lapb_stop_t2timer(lapb
);
286 lapb_disconnect_confirmation(lapb
, LAPB_OK
);
292 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n",
293 lapb
->dev
, frame
->pf
);
297 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n",
300 lapb
->state
= LAPB_STATE_0
;
301 lapb_start_t1timer(lapb
);
302 lapb_stop_t2timer(lapb
);
303 lapb_disconnect_confirmation(lapb
,
313 printk(KERN_DEBUG
"lapb: (%p) S2 RX {I,REJ,RNR,RR}"
314 "(%d)\n", lapb
->dev
, frame
->pf
);
315 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n",
316 lapb
->dev
, frame
->pf
);
319 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
328 * State machine for state 3, Connected State.
329 * The handling of the timer(s) is in file lapb_timer.c
331 static void lapb_state3_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
332 struct lapb_frame
*frame
)
335 int modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
:
338 switch (frame
->type
) {
341 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABM(%d)\n",
342 lapb
->dev
, frame
->pf
);
344 if (lapb
->mode
& LAPB_EXTENDED
) {
346 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n",
347 lapb
->dev
, frame
->pf
);
349 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
353 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n",
354 lapb
->dev
, frame
->pf
);
356 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
358 lapb_stop_t1timer(lapb
);
359 lapb_stop_t2timer(lapb
);
360 lapb
->condition
= 0x00;
365 lapb_requeue_frames(lapb
);
371 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABME(%d)\n",
372 lapb
->dev
, frame
->pf
);
374 if (lapb
->mode
& LAPB_EXTENDED
) {
376 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n",
377 lapb
->dev
, frame
->pf
);
379 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
381 lapb_stop_t1timer(lapb
);
382 lapb_stop_t2timer(lapb
);
383 lapb
->condition
= 0x00;
388 lapb_requeue_frames(lapb
);
391 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n",
392 lapb
->dev
, frame
->pf
);
394 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
401 printk(KERN_DEBUG
"lapb: (%p) S3 RX DISC(%d)\n",
402 lapb
->dev
, frame
->pf
);
405 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n",
408 lapb_clear_queues(lapb
);
409 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
411 lapb_start_t1timer(lapb
);
412 lapb_stop_t2timer(lapb
);
413 lapb
->state
= LAPB_STATE_0
;
414 lapb_disconnect_indication(lapb
, LAPB_OK
);
419 printk(KERN_DEBUG
"lapb: (%p) S3 RX DM(%d)\n",
420 lapb
->dev
, frame
->pf
);
423 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n",
426 lapb_clear_queues(lapb
);
427 lapb
->state
= LAPB_STATE_0
;
428 lapb_start_t1timer(lapb
);
429 lapb_stop_t2timer(lapb
);
430 lapb_disconnect_indication(lapb
, LAPB_NOTCONNECTED
);
435 printk(KERN_DEBUG
"lapb: (%p) S3 RX RNR(%d) R%d\n",
436 lapb
->dev
, frame
->pf
, frame
->nr
);
438 lapb
->condition
|= LAPB_PEER_RX_BUSY_CONDITION
;
439 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
440 if (lapb_validate_nr(lapb
, frame
->nr
)) {
441 lapb_check_iframes_acked(lapb
, frame
->nr
);
443 lapb
->frmr_data
= *frame
;
444 lapb
->frmr_type
= LAPB_FRMR_Z
;
445 lapb_transmit_frmr(lapb
);
447 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
450 lapb_start_t1timer(lapb
);
451 lapb_stop_t2timer(lapb
);
452 lapb
->state
= LAPB_STATE_4
;
459 printk(KERN_DEBUG
"lapb: (%p) S3 RX RR(%d) R%d\n",
460 lapb
->dev
, frame
->pf
, frame
->nr
);
462 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
463 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
464 if (lapb_validate_nr(lapb
, frame
->nr
)) {
465 lapb_check_iframes_acked(lapb
, frame
->nr
);
467 lapb
->frmr_data
= *frame
;
468 lapb
->frmr_type
= LAPB_FRMR_Z
;
469 lapb_transmit_frmr(lapb
);
471 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
474 lapb_start_t1timer(lapb
);
475 lapb_stop_t2timer(lapb
);
476 lapb
->state
= LAPB_STATE_4
;
483 printk(KERN_DEBUG
"lapb: (%p) S3 RX REJ(%d) R%d\n",
484 lapb
->dev
, frame
->pf
, frame
->nr
);
486 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
487 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
488 if (lapb_validate_nr(lapb
, frame
->nr
)) {
489 lapb_frames_acked(lapb
, frame
->nr
);
490 lapb_stop_t1timer(lapb
);
492 lapb_requeue_frames(lapb
);
494 lapb
->frmr_data
= *frame
;
495 lapb
->frmr_type
= LAPB_FRMR_Z
;
496 lapb_transmit_frmr(lapb
);
498 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
501 lapb_start_t1timer(lapb
);
502 lapb_stop_t2timer(lapb
);
503 lapb
->state
= LAPB_STATE_4
;
510 printk(KERN_DEBUG
"lapb: (%p) S3 RX I(%d) S%d R%d\n",
511 lapb
->dev
, frame
->pf
, frame
->ns
, frame
->nr
);
513 if (!lapb_validate_nr(lapb
, frame
->nr
)) {
514 lapb
->frmr_data
= *frame
;
515 lapb
->frmr_type
= LAPB_FRMR_Z
;
516 lapb_transmit_frmr(lapb
);
518 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n",
521 lapb_start_t1timer(lapb
);
522 lapb_stop_t2timer(lapb
);
523 lapb
->state
= LAPB_STATE_4
;
527 if (lapb
->condition
& LAPB_PEER_RX_BUSY_CONDITION
)
528 lapb_frames_acked(lapb
, frame
->nr
);
530 lapb_check_iframes_acked(lapb
, frame
->nr
);
532 if (frame
->ns
== lapb
->vr
) {
534 cn
= lapb_data_indication(lapb
, skb
);
537 * If upper layer has dropped the frame, we
538 * basically ignore any further protocol
539 * processing. This will cause the peer
540 * to re-transmit the frame later like
541 * a frame lost on the wire.
543 if (cn
== NET_RX_DROP
) {
545 "LAPB: rx congestion\n");
548 lapb
->vr
= (lapb
->vr
+ 1) % modulus
;
549 lapb
->condition
&= ~LAPB_REJECT_CONDITION
;
551 lapb_enquiry_response(lapb
);
553 if (!(lapb
->condition
&
554 LAPB_ACK_PENDING_CONDITION
)) {
555 lapb
->condition
|= LAPB_ACK_PENDING_CONDITION
;
556 lapb_start_t2timer(lapb
);
560 if (lapb
->condition
& LAPB_REJECT_CONDITION
) {
562 lapb_enquiry_response(lapb
);
566 "lapb: (%p) S3 TX REJ(%d) R%d\n",
567 lapb
->dev
, frame
->pf
, lapb
->vr
);
569 lapb
->condition
|= LAPB_REJECT_CONDITION
;
570 lapb_send_control(lapb
, LAPB_REJ
,
573 lapb
->condition
&= ~LAPB_ACK_PENDING_CONDITION
;
580 printk(KERN_DEBUG
"lapb: (%p) S3 RX FRMR(%d) %02X "
581 "%02X %02X %02X %02X\n", lapb
->dev
, frame
->pf
,
582 skb
->data
[0], skb
->data
[1], skb
->data
[2],
583 skb
->data
[3], skb
->data
[4]);
585 lapb_establish_data_link(lapb
);
587 printk(KERN_DEBUG
"lapb: (%p) S3 -> S1\n",
590 lapb_requeue_frames(lapb
);
591 lapb
->state
= LAPB_STATE_1
;
596 printk(KERN_DEBUG
"lapb: (%p) S3 RX ILLEGAL(%d)\n",
597 lapb
->dev
, frame
->pf
);
599 lapb
->frmr_data
= *frame
;
600 lapb
->frmr_type
= LAPB_FRMR_W
;
601 lapb_transmit_frmr(lapb
);
603 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
605 lapb_start_t1timer(lapb
);
606 lapb_stop_t2timer(lapb
);
607 lapb
->state
= LAPB_STATE_4
;
617 * State machine for state 4, Frame Reject State.
618 * The handling of the timer(s) is in file lapb_timer.c.
620 static void lapb_state4_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
621 struct lapb_frame
*frame
)
623 switch (frame
->type
) {
626 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABM(%d)\n",
627 lapb
->dev
, frame
->pf
);
629 if (lapb
->mode
& LAPB_EXTENDED
) {
631 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n",
632 lapb
->dev
, frame
->pf
);
634 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
638 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n",
639 lapb
->dev
, frame
->pf
);
642 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n",
645 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
647 lapb_stop_t1timer(lapb
);
648 lapb_stop_t2timer(lapb
);
649 lapb
->state
= LAPB_STATE_3
;
650 lapb
->condition
= 0x00;
655 lapb_connect_indication(lapb
, LAPB_OK
);
661 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABME(%d)\n",
662 lapb
->dev
, frame
->pf
);
664 if (lapb
->mode
& LAPB_EXTENDED
) {
666 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n",
667 lapb
->dev
, frame
->pf
);
670 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n",
673 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
675 lapb_stop_t1timer(lapb
);
676 lapb_stop_t2timer(lapb
);
677 lapb
->state
= LAPB_STATE_3
;
678 lapb
->condition
= 0x00;
683 lapb_connect_indication(lapb
, LAPB_OK
);
686 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n",
687 lapb
->dev
, frame
->pf
);
689 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
699 * Process an incoming LAPB frame
701 void lapb_data_input(struct lapb_cb
*lapb
, struct sk_buff
*skb
)
703 struct lapb_frame frame
;
705 if (lapb_decode(lapb
, skb
, &frame
) < 0) {
710 switch (lapb
->state
) {
712 lapb_state0_machine(lapb
, skb
, &frame
); break;
714 lapb_state1_machine(lapb
, skb
, &frame
); break;
716 lapb_state2_machine(lapb
, skb
, &frame
); break;
718 lapb_state3_machine(lapb
, skb
, &frame
); break;
720 lapb_state4_machine(lapb
, skb
, &frame
); break;