tcp_timer.c 21 KB

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