^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * DECnet An implementation of the DECnet protocol suite for the LINUX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * operating system. DECnet is implemented using the BSD Socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * interface as the means of communication with the user level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * DECnet Socket Timer Functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Steve Whitehouse <SteveW@ACM.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Steve Whitehouse : Made keepalive timer part of the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * timer idea.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Steve Whitehouse : Added checks for sk->sock_readers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * David S. Miller : New socket locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Steve Whitehouse : Timer grabs socket ref.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <net/flow.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <net/dn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Slow timer is for everything else (n * 500mS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define SLOW_INTERVAL (HZ/2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static void dn_slow_timer(struct timer_list *t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void dn_start_slow_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) timer_setup(&sk->sk_timer, dn_slow_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) sk_reset_timer(sk, &sk->sk_timer, jiffies + SLOW_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void dn_stop_slow_timer(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) sk_stop_timer(sk, &sk->sk_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void dn_slow_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct sock *sk = from_timer(sk, t, sk_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct dn_scp *scp = DN_SK(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (sock_owned_by_user(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * The persist timer is the standard slow timer used for retransmits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * in both connection establishment and disconnection as well as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * in the RUN state. The different states are catered for by changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * the function pointer in the socket. Setting the timer to a value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * of zero turns it off. We allow the persist_fxn to turn the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * timer off in a permant way by returning non-zero, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * timer based routines may remove sockets. This is why we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * sock_hold()/sock_put() around the timer to prevent the socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * going away in the middle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (scp->persist && scp->persist_fxn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (scp->persist <= SLOW_INTERVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) scp->persist = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (scp->persist_fxn(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) scp->persist -= SLOW_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Check for keepalive timeout. After the other timer 'cos if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * the previous timer caused a retransmit, we don't need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * do this. scp->stamp is the last time that we sent a packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * The keepalive function sends a link service packet to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * other end. If it remains unacknowledged, the standard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * socket timers will eventually shut the socket down. Each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * time we do this, scp->stamp will be updated, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * we won't try and send another until scp->keepalive has passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * since the last successful transmission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (scp->keepalive && scp->keepalive_fxn && (scp->state == DN_RUN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (time_after_eq(jiffies, scp->stamp + scp->keepalive))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) scp->keepalive_fxn(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) sk_reset_timer(sk, &sk->sk_timer, jiffies + SLOW_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }