Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * INET     An implementation of the TCP/IP protocol suite for the LINUX
0004  *      operating system.  INET is implemented using the  BSD Socket
0005  *      interface as the means of communication with the user level.
0006  *
0007  *      Implementation of the Transmission Control Protocol(TCP).
0008  *
0009  * Authors: Ross Biro
0010  *      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
0011  *      Mark Evans, <evansmp@uhura.aston.ac.uk>
0012  *      Corey Minyard <wf-rch!minyard@relay.EU.net>
0013  *      Florian La Roche, <flla@stud.uni-sb.de>
0014  *      Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
0015  *      Linus Torvalds, <torvalds@cs.helsinki.fi>
0016  *      Alan Cox, <gw4pts@gw4pts.ampr.org>
0017  *      Matthew Dillon, <dillon@apollo.west.oic.com>
0018  *      Arnt Gulbrandsen, <agulbra@nvg.unit.no>
0019  *      Jorge Cwik, <jorge@laser.satlink.net>
0020  */
0021 
0022 #include <linux/module.h>
0023 #include <linux/gfp.h>
0024 #include <net/tcp.h>
0025 
0026 static u32 tcp_clamp_rto_to_user_timeout(const struct sock *sk)
0027 {
0028     struct inet_connection_sock *icsk = inet_csk(sk);
0029     u32 elapsed, start_ts;
0030     s32 remaining;
0031 
0032     start_ts = tcp_sk(sk)->retrans_stamp;
0033     if (!icsk->icsk_user_timeout)
0034         return icsk->icsk_rto;
0035     elapsed = tcp_time_stamp(tcp_sk(sk)) - start_ts;
0036     remaining = icsk->icsk_user_timeout - elapsed;
0037     if (remaining <= 0)
0038         return 1; /* user timeout has passed; fire ASAP */
0039 
0040     return min_t(u32, icsk->icsk_rto, msecs_to_jiffies(remaining));
0041 }
0042 
0043 u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when)
0044 {
0045     struct inet_connection_sock *icsk = inet_csk(sk);
0046     u32 remaining;
0047     s32 elapsed;
0048 
0049     if (!icsk->icsk_user_timeout || !icsk->icsk_probes_tstamp)
0050         return when;
0051 
0052     elapsed = tcp_jiffies32 - icsk->icsk_probes_tstamp;
0053     if (unlikely(elapsed < 0))
0054         elapsed = 0;
0055     remaining = msecs_to_jiffies(icsk->icsk_user_timeout) - elapsed;
0056     remaining = max_t(u32, remaining, TCP_TIMEOUT_MIN);
0057 
0058     return min_t(u32, remaining, when);
0059 }
0060 
0061 /**
0062  *  tcp_write_err() - close socket and save error info
0063  *  @sk:  The socket the error has appeared on.
0064  *
0065  *  Returns: Nothing (void)
0066  */
0067 
0068 static void tcp_write_err(struct sock *sk)
0069 {
0070     sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
0071     sk_error_report(sk);
0072 
0073     tcp_write_queue_purge(sk);
0074     tcp_done(sk);
0075     __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
0076 }
0077 
0078 /**
0079  *  tcp_out_of_resources() - Close socket if out of resources
0080  *  @sk:        pointer to current socket
0081  *  @do_reset:  send a last packet with reset flag
0082  *
0083  *  Do not allow orphaned sockets to eat all our resources.
0084  *  This is direct violation of TCP specs, but it is required
0085  *  to prevent DoS attacks. It is called when a retransmission timeout
0086  *  or zero probe timeout occurs on orphaned socket.
0087  *
0088  *  Also close if our net namespace is exiting; in that case there is no
0089  *  hope of ever communicating again since all netns interfaces are already
0090  *  down (or about to be down), and we need to release our dst references,
0091  *  which have been moved to the netns loopback interface, so the namespace
0092  *  can finish exiting.  This condition is only possible if we are a kernel
0093  *  socket, as those do not hold references to the namespace.
0094  *
0095  *  Criteria is still not confirmed experimentally and may change.
0096  *  We kill the socket, if:
0097  *  1. If number of orphaned sockets exceeds an administratively configured
0098  *     limit.
0099  *  2. If we have strong memory pressure.
0100  *  3. If our net namespace is exiting.
0101  */
0102 static int tcp_out_of_resources(struct sock *sk, bool do_reset)
0103 {
0104     struct tcp_sock *tp = tcp_sk(sk);
0105     int shift = 0;
0106 
0107     /* If peer does not open window for long time, or did not transmit
0108      * anything for long time, penalize it. */
0109     if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
0110         shift++;
0111 
0112     /* If some dubious ICMP arrived, penalize even more. */
0113     if (sk->sk_err_soft)
0114         shift++;
0115 
0116     if (tcp_check_oom(sk, shift)) {
0117         /* Catch exceptional cases, when connection requires reset.
0118          *      1. Last segment was sent recently. */
0119         if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
0120             /*  2. Window is closed. */
0121             (!tp->snd_wnd && !tp->packets_out))
0122             do_reset = true;
0123         if (do_reset)
0124             tcp_send_active_reset(sk, GFP_ATOMIC);
0125         tcp_done(sk);
0126         __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
0127         return 1;
0128     }
0129 
0130     if (!check_net(sock_net(sk))) {
0131         /* Not possible to send reset; just close */
0132         tcp_done(sk);
0133         return 1;
0134     }
0135 
0136     return 0;
0137 }
0138 
0139 /**
0140  *  tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket
0141  *  @sk:    Pointer to the current socket.
0142  *  @alive: bool, socket alive state
0143  */
0144 static int tcp_orphan_retries(struct sock *sk, bool alive)
0145 {
0146     int retries = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_orphan_retries); /* May be zero. */
0147 
0148     /* We know from an ICMP that something is wrong. */
0149     if (sk->sk_err_soft && !alive)
0150         retries = 0;
0151 
0152     /* However, if socket sent something recently, select some safe
0153      * number of retries. 8 corresponds to >100 seconds with minimal
0154      * RTO of 200msec. */
0155     if (retries == 0 && alive)
0156         retries = 8;
0157     return retries;
0158 }
0159 
0160 static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
0161 {
0162     const struct net *net = sock_net(sk);
0163     int mss;
0164 
0165     /* Black hole detection */
0166     if (!READ_ONCE(net->ipv4.sysctl_tcp_mtu_probing))
0167         return;
0168 
0169     if (!icsk->icsk_mtup.enabled) {
0170         icsk->icsk_mtup.enabled = 1;
0171         icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
0172     } else {
0173         mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
0174         mss = min(READ_ONCE(net->ipv4.sysctl_tcp_base_mss), mss);
0175         mss = max(mss, READ_ONCE(net->ipv4.sysctl_tcp_mtu_probe_floor));
0176         mss = max(mss, READ_ONCE(net->ipv4.sysctl_tcp_min_snd_mss));
0177         icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
0178     }
0179     tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
0180 }
0181 
0182 static unsigned int tcp_model_timeout(struct sock *sk,
0183                       unsigned int boundary,
0184                       unsigned int rto_base)
0185 {
0186     unsigned int linear_backoff_thresh, timeout;
0187 
0188     linear_backoff_thresh = ilog2(TCP_RTO_MAX / rto_base);
0189     if (boundary <= linear_backoff_thresh)
0190         timeout = ((2 << boundary) - 1) * rto_base;
0191     else
0192         timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
0193             (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
0194     return jiffies_to_msecs(timeout);
0195 }
0196 /**
0197  *  retransmits_timed_out() - returns true if this connection has timed out
0198  *  @sk:       The current socket
0199  *  @boundary: max number of retransmissions
0200  *  @timeout:  A custom timeout value.
0201  *             If set to 0 the default timeout is calculated and used.
0202  *             Using TCP_RTO_MIN and the number of unsuccessful retransmits.
0203  *
0204  * The default "timeout" value this function can calculate and use
0205  * is equivalent to the timeout of a TCP Connection
0206  * after "boundary" unsuccessful, exponentially backed-off
0207  * retransmissions with an initial RTO of TCP_RTO_MIN.
0208  */
0209 static bool retransmits_timed_out(struct sock *sk,
0210                   unsigned int boundary,
0211                   unsigned int timeout)
0212 {
0213     unsigned int start_ts;
0214 
0215     if (!inet_csk(sk)->icsk_retransmits)
0216         return false;
0217 
0218     start_ts = tcp_sk(sk)->retrans_stamp;
0219     if (likely(timeout == 0)) {
0220         unsigned int rto_base = TCP_RTO_MIN;
0221 
0222         if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
0223             rto_base = tcp_timeout_init(sk);
0224         timeout = tcp_model_timeout(sk, boundary, rto_base);
0225     }
0226 
0227     return (s32)(tcp_time_stamp(tcp_sk(sk)) - start_ts - timeout) >= 0;
0228 }
0229 
0230 /* A write timeout has occurred. Process the after effects. */
0231 static int tcp_write_timeout(struct sock *sk)
0232 {
0233     struct inet_connection_sock *icsk = inet_csk(sk);
0234     struct tcp_sock *tp = tcp_sk(sk);
0235     struct net *net = sock_net(sk);
0236     bool expired = false, do_reset;
0237     int retry_until;
0238 
0239     if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
0240         if (icsk->icsk_retransmits)
0241             __dst_negative_advice(sk);
0242         retry_until = icsk->icsk_syn_retries ? :
0243             READ_ONCE(net->ipv4.sysctl_tcp_syn_retries);
0244         expired = icsk->icsk_retransmits >= retry_until;
0245     } else {
0246         if (retransmits_timed_out(sk, READ_ONCE(net->ipv4.sysctl_tcp_retries1), 0)) {
0247             /* Black hole detection */
0248             tcp_mtu_probing(icsk, sk);
0249 
0250             __dst_negative_advice(sk);
0251         }
0252 
0253         retry_until = READ_ONCE(net->ipv4.sysctl_tcp_retries2);
0254         if (sock_flag(sk, SOCK_DEAD)) {
0255             const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
0256 
0257             retry_until = tcp_orphan_retries(sk, alive);
0258             do_reset = alive ||
0259                 !retransmits_timed_out(sk, retry_until, 0);
0260 
0261             if (tcp_out_of_resources(sk, do_reset))
0262                 return 1;
0263         }
0264     }
0265     if (!expired)
0266         expired = retransmits_timed_out(sk, retry_until,
0267                         icsk->icsk_user_timeout);
0268     tcp_fastopen_active_detect_blackhole(sk, expired);
0269 
0270     if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RTO_CB_FLAG))
0271         tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RTO_CB,
0272                   icsk->icsk_retransmits,
0273                   icsk->icsk_rto, (int)expired);
0274 
0275     if (expired) {
0276         /* Has it gone just too far? */
0277         tcp_write_err(sk);
0278         return 1;
0279     }
0280 
0281     if (sk_rethink_txhash(sk)) {
0282         tp->timeout_rehash++;
0283         __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTREHASH);
0284     }
0285 
0286     return 0;
0287 }
0288 
0289 /* Called with BH disabled */
0290 void tcp_delack_timer_handler(struct sock *sk)
0291 {
0292     struct inet_connection_sock *icsk = inet_csk(sk);
0293 
0294     if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
0295         !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
0296         return;
0297 
0298     if (time_after(icsk->icsk_ack.timeout, jiffies)) {
0299         sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
0300         return;
0301     }
0302     icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
0303 
0304     if (inet_csk_ack_scheduled(sk)) {
0305         if (!inet_csk_in_pingpong_mode(sk)) {
0306             /* Delayed ACK missed: inflate ATO. */
0307             icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
0308         } else {
0309             /* Delayed ACK missed: leave pingpong mode and
0310              * deflate ATO.
0311              */
0312             inet_csk_exit_pingpong_mode(sk);
0313             icsk->icsk_ack.ato      = TCP_ATO_MIN;
0314         }
0315         tcp_mstamp_refresh(tcp_sk(sk));
0316         tcp_send_ack(sk);
0317         __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
0318     }
0319 }
0320 
0321 
0322 /**
0323  *  tcp_delack_timer() - The TCP delayed ACK timeout handler
0324  *  @t:  Pointer to the timer. (gets casted to struct sock *)
0325  *
0326  *  This function gets (indirectly) called when the kernel timer for a TCP packet
0327  *  of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
0328  *
0329  *  Returns: Nothing (void)
0330  */
0331 static void tcp_delack_timer(struct timer_list *t)
0332 {
0333     struct inet_connection_sock *icsk =
0334             from_timer(icsk, t, icsk_delack_timer);
0335     struct sock *sk = &icsk->icsk_inet.sk;
0336 
0337     bh_lock_sock(sk);
0338     if (!sock_owned_by_user(sk)) {
0339         tcp_delack_timer_handler(sk);
0340     } else {
0341         __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
0342         /* deleguate our work to tcp_release_cb() */
0343         if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
0344             sock_hold(sk);
0345     }
0346     bh_unlock_sock(sk);
0347     sock_put(sk);
0348 }
0349 
0350 static void tcp_probe_timer(struct sock *sk)
0351 {
0352     struct inet_connection_sock *icsk = inet_csk(sk);
0353     struct sk_buff *skb = tcp_send_head(sk);
0354     struct tcp_sock *tp = tcp_sk(sk);
0355     int max_probes;
0356 
0357     if (tp->packets_out || !skb) {
0358         icsk->icsk_probes_out = 0;
0359         icsk->icsk_probes_tstamp = 0;
0360         return;
0361     }
0362 
0363     /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
0364      * long as the receiver continues to respond probes. We support this by
0365      * default and reset icsk_probes_out with incoming ACKs. But if the
0366      * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
0367      * kill the socket when the retry count and the time exceeds the
0368      * corresponding system limit. We also implement similar policy when
0369      * we use RTO to probe window in tcp_retransmit_timer().
0370      */
0371     if (!icsk->icsk_probes_tstamp)
0372         icsk->icsk_probes_tstamp = tcp_jiffies32;
0373     else if (icsk->icsk_user_timeout &&
0374          (s32)(tcp_jiffies32 - icsk->icsk_probes_tstamp) >=
0375          msecs_to_jiffies(icsk->icsk_user_timeout))
0376         goto abort;
0377 
0378     max_probes = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retries2);
0379     if (sock_flag(sk, SOCK_DEAD)) {
0380         const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
0381 
0382         max_probes = tcp_orphan_retries(sk, alive);
0383         if (!alive && icsk->icsk_backoff >= max_probes)
0384             goto abort;
0385         if (tcp_out_of_resources(sk, true))
0386             return;
0387     }
0388 
0389     if (icsk->icsk_probes_out >= max_probes) {
0390 abort:      tcp_write_err(sk);
0391     } else {
0392         /* Only send another probe if we didn't close things up. */
0393         tcp_send_probe0(sk);
0394     }
0395 }
0396 
0397 /*
0398  *  Timer for Fast Open socket to retransmit SYNACK. Note that the
0399  *  sk here is the child socket, not the parent (listener) socket.
0400  */
0401 static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
0402 {
0403     struct inet_connection_sock *icsk = inet_csk(sk);
0404     struct tcp_sock *tp = tcp_sk(sk);
0405     int max_retries;
0406 
0407     req->rsk_ops->syn_ack_timeout(req);
0408 
0409     /* add one more retry for fastopen */
0410     max_retries = icsk->icsk_syn_retries ? :
0411         READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_synack_retries) + 1;
0412 
0413     if (req->num_timeout >= max_retries) {
0414         tcp_write_err(sk);
0415         return;
0416     }
0417     /* Lower cwnd after certain SYNACK timeout like tcp_init_transfer() */
0418     if (icsk->icsk_retransmits == 1)
0419         tcp_enter_loss(sk);
0420     /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
0421      * returned from rtx_syn_ack() to make it more persistent like
0422      * regular retransmit because if the child socket has been accepted
0423      * it's not good to give up too easily.
0424      */
0425     inet_rtx_syn_ack(sk, req);
0426     req->num_timeout++;
0427     icsk->icsk_retransmits++;
0428     if (!tp->retrans_stamp)
0429         tp->retrans_stamp = tcp_time_stamp(tp);
0430     inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
0431               TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
0432 }
0433 
0434 
0435 /**
0436  *  tcp_retransmit_timer() - The TCP retransmit timeout handler
0437  *  @sk:  Pointer to the current socket.
0438  *
0439  *  This function gets called when the kernel timer for a TCP packet
0440  *  of this socket expires.
0441  *
0442  *  It handles retransmission, timer adjustment and other necessary measures.
0443  *
0444  *  Returns: Nothing (void)
0445  */
0446 void tcp_retransmit_timer(struct sock *sk)
0447 {
0448     struct tcp_sock *tp = tcp_sk(sk);
0449     struct net *net = sock_net(sk);
0450     struct inet_connection_sock *icsk = inet_csk(sk);
0451     struct request_sock *req;
0452     struct sk_buff *skb;
0453 
0454     req = rcu_dereference_protected(tp->fastopen_rsk,
0455                     lockdep_sock_is_held(sk));
0456     if (req) {
0457         WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
0458                  sk->sk_state != TCP_FIN_WAIT1);
0459         tcp_fastopen_synack_timer(sk, req);
0460         /* Before we receive ACK to our SYN-ACK don't retransmit
0461          * anything else (e.g., data or FIN segments).
0462          */
0463         return;
0464     }
0465 
0466     if (!tp->packets_out)
0467         return;
0468 
0469     skb = tcp_rtx_queue_head(sk);
0470     if (WARN_ON_ONCE(!skb))
0471         return;
0472 
0473     tp->tlp_high_seq = 0;
0474 
0475     if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
0476         !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
0477         /* Receiver dastardly shrinks window. Our retransmits
0478          * become zero probes, but we should not timeout this
0479          * connection. If the socket is an orphan, time it out,
0480          * we cannot allow such beasts to hang infinitely.
0481          */
0482         struct inet_sock *inet = inet_sk(sk);
0483         if (sk->sk_family == AF_INET) {
0484             net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
0485                         &inet->inet_daddr,
0486                         ntohs(inet->inet_dport),
0487                         inet->inet_num,
0488                         tp->snd_una, tp->snd_nxt);
0489         }
0490 #if IS_ENABLED(CONFIG_IPV6)
0491         else if (sk->sk_family == AF_INET6) {
0492             net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
0493                         &sk->sk_v6_daddr,
0494                         ntohs(inet->inet_dport),
0495                         inet->inet_num,
0496                         tp->snd_una, tp->snd_nxt);
0497         }
0498 #endif
0499         if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
0500             tcp_write_err(sk);
0501             goto out;
0502         }
0503         tcp_enter_loss(sk);
0504         tcp_retransmit_skb(sk, skb, 1);
0505         __sk_dst_reset(sk);
0506         goto out_reset_timer;
0507     }
0508 
0509     __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
0510     if (tcp_write_timeout(sk))
0511         goto out;
0512 
0513     if (icsk->icsk_retransmits == 0) {
0514         int mib_idx = 0;
0515 
0516         if (icsk->icsk_ca_state == TCP_CA_Recovery) {
0517             if (tcp_is_sack(tp))
0518                 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
0519             else
0520                 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
0521         } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
0522             mib_idx = LINUX_MIB_TCPLOSSFAILURES;
0523         } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
0524                tp->sacked_out) {
0525             if (tcp_is_sack(tp))
0526                 mib_idx = LINUX_MIB_TCPSACKFAILURES;
0527             else
0528                 mib_idx = LINUX_MIB_TCPRENOFAILURES;
0529         }
0530         if (mib_idx)
0531             __NET_INC_STATS(sock_net(sk), mib_idx);
0532     }
0533 
0534     tcp_enter_loss(sk);
0535 
0536     icsk->icsk_retransmits++;
0537     if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
0538         /* Retransmission failed because of local congestion,
0539          * Let senders fight for local resources conservatively.
0540          */
0541         inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
0542                       TCP_RESOURCE_PROBE_INTERVAL,
0543                       TCP_RTO_MAX);
0544         goto out;
0545     }
0546 
0547     /* Increase the timeout each time we retransmit.  Note that
0548      * we do not increase the rtt estimate.  rto is initialized
0549      * from rtt, but increases here.  Jacobson (SIGCOMM 88) suggests
0550      * that doubling rto each time is the least we can get away with.
0551      * In KA9Q, Karn uses this for the first few times, and then
0552      * goes to quadratic.  netBSD doubles, but only goes up to *64,
0553      * and clamps at 1 to 64 sec afterwards.  Note that 120 sec is
0554      * defined in the protocol as the maximum possible RTT.  I guess
0555      * we'll have to use something other than TCP to talk to the
0556      * University of Mars.
0557      *
0558      * PAWS allows us longer timeouts and large windows, so once
0559      * implemented ftp to mars will work nicely. We will have to fix
0560      * the 120 second clamps though!
0561      */
0562     icsk->icsk_backoff++;
0563 
0564 out_reset_timer:
0565     /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
0566      * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
0567      * might be increased if the stream oscillates between thin and thick,
0568      * thus the old value might already be too high compared to the value
0569      * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
0570      * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
0571      * exponential backoff behaviour to avoid continue hammering
0572      * linear-timeout retransmissions into a black hole
0573      */
0574     if (sk->sk_state == TCP_ESTABLISHED &&
0575         (tp->thin_lto || READ_ONCE(net->ipv4.sysctl_tcp_thin_linear_timeouts)) &&
0576         tcp_stream_is_thin(tp) &&
0577         icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
0578         icsk->icsk_backoff = 0;
0579         icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
0580     } else {
0581         /* Use normal (exponential) backoff */
0582         icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
0583     }
0584     inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
0585                   tcp_clamp_rto_to_user_timeout(sk), TCP_RTO_MAX);
0586     if (retransmits_timed_out(sk, READ_ONCE(net->ipv4.sysctl_tcp_retries1) + 1, 0))
0587         __sk_dst_reset(sk);
0588 
0589 out:;
0590 }
0591 
0592 /* Called with bottom-half processing disabled.
0593    Called by tcp_write_timer() */
0594 void tcp_write_timer_handler(struct sock *sk)
0595 {
0596     struct inet_connection_sock *icsk = inet_csk(sk);
0597     int event;
0598 
0599     if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
0600         !icsk->icsk_pending)
0601         return;
0602 
0603     if (time_after(icsk->icsk_timeout, jiffies)) {
0604         sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
0605         return;
0606     }
0607 
0608     tcp_mstamp_refresh(tcp_sk(sk));
0609     event = icsk->icsk_pending;
0610 
0611     switch (event) {
0612     case ICSK_TIME_REO_TIMEOUT:
0613         tcp_rack_reo_timeout(sk);
0614         break;
0615     case ICSK_TIME_LOSS_PROBE:
0616         tcp_send_loss_probe(sk);
0617         break;
0618     case ICSK_TIME_RETRANS:
0619         icsk->icsk_pending = 0;
0620         tcp_retransmit_timer(sk);
0621         break;
0622     case ICSK_TIME_PROBE0:
0623         icsk->icsk_pending = 0;
0624         tcp_probe_timer(sk);
0625         break;
0626     }
0627 }
0628 
0629 static void tcp_write_timer(struct timer_list *t)
0630 {
0631     struct inet_connection_sock *icsk =
0632             from_timer(icsk, t, icsk_retransmit_timer);
0633     struct sock *sk = &icsk->icsk_inet.sk;
0634 
0635     bh_lock_sock(sk);
0636     if (!sock_owned_by_user(sk)) {
0637         tcp_write_timer_handler(sk);
0638     } else {
0639         /* delegate our work to tcp_release_cb() */
0640         if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
0641             sock_hold(sk);
0642     }
0643     bh_unlock_sock(sk);
0644     sock_put(sk);
0645 }
0646 
0647 void tcp_syn_ack_timeout(const struct request_sock *req)
0648 {
0649     struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
0650 
0651     __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
0652 }
0653 EXPORT_SYMBOL(tcp_syn_ack_timeout);
0654 
0655 void tcp_set_keepalive(struct sock *sk, int val)
0656 {
0657     if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
0658         return;
0659 
0660     if (val && !sock_flag(sk, SOCK_KEEPOPEN))
0661         inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
0662     else if (!val)
0663         inet_csk_delete_keepalive_timer(sk);
0664 }
0665 EXPORT_SYMBOL_GPL(tcp_set_keepalive);
0666 
0667 
0668 static void tcp_keepalive_timer (struct timer_list *t)
0669 {
0670     struct sock *sk = from_timer(sk, t, sk_timer);
0671     struct inet_connection_sock *icsk = inet_csk(sk);
0672     struct tcp_sock *tp = tcp_sk(sk);
0673     u32 elapsed;
0674 
0675     /* Only process if socket is not in use. */
0676     bh_lock_sock(sk);
0677     if (sock_owned_by_user(sk)) {
0678         /* Try again later. */
0679         inet_csk_reset_keepalive_timer (sk, HZ/20);
0680         goto out;
0681     }
0682 
0683     if (sk->sk_state == TCP_LISTEN) {
0684         pr_err("Hmm... keepalive on a LISTEN ???\n");
0685         goto out;
0686     }
0687 
0688     tcp_mstamp_refresh(tp);
0689     if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
0690         if (tp->linger2 >= 0) {
0691             const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
0692 
0693             if (tmo > 0) {
0694                 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
0695                 goto out;
0696             }
0697         }
0698         tcp_send_active_reset(sk, GFP_ATOMIC);
0699         goto death;
0700     }
0701 
0702     if (!sock_flag(sk, SOCK_KEEPOPEN) ||
0703         ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
0704         goto out;
0705 
0706     elapsed = keepalive_time_when(tp);
0707 
0708     /* It is alive without keepalive 8) */
0709     if (tp->packets_out || !tcp_write_queue_empty(sk))
0710         goto resched;
0711 
0712     elapsed = keepalive_time_elapsed(tp);
0713 
0714     if (elapsed >= keepalive_time_when(tp)) {
0715         /* If the TCP_USER_TIMEOUT option is enabled, use that
0716          * to determine when to timeout instead.
0717          */
0718         if ((icsk->icsk_user_timeout != 0 &&
0719             elapsed >= msecs_to_jiffies(icsk->icsk_user_timeout) &&
0720             icsk->icsk_probes_out > 0) ||
0721             (icsk->icsk_user_timeout == 0 &&
0722             icsk->icsk_probes_out >= keepalive_probes(tp))) {
0723             tcp_send_active_reset(sk, GFP_ATOMIC);
0724             tcp_write_err(sk);
0725             goto out;
0726         }
0727         if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
0728             icsk->icsk_probes_out++;
0729             elapsed = keepalive_intvl_when(tp);
0730         } else {
0731             /* If keepalive was lost due to local congestion,
0732              * try harder.
0733              */
0734             elapsed = TCP_RESOURCE_PROBE_INTERVAL;
0735         }
0736     } else {
0737         /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
0738         elapsed = keepalive_time_when(tp) - elapsed;
0739     }
0740 
0741 resched:
0742     inet_csk_reset_keepalive_timer (sk, elapsed);
0743     goto out;
0744 
0745 death:
0746     tcp_done(sk);
0747 
0748 out:
0749     bh_unlock_sock(sk);
0750     sock_put(sk);
0751 }
0752 
0753 static enum hrtimer_restart tcp_compressed_ack_kick(struct hrtimer *timer)
0754 {
0755     struct tcp_sock *tp = container_of(timer, struct tcp_sock, compressed_ack_timer);
0756     struct sock *sk = (struct sock *)tp;
0757 
0758     bh_lock_sock(sk);
0759     if (!sock_owned_by_user(sk)) {
0760         if (tp->compressed_ack) {
0761             /* Since we have to send one ack finally,
0762              * subtract one from tp->compressed_ack to keep
0763              * LINUX_MIB_TCPACKCOMPRESSED accurate.
0764              */
0765             tp->compressed_ack--;
0766             tcp_send_ack(sk);
0767         }
0768     } else {
0769         if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
0770                       &sk->sk_tsq_flags))
0771             sock_hold(sk);
0772     }
0773     bh_unlock_sock(sk);
0774 
0775     sock_put(sk);
0776 
0777     return HRTIMER_NORESTART;
0778 }
0779 
0780 void tcp_init_xmit_timers(struct sock *sk)
0781 {
0782     inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
0783                   &tcp_keepalive_timer);
0784     hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
0785              HRTIMER_MODE_ABS_PINNED_SOFT);
0786     tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
0787 
0788     hrtimer_init(&tcp_sk(sk)->compressed_ack_timer, CLOCK_MONOTONIC,
0789              HRTIMER_MODE_REL_PINNED_SOFT);
0790     tcp_sk(sk)->compressed_ack_timer.function = tcp_compressed_ack_kick;
0791 }