Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
0005  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
0006  */
0007 #include <linux/errno.h>
0008 #include <linux/types.h>
0009 #include <linux/socket.h>
0010 #include <linux/spinlock.h>
0011 #include <linux/in.h>
0012 #include <linux/kernel.h>
0013 #include <linux/jiffies.h>
0014 #include <linux/timer.h>
0015 #include <linux/string.h>
0016 #include <linux/sockios.h>
0017 #include <linux/net.h>
0018 #include <net/tcp_states.h>
0019 #include <net/ax25.h>
0020 #include <linux/inet.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/skbuff.h>
0023 #include <net/sock.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/fcntl.h>
0026 #include <linux/mm.h>
0027 #include <linux/interrupt.h>
0028 
0029 static void ax25_ds_timeout(struct timer_list *);
0030 
0031 /*
0032  *  Add DAMA slave timeout timer to timer list.
0033  *  Unlike the connection based timers the timeout function gets
0034  *  triggered every second. Please note that NET_AX25_DAMA_SLAVE_TIMEOUT
0035  *  (aka /proc/sys/net/ax25/{dev}/dama_slave_timeout) is still in
0036  *  1/10th of a second.
0037  */
0038 
0039 void ax25_ds_setup_timer(ax25_dev *ax25_dev)
0040 {
0041     timer_setup(&ax25_dev->dama.slave_timer, ax25_ds_timeout, 0);
0042 }
0043 
0044 void ax25_ds_del_timer(ax25_dev *ax25_dev)
0045 {
0046     if (ax25_dev)
0047         del_timer(&ax25_dev->dama.slave_timer);
0048 }
0049 
0050 void ax25_ds_set_timer(ax25_dev *ax25_dev)
0051 {
0052     if (ax25_dev == NULL)       /* paranoia */
0053         return;
0054 
0055     ax25_dev->dama.slave_timeout =
0056         msecs_to_jiffies(ax25_dev->values[AX25_VALUES_DS_TIMEOUT]) / 10;
0057     mod_timer(&ax25_dev->dama.slave_timer, jiffies + HZ);
0058 }
0059 
0060 /*
0061  *  DAMA Slave Timeout
0062  *  Silently discard all (slave) connections in case our master forgot us...
0063  */
0064 
0065 static void ax25_ds_timeout(struct timer_list *t)
0066 {
0067     ax25_dev *ax25_dev = from_timer(ax25_dev, t, dama.slave_timer);
0068     ax25_cb *ax25;
0069 
0070     if (ax25_dev == NULL || !ax25_dev->dama.slave)
0071         return;         /* Yikes! */
0072 
0073     if (!ax25_dev->dama.slave_timeout || --ax25_dev->dama.slave_timeout) {
0074         ax25_ds_set_timer(ax25_dev);
0075         return;
0076     }
0077 
0078     spin_lock(&ax25_list_lock);
0079     ax25_for_each(ax25, &ax25_list) {
0080         if (ax25->ax25_dev != ax25_dev || !(ax25->condition & AX25_COND_DAMA_MODE))
0081             continue;
0082 
0083         ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0084         ax25_disconnect(ax25, ETIMEDOUT);
0085     }
0086     spin_unlock(&ax25_list_lock);
0087 
0088     ax25_dev_dama_off(ax25_dev);
0089 }
0090 
0091 void ax25_ds_heartbeat_expiry(ax25_cb *ax25)
0092 {
0093     struct sock *sk=ax25->sk;
0094 
0095     if (sk)
0096         bh_lock_sock(sk);
0097 
0098     switch (ax25->state) {
0099 
0100     case AX25_STATE_0:
0101     case AX25_STATE_2:
0102         /* Magic here: If we listen() and a new link dies before it
0103            is accepted() it isn't 'dead' so doesn't get removed. */
0104         if (!sk || sock_flag(sk, SOCK_DESTROY) ||
0105             (sk->sk_state == TCP_LISTEN &&
0106              sock_flag(sk, SOCK_DEAD))) {
0107             if (sk) {
0108                 sock_hold(sk);
0109                 ax25_destroy_socket(ax25);
0110                 bh_unlock_sock(sk);
0111                 /* Ungrab socket and destroy it */
0112                 sock_put(sk);
0113             } else
0114                 ax25_destroy_socket(ax25);
0115             return;
0116         }
0117         break;
0118 
0119     case AX25_STATE_3:
0120         /*
0121          * Check the state of the receive buffer.
0122          */
0123         if (sk != NULL) {
0124             if (atomic_read(&sk->sk_rmem_alloc) <
0125                 (sk->sk_rcvbuf >> 1) &&
0126                 (ax25->condition & AX25_COND_OWN_RX_BUSY)) {
0127                 ax25->condition &= ~AX25_COND_OWN_RX_BUSY;
0128                 ax25->condition &= ~AX25_COND_ACK_PENDING;
0129                 break;
0130             }
0131         }
0132         break;
0133     }
0134 
0135     if (sk)
0136         bh_unlock_sock(sk);
0137 
0138     ax25_start_heartbeat(ax25);
0139 }
0140 
0141 /* dl1bke 960114: T3 works much like the IDLE timeout, but
0142  *                gets reloaded with every frame for this
0143  *        connection.
0144  */
0145 void ax25_ds_t3timer_expiry(ax25_cb *ax25)
0146 {
0147     ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0148     ax25_dama_off(ax25);
0149     ax25_disconnect(ax25, ETIMEDOUT);
0150 }
0151 
0152 /* dl1bke 960228: close the connection when IDLE expires.
0153  *        unlike T3 this timer gets reloaded only on
0154  *        I frames.
0155  */
0156 void ax25_ds_idletimer_expiry(ax25_cb *ax25)
0157 {
0158     ax25_clear_queues(ax25);
0159 
0160     ax25->n2count = 0;
0161     ax25->state = AX25_STATE_2;
0162 
0163     ax25_calculate_t1(ax25);
0164     ax25_start_t1timer(ax25);
0165     ax25_stop_t3timer(ax25);
0166 
0167     if (ax25->sk != NULL) {
0168         bh_lock_sock(ax25->sk);
0169         ax25->sk->sk_state     = TCP_CLOSE;
0170         ax25->sk->sk_err       = 0;
0171         ax25->sk->sk_shutdown |= SEND_SHUTDOWN;
0172         if (!sock_flag(ax25->sk, SOCK_DEAD)) {
0173             ax25->sk->sk_state_change(ax25->sk);
0174             sock_set_flag(ax25->sk, SOCK_DEAD);
0175         }
0176         bh_unlock_sock(ax25->sk);
0177     }
0178 }
0179 
0180 /* dl1bke 960114: The DAMA protocol requires to send data and SABM/DISC
0181  *                within the poll of any connected channel. Remember
0182  *                that we are not allowed to send anything unless we
0183  *                get polled by the Master.
0184  *
0185  *                Thus we'll have to do parts of our T1 handling in
0186  *                ax25_enquiry_response().
0187  */
0188 void ax25_ds_t1_timeout(ax25_cb *ax25)
0189 {
0190     switch (ax25->state) {
0191     case AX25_STATE_1:
0192         if (ax25->n2count == ax25->n2) {
0193             if (ax25->modulus == AX25_MODULUS) {
0194                 ax25_disconnect(ax25, ETIMEDOUT);
0195                 return;
0196             } else {
0197                 ax25->modulus = AX25_MODULUS;
0198                 ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
0199                 ax25->n2count = 0;
0200                 ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
0201             }
0202         } else {
0203             ax25->n2count++;
0204             if (ax25->modulus == AX25_MODULUS)
0205                 ax25_send_control(ax25, AX25_SABM, AX25_POLLOFF, AX25_COMMAND);
0206             else
0207                 ax25_send_control(ax25, AX25_SABME, AX25_POLLOFF, AX25_COMMAND);
0208         }
0209         break;
0210 
0211     case AX25_STATE_2:
0212         if (ax25->n2count == ax25->n2) {
0213             ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
0214             if (!sock_flag(ax25->sk, SOCK_DESTROY))
0215                 ax25_disconnect(ax25, ETIMEDOUT);
0216             return;
0217         } else {
0218             ax25->n2count++;
0219         }
0220         break;
0221 
0222     case AX25_STATE_3:
0223         if (ax25->n2count == ax25->n2) {
0224             ax25_send_control(ax25, AX25_DM, AX25_POLLON, AX25_RESPONSE);
0225             ax25_disconnect(ax25, ETIMEDOUT);
0226             return;
0227         } else {
0228             ax25->n2count++;
0229         }
0230         break;
0231     }
0232 
0233     ax25_calculate_t1(ax25);
0234     ax25_start_t1timer(ax25);
0235 }