2  * This program is free software; you can redistribute it and/or modify
 
   3  * it under the terms of the GNU General Public License as published by
 
   4  * the Free Software Foundation; either version 2 of the License, or
 
   5  * (at your option) any later version.
 
   7  * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
 
   8  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
 
   9  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
 
  10  * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
 
  12  * Most of this code is based on the SDL diagrams published in the 7th ARRL
 
  13  * Computer Networking Conference papers. The diagrams have mistakes in them,
 
  14  * but are mostly correct. Before you modify the code could you read the SDL
 
  15  * diagrams as the code is not obvious and probably very easy to break.
 
  17 #include <linux/errno.h>
 
  18 #include <linux/types.h>
 
  19 #include <linux/socket.h>
 
  21 #include <linux/kernel.h>
 
  22 #include <linux/timer.h>
 
  23 #include <linux/string.h>
 
  24 #include <linux/sockios.h>
 
  25 #include <linux/net.h>
 
  27 #include <linux/inet.h>
 
  28 #include <linux/netdevice.h>
 
  29 #include <linux/skbuff.h>
 
  31 #include <net/tcp_states.h>
 
  32 #include <asm/uaccess.h>
 
  33 #include <asm/system.h>
 
  34 #include <linux/fcntl.h>
 
  36 #include <linux/interrupt.h>
 
  39  *      State machine for state 1, Awaiting Connection State.
 
  40  *      The handling of the timer(s) is in file ax25_std_timer.c.
 
  41  *      Handling of state 0 and connection release is in ax25.c.
 
  43 static int ax25_std_state1_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
 
  47                 ax25->modulus = AX25_MODULUS;
 
  48                 ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
 
  49                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
  53                 ax25->modulus = AX25_EMODULUS;
 
  54                 ax25->window  = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
 
  55                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
  59                 ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
 
  64                         ax25_calculate_rtt(ax25);
 
  65                         ax25_stop_t1timer(ax25);
 
  66                         ax25_start_t3timer(ax25);
 
  67                         ax25_start_idletimer(ax25);
 
  71                         ax25->state   = AX25_STATE_3;
 
  73                         if (ax25->sk != NULL) {
 
  74                                 bh_lock_sock(ax25->sk);
 
  75                                 ax25->sk->sk_state = TCP_ESTABLISHED;
 
  76                                 /* For WAIT_SABM connections we will produce an accept ready socket here */
 
  77                                 if (!sock_flag(ax25->sk, SOCK_DEAD))
 
  78                                         ax25->sk->sk_state_change(ax25->sk);
 
  79                                 bh_unlock_sock(ax25->sk);
 
  86                         if (ax25->modulus == AX25_MODULUS) {
 
  87                                 ax25_disconnect(ax25, ECONNREFUSED);
 
  89                                 ax25->modulus = AX25_MODULUS;
 
  90                                 ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
 
 103  *      State machine for state 2, Awaiting Release State.
 
 104  *      The handling of the timer(s) is in file ax25_std_timer.c
 
 105  *      Handling of state 0 and connection release is in ax25.c.
 
 107 static int ax25_std_state2_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int pf, int type)
 
 112                 ax25_send_control(ax25, AX25_DM, pf, AX25_RESPONSE);
 
 116                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
 117                 ax25_disconnect(ax25, 0);
 
 123                         ax25_disconnect(ax25, 0);
 
 130                 if (pf) ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
 
 141  *      State machine for state 3, Connected State.
 
 142  *      The handling of the timer(s) is in file ax25_std_timer.c
 
 143  *      Handling of state 0 and connection release is in ax25.c.
 
 145 static int ax25_std_state3_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
 
 152                 if (frametype == AX25_SABM) {
 
 153                         ax25->modulus = AX25_MODULUS;
 
 154                         ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
 
 156                         ax25->modulus = AX25_EMODULUS;
 
 157                         ax25->window  = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
 
 159                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
 160                 ax25_stop_t1timer(ax25);
 
 161                 ax25_stop_t2timer(ax25);
 
 162                 ax25_start_t3timer(ax25);
 
 163                 ax25_start_idletimer(ax25);
 
 164                 ax25->condition = 0x00;
 
 168                 ax25_requeue_frames(ax25);
 
 172                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
 173                 ax25_disconnect(ax25, 0);
 
 177                 ax25_disconnect(ax25, ECONNRESET);
 
 182                 if (frametype == AX25_RR)
 
 183                         ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
 
 185                         ax25->condition |= AX25_COND_PEER_RX_BUSY;
 
 186                 if (type == AX25_COMMAND && pf)
 
 187                         ax25_std_enquiry_response(ax25);
 
 188                 if (ax25_validate_nr(ax25, nr)) {
 
 189                         ax25_check_iframes_acked(ax25, nr);
 
 191                         ax25_std_nr_error_recovery(ax25);
 
 192                         ax25->state = AX25_STATE_1;
 
 197                 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
 
 198                 if (type == AX25_COMMAND && pf)
 
 199                         ax25_std_enquiry_response(ax25);
 
 200                 if (ax25_validate_nr(ax25, nr)) {
 
 201                         ax25_frames_acked(ax25, nr);
 
 202                         ax25_calculate_rtt(ax25);
 
 203                         ax25_stop_t1timer(ax25);
 
 204                         ax25_start_t3timer(ax25);
 
 205                         ax25_requeue_frames(ax25);
 
 207                         ax25_std_nr_error_recovery(ax25);
 
 208                         ax25->state = AX25_STATE_1;
 
 213                 if (!ax25_validate_nr(ax25, nr)) {
 
 214                         ax25_std_nr_error_recovery(ax25);
 
 215                         ax25->state = AX25_STATE_1;
 
 218                 if (ax25->condition & AX25_COND_PEER_RX_BUSY) {
 
 219                         ax25_frames_acked(ax25, nr);
 
 221                         ax25_check_iframes_acked(ax25, nr);
 
 223                 if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
 
 224                         if (pf) ax25_std_enquiry_response(ax25);
 
 227                 if (ns == ax25->vr) {
 
 228                         ax25->vr = (ax25->vr + 1) % ax25->modulus;
 
 229                         queued = ax25_rx_iframe(ax25, skb);
 
 230                         if (ax25->condition & AX25_COND_OWN_RX_BUSY)
 
 231                                 ax25->vr = ns;  /* ax25->vr - 1 */
 
 232                         ax25->condition &= ~AX25_COND_REJECT;
 
 234                                 ax25_std_enquiry_response(ax25);
 
 236                                 if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
 
 237                                         ax25->condition |= AX25_COND_ACK_PENDING;
 
 238                                         ax25_start_t2timer(ax25);
 
 242                         if (ax25->condition & AX25_COND_REJECT) {
 
 243                                 if (pf) ax25_std_enquiry_response(ax25);
 
 245                                 ax25->condition |= AX25_COND_REJECT;
 
 246                                 ax25_send_control(ax25, AX25_REJ, pf, AX25_RESPONSE);
 
 247                                 ax25->condition &= ~AX25_COND_ACK_PENDING;
 
 254                 ax25_std_establish_data_link(ax25);
 
 255                 ax25->state = AX25_STATE_1;
 
 266  *      State machine for state 4, Timer Recovery State.
 
 267  *      The handling of the timer(s) is in file ax25_std_timer.c
 
 268  *      Handling of state 0 and connection release is in ax25.c.
 
 270 static int ax25_std_state4_machine(ax25_cb *ax25, struct sk_buff *skb, int frametype, int ns, int nr, int pf, int type)
 
 277                 if (frametype == AX25_SABM) {
 
 278                         ax25->modulus = AX25_MODULUS;
 
 279                         ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
 
 281                         ax25->modulus = AX25_EMODULUS;
 
 282                         ax25->window  = ax25->ax25_dev->values[AX25_VALUES_EWINDOW];
 
 284                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
 285                 ax25_stop_t1timer(ax25);
 
 286                 ax25_stop_t2timer(ax25);
 
 287                 ax25_start_t3timer(ax25);
 
 288                 ax25_start_idletimer(ax25);
 
 289                 ax25->condition = 0x00;
 
 293                 ax25->state     = AX25_STATE_3;
 
 295                 ax25_requeue_frames(ax25);
 
 299                 ax25_send_control(ax25, AX25_UA, pf, AX25_RESPONSE);
 
 300                 ax25_disconnect(ax25, 0);
 
 304                 ax25_disconnect(ax25, ECONNRESET);
 
 309                 if (frametype == AX25_RR)
 
 310                         ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
 
 312                         ax25->condition |= AX25_COND_PEER_RX_BUSY;
 
 313                 if (type == AX25_RESPONSE && pf) {
 
 314                         ax25_stop_t1timer(ax25);
 
 316                         if (ax25_validate_nr(ax25, nr)) {
 
 317                                 ax25_frames_acked(ax25, nr);
 
 318                                 if (ax25->vs == ax25->va) {
 
 319                                         ax25_start_t3timer(ax25);
 
 320                                         ax25->state   = AX25_STATE_3;
 
 322                                         ax25_requeue_frames(ax25);
 
 325                                 ax25_std_nr_error_recovery(ax25);
 
 326                                 ax25->state = AX25_STATE_1;
 
 330                 if (type == AX25_COMMAND && pf)
 
 331                         ax25_std_enquiry_response(ax25);
 
 332                 if (ax25_validate_nr(ax25, nr)) {
 
 333                         ax25_frames_acked(ax25, nr);
 
 335                         ax25_std_nr_error_recovery(ax25);
 
 336                         ax25->state = AX25_STATE_1;
 
 341                 ax25->condition &= ~AX25_COND_PEER_RX_BUSY;
 
 342                 if (pf && type == AX25_RESPONSE) {
 
 343                         ax25_stop_t1timer(ax25);
 
 345                         if (ax25_validate_nr(ax25, nr)) {
 
 346                                 ax25_frames_acked(ax25, nr);
 
 347                                 if (ax25->vs == ax25->va) {
 
 348                                         ax25_start_t3timer(ax25);
 
 349                                         ax25->state   = AX25_STATE_3;
 
 351                                         ax25_requeue_frames(ax25);
 
 354                                 ax25_std_nr_error_recovery(ax25);
 
 355                                 ax25->state = AX25_STATE_1;
 
 359                 if (type == AX25_COMMAND && pf)
 
 360                         ax25_std_enquiry_response(ax25);
 
 361                 if (ax25_validate_nr(ax25, nr)) {
 
 362                         ax25_frames_acked(ax25, nr);
 
 363                         ax25_requeue_frames(ax25);
 
 365                         ax25_std_nr_error_recovery(ax25);
 
 366                         ax25->state = AX25_STATE_1;
 
 371                 if (!ax25_validate_nr(ax25, nr)) {
 
 372                         ax25_std_nr_error_recovery(ax25);
 
 373                         ax25->state = AX25_STATE_1;
 
 376                 ax25_frames_acked(ax25, nr);
 
 377                 if (ax25->condition & AX25_COND_OWN_RX_BUSY) {
 
 379                                 ax25_std_enquiry_response(ax25);
 
 382                 if (ns == ax25->vr) {
 
 383                         ax25->vr = (ax25->vr + 1) % ax25->modulus;
 
 384                         queued = ax25_rx_iframe(ax25, skb);
 
 385                         if (ax25->condition & AX25_COND_OWN_RX_BUSY)
 
 386                                 ax25->vr = ns;  /* ax25->vr - 1 */
 
 387                         ax25->condition &= ~AX25_COND_REJECT;
 
 389                                 ax25_std_enquiry_response(ax25);
 
 391                                 if (!(ax25->condition & AX25_COND_ACK_PENDING)) {
 
 392                                         ax25->condition |= AX25_COND_ACK_PENDING;
 
 393                                         ax25_start_t2timer(ax25);
 
 397                         if (ax25->condition & AX25_COND_REJECT) {
 
 398                                 if (pf) ax25_std_enquiry_response(ax25);
 
 400                                 ax25->condition |= AX25_COND_REJECT;
 
 401                                 ax25_send_control(ax25, AX25_REJ, pf, AX25_RESPONSE);
 
 402                                 ax25->condition &= ~AX25_COND_ACK_PENDING;
 
 409                 ax25_std_establish_data_link(ax25);
 
 410                 ax25->state = AX25_STATE_1;
 
 421  *      Higher level upcall for a LAPB frame
 
 423 int ax25_std_frame_in(ax25_cb *ax25, struct sk_buff *skb, int type)
 
 425         int queued = 0, frametype, ns, nr, pf;
 
 427         frametype = ax25_decode(ax25, skb, &ns, &nr, &pf);
 
 429         switch (ax25->state) {
 
 431                 queued = ax25_std_state1_machine(ax25, skb, frametype, pf, type);
 
 434                 queued = ax25_std_state2_machine(ax25, skb, frametype, pf, type);
 
 437                 queued = ax25_std_state3_machine(ax25, skb, frametype, ns, nr, pf, type);
 
 440                 queued = ax25_std_state4_machine(ax25, skb, frametype, ns, nr, pf, type);