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