123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- #include <linux/module.h>
- #include <linux/gfp.h>
- #include <net/tcp.h>
- int sysctl_tcp_thin_linear_timeouts __read_mostly;
- static void tcp_write_err(struct sock *sk)
- {
- sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
- sk->sk_error_report(sk);
- tcp_done(sk);
- __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
- }
- static int tcp_out_of_resources(struct sock *sk, bool do_reset)
- {
- struct tcp_sock *tp = tcp_sk(sk);
- int shift = 0;
-
- if ((s32)(tcp_time_stamp - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
- shift++;
-
- if (sk->sk_err_soft)
- shift++;
- if (tcp_check_oom(sk, shift)) {
-
- if ((s32)(tcp_time_stamp - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
-
- (!tp->snd_wnd && !tp->packets_out))
- do_reset = true;
- if (do_reset)
- tcp_send_active_reset(sk, GFP_ATOMIC);
- tcp_done(sk);
- __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
- return 1;
- }
- if (!check_net(sock_net(sk))) {
-
- tcp_done(sk);
- return 1;
- }
- return 0;
- }
- static int tcp_orphan_retries(struct sock *sk, bool alive)
- {
- int retries = sock_net(sk)->ipv4.sysctl_tcp_orphan_retries;
-
- if (sk->sk_err_soft && !alive)
- retries = 0;
-
- if (retries == 0 && alive)
- retries = 8;
- return retries;
- }
- static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
- {
- struct net *net = sock_net(sk);
-
- if (net->ipv4.sysctl_tcp_mtu_probing) {
- if (!icsk->icsk_mtup.enabled) {
- icsk->icsk_mtup.enabled = 1;
- icsk->icsk_mtup.probe_timestamp = tcp_time_stamp;
- tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
- } else {
- struct net *net = sock_net(sk);
- struct tcp_sock *tp = tcp_sk(sk);
- int mss;
- mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
- mss = min(net->ipv4.sysctl_tcp_base_mss, mss);
- mss = max(mss, 68 - tp->tcp_header_len);
- icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
- tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
- }
- }
- }
- static bool retransmits_timed_out(struct sock *sk,
- unsigned int boundary,
- unsigned int timeout,
- bool syn_set)
- {
- unsigned int linear_backoff_thresh, start_ts;
- unsigned int rto_base = syn_set ? TCP_TIMEOUT_INIT : TCP_RTO_MIN;
- if (!inet_csk(sk)->icsk_retransmits)
- return false;
- start_ts = tcp_sk(sk)->retrans_stamp;
- if (unlikely(!start_ts))
- start_ts = tcp_skb_timestamp(tcp_write_queue_head(sk));
- if (likely(timeout == 0)) {
- linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
- if (boundary <= linear_backoff_thresh)
- timeout = ((2 << boundary) - 1) * rto_base;
- else
- timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
- (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
- }
- return (tcp_time_stamp - start_ts) >= timeout;
- }
- static int tcp_write_timeout(struct sock *sk)
- {
- struct inet_connection_sock *icsk = inet_csk(sk);
- struct tcp_sock *tp = tcp_sk(sk);
- struct net *net = sock_net(sk);
- int retry_until;
- bool do_reset, syn_set = false;
- if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
- if (icsk->icsk_retransmits) {
- dst_negative_advice(sk);
- if (tp->syn_fastopen || tp->syn_data)
- tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
- if (tp->syn_data && icsk->icsk_retransmits == 1)
- NET_INC_STATS(sock_net(sk),
- LINUX_MIB_TCPFASTOPENACTIVEFAIL);
- } else if (!tp->syn_data && !tp->syn_fastopen) {
- sk_rethink_txhash(sk);
- }
- retry_until = icsk->icsk_syn_retries ? : net->ipv4.sysctl_tcp_syn_retries;
- syn_set = true;
- } else {
- if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1, 0, 0)) {
-
- if (tp->syn_data_acked &&
- tp->bytes_acked <= tp->rx_opt.mss_clamp) {
- tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
- if (icsk->icsk_retransmits == net->ipv4.sysctl_tcp_retries1)
- NET_INC_STATS(sock_net(sk),
- LINUX_MIB_TCPFASTOPENACTIVEFAIL);
- }
-
- tcp_mtu_probing(icsk, sk);
- dst_negative_advice(sk);
- } else {
- sk_rethink_txhash(sk);
- }
- retry_until = net->ipv4.sysctl_tcp_retries2;
- if (sock_flag(sk, SOCK_DEAD)) {
- const bool alive = icsk->icsk_rto < TCP_RTO_MAX;
- retry_until = tcp_orphan_retries(sk, alive);
- do_reset = alive ||
- !retransmits_timed_out(sk, retry_until, 0, 0);
- if (tcp_out_of_resources(sk, do_reset))
- return 1;
- }
- }
- if (retransmits_timed_out(sk, retry_until,
- syn_set ? 0 : icsk->icsk_user_timeout, syn_set)) {
-
- tcp_write_err(sk);
- return 1;
- }
- return 0;
- }
- void tcp_delack_timer_handler(struct sock *sk)
- {
- struct tcp_sock *tp = tcp_sk(sk);
- struct inet_connection_sock *icsk = inet_csk(sk);
- sk_mem_reclaim_partial(sk);
- if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
- !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
- goto out;
- if (time_after(icsk->icsk_ack.timeout, jiffies)) {
- sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
- goto out;
- }
- icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
- if (!skb_queue_empty(&tp->ucopy.prequeue)) {
- struct sk_buff *skb;
- __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSCHEDULERFAILED);
- while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
- sk_backlog_rcv(sk, skb);
- tp->ucopy.memory = 0;
- }
- if (inet_csk_ack_scheduled(sk)) {
- if (!icsk->icsk_ack.pingpong) {
-
- icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
- } else {
-
- icsk->icsk_ack.pingpong = 0;
- icsk->icsk_ack.ato = TCP_ATO_MIN;
- }
- tcp_send_ack(sk);
- __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKS);
- }
- out:
- if (tcp_under_memory_pressure(sk))
- sk_mem_reclaim(sk);
- }
- static void tcp_delack_timer(unsigned long data)
- {
- struct sock *sk = (struct sock *)data;
- bh_lock_sock(sk);
- if (!sock_owned_by_user(sk)) {
- tcp_delack_timer_handler(sk);
- } else {
- inet_csk(sk)->icsk_ack.blocked = 1;
- __NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
-
- if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &tcp_sk(sk)->tsq_flags))
- sock_hold(sk);
- }
- bh_unlock_sock(sk);
- sock_put(sk);
- }
- static void tcp_probe_timer(struct sock *sk)
- {
- struct inet_connection_sock *icsk = inet_csk(sk);
- struct tcp_sock *tp = tcp_sk(sk);
- int max_probes;
- u32 start_ts;
- if (tp->packets_out || !tcp_send_head(sk)) {
- icsk->icsk_probes_out = 0;
- return;
- }
-
- start_ts = tcp_skb_timestamp(tcp_send_head(sk));
- if (!start_ts)
- skb_mstamp_get(&tcp_send_head(sk)->skb_mstamp);
- else if (icsk->icsk_user_timeout &&
- (s32)(tcp_time_stamp - start_ts) > icsk->icsk_user_timeout)
- goto abort;
- max_probes = sock_net(sk)->ipv4.sysctl_tcp_retries2;
- if (sock_flag(sk, SOCK_DEAD)) {
- const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX;
- max_probes = tcp_orphan_retries(sk, alive);
- if (!alive && icsk->icsk_backoff >= max_probes)
- goto abort;
- if (tcp_out_of_resources(sk, true))
- return;
- }
- if (icsk->icsk_probes_out > max_probes) {
- abort: tcp_write_err(sk);
- } else {
-
- tcp_send_probe0(sk);
- }
- }
- static void tcp_fastopen_synack_timer(struct sock *sk)
- {
- struct inet_connection_sock *icsk = inet_csk(sk);
- int max_retries = icsk->icsk_syn_retries ? :
- sock_net(sk)->ipv4.sysctl_tcp_synack_retries + 1;
- struct request_sock *req;
- req = tcp_sk(sk)->fastopen_rsk;
- req->rsk_ops->syn_ack_timeout(req);
- if (req->num_timeout >= max_retries) {
- tcp_write_err(sk);
- return;
- }
-
- inet_rtx_syn_ack(sk, req);
- req->num_timeout++;
- icsk->icsk_retransmits++;
- inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
- TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
- }
- void tcp_retransmit_timer(struct sock *sk)
- {
- struct tcp_sock *tp = tcp_sk(sk);
- struct net *net = sock_net(sk);
- struct inet_connection_sock *icsk = inet_csk(sk);
- if (tp->fastopen_rsk) {
- WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
- sk->sk_state != TCP_FIN_WAIT1);
- tcp_fastopen_synack_timer(sk);
-
- return;
- }
- if (!tp->packets_out)
- goto out;
- WARN_ON(tcp_write_queue_empty(sk));
- tp->tlp_high_seq = 0;
- if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
- !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
-
- struct inet_sock *inet = inet_sk(sk);
- if (sk->sk_family == AF_INET) {
- net_dbg_ratelimited("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
- &inet->inet_daddr,
- ntohs(inet->inet_dport),
- inet->inet_num,
- tp->snd_una, tp->snd_nxt);
- }
- #if IS_ENABLED(CONFIG_IPV6)
- else if (sk->sk_family == AF_INET6) {
- net_dbg_ratelimited("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n",
- &sk->sk_v6_daddr,
- ntohs(inet->inet_dport),
- inet->inet_num,
- tp->snd_una, tp->snd_nxt);
- }
- #endif
- if (tcp_time_stamp - tp->rcv_tstamp > TCP_RTO_MAX) {
- tcp_write_err(sk);
- goto out;
- }
- tcp_enter_loss(sk);
- tcp_retransmit_skb(sk, tcp_write_queue_head(sk), 1);
- __sk_dst_reset(sk);
- goto out_reset_timer;
- }
- if (tcp_write_timeout(sk))
- goto out;
- if (icsk->icsk_retransmits == 0) {
- int mib_idx;
- if (icsk->icsk_ca_state == TCP_CA_Recovery) {
- if (tcp_is_sack(tp))
- mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
- else
- mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
- } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
- mib_idx = LINUX_MIB_TCPLOSSFAILURES;
- } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
- tp->sacked_out) {
- if (tcp_is_sack(tp))
- mib_idx = LINUX_MIB_TCPSACKFAILURES;
- else
- mib_idx = LINUX_MIB_TCPRENOFAILURES;
- } else {
- mib_idx = LINUX_MIB_TCPTIMEOUTS;
- }
- __NET_INC_STATS(sock_net(sk), mib_idx);
- }
- tcp_enter_loss(sk);
- if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk), 1) > 0) {
-
- if (!icsk->icsk_retransmits)
- icsk->icsk_retransmits = 1;
- inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
- min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
- TCP_RTO_MAX);
- goto out;
- }
-
- icsk->icsk_backoff++;
- icsk->icsk_retransmits++;
- out_reset_timer:
-
- if (sk->sk_state == TCP_ESTABLISHED &&
- (tp->thin_lto || sysctl_tcp_thin_linear_timeouts) &&
- tcp_stream_is_thin(tp) &&
- icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
- icsk->icsk_backoff = 0;
- icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
- } else {
-
- icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
- }
- inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
- if (retransmits_timed_out(sk, net->ipv4.sysctl_tcp_retries1 + 1, 0, 0))
- __sk_dst_reset(sk);
- out:;
- }
- void tcp_write_timer_handler(struct sock *sk)
- {
- struct inet_connection_sock *icsk = inet_csk(sk);
- int event;
- if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)) ||
- !icsk->icsk_pending)
- goto out;
- if (time_after(icsk->icsk_timeout, jiffies)) {
- sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
- goto out;
- }
- event = icsk->icsk_pending;
- switch (event) {
- case ICSK_TIME_EARLY_RETRANS:
- tcp_resume_early_retransmit(sk);
- break;
- case ICSK_TIME_LOSS_PROBE:
- tcp_send_loss_probe(sk);
- break;
- case ICSK_TIME_RETRANS:
- icsk->icsk_pending = 0;
- tcp_retransmit_timer(sk);
- break;
- case ICSK_TIME_PROBE0:
- icsk->icsk_pending = 0;
- tcp_probe_timer(sk);
- break;
- }
- out:
- sk_mem_reclaim(sk);
- }
- static void tcp_write_timer(unsigned long data)
- {
- struct sock *sk = (struct sock *)data;
- bh_lock_sock(sk);
- if (!sock_owned_by_user(sk)) {
- tcp_write_timer_handler(sk);
- } else {
-
- if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED, &tcp_sk(sk)->tsq_flags))
- sock_hold(sk);
- }
- bh_unlock_sock(sk);
- sock_put(sk);
- }
- void tcp_syn_ack_timeout(const struct request_sock *req)
- {
- struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
- __NET_INC_STATS(net, LINUX_MIB_TCPTIMEOUTS);
- }
- EXPORT_SYMBOL(tcp_syn_ack_timeout);
- void tcp_set_keepalive(struct sock *sk, int val)
- {
- if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
- return;
- if (val && !sock_flag(sk, SOCK_KEEPOPEN))
- inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
- else if (!val)
- inet_csk_delete_keepalive_timer(sk);
- }
- static void tcp_keepalive_timer (unsigned long data)
- {
- struct sock *sk = (struct sock *) data;
- struct inet_connection_sock *icsk = inet_csk(sk);
- struct tcp_sock *tp = tcp_sk(sk);
- u32 elapsed;
-
- bh_lock_sock(sk);
- if (sock_owned_by_user(sk)) {
-
- inet_csk_reset_keepalive_timer (sk, HZ/20);
- goto out;
- }
- if (sk->sk_state == TCP_LISTEN) {
- pr_err("Hmm... keepalive on a LISTEN ???\n");
- goto out;
- }
- if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
- if (tp->linger2 >= 0) {
- const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
- if (tmo > 0) {
- tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
- goto out;
- }
- }
- tcp_send_active_reset(sk, GFP_ATOMIC);
- goto death;
- }
- if (!sock_flag(sk, SOCK_KEEPOPEN) ||
- ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)))
- goto out;
- elapsed = keepalive_time_when(tp);
-
- if (tp->packets_out || tcp_send_head(sk))
- goto resched;
- elapsed = keepalive_time_elapsed(tp);
- if (elapsed >= keepalive_time_when(tp)) {
-
- if ((icsk->icsk_user_timeout != 0 &&
- elapsed >= icsk->icsk_user_timeout &&
- icsk->icsk_probes_out > 0) ||
- (icsk->icsk_user_timeout == 0 &&
- icsk->icsk_probes_out >= keepalive_probes(tp))) {
- tcp_send_active_reset(sk, GFP_ATOMIC);
- tcp_write_err(sk);
- goto out;
- }
- if (tcp_write_wakeup(sk, LINUX_MIB_TCPKEEPALIVE) <= 0) {
- icsk->icsk_probes_out++;
- elapsed = keepalive_intvl_when(tp);
- } else {
-
- elapsed = TCP_RESOURCE_PROBE_INTERVAL;
- }
- } else {
-
- elapsed = keepalive_time_when(tp) - elapsed;
- }
- sk_mem_reclaim(sk);
- resched:
- inet_csk_reset_keepalive_timer (sk, elapsed);
- goto out;
- death:
- tcp_done(sk);
- out:
- bh_unlock_sock(sk);
- sock_put(sk);
- }
- void tcp_init_xmit_timers(struct sock *sk)
- {
- inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
- &tcp_keepalive_timer);
- }
|