tcp_timer.c 21 KB

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