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) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
0006  */
0007 #include <linux/errno.h>
0008 #include <linux/types.h>
0009 #include <linux/socket.h>
0010 #include <linux/in.h>
0011 #include <linux/kernel.h>
0012 #include <linux/jiffies.h>
0013 #include <linux/timer.h>
0014 #include <linux/string.h>
0015 #include <linux/sockios.h>
0016 #include <linux/net.h>
0017 #include <net/ax25.h>
0018 #include <linux/inet.h>
0019 #include <linux/netdevice.h>
0020 #include <linux/skbuff.h>
0021 #include <net/sock.h>
0022 #include <net/tcp_states.h>
0023 #include <linux/uaccess.h>
0024 #include <linux/fcntl.h>
0025 #include <linux/mm.h>
0026 #include <linux/interrupt.h>
0027 #include <net/netrom.h>
0028 
0029 static void nr_heartbeat_expiry(struct timer_list *);
0030 static void nr_t1timer_expiry(struct timer_list *);
0031 static void nr_t2timer_expiry(struct timer_list *);
0032 static void nr_t4timer_expiry(struct timer_list *);
0033 static void nr_idletimer_expiry(struct timer_list *);
0034 
0035 void nr_init_timers(struct sock *sk)
0036 {
0037     struct nr_sock *nr = nr_sk(sk);
0038 
0039     timer_setup(&nr->t1timer, nr_t1timer_expiry, 0);
0040     timer_setup(&nr->t2timer, nr_t2timer_expiry, 0);
0041     timer_setup(&nr->t4timer, nr_t4timer_expiry, 0);
0042     timer_setup(&nr->idletimer, nr_idletimer_expiry, 0);
0043 
0044     /* initialized by sock_init_data */
0045     sk->sk_timer.function = nr_heartbeat_expiry;
0046 }
0047 
0048 void nr_start_t1timer(struct sock *sk)
0049 {
0050     struct nr_sock *nr = nr_sk(sk);
0051 
0052     sk_reset_timer(sk, &nr->t1timer, jiffies + nr->t1);
0053 }
0054 
0055 void nr_start_t2timer(struct sock *sk)
0056 {
0057     struct nr_sock *nr = nr_sk(sk);
0058 
0059     sk_reset_timer(sk, &nr->t2timer, jiffies + nr->t2);
0060 }
0061 
0062 void nr_start_t4timer(struct sock *sk)
0063 {
0064     struct nr_sock *nr = nr_sk(sk);
0065 
0066     sk_reset_timer(sk, &nr->t4timer, jiffies + nr->t4);
0067 }
0068 
0069 void nr_start_idletimer(struct sock *sk)
0070 {
0071     struct nr_sock *nr = nr_sk(sk);
0072 
0073     if (nr->idle > 0)
0074         sk_reset_timer(sk, &nr->idletimer, jiffies + nr->idle);
0075 }
0076 
0077 void nr_start_heartbeat(struct sock *sk)
0078 {
0079     sk_reset_timer(sk, &sk->sk_timer, jiffies + 5 * HZ);
0080 }
0081 
0082 void nr_stop_t1timer(struct sock *sk)
0083 {
0084     sk_stop_timer(sk, &nr_sk(sk)->t1timer);
0085 }
0086 
0087 void nr_stop_t2timer(struct sock *sk)
0088 {
0089     sk_stop_timer(sk, &nr_sk(sk)->t2timer);
0090 }
0091 
0092 void nr_stop_t4timer(struct sock *sk)
0093 {
0094     sk_stop_timer(sk, &nr_sk(sk)->t4timer);
0095 }
0096 
0097 void nr_stop_idletimer(struct sock *sk)
0098 {
0099     sk_stop_timer(sk, &nr_sk(sk)->idletimer);
0100 }
0101 
0102 void nr_stop_heartbeat(struct sock *sk)
0103 {
0104     sk_stop_timer(sk, &sk->sk_timer);
0105 }
0106 
0107 int nr_t1timer_running(struct sock *sk)
0108 {
0109     return timer_pending(&nr_sk(sk)->t1timer);
0110 }
0111 
0112 static void nr_heartbeat_expiry(struct timer_list *t)
0113 {
0114     struct sock *sk = from_timer(sk, t, sk_timer);
0115     struct nr_sock *nr = nr_sk(sk);
0116 
0117     bh_lock_sock(sk);
0118     switch (nr->state) {
0119     case NR_STATE_0:
0120         /* Magic here: If we listen() and a new link dies before it
0121            is accepted() it isn't 'dead' so doesn't get removed. */
0122         if (sock_flag(sk, SOCK_DESTROY) ||
0123             (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
0124             bh_unlock_sock(sk);
0125             nr_destroy_socket(sk);
0126             goto out;
0127         }
0128         break;
0129 
0130     case NR_STATE_3:
0131         /*
0132          * Check for the state of the receive buffer.
0133          */
0134         if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
0135             (nr->condition & NR_COND_OWN_RX_BUSY)) {
0136             nr->condition &= ~NR_COND_OWN_RX_BUSY;
0137             nr->condition &= ~NR_COND_ACK_PENDING;
0138             nr->vl         = nr->vr;
0139             nr_write_internal(sk, NR_INFOACK);
0140             break;
0141         }
0142         break;
0143     }
0144 
0145     nr_start_heartbeat(sk);
0146     bh_unlock_sock(sk);
0147 out:
0148     sock_put(sk);
0149 }
0150 
0151 static void nr_t2timer_expiry(struct timer_list *t)
0152 {
0153     struct nr_sock *nr = from_timer(nr, t, t2timer);
0154     struct sock *sk = &nr->sock;
0155 
0156     bh_lock_sock(sk);
0157     if (nr->condition & NR_COND_ACK_PENDING) {
0158         nr->condition &= ~NR_COND_ACK_PENDING;
0159         nr_enquiry_response(sk);
0160     }
0161     bh_unlock_sock(sk);
0162     sock_put(sk);
0163 }
0164 
0165 static void nr_t4timer_expiry(struct timer_list *t)
0166 {
0167     struct nr_sock *nr = from_timer(nr, t, t4timer);
0168     struct sock *sk = &nr->sock;
0169 
0170     bh_lock_sock(sk);
0171     nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
0172     bh_unlock_sock(sk);
0173     sock_put(sk);
0174 }
0175 
0176 static void nr_idletimer_expiry(struct timer_list *t)
0177 {
0178     struct nr_sock *nr = from_timer(nr, t, idletimer);
0179     struct sock *sk = &nr->sock;
0180 
0181     bh_lock_sock(sk);
0182 
0183     nr_clear_queues(sk);
0184 
0185     nr->n2count = 0;
0186     nr_write_internal(sk, NR_DISCREQ);
0187     nr->state = NR_STATE_2;
0188 
0189     nr_start_t1timer(sk);
0190     nr_stop_t2timer(sk);
0191     nr_stop_t4timer(sk);
0192 
0193     sk->sk_state     = TCP_CLOSE;
0194     sk->sk_err       = 0;
0195     sk->sk_shutdown |= SEND_SHUTDOWN;
0196 
0197     if (!sock_flag(sk, SOCK_DEAD)) {
0198         sk->sk_state_change(sk);
0199         sock_set_flag(sk, SOCK_DEAD);
0200     }
0201     bh_unlock_sock(sk);
0202     sock_put(sk);
0203 }
0204 
0205 static void nr_t1timer_expiry(struct timer_list *t)
0206 {
0207     struct nr_sock *nr = from_timer(nr, t, t1timer);
0208     struct sock *sk = &nr->sock;
0209 
0210     bh_lock_sock(sk);
0211     switch (nr->state) {
0212     case NR_STATE_1:
0213         if (nr->n2count == nr->n2) {
0214             nr_disconnect(sk, ETIMEDOUT);
0215             goto out;
0216         } else {
0217             nr->n2count++;
0218             nr_write_internal(sk, NR_CONNREQ);
0219         }
0220         break;
0221 
0222     case NR_STATE_2:
0223         if (nr->n2count == nr->n2) {
0224             nr_disconnect(sk, ETIMEDOUT);
0225             goto out;
0226         } else {
0227             nr->n2count++;
0228             nr_write_internal(sk, NR_DISCREQ);
0229         }
0230         break;
0231 
0232     case NR_STATE_3:
0233         if (nr->n2count == nr->n2) {
0234             nr_disconnect(sk, ETIMEDOUT);
0235             goto out;
0236         } else {
0237             nr->n2count++;
0238             nr_requeue_frames(sk);
0239         }
0240         break;
0241     }
0242 
0243     nr_start_t1timer(sk);
0244 out:
0245     bh_unlock_sock(sk);
0246     sock_put(sk);
0247 }