Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
0005  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
0006  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
0007  * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
0008  */
0009 #include <linux/errno.h>
0010 #include <linux/types.h>
0011 #include <linux/socket.h>
0012 #include <linux/in.h>
0013 #include <linux/kernel.h>
0014 #include <linux/timer.h>
0015 #include <linux/string.h>
0016 #include <linux/sockios.h>
0017 #include <linux/net.h>
0018 #include <net/ax25.h>
0019 #include <linux/inet.h>
0020 #include <linux/netdevice.h>
0021 #include <linux/skbuff.h>
0022 #include <net/sock.h>
0023 #include <net/tcp_states.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/fcntl.h>
0026 #include <linux/mm.h>
0027 #include <linux/interrupt.h>
0028 
0029 void ax25_std_heartbeat_expiry(ax25_cb *ax25)
0030 {
0031     struct sock *sk = ax25->sk;
0032 
0033     if (sk)
0034         bh_lock_sock(sk);
0035 
0036     switch (ax25->state) {
0037     case AX25_STATE_0:
0038     case AX25_STATE_2:
0039         /* Magic here: If we listen() and a new link dies before it
0040            is accepted() it isn't 'dead' so doesn't get removed. */
0041         if (!sk || sock_flag(sk, SOCK_DESTROY) ||
0042             (sk->sk_state == TCP_LISTEN &&
0043              sock_flag(sk, SOCK_DEAD))) {
0044             if (sk) {
0045                 sock_hold(sk);
0046                 ax25_destroy_socket(ax25);
0047                 bh_unlock_sock(sk);
0048                 /* Ungrab socket and destroy it */
0049                 sock_put(sk);
0050             } else
0051                 ax25_destroy_socket(ax25);
0052             return;
0053         }
0054         break;
0055 
0056     case AX25_STATE_3:
0057     case AX25_STATE_4:
0058         /*
0059          * Check the state of the receive buffer.
0060          */
0061         if (sk != NULL) {
0062             if (atomic_read(&sk->sk_rmem_alloc) <
0063                 (sk->sk_rcvbuf >> 1) &&
0064                 (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
0065                 ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
0066                 ax25->condition &= ~AX25_COND_ACK_PENDING;
0067                 ax25_send_control(ax25, AX25_RR, AX25_POLLOFF, AX25_RESPONSE);
0068                 break;
0069             }
0070         }
0071     }
0072 
0073     if (sk)
0074         bh_unlock_sock(sk);
0075 
0076     ax25_start_heartbeat(ax25);
0077 }
0078 
0079 void ax25_std_t2timer_expiry(ax25_cb *ax25)
0080 {
0081     if (ax25->condition & AX25_COND_ACK_PENDING) {
0082         ax25->condition &= ~AX25_COND_ACK_PENDING;
0083         ax25_std_timeout_response(ax25);
0084     }
0085 }
0086 
0087 void ax25_std_t3timer_expiry(ax25_cb *ax25)
0088 {
0089     ax25->n2count = 0;
0090     ax25_std_transmit_enquiry(ax25);
0091     ax25->state   = AX25_STATE_4;
0092 }
0093 
0094 void ax25_std_idletimer_expiry(ax25_cb *ax25)
0095 {
0096     ax25_clear_queues(ax25);
0097 
0098     ax25->n2count = 0;
0099     ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0100     ax25->state   = AX25_STATE_2;
0101 
0102     ax25_calculate_t1(ax25);
0103     ax25_start_t1timer(ax25);
0104     ax25_stop_t2timer(ax25);
0105     ax25_stop_t3timer(ax25);
0106 
0107     if (ax25->sk != NULL) {
0108         bh_lock_sock(ax25->sk);
0109         ax25->sk->sk_state     = TCP_CLOSE;
0110         ax25->sk->sk_err       = 0;
0111         ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
0112         if (!sock_flag(ax25->sk, SOCK_DEAD)) {
0113             ax25->sk->sk_state_change(ax25->sk);
0114             sock_set_flag(ax25->sk, SOCK_DEAD);
0115         }
0116         bh_unlock_sock(ax25->sk);
0117     }
0118 }
0119 
0120 void ax25_std_t1timer_expiry(ax25_cb *ax25)
0121 {
0122     switch (ax25->state) {
0123     case AX25_STATE_1:
0124         if (ax25->n2count == ax25->n2) {
0125             if (ax25->modulus == AX25_MODULUS) {
0126                 ax25_disconnect(ax25, ETIMEDOUT);
0127                 return;
0128             } else {
0129                 ax25->modulus = AX25_MODULUS;
0130                 ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
0131                 ax25->n2count = 0;
0132                 ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
0133             }
0134         } else {
0135             ax25->n2count++;
0136             if (ax25->modulus == AX25_MODULUS)
0137                 ax25_send_control(ax25, AX25_SABM, AX25_POLLON, AX25_COMMAND);
0138             else
0139                 ax25_send_control(ax25, AX25_SABME, AX25_POLLON, AX25_COMMAND);
0140         }
0141         break;
0142 
0143     case AX25_STATE_2:
0144         if (ax25->n2count == ax25->n2) {
0145             ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0146             if (!sock_flag(ax25->sk, SOCK_DESTROY))
0147                 ax25_disconnect(ax25, ETIMEDOUT);
0148             return;
0149         } else {
0150             ax25->n2count++;
0151             ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0152         }
0153         break;
0154 
0155     case AX25_STATE_3:
0156         ax25->n2count = 1;
0157         ax25_std_transmit_enquiry(ax25);
0158         ax25->state   = AX25_STATE_4;
0159         break;
0160 
0161     case AX25_STATE_4:
0162         if (ax25->n2count == ax25->n2) {
0163             ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
0164             ax25_disconnect(ax25, ETIMEDOUT);
0165             return;
0166         } else {
0167             ax25->n2count++;
0168             ax25_std_transmit_enquiry(ax25);
0169         }
0170         break;
0171     }
0172 
0173     ax25_calculate_t1(ax25);
0174     ax25_start_t1timer(ax25);
0175 }