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