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.
17 #include <linux/config.h>
18 #if defined(CONFIG_LAPB) || defined(CONFIG_LAPB_MODULE)
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
41 * State machine for state 0, Disconnected State.
42 * The handling of the timer(s) is in file lapb_timer.c.
44 static void lapb_state0_machine(lapb_cb
*lapb
, struct sk_buff
*skb
, struct lapb_frame
*frame
)
46 switch (frame
->type
) {
49 printk(KERN_DEBUG
"lapb: (%p) S0 RX SABM(%d)\n", lapb
->token
, frame
->pf
);
51 if (lapb
->mode
& LAPB_EXTENDED
) {
53 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n", lapb
->token
, frame
->pf
);
55 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
58 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n", lapb
->token
, frame
->pf
);
61 printk(KERN_DEBUG
"lapb: (%p) S0 -> S3\n", lapb
->token
);
63 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
64 lapb_stop_t1timer(lapb
);
65 lapb_stop_t2timer(lapb
);
66 lapb
->state
= LAPB_STATE_3
;
67 lapb
->condition
= 0x00;
72 lapb_connect_indication(lapb
, LAPB_OK
);
78 printk(KERN_DEBUG
"lapb: (%p) S0 RX SABME(%d)\n", lapb
->token
, frame
->pf
);
80 if (lapb
->mode
& LAPB_EXTENDED
) {
82 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n", lapb
->token
, frame
->pf
);
85 printk(KERN_DEBUG
"lapb: (%p) S0 -> S3\n", lapb
->token
);
87 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
88 lapb_stop_t1timer(lapb
);
89 lapb_stop_t2timer(lapb
);
90 lapb
->state
= LAPB_STATE_3
;
91 lapb
->condition
= 0x00;
96 lapb_connect_indication(lapb
, LAPB_OK
);
99 printk(KERN_DEBUG
"lapb: (%p) S0 TX DM(%d)\n", lapb
->token
, frame
->pf
);
101 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
107 printk(KERN_DEBUG
"lapb: (%p) S0 RX DISC(%d)\n", lapb
->token
, frame
->pf
);
108 printk(KERN_DEBUG
"lapb: (%p) S0 TX UA(%d)\n", lapb
->token
, frame
->pf
);
110 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
121 * State machine for state 1, Awaiting Connection State.
122 * The handling of the timer(s) is in file lapb_timer.c.
124 static void lapb_state1_machine(lapb_cb
*lapb
, struct sk_buff
*skb
, struct lapb_frame
*frame
)
126 switch (frame
->type
) {
129 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABM(%d)\n", lapb
->token
, frame
->pf
);
131 if (lapb
->mode
& LAPB_EXTENDED
) {
133 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n", lapb
->token
, frame
->pf
);
135 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
138 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n", lapb
->token
, frame
->pf
);
140 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
146 printk(KERN_DEBUG
"lapb: (%p) S1 RX SABME(%d)\n", lapb
->token
, frame
->pf
);
148 if (lapb
->mode
& LAPB_EXTENDED
) {
150 printk(KERN_DEBUG
"lapb: (%p) S1 TX UA(%d)\n", lapb
->token
, frame
->pf
);
152 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
155 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n", lapb
->token
, frame
->pf
);
157 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
163 printk(KERN_DEBUG
"lapb: (%p) S1 RX DISC(%d)\n", lapb
->token
, frame
->pf
);
164 printk(KERN_DEBUG
"lapb: (%p) S1 TX DM(%d)\n", lapb
->token
, frame
->pf
);
166 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
171 printk(KERN_DEBUG
"lapb: (%p) S1 RX UA(%d)\n", lapb
->token
, frame
->pf
);
175 printk(KERN_DEBUG
"lapb: (%p) S1 -> S3\n", lapb
->token
);
177 lapb_stop_t1timer(lapb
);
178 lapb_stop_t2timer(lapb
);
179 lapb
->state
= LAPB_STATE_3
;
180 lapb
->condition
= 0x00;
185 lapb_connect_confirmation(lapb
, LAPB_OK
);
191 printk(KERN_DEBUG
"lapb: (%p) S1 RX DM(%d)\n", lapb
->token
, frame
->pf
);
195 printk(KERN_DEBUG
"lapb: (%p) S1 -> S0\n", lapb
->token
);
197 lapb_clear_queues(lapb
);
198 lapb
->state
= LAPB_STATE_0
;
199 lapb_start_t1timer(lapb
);
200 lapb_stop_t2timer(lapb
);
201 lapb_disconnect_indication(lapb
, LAPB_REFUSED
);
213 * State machine for state 2, Awaiting Release State.
214 * The handling of the timer(s) is in file lapb_timer.c
216 static void lapb_state2_machine(lapb_cb
*lapb
, struct sk_buff
*skb
, struct lapb_frame
*frame
)
218 switch (frame
->type
) {
222 printk(KERN_DEBUG
"lapb: (%p) S2 RX {SABM,SABME}(%d)\n", lapb
->token
, frame
->pf
);
223 printk(KERN_DEBUG
"lapb: (%p) S2 TX DM(%d)\n", lapb
->token
, frame
->pf
);
225 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
230 printk(KERN_DEBUG
"lapb: (%p) S2 RX DISC(%d)\n", lapb
->token
, frame
->pf
);
231 printk(KERN_DEBUG
"lapb: (%p) S2 TX UA(%d)\n", lapb
->token
, frame
->pf
);
233 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
238 printk(KERN_DEBUG
"lapb: (%p) S2 RX UA(%d)\n", lapb
->token
, frame
->pf
);
242 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n", lapb
->token
);
244 lapb
->state
= LAPB_STATE_0
;
245 lapb_start_t1timer(lapb
);
246 lapb_stop_t2timer(lapb
);
247 lapb_disconnect_confirmation(lapb
, LAPB_OK
);
253 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n", lapb
->token
, frame
->pf
);
257 printk(KERN_DEBUG
"lapb: (%p) S2 -> S0\n", lapb
->token
);
259 lapb
->state
= LAPB_STATE_0
;
260 lapb_start_t1timer(lapb
);
261 lapb_stop_t2timer(lapb
);
262 lapb_disconnect_confirmation(lapb
, LAPB_NOTCONNECTED
);
271 printk(KERN_DEBUG
"lapb: (%p) S2 RX {I,REJ,RNR,RR}(%d)\n", lapb
->token
, frame
->pf
);
272 printk(KERN_DEBUG
"lapb: (%p) S2 RX DM(%d)\n", lapb
->token
, frame
->pf
);
274 if (frame
->pf
) lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
285 * State machine for state 3, Connected State.
286 * The handling of the timer(s) is in file lapb_timer.c
288 static void lapb_state3_machine(lapb_cb
*lapb
, struct sk_buff
*skb
, struct lapb_frame
*frame
)
293 modulus
= (lapb
->mode
& LAPB_EXTENDED
) ? LAPB_EMODULUS
: LAPB_SMODULUS
;
295 switch (frame
->type
) {
298 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABM(%d)\n", lapb
->token
, frame
->pf
);
300 if (lapb
->mode
& LAPB_EXTENDED
) {
302 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n", lapb
->token
, frame
->pf
);
304 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
307 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n", lapb
->token
, frame
->pf
);
309 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
310 lapb_stop_t1timer(lapb
);
311 lapb_stop_t2timer(lapb
);
312 lapb
->condition
= 0x00;
317 lapb_requeue_frames(lapb
);
323 printk(KERN_DEBUG
"lapb: (%p) S3 RX SABME(%d)\n", lapb
->token
, frame
->pf
);
325 if (lapb
->mode
& LAPB_EXTENDED
) {
327 printk(KERN_DEBUG
"lapb: (%p) S3 TX UA(%d)\n", lapb
->token
, frame
->pf
);
329 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
330 lapb_stop_t1timer(lapb
);
331 lapb_stop_t2timer(lapb
);
332 lapb
->condition
= 0x00;
337 lapb_requeue_frames(lapb
);
340 printk(KERN_DEBUG
"lapb: (%p) S3 TX DM(%d)\n", lapb
->token
, frame
->pf
);
342 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
348 printk(KERN_DEBUG
"lapb: (%p) S3 RX DISC(%d)\n", lapb
->token
, frame
->pf
);
351 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n", lapb
->token
);
353 lapb_clear_queues(lapb
);
354 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
355 lapb_start_t1timer(lapb
);
356 lapb_stop_t2timer(lapb
);
357 lapb
->state
= LAPB_STATE_0
;
358 lapb_disconnect_indication(lapb
, LAPB_OK
);
363 printk(KERN_DEBUG
"lapb: (%p) S3 RX DM(%d)\n", lapb
->token
, frame
->pf
);
366 printk(KERN_DEBUG
"lapb: (%p) S3 -> S0\n", lapb
->token
);
368 lapb_clear_queues(lapb
);
369 lapb
->state
= LAPB_STATE_0
;
370 lapb_start_t1timer(lapb
);
371 lapb_stop_t2timer(lapb
);
372 lapb_disconnect_indication(lapb
, LAPB_NOTCONNECTED
);
377 printk(KERN_DEBUG
"lapb: (%p) S3 RX RNR(%d) R%d\n", lapb
->token
, frame
->pf
, frame
->nr
);
379 lapb
->condition
|= LAPB_PEER_RX_BUSY_CONDITION
;
380 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
381 if (lapb_validate_nr(lapb
, frame
->nr
)) {
382 lapb_check_iframes_acked(lapb
, frame
->nr
);
384 lapb
->frmr_data
= *frame
;
385 lapb
->frmr_type
= LAPB_FRMR_Z
;
386 lapb_transmit_frmr(lapb
);
388 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->token
);
390 lapb_start_t1timer(lapb
);
391 lapb_stop_t2timer(lapb
);
392 lapb
->state
= LAPB_STATE_4
;
399 printk(KERN_DEBUG
"lapb: (%p) S3 RX RR(%d) R%d\n", lapb
->token
, frame
->pf
, frame
->nr
);
401 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
402 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
403 if (lapb_validate_nr(lapb
, frame
->nr
)) {
404 lapb_check_iframes_acked(lapb
, frame
->nr
);
406 lapb
->frmr_data
= *frame
;
407 lapb
->frmr_type
= LAPB_FRMR_Z
;
408 lapb_transmit_frmr(lapb
);
410 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->token
);
412 lapb_start_t1timer(lapb
);
413 lapb_stop_t2timer(lapb
);
414 lapb
->state
= LAPB_STATE_4
;
421 printk(KERN_DEBUG
"lapb: (%p) S3 RX REJ(%d) R%d\n", lapb
->token
, frame
->pf
, frame
->nr
);
423 lapb
->condition
&= ~LAPB_PEER_RX_BUSY_CONDITION
;
424 lapb_check_need_response(lapb
, frame
->cr
, frame
->pf
);
425 if (lapb_validate_nr(lapb
, frame
->nr
)) {
426 lapb_frames_acked(lapb
, frame
->nr
);
427 lapb_stop_t1timer(lapb
);
429 lapb_requeue_frames(lapb
);
431 lapb
->frmr_data
= *frame
;
432 lapb
->frmr_type
= LAPB_FRMR_Z
;
433 lapb_transmit_frmr(lapb
);
435 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->token
);
437 lapb_start_t1timer(lapb
);
438 lapb_stop_t2timer(lapb
);
439 lapb
->state
= LAPB_STATE_4
;
446 printk(KERN_DEBUG
"lapb: (%p) S3 RX I(%d) S%d R%d\n", lapb
->token
, frame
->pf
, frame
->ns
, frame
->nr
);
448 if (!lapb_validate_nr(lapb
, frame
->nr
)) {
449 lapb
->frmr_data
= *frame
;
450 lapb
->frmr_type
= LAPB_FRMR_Z
;
451 lapb_transmit_frmr(lapb
);
453 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->token
);
455 lapb_start_t1timer(lapb
);
456 lapb_stop_t2timer(lapb
);
457 lapb
->state
= LAPB_STATE_4
;
461 if (lapb
->condition
& LAPB_PEER_RX_BUSY_CONDITION
) {
462 lapb_frames_acked(lapb
, frame
->nr
);
464 lapb_check_iframes_acked(lapb
, frame
->nr
);
466 if (frame
->ns
== lapb
->vr
) {
467 lapb
->vr
= (lapb
->vr
+ 1) % modulus
;
468 queued
= lapb_data_indication(lapb
, skb
);
469 lapb
->condition
&= ~LAPB_REJECT_CONDITION
;
471 lapb_enquiry_response(lapb
);
473 if (!(lapb
->condition
& LAPB_ACK_PENDING_CONDITION
)) {
474 lapb
->condition
|= LAPB_ACK_PENDING_CONDITION
;
475 lapb_start_t2timer(lapb
);
479 if (lapb
->condition
& LAPB_REJECT_CONDITION
) {
481 lapb_enquiry_response(lapb
);
484 printk(KERN_DEBUG
"lapb: (%p) S3 TX REJ(%d) R%d\n", lapb
->token
, frame
->pf
, lapb
->vr
);
486 lapb
->condition
|= LAPB_REJECT_CONDITION
;
487 lapb_send_control(lapb
, LAPB_REJ
, frame
->pf
, LAPB_RESPONSE
);
488 lapb
->condition
&= ~LAPB_ACK_PENDING_CONDITION
;
495 printk(KERN_DEBUG
"lapb: (%p) S3 RX FRMR(%d) %02X %02X %02X %02X %02X\n", lapb
->token
, frame
->pf
, skb
->data
[0], skb
->data
[1], skb
->data
[2], skb
->data
[3], skb
->data
[4]);
497 lapb_establish_data_link(lapb
);
499 printk(KERN_DEBUG
"lapb: (%p) S3 -> S1\n", lapb
->token
);
501 lapb_requeue_frames(lapb
);
502 lapb
->state
= LAPB_STATE_1
;
507 printk(KERN_DEBUG
"lapb: (%p) S3 RX ILLEGAL(%d)\n", lapb
->token
, frame
->pf
);
509 lapb
->frmr_data
= *frame
;
510 lapb
->frmr_type
= LAPB_FRMR_W
;
511 lapb_transmit_frmr(lapb
);
513 printk(KERN_DEBUG
"lapb: (%p) S3 -> S4\n", lapb
->token
);
515 lapb_start_t1timer(lapb
);
516 lapb_stop_t2timer(lapb
);
517 lapb
->state
= LAPB_STATE_4
;
530 * State machine for state 4, Frame Reject State.
531 * The handling of the timer(s) is in file lapb_timer.c.
533 static void lapb_state4_machine(lapb_cb
*lapb
, struct sk_buff
*skb
, struct lapb_frame
*frame
)
535 switch (frame
->type
) {
538 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABM(%d)\n", lapb
->token
, frame
->pf
);
540 if (lapb
->mode
& LAPB_EXTENDED
) {
542 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n", lapb
->token
, frame
->pf
);
544 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
547 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n", lapb
->token
, frame
->pf
);
550 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n", lapb
->token
);
552 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
553 lapb_stop_t1timer(lapb
);
554 lapb_stop_t2timer(lapb
);
555 lapb
->state
= LAPB_STATE_3
;
556 lapb
->condition
= 0x00;
561 lapb_connect_indication(lapb
, LAPB_OK
);
567 printk(KERN_DEBUG
"lapb: (%p) S4 RX SABME(%d)\n", lapb
->token
, frame
->pf
);
569 if (lapb
->mode
& LAPB_EXTENDED
) {
571 printk(KERN_DEBUG
"lapb: (%p) S4 TX UA(%d)\n", lapb
->token
, frame
->pf
);
574 printk(KERN_DEBUG
"lapb: (%p) S4 -> S3\n", lapb
->token
);
576 lapb_send_control(lapb
, LAPB_UA
, frame
->pf
, LAPB_RESPONSE
);
577 lapb_stop_t1timer(lapb
);
578 lapb_stop_t2timer(lapb
);
579 lapb
->state
= LAPB_STATE_3
;
580 lapb
->condition
= 0x00;
585 lapb_connect_indication(lapb
, LAPB_OK
);
588 printk(KERN_DEBUG
"lapb: (%p) S4 TX DM(%d)\n", lapb
->token
, frame
->pf
);
590 lapb_send_control(lapb
, LAPB_DM
, frame
->pf
, LAPB_RESPONSE
);
602 * Process an incoming LAPB frame
604 void lapb_data_input(lapb_cb
*lapb
, struct sk_buff
*skb
)
606 struct lapb_frame frame
;
608 lapb_decode(lapb
, skb
, &frame
);
610 switch (lapb
->state
) {
612 lapb_state0_machine(lapb
, skb
, &frame
);
615 lapb_state1_machine(lapb
, skb
, &frame
);
618 lapb_state2_machine(lapb
, skb
, &frame
);
621 lapb_state3_machine(lapb
, skb
, &frame
);
624 lapb_state4_machine(lapb
, skb
, &frame
);