tcp_timer.c 20 KB

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