tcp_timer.c 19 KB

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