tcp_minisocks.c 31 KB

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