tcp_timer.c 20 KB

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