tcp_minisocks.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/workqueue.h>
  25. #include <net/tcp.h>
  26. #include <net/inet_common.h>
  27. #include <net/xfrm.h>
  28. int sysctl_tcp_syncookies __read_mostly = 1;
  29. EXPORT_SYMBOL(sysctl_tcp_syncookies);
  30. int sysctl_tcp_abort_on_overflow __read_mostly;
  31. struct inet_timewait_death_row tcp_death_row = {
  32. .sysctl_max_tw_buckets = NR_FILE * 2,
  33. .period = TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
  34. .death_lock = __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock),
  35. .hashinfo = &tcp_hashinfo,
  36. .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
  37. (unsigned long)&tcp_death_row),
  38. .twkill_work = __WORK_INITIALIZER(tcp_death_row.twkill_work,
  39. inet_twdr_twkill_work),
  40. /* Short-time timewait calendar */
  41. .twcal_hand = -1,
  42. .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
  43. (unsigned long)&tcp_death_row),
  44. };
  45. EXPORT_SYMBOL_GPL(tcp_death_row);
  46. /* VJ's idea. Save last timestamp seen from this destination
  47. * and hold it at least for normal timewait interval to use for duplicate
  48. * segment detection in subsequent connections, before they enter synchronized
  49. * state.
  50. */
  51. static int tcp_remember_stamp(struct sock *sk)
  52. {
  53. const struct inet_connection_sock *icsk = inet_csk(sk);
  54. struct tcp_sock *tp = tcp_sk(sk);
  55. struct inet_peer *peer;
  56. bool release_it;
  57. peer = icsk->icsk_af_ops->get_peer(sk, &release_it);
  58. if (peer) {
  59. if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
  60. ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
  61. peer->tcp_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
  62. peer->tcp_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
  63. peer->tcp_ts = tp->rx_opt.ts_recent;
  64. }
  65. if (release_it)
  66. inet_putpeer(peer);
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. static int tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
  72. {
  73. struct sock *sk = (struct sock *) tw;
  74. struct inet_peer *peer;
  75. peer = twsk_getpeer(sk);
  76. if (peer) {
  77. const struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  78. if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
  79. ((u32)get_seconds() - peer->tcp_ts_stamp > TCP_PAWS_MSL &&
  80. peer->tcp_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
  81. peer->tcp_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
  82. peer->tcp_ts = tcptw->tw_ts_recent;
  83. }
  84. inet_putpeer(peer);
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
  90. {
  91. if (seq == s_win)
  92. return 1;
  93. if (after(end_seq, s_win) && before(seq, e_win))
  94. return 1;
  95. return seq == e_win && seq == end_seq;
  96. }
  97. /*
  98. * * Main purpose of TIME-WAIT state is to close connection gracefully,
  99. * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
  100. * (and, probably, tail of data) and one or more our ACKs are lost.
  101. * * What is TIME-WAIT timeout? It is associated with maximal packet
  102. * lifetime in the internet, which results in wrong conclusion, that
  103. * it is set to catch "old duplicate segments" wandering out of their path.
  104. * It is not quite correct. This timeout is calculated so that it exceeds
  105. * maximal retransmission timeout enough to allow to lose one (or more)
  106. * segments sent by peer and our ACKs. This time may be calculated from RTO.
  107. * * When TIME-WAIT socket receives RST, it means that another end
  108. * finally closed and we are allowed to kill TIME-WAIT too.
  109. * * Second purpose of TIME-WAIT is catching old duplicate segments.
  110. * Well, certainly it is pure paranoia, but if we load TIME-WAIT
  111. * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
  112. * * If we invented some more clever way to catch duplicates
  113. * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
  114. *
  115. * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
  116. * When you compare it to RFCs, please, read section SEGMENT ARRIVES
  117. * from the very beginning.
  118. *
  119. * NOTE. With recycling (and later with fin-wait-2) TW bucket
  120. * is _not_ stateless. It means, that strictly speaking we must
  121. * spinlock it. I do not want! Well, probability of misbehaviour
  122. * is ridiculously low and, seems, we could use some mb() tricks
  123. * to avoid misread sequence numbers, states etc. --ANK
  124. */
  125. enum tcp_tw_status
  126. tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
  127. const struct tcphdr *th)
  128. {
  129. struct tcp_options_received tmp_opt;
  130. const u8 *hash_location;
  131. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  132. int paws_reject = 0;
  133. tmp_opt.saw_tstamp = 0;
  134. if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
  135. tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
  136. if (tmp_opt.saw_tstamp) {
  137. tmp_opt.ts_recent = tcptw->tw_ts_recent;
  138. tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  139. paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
  140. }
  141. }
  142. if (tw->tw_substate == TCP_FIN_WAIT2) {
  143. /* Just repeat all the checks of tcp_rcv_state_process() */
  144. /* Out of window, send ACK */
  145. if (paws_reject ||
  146. !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  147. tcptw->tw_rcv_nxt,
  148. tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
  149. return TCP_TW_ACK;
  150. if (th->rst)
  151. goto kill;
  152. if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
  153. goto kill_with_rst;
  154. /* Dup ACK? */
  155. if (!th->ack ||
  156. !after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
  157. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
  158. inet_twsk_put(tw);
  159. return TCP_TW_SUCCESS;
  160. }
  161. /* New data or FIN. If new data arrive after half-duplex close,
  162. * reset.
  163. */
  164. if (!th->fin ||
  165. TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
  166. kill_with_rst:
  167. inet_twsk_deschedule(tw, &tcp_death_row);
  168. inet_twsk_put(tw);
  169. return TCP_TW_RST;
  170. }
  171. /* FIN arrived, enter true time-wait state. */
  172. tw->tw_substate = TCP_TIME_WAIT;
  173. tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  174. if (tmp_opt.saw_tstamp) {
  175. tcptw->tw_ts_recent_stamp = get_seconds();
  176. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  177. }
  178. if (tcp_death_row.sysctl_tw_recycle &&
  179. tcptw->tw_ts_recent_stamp &&
  180. tcp_tw_remember_stamp(tw))
  181. inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout,
  182. TCP_TIMEWAIT_LEN);
  183. else
  184. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  185. TCP_TIMEWAIT_LEN);
  186. return TCP_TW_ACK;
  187. }
  188. /*
  189. * Now real TIME-WAIT state.
  190. *
  191. * RFC 1122:
  192. * "When a connection is [...] on TIME-WAIT state [...]
  193. * [a TCP] MAY accept a new SYN from the remote TCP to
  194. * reopen the connection directly, if it:
  195. *
  196. * (1) assigns its initial sequence number for the new
  197. * connection to be larger than the largest sequence
  198. * number it used on the previous connection incarnation,
  199. * and
  200. *
  201. * (2) returns to TIME-WAIT state if the SYN turns out
  202. * to be an old duplicate".
  203. */
  204. if (!paws_reject &&
  205. (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
  206. (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
  207. /* In window segment, it may be only reset or bare ack. */
  208. if (th->rst) {
  209. /* This is TIME_WAIT assassination, in two flavors.
  210. * Oh well... nobody has a sufficient solution to this
  211. * protocol bug yet.
  212. */
  213. if (sysctl_tcp_rfc1337 == 0) {
  214. kill:
  215. inet_twsk_deschedule(tw, &tcp_death_row);
  216. inet_twsk_put(tw);
  217. return TCP_TW_SUCCESS;
  218. }
  219. }
  220. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  221. TCP_TIMEWAIT_LEN);
  222. if (tmp_opt.saw_tstamp) {
  223. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  224. tcptw->tw_ts_recent_stamp = get_seconds();
  225. }
  226. inet_twsk_put(tw);
  227. return TCP_TW_SUCCESS;
  228. }
  229. /* Out of window segment.
  230. All the segments are ACKed immediately.
  231. The only exception is new SYN. We accept it, if it is
  232. not old duplicate and we are not in danger to be killed
  233. by delayed old duplicates. RFC check is that it has
  234. newer sequence number works at rates <40Mbit/sec.
  235. However, if paws works, it is reliable AND even more,
  236. we even may relax silly seq space cutoff.
  237. RED-PEN: we violate main RFC requirement, if this SYN will appear
  238. old duplicate (i.e. we receive RST in reply to SYN-ACK),
  239. we must return socket to time-wait state. It is not good,
  240. but not fatal yet.
  241. */
  242. if (th->syn && !th->rst && !th->ack && !paws_reject &&
  243. (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
  244. (tmp_opt.saw_tstamp &&
  245. (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
  246. u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
  247. if (isn == 0)
  248. isn++;
  249. TCP_SKB_CB(skb)->when = isn;
  250. return TCP_TW_SYN;
  251. }
  252. if (paws_reject)
  253. NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_PAWSESTABREJECTED);
  254. if (!th->rst) {
  255. /* In this case we must reset the TIMEWAIT timer.
  256. *
  257. * If it is ACKless SYN it may be both old duplicate
  258. * and new good SYN with random sequence number <rcv_nxt.
  259. * Do not reschedule in the last case.
  260. */
  261. if (paws_reject || th->ack)
  262. inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN,
  263. TCP_TIMEWAIT_LEN);
  264. /* Send ACK. Note, we do not put the bucket,
  265. * it will be released by caller.
  266. */
  267. return TCP_TW_ACK;
  268. }
  269. inet_twsk_put(tw);
  270. return TCP_TW_SUCCESS;
  271. }
  272. EXPORT_SYMBOL(tcp_timewait_state_process);
  273. /*
  274. * Move a socket to time-wait or dead fin-wait-2 state.
  275. */
  276. void tcp_time_wait(struct sock *sk, int state, int timeo)
  277. {
  278. struct inet_timewait_sock *tw = NULL;
  279. const struct inet_connection_sock *icsk = inet_csk(sk);
  280. const struct tcp_sock *tp = tcp_sk(sk);
  281. int recycle_ok = 0;
  282. if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
  283. recycle_ok = tcp_remember_stamp(sk);
  284. if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)
  285. tw = inet_twsk_alloc(sk, state);
  286. if (tw != NULL) {
  287. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  288. const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
  289. tw->tw_transparent = inet_sk(sk)->transparent;
  290. tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
  291. tcptw->tw_rcv_nxt = tp->rcv_nxt;
  292. tcptw->tw_snd_nxt = tp->snd_nxt;
  293. tcptw->tw_rcv_wnd = tcp_receive_window(tp);
  294. tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
  295. tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
  296. #if IS_ENABLED(CONFIG_IPV6)
  297. if (tw->tw_family == PF_INET6) {
  298. struct ipv6_pinfo *np = inet6_sk(sk);
  299. struct inet6_timewait_sock *tw6;
  300. tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
  301. tw6 = inet6_twsk((struct sock *)tw);
  302. tw6->tw_v6_daddr = np->daddr;
  303. tw6->tw_v6_rcv_saddr = np->rcv_saddr;
  304. tw->tw_tclass = np->tclass;
  305. tw->tw_ipv6only = np->ipv6only;
  306. }
  307. #endif
  308. #ifdef CONFIG_TCP_MD5SIG
  309. /*
  310. * The timewait bucket does not have the key DB from the
  311. * sock structure. We just make a quick copy of the
  312. * md5 key being used (if indeed we are using one)
  313. * so the timewait ack generating code has the key.
  314. */
  315. do {
  316. struct tcp_md5sig_key *key;
  317. tcptw->tw_md5_key = NULL;
  318. key = tp->af_specific->md5_lookup(sk, sk);
  319. if (key != NULL) {
  320. tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC);
  321. if (tcptw->tw_md5_key && tcp_alloc_md5sig_pool(sk) == NULL)
  322. BUG();
  323. }
  324. } while (0);
  325. #endif
  326. /* Linkage updates. */
  327. __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
  328. /* Get the TIME_WAIT timeout firing. */
  329. if (timeo < rto)
  330. timeo = rto;
  331. if (recycle_ok) {
  332. tw->tw_timeout = rto;
  333. } else {
  334. tw->tw_timeout = TCP_TIMEWAIT_LEN;
  335. if (state == TCP_TIME_WAIT)
  336. timeo = TCP_TIMEWAIT_LEN;
  337. }
  338. inet_twsk_schedule(tw, &tcp_death_row, timeo,
  339. TCP_TIMEWAIT_LEN);
  340. inet_twsk_put(tw);
  341. } else {
  342. /* Sorry, if we're out of memory, just CLOSE this
  343. * socket up. We've got bigger problems than
  344. * non-graceful socket closings.
  345. */
  346. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEWAITOVERFLOW);
  347. }
  348. tcp_update_metrics(sk);
  349. tcp_done(sk);
  350. }
  351. void tcp_twsk_destructor(struct sock *sk)
  352. {
  353. #ifdef CONFIG_TCP_MD5SIG
  354. struct tcp_timewait_sock *twsk = tcp_twsk(sk);
  355. if (twsk->tw_md5_key) {
  356. tcp_free_md5sig_pool();
  357. kfree_rcu(twsk->tw_md5_key, rcu);
  358. }
  359. #endif
  360. }
  361. EXPORT_SYMBOL_GPL(tcp_twsk_destructor);
  362. static inline void TCP_ECN_openreq_child(struct tcp_sock *tp,
  363. struct request_sock *req)
  364. {
  365. tp->ecn_flags = inet_rsk(req)->ecn_ok ? TCP_ECN_OK : 0;
  366. }
  367. /* This is not only more efficient than what we used to do, it eliminates
  368. * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
  369. *
  370. * Actually, we could lots of memory writes here. tp of listening
  371. * socket contains all necessary default parameters.
  372. */
  373. struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
  374. {
  375. struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
  376. if (newsk != NULL) {
  377. const struct inet_request_sock *ireq = inet_rsk(req);
  378. struct tcp_request_sock *treq = tcp_rsk(req);
  379. struct inet_connection_sock *newicsk = inet_csk(newsk);
  380. struct tcp_sock *newtp = tcp_sk(newsk);
  381. struct tcp_sock *oldtp = tcp_sk(sk);
  382. struct tcp_cookie_values *oldcvp = oldtp->cookie_values;
  383. /* TCP Cookie Transactions require space for the cookie pair,
  384. * as it differs for each connection. There is no need to
  385. * copy any s_data_payload stored at the original socket.
  386. * Failure will prevent resuming the connection.
  387. *
  388. * Presumed copied, in order of appearance:
  389. * cookie_in_always, cookie_out_never
  390. */
  391. if (oldcvp != NULL) {
  392. struct tcp_cookie_values *newcvp =
  393. kzalloc(sizeof(*newtp->cookie_values),
  394. GFP_ATOMIC);
  395. if (newcvp != NULL) {
  396. kref_init(&newcvp->kref);
  397. newcvp->cookie_desired =
  398. oldcvp->cookie_desired;
  399. newtp->cookie_values = newcvp;
  400. } else {
  401. /* Not Yet Implemented */
  402. newtp->cookie_values = NULL;
  403. }
  404. }
  405. /* Now setup tcp_sock */
  406. newtp->pred_flags = 0;
  407. newtp->rcv_wup = newtp->copied_seq =
  408. newtp->rcv_nxt = treq->rcv_isn + 1;
  409. newtp->segs_in = 1;
  410. newtp->snd_sml = newtp->snd_una =
  411. newtp->snd_nxt = newtp->snd_up =
  412. treq->snt_isn + 1 + tcp_s_data_size(oldtp);
  413. tcp_prequeue_init(newtp);
  414. INIT_LIST_HEAD(&newtp->tsq_node);
  415. tcp_init_wl(newtp, treq->rcv_isn);
  416. newtp->srtt = 0;
  417. newtp->mdev = TCP_TIMEOUT_INIT;
  418. newicsk->icsk_rto = TCP_TIMEOUT_INIT;
  419. newtp->packets_out = 0;
  420. newtp->retrans_out = 0;
  421. newtp->sacked_out = 0;
  422. newtp->fackets_out = 0;
  423. newtp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  424. /* So many TCP implementations out there (incorrectly) count the
  425. * initial SYN frame in their delayed-ACK and congestion control
  426. * algorithms that we must have the following bandaid to talk
  427. * efficiently to them. -DaveM
  428. */
  429. newtp->snd_cwnd = TCP_INIT_CWND;
  430. newtp->snd_cwnd_cnt = 0;
  431. newtp->frto_counter = 0;
  432. newtp->frto_highmark = 0;
  433. if (newicsk->icsk_ca_ops != &tcp_init_congestion_ops &&
  434. !try_module_get(newicsk->icsk_ca_ops->owner))
  435. newicsk->icsk_ca_ops = &tcp_init_congestion_ops;
  436. tcp_set_ca_state(newsk, TCP_CA_Open);
  437. tcp_init_xmit_timers(newsk);
  438. skb_queue_head_init(&newtp->out_of_order_queue);
  439. newtp->write_seq = newtp->pushed_seq =
  440. treq->snt_isn + 1 + tcp_s_data_size(oldtp);
  441. newtp->rx_opt.saw_tstamp = 0;
  442. newtp->rx_opt.dsack = 0;
  443. newtp->rx_opt.num_sacks = 0;
  444. newtp->urg_data = 0;
  445. if (sock_flag(newsk, SOCK_KEEPOPEN))
  446. inet_csk_reset_keepalive_timer(newsk,
  447. keepalive_time_when(newtp));
  448. newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
  449. if ((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
  450. if (sysctl_tcp_fack)
  451. tcp_enable_fack(newtp);
  452. }
  453. newtp->window_clamp = req->window_clamp;
  454. newtp->rcv_ssthresh = req->rcv_wnd;
  455. newtp->rcv_wnd = req->rcv_wnd;
  456. newtp->rx_opt.wscale_ok = ireq->wscale_ok;
  457. if (newtp->rx_opt.wscale_ok) {
  458. newtp->rx_opt.snd_wscale = ireq->snd_wscale;
  459. newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
  460. } else {
  461. newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
  462. newtp->window_clamp = min(newtp->window_clamp, 65535U);
  463. }
  464. newtp->snd_wnd = (ntohs(tcp_hdr(skb)->window) <<
  465. newtp->rx_opt.snd_wscale);
  466. newtp->max_window = newtp->snd_wnd;
  467. if (newtp->rx_opt.tstamp_ok) {
  468. newtp->rx_opt.ts_recent = req->ts_recent;
  469. newtp->rx_opt.ts_recent_stamp = get_seconds();
  470. newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  471. } else {
  472. newtp->rx_opt.ts_recent_stamp = 0;
  473. newtp->tcp_header_len = sizeof(struct tcphdr);
  474. }
  475. #ifdef CONFIG_TCP_MD5SIG
  476. newtp->md5sig_info = NULL; /*XXX*/
  477. if (newtp->af_specific->md5_lookup(sk, newsk))
  478. newtp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
  479. #endif
  480. if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
  481. newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
  482. newtp->rx_opt.mss_clamp = req->mss;
  483. TCP_ECN_openreq_child(newtp, req);
  484. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_PASSIVEOPENS);
  485. }
  486. return newsk;
  487. }
  488. EXPORT_SYMBOL(tcp_create_openreq_child);
  489. /*
  490. * Process an incoming packet for SYN_RECV sockets represented
  491. * as a request_sock.
  492. */
  493. struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
  494. struct request_sock *req,
  495. struct request_sock **prev)
  496. {
  497. struct tcp_options_received tmp_opt;
  498. const u8 *hash_location;
  499. struct sock *child;
  500. const struct tcphdr *th = tcp_hdr(skb);
  501. __be32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
  502. int paws_reject = 0;
  503. tmp_opt.saw_tstamp = 0;
  504. if (th->doff > (sizeof(struct tcphdr)>>2)) {
  505. tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
  506. if (tmp_opt.saw_tstamp) {
  507. tmp_opt.ts_recent = req->ts_recent;
  508. /* We do not store true stamp, but it is not required,
  509. * it can be estimated (approximately)
  510. * from another data.
  511. */
  512. tmp_opt.ts_recent_stamp = get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
  513. paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
  514. }
  515. }
  516. /* Check for pure retransmitted SYN. */
  517. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
  518. flg == TCP_FLAG_SYN &&
  519. !paws_reject) {
  520. /*
  521. * RFC793 draws (Incorrectly! It was fixed in RFC1122)
  522. * this case on figure 6 and figure 8, but formal
  523. * protocol description says NOTHING.
  524. * To be more exact, it says that we should send ACK,
  525. * because this segment (at least, if it has no data)
  526. * is out of window.
  527. *
  528. * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
  529. * describe SYN-RECV state. All the description
  530. * is wrong, we cannot believe to it and should
  531. * rely only on common sense and implementation
  532. * experience.
  533. *
  534. * Enforce "SYN-ACK" according to figure 8, figure 6
  535. * of RFC793, fixed by RFC1122.
  536. */
  537. req->rsk_ops->rtx_syn_ack(sk, req, NULL);
  538. return NULL;
  539. }
  540. /* Further reproduces section "SEGMENT ARRIVES"
  541. for state SYN-RECEIVED of RFC793.
  542. It is broken, however, it does not work only
  543. when SYNs are crossed.
  544. You would think that SYN crossing is impossible here, since
  545. we should have a SYN_SENT socket (from connect()) on our end,
  546. but this is not true if the crossed SYNs were sent to both
  547. ends by a malicious third party. We must defend against this,
  548. and to do that we first verify the ACK (as per RFC793, page
  549. 36) and reset if it is invalid. Is this a true full defense?
  550. To convince ourselves, let us consider a way in which the ACK
  551. test can still pass in this 'malicious crossed SYNs' case.
  552. Malicious sender sends identical SYNs (and thus identical sequence
  553. numbers) to both A and B:
  554. A: gets SYN, seq=7
  555. B: gets SYN, seq=7
  556. By our good fortune, both A and B select the same initial
  557. send sequence number of seven :-)
  558. A: sends SYN|ACK, seq=7, ack_seq=8
  559. B: sends SYN|ACK, seq=7, ack_seq=8
  560. So we are now A eating this SYN|ACK, ACK test passes. So
  561. does sequence test, SYN is truncated, and thus we consider
  562. it a bare ACK.
  563. If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
  564. bare ACK. Otherwise, we create an established connection. Both
  565. ends (listening sockets) accept the new incoming connection and try
  566. to talk to each other. 8-)
  567. Note: This case is both harmless, and rare. Possibility is about the
  568. same as us discovering intelligent life on another plant tomorrow.
  569. But generally, we should (RFC lies!) to accept ACK
  570. from SYNACK both here and in tcp_rcv_state_process().
  571. tcp_rcv_state_process() does not, hence, we do not too.
  572. Note that the case is absolutely generic:
  573. we cannot optimize anything here without
  574. violating protocol. All the checks must be made
  575. before attempt to create socket.
  576. */
  577. /* RFC793 page 36: "If the connection is in any non-synchronized state ...
  578. * and the incoming segment acknowledges something not yet
  579. * sent (the segment carries an unacceptable ACK) ...
  580. * a reset is sent."
  581. *
  582. * Invalid ACK: reset will be sent by listening socket
  583. */
  584. if ((flg & TCP_FLAG_ACK) &&
  585. (TCP_SKB_CB(skb)->ack_seq !=
  586. tcp_rsk(req)->snt_isn + 1 + tcp_s_data_size(tcp_sk(sk))))
  587. return sk;
  588. /* Also, it would be not so bad idea to check rcv_tsecr, which
  589. * is essentially ACK extension and too early or too late values
  590. * should cause reset in unsynchronized states.
  591. */
  592. /* RFC793: "first check sequence number". */
  593. if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  594. tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
  595. /* Out of window: send ACK and drop. */
  596. if (!(flg & TCP_FLAG_RST))
  597. req->rsk_ops->send_ack(sk, skb, req);
  598. if (paws_reject)
  599. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
  600. return NULL;
  601. }
  602. /* In sequence, PAWS is OK. */
  603. if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
  604. req->ts_recent = tmp_opt.rcv_tsval;
  605. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
  606. /* Truncate SYN, it is out of window starting
  607. at tcp_rsk(req)->rcv_isn + 1. */
  608. flg &= ~TCP_FLAG_SYN;
  609. }
  610. /* RFC793: "second check the RST bit" and
  611. * "fourth, check the SYN bit"
  612. */
  613. if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN)) {
  614. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_ATTEMPTFAILS);
  615. goto embryonic_reset;
  616. }
  617. /* ACK sequence verified above, just make sure ACK is
  618. * set. If ACK not set, just silently drop the packet.
  619. */
  620. if (!(flg & TCP_FLAG_ACK))
  621. return NULL;
  622. /* While TCP_DEFER_ACCEPT is active, drop bare ACK. */
  623. if (req->retrans < inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
  624. TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
  625. inet_rsk(req)->acked = 1;
  626. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDEFERACCEPTDROP);
  627. return NULL;
  628. }
  629. if (tmp_opt.saw_tstamp && tmp_opt.rcv_tsecr)
  630. tcp_rsk(req)->snt_synack = tmp_opt.rcv_tsecr;
  631. else if (req->retrans) /* don't take RTT sample if retrans && ~TS */
  632. tcp_rsk(req)->snt_synack = 0;
  633. /* OK, ACK is valid, create big socket and
  634. * feed this segment to it. It will repeat all
  635. * the tests. THIS SEGMENT MUST MOVE SOCKET TO
  636. * ESTABLISHED STATE. If it will be dropped after
  637. * socket is created, wait for troubles.
  638. */
  639. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
  640. if (child == NULL)
  641. goto listen_overflow;
  642. inet_csk_reqsk_queue_unlink(sk, req, prev);
  643. inet_csk_reqsk_queue_removed(sk, req);
  644. inet_csk_reqsk_queue_add(sk, req, child);
  645. return child;
  646. listen_overflow:
  647. if (!sysctl_tcp_abort_on_overflow) {
  648. inet_rsk(req)->acked = 1;
  649. return NULL;
  650. }
  651. embryonic_reset:
  652. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_EMBRYONICRSTS);
  653. if (!(flg & TCP_FLAG_RST))
  654. req->rsk_ops->send_reset(sk, skb);
  655. inet_csk_reqsk_queue_drop(sk, req, prev);
  656. return NULL;
  657. }
  658. EXPORT_SYMBOL(tcp_check_req);
  659. /*
  660. * Queue segment on the new socket if the new socket is active,
  661. * otherwise we just shortcircuit this and continue with
  662. * the new socket.
  663. */
  664. int tcp_child_process(struct sock *parent, struct sock *child,
  665. struct sk_buff *skb)
  666. {
  667. int ret = 0;
  668. int state = child->sk_state;
  669. tcp_sk(child)->segs_in += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
  670. if (!sock_owned_by_user(child)) {
  671. ret = tcp_rcv_state_process(child, skb, tcp_hdr(skb),
  672. skb->len);
  673. /* Wakeup parent, send SIGIO */
  674. if (state == TCP_SYN_RECV && child->sk_state != state)
  675. parent->sk_data_ready(parent, 0);
  676. } else {
  677. /* Alas, it is possible again, because we do lookup
  678. * in main socket hash table and lock on listening
  679. * socket does not protect us more.
  680. */
  681. __sk_add_backlog(child, skb);
  682. }
  683. bh_unlock_sock(child);
  684. sock_put(child);
  685. return ret;
  686. }
  687. EXPORT_SYMBOL(tcp_child_process);