tcp_minisocks.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. * Version: $Id: tcp_minisocks.c,v 1.15 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  13. * Corey Minyard <wf-rch!minyard@relay.EU.net>
  14. * Florian La Roche, <flla@stud.uni-sb.de>
  15. * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  16. * Linus Torvalds, <torvalds@cs.helsinki.fi>
  17. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  18. * Matthew Dillon, <dillon@apollo.west.oic.com>
  19. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  20. * Jorge Cwik, <jorge@laser.satlink.net>
  21. */
  22. #include <linux/mm.h>
  23. #include <linux/module.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/workqueue.h>
  26. #include <net/tcp.h>
  27. #include <net/inet_common.h>
  28. #include <net/xfrm.h>
  29. #ifdef CONFIG_SYSCTL
  30. #define SYNC_INIT 0 /* let the user enable it */
  31. #else
  32. #define SYNC_INIT 1
  33. #endif
  34. int sysctl_tcp_syncookies __read_mostly = SYNC_INIT;
  35. int sysctl_tcp_abort_on_overflow __read_mostly;
  36. struct inet_timewait_death_row tcp_death_row = {
  37. .sysctl_max_tw_buckets = NR_FILE * 2,
  38. .period = TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
  39. .death_lock = __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock),
  40. .hashinfo = &tcp_hashinfo,
  41. .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
  42. (unsigned long)&tcp_death_row),
  43. .twkill_work = __WORK_INITIALIZER(tcp_death_row.twkill_work,
  44. inet_twdr_twkill_work),
  45. /* Short-time timewait calendar */
  46. .twcal_hand = -1,
  47. .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
  48. (unsigned long)&tcp_death_row),
  49. };
  50. EXPORT_SYMBOL_GPL(tcp_death_row);
  51. static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
  52. {
  53. if (seq == s_win)
  54. return 1;
  55. if (after(end_seq, s_win) && before(seq, e_win))
  56. return 1;
  57. return (seq == e_win && seq == end_seq);
  58. }
  59. /*
  60. * * Main purpose of TIME-WAIT state is to close connection gracefully,
  61. * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
  62. * (and, probably, tail of data) and one or more our ACKs are lost.
  63. * * What is TIME-WAIT timeout? It is associated with maximal packet
  64. * lifetime in the internet, which results in wrong conclusion, that
  65. * it is set to catch "old duplicate segments" wandering out of their path.
  66. * It is not quite correct. This timeout is calculated so that it exceeds
  67. * maximal retransmission timeout enough to allow to lose one (or more)
  68. * segments sent by peer and our ACKs. This time may be calculated from RTO.
  69. * * When TIME-WAIT socket receives RST, it means that another end
  70. * finally closed and we are allowed to kill TIME-WAIT too.
  71. * * Second purpose of TIME-WAIT is catching old duplicate segments.
  72. * Well, certainly it is pure paranoia, but if we load TIME-WAIT
  73. * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
  74. * * If we invented some more clever way to catch duplicates
  75. * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
  76. *
  77. * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
  78. * When you compare it to RFCs, please, read section SEGMENT ARRIVES
  79. * from the very beginning.
  80. *
  81. * NOTE. With recycling (and later with fin-wait-2) TW bucket
  82. * is _not_ stateless. It means, that strictly speaking we must
  83. * spinlock it. I do not want! Well, probability of misbehaviour
  84. * is ridiculously low and, seems, we could use some mb() tricks
  85. * to avoid misread sequence numbers, states etc. --ANK
  86. */
  87. enum tcp_tw_status
  88. tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
  89. const struct tcphdr *th)
  90. {
  91. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  92. struct tcp_options_received tmp_opt;
  93. int paws_reject = 0;
  94. tmp_opt.saw_tstamp = 0;
  95. if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
  96. tcp_parse_options(skb, &tmp_opt, 0);
  97. if (tmp_opt.saw_tstamp) {
  98. tmp_opt.ts_recent = tcptw->tw_ts_recent;
  99. tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  100. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  101. }
  102. }
  103. if (tw->tw_substate == TCP_FIN_WAIT2) {
  104. /* Just repeat all the checks of tcp_rcv_state_process() */
  105. /* Out of window, send ACK */
  106. if (paws_reject ||
  107. !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  108. tcptw->tw_rcv_nxt,
  109. tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
  110. return TCP_TW_ACK;
  111. if (th->rst)
  112. goto kill;
  113. if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
  114. goto kill_with_rst;
  115. /* Dup ACK? */
  116. if (!after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
  117. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
  118. inet_twsk_put(tw);
  119. return TCP_TW_SUCCESS;
  120. }
  121. /* New data or FIN. If new data arrive after half-duplex close,
  122. * reset.
  123. */
  124. if (!th->fin ||
  125. TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
  126. kill_with_rst:
  127. inet_twsk_deschedule(tw, &tcp_death_row);
  128. inet_twsk_put(tw);
  129. return TCP_TW_RST;
  130. }
  131. /* FIN arrived, enter true time-wait state. */
  132. tw->tw_substate = TCP_TIME_WAIT;
  133. tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  134. if (tmp_opt.saw_tstamp) {
  135. tcptw->tw_ts_recent_stamp = xtime.tv_sec;
  136. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  137. }
  138. /* I am shamed, but failed to make it more elegant.
  139. * Yes, it is direct reference to IP, which is impossible
  140. * to generalize to IPv6. Taking into account that IPv6
  141. * do not understand recycling in any case, it not
  142. * a big problem in practice. --ANK */
  143. if (tw->tw_family == AF_INET &&
  144. tcp_death_row.sysctl_tw_recycle && tcptw->tw_ts_recent_stamp &&
  145. tcp_v4_tw_remember_stamp(tw))
  146. inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout,
  147. TCP_TIMEWAIT_LEN);
  148. else
  149. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  150. TCP_TIMEWAIT_LEN);
  151. return TCP_TW_ACK;
  152. }
  153. /*
  154. * Now real TIME-WAIT state.
  155. *
  156. * RFC 1122:
  157. * "When a connection is [...] on TIME-WAIT state [...]
  158. * [a TCP] MAY accept a new SYN from the remote TCP to
  159. * reopen the connection directly, if it:
  160. *
  161. * (1) assigns its initial sequence number for the new
  162. * connection to be larger than the largest sequence
  163. * number it used on the previous connection incarnation,
  164. * and
  165. *
  166. * (2) returns to TIME-WAIT state if the SYN turns out
  167. * to be an old duplicate".
  168. */
  169. if (!paws_reject &&
  170. (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
  171. (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
  172. /* In window segment, it may be only reset or bare ack. */
  173. if (th->rst) {
  174. /* This is TIME_WAIT assassination, in two flavors.
  175. * Oh well... nobody has a sufficient solution to this
  176. * protocol bug yet.
  177. */
  178. if (sysctl_tcp_rfc1337 == 0) {
  179. kill:
  180. inet_twsk_deschedule(tw, &tcp_death_row);
  181. inet_twsk_put(tw);
  182. return TCP_TW_SUCCESS;
  183. }
  184. }
  185. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  186. TCP_TIMEWAIT_LEN);
  187. if (tmp_opt.saw_tstamp) {
  188. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  189. tcptw->tw_ts_recent_stamp = xtime.tv_sec;
  190. }
  191. inet_twsk_put(tw);
  192. return TCP_TW_SUCCESS;
  193. }
  194. /* Out of window segment.
  195. All the segments are ACKed immediately.
  196. The only exception is new SYN. We accept it, if it is
  197. not old duplicate and we are not in danger to be killed
  198. by delayed old duplicates. RFC check is that it has
  199. newer sequence number works at rates <40Mbit/sec.
  200. However, if paws works, it is reliable AND even more,
  201. we even may relax silly seq space cutoff.
  202. RED-PEN: we violate main RFC requirement, if this SYN will appear
  203. old duplicate (i.e. we receive RST in reply to SYN-ACK),
  204. we must return socket to time-wait state. It is not good,
  205. but not fatal yet.
  206. */
  207. if (th->syn && !th->rst && !th->ack && !paws_reject &&
  208. (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
  209. (tmp_opt.saw_tstamp &&
  210. (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
  211. u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
  212. if (isn == 0)
  213. isn++;
  214. TCP_SKB_CB(skb)->when = isn;
  215. return TCP_TW_SYN;
  216. }
  217. if (paws_reject)
  218. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  219. if(!th->rst) {
  220. /* In this case we must reset the TIMEWAIT timer.
  221. *
  222. * If it is ACKless SYN it may be both old duplicate
  223. * and new good SYN with random sequence number <rcv_nxt.
  224. * Do not reschedule in the last case.
  225. */
  226. if (paws_reject || th->ack)
  227. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  228. TCP_TIMEWAIT_LEN);
  229. /* Send ACK. Note, we do not put the bucket,
  230. * it will be released by caller.
  231. */
  232. return TCP_TW_ACK;
  233. }
  234. inet_twsk_put(tw);
  235. return TCP_TW_SUCCESS;
  236. }
  237. /*
  238. * Move a socket to time-wait or dead fin-wait-2 state.
  239. */
  240. void tcp_time_wait(struct sock *sk, int state, int timeo)
  241. {
  242. struct inet_timewait_sock *tw = NULL;
  243. const struct inet_connection_sock *icsk = inet_csk(sk);
  244. const struct tcp_sock *tp = tcp_sk(sk);
  245. int recycle_ok = 0;
  246. if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
  247. recycle_ok = icsk->icsk_af_ops->remember_stamp(sk);
  248. if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)
  249. tw = inet_twsk_alloc(sk, state);
  250. if (tw != NULL) {
  251. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  252. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  253. tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
  254. tcptw->tw_rcv_nxt = tp->rcv_nxt;
  255. tcptw->tw_snd_nxt = tp->snd_nxt;
  256. tcptw->tw_rcv_wnd = tcp_receive_window(tp);
  257. tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
  258. tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
  259. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  260. if (tw->tw_family == PF_INET6) {
  261. struct ipv6_pinfo *np = inet6_sk(sk);
  262. struct inet6_timewait_sock *tw6;
  263. tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
  264. tw6 = inet6_twsk((struct sock *)tw);
  265. ipv6_addr_copy(&tw6->tw_v6_daddr, &np->daddr);
  266. ipv6_addr_copy(&tw6->tw_v6_rcv_saddr, &np->rcv_saddr);
  267. tw->tw_ipv6only = np->ipv6only;
  268. }
  269. #endif
  270. /* Linkage updates. */
  271. __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
  272. /* Get the TIME_WAIT timeout firing. */
  273. if (timeo < rto)
  274. timeo = rto;
  275. if (recycle_ok) {
  276. tw->tw_timeout = rto;
  277. } else {
  278. tw->tw_timeout = TCP_TIMEWAIT_LEN;
  279. if (state == TCP_TIME_WAIT)
  280. timeo = TCP_TIMEWAIT_LEN;
  281. }
  282. inet_twsk_schedule(tw, &tcp_death_row, timeo,
  283. TCP_TIMEWAIT_LEN);
  284. inet_twsk_put(tw);
  285. } else {
  286. /* Sorry, if we're out of memory, just CLOSE this
  287. * socket up. We've got bigger problems than
  288. * non-graceful socket closings.
  289. */
  290. if (net_ratelimit())
  291. printk(KERN_INFO "TCP: time wait bucket table overflow\n");
  292. }
  293. tcp_update_metrics(sk);
  294. tcp_done(sk);
  295. }
  296. /* This is not only more efficient than what we used to do, it eliminates
  297. * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
  298. *
  299. * Actually, we could lots of memory writes here. tp of listening
  300. * socket contains all necessary default parameters.
  301. */
  302. struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
  303. {
  304. struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
  305. if (newsk != NULL) {
  306. const struct inet_request_sock *ireq = inet_rsk(req);
  307. struct tcp_request_sock *treq = tcp_rsk(req);
  308. struct inet_connection_sock *newicsk = inet_csk(sk);
  309. struct tcp_sock *newtp;
  310. /* Now setup tcp_sock */
  311. newtp = tcp_sk(newsk);
  312. newtp->pred_flags = 0;
  313. newtp->rcv_nxt = treq->rcv_isn + 1;
  314. newtp->snd_nxt = newtp->snd_una = newtp->snd_sml = treq->snt_isn + 1;
  315. tcp_prequeue_init(newtp);
  316. tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);
  317. newtp->srtt = 0;
  318. newtp->mdev = TCP_TIMEOUT_INIT;
  319. newicsk->icsk_rto = TCP_TIMEOUT_INIT;
  320. newtp->packets_out = 0;
  321. newtp->left_out = 0;
  322. newtp->retrans_out = 0;
  323. newtp->sacked_out = 0;
  324. newtp->fackets_out = 0;
  325. newtp->snd_ssthresh = 0x7fffffff;
  326. /* So many TCP implementations out there (incorrectly) count the
  327. * initial SYN frame in their delayed-ACK and congestion control
  328. * algorithms that we must have the following bandaid to talk
  329. * efficiently to them. -DaveM
  330. */
  331. newtp->snd_cwnd = 2;
  332. newtp->snd_cwnd_cnt = 0;
  333. newtp->bytes_acked = 0;
  334. newtp->frto_counter = 0;
  335. newtp->frto_highmark = 0;
  336. newicsk->icsk_ca_ops = &tcp_init_congestion_ops;
  337. tcp_set_ca_state(newsk, TCP_CA_Open);
  338. tcp_init_xmit_timers(newsk);
  339. skb_queue_head_init(&newtp->out_of_order_queue);
  340. newtp->rcv_wup = treq->rcv_isn + 1;
  341. newtp->write_seq = treq->snt_isn + 1;
  342. newtp->pushed_seq = newtp->write_seq;
  343. newtp->copied_seq = treq->rcv_isn + 1;
  344. newtp->rx_opt.saw_tstamp = 0;
  345. newtp->rx_opt.dsack = 0;
  346. newtp->rx_opt.eff_sacks = 0;
  347. newtp->rx_opt.num_sacks = 0;
  348. newtp->urg_data = 0;
  349. if (sock_flag(newsk, SOCK_KEEPOPEN))
  350. inet_csk_reset_keepalive_timer(newsk,
  351. keepalive_time_when(newtp));
  352. newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
  353. if((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
  354. if (sysctl_tcp_fack)
  355. newtp->rx_opt.sack_ok |= 2;
  356. }
  357. newtp->window_clamp = req->window_clamp;
  358. newtp->rcv_ssthresh = req->rcv_wnd;
  359. newtp->rcv_wnd = req->rcv_wnd;
  360. newtp->rx_opt.wscale_ok = ireq->wscale_ok;
  361. if (newtp->rx_opt.wscale_ok) {
  362. newtp->rx_opt.snd_wscale = ireq->snd_wscale;
  363. newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
  364. } else {
  365. newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
  366. newtp->window_clamp = min(newtp->window_clamp, 65535U);
  367. }
  368. newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->rx_opt.snd_wscale;
  369. newtp->max_window = newtp->snd_wnd;
  370. if (newtp->rx_opt.tstamp_ok) {
  371. newtp->rx_opt.ts_recent = req->ts_recent;
  372. newtp->rx_opt.ts_recent_stamp = xtime.tv_sec;
  373. newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  374. } else {
  375. newtp->rx_opt.ts_recent_stamp = 0;
  376. newtp->tcp_header_len = sizeof(struct tcphdr);
  377. }
  378. if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
  379. newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
  380. newtp->rx_opt.mss_clamp = req->mss;
  381. TCP_ECN_openreq_child(newtp, req);
  382. TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);
  383. }
  384. return newsk;
  385. }
  386. /*
  387. * Process an incoming packet for SYN_RECV sockets represented
  388. * as a request_sock.
  389. */
  390. struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
  391. struct request_sock *req,
  392. struct request_sock **prev)
  393. {
  394. struct tcphdr *th = skb->h.th;
  395. u32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
  396. int paws_reject = 0;
  397. struct tcp_options_received tmp_opt;
  398. struct sock *child;
  399. tmp_opt.saw_tstamp = 0;
  400. if (th->doff > (sizeof(struct tcphdr)>>2)) {
  401. tcp_parse_options(skb, &tmp_opt, 0);
  402. if (tmp_opt.saw_tstamp) {
  403. tmp_opt.ts_recent = req->ts_recent;
  404. /* We do not store true stamp, but it is not required,
  405. * it can be estimated (approximately)
  406. * from another data.
  407. */
  408. tmp_opt.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
  409. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  410. }
  411. }
  412. /* Check for pure retransmitted SYN. */
  413. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
  414. flg == TCP_FLAG_SYN &&
  415. !paws_reject) {
  416. /*
  417. * RFC793 draws (Incorrectly! It was fixed in RFC1122)
  418. * this case on figure 6 and figure 8, but formal
  419. * protocol description says NOTHING.
  420. * To be more exact, it says that we should send ACK,
  421. * because this segment (at least, if it has no data)
  422. * is out of window.
  423. *
  424. * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
  425. * describe SYN-RECV state. All the description
  426. * is wrong, we cannot believe to it and should
  427. * rely only on common sense and implementation
  428. * experience.
  429. *
  430. * Enforce "SYN-ACK" according to figure 8, figure 6
  431. * of RFC793, fixed by RFC1122.
  432. */
  433. req->rsk_ops->rtx_syn_ack(sk, req, NULL);
  434. return NULL;
  435. }
  436. /* Further reproduces section "SEGMENT ARRIVES"
  437. for state SYN-RECEIVED of RFC793.
  438. It is broken, however, it does not work only
  439. when SYNs are crossed.
  440. You would think that SYN crossing is impossible here, since
  441. we should have a SYN_SENT socket (from connect()) on our end,
  442. but this is not true if the crossed SYNs were sent to both
  443. ends by a malicious third party. We must defend against this,
  444. and to do that we first verify the ACK (as per RFC793, page
  445. 36) and reset if it is invalid. Is this a true full defense?
  446. To convince ourselves, let us consider a way in which the ACK
  447. test can still pass in this 'malicious crossed SYNs' case.
  448. Malicious sender sends identical SYNs (and thus identical sequence
  449. numbers) to both A and B:
  450. A: gets SYN, seq=7
  451. B: gets SYN, seq=7
  452. By our good fortune, both A and B select the same initial
  453. send sequence number of seven :-)
  454. A: sends SYN|ACK, seq=7, ack_seq=8
  455. B: sends SYN|ACK, seq=7, ack_seq=8
  456. So we are now A eating this SYN|ACK, ACK test passes. So
  457. does sequence test, SYN is truncated, and thus we consider
  458. it a bare ACK.
  459. If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
  460. bare ACK. Otherwise, we create an established connection. Both
  461. ends (listening sockets) accept the new incoming connection and try
  462. to talk to each other. 8-)
  463. Note: This case is both harmless, and rare. Possibility is about the
  464. same as us discovering intelligent life on another plant tomorrow.
  465. But generally, we should (RFC lies!) to accept ACK
  466. from SYNACK both here and in tcp_rcv_state_process().
  467. tcp_rcv_state_process() does not, hence, we do not too.
  468. Note that the case is absolutely generic:
  469. we cannot optimize anything here without
  470. violating protocol. All the checks must be made
  471. before attempt to create socket.
  472. */
  473. /* RFC793 page 36: "If the connection is in any non-synchronized state ...
  474. * and the incoming segment acknowledges something not yet
  475. * sent (the segment carries an unacceptable ACK) ...
  476. * a reset is sent."
  477. *
  478. * Invalid ACK: reset will be sent by listening socket
  479. */
  480. if ((flg & TCP_FLAG_ACK) &&
  481. (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))
  482. return sk;
  483. /* Also, it would be not so bad idea to check rcv_tsecr, which
  484. * is essentially ACK extension and too early or too late values
  485. * should cause reset in unsynchronized states.
  486. */
  487. /* RFC793: "first check sequence number". */
  488. if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  489. tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
  490. /* Out of window: send ACK and drop. */
  491. if (!(flg & TCP_FLAG_RST))
  492. req->rsk_ops->send_ack(skb, req);
  493. if (paws_reject)
  494. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  495. return NULL;
  496. }
  497. /* In sequence, PAWS is OK. */
  498. if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
  499. req->ts_recent = tmp_opt.rcv_tsval;
  500. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
  501. /* Truncate SYN, it is out of window starting
  502. at tcp_rsk(req)->rcv_isn + 1. */
  503. flg &= ~TCP_FLAG_SYN;
  504. }
  505. /* RFC793: "second check the RST bit" and
  506. * "fourth, check the SYN bit"
  507. */
  508. if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
  509. TCP_INC_STATS_BH(TCP_MIB_ATTEMPTFAILS);
  510. goto embryonic_reset;
  511. }
  512. /* ACK sequence verified above, just make sure ACK is
  513. * set. If ACK not set, just silently drop the packet.
  514. */
  515. if (!(flg & TCP_FLAG_ACK))
  516. return NULL;
  517. /* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
  518. if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
  519. TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
  520. inet_rsk(req)->acked = 1;
  521. return NULL;
  522. }
  523. /* OK, ACK is valid, create big socket and
  524. * feed this segment to it. It will repeat all
  525. * the tests. THIS SEGMENT MUST MOVE SOCKET TO
  526. * ESTABLISHED STATE. If it will be dropped after
  527. * socket is created, wait for troubles.
  528. */
  529. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb,
  530. req, NULL);
  531. if (child == NULL)
  532. goto listen_overflow;
  533. inet_csk_reqsk_queue_unlink(sk, req, prev);
  534. inet_csk_reqsk_queue_removed(sk, req);
  535. inet_csk_reqsk_queue_add(sk, req, child);
  536. return child;
  537. listen_overflow:
  538. if (!sysctl_tcp_abort_on_overflow) {
  539. inet_rsk(req)->acked = 1;
  540. return NULL;
  541. }
  542. embryonic_reset:
  543. NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);
  544. if (!(flg & TCP_FLAG_RST))
  545. req->rsk_ops->send_reset(skb);
  546. inet_csk_reqsk_queue_drop(sk, req, prev);
  547. return NULL;
  548. }
  549. /*
  550. * Queue segment on the new socket if the new socket is active,
  551. * otherwise we just shortcircuit this and continue with
  552. * the new socket.
  553. */
  554. int tcp_child_process(struct sock *parent, struct sock *child,
  555. struct sk_buff *skb)
  556. {
  557. int ret = 0;
  558. int state = child->sk_state;
  559. if (!sock_owned_by_user(child)) {
  560. ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
  561. /* Wakeup parent, send SIGIO */
  562. if (state == TCP_SYN_RECV && child->sk_state != state)
  563. parent->sk_data_ready(parent, 0);
  564. } else {
  565. /* Alas, it is possible again, because we do lookup
  566. * in main socket hash table and lock on listening
  567. * socket does not protect us more.
  568. */
  569. sk_add_backlog(child, skb);
  570. }
  571. bh_unlock_sock(child);
  572. sock_put(child);
  573. return ret;
  574. }
  575. EXPORT_SYMBOL(tcp_check_req);
  576. EXPORT_SYMBOL(tcp_child_process);
  577. EXPORT_SYMBOL(tcp_create_openreq_child);
  578. EXPORT_SYMBOL(tcp_timewait_state_process);