tcp_timer.c 22 KB

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