tcp_timer.c 21 KB

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