tcp_timer.c 22 KB

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