tcp_minisocks.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:29k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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