1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34#else
35#define SYNC_INIT 1
36#endif
37
38int sysctl_tcp_tw_recycle;
39int sysctl_tcp_max_tw_buckets = NR_FILE*2;
40
41int sysctl_tcp_syncookies = SYNC_INIT;
42int sysctl_tcp_abort_on_overflow;
43
44static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
45{
46 if (seq == s_win)
47 return 1;
48 if (after(end_seq, s_win) && before(seq, e_win))
49 return 1;
50 return (seq == e_win && seq == end_seq);
51}
52
53
54
55int tcp_tw_count;
56
57
58
59static void tcp_timewait_kill(struct tcp_tw_bucket *tw)
60{
61 struct tcp_ehash_bucket *ehead;
62 struct tcp_bind_hashbucket *bhead;
63 struct tcp_bind_bucket *tb;
64
65
66 ehead = &tcp_ehash[tw->tw_hashent];
67 write_lock(&ehead->lock);
68 if (hlist_unhashed(&tw->tw_node)) {
69 write_unlock(&ehead->lock);
70 return;
71 }
72 __hlist_del(&tw->tw_node);
73 sk_node_init(&tw->tw_node);
74 write_unlock(&ehead->lock);
75
76
77 bhead = &tcp_bhash[tcp_bhashfn(tw->tw_num)];
78 spin_lock(&bhead->lock);
79 tb = tw->tw_tb;
80 __hlist_del(&tw->tw_bind_node);
81 tw->tw_tb = NULL;
82 tcp_bucket_destroy(tb);
83 spin_unlock(&bhead->lock);
84
85#ifdef INET_REFCNT_DEBUG
86 if (atomic_read(&tw->tw_refcnt) != 1) {
87 printk(KERN_DEBUG "tw_bucket %p refcnt=%d\n", tw,
88 atomic_read(&tw->tw_refcnt));
89 }
90#endif
91 tcp_tw_put(tw);
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122enum tcp_tw_status
123tcp_timewait_state_process(struct tcp_tw_bucket *tw, struct sk_buff *skb,
124 struct tcphdr *th, unsigned len)
125{
126 struct tcp_opt tp;
127 int paws_reject = 0;
128
129 tp.saw_tstamp = 0;
130 if (th->doff > (sizeof(struct tcphdr) >> 2) && tw->tw_ts_recent_stamp) {
131 tcp_parse_options(skb, &tp, 0);
132
133 if (tp.saw_tstamp) {
134 tp.ts_recent = tw->tw_ts_recent;
135 tp.ts_recent_stamp = tw->tw_ts_recent_stamp;
136 paws_reject = tcp_paws_check(&tp, th->rst);
137 }
138 }
139
140 if (tw->tw_substate == TCP_FIN_WAIT2) {
141
142
143
144 if (paws_reject ||
145 !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
146 tw->tw_rcv_nxt,
147 tw->tw_rcv_nxt + tw->tw_rcv_wnd))
148 return TCP_TW_ACK;
149
150 if (th->rst)
151 goto kill;
152
153 if (th->syn && !before(TCP_SKB_CB(skb)->seq, tw->tw_rcv_nxt))
154 goto kill_with_rst;
155
156
157 if (!after(TCP_SKB_CB(skb)->end_seq, tw->tw_rcv_nxt) ||
158 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
159 tcp_tw_put(tw);
160 return TCP_TW_SUCCESS;
161 }
162
163
164
165
166 if (!th->fin ||
167 TCP_SKB_CB(skb)->end_seq != tw->tw_rcv_nxt + 1) {
168kill_with_rst:
169 tcp_tw_deschedule(tw);
170 tcp_tw_put(tw);
171 return TCP_TW_RST;
172 }
173
174
175 tw->tw_substate = TCP_TIME_WAIT;
176 tw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
177 if (tp.saw_tstamp) {
178 tw->tw_ts_recent_stamp = xtime.tv_sec;
179 tw->tw_ts_recent = tp.rcv_tsval;
180 }
181
182
183
184
185
186
187 if (tw->tw_family == AF_INET &&
188 sysctl_tcp_tw_recycle && tw->tw_ts_recent_stamp &&
189 tcp_v4_tw_remember_stamp(tw))
190 tcp_tw_schedule(tw, tw->tw_timeout);
191 else
192 tcp_tw_schedule(tw, TCP_TIMEWAIT_LEN);
193 return TCP_TW_ACK;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 if (!paws_reject &&
214 (TCP_SKB_CB(skb)->seq == tw->tw_rcv_nxt &&
215 (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
216
217
218 if (th->rst) {
219
220
221
222
223 if (sysctl_tcp_rfc1337 == 0) {
224kill:
225 tcp_tw_deschedule(tw);
226 tcp_tw_put(tw);
227 return TCP_TW_SUCCESS;
228 }
229 }
230 tcp_tw_schedule(tw, TCP_TIMEWAIT_LEN);
231
232 if (tp.saw_tstamp) {
233 tw->tw_ts_recent = tp.rcv_tsval;
234 tw->tw_ts_recent_stamp = xtime.tv_sec;
235 }
236
237 tcp_tw_put(tw);
238 return TCP_TW_SUCCESS;
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 if (th->syn && !th->rst && !th->ack && !paws_reject &&
259 (after(TCP_SKB_CB(skb)->seq, tw->tw_rcv_nxt) ||
260 (tp.saw_tstamp && (s32)(tw->tw_ts_recent - tp.rcv_tsval) < 0))) {
261 u32 isn = tw->tw_snd_nxt + 65535 + 2;
262 if (isn == 0)
263 isn++;
264 TCP_SKB_CB(skb)->when = isn;
265 return TCP_TW_SYN;
266 }
267
268 if (paws_reject)
269 NET_INC_STATS_BH(PAWSEstabRejected);
270
271 if(!th->rst) {
272
273
274
275
276
277
278 if (paws_reject || th->ack)
279 tcp_tw_schedule(tw, TCP_TIMEWAIT_LEN);
280
281
282
283
284 return TCP_TW_ACK;
285 }
286 tcp_tw_put(tw);
287 return TCP_TW_SUCCESS;
288}
289
290
291
292
293
294
295static void __tcp_tw_hashdance(struct sock *sk, struct tcp_tw_bucket *tw)
296{
297 struct tcp_ehash_bucket *ehead = &tcp_ehash[sk->sk_hashent];
298 struct tcp_bind_hashbucket *bhead;
299
300
301
302
303
304 bhead = &tcp_bhash[tcp_bhashfn(inet_sk(sk)->num)];
305 spin_lock(&bhead->lock);
306 tw->tw_tb = tcp_sk(sk)->bind_hash;
307 BUG_TRAP(tcp_sk(sk)->bind_hash);
308 tw_add_bind_node(tw, &tw->tw_tb->owners);
309 spin_unlock(&bhead->lock);
310
311 write_lock(&ehead->lock);
312
313
314 if (__sk_del_node_init(sk))
315 sock_prot_dec_use(sk->sk_prot);
316
317
318 tw_add_node(tw, &(ehead + tcp_ehash_size)->chain);
319 atomic_inc(&tw->tw_refcnt);
320
321 write_unlock(&ehead->lock);
322}
323
324
325
326
327void tcp_time_wait(struct sock *sk, int state, int timeo)
328{
329 struct tcp_tw_bucket *tw = NULL;
330 struct tcp_opt *tp = tcp_sk(sk);
331 int recycle_ok = 0;
332
333 if (sysctl_tcp_tw_recycle && tp->ts_recent_stamp)
334 recycle_ok = tp->af_specific->remember_stamp(sk);
335
336 if (tcp_tw_count < sysctl_tcp_max_tw_buckets)
337 tw = kmem_cache_alloc(tcp_timewait_cachep, SLAB_ATOMIC);
338
339 if(tw != NULL) {
340 struct inet_opt *inet = inet_sk(sk);
341 int rto = (tp->rto<<2) - (tp->rto>>1);
342
343
344 tw->tw_daddr = inet->daddr;
345 tw->tw_rcv_saddr = inet->rcv_saddr;
346 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
347 tw->tw_num = inet->num;
348 tw->tw_state = TCP_TIME_WAIT;
349 tw->tw_substate = state;
350 tw->tw_sport = inet->sport;
351 tw->tw_dport = inet->dport;
352 tw->tw_family = sk->sk_family;
353 tw->tw_reuse = sk->sk_reuse;
354 tw->tw_rcv_wscale = tp->rcv_wscale;
355 atomic_set(&tw->tw_refcnt, 1);
356
357 tw->tw_hashent = sk->sk_hashent;
358 tw->tw_rcv_nxt = tp->rcv_nxt;
359 tw->tw_snd_nxt = tp->snd_nxt;
360 tw->tw_rcv_wnd = tcp_receive_window(tp);
361 tw->tw_ts_recent = tp->ts_recent;
362 tw->tw_ts_recent_stamp = tp->ts_recent_stamp;
363 tw_dead_node_init(tw);
364
365#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
366 if (tw->tw_family == PF_INET6) {
367 struct ipv6_pinfo *np = inet6_sk(sk);
368
369 ipv6_addr_copy(&tw->tw_v6_daddr, &np->daddr);
370 ipv6_addr_copy(&tw->tw_v6_rcv_saddr, &np->rcv_saddr);
371 tw->tw_v6_ipv6only = np->ipv6only;
372 } else {
373 memset(&tw->tw_v6_daddr, 0, sizeof(tw->tw_v6_daddr));
374 memset(&tw->tw_v6_rcv_saddr, 0, sizeof(tw->tw_v6_rcv_saddr));
375 tw->tw_v6_ipv6only = 0;
376 }
377#endif
378
379 __tcp_tw_hashdance(sk, tw);
380
381
382 if (timeo < rto)
383 timeo = rto;
384
385 if (recycle_ok) {
386 tw->tw_timeout = rto;
387 } else {
388 tw->tw_timeout = TCP_TIMEWAIT_LEN;
389 if (state == TCP_TIME_WAIT)
390 timeo = TCP_TIMEWAIT_LEN;
391 }
392
393 tcp_tw_schedule(tw, timeo);
394 tcp_tw_put(tw);
395 } else {
396
397
398
399
400 if (net_ratelimit())
401 printk(KERN_INFO "TCP: time wait bucket table overflow\n");
402 }
403
404 tcp_update_metrics(sk);
405 tcp_done(sk);
406}
407
408
409static int tcp_tw_death_row_slot;
410
411static void tcp_twkill(unsigned long);
412
413
414#define TCP_TWKILL_SLOTS 8
415#define TCP_TWKILL_PERIOD (TCP_TIMEWAIT_LEN/TCP_TWKILL_SLOTS)
416
417#define TCP_TWKILL_QUOTA 100
418
419static struct hlist_head tcp_tw_death_row[TCP_TWKILL_SLOTS];
420static spinlock_t tw_death_lock = SPIN_LOCK_UNLOCKED;
421static struct timer_list tcp_tw_timer = TIMER_INITIALIZER(tcp_twkill, 0, 0);
422static void twkill_work(void *);
423static DECLARE_WORK(tcp_twkill_work, twkill_work, NULL);
424static u32 twkill_thread_slots;
425
426
427static int tcp_do_twkill_work(int slot, unsigned int quota)
428{
429 struct tcp_tw_bucket *tw;
430 struct hlist_node *node, *safe;
431 unsigned int killed;
432 int ret;
433
434
435
436
437
438
439
440 killed = 0;
441 ret = 0;
442 tw_for_each_inmate(tw, node, safe,
443 &tcp_tw_death_row[slot]) {
444 __tw_del_dead_node(tw);
445 spin_unlock(&tw_death_lock);
446 tcp_timewait_kill(tw);
447 tcp_tw_put(tw);
448 killed++;
449 spin_lock(&tw_death_lock);
450 if (killed > quota) {
451 ret = 1;
452 break;
453 }
454 }
455
456 tcp_tw_count -= killed;
457 NET_ADD_STATS_BH(TimeWaited, killed);
458
459 return ret;
460}
461
462static void tcp_twkill(unsigned long dummy)
463{
464 int need_timer, ret;
465
466 spin_lock(&tw_death_lock);
467
468 if (tcp_tw_count == 0)
469 goto out;
470
471 need_timer = 0;
472 ret = tcp_do_twkill_work(tcp_tw_death_row_slot, TCP_TWKILL_QUOTA);
473 if (ret) {
474 twkill_thread_slots |= (1 << tcp_tw_death_row_slot);
475 mb();
476 schedule_work(&tcp_twkill_work);
477 need_timer = 1;
478 } else {
479
480 if (tcp_tw_count)
481 need_timer = 1;
482 }
483 tcp_tw_death_row_slot =
484 ((tcp_tw_death_row_slot + 1) & (TCP_TWKILL_SLOTS - 1));
485 if (need_timer)
486 mod_timer(&tcp_tw_timer, jiffies + TCP_TWKILL_PERIOD);
487out:
488 spin_unlock(&tw_death_lock);
489}
490
491extern void twkill_slots_invalid(void);
492
493static void twkill_work(void *dummy)
494{
495 int i;
496
497 if ((TCP_TWKILL_SLOTS - 1) > (sizeof(twkill_thread_slots) * 8))
498 twkill_slots_invalid();
499
500 while (twkill_thread_slots) {
501 spin_lock_bh(&tw_death_lock);
502 for (i = 0; i < TCP_TWKILL_SLOTS; i++) {
503 if (!(twkill_thread_slots & (1 << i)))
504 continue;
505
506 while (tcp_do_twkill_work(i, TCP_TWKILL_QUOTA) != 0) {
507 if (need_resched()) {
508 spin_unlock_bh(&tw_death_lock);
509 schedule();
510 spin_lock_bh(&tw_death_lock);
511 }
512 }
513
514 twkill_thread_slots &= ~(1 << i);
515 }
516 spin_unlock_bh(&tw_death_lock);
517 }
518}
519
520
521
522
523
524
525void tcp_tw_deschedule(struct tcp_tw_bucket *tw)
526{
527 spin_lock(&tw_death_lock);
528 if (tw_del_dead_node(tw)) {
529 tcp_tw_put(tw);
530 if (--tcp_tw_count == 0)
531 del_timer(&tcp_tw_timer);
532 }
533 spin_unlock(&tw_death_lock);
534 tcp_timewait_kill(tw);
535}
536
537
538
539static int tcp_twcal_hand = -1;
540static int tcp_twcal_jiffie;
541static void tcp_twcal_tick(unsigned long);
542static struct timer_list tcp_twcal_timer =
543 TIMER_INITIALIZER(tcp_twcal_tick, 0, 0);
544static struct hlist_head tcp_twcal_row[TCP_TW_RECYCLE_SLOTS];
545
546void tcp_tw_schedule(struct tcp_tw_bucket *tw, int timeo)
547{
548 struct hlist_head *list;
549 int slot;
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575 slot = (timeo + (1<<TCP_TW_RECYCLE_TICK) - 1) >> TCP_TW_RECYCLE_TICK;
576
577 spin_lock(&tw_death_lock);
578
579
580 if (tw_del_dead_node(tw))
581 tcp_tw_count--;
582 else
583 atomic_inc(&tw->tw_refcnt);
584
585 if (slot >= TCP_TW_RECYCLE_SLOTS) {
586
587 if (timeo >= TCP_TIMEWAIT_LEN) {
588 slot = TCP_TWKILL_SLOTS-1;
589 } else {
590 slot = (timeo + TCP_TWKILL_PERIOD-1) / TCP_TWKILL_PERIOD;
591 if (slot >= TCP_TWKILL_SLOTS)
592 slot = TCP_TWKILL_SLOTS-1;
593 }
594 tw->tw_ttd = jiffies + timeo;
595 slot = (tcp_tw_death_row_slot + slot) & (TCP_TWKILL_SLOTS - 1);
596 list = &tcp_tw_death_row[slot];
597 } else {
598 tw->tw_ttd = jiffies + (slot << TCP_TW_RECYCLE_TICK);
599
600 if (tcp_twcal_hand < 0) {
601 tcp_twcal_hand = 0;
602 tcp_twcal_jiffie = jiffies;
603 tcp_twcal_timer.expires = tcp_twcal_jiffie + (slot<<TCP_TW_RECYCLE_TICK);
604 add_timer(&tcp_twcal_timer);
605 } else {
606 if (time_after(tcp_twcal_timer.expires, jiffies + (slot<<TCP_TW_RECYCLE_TICK)))
607 mod_timer(&tcp_twcal_timer, jiffies + (slot<<TCP_TW_RECYCLE_TICK));
608 slot = (tcp_twcal_hand + slot)&(TCP_TW_RECYCLE_SLOTS-1);
609 }
610 list = &tcp_twcal_row[slot];
611 }
612
613 hlist_add_head(&tw->tw_death_node, list);
614
615 if (tcp_tw_count++ == 0)
616 mod_timer(&tcp_tw_timer, jiffies+TCP_TWKILL_PERIOD);
617 spin_unlock(&tw_death_lock);
618}
619
620void tcp_twcal_tick(unsigned long dummy)
621{
622 int n, slot;
623 unsigned long j;
624 unsigned long now = jiffies;
625 int killed = 0;
626 int adv = 0;
627
628 spin_lock(&tw_death_lock);
629 if (tcp_twcal_hand < 0)
630 goto out;
631
632 slot = tcp_twcal_hand;
633 j = tcp_twcal_jiffie;
634
635 for (n=0; n<TCP_TW_RECYCLE_SLOTS; n++) {
636 if (time_before_eq(j, now)) {
637 struct hlist_node *node, *safe;
638 struct tcp_tw_bucket *tw;
639
640 tw_for_each_inmate(tw, node, safe,
641 &tcp_twcal_row[slot]) {
642 __tw_del_dead_node(tw);
643 tcp_timewait_kill(tw);
644 tcp_tw_put(tw);
645 killed++;
646 }
647 } else {
648 if (!adv) {
649 adv = 1;
650 tcp_twcal_jiffie = j;
651 tcp_twcal_hand = slot;
652 }
653
654 if (!hlist_empty(&tcp_twcal_row[slot])) {
655 mod_timer(&tcp_twcal_timer, j);
656 goto out;
657 }
658 }
659 j += (1<<TCP_TW_RECYCLE_TICK);
660 slot = (slot+1)&(TCP_TW_RECYCLE_SLOTS-1);
661 }
662 tcp_twcal_hand = -1;
663
664out:
665 if ((tcp_tw_count -= killed) == 0)
666 del_timer(&tcp_tw_timer);
667 NET_ADD_STATS_BH(TimeWaitKilled, killed);
668 spin_unlock(&tw_death_lock);
669}
670
671
672
673
674
675
676
677struct sock *tcp_create_openreq_child(struct sock *sk, struct open_request *req, struct sk_buff *skb)
678{
679
680
681
682 struct sock *newsk = sk_alloc(PF_INET, GFP_ATOMIC, 0, sk->sk_slab);
683
684 if(newsk != NULL) {
685 struct tcp_opt *newtp;
686 struct sk_filter *filter;
687
688 memcpy(newsk, sk, sizeof(struct tcp_sock));
689 newsk->sk_state = TCP_SYN_RECV;
690
691
692 sk_node_init(&newsk->sk_node);
693 tcp_sk(newsk)->bind_hash = NULL;
694
695
696 inet_sk(newsk)->dport = req->rmt_port;
697
698 sock_lock_init(newsk);
699 bh_lock_sock(newsk);
700
701 newsk->sk_dst_lock = RW_LOCK_UNLOCKED;
702 atomic_set(&newsk->sk_rmem_alloc, 0);
703 skb_queue_head_init(&newsk->sk_receive_queue);
704 atomic_set(&newsk->sk_wmem_alloc, 0);
705 skb_queue_head_init(&newsk->sk_write_queue);
706 atomic_set(&newsk->sk_omem_alloc, 0);
707 newsk->sk_wmem_queued = 0;
708 newsk->sk_forward_alloc = 0;
709
710 sock_reset_flag(newsk, SOCK_DONE);
711 newsk->sk_userlocks = sk->sk_userlocks & ~SOCK_BINDPORT_LOCK;
712 newsk->sk_backlog.head = newsk->sk_backlog.tail = NULL;
713 newsk->sk_callback_lock = RW_LOCK_UNLOCKED;
714 skb_queue_head_init(&newsk->sk_error_queue);
715 newsk->sk_write_space = tcp_write_space;
716
717 if ((filter = newsk->sk_filter) != NULL)
718 sk_filter_charge(newsk, filter);
719
720 if (unlikely(xfrm_sk_clone_policy(newsk))) {
721
722
723 newsk->sk_destruct = NULL;
724 sk_free(newsk);
725 return NULL;
726 }
727
728
729 newtp = tcp_sk(newsk);
730 newtp->pred_flags = 0;
731 newtp->rcv_nxt = req->rcv_isn + 1;
732 newtp->snd_nxt = req->snt_isn + 1;
733 newtp->snd_una = req->snt_isn + 1;
734 newtp->snd_sml = req->snt_isn + 1;
735
736 tcp_prequeue_init(newtp);
737
738 tcp_init_wl(newtp, req->snt_isn, req->rcv_isn);
739
740 newtp->retransmits = 0;
741 newtp->backoff = 0;
742 newtp->srtt = 0;
743 newtp->mdev = TCP_TIMEOUT_INIT;
744 newtp->rto = TCP_TIMEOUT_INIT;
745
746 newtp->packets_out = 0;
747 newtp->left_out = 0;
748 newtp->retrans_out = 0;
749 newtp->sacked_out = 0;
750 newtp->fackets_out = 0;
751 newtp->snd_ssthresh = 0x7fffffff;
752
753
754
755
756
757
758 newtp->snd_cwnd = 2;
759 newtp->snd_cwnd_cnt = 0;
760
761 newtp->frto_counter = 0;
762 newtp->frto_highmark = 0;
763
764 newtp->ca_state = TCP_CA_Open;
765 tcp_init_xmit_timers(newsk);
766 skb_queue_head_init(&newtp->out_of_order_queue);
767 newtp->send_head = NULL;
768 newtp->rcv_wup = req->rcv_isn + 1;
769 newtp->write_seq = req->snt_isn + 1;
770 newtp->pushed_seq = newtp->write_seq;
771 newtp->copied_seq = req->rcv_isn + 1;
772
773 newtp->saw_tstamp = 0;
774
775 newtp->dsack = 0;
776 newtp->eff_sacks = 0;
777
778 newtp->probes_out = 0;
779 newtp->num_sacks = 0;
780 newtp->urg_data = 0;
781 newtp->listen_opt = NULL;
782 newtp->accept_queue = newtp->accept_queue_tail = NULL;
783
784 memset(&newtp->syn_wait_lock, 0, sizeof(newtp->syn_wait_lock));
785
786
787 newsk->sk_err = 0;
788 newsk->sk_priority = 0;
789 atomic_set(&newsk->sk_refcnt, 2);
790#ifdef INET_REFCNT_DEBUG
791 atomic_inc(&inet_sock_nr);
792#endif
793 atomic_inc(&tcp_sockets_allocated);
794
795 if (sock_flag(newsk, SOCK_KEEPOPEN))
796 tcp_reset_keepalive_timer(newsk,
797 keepalive_time_when(newtp));
798 newsk->sk_socket = NULL;
799 newsk->sk_sleep = NULL;
800 newsk->sk_owner = NULL;
801
802 newtp->tstamp_ok = req->tstamp_ok;
803 if((newtp->sack_ok = req->sack_ok) != 0) {
804 if (sysctl_tcp_fack)
805 newtp->sack_ok |= 2;
806 }
807 newtp->window_clamp = req->window_clamp;
808 newtp->rcv_ssthresh = req->rcv_wnd;
809 newtp->rcv_wnd = req->rcv_wnd;
810 newtp->wscale_ok = req->wscale_ok;
811 if (newtp->wscale_ok) {
812 newtp->snd_wscale = req->snd_wscale;
813 newtp->rcv_wscale = req->rcv_wscale;
814 } else {
815 newtp->snd_wscale = newtp->rcv_wscale = 0;
816 newtp->window_clamp = min(newtp->window_clamp, 65535U);
817 }
818 newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->snd_wscale;
819 newtp->max_window = newtp->snd_wnd;
820
821 if (newtp->tstamp_ok) {
822 newtp->ts_recent = req->ts_recent;
823 newtp->ts_recent_stamp = xtime.tv_sec;
824 newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
825 } else {
826 newtp->ts_recent_stamp = 0;
827 newtp->tcp_header_len = sizeof(struct tcphdr);
828 }
829 if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
830 newtp->ack.last_seg_size = skb->len-newtp->tcp_header_len;
831 newtp->mss_clamp = req->mss;
832 TCP_ECN_openreq_child(newtp, req);
833 if (newtp->ecn_flags&TCP_ECN_OK)
834 newsk->sk_no_largesend = 1;
835
836 TCP_INC_STATS_BH(TcpPassiveOpens);
837 }
838 return newsk;
839}
840
841
842
843
844
845
846struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
847 struct open_request *req,
848 struct open_request **prev)
849{
850 struct tcphdr *th = skb->h.th;
851 struct tcp_opt *tp = tcp_sk(sk);
852 u32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
853 int paws_reject = 0;
854 struct tcp_opt ttp;
855 struct sock *child;
856
857 ttp.saw_tstamp = 0;
858 if (th->doff > (sizeof(struct tcphdr)>>2)) {
859 tcp_parse_options(skb, &ttp, 0);
860
861 if (ttp.saw_tstamp) {
862 ttp.ts_recent = req->ts_recent;
863
864
865
866
867 ttp.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
868 paws_reject = tcp_paws_check(&ttp, th->rst);
869 }
870 }
871
872
873 if (TCP_SKB_CB(skb)->seq == req->rcv_isn &&
874 flg == TCP_FLAG_SYN &&
875 !paws_reject) {
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893 req->class->rtx_syn_ack(sk, req, NULL);
894 return NULL;
895 }
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950 if ((flg & TCP_FLAG_ACK) &&
951 (TCP_SKB_CB(skb)->ack_seq != req->snt_isn+1))
952 return sk;
953
954
955
956
957
958
959
960
961 if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
962 req->rcv_isn+1, req->rcv_isn+1+req->rcv_wnd)) {
963
964 if (!(flg & TCP_FLAG_RST))
965 req->class->send_ack(skb, req);
966 if (paws_reject)
967 NET_INC_STATS_BH(PAWSEstabRejected);
968 return NULL;
969 }
970
971
972
973 if (ttp.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, req->rcv_isn+1))
974 req->ts_recent = ttp.rcv_tsval;
975
976 if (TCP_SKB_CB(skb)->seq == req->rcv_isn) {
977
978
979 flg &= ~TCP_FLAG_SYN;
980 }
981
982
983
984
985 if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN))
986 goto embryonic_reset;
987
988
989
990
991 if (!(flg & TCP_FLAG_ACK))
992 return NULL;
993
994
995 if (tp->defer_accept && TCP_SKB_CB(skb)->end_seq == req->rcv_isn+1) {
996 req->acked = 1;
997 return NULL;
998 }
999
1000
1001
1002
1003
1004
1005
1006 child = tp->af_specific->syn_recv_sock(sk, skb, req, NULL);
1007 if (child == NULL)
1008 goto listen_overflow;
1009
1010 sk_set_owner(child, sk->sk_owner);
1011 tcp_synq_unlink(tp, req, prev);
1012 tcp_synq_removed(sk, req);
1013
1014 tcp_acceptq_queue(sk, req, child);
1015 return child;
1016
1017listen_overflow:
1018 if (!sysctl_tcp_abort_on_overflow) {
1019 req->acked = 1;
1020 return NULL;
1021 }
1022
1023embryonic_reset:
1024 NET_INC_STATS_BH(EmbryonicRsts);
1025 if (!(flg & TCP_FLAG_RST))
1026 req->class->send_reset(skb);
1027
1028 tcp_synq_drop(sk, req, prev);
1029 return NULL;
1030}
1031
1032
1033
1034
1035
1036
1037
1038int tcp_child_process(struct sock *parent, struct sock *child,
1039 struct sk_buff *skb)
1040{
1041 int ret = 0;
1042 int state = child->sk_state;
1043
1044 if (!sock_owned_by_user(child)) {
1045 ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
1046
1047
1048 if (state == TCP_SYN_RECV && child->sk_state != state)
1049 parent->sk_data_ready(parent, 0);
1050 } else {
1051
1052
1053
1054
1055 sk_add_backlog(child, skb);
1056 }
1057
1058 bh_unlock_sock(child);
1059 sock_put(child);
1060 return ret;
1061}
1062
1063EXPORT_SYMBOL(tcp_check_req);
1064EXPORT_SYMBOL(tcp_child_process);
1065EXPORT_SYMBOL(tcp_create_openreq_child);
1066EXPORT_SYMBOL(tcp_timewait_state_process);
1067EXPORT_SYMBOL(tcp_tw_deschedule);
1068
1069#ifdef CONFIG_SYSCTL
1070EXPORT_SYMBOL(sysctl_tcp_tw_recycle);
1071#endif
1072