tcp_minisocks.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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/config.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/workqueue.h>
  27. #include <net/tcp.h>
  28. #include <net/inet_common.h>
  29. #include <net/xfrm.h>
  30. #ifdef CONFIG_SYSCTL
  31. #define SYNC_INIT 0 /* let the user enable it */
  32. #else
  33. #define SYNC_INIT 1
  34. #endif
  35. int sysctl_tcp_tw_recycle;
  36. int sysctl_tcp_max_tw_buckets = NR_FILE*2;
  37. int sysctl_tcp_syncookies = SYNC_INIT;
  38. int sysctl_tcp_abort_on_overflow;
  39. static void tcp_tw_schedule(struct inet_timewait_sock *tw, int timeo);
  40. static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
  41. {
  42. if (seq == s_win)
  43. return 1;
  44. if (after(end_seq, s_win) && before(seq, e_win))
  45. return 1;
  46. return (seq == e_win && seq == end_seq);
  47. }
  48. /* New-style handling of TIME_WAIT sockets. */
  49. int tcp_tw_count;
  50. /*
  51. * * Main purpose of TIME-WAIT state is to close connection gracefully,
  52. * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
  53. * (and, probably, tail of data) and one or more our ACKs are lost.
  54. * * What is TIME-WAIT timeout? It is associated with maximal packet
  55. * lifetime in the internet, which results in wrong conclusion, that
  56. * it is set to catch "old duplicate segments" wandering out of their path.
  57. * It is not quite correct. This timeout is calculated so that it exceeds
  58. * maximal retransmission timeout enough to allow to lose one (or more)
  59. * segments sent by peer and our ACKs. This time may be calculated from RTO.
  60. * * When TIME-WAIT socket receives RST, it means that another end
  61. * finally closed and we are allowed to kill TIME-WAIT too.
  62. * * Second purpose of TIME-WAIT is catching old duplicate segments.
  63. * Well, certainly it is pure paranoia, but if we load TIME-WAIT
  64. * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
  65. * * If we invented some more clever way to catch duplicates
  66. * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
  67. *
  68. * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
  69. * When you compare it to RFCs, please, read section SEGMENT ARRIVES
  70. * from the very beginning.
  71. *
  72. * NOTE. With recycling (and later with fin-wait-2) TW bucket
  73. * is _not_ stateless. It means, that strictly speaking we must
  74. * spinlock it. I do not want! Well, probability of misbehaviour
  75. * is ridiculously low and, seems, we could use some mb() tricks
  76. * to avoid misread sequence numbers, states etc. --ANK
  77. */
  78. enum tcp_tw_status
  79. tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
  80. const struct tcphdr *th)
  81. {
  82. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  83. struct tcp_options_received tmp_opt;
  84. int paws_reject = 0;
  85. tmp_opt.saw_tstamp = 0;
  86. if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
  87. tcp_parse_options(skb, &tmp_opt, 0);
  88. if (tmp_opt.saw_tstamp) {
  89. tmp_opt.ts_recent = tcptw->tw_ts_recent;
  90. tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  91. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  92. }
  93. }
  94. if (tw->tw_substate == TCP_FIN_WAIT2) {
  95. /* Just repeat all the checks of tcp_rcv_state_process() */
  96. /* Out of window, send ACK */
  97. if (paws_reject ||
  98. !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  99. tcptw->tw_rcv_nxt,
  100. tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
  101. return TCP_TW_ACK;
  102. if (th->rst)
  103. goto kill;
  104. if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
  105. goto kill_with_rst;
  106. /* Dup ACK? */
  107. if (!after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
  108. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
  109. inet_twsk_put(tw);
  110. return TCP_TW_SUCCESS;
  111. }
  112. /* New data or FIN. If new data arrive after half-duplex close,
  113. * reset.
  114. */
  115. if (!th->fin ||
  116. TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
  117. kill_with_rst:
  118. tcp_tw_deschedule(tw);
  119. inet_twsk_put(tw);
  120. return TCP_TW_RST;
  121. }
  122. /* FIN arrived, enter true time-wait state. */
  123. tw->tw_substate = TCP_TIME_WAIT;
  124. tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  125. if (tmp_opt.saw_tstamp) {
  126. tcptw->tw_ts_recent_stamp = xtime.tv_sec;
  127. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  128. }
  129. /* I am shamed, but failed to make it more elegant.
  130. * Yes, it is direct reference to IP, which is impossible
  131. * to generalize to IPv6. Taking into account that IPv6
  132. * do not undertsnad recycling in any case, it not
  133. * a big problem in practice. --ANK */
  134. if (tw->tw_family == AF_INET &&
  135. sysctl_tcp_tw_recycle && tcptw->tw_ts_recent_stamp &&
  136. tcp_v4_tw_remember_stamp(tw))
  137. tcp_tw_schedule(tw, tw->tw_timeout);
  138. else
  139. tcp_tw_schedule(tw, TCP_TIMEWAIT_LEN);
  140. return TCP_TW_ACK;
  141. }
  142. /*
  143. * Now real TIME-WAIT state.
  144. *
  145. * RFC 1122:
  146. * "When a connection is [...] on TIME-WAIT state [...]
  147. * [a TCP] MAY accept a new SYN from the remote TCP to
  148. * reopen the connection directly, if it:
  149. *
  150. * (1) assigns its initial sequence number for the new
  151. * connection to be larger than the largest sequence
  152. * number it used on the previous connection incarnation,
  153. * and
  154. *
  155. * (2) returns to TIME-WAIT state if the SYN turns out
  156. * to be an old duplicate".
  157. */
  158. if (!paws_reject &&
  159. (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
  160. (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
  161. /* In window segment, it may be only reset or bare ack. */
  162. if (th->rst) {
  163. /* This is TIME_WAIT assasination, in two flavors.
  164. * Oh well... nobody has a sufficient solution to this
  165. * protocol bug yet.
  166. */
  167. if (sysctl_tcp_rfc1337 == 0) {
  168. kill:
  169. tcp_tw_deschedule(tw);
  170. inet_twsk_put(tw);
  171. return TCP_TW_SUCCESS;
  172. }
  173. }
  174. tcp_tw_schedule(tw, TCP_TIMEWAIT_LEN);
  175. if (tmp_opt.saw_tstamp) {
  176. tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
  177. tcptw->tw_ts_recent_stamp = xtime.tv_sec;
  178. }
  179. inet_twsk_put(tw);
  180. return TCP_TW_SUCCESS;
  181. }
  182. /* Out of window segment.
  183. All the segments are ACKed immediately.
  184. The only exception is new SYN. We accept it, if it is
  185. not old duplicate and we are not in danger to be killed
  186. by delayed old duplicates. RFC check is that it has
  187. newer sequence number works at rates <40Mbit/sec.
  188. However, if paws works, it is reliable AND even more,
  189. we even may relax silly seq space cutoff.
  190. RED-PEN: we violate main RFC requirement, if this SYN will appear
  191. old duplicate (i.e. we receive RST in reply to SYN-ACK),
  192. we must return socket to time-wait state. It is not good,
  193. but not fatal yet.
  194. */
  195. if (th->syn && !th->rst && !th->ack && !paws_reject &&
  196. (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
  197. (tmp_opt.saw_tstamp &&
  198. (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
  199. u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
  200. if (isn == 0)
  201. isn++;
  202. TCP_SKB_CB(skb)->when = isn;
  203. return TCP_TW_SYN;
  204. }
  205. if (paws_reject)
  206. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  207. if(!th->rst) {
  208. /* In this case we must reset the TIMEWAIT timer.
  209. *
  210. * If it is ACKless SYN it may be both old duplicate
  211. * and new good SYN with random sequence number <rcv_nxt.
  212. * Do not reschedule in the last case.
  213. */
  214. if (paws_reject || th->ack)
  215. tcp_tw_schedule(tw, TCP_TIMEWAIT_LEN);
  216. /* Send ACK. Note, we do not put the bucket,
  217. * it will be released by caller.
  218. */
  219. return TCP_TW_ACK;
  220. }
  221. inet_twsk_put(tw);
  222. return TCP_TW_SUCCESS;
  223. }
  224. /*
  225. * Move a socket to time-wait or dead fin-wait-2 state.
  226. */
  227. void tcp_time_wait(struct sock *sk, int state, int timeo)
  228. {
  229. struct inet_timewait_sock *tw = NULL;
  230. const struct tcp_sock *tp = tcp_sk(sk);
  231. int recycle_ok = 0;
  232. if (sysctl_tcp_tw_recycle && tp->rx_opt.ts_recent_stamp)
  233. recycle_ok = tp->af_specific->remember_stamp(sk);
  234. if (tcp_tw_count < sysctl_tcp_max_tw_buckets)
  235. tw = inet_twsk_alloc(sk, state);
  236. if (tw != NULL) {
  237. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  238. const int rto = (tp->rto << 2) - (tp->rto >> 1);
  239. tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
  240. tcptw->tw_rcv_nxt = tp->rcv_nxt;
  241. tcptw->tw_snd_nxt = tp->snd_nxt;
  242. tcptw->tw_rcv_wnd = tcp_receive_window(tp);
  243. tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
  244. tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
  245. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  246. if (tw->tw_family == PF_INET6) {
  247. struct ipv6_pinfo *np = inet6_sk(sk);
  248. struct tcp6_timewait_sock *tcp6tw = tcp6_twsk((struct sock *)tw);
  249. ipv6_addr_copy(&tcp6tw->tw_v6_daddr, &np->daddr);
  250. ipv6_addr_copy(&tcp6tw->tw_v6_rcv_saddr, &np->rcv_saddr);
  251. tw->tw_ipv6only = np->ipv6only;
  252. }
  253. #endif
  254. /* Linkage updates. */
  255. __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
  256. /* Get the TIME_WAIT timeout firing. */
  257. if (timeo < rto)
  258. timeo = rto;
  259. if (recycle_ok) {
  260. tw->tw_timeout = rto;
  261. } else {
  262. tw->tw_timeout = TCP_TIMEWAIT_LEN;
  263. if (state == TCP_TIME_WAIT)
  264. timeo = TCP_TIMEWAIT_LEN;
  265. }
  266. tcp_tw_schedule(tw, timeo);
  267. inet_twsk_put(tw);
  268. } else {
  269. /* Sorry, if we're out of memory, just CLOSE this
  270. * socket up. We've got bigger problems than
  271. * non-graceful socket closings.
  272. */
  273. if (net_ratelimit())
  274. printk(KERN_INFO "TCP: time wait bucket table overflow\n");
  275. }
  276. tcp_update_metrics(sk);
  277. tcp_done(sk);
  278. }
  279. /* Kill off TIME_WAIT sockets once their lifetime has expired. */
  280. static int tcp_tw_death_row_slot;
  281. static void tcp_twkill(unsigned long);
  282. /* TIME_WAIT reaping mechanism. */
  283. #define TCP_TWKILL_SLOTS 8 /* Please keep this a power of 2. */
  284. #define TCP_TWKILL_PERIOD (TCP_TIMEWAIT_LEN/TCP_TWKILL_SLOTS)
  285. #define TCP_TWKILL_QUOTA 100
  286. static struct hlist_head tcp_tw_death_row[TCP_TWKILL_SLOTS];
  287. static DEFINE_SPINLOCK(tw_death_lock);
  288. static struct timer_list tcp_tw_timer = TIMER_INITIALIZER(tcp_twkill, 0, 0);
  289. static void twkill_work(void *);
  290. static DECLARE_WORK(tcp_twkill_work, twkill_work, NULL);
  291. static u32 twkill_thread_slots;
  292. /* Returns non-zero if quota exceeded. */
  293. static int tcp_do_twkill_work(int slot, unsigned int quota)
  294. {
  295. struct inet_timewait_sock *tw;
  296. struct hlist_node *node;
  297. unsigned int killed;
  298. int ret;
  299. /* NOTE: compare this to previous version where lock
  300. * was released after detaching chain. It was racy,
  301. * because tw buckets are scheduled in not serialized context
  302. * in 2.3 (with netfilter), and with softnet it is common, because
  303. * soft irqs are not sequenced.
  304. */
  305. killed = 0;
  306. ret = 0;
  307. rescan:
  308. inet_twsk_for_each_inmate(tw, node, &tcp_tw_death_row[slot]) {
  309. __inet_twsk_del_dead_node(tw);
  310. spin_unlock(&tw_death_lock);
  311. __inet_twsk_kill(tw, &tcp_hashinfo);
  312. inet_twsk_put(tw);
  313. killed++;
  314. spin_lock(&tw_death_lock);
  315. if (killed > quota) {
  316. ret = 1;
  317. break;
  318. }
  319. /* While we dropped tw_death_lock, another cpu may have
  320. * killed off the next TW bucket in the list, therefore
  321. * do a fresh re-read of the hlist head node with the
  322. * lock reacquired. We still use the hlist traversal
  323. * macro in order to get the prefetches.
  324. */
  325. goto rescan;
  326. }
  327. tcp_tw_count -= killed;
  328. NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
  329. return ret;
  330. }
  331. static void tcp_twkill(unsigned long dummy)
  332. {
  333. int need_timer, ret;
  334. spin_lock(&tw_death_lock);
  335. if (tcp_tw_count == 0)
  336. goto out;
  337. need_timer = 0;
  338. ret = tcp_do_twkill_work(tcp_tw_death_row_slot, TCP_TWKILL_QUOTA);
  339. if (ret) {
  340. twkill_thread_slots |= (1 << tcp_tw_death_row_slot);
  341. mb();
  342. schedule_work(&tcp_twkill_work);
  343. need_timer = 1;
  344. } else {
  345. /* We purged the entire slot, anything left? */
  346. if (tcp_tw_count)
  347. need_timer = 1;
  348. }
  349. tcp_tw_death_row_slot =
  350. ((tcp_tw_death_row_slot + 1) & (TCP_TWKILL_SLOTS - 1));
  351. if (need_timer)
  352. mod_timer(&tcp_tw_timer, jiffies + TCP_TWKILL_PERIOD);
  353. out:
  354. spin_unlock(&tw_death_lock);
  355. }
  356. extern void twkill_slots_invalid(void);
  357. static void twkill_work(void *dummy)
  358. {
  359. int i;
  360. if ((TCP_TWKILL_SLOTS - 1) > (sizeof(twkill_thread_slots) * 8))
  361. twkill_slots_invalid();
  362. while (twkill_thread_slots) {
  363. spin_lock_bh(&tw_death_lock);
  364. for (i = 0; i < TCP_TWKILL_SLOTS; i++) {
  365. if (!(twkill_thread_slots & (1 << i)))
  366. continue;
  367. while (tcp_do_twkill_work(i, TCP_TWKILL_QUOTA) != 0) {
  368. if (need_resched()) {
  369. spin_unlock_bh(&tw_death_lock);
  370. schedule();
  371. spin_lock_bh(&tw_death_lock);
  372. }
  373. }
  374. twkill_thread_slots &= ~(1 << i);
  375. }
  376. spin_unlock_bh(&tw_death_lock);
  377. }
  378. }
  379. /* These are always called from BH context. See callers in
  380. * tcp_input.c to verify this.
  381. */
  382. /* This is for handling early-kills of TIME_WAIT sockets. */
  383. void tcp_tw_deschedule(struct inet_timewait_sock *tw)
  384. {
  385. spin_lock(&tw_death_lock);
  386. if (inet_twsk_del_dead_node(tw)) {
  387. inet_twsk_put(tw);
  388. if (--tcp_tw_count == 0)
  389. del_timer(&tcp_tw_timer);
  390. }
  391. spin_unlock(&tw_death_lock);
  392. __inet_twsk_kill(tw, &tcp_hashinfo);
  393. }
  394. /* Short-time timewait calendar */
  395. static int tcp_twcal_hand = -1;
  396. static int tcp_twcal_jiffie;
  397. static void tcp_twcal_tick(unsigned long);
  398. static struct timer_list tcp_twcal_timer =
  399. TIMER_INITIALIZER(tcp_twcal_tick, 0, 0);
  400. static struct hlist_head tcp_twcal_row[TCP_TW_RECYCLE_SLOTS];
  401. static void tcp_tw_schedule(struct inet_timewait_sock *tw, const int timeo)
  402. {
  403. struct hlist_head *list;
  404. int slot;
  405. /* timeout := RTO * 3.5
  406. *
  407. * 3.5 = 1+2+0.5 to wait for two retransmits.
  408. *
  409. * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
  410. * our ACK acking that FIN can be lost. If N subsequent retransmitted
  411. * FINs (or previous seqments) are lost (probability of such event
  412. * is p^(N+1), where p is probability to lose single packet and
  413. * time to detect the loss is about RTO*(2^N - 1) with exponential
  414. * backoff). Normal timewait length is calculated so, that we
  415. * waited at least for one retransmitted FIN (maximal RTO is 120sec).
  416. * [ BTW Linux. following BSD, violates this requirement waiting
  417. * only for 60sec, we should wait at least for 240 secs.
  418. * Well, 240 consumes too much of resources 8)
  419. * ]
  420. * This interval is not reduced to catch old duplicate and
  421. * responces to our wandering segments living for two MSLs.
  422. * However, if we use PAWS to detect
  423. * old duplicates, we can reduce the interval to bounds required
  424. * by RTO, rather than MSL. So, if peer understands PAWS, we
  425. * kill tw bucket after 3.5*RTO (it is important that this number
  426. * is greater than TS tick!) and detect old duplicates with help
  427. * of PAWS.
  428. */
  429. slot = (timeo + (1<<TCP_TW_RECYCLE_TICK) - 1) >> TCP_TW_RECYCLE_TICK;
  430. spin_lock(&tw_death_lock);
  431. /* Unlink it, if it was scheduled */
  432. if (inet_twsk_del_dead_node(tw))
  433. tcp_tw_count--;
  434. else
  435. atomic_inc(&tw->tw_refcnt);
  436. if (slot >= TCP_TW_RECYCLE_SLOTS) {
  437. /* Schedule to slow timer */
  438. if (timeo >= TCP_TIMEWAIT_LEN) {
  439. slot = TCP_TWKILL_SLOTS-1;
  440. } else {
  441. slot = (timeo + TCP_TWKILL_PERIOD-1) / TCP_TWKILL_PERIOD;
  442. if (slot >= TCP_TWKILL_SLOTS)
  443. slot = TCP_TWKILL_SLOTS-1;
  444. }
  445. tw->tw_ttd = jiffies + timeo;
  446. slot = (tcp_tw_death_row_slot + slot) & (TCP_TWKILL_SLOTS - 1);
  447. list = &tcp_tw_death_row[slot];
  448. } else {
  449. tw->tw_ttd = jiffies + (slot << TCP_TW_RECYCLE_TICK);
  450. if (tcp_twcal_hand < 0) {
  451. tcp_twcal_hand = 0;
  452. tcp_twcal_jiffie = jiffies;
  453. tcp_twcal_timer.expires = tcp_twcal_jiffie + (slot<<TCP_TW_RECYCLE_TICK);
  454. add_timer(&tcp_twcal_timer);
  455. } else {
  456. if (time_after(tcp_twcal_timer.expires, jiffies + (slot<<TCP_TW_RECYCLE_TICK)))
  457. mod_timer(&tcp_twcal_timer, jiffies + (slot<<TCP_TW_RECYCLE_TICK));
  458. slot = (tcp_twcal_hand + slot)&(TCP_TW_RECYCLE_SLOTS-1);
  459. }
  460. list = &tcp_twcal_row[slot];
  461. }
  462. hlist_add_head(&tw->tw_death_node, list);
  463. if (tcp_tw_count++ == 0)
  464. mod_timer(&tcp_tw_timer, jiffies+TCP_TWKILL_PERIOD);
  465. spin_unlock(&tw_death_lock);
  466. }
  467. void tcp_twcal_tick(unsigned long dummy)
  468. {
  469. int n, slot;
  470. unsigned long j;
  471. unsigned long now = jiffies;
  472. int killed = 0;
  473. int adv = 0;
  474. spin_lock(&tw_death_lock);
  475. if (tcp_twcal_hand < 0)
  476. goto out;
  477. slot = tcp_twcal_hand;
  478. j = tcp_twcal_jiffie;
  479. for (n=0; n<TCP_TW_RECYCLE_SLOTS; n++) {
  480. if (time_before_eq(j, now)) {
  481. struct hlist_node *node, *safe;
  482. struct inet_timewait_sock *tw;
  483. inet_twsk_for_each_inmate_safe(tw, node, safe,
  484. &tcp_twcal_row[slot]) {
  485. __inet_twsk_del_dead_node(tw);
  486. __inet_twsk_kill(tw, &tcp_hashinfo);
  487. inet_twsk_put(tw);
  488. killed++;
  489. }
  490. } else {
  491. if (!adv) {
  492. adv = 1;
  493. tcp_twcal_jiffie = j;
  494. tcp_twcal_hand = slot;
  495. }
  496. if (!hlist_empty(&tcp_twcal_row[slot])) {
  497. mod_timer(&tcp_twcal_timer, j);
  498. goto out;
  499. }
  500. }
  501. j += (1<<TCP_TW_RECYCLE_TICK);
  502. slot = (slot+1)&(TCP_TW_RECYCLE_SLOTS-1);
  503. }
  504. tcp_twcal_hand = -1;
  505. out:
  506. if ((tcp_tw_count -= killed) == 0)
  507. del_timer(&tcp_tw_timer);
  508. NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
  509. spin_unlock(&tw_death_lock);
  510. }
  511. /* This is not only more efficient than what we used to do, it eliminates
  512. * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
  513. *
  514. * Actually, we could lots of memory writes here. tp of listening
  515. * socket contains all necessary default parameters.
  516. */
  517. struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
  518. {
  519. struct sock *newsk = sk_clone(sk, GFP_ATOMIC);
  520. if (newsk != NULL) {
  521. struct inet_request_sock *ireq = inet_rsk(req);
  522. struct tcp_request_sock *treq = tcp_rsk(req);
  523. struct inet_sock *newinet = inet_sk(newsk);
  524. struct tcp_sock *newtp;
  525. newsk->sk_state = TCP_SYN_RECV;
  526. newinet->bind_hash = NULL;
  527. /* Clone the TCP header template */
  528. newinet->dport = ireq->rmt_port;
  529. newsk->sk_write_space = sk_stream_write_space;
  530. /* Now setup tcp_sock */
  531. newtp = tcp_sk(newsk);
  532. newtp->pred_flags = 0;
  533. newtp->rcv_nxt = treq->rcv_isn + 1;
  534. newtp->snd_nxt = newtp->snd_una = newtp->snd_sml = treq->snt_isn + 1;
  535. tcp_prequeue_init(newtp);
  536. tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);
  537. newtp->retransmits = 0;
  538. newtp->backoff = 0;
  539. newtp->srtt = 0;
  540. newtp->mdev = TCP_TIMEOUT_INIT;
  541. newtp->rto = TCP_TIMEOUT_INIT;
  542. newtp->packets_out = 0;
  543. newtp->left_out = 0;
  544. newtp->retrans_out = 0;
  545. newtp->sacked_out = 0;
  546. newtp->fackets_out = 0;
  547. newtp->snd_ssthresh = 0x7fffffff;
  548. /* So many TCP implementations out there (incorrectly) count the
  549. * initial SYN frame in their delayed-ACK and congestion control
  550. * algorithms that we must have the following bandaid to talk
  551. * efficiently to them. -DaveM
  552. */
  553. newtp->snd_cwnd = 2;
  554. newtp->snd_cwnd_cnt = 0;
  555. newtp->frto_counter = 0;
  556. newtp->frto_highmark = 0;
  557. newtp->ca_ops = &tcp_reno;
  558. tcp_set_ca_state(newtp, TCP_CA_Open);
  559. tcp_init_xmit_timers(newsk);
  560. skb_queue_head_init(&newtp->out_of_order_queue);
  561. newtp->rcv_wup = treq->rcv_isn + 1;
  562. newtp->write_seq = treq->snt_isn + 1;
  563. newtp->pushed_seq = newtp->write_seq;
  564. newtp->copied_seq = treq->rcv_isn + 1;
  565. newtp->rx_opt.saw_tstamp = 0;
  566. newtp->rx_opt.dsack = 0;
  567. newtp->rx_opt.eff_sacks = 0;
  568. newtp->probes_out = 0;
  569. newtp->rx_opt.num_sacks = 0;
  570. newtp->urg_data = 0;
  571. /* Deinitialize accept_queue to trap illegal accesses. */
  572. memset(&newtp->accept_queue, 0, sizeof(newtp->accept_queue));
  573. if (sock_flag(newsk, SOCK_KEEPOPEN))
  574. tcp_reset_keepalive_timer(newsk,
  575. keepalive_time_when(newtp));
  576. newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
  577. if((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
  578. if (sysctl_tcp_fack)
  579. newtp->rx_opt.sack_ok |= 2;
  580. }
  581. newtp->window_clamp = req->window_clamp;
  582. newtp->rcv_ssthresh = req->rcv_wnd;
  583. newtp->rcv_wnd = req->rcv_wnd;
  584. newtp->rx_opt.wscale_ok = ireq->wscale_ok;
  585. if (newtp->rx_opt.wscale_ok) {
  586. newtp->rx_opt.snd_wscale = ireq->snd_wscale;
  587. newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
  588. } else {
  589. newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
  590. newtp->window_clamp = min(newtp->window_clamp, 65535U);
  591. }
  592. newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->rx_opt.snd_wscale;
  593. newtp->max_window = newtp->snd_wnd;
  594. if (newtp->rx_opt.tstamp_ok) {
  595. newtp->rx_opt.ts_recent = req->ts_recent;
  596. newtp->rx_opt.ts_recent_stamp = xtime.tv_sec;
  597. newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  598. } else {
  599. newtp->rx_opt.ts_recent_stamp = 0;
  600. newtp->tcp_header_len = sizeof(struct tcphdr);
  601. }
  602. if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
  603. newtp->ack.last_seg_size = skb->len-newtp->tcp_header_len;
  604. newtp->rx_opt.mss_clamp = req->mss;
  605. TCP_ECN_openreq_child(newtp, req);
  606. if (newtp->ecn_flags&TCP_ECN_OK)
  607. sock_set_flag(newsk, SOCK_NO_LARGESEND);
  608. TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);
  609. }
  610. return newsk;
  611. }
  612. /*
  613. * Process an incoming packet for SYN_RECV sockets represented
  614. * as a request_sock.
  615. */
  616. struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
  617. struct request_sock *req,
  618. struct request_sock **prev)
  619. {
  620. struct tcphdr *th = skb->h.th;
  621. struct tcp_sock *tp = tcp_sk(sk);
  622. u32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
  623. int paws_reject = 0;
  624. struct tcp_options_received tmp_opt;
  625. struct sock *child;
  626. tmp_opt.saw_tstamp = 0;
  627. if (th->doff > (sizeof(struct tcphdr)>>2)) {
  628. tcp_parse_options(skb, &tmp_opt, 0);
  629. if (tmp_opt.saw_tstamp) {
  630. tmp_opt.ts_recent = req->ts_recent;
  631. /* We do not store true stamp, but it is not required,
  632. * it can be estimated (approximately)
  633. * from another data.
  634. */
  635. tmp_opt.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
  636. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  637. }
  638. }
  639. /* Check for pure retransmitted SYN. */
  640. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
  641. flg == TCP_FLAG_SYN &&
  642. !paws_reject) {
  643. /*
  644. * RFC793 draws (Incorrectly! It was fixed in RFC1122)
  645. * this case on figure 6 and figure 8, but formal
  646. * protocol description says NOTHING.
  647. * To be more exact, it says that we should send ACK,
  648. * because this segment (at least, if it has no data)
  649. * is out of window.
  650. *
  651. * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
  652. * describe SYN-RECV state. All the description
  653. * is wrong, we cannot believe to it and should
  654. * rely only on common sense and implementation
  655. * experience.
  656. *
  657. * Enforce "SYN-ACK" according to figure 8, figure 6
  658. * of RFC793, fixed by RFC1122.
  659. */
  660. req->rsk_ops->rtx_syn_ack(sk, req, NULL);
  661. return NULL;
  662. }
  663. /* Further reproduces section "SEGMENT ARRIVES"
  664. for state SYN-RECEIVED of RFC793.
  665. It is broken, however, it does not work only
  666. when SYNs are crossed.
  667. You would think that SYN crossing is impossible here, since
  668. we should have a SYN_SENT socket (from connect()) on our end,
  669. but this is not true if the crossed SYNs were sent to both
  670. ends by a malicious third party. We must defend against this,
  671. and to do that we first verify the ACK (as per RFC793, page
  672. 36) and reset if it is invalid. Is this a true full defense?
  673. To convince ourselves, let us consider a way in which the ACK
  674. test can still pass in this 'malicious crossed SYNs' case.
  675. Malicious sender sends identical SYNs (and thus identical sequence
  676. numbers) to both A and B:
  677. A: gets SYN, seq=7
  678. B: gets SYN, seq=7
  679. By our good fortune, both A and B select the same initial
  680. send sequence number of seven :-)
  681. A: sends SYN|ACK, seq=7, ack_seq=8
  682. B: sends SYN|ACK, seq=7, ack_seq=8
  683. So we are now A eating this SYN|ACK, ACK test passes. So
  684. does sequence test, SYN is truncated, and thus we consider
  685. it a bare ACK.
  686. If tp->defer_accept, we silently drop this bare ACK. Otherwise,
  687. we create an established connection. Both ends (listening sockets)
  688. accept the new incoming connection and try to talk to each other. 8-)
  689. Note: This case is both harmless, and rare. Possibility is about the
  690. same as us discovering intelligent life on another plant tomorrow.
  691. But generally, we should (RFC lies!) to accept ACK
  692. from SYNACK both here and in tcp_rcv_state_process().
  693. tcp_rcv_state_process() does not, hence, we do not too.
  694. Note that the case is absolutely generic:
  695. we cannot optimize anything here without
  696. violating protocol. All the checks must be made
  697. before attempt to create socket.
  698. */
  699. /* RFC793 page 36: "If the connection is in any non-synchronized state ...
  700. * and the incoming segment acknowledges something not yet
  701. * sent (the segment carries an unaccaptable ACK) ...
  702. * a reset is sent."
  703. *
  704. * Invalid ACK: reset will be sent by listening socket
  705. */
  706. if ((flg & TCP_FLAG_ACK) &&
  707. (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))
  708. return sk;
  709. /* Also, it would be not so bad idea to check rcv_tsecr, which
  710. * is essentially ACK extension and too early or too late values
  711. * should cause reset in unsynchronized states.
  712. */
  713. /* RFC793: "first check sequence number". */
  714. if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  715. tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
  716. /* Out of window: send ACK and drop. */
  717. if (!(flg & TCP_FLAG_RST))
  718. req->rsk_ops->send_ack(skb, req);
  719. if (paws_reject)
  720. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  721. return NULL;
  722. }
  723. /* In sequence, PAWS is OK. */
  724. if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
  725. req->ts_recent = tmp_opt.rcv_tsval;
  726. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
  727. /* Truncate SYN, it is out of window starting
  728. at tcp_rsk(req)->rcv_isn + 1. */
  729. flg &= ~TCP_FLAG_SYN;
  730. }
  731. /* RFC793: "second check the RST bit" and
  732. * "fourth, check the SYN bit"
  733. */
  734. if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN))
  735. goto embryonic_reset;
  736. /* ACK sequence verified above, just make sure ACK is
  737. * set. If ACK not set, just silently drop the packet.
  738. */
  739. if (!(flg & TCP_FLAG_ACK))
  740. return NULL;
  741. /* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
  742. if (tp->defer_accept && TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
  743. inet_rsk(req)->acked = 1;
  744. return NULL;
  745. }
  746. /* OK, ACK is valid, create big socket and
  747. * feed this segment to it. It will repeat all
  748. * the tests. THIS SEGMENT MUST MOVE SOCKET TO
  749. * ESTABLISHED STATE. If it will be dropped after
  750. * socket is created, wait for troubles.
  751. */
  752. child = tp->af_specific->syn_recv_sock(sk, skb, req, NULL);
  753. if (child == NULL)
  754. goto listen_overflow;
  755. tcp_synq_unlink(tp, req, prev);
  756. tcp_synq_removed(sk, req);
  757. tcp_acceptq_queue(sk, req, child);
  758. return child;
  759. listen_overflow:
  760. if (!sysctl_tcp_abort_on_overflow) {
  761. inet_rsk(req)->acked = 1;
  762. return NULL;
  763. }
  764. embryonic_reset:
  765. NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);
  766. if (!(flg & TCP_FLAG_RST))
  767. req->rsk_ops->send_reset(skb);
  768. tcp_synq_drop(sk, req, prev);
  769. return NULL;
  770. }
  771. /*
  772. * Queue segment on the new socket if the new socket is active,
  773. * otherwise we just shortcircuit this and continue with
  774. * the new socket.
  775. */
  776. int tcp_child_process(struct sock *parent, struct sock *child,
  777. struct sk_buff *skb)
  778. {
  779. int ret = 0;
  780. int state = child->sk_state;
  781. if (!sock_owned_by_user(child)) {
  782. ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
  783. /* Wakeup parent, send SIGIO */
  784. if (state == TCP_SYN_RECV && child->sk_state != state)
  785. parent->sk_data_ready(parent, 0);
  786. } else {
  787. /* Alas, it is possible again, because we do lookup
  788. * in main socket hash table and lock on listening
  789. * socket does not protect us more.
  790. */
  791. sk_add_backlog(child, skb);
  792. }
  793. bh_unlock_sock(child);
  794. sock_put(child);
  795. return ret;
  796. }
  797. EXPORT_SYMBOL(tcp_check_req);
  798. EXPORT_SYMBOL(tcp_child_process);
  799. EXPORT_SYMBOL(tcp_create_openreq_child);
  800. EXPORT_SYMBOL(tcp_timewait_state_process);
  801. EXPORT_SYMBOL(tcp_tw_deschedule);