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>
30 #include <linux/slab.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", lapb
->dev
);
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", lapb
->dev
);
94 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
96 lapb_stop_t1timer(lapb
);
97 lapb_stop_t2timer(lapb
);
98 lapb
->state
= LAPB_STATE_3
;
99 lapb
->condition
= 0x00;
104 lapb_connect_indication(lapb
, LAPB_OK
);
107 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n",
108 lapb
->dev
, frame
->pf
);
110 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
117 printk(KERN_DEBUG
"lapb: (%p) S0 RX DISC(%d)\n",
118 lapb
->dev
, frame
->pf
);
119 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n",
120 lapb
->dev
, frame
->pf
);
122 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
133 * State machine for state 1, Awaiting Connection State.
134 * The handling of the timer(s) is in file lapb_timer.c.
136 static void lapb_state1_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
137 struct lapb_frame
*frame
)
139 switch (frame
->type
) {
142 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABM(%d)\n",
143 lapb
->dev
, frame
->pf
);
145 if (lapb
->mode
& LAPB_EXTENDED
) {
147 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
148 lapb
->dev
, frame
->pf
);
150 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
154 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n",
155 lapb
->dev
, frame
->pf
);
157 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
164 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABME(%d)\n",
165 lapb
->dev
, frame
->pf
);
167 if (lapb
->mode
& LAPB_EXTENDED
) {
169 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n",
170 lapb
->dev
, frame
->pf
);
172 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
176 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
177 lapb
->dev
, frame
->pf
);
179 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
186 printk(KERN_DEBUG
"lapb: (%p) S1 RX DISC(%d)\n",
187 lapb
->dev
, frame
->pf
);
188 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n",
189 lapb
->dev
, frame
->pf
);
191 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
196 printk(KERN_DEBUG
"lapb: (%p) S1 RX UA(%d)\n",
197 lapb
->dev
, frame
->pf
);
201 printk(KERN_DEBUG
"lapb: (%p) S1 -> S3\n", lapb
->dev
);
203 lapb_stop_t1timer(lapb
);
204 lapb_stop_t2timer(lapb
);
205 lapb
->state
= LAPB_STATE_3
;
206 lapb
->condition
= 0x00;
211 lapb_connect_confirmation(lapb
, LAPB_OK
);
217 printk(KERN_DEBUG
"lapb: (%p) S1 RX DM(%d)\n",
218 lapb
->dev
, frame
->pf
);
222 printk(KERN_DEBUG
"lapb: (%p) S1 -> S0\n", lapb
->dev
);
224 lapb_clear_queues(lapb
);
225 lapb
->state
= LAPB_STATE_0
;
226 lapb_start_t1timer(lapb
);
227 lapb_stop_t2timer(lapb
);
228 lapb_disconnect_indication(lapb
, LAPB_REFUSED
);
237 * State machine for state 2, Awaiting Release State.
238 * The handling of the timer(s) is in file lapb_timer.c
240 static void lapb_state2_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
241 struct lapb_frame
*frame
)
243 switch (frame
->type
) {
247 printk(KERN_DEBUG
"lapb: (%p) S2 RX {SABM,SABME}(%d)\n",
248 lapb
->dev
, frame
->pf
);
249 printk(KERN_DEBUG
"lapb: (%p) S2 TX DM(%d)\n",
250 lapb
->dev
, frame
->pf
);
252 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
257 printk(KERN_DEBUG
"lapb: (%p) S2 RX DISC(%d)\n",
258 lapb
->dev
, frame
->pf
);
259 printk(KERN_DEBUG
"lapb: (%p) S2 TX UA(%d)\n",
260 lapb
->dev
, frame
->pf
);
262 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
267 printk(KERN_DEBUG
"lapb: (%p) S2 RX UA(%d)\n",
268 lapb
->dev
, frame
->pf
);
272 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n", lapb
->dev
);
274 lapb
->state
= LAPB_STATE_0
;
275 lapb_start_t1timer(lapb
);
276 lapb_stop_t2timer(lapb
);
277 lapb_disconnect_confirmation(lapb
, LAPB_OK
);
283 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n",
284 lapb
->dev
, frame
->pf
);
288 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n", lapb
->dev
);
290 lapb
->state
= LAPB_STATE_0
;
291 lapb_start_t1timer(lapb
);
292 lapb_stop_t2timer(lapb
);
293 lapb_disconnect_confirmation(lapb
, LAPB_NOTCONNECTED
);
302 printk(KERN_DEBUG
"lapb: (%p) S2 RX {I,REJ,RNR,RR}(%d)\n",
303 lapb
->dev
, frame
->pf
);
304 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n",
305 lapb
->dev
, frame
->pf
);
308 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
317 * State machine for state 3, Connected State.
318 * The handling of the timer(s) is in file lapb_timer.c
320 static void lapb_state3_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
321 struct lapb_frame
*frame
)
324 int modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
:
327 switch (frame
->type
) {
330 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABM(%d)\n",
331 lapb
->dev
, frame
->pf
);
333 if (lapb
->mode
& LAPB_EXTENDED
) {
335 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n",
336 lapb
->dev
, frame
->pf
);
338 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
342 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n",
343 lapb
->dev
, frame
->pf
);
345 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
347 lapb_stop_t1timer(lapb
);
348 lapb_stop_t2timer(lapb
);
349 lapb
->condition
= 0x00;
354 lapb_requeue_frames(lapb
);
360 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABME(%d)\n",
361 lapb
->dev
, frame
->pf
);
363 if (lapb
->mode
& LAPB_EXTENDED
) {
365 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n",
366 lapb
->dev
, frame
->pf
);
368 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
370 lapb_stop_t1timer(lapb
);
371 lapb_stop_t2timer(lapb
);
372 lapb
->condition
= 0x00;
377 lapb_requeue_frames(lapb
);
380 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n",
381 lapb
->dev
, frame
->pf
);
383 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
390 printk(KERN_DEBUG
"lapb: (%p) S3 RX DISC(%d)\n",
391 lapb
->dev
, frame
->pf
);
394 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n", lapb
->dev
);
396 lapb_clear_queues(lapb
);
397 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
398 lapb_start_t1timer(lapb
);
399 lapb_stop_t2timer(lapb
);
400 lapb
->state
= LAPB_STATE_0
;
401 lapb_disconnect_indication(lapb
, LAPB_OK
);
406 printk(KERN_DEBUG
"lapb: (%p) S3 RX DM(%d)\n",
407 lapb
->dev
, frame
->pf
);
410 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n", lapb
->dev
);
412 lapb_clear_queues(lapb
);
413 lapb
->state
= LAPB_STATE_0
;
414 lapb_start_t1timer(lapb
);
415 lapb_stop_t2timer(lapb
);
416 lapb_disconnect_indication(lapb
, LAPB_NOTCONNECTED
);
421 printk(KERN_DEBUG
"lapb: (%p) S3 RX RNR(%d) R%d\n",
422 lapb
->dev
, frame
->pf
, frame
->nr
);
424 lapb
->condition
|= LAPB_PEER_RX_BUSY_CONDITION
;
425 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
426 if (lapb_validate_nr(lapb
, frame
->nr
)) {
427 lapb_check_iframes_acked(lapb
, frame
->nr
);
429 lapb
->frmr_data
= *frame
;
430 lapb
->frmr_type
= LAPB_FRMR_Z
;
431 lapb_transmit_frmr(lapb
);
433 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
435 lapb_start_t1timer(lapb
);
436 lapb_stop_t2timer(lapb
);
437 lapb
->state
= LAPB_STATE_4
;
444 printk(KERN_DEBUG
"lapb: (%p) S3 RX RR(%d) R%d\n",
445 lapb
->dev
, frame
->pf
, frame
->nr
);
447 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
448 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
449 if (lapb_validate_nr(lapb
, frame
->nr
)) {
450 lapb_check_iframes_acked(lapb
, frame
->nr
);
452 lapb
->frmr_data
= *frame
;
453 lapb
->frmr_type
= LAPB_FRMR_Z
;
454 lapb_transmit_frmr(lapb
);
456 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
458 lapb_start_t1timer(lapb
);
459 lapb_stop_t2timer(lapb
);
460 lapb
->state
= LAPB_STATE_4
;
467 printk(KERN_DEBUG
"lapb: (%p) S3 RX REJ(%d) R%d\n",
468 lapb
->dev
, frame
->pf
, frame
->nr
);
470 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
471 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
472 if (lapb_validate_nr(lapb
, frame
->nr
)) {
473 lapb_frames_acked(lapb
, frame
->nr
);
474 lapb_stop_t1timer(lapb
);
476 lapb_requeue_frames(lapb
);
478 lapb
->frmr_data
= *frame
;
479 lapb
->frmr_type
= LAPB_FRMR_Z
;
480 lapb_transmit_frmr(lapb
);
482 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
484 lapb_start_t1timer(lapb
);
485 lapb_stop_t2timer(lapb
);
486 lapb
->state
= LAPB_STATE_4
;
493 printk(KERN_DEBUG
"lapb: (%p) S3 RX I(%d) S%d R%d\n",
494 lapb
->dev
, frame
->pf
, frame
->ns
, frame
->nr
);
496 if (!lapb_validate_nr(lapb
, frame
->nr
)) {
497 lapb
->frmr_data
= *frame
;
498 lapb
->frmr_type
= LAPB_FRMR_Z
;
499 lapb_transmit_frmr(lapb
);
501 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
503 lapb_start_t1timer(lapb
);
504 lapb_stop_t2timer(lapb
);
505 lapb
->state
= LAPB_STATE_4
;
509 if (lapb
->condition
& LAPB_PEER_RX_BUSY_CONDITION
)
510 lapb_frames_acked(lapb
, frame
->nr
);
512 lapb_check_iframes_acked(lapb
, frame
->nr
);
514 if (frame
->ns
== lapb
->vr
) {
516 cn
= lapb_data_indication(lapb
, skb
);
519 * If upper layer has dropped the frame, we
520 * basically ignore any further protocol
521 * processing. This will cause the peer
522 * to re-transmit the frame later like
523 * a frame lost on the wire.
525 if (cn
== NET_RX_DROP
) {
526 printk(KERN_DEBUG
"LAPB: rx congestion\n");
529 lapb
->vr
= (lapb
->vr
+ 1) % modulus
;
530 lapb
->condition
&= ~LAPB_REJECT_CONDITION
;
532 lapb_enquiry_response(lapb
);
534 if (!(lapb
->condition
&
535 LAPB_ACK_PENDING_CONDITION
)) {
536 lapb
->condition
|= LAPB_ACK_PENDING_CONDITION
;
537 lapb_start_t2timer(lapb
);
541 if (lapb
->condition
& LAPB_REJECT_CONDITION
) {
543 lapb_enquiry_response(lapb
);
547 "lapb: (%p) S3 TX REJ(%d) R%d\n",
548 lapb
->dev
, frame
->pf
, lapb
->vr
);
550 lapb
->condition
|= LAPB_REJECT_CONDITION
;
551 lapb_send_control(lapb
, LAPB_REJ
, frame
->pf
,
553 lapb
->condition
&= ~LAPB_ACK_PENDING_CONDITION
;
560 printk(KERN_DEBUG
"lapb: (%p) S3 RX FRMR(%d) %02X "
561 "%02X %02X %02X %02X\n", lapb
->dev
, frame
->pf
,
562 skb
->data
[0], skb
->data
[1], skb
->data
[2],
563 skb
->data
[3], skb
->data
[4]);
565 lapb_establish_data_link(lapb
);
567 printk(KERN_DEBUG
"lapb: (%p) S3 -> S1\n", lapb
->dev
);
569 lapb_requeue_frames(lapb
);
570 lapb
->state
= LAPB_STATE_1
;
575 printk(KERN_DEBUG
"lapb: (%p) S3 RX ILLEGAL(%d)\n",
576 lapb
->dev
, frame
->pf
);
578 lapb
->frmr_data
= *frame
;
579 lapb
->frmr_type
= LAPB_FRMR_W
;
580 lapb_transmit_frmr(lapb
);
582 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->dev
);
584 lapb_start_t1timer(lapb
);
585 lapb_stop_t2timer(lapb
);
586 lapb
->state
= LAPB_STATE_4
;
596 * State machine for state 4, Frame Reject State.
597 * The handling of the timer(s) is in file lapb_timer.c.
599 static void lapb_state4_machine(struct lapb_cb
*lapb
, struct sk_buff
*skb
,
600 struct lapb_frame
*frame
)
602 switch (frame
->type
) {
605 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABM(%d)\n",
606 lapb
->dev
, frame
->pf
);
608 if (lapb
->mode
& LAPB_EXTENDED
) {
610 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n",
611 lapb
->dev
, frame
->pf
);
613 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
617 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n",
618 lapb
->dev
, frame
->pf
);
621 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n", lapb
->dev
);
623 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
625 lapb_stop_t1timer(lapb
);
626 lapb_stop_t2timer(lapb
);
627 lapb
->state
= LAPB_STATE_3
;
628 lapb
->condition
= 0x00;
633 lapb_connect_indication(lapb
, LAPB_OK
);
639 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABME(%d)\n",
640 lapb
->dev
, frame
->pf
);
642 if (lapb
->mode
& LAPB_EXTENDED
) {
644 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n",
645 lapb
->dev
, frame
->pf
);
648 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n", lapb
->dev
);
650 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
,
652 lapb_stop_t1timer(lapb
);
653 lapb_stop_t2timer(lapb
);
654 lapb
->state
= LAPB_STATE_3
;
655 lapb
->condition
= 0x00;
660 lapb_connect_indication(lapb
, LAPB_OK
);
663 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n",
664 lapb
->dev
, frame
->pf
);
666 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
,
676 * Process an incoming LAPB frame
678 void lapb_data_input(struct lapb_cb
*lapb
, struct sk_buff
*skb
)
680 struct lapb_frame frame
;
682 if (lapb_decode(lapb
, skb
, &frame
) < 0) {
687 switch (lapb
->state
) {
689 lapb_state0_machine(lapb
, skb
, &frame
); break;
691 lapb_state1_machine(lapb
, skb
, &frame
); break;
693 lapb_state2_machine(lapb
, skb
, &frame
); break;
695 lapb_state3_machine(lapb
, skb
, &frame
); break;
697 lapb_state4_machine(lapb
, skb
, &frame
); break;