linux/net/ipv4/tcp_timer.c
<<
>>
Prefs
   1/*
   2 * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3 *              operating system.  INET is implemented using the  BSD Socket
   4 *              interface as the means of communication with the user level.
   5 *
   6 *              Implementation of the Transmission Control Protocol(TCP).
   7 *
   8 * Authors:     Ross Biro
   9 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10 *              Mark Evans, <evansmp@uhura.aston.ac.uk>
  11 *              Corey Minyard <wf-rch!minyard@relay.EU.net>
  12 *              Florian La Roche, <flla@stud.uni-sb.de>
  13 *              Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  14 *              Linus Torvalds, <torvalds@cs.helsinki.fi>
  15 *              Alan Cox, <gw4pts@gw4pts.ampr.org>
  16 *              Matthew Dillon, <dillon@apollo.west.oic.com>
  17 *              Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  18 *              Jorge Cwik, <jorge@laser.satlink.net>
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/gfp.h>
  23#include <net/tcp.h>
  24
  25/**
  26 *  tcp_write_err() - close socket and save error info
  27 *  @sk:  The socket the error has appeared on.
  28 *
  29 *  Returns: Nothing (void)
  30 */
  31
  32static void tcp_write_err(struct sock *sk)
  33{
  34        sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
  35        sk->sk_error_report(sk);
  36
  37        tcp_done(sk);
  38        __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
  39}
  40
  41/**
  42 *  tcp_out_of_resources() - Close socket if out of resources
  43 *  @sk:        pointer to current socket
  44 *  @do_reset:  send a last packet with reset flag
  45 *
  46 *  Do not allow orphaned sockets to eat all our resources.
  47 *  This is direct violation of TCP specs, but it is required
  48 *  to prevent DoS attacks. It is called when a retransmission timeout
  49 *  or zero probe timeout occurs on orphaned socket.
  50 *
  51 *  Also close if our net namespace is exiting; in that case there is no
  52 *  hope of ever communicating again since all netns interfaces are already
  53 *  down (or about to be down), and we need to release our dst references,
  54 *  which have been moved to the netns loopback interface, so the namespace
  55 *  can finish exiting.  This condition is only possible if we are a kernel
  56 *  socket, as those do not hold references to the namespace.
  57 *
  58 *  Criteria is still not confirmed experimentally and may change.
  59 *  We kill the socket, if:
  60 *  1. If number of orphaned sockets exceeds an administratively configured
  61 *     limit.
  62 *  2. If we have strong memory pressure.
  63 *  3. If our net namespace is exiting.
  64 */
  65static int tcp_out_of_resources(struct sock *sk, bool do_reset)
  66{
  67        struct tcp_sock *tp = tcp_sk(sk);
  68        int shift = 0;
  69
  70        /* If peer does not open window for long time, or did not transmit
  71         * anything for long time, penalize it. */
  72        if ((s32)(tcp_jiffies32 - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
  73                shift++;
  74
  75        /* If some dubious ICMP arrived, penalize even more. */
  76        if (sk->sk_err_soft)
  77                shift++;
  78
  79        if (tcp_check_oom(sk, shift)) {
  80                /* Catch exceptional cases, when connection requires reset.
  81                 *      1. Last segment was sent recently. */
  82                if ((s32)(tcp_jiffies32 - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
  83                    /*  2. Window is closed. */
  84                    (!tp->snd_wnd && !tp->packets_out))
  85                        do_reset = true;
  86                if (do_reset)
  87                        tcp_send_active_reset(sk, GFP_ATOMIC);
  88                tcp_done(sk);
  89                __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
  90                return 1;
  91        }
  92
  93        if (!check_net(sock_net(sk))) {
  94                /* Not possible to send reset; just close */
  95                tcp_done(sk);
  96                return 1;
  97        }
  98
  99        return 0;
 100}
 101
 102/**
 103 *  tcp_orphan_retries() - Returns maximal number of retries on an orphaned socket
 104 *  @sk:    Pointer to the current socket.
 105 *  @alive: bool, socket alive state
 106 */
 107static int tcp_orphan_retries(struct sock *sk, bool alive)
 108{
 109        int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries; /* May be zero. */
 110
 111        /* We know from an ICMP that something is wrong. */
 112        if (sk->sk_err_soft && !alive)
 113                retries = 0;
 114
 115        /* However, if socket sent something recently, select some safe
 116         * number of retries. 8 corresponds to >100 seconds with minimal
 117         * RTO of 200msec. */
 118        if (retries == 0 && alive)
 119                retries = 8;
 120        return retries;
 121}
 122
 123static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
 124{
 125        const struct net *net = sock_net(sk);
 126        int mss;
 127
 128        /* Black hole detection */
 129        if (!net->ipv4.sysctl_tcp_mtu_probing)
 130                return;
 131
 132        if (!icsk->icsk_mtup.enabled) {
 133                icsk->icsk_mtup.enabled = 1;
 134                icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
 135        } else {
 136                mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
 137                mss = min(net->ipv4.sysctl_tcp_base_mss, mss);
 138                mss = max(mss, 68 - tcp_sk(sk)->tcp_header_len);
 139                icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
 140        }
 141        tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
 142}
 143
 144
 145/**
 146 *  retransmits_timed_out() - returns true if this connection has timed out
 147 *  @sk:       The current socket
 148 *  @boundary: max number of retransmissions
 149 *  @timeout:  A custom timeout value.
 150 *             If set to 0 the default timeout is calculated and used.
 151 *             Using TCP_RTO_MIN and the number of unsuccessful retransmits.
 152 *
 153 * The default "timeout" value this function can calculate and use
 154 * is equivalent to the timeout of a TCP Connection
 155 * after "boundary" unsuccessful, exponentially backed-off
 156 * retransmissions with an initial RTO of TCP_RTO_MIN.
 157 */
 158static bool retransmits_timed_out(struct sock *sk,
 159                                  unsigned int boundary,
 160                                  unsigned int timeout)
 161{
 162        const unsigned int rto_base = TCP_RTO_MIN;
 163        unsigned int linear_backoff_thresh, start_ts;
 164
 165        if (!inet_csk(sk)->icsk_retransmits)
 166                return false;
 167
 168        start_ts = tcp_sk(sk)->retrans_stamp;
 169        if (unlikely(!start_ts)) {
 170                struct sk_buff *head = tcp_rtx_queue_head(sk);
 171
 172                if (!head)
 173                        return false;
 174                start_ts = tcp_skb_timestamp(head);
 175        }
 176
 177        if (likely(timeout == 0)) {
 178                linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
 179
 180                if (boundary <= linear_backoff_thresh)
 181                        timeout = ((2 << boundary) - 1) * rto_base;
 182                else
 183                        timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
 184                                (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
 185        }
 186        return (tcp_time_stamp(tcp_sk(sk)) - start_ts) >= jiffies_to_msecs(timeout);
 187}
 188
 189/* A write timeout has occurred. Process the after effects. */
 190static int tcp_write_timeout(struct sock *sk)
 191{
 192        struct inet_connection_sock *icsk = inet_csk(sk);
 193        struct tcp_sock *tp = tcp_sk(sk);
 194        struct net *net = sock_net(sk);
 195        bool expired, do_reset;
 196        int retry_until;
 197
 198        if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
 199                if (icsk->icsk_retransmits) {
 200                        dst_negative_advice(sk);
 201                        if (tp->syn_fastopen || tp->syn_data)
 202                                tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
 203                        if (tp->syn_data && icsk->icsk_retransmits == 1)
 204                                NET_INC_STATS(sock_net(sk),
 205                                              LINUX_MIB_TCPFASTOPENACTIVEFAIL);
 206                } else if (!tp->syn_data && !tp->syn_fastopen) {
 207                        sk_rethink_txhash(sk);
 208                }
 209                retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
 210                expired = icsk->icsk_retransmits >= retry_until;
 211        } else {
 212                if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0)) {
 213                        /* Some middle-boxes may black-hole Fast Open _after_
 214                         * the handshake. Therefore we conservatively disable
 215                         * Fast Open on this path on recurring timeouts after
 216                         * successful Fast Open.
 217                         */
 218                        if (tp->syn_data_acked) {
 219                                tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
 220                                if (icsk->icsk_retransmits == net->ipv4.sysctl_tcp_retries1)
 221                                        NET_INC_STATS(sock_net(sk),
 222                                                      LINUX_MIB_TCPFASTOPENACTIVEFAIL);
 223                        }
 224                        /* Black hole detection */
 225                        tcp_mtu_probing(icsk, sk);
 226
 227                        dst_negative_advice(sk);
 228                } else {
 229                        sk_rethink_txhash(sk);
 230                }
 231
 232                retry_until = net->ipv4.sysctl_tcp_retries2;
 233                if (sock_flag(sk, SOCK_DEAD)) {
 234                        const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
 235
 236                        retry_until = tcp_orphan_retries(sk, alive);
 237                        do_reset = alive ||
 238                                !retransmits_timed_out(sk, retry_until, 0);
 239
 240                        if (tcp_out_of_resources(sk, do_reset))
 241                                return 1;
 242                }
 243                expired = retransmits_timed_out(sk, retry_until,
 244                                                icsk->icsk_user_timeout);
 245        }
 246        if (expired) {
 247                /* Has it gone just too far? */
 248                tcp_write_err(sk);
 249                return 1;
 250        }
 251        return 0;
 252}
 253
 254/* Called with BH disabled */
 255void tcp_delack_timer_handler(struct sock *sk)
 256{
 257        struct inet_connection_sock *icsk = inet_csk(sk);
 258
 259        sk_mem_reclaim_partial(sk);
 260
 261        if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
 262            !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
 263                goto out;
 264
 265        if (time_after(icsk->icsk_ack.timeout, jiffies)) {
 266                sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
 267                goto out;
 268        }
 269        icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
 270
 271        if (inet_csk_ack_scheduled(sk)) {
 272                if (!icsk->icsk_ack.pingpong) {
 273                        /* Delayed ACK missed: inflate ATO. */
 274                        icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
 275                } else {
 276                        /* Delayed ACK missed: leave pingpong mode and
 277                         * deflate ATO.
 278                         */
 279                        icsk->icsk_ack.pingpong = 0;
 280                        icsk->icsk_ack.ato      = TCP_ATO_MIN;
 281                }
 282                tcp_mstamp_refresh(tcp_sk(sk));
 283                tcp_send_ack(sk);
 284                __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
 285        }
 286
 287out:
 288        if (tcp_under_memory_pressure(sk))
 289                sk_mem_reclaim(sk);
 290}
 291
 292
 293/**
 294 *  tcp_delack_timer() - The TCP delayed ACK timeout handler
 295 *  @data:  Pointer to the current socket. (gets casted to struct sock *)
 296 *
 297 *  This function gets (indirectly) called when the kernel timer for a TCP packet
 298 *  of this socket expires. Calls tcp_delack_timer_handler() to do the actual work.
 299 *
 300 *  Returns: Nothing (void)
 301 */
 302static void tcp_delack_timer(struct timer_list *t)
 303{
 304        struct inet_connection_sock *icsk =
 305                        from_timer(icsk, t, icsk_delack_timer);
 306        struct sock *sk = &icsk->icsk_inet.sk;
 307
 308        bh_lock_sock(sk);
 309        if (!sock_owned_by_user(sk)) {
 310                tcp_delack_timer_handler(sk);
 311        } else {
 312                icsk->icsk_ack.blocked = 1;
 313                __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
 314                /* deleguate our work to tcp_release_cb() */
 315                if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags))
 316                        sock_hold(sk);
 317        }
 318        bh_unlock_sock(sk);
 319        sock_put(sk);
 320}
 321
 322static void tcp_probe_timer(struct sock *sk)
 323{
 324        struct inet_connection_sock *icsk = inet_csk(sk);
 325        struct sk_buff *skb = tcp_send_head(sk);
 326        struct tcp_sock *tp = tcp_sk(sk);
 327        int max_probes;
 328        u32 start_ts;
 329
 330        if (tp->packets_out || !skb) {
 331                icsk->icsk_probes_out = 0;
 332                return;
 333        }
 334
 335        /* RFC 1122 4.2.2.17 requires the sender to stay open indefinitely as
 336         * long as the receiver continues to respond probes. We support this by
 337         * default and reset icsk_probes_out with incoming ACKs. But if the
 338         * socket is orphaned or the user specifies TCP_USER_TIMEOUT, we
 339         * kill the socket when the retry count and the time exceeds the
 340         * corresponding system limit. We also implement similar policy when
 341         * we use RTO to probe window in tcp_retransmit_timer().
 342         */
 343        start_ts = tcp_skb_timestamp(skb);
 344        if (!start_ts)
 345                skb->skb_mstamp = tp->tcp_mstamp;
 346        else if (icsk->icsk_user_timeout &&
 347                 (s32)(tcp_time_stamp(tp) - start_ts) >
 348                 jiffies_to_msecs(icsk->icsk_user_timeout))
 349                goto abort;
 350
 351        max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
 352        if (sock_flag(sk, SOCK_DEAD)) {
 353                const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
 354
 355                max_probes = tcp_orphan_retries(sk, alive);
 356                if (!alive && icsk->icsk_backoff >= max_probes)
 357                        goto abort;
 358                if (tcp_out_of_resources(sk, true))
 359                        return;
 360        }
 361
 362        if (icsk->icsk_probes_out > max_probes) {
 363abort:          tcp_write_err(sk);
 364        } else {
 365                /* Only send another probe if we didn't close things up. */
 366                tcp_send_probe0(sk);
 367        }
 368}
 369
 370/*
 371 *      Timer for Fast Open socket to retransmit SYNACK. Note that the
 372 *      sk here is the child socket, not the parent (listener) socket.
 373 */
 374static void tcp_fastopen_synack_timer(struct sock *sk)
 375{
 376        struct inet_connection_sock *icsk = inet_csk(sk);
 377        int max_retries = icsk->icsk_syn_retries ? :
 378            sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1; /* add one more retry for fastopen */
 379        struct request_sock *req;
 380
 381        req = tcp_sk(sk)->fastopen_rsk;
 382        req->rsk_ops->syn_ack_timeout(req);
 383
 384        if (req->num_timeout >= max_retries) {
 385                tcp_write_err(sk);
 386                return;
 387        }
 388        /* XXX (TFO) - Unlike regular SYN-ACK retransmit, we ignore error
 389         * returned from rtx_syn_ack() to make it more persistent like
 390         * regular retransmit because if the child socket has been accepted
 391         * it's not good to give up too easily.
 392         */
 393        inet_rtx_syn_ack(sk, req);
 394        req->num_timeout++;
 395        icsk->icsk_retransmits++;
 396        inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
 397                          TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
 398}
 399
 400
 401/**
 402 *  tcp_retransmit_timer() - The TCP retransmit timeout handler
 403 *  @sk:  Pointer to the current socket.
 404 *
 405 *  This function gets called when the kernel timer for a TCP packet
 406 *  of this socket expires.
 407 *
 408 *  It handles retransmission, timer adjustment and other necesarry measures.
 409 *
 410 *  Returns: Nothing (void)
 411 */
 412void tcp_retransmit_timer(struct sock *sk)
 413{
 414        struct tcp_sock *tp = tcp_sk(sk);
 415        struct net *net = sock_net(sk);
 416        struct inet_connection_sock *icsk = inet_csk(sk);
 417
 418        if (tp->fastopen_rsk) {
 419                WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
 420                             sk->sk_state != TCP_FIN_WAIT1);
 421                tcp_fastopen_synack_timer(sk);
 422                /* Before we receive ACK to our SYN-ACK don't retransmit
 423                 * anything else (e.g., data or FIN segments).
 424                 */
 425                return;
 426        }
 427        if (!tp->packets_out)
 428                goto out;
 429
 430        WARN_ON(tcp_rtx_queue_empty(sk));
 431
 432        tp->tlp_high_seq = 0;
 433
 434        if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
 435            !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
 436                /* Receiver dastardly shrinks window. Our retransmits
 437                 * become zero probes, but we should not timeout this
 438                 * connection. If the socket is an orphan, time it out,
 439                 * we cannot allow such beasts to hang infinitely.
 440                 */
 441                struct inet_sock *inet = inet_sk(sk);
 442                if (sk->sk_family == AF_INET) {
 443                        net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
 444                                            &inet->inet_daddr,
 445                                            ntohs(inet->inet_dport),
 446                                            inet->inet_num,
 447                                            tp->snd_una, tp->snd_nxt);
 448                }
 449#if IS_ENABLED(CONFIG_IPV6)
 450                else if (sk->sk_family == AF_INET6) {
 451                        net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
 452                                            &sk->sk_v6_daddr,
 453                                            ntohs(inet->inet_dport),
 454                                            inet->inet_num,
 455                                            tp->snd_una, tp->snd_nxt);
 456                }
 457#endif
 458                if (tcp_jiffies32 - tp->rcv_tstamp > TCP_RTO_MAX) {
 459                        tcp_write_err(sk);
 460                        goto out;
 461                }
 462                tcp_enter_loss(sk);
 463                tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1);
 464                __sk_dst_reset(sk);
 465                goto out_reset_timer;
 466        }
 467
 468        if (tcp_write_timeout(sk))
 469                goto out;
 470
 471        if (icsk->icsk_retransmits == 0) {
 472                int mib_idx;
 473
 474                if (icsk->icsk_ca_state == TCP_CA_Recovery) {
 475                        if (tcp_is_sack(tp))
 476                                mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
 477                        else
 478                                mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
 479                } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
 480                        mib_idx = LINUX_MIB_TCPLOSSFAILURES;
 481                } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
 482                           tp->sacked_out) {
 483                        if (tcp_is_sack(tp))
 484                                mib_idx = LINUX_MIB_TCPSACKFAILURES;
 485                        else
 486                                mib_idx = LINUX_MIB_TCPRENOFAILURES;
 487                } else {
 488                        mib_idx = LINUX_MIB_TCPTIMEOUTS;
 489                }
 490                __NET_INC_STATS(sock_net(sk), mib_idx);
 491        }
 492
 493        tcp_enter_loss(sk);
 494
 495        if (tcp_retransmit_skb(sk, tcp_rtx_queue_head(sk), 1) > 0) {
 496                /* Retransmission failed because of local congestion,
 497                 * do not backoff.
 498                 */
 499                if (!icsk->icsk_retransmits)
 500                        icsk->icsk_retransmits = 1;
 501                inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
 502                                          min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
 503                                          TCP_RTO_MAX);
 504                goto out;
 505        }
 506
 507        /* Increase the timeout each time we retransmit.  Note that
 508         * we do not increase the rtt estimate.  rto is initialized
 509         * from rtt, but increases here.  Jacobson (SIGCOMM 88) suggests
 510         * that doubling rto each time is the least we can get away with.
 511         * In KA9Q, Karn uses this for the first few times, and then
 512         * goes to quadratic.  netBSD doubles, but only goes up to *64,
 513         * and clamps at 1 to 64 sec afterwards.  Note that 120 sec is
 514         * defined in the protocol as the maximum possible RTT.  I guess
 515         * we'll have to use something other than TCP to talk to the
 516         * University of Mars.
 517         *
 518         * PAWS allows us longer timeouts and large windows, so once
 519         * implemented ftp to mars will work nicely. We will have to fix
 520         * the 120 second clamps though!
 521         */
 522        icsk->icsk_backoff++;
 523        icsk->icsk_retransmits++;
 524
 525out_reset_timer:
 526        /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
 527         * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
 528         * might be increased if the stream oscillates between thin and thick,
 529         * thus the old value might already be too high compared to the value
 530         * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
 531         * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
 532         * exponential backoff behaviour to avoid continue hammering
 533         * linear-timeout retransmissions into a black hole
 534         */
 535        if (sk->sk_state == TCP_ESTABLISHED &&
 536            (tp->thin_lto || net->ipv4.sysctl_tcp_thin_linear_timeouts) &&
 537            tcp_stream_is_thin(tp) &&
 538            icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
 539                icsk->icsk_backoff = 0;
 540                icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
 541        } else {
 542                /* Use normal (exponential) backoff */
 543                icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
 544        }
 545        inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
 546        if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0))
 547                __sk_dst_reset(sk);
 548
 549out:;
 550}
 551
 552/* Called with bottom-half processing disabled.
 553   Called by tcp_write_timer() */
 554void tcp_write_timer_handler(struct sock *sk)
 555{
 556        struct inet_connection_sock *icsk = inet_csk(sk);
 557        int event;
 558
 559        if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
 560            !icsk->icsk_pending)
 561                goto out;
 562
 563        if (time_after(icsk->icsk_timeout, jiffies)) {
 564                sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
 565                goto out;
 566        }
 567
 568        tcp_mstamp_refresh(tcp_sk(sk));
 569        event = icsk->icsk_pending;
 570
 571        switch (event) {
 572        case ICSK_TIME_REO_TIMEOUT:
 573                tcp_rack_reo_timeout(sk);
 574                break;
 575        case ICSK_TIME_LOSS_PROBE:
 576                tcp_send_loss_probe(sk);
 577                break;
 578        case ICSK_TIME_RETRANS:
 579                icsk->icsk_pending = 0;
 580                tcp_retransmit_timer(sk);
 581                break;
 582        case ICSK_TIME_PROBE0:
 583                icsk->icsk_pending = 0;
 584                tcp_probe_timer(sk);
 585                break;
 586        }
 587
 588out:
 589        sk_mem_reclaim(sk);
 590}
 591
 592static void tcp_write_timer(struct timer_list *t)
 593{
 594        struct inet_connection_sock *icsk =
 595                        from_timer(icsk, t, icsk_retransmit_timer);
 596        struct sock *sk = &icsk->icsk_inet.sk;
 597
 598        bh_lock_sock(sk);
 599        if (!sock_owned_by_user(sk)) {
 600                tcp_write_timer_handler(sk);
 601        } else {
 602                /* delegate our work to tcp_release_cb() */
 603                if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &sk->sk_tsq_flags))
 604                        sock_hold(sk);
 605        }
 606        bh_unlock_sock(sk);
 607        sock_put(sk);
 608}
 609
 610void tcp_syn_ack_timeout(const struct request_sock *req)
 611{
 612        struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
 613
 614        __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
 615}
 616EXPORT_SYMBOL(tcp_syn_ack_timeout);
 617
 618void tcp_set_keepalive(struct sock *sk, int val)
 619{
 620        if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
 621                return;
 622
 623        if (val && !sock_flag(sk, SOCK_KEEPOPEN))
 624                inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
 625        else if (!val)
 626                inet_csk_delete_keepalive_timer(sk);
 627}
 628EXPORT_SYMBOL_GPL(tcp_set_keepalive);
 629
 630
 631static void tcp_keepalive_timer (struct timer_list *t)
 632{
 633        struct sock *sk = from_timer(sk, t, sk_timer);
 634        struct inet_connection_sock *icsk = inet_csk(sk);
 635        struct tcp_sock *tp = tcp_sk(sk);
 636        u32 elapsed;
 637
 638        /* Only process if socket is not in use. */
 639        bh_lock_sock(sk);
 640        if (sock_owned_by_user(sk)) {
 641                /* Try again later. */
 642                inet_csk_reset_keepalive_timer (sk, HZ/20);
 643                goto out;
 644        }
 645
 646        if (sk->sk_state == TCP_LISTEN) {
 647                pr_err("Hmm... keepalive on a LISTEN ???\n");
 648                goto out;
 649        }
 650
 651        tcp_mstamp_refresh(tp);
 652        if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
 653                if (tp->linger2 >= 0) {
 654                        const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
 655
 656                        if (tmo > 0) {
 657                                tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
 658                                goto out;
 659                        }
 660                }
 661                tcp_send_active_reset(sk, GFP_ATOMIC);
 662                goto death;
 663        }
 664
 665        if (!sock_flag(sk, SOCK_KEEPOPEN) ||
 666            ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
 667                goto out;
 668
 669        elapsed = keepalive_time_when(tp);
 670
 671        /* It is alive without keepalive 8) */
 672        if (tp->packets_out || !tcp_write_queue_empty(sk))
 673                goto resched;
 674
 675        elapsed = keepalive_time_elapsed(tp);
 676
 677        if (elapsed >= keepalive_time_when(tp)) {
 678                /* If the TCP_USER_TIMEOUT option is enabled, use that
 679                 * to determine when to timeout instead.
 680                 */
 681                if ((icsk->icsk_user_timeout != 0 &&
 682                    elapsed >= icsk->icsk_user_timeout &&
 683                    icsk->icsk_probes_out > 0) ||
 684                    (icsk->icsk_user_timeout == 0 &&
 685                    icsk->icsk_probes_out >= keepalive_probes(tp))) {
 686                        tcp_send_active_reset(sk, GFP_ATOMIC);
 687                        tcp_write_err(sk);
 688                        goto out;
 689                }
 690                if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
 691                        icsk->icsk_probes_out++;
 692                        elapsed = keepalive_intvl_when(tp);
 693                } else {
 694                        /* If keepalive was lost due to local congestion,
 695                         * try harder.
 696                         */
 697                        elapsed = TCP_RESOURCE_PROBE_INTERVAL;
 698                }
 699        } else {
 700                /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
 701                elapsed = keepalive_time_when(tp) - elapsed;
 702        }
 703
 704        sk_mem_reclaim(sk);
 705
 706resched:
 707        inet_csk_reset_keepalive_timer (sk, elapsed);
 708        goto out;
 709
 710death:
 711        tcp_done(sk);
 712
 713out:
 714        bh_unlock_sock(sk);
 715        sock_put(sk);
 716}
 717
 718void tcp_init_xmit_timers(struct sock *sk)
 719{
 720        inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
 721                                  &tcp_keepalive_timer);
 722        hrtimer_init(&tcp_sk(sk)->pacing_timer, CLOCK_MONOTONIC,
 723                     HRTIMER_MODE_ABS_PINNED);
 724        tcp_sk(sk)->pacing_timer.function = tcp_pace_kick;
 725}
 726
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.