tcp_timer.c 27 KB

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