tcp_minisocks.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 = kmem_cache_alloc(sk->sk_prot_creator->twsk_slab, SLAB_ATOMIC);
  236. if (tw != NULL) {
  237. struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  238. const struct inet_sock *inet = inet_sk(sk);
  239. const int rto = (tp->rto << 2) - (tp->rto >> 1);
  240. /* Remember our protocol */
  241. tw->tw_prot = sk->sk_prot_creator;
  242. /* Give us an identity. */
  243. tw->tw_daddr = inet->daddr;
  244. tw->tw_rcv_saddr = inet->rcv_saddr;
  245. tw->tw_bound_dev_if = sk->sk_bound_dev_if;
  246. tw->tw_num = inet->num;
  247. tw->tw_state = TCP_TIME_WAIT;
  248. tw->tw_substate = state;
  249. tw->tw_sport = inet->sport;
  250. tw->tw_dport = inet->dport;
  251. tw->tw_family = sk->sk_family;
  252. tw->tw_reuse = sk->sk_reuse;
  253. tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
  254. atomic_set(&tw->tw_refcnt, 1);
  255. tw->tw_hashent = sk->sk_hashent;
  256. tcptw->tw_rcv_nxt = tp->rcv_nxt;
  257. tcptw->tw_snd_nxt = tp->snd_nxt;
  258. tcptw->tw_rcv_wnd = tcp_receive_window(tp);
  259. tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
  260. tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
  261. inet_twsk_dead_node_init(tw);
  262. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  263. if (tw->tw_family == PF_INET6) {
  264. struct ipv6_pinfo *np = inet6_sk(sk);
  265. struct tcp6_timewait_sock *tcp6tw = tcp6_twsk((struct sock *)tw);
  266. ipv6_addr_copy(&tcp6tw->tw_v6_daddr, &np->daddr);
  267. ipv6_addr_copy(&tcp6tw->tw_v6_rcv_saddr, &np->rcv_saddr);
  268. tw->tw_ipv6only = np->ipv6only;
  269. } else
  270. tw->tw_ipv6only = 0;
  271. #endif
  272. /* Linkage updates. */
  273. __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
  274. /* Get the TIME_WAIT timeout firing. */
  275. if (timeo < rto)
  276. timeo = rto;
  277. if (recycle_ok) {
  278. tw->tw_timeout = rto;
  279. } else {
  280. tw->tw_timeout = TCP_TIMEWAIT_LEN;
  281. if (state == TCP_TIME_WAIT)
  282. timeo = TCP_TIMEWAIT_LEN;
  283. }
  284. tcp_tw_schedule(tw, timeo);
  285. inet_twsk_put(tw);
  286. } else {
  287. /* Sorry, if we're out of memory, just CLOSE this
  288. * socket up. We've got bigger problems than
  289. * non-graceful socket closings.
  290. */
  291. if (net_ratelimit())
  292. printk(KERN_INFO "TCP: time wait bucket table overflow\n");
  293. }
  294. tcp_update_metrics(sk);
  295. tcp_done(sk);
  296. }
  297. /* Kill off TIME_WAIT sockets once their lifetime has expired. */
  298. static int tcp_tw_death_row_slot;
  299. static void tcp_twkill(unsigned long);
  300. /* TIME_WAIT reaping mechanism. */
  301. #define TCP_TWKILL_SLOTS 8 /* Please keep this a power of 2. */
  302. #define TCP_TWKILL_PERIOD (TCP_TIMEWAIT_LEN/TCP_TWKILL_SLOTS)
  303. #define TCP_TWKILL_QUOTA 100
  304. static struct hlist_head tcp_tw_death_row[TCP_TWKILL_SLOTS];
  305. static DEFINE_SPINLOCK(tw_death_lock);
  306. static struct timer_list tcp_tw_timer = TIMER_INITIALIZER(tcp_twkill, 0, 0);
  307. static void twkill_work(void *);
  308. static DECLARE_WORK(tcp_twkill_work, twkill_work, NULL);
  309. static u32 twkill_thread_slots;
  310. /* Returns non-zero if quota exceeded. */
  311. static int tcp_do_twkill_work(int slot, unsigned int quota)
  312. {
  313. struct inet_timewait_sock *tw;
  314. struct hlist_node *node;
  315. unsigned int killed;
  316. int ret;
  317. /* NOTE: compare this to previous version where lock
  318. * was released after detaching chain. It was racy,
  319. * because tw buckets are scheduled in not serialized context
  320. * in 2.3 (with netfilter), and with softnet it is common, because
  321. * soft irqs are not sequenced.
  322. */
  323. killed = 0;
  324. ret = 0;
  325. rescan:
  326. inet_twsk_for_each_inmate(tw, node, &tcp_tw_death_row[slot]) {
  327. __inet_twsk_del_dead_node(tw);
  328. spin_unlock(&tw_death_lock);
  329. __inet_twsk_kill(tw, &tcp_hashinfo);
  330. inet_twsk_put(tw);
  331. killed++;
  332. spin_lock(&tw_death_lock);
  333. if (killed > quota) {
  334. ret = 1;
  335. break;
  336. }
  337. /* While we dropped tw_death_lock, another cpu may have
  338. * killed off the next TW bucket in the list, therefore
  339. * do a fresh re-read of the hlist head node with the
  340. * lock reacquired. We still use the hlist traversal
  341. * macro in order to get the prefetches.
  342. */
  343. goto rescan;
  344. }
  345. tcp_tw_count -= killed;
  346. NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
  347. return ret;
  348. }
  349. static void tcp_twkill(unsigned long dummy)
  350. {
  351. int need_timer, ret;
  352. spin_lock(&tw_death_lock);
  353. if (tcp_tw_count == 0)
  354. goto out;
  355. need_timer = 0;
  356. ret = tcp_do_twkill_work(tcp_tw_death_row_slot, TCP_TWKILL_QUOTA);
  357. if (ret) {
  358. twkill_thread_slots |= (1 << tcp_tw_death_row_slot);
  359. mb();
  360. schedule_work(&tcp_twkill_work);
  361. need_timer = 1;
  362. } else {
  363. /* We purged the entire slot, anything left? */
  364. if (tcp_tw_count)
  365. need_timer = 1;
  366. }
  367. tcp_tw_death_row_slot =
  368. ((tcp_tw_death_row_slot + 1) & (TCP_TWKILL_SLOTS - 1));
  369. if (need_timer)
  370. mod_timer(&tcp_tw_timer, jiffies + TCP_TWKILL_PERIOD);
  371. out:
  372. spin_unlock(&tw_death_lock);
  373. }
  374. extern void twkill_slots_invalid(void);
  375. static void twkill_work(void *dummy)
  376. {
  377. int i;
  378. if ((TCP_TWKILL_SLOTS - 1) > (sizeof(twkill_thread_slots) * 8))
  379. twkill_slots_invalid();
  380. while (twkill_thread_slots) {
  381. spin_lock_bh(&tw_death_lock);
  382. for (i = 0; i < TCP_TWKILL_SLOTS; i++) {
  383. if (!(twkill_thread_slots & (1 << i)))
  384. continue;
  385. while (tcp_do_twkill_work(i, TCP_TWKILL_QUOTA) != 0) {
  386. if (need_resched()) {
  387. spin_unlock_bh(&tw_death_lock);
  388. schedule();
  389. spin_lock_bh(&tw_death_lock);
  390. }
  391. }
  392. twkill_thread_slots &= ~(1 << i);
  393. }
  394. spin_unlock_bh(&tw_death_lock);
  395. }
  396. }
  397. /* These are always called from BH context. See callers in
  398. * tcp_input.c to verify this.
  399. */
  400. /* This is for handling early-kills of TIME_WAIT sockets. */
  401. void tcp_tw_deschedule(struct inet_timewait_sock *tw)
  402. {
  403. spin_lock(&tw_death_lock);
  404. if (inet_twsk_del_dead_node(tw)) {
  405. inet_twsk_put(tw);
  406. if (--tcp_tw_count == 0)
  407. del_timer(&tcp_tw_timer);
  408. }
  409. spin_unlock(&tw_death_lock);
  410. __inet_twsk_kill(tw, &tcp_hashinfo);
  411. }
  412. /* Short-time timewait calendar */
  413. static int tcp_twcal_hand = -1;
  414. static int tcp_twcal_jiffie;
  415. static void tcp_twcal_tick(unsigned long);
  416. static struct timer_list tcp_twcal_timer =
  417. TIMER_INITIALIZER(tcp_twcal_tick, 0, 0);
  418. static struct hlist_head tcp_twcal_row[TCP_TW_RECYCLE_SLOTS];
  419. static void tcp_tw_schedule(struct inet_timewait_sock *tw, const int timeo)
  420. {
  421. struct hlist_head *list;
  422. int slot;
  423. /* timeout := RTO * 3.5
  424. *
  425. * 3.5 = 1+2+0.5 to wait for two retransmits.
  426. *
  427. * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
  428. * our ACK acking that FIN can be lost. If N subsequent retransmitted
  429. * FINs (or previous seqments) are lost (probability of such event
  430. * is p^(N+1), where p is probability to lose single packet and
  431. * time to detect the loss is about RTO*(2^N - 1) with exponential
  432. * backoff). Normal timewait length is calculated so, that we
  433. * waited at least for one retransmitted FIN (maximal RTO is 120sec).
  434. * [ BTW Linux. following BSD, violates this requirement waiting
  435. * only for 60sec, we should wait at least for 240 secs.
  436. * Well, 240 consumes too much of resources 8)
  437. * ]
  438. * This interval is not reduced to catch old duplicate and
  439. * responces to our wandering segments living for two MSLs.
  440. * However, if we use PAWS to detect
  441. * old duplicates, we can reduce the interval to bounds required
  442. * by RTO, rather than MSL. So, if peer understands PAWS, we
  443. * kill tw bucket after 3.5*RTO (it is important that this number
  444. * is greater than TS tick!) and detect old duplicates with help
  445. * of PAWS.
  446. */
  447. slot = (timeo + (1<<TCP_TW_RECYCLE_TICK) - 1) >> TCP_TW_RECYCLE_TICK;
  448. spin_lock(&tw_death_lock);
  449. /* Unlink it, if it was scheduled */
  450. if (inet_twsk_del_dead_node(tw))
  451. tcp_tw_count--;
  452. else
  453. atomic_inc(&tw->tw_refcnt);
  454. if (slot >= TCP_TW_RECYCLE_SLOTS) {
  455. /* Schedule to slow timer */
  456. if (timeo >= TCP_TIMEWAIT_LEN) {
  457. slot = TCP_TWKILL_SLOTS-1;
  458. } else {
  459. slot = (timeo + TCP_TWKILL_PERIOD-1) / TCP_TWKILL_PERIOD;
  460. if (slot >= TCP_TWKILL_SLOTS)
  461. slot = TCP_TWKILL_SLOTS-1;
  462. }
  463. tw->tw_ttd = jiffies + timeo;
  464. slot = (tcp_tw_death_row_slot + slot) & (TCP_TWKILL_SLOTS - 1);
  465. list = &tcp_tw_death_row[slot];
  466. } else {
  467. tw->tw_ttd = jiffies + (slot << TCP_TW_RECYCLE_TICK);
  468. if (tcp_twcal_hand < 0) {
  469. tcp_twcal_hand = 0;
  470. tcp_twcal_jiffie = jiffies;
  471. tcp_twcal_timer.expires = tcp_twcal_jiffie + (slot<<TCP_TW_RECYCLE_TICK);
  472. add_timer(&tcp_twcal_timer);
  473. } else {
  474. if (time_after(tcp_twcal_timer.expires, jiffies + (slot<<TCP_TW_RECYCLE_TICK)))
  475. mod_timer(&tcp_twcal_timer, jiffies + (slot<<TCP_TW_RECYCLE_TICK));
  476. slot = (tcp_twcal_hand + slot)&(TCP_TW_RECYCLE_SLOTS-1);
  477. }
  478. list = &tcp_twcal_row[slot];
  479. }
  480. hlist_add_head(&tw->tw_death_node, list);
  481. if (tcp_tw_count++ == 0)
  482. mod_timer(&tcp_tw_timer, jiffies+TCP_TWKILL_PERIOD);
  483. spin_unlock(&tw_death_lock);
  484. }
  485. void tcp_twcal_tick(unsigned long dummy)
  486. {
  487. int n, slot;
  488. unsigned long j;
  489. unsigned long now = jiffies;
  490. int killed = 0;
  491. int adv = 0;
  492. spin_lock(&tw_death_lock);
  493. if (tcp_twcal_hand < 0)
  494. goto out;
  495. slot = tcp_twcal_hand;
  496. j = tcp_twcal_jiffie;
  497. for (n=0; n<TCP_TW_RECYCLE_SLOTS; n++) {
  498. if (time_before_eq(j, now)) {
  499. struct hlist_node *node, *safe;
  500. struct inet_timewait_sock *tw;
  501. inet_twsk_for_each_inmate_safe(tw, node, safe,
  502. &tcp_twcal_row[slot]) {
  503. __inet_twsk_del_dead_node(tw);
  504. __inet_twsk_kill(tw, &tcp_hashinfo);
  505. inet_twsk_put(tw);
  506. killed++;
  507. }
  508. } else {
  509. if (!adv) {
  510. adv = 1;
  511. tcp_twcal_jiffie = j;
  512. tcp_twcal_hand = slot;
  513. }
  514. if (!hlist_empty(&tcp_twcal_row[slot])) {
  515. mod_timer(&tcp_twcal_timer, j);
  516. goto out;
  517. }
  518. }
  519. j += (1<<TCP_TW_RECYCLE_TICK);
  520. slot = (slot+1)&(TCP_TW_RECYCLE_SLOTS-1);
  521. }
  522. tcp_twcal_hand = -1;
  523. out:
  524. if ((tcp_tw_count -= killed) == 0)
  525. del_timer(&tcp_tw_timer);
  526. NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
  527. spin_unlock(&tw_death_lock);
  528. }
  529. /* This is not only more efficient than what we used to do, it eliminates
  530. * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
  531. *
  532. * Actually, we could lots of memory writes here. tp of listening
  533. * socket contains all necessary default parameters.
  534. */
  535. struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
  536. {
  537. /* allocate the newsk from the same slab of the master sock,
  538. * if not, at sk_free time we'll try to free it from the wrong
  539. * slabcache (i.e. is it TCPv4 or v6?), this is handled thru sk->sk_prot -acme */
  540. struct sock *newsk = sk_alloc(PF_INET, GFP_ATOMIC, sk->sk_prot, 0);
  541. if(newsk != NULL) {
  542. struct inet_request_sock *ireq = inet_rsk(req);
  543. struct tcp_request_sock *treq = tcp_rsk(req);
  544. struct inet_sock *newinet = inet_sk(newsk);
  545. struct tcp_sock *newtp;
  546. struct sk_filter *filter;
  547. memcpy(newsk, sk, sizeof(struct tcp_sock));
  548. newsk->sk_state = TCP_SYN_RECV;
  549. /* SANITY */
  550. sk_node_init(&newsk->sk_node);
  551. newinet->bind_hash = NULL;
  552. /* Clone the TCP header template */
  553. newinet->dport = ireq->rmt_port;
  554. sock_lock_init(newsk);
  555. bh_lock_sock(newsk);
  556. rwlock_init(&newsk->sk_dst_lock);
  557. newsk->sk_dst_cache = NULL;
  558. atomic_set(&newsk->sk_rmem_alloc, 0);
  559. skb_queue_head_init(&newsk->sk_receive_queue);
  560. atomic_set(&newsk->sk_wmem_alloc, 0);
  561. skb_queue_head_init(&newsk->sk_write_queue);
  562. atomic_set(&newsk->sk_omem_alloc, 0);
  563. newsk->sk_wmem_queued = 0;
  564. newsk->sk_forward_alloc = 0;
  565. sock_reset_flag(newsk, SOCK_DONE);
  566. newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
  567. newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
  568. newsk->sk_send_head = NULL;
  569. rwlock_init(&newsk->sk_callback_lock);
  570. skb_queue_head_init(&newsk->sk_error_queue);
  571. newsk->sk_write_space = sk_stream_write_space;
  572. if ((filter = newsk->sk_filter) != NULL)
  573. sk_filter_charge(newsk, filter);
  574. if (unlikely(xfrm_sk_clone_policy(newsk))) {
  575. /* It is still raw copy of parent, so invalidate
  576. * destructor and make plain sk_free() */
  577. newsk->sk_destruct = NULL;
  578. sk_free(newsk);
  579. return NULL;
  580. }
  581. /* Now setup tcp_sock */
  582. newtp = tcp_sk(newsk);
  583. newtp->pred_flags = 0;
  584. newtp->rcv_nxt = treq->rcv_isn + 1;
  585. newtp->snd_nxt = treq->snt_isn + 1;
  586. newtp->snd_una = treq->snt_isn + 1;
  587. newtp->snd_sml = treq->snt_isn + 1;
  588. tcp_prequeue_init(newtp);
  589. tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);
  590. newtp->retransmits = 0;
  591. newtp->backoff = 0;
  592. newtp->srtt = 0;
  593. newtp->mdev = TCP_TIMEOUT_INIT;
  594. newtp->rto = TCP_TIMEOUT_INIT;
  595. newtp->packets_out = 0;
  596. newtp->left_out = 0;
  597. newtp->retrans_out = 0;
  598. newtp->sacked_out = 0;
  599. newtp->fackets_out = 0;
  600. newtp->snd_ssthresh = 0x7fffffff;
  601. /* So many TCP implementations out there (incorrectly) count the
  602. * initial SYN frame in their delayed-ACK and congestion control
  603. * algorithms that we must have the following bandaid to talk
  604. * efficiently to them. -DaveM
  605. */
  606. newtp->snd_cwnd = 2;
  607. newtp->snd_cwnd_cnt = 0;
  608. newtp->frto_counter = 0;
  609. newtp->frto_highmark = 0;
  610. newtp->ca_ops = &tcp_reno;
  611. tcp_set_ca_state(newtp, TCP_CA_Open);
  612. tcp_init_xmit_timers(newsk);
  613. skb_queue_head_init(&newtp->out_of_order_queue);
  614. newtp->rcv_wup = treq->rcv_isn + 1;
  615. newtp->write_seq = treq->snt_isn + 1;
  616. newtp->pushed_seq = newtp->write_seq;
  617. newtp->copied_seq = treq->rcv_isn + 1;
  618. newtp->rx_opt.saw_tstamp = 0;
  619. newtp->rx_opt.dsack = 0;
  620. newtp->rx_opt.eff_sacks = 0;
  621. newtp->probes_out = 0;
  622. newtp->rx_opt.num_sacks = 0;
  623. newtp->urg_data = 0;
  624. /* Deinitialize accept_queue to trap illegal accesses. */
  625. memset(&newtp->accept_queue, 0, sizeof(newtp->accept_queue));
  626. /* Back to base struct sock members. */
  627. newsk->sk_err = 0;
  628. newsk->sk_priority = 0;
  629. atomic_set(&newsk->sk_refcnt, 2);
  630. /*
  631. * Increment the counter in the same struct proto as the master
  632. * sock (sk_refcnt_debug_inc uses newsk->sk_prot->socks, that
  633. * is the same as sk->sk_prot->socks, as this field was copied
  634. * with memcpy), same rationale as the first comment in this
  635. * function.
  636. *
  637. * This _changes_ the previous behaviour, where
  638. * tcp_create_openreq_child always was incrementing the
  639. * equivalent to tcp_prot->socks (inet_sock_nr), so this have
  640. * to be taken into account in all callers. -acme
  641. */
  642. sk_refcnt_debug_inc(newsk);
  643. atomic_inc(&tcp_sockets_allocated);
  644. if (sock_flag(newsk, SOCK_KEEPOPEN))
  645. tcp_reset_keepalive_timer(newsk,
  646. keepalive_time_when(newtp));
  647. newsk->sk_socket = NULL;
  648. newsk->sk_sleep = NULL;
  649. newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
  650. if((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
  651. if (sysctl_tcp_fack)
  652. newtp->rx_opt.sack_ok |= 2;
  653. }
  654. newtp->window_clamp = req->window_clamp;
  655. newtp->rcv_ssthresh = req->rcv_wnd;
  656. newtp->rcv_wnd = req->rcv_wnd;
  657. newtp->rx_opt.wscale_ok = ireq->wscale_ok;
  658. if (newtp->rx_opt.wscale_ok) {
  659. newtp->rx_opt.snd_wscale = ireq->snd_wscale;
  660. newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
  661. } else {
  662. newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
  663. newtp->window_clamp = min(newtp->window_clamp, 65535U);
  664. }
  665. newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->rx_opt.snd_wscale;
  666. newtp->max_window = newtp->snd_wnd;
  667. if (newtp->rx_opt.tstamp_ok) {
  668. newtp->rx_opt.ts_recent = req->ts_recent;
  669. newtp->rx_opt.ts_recent_stamp = xtime.tv_sec;
  670. newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  671. } else {
  672. newtp->rx_opt.ts_recent_stamp = 0;
  673. newtp->tcp_header_len = sizeof(struct tcphdr);
  674. }
  675. if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
  676. newtp->ack.last_seg_size = skb->len-newtp->tcp_header_len;
  677. newtp->rx_opt.mss_clamp = req->mss;
  678. TCP_ECN_openreq_child(newtp, req);
  679. if (newtp->ecn_flags&TCP_ECN_OK)
  680. sock_set_flag(newsk, SOCK_NO_LARGESEND);
  681. TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);
  682. }
  683. return newsk;
  684. }
  685. /*
  686. * Process an incoming packet for SYN_RECV sockets represented
  687. * as a request_sock.
  688. */
  689. struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
  690. struct request_sock *req,
  691. struct request_sock **prev)
  692. {
  693. struct tcphdr *th = skb->h.th;
  694. struct tcp_sock *tp = tcp_sk(sk);
  695. u32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
  696. int paws_reject = 0;
  697. struct tcp_options_received tmp_opt;
  698. struct sock *child;
  699. tmp_opt.saw_tstamp = 0;
  700. if (th->doff > (sizeof(struct tcphdr)>>2)) {
  701. tcp_parse_options(skb, &tmp_opt, 0);
  702. if (tmp_opt.saw_tstamp) {
  703. tmp_opt.ts_recent = req->ts_recent;
  704. /* We do not store true stamp, but it is not required,
  705. * it can be estimated (approximately)
  706. * from another data.
  707. */
  708. tmp_opt.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
  709. paws_reject = tcp_paws_check(&tmp_opt, th->rst);
  710. }
  711. }
  712. /* Check for pure retransmitted SYN. */
  713. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
  714. flg == TCP_FLAG_SYN &&
  715. !paws_reject) {
  716. /*
  717. * RFC793 draws (Incorrectly! It was fixed in RFC1122)
  718. * this case on figure 6 and figure 8, but formal
  719. * protocol description says NOTHING.
  720. * To be more exact, it says that we should send ACK,
  721. * because this segment (at least, if it has no data)
  722. * is out of window.
  723. *
  724. * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
  725. * describe SYN-RECV state. All the description
  726. * is wrong, we cannot believe to it and should
  727. * rely only on common sense and implementation
  728. * experience.
  729. *
  730. * Enforce "SYN-ACK" according to figure 8, figure 6
  731. * of RFC793, fixed by RFC1122.
  732. */
  733. req->rsk_ops->rtx_syn_ack(sk, req, NULL);
  734. return NULL;
  735. }
  736. /* Further reproduces section "SEGMENT ARRIVES"
  737. for state SYN-RECEIVED of RFC793.
  738. It is broken, however, it does not work only
  739. when SYNs are crossed.
  740. You would think that SYN crossing is impossible here, since
  741. we should have a SYN_SENT socket (from connect()) on our end,
  742. but this is not true if the crossed SYNs were sent to both
  743. ends by a malicious third party. We must defend against this,
  744. and to do that we first verify the ACK (as per RFC793, page
  745. 36) and reset if it is invalid. Is this a true full defense?
  746. To convince ourselves, let us consider a way in which the ACK
  747. test can still pass in this 'malicious crossed SYNs' case.
  748. Malicious sender sends identical SYNs (and thus identical sequence
  749. numbers) to both A and B:
  750. A: gets SYN, seq=7
  751. B: gets SYN, seq=7
  752. By our good fortune, both A and B select the same initial
  753. send sequence number of seven :-)
  754. A: sends SYN|ACK, seq=7, ack_seq=8
  755. B: sends SYN|ACK, seq=7, ack_seq=8
  756. So we are now A eating this SYN|ACK, ACK test passes. So
  757. does sequence test, SYN is truncated, and thus we consider
  758. it a bare ACK.
  759. If tp->defer_accept, we silently drop this bare ACK. Otherwise,
  760. we create an established connection. Both ends (listening sockets)
  761. accept the new incoming connection and try to talk to each other. 8-)
  762. Note: This case is both harmless, and rare. Possibility is about the
  763. same as us discovering intelligent life on another plant tomorrow.
  764. But generally, we should (RFC lies!) to accept ACK
  765. from SYNACK both here and in tcp_rcv_state_process().
  766. tcp_rcv_state_process() does not, hence, we do not too.
  767. Note that the case is absolutely generic:
  768. we cannot optimize anything here without
  769. violating protocol. All the checks must be made
  770. before attempt to create socket.
  771. */
  772. /* RFC793 page 36: "If the connection is in any non-synchronized state ...
  773. * and the incoming segment acknowledges something not yet
  774. * sent (the segment carries an unaccaptable ACK) ...
  775. * a reset is sent."
  776. *
  777. * Invalid ACK: reset will be sent by listening socket
  778. */
  779. if ((flg & TCP_FLAG_ACK) &&
  780. (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))
  781. return sk;
  782. /* Also, it would be not so bad idea to check rcv_tsecr, which
  783. * is essentially ACK extension and too early or too late values
  784. * should cause reset in unsynchronized states.
  785. */
  786. /* RFC793: "first check sequence number". */
  787. if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
  788. tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
  789. /* Out of window: send ACK and drop. */
  790. if (!(flg & TCP_FLAG_RST))
  791. req->rsk_ops->send_ack(skb, req);
  792. if (paws_reject)
  793. NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
  794. return NULL;
  795. }
  796. /* In sequence, PAWS is OK. */
  797. if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
  798. req->ts_recent = tmp_opt.rcv_tsval;
  799. if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
  800. /* Truncate SYN, it is out of window starting
  801. at tcp_rsk(req)->rcv_isn + 1. */
  802. flg &= ~TCP_FLAG_SYN;
  803. }
  804. /* RFC793: "second check the RST bit" and
  805. * "fourth, check the SYN bit"
  806. */
  807. if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN))
  808. goto embryonic_reset;
  809. /* ACK sequence verified above, just make sure ACK is
  810. * set. If ACK not set, just silently drop the packet.
  811. */
  812. if (!(flg & TCP_FLAG_ACK))
  813. return NULL;
  814. /* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
  815. if (tp->defer_accept && TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
  816. inet_rsk(req)->acked = 1;
  817. return NULL;
  818. }
  819. /* OK, ACK is valid, create big socket and
  820. * feed this segment to it. It will repeat all
  821. * the tests. THIS SEGMENT MUST MOVE SOCKET TO
  822. * ESTABLISHED STATE. If it will be dropped after
  823. * socket is created, wait for troubles.
  824. */
  825. child = tp->af_specific->syn_recv_sock(sk, skb, req, NULL);
  826. if (child == NULL)
  827. goto listen_overflow;
  828. tcp_synq_unlink(tp, req, prev);
  829. tcp_synq_removed(sk, req);
  830. tcp_acceptq_queue(sk, req, child);
  831. return child;
  832. listen_overflow:
  833. if (!sysctl_tcp_abort_on_overflow) {
  834. inet_rsk(req)->acked = 1;
  835. return NULL;
  836. }
  837. embryonic_reset:
  838. NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);
  839. if (!(flg & TCP_FLAG_RST))
  840. req->rsk_ops->send_reset(skb);
  841. tcp_synq_drop(sk, req, prev);
  842. return NULL;
  843. }
  844. /*
  845. * Queue segment on the new socket if the new socket is active,
  846. * otherwise we just shortcircuit this and continue with
  847. * the new socket.
  848. */
  849. int tcp_child_process(struct sock *parent, struct sock *child,
  850. struct sk_buff *skb)
  851. {
  852. int ret = 0;
  853. int state = child->sk_state;
  854. if (!sock_owned_by_user(child)) {
  855. ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
  856. /* Wakeup parent, send SIGIO */
  857. if (state == TCP_SYN_RECV && child->sk_state != state)
  858. parent->sk_data_ready(parent, 0);
  859. } else {
  860. /* Alas, it is possible again, because we do lookup
  861. * in main socket hash table and lock on listening
  862. * socket does not protect us more.
  863. */
  864. sk_add_backlog(child, skb);
  865. }
  866. bh_unlock_sock(child);
  867. sock_put(child);
  868. return ret;
  869. }
  870. EXPORT_SYMBOL(tcp_check_req);
  871. EXPORT_SYMBOL(tcp_child_process);
  872. EXPORT_SYMBOL(tcp_create_openreq_child);
  873. EXPORT_SYMBOL(tcp_timewait_state_process);
  874. EXPORT_SYMBOL(tcp_tw_deschedule);