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 <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", lapb->dev);
66 lapb_send_control(lapb, LAPB_UA, frame->pf,
68 lapb_stop_t1timer(lapb);
69 lapb_stop_t2timer(lapb);
70 lapb->state = LAPB_STATE_3;
71 lapb->condition = 0x00;
76 lapb_connect_indication(lapb, LAPB_OK);
82 printk(KERN_DEBUG "lapb: (%p) S0 RX SABME(%d)\n",
83 lapb->dev, frame->pf);
85 if (lapb->mode & LAPB_EXTENDED) {
87 printk(KERN_DEBUG "lapb: (%p) S0 TX UA(%d)\n",
88 lapb->dev, frame->pf);
91 printk(KERN_DEBUG "lapb: (%p) S0 -> S3\n", lapb->dev);
93 lapb_send_control(lapb, LAPB_UA, frame->pf,
95 lapb_stop_t1timer(lapb);
96 lapb_stop_t2timer(lapb);
97 lapb->state = LAPB_STATE_3;
98 lapb->condition = 0x00;
103 lapb_connect_indication(lapb, LAPB_OK);
106 printk(KERN_DEBUG "lapb: (%p) S0 TX DM(%d)\n",
107 lapb->dev, frame->pf);
109 lapb_send_control(lapb, LAPB_DM, frame->pf,
116 printk(KERN_DEBUG "lapb: (%p) S0 RX DISC(%d)\n",
117 lapb->dev, frame->pf);
118 printk(KERN_DEBUG "lapb: (%p) S0 TX UA(%d)\n",
119 lapb->dev, frame->pf);
121 lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
132 * State machine for state 1, Awaiting Connection State.
133 * The handling of the timer(s) is in file lapb_timer.c.
135 static void lapb_state1_machine(struct lapb_cb *lapb, struct sk_buff *skb,
136 struct lapb_frame *frame)
138 switch (frame->type) {
141 printk(KERN_DEBUG "lapb: (%p) S1 RX SABM(%d)\n",
142 lapb->dev, frame->pf);
144 if (lapb->mode & LAPB_EXTENDED) {
146 printk(KERN_DEBUG "lapb: (%p) S1 TX DM(%d)\n",
147 lapb->dev, frame->pf);
149 lapb_send_control(lapb, LAPB_DM, frame->pf,
153 printk(KERN_DEBUG "lapb: (%p) S1 TX UA(%d)\n",
154 lapb->dev, frame->pf);
156 lapb_send_control(lapb, LAPB_UA, frame->pf,
163 printk(KERN_DEBUG "lapb: (%p) S1 RX SABME(%d)\n",
164 lapb->dev, frame->pf);
166 if (lapb->mode & LAPB_EXTENDED) {
168 printk(KERN_DEBUG "lapb: (%p) S1 TX UA(%d)\n",
169 lapb->dev, frame->pf);
171 lapb_send_control(lapb, LAPB_UA, frame->pf,
175 printk(KERN_DEBUG "lapb: (%p) S1 TX DM(%d)\n",
176 lapb->dev, frame->pf);
178 lapb_send_control(lapb, LAPB_DM, frame->pf,
185 printk(KERN_DEBUG "lapb: (%p) S1 RX DISC(%d)\n",
186 lapb->dev, frame->pf);
187 printk(KERN_DEBUG "lapb: (%p) S1 TX DM(%d)\n",
188 lapb->dev, frame->pf);
190 lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
195 printk(KERN_DEBUG "lapb: (%p) S1 RX UA(%d)\n",
196 lapb->dev, frame->pf);
200 printk(KERN_DEBUG "lapb: (%p) S1 -> S3\n", lapb->dev);
202 lapb_stop_t1timer(lapb);
203 lapb_stop_t2timer(lapb);
204 lapb->state = LAPB_STATE_3;
205 lapb->condition = 0x00;
210 lapb_connect_confirmation(lapb, LAPB_OK);
216 printk(KERN_DEBUG "lapb: (%p) S1 RX DM(%d)\n",
217 lapb->dev, frame->pf);
221 printk(KERN_DEBUG "lapb: (%p) S1 -> S0\n", lapb->dev);
223 lapb_clear_queues(lapb);
224 lapb->state = LAPB_STATE_0;
225 lapb_start_t1timer(lapb);
226 lapb_stop_t2timer(lapb);
227 lapb_disconnect_indication(lapb, LAPB_REFUSED);
236 * State machine for state 2, Awaiting Release State.
237 * The handling of the timer(s) is in file lapb_timer.c
239 static void lapb_state2_machine(struct lapb_cb *lapb, struct sk_buff *skb,
240 struct lapb_frame *frame)
242 switch (frame->type) {
246 printk(KERN_DEBUG "lapb: (%p) S2 RX {SABM,SABME}(%d)\n",
247 lapb->dev, frame->pf);
248 printk(KERN_DEBUG "lapb: (%p) S2 TX DM(%d)\n",
249 lapb->dev, frame->pf);
251 lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
256 printk(KERN_DEBUG "lapb: (%p) S2 RX DISC(%d)\n",
257 lapb->dev, frame->pf);
258 printk(KERN_DEBUG "lapb: (%p) S2 TX UA(%d)\n",
259 lapb->dev, frame->pf);
261 lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
266 printk(KERN_DEBUG "lapb: (%p) S2 RX UA(%d)\n",
267 lapb->dev, frame->pf);
271 printk(KERN_DEBUG "lapb: (%p) S2 -> S0\n", lapb->dev);
273 lapb->state = LAPB_STATE_0;
274 lapb_start_t1timer(lapb);
275 lapb_stop_t2timer(lapb);
276 lapb_disconnect_confirmation(lapb, LAPB_OK);
282 printk(KERN_DEBUG "lapb: (%p) S2 RX DM(%d)\n",
283 lapb->dev, frame->pf);
287 printk(KERN_DEBUG "lapb: (%p) S2 -> S0\n", lapb->dev);
289 lapb->state = LAPB_STATE_0;
290 lapb_start_t1timer(lapb);
291 lapb_stop_t2timer(lapb);
292 lapb_disconnect_confirmation(lapb, LAPB_NOTCONNECTED);
301 printk(KERN_DEBUG "lapb: (%p) S2 RX {I,REJ,RNR,RR}(%d)\n",
302 lapb->dev, frame->pf);
303 printk(KERN_DEBUG "lapb: (%p) S2 RX DM(%d)\n",
304 lapb->dev, frame->pf);
307 lapb_send_control(lapb, LAPB_DM, frame->pf,
316 * State machine for state 3, Connected State.
317 * The handling of the timer(s) is in file lapb_timer.c
319 static void lapb_state3_machine(struct lapb_cb *lapb, struct sk_buff *skb,
320 struct lapb_frame *frame)
323 int modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS :
326 switch (frame->type) {
329 printk(KERN_DEBUG "lapb: (%p) S3 RX SABM(%d)\n",
330 lapb->dev, frame->pf);
332 if (lapb->mode & LAPB_EXTENDED) {
334 printk(KERN_DEBUG "lapb: (%p) S3 TX DM(%d)\n",
335 lapb->dev, frame->pf);
337 lapb_send_control(lapb, LAPB_DM, frame->pf,
341 printk(KERN_DEBUG "lapb: (%p) S3 TX UA(%d)\n",
342 lapb->dev, frame->pf);
344 lapb_send_control(lapb, LAPB_UA, frame->pf,
346 lapb_stop_t1timer(lapb);
347 lapb_stop_t2timer(lapb);
348 lapb->condition = 0x00;
353 lapb_requeue_frames(lapb);
359 printk(KERN_DEBUG "lapb: (%p) S3 RX SABME(%d)\n",
360 lapb->dev, frame->pf);
362 if (lapb->mode & LAPB_EXTENDED) {
364 printk(KERN_DEBUG "lapb: (%p) S3 TX UA(%d)\n",
365 lapb->dev, frame->pf);
367 lapb_send_control(lapb, LAPB_UA, frame->pf,
369 lapb_stop_t1timer(lapb);
370 lapb_stop_t2timer(lapb);
371 lapb->condition = 0x00;
376 lapb_requeue_frames(lapb);
379 printk(KERN_DEBUG "lapb: (%p) S3 TX DM(%d)\n",
380 lapb->dev, frame->pf);
382 lapb_send_control(lapb, LAPB_DM, frame->pf,
389 printk(KERN_DEBUG "lapb: (%p) S3 RX DISC(%d)\n",
390 lapb->dev, frame->pf);
393 printk(KERN_DEBUG "lapb: (%p) S3 -> S0\n", lapb->dev);
395 lapb_clear_queues(lapb);
396 lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
397 lapb_start_t1timer(lapb);
398 lapb_stop_t2timer(lapb);
399 lapb->state = LAPB_STATE_0;
400 lapb_disconnect_indication(lapb, LAPB_OK);
405 printk(KERN_DEBUG "lapb: (%p) S3 RX DM(%d)\n",
406 lapb->dev, frame->pf);
409 printk(KERN_DEBUG "lapb: (%p) S3 -> S0\n", lapb->dev);
411 lapb_clear_queues(lapb);
412 lapb->state = LAPB_STATE_0;
413 lapb_start_t1timer(lapb);
414 lapb_stop_t2timer(lapb);
415 lapb_disconnect_indication(lapb, LAPB_NOTCONNECTED);
420 printk(KERN_DEBUG "lapb: (%p) S3 RX RNR(%d) R%d\n",
421 lapb->dev, 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_check_iframes_acked(lapb, frame->nr);
428 lapb->frmr_data = *frame;
429 lapb->frmr_type = LAPB_FRMR_Z;
430 lapb_transmit_frmr(lapb);
432 printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->dev);
434 lapb_start_t1timer(lapb);
435 lapb_stop_t2timer(lapb);
436 lapb->state = LAPB_STATE_4;
443 printk(KERN_DEBUG "lapb: (%p) S3 RX RR(%d) R%d\n",
444 lapb->dev, frame->pf, frame->nr);
446 lapb->condition &= ~LAPB_PEER_RX_BUSY_CONDITION;
447 lapb_check_need_response(lapb, frame->cr, frame->pf);
448 if (lapb_validate_nr(lapb, frame->nr)) {
449 lapb_check_iframes_acked(lapb, frame->nr);
451 lapb->frmr_data = *frame;
452 lapb->frmr_type = LAPB_FRMR_Z;
453 lapb_transmit_frmr(lapb);
455 printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->dev);
457 lapb_start_t1timer(lapb);
458 lapb_stop_t2timer(lapb);
459 lapb->state = LAPB_STATE_4;
466 printk(KERN_DEBUG "lapb: (%p) S3 RX REJ(%d) R%d\n",
467 lapb->dev, frame->pf, frame->nr);
469 lapb->condition &= ~LAPB_PEER_RX_BUSY_CONDITION;
470 lapb_check_need_response(lapb, frame->cr, frame->pf);
471 if (lapb_validate_nr(lapb, frame->nr)) {
472 lapb_frames_acked(lapb, frame->nr);
473 lapb_stop_t1timer(lapb);
475 lapb_requeue_frames(lapb);
477 lapb->frmr_data = *frame;
478 lapb->frmr_type = LAPB_FRMR_Z;
479 lapb_transmit_frmr(lapb);
481 printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->dev);
483 lapb_start_t1timer(lapb);
484 lapb_stop_t2timer(lapb);
485 lapb->state = LAPB_STATE_4;
492 printk(KERN_DEBUG "lapb: (%p) S3 RX I(%d) S%d R%d\n",
493 lapb->dev, frame->pf, frame->ns, frame->nr);
495 if (!lapb_validate_nr(lapb, frame->nr)) {
496 lapb->frmr_data = *frame;
497 lapb->frmr_type = LAPB_FRMR_Z;
498 lapb_transmit_frmr(lapb);
500 printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->dev);
502 lapb_start_t1timer(lapb);
503 lapb_stop_t2timer(lapb);
504 lapb->state = LAPB_STATE_4;
508 if (lapb->condition & LAPB_PEER_RX_BUSY_CONDITION)
509 lapb_frames_acked(lapb, frame->nr);
511 lapb_check_iframes_acked(lapb, frame->nr);
513 if (frame->ns == lapb->vr) {
515 cn = lapb_data_indication(lapb, skb);
518 * If upper layer has dropped the frame, we
519 * basically ignore any further protocol
520 * processing. This will cause the peer
521 * to re-transmit the frame later like
522 * a frame lost on the wire.
524 if (cn == NET_RX_DROP) {
525 printk(KERN_DEBUG "LAPB: rx congestion\n");
528 lapb->vr = (lapb->vr + 1) % modulus;
529 lapb->condition &= ~LAPB_REJECT_CONDITION;
531 lapb_enquiry_response(lapb);
533 if (!(lapb->condition &
534 LAPB_ACK_PENDING_CONDITION)) {
535 lapb->condition |= LAPB_ACK_PENDING_CONDITION;
536 lapb_start_t2timer(lapb);
540 if (lapb->condition & LAPB_REJECT_CONDITION) {
542 lapb_enquiry_response(lapb);
546 "lapb: (%p) S3 TX REJ(%d) R%d\n",
547 lapb->dev, frame->pf, lapb->vr);
549 lapb->condition |= LAPB_REJECT_CONDITION;
550 lapb_send_control(lapb, LAPB_REJ, frame->pf,
552 lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
559 printk(KERN_DEBUG "lapb: (%p) S3 RX FRMR(%d) %02X "
560 "%02X %02X %02X %02X\n", lapb->dev, frame->pf,
561 skb->data[0], skb->data[1], skb->data[2],
562 skb->data[3], skb->data[4]);
564 lapb_establish_data_link(lapb);
566 printk(KERN_DEBUG "lapb: (%p) S3 -> S1\n", lapb->dev);
568 lapb_requeue_frames(lapb);
569 lapb->state = LAPB_STATE_1;
574 printk(KERN_DEBUG "lapb: (%p) S3 RX ILLEGAL(%d)\n",
575 lapb->dev, frame->pf);
577 lapb->frmr_data = *frame;
578 lapb->frmr_type = LAPB_FRMR_W;
579 lapb_transmit_frmr(lapb);
581 printk(KERN_DEBUG "lapb: (%p) S3 -> S4\n", lapb->dev);
583 lapb_start_t1timer(lapb);
584 lapb_stop_t2timer(lapb);
585 lapb->state = LAPB_STATE_4;
595 * State machine for state 4, Frame Reject State.
596 * The handling of the timer(s) is in file lapb_timer.c.
598 static void lapb_state4_machine(struct lapb_cb *lapb, struct sk_buff *skb,
599 struct lapb_frame *frame)
601 switch (frame->type) {
604 printk(KERN_DEBUG "lapb: (%p) S4 RX SABM(%d)\n",
605 lapb->dev, frame->pf);
607 if (lapb->mode & LAPB_EXTENDED) {
609 printk(KERN_DEBUG "lapb: (%p) S4 TX DM(%d)\n",
610 lapb->dev, frame->pf);
612 lapb_send_control(lapb, LAPB_DM, frame->pf,
616 printk(KERN_DEBUG "lapb: (%p) S4 TX UA(%d)\n",
617 lapb->dev, frame->pf);
620 printk(KERN_DEBUG "lapb: (%p) S4 -> S3\n", lapb->dev);
622 lapb_send_control(lapb, LAPB_UA, frame->pf,
624 lapb_stop_t1timer(lapb);
625 lapb_stop_t2timer(lapb);
626 lapb->state = LAPB_STATE_3;
627 lapb->condition = 0x00;
632 lapb_connect_indication(lapb, LAPB_OK);
638 printk(KERN_DEBUG "lapb: (%p) S4 RX SABME(%d)\n",
639 lapb->dev, frame->pf);
641 if (lapb->mode & LAPB_EXTENDED) {
643 printk(KERN_DEBUG "lapb: (%p) S4 TX UA(%d)\n",
644 lapb->dev, frame->pf);
647 printk(KERN_DEBUG "lapb: (%p) S4 -> S3\n", lapb->dev);
649 lapb_send_control(lapb, LAPB_UA, frame->pf,
651 lapb_stop_t1timer(lapb);
652 lapb_stop_t2timer(lapb);
653 lapb->state = LAPB_STATE_3;
654 lapb->condition = 0x00;
659 lapb_connect_indication(lapb, LAPB_OK);
662 printk(KERN_DEBUG "lapb: (%p) S4 TX DM(%d)\n",
663 lapb->dev, frame->pf);
665 lapb_send_control(lapb, LAPB_DM, frame->pf,
675 * Process an incoming LAPB frame
677 void lapb_data_input(struct lapb_cb *lapb, struct sk_buff *skb)
679 struct lapb_frame frame;
681 if (lapb_decode(lapb, skb, &frame) < 0) {
686 switch (lapb->state) {
688 lapb_state0_machine(lapb, skb, &frame); break;
690 lapb_state1_machine(lapb, skb, &frame); break;
692 lapb_state2_machine(lapb, skb, &frame); break;
694 lapb_state3_machine(lapb, skb, &frame); break;
696 lapb_state4_machine(lapb, skb, &frame); break;