tcp_timer.c 24 KB

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