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