This source file includes following definitions.
- tcp_reass
- tcp6_input
- tcp_input
- tcp_dooptions
- tcp_seq_subtract
- tcp_update_sack_list
- tcp_sack_option
- tcp_del_sackholes
- tcp_clean_sackreport
- tcp_sack_partialack
- tcp_pulloutofband
- tcp_xmit_timer
- tcp_mss
- tcp_hdrsz
- tcp_mss_update
- tcp_newreno
- tcp_mss_adv
- syn_cache_init
- syn_cache_insert
- syn_cache_timer
- syn_cache_reaper
- syn_cache_cleanup
- syn_cache_lookup
- syn_cache_get
- syn_cache_reset
- syn_cache_unreach
- syn_cache_add
- syn_cache_respond
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/mbuf.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/kernel.h>
78
79 #include <dev/rndvar.h>
80
81 #include <net/if.h>
82 #include <net/route.h>
83
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/tcp.h>
90 #include <netinet/tcp_fsm.h>
91 #include <netinet/tcp_seq.h>
92 #include <netinet/tcp_timer.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/tcp_debug.h>
96
97 struct tcpiphdr tcp_saveti;
98
99 int tcp_mss_adv(struct ifnet *, int);
100
101 #ifdef INET6
102 #include <netinet6/in6_var.h>
103 #include <netinet6/nd6.h>
104
105 struct tcpipv6hdr tcp_saveti6;
106
107
108 #define M_PH_LEN(m) (((struct mbuf *)(m))->m_pkthdr.len)
109 #define M_V6_LEN(m) (M_PH_LEN(m) - sizeof(struct ip6_hdr))
110 #define M_V4_LEN(m) (M_PH_LEN(m) - sizeof(struct ip))
111 #endif
112
113 int tcprexmtthresh = 3;
114 int tcptv_keep_init = TCPTV_KEEP_INIT;
115
116 extern u_long sb_max;
117
118 int tcp_rst_ppslim = 100;
119 int tcp_rst_ppslim_count = 0;
120 struct timeval tcp_rst_ppslim_last;
121
122 int tcp_ackdrop_ppslim = 100;
123 int tcp_ackdrop_ppslim_count = 0;
124 struct timeval tcp_ackdrop_ppslim_last;
125
126 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
127
128
129 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
130 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
131
132
133 #define SEQ_MIN(a,b) (SEQ_LT(a,b) ? (a) : (b))
134 #define SEQ_MAX(a,b) (SEQ_GT(a,b) ? (a) : (b))
135
136
137
138
139 #ifdef INET6
140 #define ND6_HINT(tp) \
141 do { \
142 if (tp && tp->t_inpcb && (tp->t_inpcb->inp_flags & INP_IPV6) && \
143 tp->t_inpcb->inp_route6.ro_rt) { \
144 nd6_nud_hint(tp->t_inpcb->inp_route6.ro_rt, NULL, 0); \
145 } \
146 } while (0)
147 #else
148 #define ND6_HINT(tp)
149 #endif
150
151 #ifdef TCP_ECN
152
153
154
155
156
157
158
159
160
161 #endif
162
163
164
165
166
167
168
169 #define TCP_SETUP_ACK(tp, tiflags) \
170 do { \
171 if ((tp)->t_flags & TF_DELACK || \
172 (tcp_ack_on_push && (tiflags) & TH_PUSH)) \
173 tp->t_flags |= TF_ACKNOW; \
174 else \
175 TCP_SET_DELACK(tp); \
176 } while (0)
177
178
179
180
181
182
183
184
185
186
187
188
189 int
190 tcp_reass(tp, th, m, tlen)
191 struct tcpcb *tp;
192 struct tcphdr *th;
193 struct mbuf *m;
194 int *tlen;
195 {
196 struct tcpqent *p, *q, *nq, *tiqe;
197 struct socket *so = tp->t_inpcb->inp_socket;
198 int flags;
199
200
201
202
203
204 if (th == 0)
205 goto present;
206
207
208
209
210
211 tiqe = pool_get(&tcpqe_pool, PR_NOWAIT);
212 if (tiqe == NULL) {
213 tiqe = TAILQ_LAST(&tp->t_segq, tcpqehead);
214 if (tiqe != NULL && th->th_seq == tp->rcv_nxt) {
215
216 m_freem(tiqe->tcpqe_m);
217 TAILQ_REMOVE(&tp->t_segq, tiqe, tcpqe_q);
218 }
219 if (tiqe == NULL || th->th_seq != tp->rcv_nxt) {
220
221 tcp_freeq(tp);
222 tcpstat.tcps_rcvmemdrop++;
223 m_freem(m);
224 return (0);
225 }
226 }
227
228
229
230
231 for (p = NULL, q = TAILQ_FIRST(&tp->t_segq); q != NULL;
232 p = q, q = TAILQ_NEXT(q, tcpqe_q))
233 if (SEQ_GT(q->tcpqe_tcp->th_seq, th->th_seq))
234 break;
235
236
237
238
239
240
241 if (p != NULL) {
242 struct tcphdr *phdr = p->tcpqe_tcp;
243 int i;
244
245
246 i = phdr->th_seq + phdr->th_reseqlen - th->th_seq;
247 if (i > 0) {
248 if (i >= *tlen) {
249 tcpstat.tcps_rcvduppack++;
250 tcpstat.tcps_rcvdupbyte += *tlen;
251 m_freem(m);
252 pool_put(&tcpqe_pool, tiqe);
253 return (0);
254 }
255 m_adj(m, i);
256 *tlen -= i;
257 th->th_seq += i;
258 }
259 }
260 tcpstat.tcps_rcvoopack++;
261 tcpstat.tcps_rcvoobyte += *tlen;
262
263
264
265
266
267 for (; q != NULL; q = nq) {
268 struct tcphdr *qhdr = q->tcpqe_tcp;
269 int i = (th->th_seq + *tlen) - qhdr->th_seq;
270
271 if (i <= 0)
272 break;
273 if (i < qhdr->th_reseqlen) {
274 qhdr->th_seq += i;
275 qhdr->th_reseqlen -= i;
276 m_adj(q->tcpqe_m, i);
277 break;
278 }
279 nq = TAILQ_NEXT(q, tcpqe_q);
280 m_freem(q->tcpqe_m);
281 TAILQ_REMOVE(&tp->t_segq, q, tcpqe_q);
282 pool_put(&tcpqe_pool, q);
283 }
284
285
286 tiqe->tcpqe_m = m;
287 th->th_reseqlen = *tlen;
288 tiqe->tcpqe_tcp = th;
289 if (p == NULL) {
290 TAILQ_INSERT_HEAD(&tp->t_segq, tiqe, tcpqe_q);
291 } else {
292 TAILQ_INSERT_AFTER(&tp->t_segq, p, tiqe, tcpqe_q);
293 }
294
295 present:
296
297
298
299
300 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
301 return (0);
302 q = TAILQ_FIRST(&tp->t_segq);
303 if (q == NULL || q->tcpqe_tcp->th_seq != tp->rcv_nxt)
304 return (0);
305 if (tp->t_state == TCPS_SYN_RECEIVED && q->tcpqe_tcp->th_reseqlen)
306 return (0);
307 do {
308 tp->rcv_nxt += q->tcpqe_tcp->th_reseqlen;
309 flags = q->tcpqe_tcp->th_flags & TH_FIN;
310
311 nq = TAILQ_NEXT(q, tcpqe_q);
312 TAILQ_REMOVE(&tp->t_segq, q, tcpqe_q);
313 ND6_HINT(tp);
314 if (so->so_state & SS_CANTRCVMORE)
315 m_freem(q->tcpqe_m);
316 else
317 sbappendstream(&so->so_rcv, q->tcpqe_m);
318 pool_put(&tcpqe_pool, q);
319 q = nq;
320 } while (q != NULL && q->tcpqe_tcp->th_seq == tp->rcv_nxt);
321 sorwakeup(so);
322 return (flags);
323 }
324
325 #ifdef INET6
326 int
327 tcp6_input(mp, offp, proto)
328 struct mbuf **mp;
329 int *offp, proto;
330 {
331 struct mbuf *m = *mp;
332
333 #if defined(NFAITH) && 0 < NFAITH
334 if (m->m_pkthdr.rcvif) {
335 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
336
337 m_freem(m);
338 return IPPROTO_DONE;
339 }
340 }
341 #endif
342
343
344
345
346
347 if (m->m_flags & M_ANYCAST6) {
348 if (m->m_len >= sizeof(struct ip6_hdr)) {
349 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
350 icmp6_error(m, ICMP6_DST_UNREACH,
351 ICMP6_DST_UNREACH_ADDR,
352 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
353 } else
354 m_freem(m);
355 return IPPROTO_DONE;
356 }
357
358 tcp_input(m, *offp, proto);
359 return IPPROTO_DONE;
360 }
361 #endif
362
363
364
365
366
367 void
368 tcp_input(struct mbuf *m, ...)
369 {
370 struct ip *ip;
371 struct inpcb *inp;
372 u_int8_t *optp = NULL;
373 int optlen = 0;
374 int tlen, off;
375 struct tcpcb *tp = 0;
376 int tiflags;
377 struct socket *so = NULL;
378 int todrop, acked, ourfinisacked, needoutput = 0;
379 int hdroptlen = 0;
380 short ostate = 0;
381 tcp_seq iss, *reuse = NULL;
382 u_long tiwin;
383 struct tcp_opt_info opti;
384 int iphlen;
385 va_list ap;
386 struct tcphdr *th;
387 #ifdef INET6
388 struct ip6_hdr *ip6 = NULL;
389 #endif
390 #ifdef IPSEC
391 struct m_tag *mtag;
392 struct tdb_ident *tdbi;
393 struct tdb *tdb;
394 int error, s;
395 #endif
396 int af;
397 #ifdef TCP_ECN
398 u_char iptos;
399 #endif
400
401 va_start(ap, m);
402 iphlen = va_arg(ap, int);
403 va_end(ap);
404
405 tcpstat.tcps_rcvtotal++;
406
407 opti.ts_present = 0;
408 opti.maxseg = 0;
409
410
411
412
413
414 if (m->m_flags & (M_BCAST|M_MCAST))
415 goto drop;
416
417
418
419
420
421 switch (mtod(m, struct ip *)->ip_v) {
422 #ifdef INET6
423 case 6:
424 af = AF_INET6;
425 break;
426 #endif
427 case 4:
428 af = AF_INET;
429 break;
430 default:
431 m_freem(m);
432 return;
433 }
434
435
436
437
438
439 switch (af) {
440 case AF_INET:
441 #ifdef DIAGNOSTIC
442 if (iphlen < sizeof(struct ip)) {
443 m_freem(m);
444 return;
445 }
446 #endif
447 break;
448 #ifdef INET6
449 case AF_INET6:
450 #ifdef DIAGNOSTIC
451 if (iphlen < sizeof(struct ip6_hdr)) {
452 m_freem(m);
453 return;
454 }
455 #endif
456 break;
457 #endif
458 default:
459 m_freem(m);
460 return;
461 }
462
463 IP6_EXTHDR_GET(th, struct tcphdr *, m, iphlen, sizeof(*th));
464 if (!th) {
465 tcpstat.tcps_rcvshort++;
466 return;
467 }
468
469 tlen = m->m_pkthdr.len - iphlen;
470 ip = NULL;
471 #ifdef INET6
472 ip6 = NULL;
473 #endif
474 switch (af) {
475 case AF_INET:
476 ip = mtod(m, struct ip *);
477 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
478 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
479 goto drop;
480 #ifdef TCP_ECN
481
482 iptos = ip->ip_tos;
483 #endif
484
485
486
487 if ((m->m_pkthdr.csum_flags & M_TCP_CSUM_IN_OK) == 0) {
488 if (m->m_pkthdr.csum_flags & M_TCP_CSUM_IN_BAD) {
489 tcpstat.tcps_inhwcsum++;
490 tcpstat.tcps_rcvbadsum++;
491 goto drop;
492 }
493 if (in4_cksum(m, IPPROTO_TCP, iphlen, tlen) != 0) {
494 tcpstat.tcps_rcvbadsum++;
495 goto drop;
496 }
497 } else {
498 m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_IN_OK;
499 tcpstat.tcps_inhwcsum++;
500 }
501 break;
502 #ifdef INET6
503 case AF_INET6:
504 ip6 = mtod(m, struct ip6_hdr *);
505 #ifdef TCP_ECN
506 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
507 #endif
508
509
510 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
511 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
512
513 goto drop;
514 }
515
516
517
518
519
520
521
522
523
524 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
525
526 goto drop;
527 }
528
529
530 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
531
532 goto drop;
533 }
534
535
536
537
538 if (in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), tlen)) {
539 tcpstat.tcps_rcvbadsum++;
540 goto drop;
541 }
542 break;
543 #endif
544 }
545
546
547
548
549
550 off = th->th_off << 2;
551 if (off < sizeof(struct tcphdr) || off > tlen) {
552 tcpstat.tcps_rcvbadoff++;
553 goto drop;
554 }
555 tlen -= off;
556 if (off > sizeof(struct tcphdr)) {
557 IP6_EXTHDR_GET(th, struct tcphdr *, m, iphlen, off);
558 if (!th) {
559 tcpstat.tcps_rcvshort++;
560 return;
561 }
562 optlen = off - sizeof(struct tcphdr);
563 optp = (u_int8_t *)(th + 1);
564
565
566
567
568
569
570
571 if ((optlen == TCPOLEN_TSTAMP_APPA ||
572 (optlen > TCPOLEN_TSTAMP_APPA &&
573 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
574 *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
575 (th->th_flags & TH_SYN) == 0) {
576 opti.ts_present = 1;
577 opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
578 opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
579 optp = NULL;
580 }
581 }
582 tiflags = th->th_flags;
583
584
585
586
587 NTOHL(th->th_seq);
588 NTOHL(th->th_ack);
589 NTOHS(th->th_win);
590 NTOHS(th->th_urp);
591
592
593
594
595 findpcb:
596 switch (af) {
597 #ifdef INET6
598 case AF_INET6:
599 inp = in6_pcbhashlookup(&tcbtable, &ip6->ip6_src, th->th_sport,
600 &ip6->ip6_dst, th->th_dport);
601 break;
602 #endif
603 case AF_INET:
604 inp = in_pcbhashlookup(&tcbtable, ip->ip_src, th->th_sport,
605 ip->ip_dst, th->th_dport);
606 break;
607 }
608 if (inp == 0) {
609 int inpl_flags = 0;
610 if (m->m_pkthdr.pf.flags & PF_TAG_TRANSLATE_LOCALHOST)
611 inpl_flags = INPLOOKUP_WILDCARD;
612 ++tcpstat.tcps_pcbhashmiss;
613 switch (af) {
614 #ifdef INET6
615 case AF_INET6:
616 inp = in6_pcblookup_listen(&tcbtable,
617 &ip6->ip6_dst, th->th_dport, inpl_flags);
618 break;
619 #endif
620 case AF_INET:
621 inp = in_pcblookup_listen(&tcbtable,
622 ip->ip_dst, th->th_dport, inpl_flags);
623 break;
624 }
625
626
627
628
629
630
631 if (inp == 0) {
632 ++tcpstat.tcps_noport;
633 goto dropwithreset_ratelim;
634 }
635 }
636
637
638 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
639 goto drop;
640
641 tp = intotcpcb(inp);
642 if (tp == 0)
643 goto dropwithreset_ratelim;
644 if (tp->t_state == TCPS_CLOSED)
645 goto drop;
646
647
648 if ((tiflags & TH_SYN) == 0)
649 tiwin = th->th_win << tp->snd_scale;
650 else
651 tiwin = th->th_win;
652
653 so = inp->inp_socket;
654 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
655 union syn_cache_sa src;
656 union syn_cache_sa dst;
657
658 bzero(&src, sizeof(src));
659 bzero(&dst, sizeof(dst));
660 switch (af) {
661 #ifdef INET
662 case AF_INET:
663 src.sin.sin_len = sizeof(struct sockaddr_in);
664 src.sin.sin_family = AF_INET;
665 src.sin.sin_addr = ip->ip_src;
666 src.sin.sin_port = th->th_sport;
667
668 dst.sin.sin_len = sizeof(struct sockaddr_in);
669 dst.sin.sin_family = AF_INET;
670 dst.sin.sin_addr = ip->ip_dst;
671 dst.sin.sin_port = th->th_dport;
672 break;
673 #endif
674 #ifdef INET6
675 case AF_INET6:
676 src.sin6.sin6_len = sizeof(struct sockaddr_in6);
677 src.sin6.sin6_family = AF_INET6;
678 src.sin6.sin6_addr = ip6->ip6_src;
679 src.sin6.sin6_port = th->th_sport;
680
681 dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
682 dst.sin6.sin6_family = AF_INET6;
683 dst.sin6.sin6_addr = ip6->ip6_dst;
684 dst.sin6.sin6_port = th->th_dport;
685 break;
686 #endif
687 default:
688 goto badsyn;
689 }
690
691 if (so->so_options & SO_DEBUG) {
692 ostate = tp->t_state;
693 switch (af) {
694 #ifdef INET6
695 case AF_INET6:
696 bcopy(ip6, &tcp_saveti6.ti6_i, sizeof(*ip6));
697 bcopy(th, &tcp_saveti6.ti6_t, sizeof(*th));
698 break;
699 #endif
700 case AF_INET:
701 bcopy(ip, &tcp_saveti.ti_i, sizeof(*ip));
702 bcopy(th, &tcp_saveti.ti_t, sizeof(*th));
703 break;
704 }
705 }
706 if (so->so_options & SO_ACCEPTCONN) {
707 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
708 if (tiflags & TH_RST) {
709 syn_cache_reset(&src.sa, &dst.sa, th);
710 } else if ((tiflags & (TH_ACK|TH_SYN)) ==
711 (TH_ACK|TH_SYN)) {
712
713
714
715
716
717 goto badsyn;
718 } else if (tiflags & TH_ACK) {
719 so = syn_cache_get(&src.sa, &dst.sa,
720 th, iphlen, tlen, so, m);
721 if (so == NULL) {
722
723
724
725
726 goto badsyn;
727 } else if (so ==
728 (struct socket *)(-1)) {
729
730
731
732
733
734
735
736
737
738
739 m = NULL;
740 } else {
741
742
743
744
745 tp = NULL;
746 inp = (struct inpcb *)so->so_pcb;
747 tp = intotcpcb(inp);
748 if (tp == NULL)
749 goto badsyn;
750
751
752
753
754
755 tcp_rscale(tp, so->so_rcv.sb_hiwat);
756 goto after_listen;
757 }
758 } else {
759
760
761
762
763
764 goto badsyn;
765 }
766 } else {
767
768
769
770 #ifdef INET6
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806 if (ip6 && !ip6_use_deprecated) {
807 struct in6_ifaddr *ia6;
808
809 if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif,
810 &ip6->ip6_dst)) &&
811 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
812 tp = NULL;
813 goto dropwithreset;
814 }
815 }
816 #endif
817
818
819
820
821
822
823 if (th->th_dport == th->th_sport) {
824 switch (af) {
825 #ifdef INET6
826 case AF_INET6:
827 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
828 &ip6->ip6_dst)) {
829 tcpstat.tcps_badsyn++;
830 goto drop;
831 }
832 break;
833 #endif
834 case AF_INET:
835 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) {
836 tcpstat.tcps_badsyn++;
837 goto drop;
838 }
839 break;
840 }
841 }
842
843
844
845
846
847 if (so->so_qlen <= so->so_qlimit &&
848 syn_cache_add(&src.sa, &dst.sa, th, iphlen,
849 so, m, optp, optlen, &opti, reuse))
850 m = NULL;
851 }
852 goto drop;
853 }
854 }
855
856 after_listen:
857 #ifdef DIAGNOSTIC
858
859
860
861
862 if (tp->t_state == TCPS_LISTEN)
863 panic("tcp_input: TCPS_LISTEN");
864 #endif
865
866 #ifdef IPSEC
867
868 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
869 s = splnet();
870 if (mtag != NULL) {
871 tdbi = (struct tdb_ident *)(mtag + 1);
872 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
873 } else
874 tdb = NULL;
875 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
876 tdb, inp);
877 if (error) {
878 splx(s);
879 goto drop;
880 }
881
882
883 if (inp->inp_tdb_in != tdb) {
884 if (tdb) {
885 tdb_add_inp(tdb, inp, 1);
886 if (inp->inp_ipo == NULL) {
887 inp->inp_ipo = ipsec_add_policy(inp, af,
888 IPSP_DIRECTION_OUT);
889 if (inp->inp_ipo == NULL) {
890 splx(s);
891 goto drop;
892 }
893 }
894 if (inp->inp_ipo->ipo_dstid == NULL &&
895 tdb->tdb_srcid != NULL) {
896 inp->inp_ipo->ipo_dstid = tdb->tdb_srcid;
897 tdb->tdb_srcid->ref_count++;
898 }
899 if (inp->inp_ipsec_remotecred == NULL &&
900 tdb->tdb_remote_cred != NULL) {
901 inp->inp_ipsec_remotecred =
902 tdb->tdb_remote_cred;
903 tdb->tdb_remote_cred->ref_count++;
904 }
905 if (inp->inp_ipsec_remoteauth == NULL &&
906 tdb->tdb_remote_auth != NULL) {
907 inp->inp_ipsec_remoteauth =
908 tdb->tdb_remote_auth;
909 tdb->tdb_remote_auth->ref_count++;
910 }
911 } else {
912 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, inp,
913 inp_tdb_in_next);
914 inp->inp_tdb_in = NULL;
915 }
916 }
917 splx(s);
918 #endif
919
920
921
922
923
924 tp->t_rcvtime = tcp_now;
925 if (TCPS_HAVEESTABLISHED(tp->t_state))
926 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
927
928 #ifdef TCP_SACK
929 if (tp->sack_enable)
930 tcp_del_sackholes(tp, th);
931 #endif
932
933
934
935
936 #ifdef TCP_SIGNATURE
937 if (optp || (tp->t_flags & TF_SIGNATURE))
938 #else
939 if (optp)
940 #endif
941 if (tcp_dooptions(tp, optp, optlen, th, m, iphlen, &opti))
942 goto drop;
943
944 if (opti.ts_present && opti.ts_ecr) {
945 int rtt_test;
946
947
948 opti.ts_ecr -= tp->ts_modulate;
949
950
951 rtt_test = tcp_now - opti.ts_ecr;
952 if (rtt_test < 0 || rtt_test > TCP_RTT_MAX)
953 opti.ts_ecr = 0;
954 }
955
956 #ifdef TCP_ECN
957
958 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) {
959 tp->t_flags |= TF_RCVD_CE;
960 tcpstat.tcps_ecn_rcvce++;
961 }
962 #endif
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977 if (tp->t_state == TCPS_ESTABLISHED &&
978 #ifdef TCP_ECN
979 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) == TH_ACK &&
980 #else
981 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
982 #endif
983 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
984 th->th_seq == tp->rcv_nxt &&
985 tiwin && tiwin == tp->snd_wnd &&
986 tp->snd_nxt == tp->snd_max) {
987
988
989
990
991
992
993 if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
994 tp->ts_recent_age = tcp_now;
995 tp->ts_recent = opti.ts_val;
996 }
997
998 if (tlen == 0) {
999 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1000 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1001 tp->snd_cwnd >= tp->snd_wnd &&
1002 tp->t_dupacks == 0) {
1003
1004
1005
1006 ++tcpstat.tcps_predack;
1007 if (opti.ts_present && opti.ts_ecr)
1008 tcp_xmit_timer(tp, tcp_now - opti.ts_ecr);
1009 else if (tp->t_rtttime &&
1010 SEQ_GT(th->th_ack, tp->t_rtseq))
1011 tcp_xmit_timer(tp,
1012 tcp_now - tp->t_rtttime);
1013 acked = th->th_ack - tp->snd_una;
1014 tcpstat.tcps_rcvackpack++;
1015 tcpstat.tcps_rcvackbyte += acked;
1016 ND6_HINT(tp);
1017 sbdrop(&so->so_snd, acked);
1018
1019
1020
1021
1022
1023
1024
1025 if ((tp->t_flags & TF_PMTUD_PEND) &&
1026 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq))
1027 tp->t_flags &= ~TF_PMTUD_PEND;
1028
1029
1030
1031
1032
1033 if (tp->t_pmtud_mss_acked < acked)
1034 tp->t_pmtud_mss_acked = acked;
1035
1036 tp->snd_una = th->th_ack;
1037 #if defined(TCP_SACK) || defined(TCP_ECN)
1038
1039
1040
1041
1042
1043 #ifdef TCP_ECN
1044 if (SEQ_GT(tp->snd_una, tp->snd_last))
1045 #endif
1046 tp->snd_last = tp->snd_una;
1047 #endif
1048 #if defined(TCP_SACK) && defined(TCP_FACK)
1049 tp->snd_fack = tp->snd_una;
1050 tp->retran_data = 0;
1051 #endif
1052 m_freem(m);
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 if (tp->snd_una == tp->snd_max)
1064 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1065 else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
1066 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1067
1068 if (sb_notify(&so->so_snd))
1069 sowwakeup(so);
1070 if (so->so_snd.sb_cc)
1071 (void) tcp_output(tp);
1072 return;
1073 }
1074 } else if (th->th_ack == tp->snd_una &&
1075 TAILQ_EMPTY(&tp->t_segq) &&
1076 tlen <= sbspace(&so->so_rcv)) {
1077
1078
1079
1080
1081
1082 #ifdef TCP_SACK
1083
1084 if (tp->sack_enable && tp->rcv_numsacks)
1085 tcp_clean_sackreport(tp);
1086 #endif
1087 ++tcpstat.tcps_preddat;
1088 tp->rcv_nxt += tlen;
1089 tcpstat.tcps_rcvpack++;
1090 tcpstat.tcps_rcvbyte += tlen;
1091 ND6_HINT(tp);
1092
1093
1094
1095
1096 if (so->so_state & SS_CANTRCVMORE)
1097 m_freem(m);
1098 else {
1099 m_adj(m, iphlen + off);
1100 sbappendstream(&so->so_rcv, m);
1101 }
1102 sorwakeup(so);
1103 TCP_SETUP_ACK(tp, tiflags);
1104 if (tp->t_flags & TF_ACKNOW)
1105 (void) tcp_output(tp);
1106 return;
1107 }
1108 }
1109
1110
1111
1112
1113 hdroptlen = iphlen + off;
1114
1115
1116
1117
1118
1119
1120
1121 { int win;
1122
1123 win = sbspace(&so->so_rcv);
1124 if (win < 0)
1125 win = 0;
1126 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1127 }
1128
1129 switch (tp->t_state) {
1130
1131
1132
1133
1134
1135
1136
1137 case TCPS_SYN_RECEIVED:
1138 if (tiflags & TH_ACK) {
1139 if (tiflags & TH_SYN) {
1140 tcpstat.tcps_badsyn++;
1141 goto dropwithreset;
1142 }
1143 if (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1144 SEQ_GT(th->th_ack, tp->snd_max))
1145 goto dropwithreset;
1146 }
1147 break;
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 case TCPS_SYN_SENT:
1162 if ((tiflags & TH_ACK) &&
1163 (SEQ_LEQ(th->th_ack, tp->iss) ||
1164 SEQ_GT(th->th_ack, tp->snd_max)))
1165 goto dropwithreset;
1166 if (tiflags & TH_RST) {
1167 #ifdef TCP_ECN
1168
1169 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN))
1170 goto drop;
1171 #endif
1172 if (tiflags & TH_ACK)
1173 tp = tcp_drop(tp, ECONNREFUSED);
1174 goto drop;
1175 }
1176 if ((tiflags & TH_SYN) == 0)
1177 goto drop;
1178 if (tiflags & TH_ACK) {
1179 tp->snd_una = th->th_ack;
1180 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1181 tp->snd_nxt = tp->snd_una;
1182 }
1183 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1184 tp->irs = th->th_seq;
1185 tcp_mss(tp, opti.maxseg);
1186
1187 if (tp->t_rxtshift > 0)
1188 tp->snd_cwnd = tp->t_maxseg;
1189 tcp_rcvseqinit(tp);
1190 tp->t_flags |= TF_ACKNOW;
1191 #ifdef TCP_SACK
1192
1193
1194
1195
1196
1197 if (tp->sack_enable)
1198 tp->sack_enable = tp->t_flags & TF_SACK_PERMIT;
1199 #endif
1200 #ifdef TCP_ECN
1201
1202
1203
1204
1205
1206 if (tcp_do_ecn) {
1207 if ((tiflags & (TH_ACK|TH_ECE|TH_CWR))
1208 == (TH_ACK|TH_ECE) ||
1209 (tiflags & (TH_ACK|TH_ECE|TH_CWR))
1210 == (TH_ECE|TH_CWR)) {
1211 tp->t_flags |= TF_ECN_PERMIT;
1212 tiflags &= ~(TH_ECE|TH_CWR);
1213 tcpstat.tcps_ecn_accepts++;
1214 }
1215 }
1216 #endif
1217
1218 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
1219 tcpstat.tcps_connects++;
1220 soisconnected(so);
1221 tp->t_state = TCPS_ESTABLISHED;
1222 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1223
1224 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1225 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1226 tp->snd_scale = tp->requested_s_scale;
1227 tp->rcv_scale = tp->request_r_scale;
1228 }
1229 tcp_reass_lock(tp);
1230 (void) tcp_reass(tp, (struct tcphdr *)0,
1231 (struct mbuf *)0, &tlen);
1232 tcp_reass_unlock(tp);
1233
1234
1235
1236
1237 if (tp->t_rtttime)
1238 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
1239
1240
1241
1242
1243
1244
1245
1246
1247 tp->snd_cwnd += tp->t_maxseg;
1248 } else
1249 tp->t_state = TCPS_SYN_RECEIVED;
1250
1251 #if 0
1252 trimthenstep6:
1253 #endif
1254
1255
1256
1257
1258
1259 th->th_seq++;
1260 if (tlen > tp->rcv_wnd) {
1261 todrop = tlen - tp->rcv_wnd;
1262 m_adj(m, -todrop);
1263 tlen = tp->rcv_wnd;
1264 tiflags &= ~TH_FIN;
1265 tcpstat.tcps_rcvpackafterwin++;
1266 tcpstat.tcps_rcvbyteafterwin += todrop;
1267 }
1268 tp->snd_wl1 = th->th_seq - 1;
1269 tp->rcv_up = th->th_seq;
1270 goto step6;
1271
1272
1273
1274
1275
1276
1277 case TCPS_TIME_WAIT:
1278 if (((tiflags & (TH_SYN|TH_ACK)) == TH_SYN) &&
1279 ((opti.ts_present &&
1280 TSTMP_LT(tp->ts_recent, opti.ts_val)) ||
1281 SEQ_GT(th->th_seq, tp->rcv_nxt))) {
1282
1283
1284
1285
1286
1287 iss = tp->snd_nxt +
1288 ((arc4random() & 0x7fffffff) | 0x8000);
1289 reuse = &iss;
1290 tp = tcp_close(tp);
1291 goto findpcb;
1292 }
1293 }
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
1306 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
1307
1308
1309 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321 tp->ts_recent = 0;
1322 } else {
1323 tcpstat.tcps_rcvduppack++;
1324 tcpstat.tcps_rcvdupbyte += tlen;
1325 tcpstat.tcps_pawsdrop++;
1326 goto dropafterack;
1327 }
1328 }
1329
1330 todrop = tp->rcv_nxt - th->th_seq;
1331 if (todrop > 0) {
1332 if (tiflags & TH_SYN) {
1333 tiflags &= ~TH_SYN;
1334 th->th_seq++;
1335 if (th->th_urp > 1)
1336 th->th_urp--;
1337 else
1338 tiflags &= ~TH_URG;
1339 todrop--;
1340 }
1341 if (todrop > tlen ||
1342 (todrop == tlen && (tiflags & TH_FIN) == 0)) {
1343
1344
1345
1346
1347
1348 tiflags &= ~TH_FIN;
1349
1350
1351
1352
1353 tp->t_flags |= TF_ACKNOW;
1354 tcpstat.tcps_rcvdupbyte += todrop = tlen;
1355 tcpstat.tcps_rcvduppack++;
1356 } else {
1357 tcpstat.tcps_rcvpartduppack++;
1358 tcpstat.tcps_rcvpartdupbyte += todrop;
1359 }
1360 hdroptlen += todrop;
1361 th->th_seq += todrop;
1362 tlen -= todrop;
1363 if (th->th_urp > todrop)
1364 th->th_urp -= todrop;
1365 else {
1366 tiflags &= ~TH_URG;
1367 th->th_urp = 0;
1368 }
1369 }
1370
1371
1372
1373
1374
1375 if ((so->so_state & SS_NOFDREF) &&
1376 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1377 tp = tcp_close(tp);
1378 tcpstat.tcps_rcvafterclose++;
1379 goto dropwithreset;
1380 }
1381
1382
1383
1384
1385
1386 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1387 if (todrop > 0) {
1388 tcpstat.tcps_rcvpackafterwin++;
1389 if (todrop >= tlen) {
1390 tcpstat.tcps_rcvbyteafterwin += tlen;
1391
1392
1393
1394
1395
1396
1397
1398 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1399 tp->t_flags |= TF_ACKNOW;
1400 tcpstat.tcps_rcvwinprobe++;
1401 } else
1402 goto dropafterack;
1403 } else
1404 tcpstat.tcps_rcvbyteafterwin += todrop;
1405 m_adj(m, -todrop);
1406 tlen -= todrop;
1407 tiflags &= ~(TH_PUSH|TH_FIN);
1408 }
1409
1410
1411
1412
1413
1414
1415 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
1416 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1417 if (SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1418 ((tiflags & (TH_SYN|TH_FIN)) != 0)))
1419 tp->ts_recent = opti.ts_val;
1420 else
1421 tp->ts_recent = 0;
1422 tp->ts_recent_age = tcp_now;
1423 }
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435 if (tiflags & TH_RST) {
1436 if (th->th_seq != tp->last_ack_sent &&
1437 th->th_seq != tp->rcv_nxt &&
1438 th->th_seq != (tp->rcv_nxt + 1))
1439 goto drop;
1440
1441 switch (tp->t_state) {
1442 case TCPS_SYN_RECEIVED:
1443 #ifdef TCP_ECN
1444
1445 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN))
1446 goto drop;
1447 #endif
1448 so->so_error = ECONNREFUSED;
1449 goto close;
1450
1451 case TCPS_ESTABLISHED:
1452 case TCPS_FIN_WAIT_1:
1453 case TCPS_FIN_WAIT_2:
1454 case TCPS_CLOSE_WAIT:
1455 so->so_error = ECONNRESET;
1456 close:
1457 tp->t_state = TCPS_CLOSED;
1458 tcpstat.tcps_drops++;
1459 tp = tcp_close(tp);
1460 goto drop;
1461 case TCPS_CLOSING:
1462 case TCPS_LAST_ACK:
1463 case TCPS_TIME_WAIT:
1464 tp = tcp_close(tp);
1465 goto drop;
1466 }
1467 }
1468
1469
1470
1471
1472
1473 if (tiflags & TH_SYN)
1474 goto dropafterack_ratelim;
1475
1476
1477
1478
1479 if ((tiflags & TH_ACK) == 0) {
1480 if (tp->t_flags & TF_ACKNOW)
1481 goto dropafterack;
1482 else
1483 goto drop;
1484 }
1485
1486
1487
1488
1489 switch (tp->t_state) {
1490
1491
1492
1493
1494
1495
1496 case TCPS_SYN_RECEIVED:
1497 tcpstat.tcps_connects++;
1498 soisconnected(so);
1499 tp->t_state = TCPS_ESTABLISHED;
1500 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1501
1502 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1503 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1504 tp->snd_scale = tp->requested_s_scale;
1505 tp->rcv_scale = tp->request_r_scale;
1506 }
1507 tcp_reass_lock(tp);
1508 (void) tcp_reass(tp, (struct tcphdr *)0, (struct mbuf *)0,
1509 &tlen);
1510 tcp_reass_unlock(tp);
1511 tp->snd_wl1 = th->th_seq - 1;
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522 case TCPS_ESTABLISHED:
1523 case TCPS_FIN_WAIT_1:
1524 case TCPS_FIN_WAIT_2:
1525 case TCPS_CLOSE_WAIT:
1526 case TCPS_CLOSING:
1527 case TCPS_LAST_ACK:
1528 case TCPS_TIME_WAIT:
1529 #ifdef TCP_ECN
1530
1531
1532
1533
1534
1535
1536 if (tcp_do_ecn && (tiflags & TH_ECE)) {
1537 if ((tp->t_flags & TF_ECN_PERMIT) &&
1538 SEQ_GEQ(tp->snd_una, tp->snd_last)) {
1539 u_int win;
1540
1541 win = min(tp->snd_wnd, tp->snd_cwnd) / tp->t_maxseg;
1542 if (win > 1) {
1543 tp->snd_ssthresh = win / 2 * tp->t_maxseg;
1544 tp->snd_cwnd = tp->snd_ssthresh;
1545 tp->snd_last = tp->snd_max;
1546 tp->t_flags |= TF_SEND_CWR;
1547 tcpstat.tcps_cwr_ecn++;
1548 }
1549 }
1550 tcpstat.tcps_ecn_rcvece++;
1551 }
1552
1553
1554
1555
1556 if ((tiflags & TH_CWR)) {
1557 tp->t_flags &= ~TF_RCVD_CE;
1558 tcpstat.tcps_ecn_rcvcwr++;
1559 }
1560 #endif
1561
1562 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575 if (tlen) {
1576
1577 if (th->th_seq != tp->rcv_nxt &&
1578 SEQ_LT(th->th_ack,
1579 tp->snd_una - tp->max_sndwnd)) {
1580 tcpstat.tcps_rcvacktooold++;
1581 goto drop;
1582 }
1583 break;
1584 }
1585
1586
1587
1588
1589
1590
1591 if (th->th_ack != tp->snd_una) {
1592 tp->t_dupacks = 0;
1593 break;
1594 }
1595 if (tiwin == tp->snd_wnd) {
1596 tcpstat.tcps_rcvdupack++;
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0)
1622 tp->t_dupacks = 0;
1623 #if defined(TCP_SACK) && defined(TCP_FACK)
1624
1625
1626
1627
1628 else if (++tp->t_dupacks == tcprexmtthresh ||
1629 ((SEQ_GT(tp->snd_fack, tcprexmtthresh *
1630 tp->t_maxseg + tp->snd_una)) &&
1631 SEQ_GT(tp->snd_una, tp->snd_last))) {
1632 #else
1633 else if (++tp->t_dupacks == tcprexmtthresh) {
1634 #endif
1635 tcp_seq onxt = tp->snd_nxt;
1636 u_long win =
1637 ulmin(tp->snd_wnd, tp->snd_cwnd) /
1638 2 / tp->t_maxseg;
1639
1640 #if defined(TCP_SACK) || defined(TCP_ECN)
1641 if (SEQ_LT(th->th_ack, tp->snd_last)){
1642
1643
1644
1645
1646 tp->t_dupacks = 0;
1647 goto drop;
1648 }
1649 #endif
1650 if (win < 2)
1651 win = 2;
1652 tp->snd_ssthresh = win * tp->t_maxseg;
1653 #if defined(TCP_SACK)
1654 tp->snd_last = tp->snd_max;
1655 #endif
1656 #ifdef TCP_SACK
1657 if (tp->sack_enable) {
1658 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1659 tp->t_rtttime = 0;
1660 #ifdef TCP_ECN
1661 tp->t_flags |= TF_SEND_CWR;
1662 #endif
1663 #if 1
1664 tcpstat.tcps_cwr_frecovery++;
1665 #endif
1666 tcpstat.tcps_sack_recovery_episode++;
1667 #if defined(TCP_SACK) && defined(TCP_FACK)
1668 tp->t_dupacks = tcprexmtthresh;
1669 (void) tcp_output(tp);
1670
1671
1672
1673
1674 tp->snd_cwnd = tp->snd_ssthresh;
1675 #else
1676
1677
1678
1679
1680 (void) tcp_output(tp);
1681 tp->snd_cwnd = tp->snd_ssthresh+
1682 tp->t_maxseg * tp->t_dupacks;
1683 #endif
1684 goto drop;
1685 }
1686 #endif
1687 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1688 tp->t_rtttime = 0;
1689 tp->snd_nxt = th->th_ack;
1690 tp->snd_cwnd = tp->t_maxseg;
1691 #ifdef TCP_ECN
1692 tp->t_flags |= TF_SEND_CWR;
1693 #endif
1694 #if 1
1695 tcpstat.tcps_cwr_frecovery++;
1696 #endif
1697 tcpstat.tcps_sndrexmitfast++;
1698 (void) tcp_output(tp);
1699
1700 tp->snd_cwnd = tp->snd_ssthresh +
1701 tp->t_maxseg * tp->t_dupacks;
1702 if (SEQ_GT(onxt, tp->snd_nxt))
1703 tp->snd_nxt = onxt;
1704 goto drop;
1705 } else if (tp->t_dupacks > tcprexmtthresh) {
1706 #if defined(TCP_SACK) && defined(TCP_FACK)
1707
1708
1709
1710
1711 if (tp->sack_enable) {
1712 if (tp->snd_awnd < tp->snd_cwnd)
1713 tcp_output(tp);
1714 goto drop;
1715 }
1716 #endif
1717 tp->snd_cwnd += tp->t_maxseg;
1718 (void) tcp_output(tp);
1719 goto drop;
1720 }
1721 } else if (tiwin < tp->snd_wnd) {
1722
1723
1724
1725
1726
1727
1728 tp->t_dupacks = 0;
1729 }
1730 break;
1731 }
1732
1733
1734
1735
1736 #if defined(TCP_SACK)
1737 if (tp->sack_enable) {
1738 if (tp->t_dupacks >= tcprexmtthresh) {
1739
1740 if (tcp_sack_partialack(tp, th)) {
1741 #if defined(TCP_SACK) && defined(TCP_FACK)
1742
1743 if (tp->snd_awnd < tp->snd_cwnd)
1744 needoutput = 1;
1745 #else
1746 tp->snd_cwnd += tp->t_maxseg;
1747 needoutput = 1;
1748 #endif
1749 } else {
1750
1751 tp->snd_cwnd = tp->snd_ssthresh;
1752 if (tcp_seq_subtract(tp->snd_max,
1753 th->th_ack) < tp->snd_ssthresh)
1754 tp->snd_cwnd =
1755 tcp_seq_subtract(tp->snd_max,
1756 th->th_ack);
1757 tp->t_dupacks = 0;
1758 #if defined(TCP_SACK) && defined(TCP_FACK)
1759 if (SEQ_GT(th->th_ack, tp->snd_fack))
1760 tp->snd_fack = th->th_ack;
1761 #endif
1762 }
1763 }
1764 } else {
1765 if (tp->t_dupacks >= tcprexmtthresh &&
1766 !tcp_newreno(tp, th)) {
1767
1768 tp->snd_cwnd = tp->snd_ssthresh;
1769 if (tcp_seq_subtract(tp->snd_max, th->th_ack) <
1770 tp->snd_ssthresh)
1771 tp->snd_cwnd =
1772 tcp_seq_subtract(tp->snd_max,
1773 th->th_ack);
1774 tp->t_dupacks = 0;
1775 }
1776 }
1777 if (tp->t_dupacks < tcprexmtthresh)
1778 tp->t_dupacks = 0;
1779 #else
1780 if (tp->t_dupacks >= tcprexmtthresh &&
1781 tp->snd_cwnd > tp->snd_ssthresh)
1782 tp->snd_cwnd = tp->snd_ssthresh;
1783 tp->t_dupacks = 0;
1784 #endif
1785 if (SEQ_GT(th->th_ack, tp->snd_max)) {
1786 tcpstat.tcps_rcvacktoomuch++;
1787 goto dropafterack_ratelim;
1788 }
1789 acked = th->th_ack - tp->snd_una;
1790 tcpstat.tcps_rcvackpack++;
1791 tcpstat.tcps_rcvackbyte += acked;
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802 if (opti.ts_present && opti.ts_ecr)
1803 tcp_xmit_timer(tp, tcp_now - opti.ts_ecr);
1804 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
1805 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
1806
1807
1808
1809
1810
1811
1812
1813 if (th->th_ack == tp->snd_max) {
1814 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1815 needoutput = 1;
1816 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
1817 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1818
1819
1820
1821
1822
1823
1824
1825 {
1826 u_int cw = tp->snd_cwnd;
1827 u_int incr = tp->t_maxseg;
1828
1829 if (cw > tp->snd_ssthresh)
1830 incr = incr * incr / cw;
1831 #if defined (TCP_SACK)
1832 if (tp->t_dupacks < tcprexmtthresh)
1833 #endif
1834 tp->snd_cwnd = ulmin(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1835 }
1836 ND6_HINT(tp);
1837 if (acked > so->so_snd.sb_cc) {
1838 tp->snd_wnd -= so->so_snd.sb_cc;
1839 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1840 ourfinisacked = 1;
1841 } else {
1842 sbdrop(&so->so_snd, acked);
1843 tp->snd_wnd -= acked;
1844 ourfinisacked = 0;
1845 }
1846 if (sb_notify(&so->so_snd))
1847 sowwakeup(so);
1848
1849
1850
1851
1852
1853
1854 if ((tp->t_flags & TF_PMTUD_PEND) &&
1855 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq))
1856 tp->t_flags &= ~TF_PMTUD_PEND;
1857
1858
1859
1860
1861
1862 if (tp->t_pmtud_mss_acked < acked)
1863 tp->t_pmtud_mss_acked = acked;
1864
1865 tp->snd_una = th->th_ack;
1866 #ifdef TCP_ECN
1867
1868 if (SEQ_GT(tp->snd_una, tp->snd_last))
1869 tp->snd_last = tp->snd_una;
1870 #endif
1871 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1872 tp->snd_nxt = tp->snd_una;
1873 #if defined (TCP_SACK) && defined (TCP_FACK)
1874 if (SEQ_GT(tp->snd_una, tp->snd_fack)) {
1875 tp->snd_fack = tp->snd_una;
1876
1877
1878
1879 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt,
1880 tp->snd_fack) + tp->retran_data;
1881 }
1882 #endif
1883
1884 switch (tp->t_state) {
1885
1886
1887
1888
1889
1890
1891 case TCPS_FIN_WAIT_1:
1892 if (ourfinisacked) {
1893
1894
1895
1896
1897
1898
1899
1900 if (so->so_state & SS_CANTRCVMORE) {
1901 soisdisconnected(so);
1902 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
1903 }
1904 tp->t_state = TCPS_FIN_WAIT_2;
1905 }
1906 break;
1907
1908
1909
1910
1911
1912
1913
1914 case TCPS_CLOSING:
1915 if (ourfinisacked) {
1916 tp->t_state = TCPS_TIME_WAIT;
1917 tcp_canceltimers(tp);
1918 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1919 soisdisconnected(so);
1920 }
1921 break;
1922
1923
1924
1925
1926
1927
1928
1929 case TCPS_LAST_ACK:
1930 if (ourfinisacked) {
1931 tp = tcp_close(tp);
1932 goto drop;
1933 }
1934 break;
1935
1936
1937
1938
1939
1940
1941 case TCPS_TIME_WAIT:
1942 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1943 goto dropafterack;
1944 }
1945 }
1946
1947 step6:
1948
1949
1950
1951
1952 if ((tiflags & TH_ACK) &&
1953 (SEQ_LT(tp->snd_wl1, th->th_seq) || (tp->snd_wl1 == th->th_seq &&
1954 (SEQ_LT(tp->snd_wl2, th->th_ack) ||
1955 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
1956
1957 if (tlen == 0 &&
1958 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
1959 tcpstat.tcps_rcvwinupd++;
1960 tp->snd_wnd = tiwin;
1961 tp->snd_wl1 = th->th_seq;
1962 tp->snd_wl2 = th->th_ack;
1963 if (tp->snd_wnd > tp->max_sndwnd)
1964 tp->max_sndwnd = tp->snd_wnd;
1965 needoutput = 1;
1966 }
1967
1968
1969
1970
1971 if ((tiflags & TH_URG) && th->th_urp &&
1972 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1973
1974
1975
1976
1977
1978
1979 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
1980 th->th_urp = 0;
1981 tiflags &= ~TH_URG;
1982 goto dodata;
1983 }
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
1999 tp->rcv_up = th->th_seq + th->th_urp;
2000 so->so_oobmark = so->so_rcv.sb_cc +
2001 (tp->rcv_up - tp->rcv_nxt) - 1;
2002 if (so->so_oobmark == 0)
2003 so->so_state |= SS_RCVATMARK;
2004 sohasoutofband(so);
2005 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2006 }
2007
2008
2009
2010
2011
2012
2013 if (th->th_urp <= (u_int16_t) tlen
2014 #ifdef SO_OOBINLINE
2015 && (so->so_options & SO_OOBINLINE) == 0
2016 #endif
2017 )
2018 tcp_pulloutofband(so, th->th_urp, m, hdroptlen);
2019 } else
2020
2021
2022
2023
2024
2025 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2026 tp->rcv_up = tp->rcv_nxt;
2027 dodata:
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037 if ((tlen || (tiflags & TH_FIN)) &&
2038 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2039 #ifdef TCP_SACK
2040 tcp_seq laststart = th->th_seq;
2041 tcp_seq lastend = th->th_seq + tlen;
2042 #endif
2043 tcp_reass_lock(tp);
2044 if (th->th_seq == tp->rcv_nxt && TAILQ_EMPTY(&tp->t_segq) &&
2045 tp->t_state == TCPS_ESTABLISHED) {
2046 tcp_reass_unlock(tp);
2047 TCP_SETUP_ACK(tp, tiflags);
2048 tp->rcv_nxt += tlen;
2049 tiflags = th->th_flags & TH_FIN;
2050 tcpstat.tcps_rcvpack++;
2051 tcpstat.tcps_rcvbyte += tlen;
2052 ND6_HINT(tp);
2053 if (so->so_state & SS_CANTRCVMORE)
2054 m_freem(m);
2055 else {
2056 m_adj(m, hdroptlen);
2057 sbappendstream(&so->so_rcv, m);
2058 }
2059 sorwakeup(so);
2060 } else {
2061 m_adj(m, hdroptlen);
2062 tiflags = tcp_reass(tp, th, m, &tlen);
2063 tcp_reass_unlock(tp);
2064 tp->t_flags |= TF_ACKNOW;
2065 }
2066 #ifdef TCP_SACK
2067 if (tp->sack_enable)
2068 tcp_update_sack_list(tp, laststart, lastend);
2069 #endif
2070
2071
2072
2073
2074
2075 #if 0
2076
2077
2078
2079
2080
2081 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2082 #endif
2083 } else {
2084 m_freem(m);
2085 tiflags &= ~TH_FIN;
2086 }
2087
2088
2089
2090
2091
2092
2093 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
2094 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2095 socantrcvmore(so);
2096 tp->t_flags |= TF_ACKNOW;
2097 tp->rcv_nxt++;
2098 }
2099 switch (tp->t_state) {
2100
2101
2102
2103
2104 case TCPS_ESTABLISHED:
2105 tp->t_state = TCPS_CLOSE_WAIT;
2106 break;
2107
2108
2109
2110
2111
2112 case TCPS_FIN_WAIT_1:
2113 tp->t_state = TCPS_CLOSING;
2114 break;
2115
2116
2117
2118
2119
2120
2121 case TCPS_FIN_WAIT_2:
2122 tp->t_state = TCPS_TIME_WAIT;
2123 tcp_canceltimers(tp);
2124 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2125 soisdisconnected(so);
2126 break;
2127
2128
2129
2130
2131 case TCPS_TIME_WAIT:
2132 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2133 break;
2134 }
2135 }
2136 if (so->so_options & SO_DEBUG) {
2137 switch (tp->pf) {
2138 #ifdef INET6
2139 case PF_INET6:
2140 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti6,
2141 0, tlen);
2142 break;
2143 #endif
2144 case PF_INET:
2145 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti,
2146 0, tlen);
2147 break;
2148 }
2149 }
2150
2151
2152
2153
2154 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
2155 (void) tcp_output(tp);
2156 }
2157 return;
2158
2159 badsyn:
2160
2161
2162
2163 tcpstat.tcps_badsyn++;
2164 tp = NULL;
2165 goto dropwithreset;
2166
2167 dropafterack_ratelim:
2168 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
2169 tcp_ackdrop_ppslim) == 0) {
2170
2171 goto drop;
2172 }
2173
2174
2175 dropafterack:
2176
2177
2178
2179
2180 if (tiflags & TH_RST)
2181 goto drop;
2182 m_freem(m);
2183 tp->t_flags |= TF_ACKNOW;
2184 (void) tcp_output(tp);
2185 return;
2186
2187 dropwithreset_ratelim:
2188
2189
2190
2191
2192
2193
2194 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count,
2195 tcp_rst_ppslim) == 0) {
2196
2197 goto drop;
2198 }
2199
2200
2201 dropwithreset:
2202
2203
2204
2205
2206
2207 if (tiflags & TH_RST)
2208 goto drop;
2209 if (tiflags & TH_ACK) {
2210 tcp_respond(tp, mtod(m, caddr_t), m, (tcp_seq)0, th->th_ack,
2211 TH_RST);
2212 } else {
2213 if (tiflags & TH_SYN)
2214 tlen++;
2215 tcp_respond(tp, mtod(m, caddr_t), m, th->th_seq + tlen,
2216 (tcp_seq)0, TH_RST|TH_ACK);
2217 }
2218 return;
2219
2220 drop:
2221
2222
2223
2224 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) {
2225 switch (tp->pf) {
2226 #ifdef INET6
2227 case PF_INET6:
2228 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti6,
2229 0, tlen);
2230 break;
2231 #endif
2232 case PF_INET:
2233 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti,
2234 0, tlen);
2235 break;
2236 }
2237 }
2238
2239 m_freem(m);
2240 return;
2241 }
2242
2243 int
2244 tcp_dooptions(tp, cp, cnt, th, m, iphlen, oi)
2245 struct tcpcb *tp;
2246 u_char *cp;
2247 int cnt;
2248 struct tcphdr *th;
2249 struct mbuf *m;
2250 int iphlen;
2251 struct tcp_opt_info *oi;
2252 {
2253 u_int16_t mss = 0;
2254 int opt, optlen;
2255 #ifdef TCP_SIGNATURE
2256 caddr_t sigp = NULL;
2257 struct tdb *tdb = NULL;
2258 #endif
2259
2260 for (; cp && cnt > 0; cnt -= optlen, cp += optlen) {
2261 opt = cp[0];
2262 if (opt == TCPOPT_EOL)
2263 break;
2264 if (opt == TCPOPT_NOP)
2265 optlen = 1;
2266 else {
2267 if (cnt < 2)
2268 break;
2269 optlen = cp[1];
2270 if (optlen < 2 || optlen > cnt)
2271 break;
2272 }
2273 switch (opt) {
2274
2275 default:
2276 continue;
2277
2278 case TCPOPT_MAXSEG:
2279 if (optlen != TCPOLEN_MAXSEG)
2280 continue;
2281 if (!(th->th_flags & TH_SYN))
2282 continue;
2283 if (TCPS_HAVERCVDSYN(tp->t_state))
2284 continue;
2285 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
2286 NTOHS(mss);
2287 oi->maxseg = mss;
2288 break;
2289
2290 case TCPOPT_WINDOW:
2291 if (optlen != TCPOLEN_WINDOW)
2292 continue;
2293 if (!(th->th_flags & TH_SYN))
2294 continue;
2295 if (TCPS_HAVERCVDSYN(tp->t_state))
2296 continue;
2297 tp->t_flags |= TF_RCVD_SCALE;
2298 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2299 break;
2300
2301 case TCPOPT_TIMESTAMP:
2302 if (optlen != TCPOLEN_TIMESTAMP)
2303 continue;
2304 oi->ts_present = 1;
2305 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
2306 NTOHL(oi->ts_val);
2307 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
2308 NTOHL(oi->ts_ecr);
2309
2310 if (!(th->th_flags & TH_SYN))
2311 continue;
2312 if (TCPS_HAVERCVDSYN(tp->t_state))
2313 continue;
2314
2315
2316
2317
2318 tp->t_flags |= TF_RCVD_TSTMP;
2319 tp->ts_recent = oi->ts_val;
2320 tp->ts_recent_age = tcp_now;
2321 break;
2322
2323 #ifdef TCP_SACK
2324 case TCPOPT_SACK_PERMITTED:
2325 if (!tp->sack_enable || optlen!=TCPOLEN_SACK_PERMITTED)
2326 continue;
2327 if (!(th->th_flags & TH_SYN))
2328 continue;
2329 if (TCPS_HAVERCVDSYN(tp->t_state))
2330 continue;
2331
2332 tp->t_flags |= TF_SACK_PERMIT;
2333 break;
2334 case TCPOPT_SACK:
2335 tcp_sack_option(tp, th, cp, optlen);
2336 break;
2337 #endif
2338 #ifdef TCP_SIGNATURE
2339 case TCPOPT_SIGNATURE:
2340 if (optlen != TCPOLEN_SIGNATURE)
2341 continue;
2342
2343 if (sigp && bcmp(sigp, cp + 2, 16))
2344 return (-1);
2345
2346 sigp = cp + 2;
2347 break;
2348 #endif
2349 }
2350 }
2351
2352 #ifdef TCP_SIGNATURE
2353 if (tp->t_flags & TF_SIGNATURE) {
2354 union sockaddr_union src, dst;
2355
2356 memset(&src, 0, sizeof(union sockaddr_union));
2357 memset(&dst, 0, sizeof(union sockaddr_union));
2358
2359 switch (tp->pf) {
2360 case 0:
2361 #ifdef INET
2362 case AF_INET:
2363 src.sa.sa_len = sizeof(struct sockaddr_in);
2364 src.sa.sa_family = AF_INET;
2365 src.sin.sin_addr = mtod(m, struct ip *)->ip_src;
2366 dst.sa.sa_len = sizeof(struct sockaddr_in);
2367 dst.sa.sa_family = AF_INET;
2368 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst;
2369 break;
2370 #endif
2371 #ifdef INET6
2372 case AF_INET6:
2373 src.sa.sa_len = sizeof(struct sockaddr_in6);
2374 src.sa.sa_family = AF_INET6;
2375 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src;
2376 dst.sa.sa_len = sizeof(struct sockaddr_in6);
2377 dst.sa.sa_family = AF_INET6;
2378 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst;
2379 break;
2380 #endif
2381 }
2382
2383 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP);
2384
2385
2386
2387
2388
2389 if (tdb == NULL && tp->t_state == TCPS_LISTEN)
2390 tp->t_flags &= ~TF_SIGNATURE;
2391
2392 }
2393
2394 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) {
2395 tcpstat.tcps_rcvbadsig++;
2396 return (-1);
2397 }
2398
2399 if (sigp) {
2400 char sig[16];
2401
2402 if (tdb == NULL) {
2403 tcpstat.tcps_rcvbadsig++;
2404 return (-1);
2405 }
2406
2407 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 1, sig) < 0)
2408 return (-1);
2409
2410 if (bcmp(sig, sigp, 16)) {
2411 tcpstat.tcps_rcvbadsig++;
2412 return (-1);
2413 }
2414
2415 tcpstat.tcps_rcvgoodsig++;
2416 }
2417 #endif
2418
2419 return (0);
2420 }
2421
2422 #if defined(TCP_SACK)
2423 u_long
2424 tcp_seq_subtract(a, b)
2425 u_long a, b;
2426 {
2427 return ((long)(a - b));
2428 }
2429 #endif
2430
2431
2432 #ifdef TCP_SACK
2433
2434
2435
2436
2437 void
2438 tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_laststart,
2439 tcp_seq rcv_lastend)
2440 {
2441
2442
2443
2444
2445
2446
2447 int i, j = 0, count = 0, lastpos = -1;
2448 struct sackblk sack, firstsack, temp[MAX_SACK_BLKS];
2449
2450
2451 for (i = 0; i < tp->rcv_numsacks; i++) {
2452 sack = tp->sackblks[i];
2453 if (sack.start == 0 && sack.end == 0) {
2454 count++;
2455 continue;
2456 }
2457 if (SEQ_LEQ(sack.end, tp->rcv_nxt)) {
2458 tp->sackblks[i].start = tp->sackblks[i].end = 0;
2459 count++;
2460 } else {
2461 temp[j].start = tp->sackblks[i].start;
2462 temp[j++].end = tp->sackblks[i].end;
2463 }
2464 }
2465 tp->rcv_numsacks -= count;
2466 if (tp->rcv_numsacks == 0) {
2467 tcp_clean_sackreport(tp);
2468 if (SEQ_LT(tp->rcv_nxt, rcv_laststart)) {
2469
2470 tp->sackblks[0].start = rcv_laststart;
2471 tp->sackblks[0].end = rcv_lastend;
2472 tp->rcv_numsacks = 1;
2473 }
2474 return;
2475 }
2476
2477 for (i = 0; i < tp->rcv_numsacks; i++)
2478 tp->sackblks[i] = temp[i];
2479 if (SEQ_GEQ(tp->rcv_nxt, rcv_lastend))
2480 return;
2481
2482
2483
2484
2485 firstsack.start = rcv_laststart;
2486 firstsack.end = rcv_lastend;
2487 for (i = 0; i < tp->rcv_numsacks; i++) {
2488 sack = tp->sackblks[i];
2489 if (SEQ_LT(sack.end, firstsack.start) ||
2490 SEQ_GT(sack.start, firstsack.end))
2491 continue;
2492 if (sack.start == firstsack.start && sack.end == firstsack.end){
2493
2494
2495
2496
2497 tp->sackblks[i].start = tp->sackblks[i].end = 0;
2498 lastpos = i;
2499 continue;
2500 }
2501 if (SEQ_LEQ(sack.start, firstsack.start))
2502 firstsack.start = sack.start;
2503 if (SEQ_GEQ(sack.end, firstsack.end))
2504 firstsack.end = sack.end;
2505 tp->sackblks[i].start = tp->sackblks[i].end = 0;
2506 lastpos = i;
2507 }
2508 if (lastpos != -1) {
2509 for (i = 0, j = 1; i < tp->rcv_numsacks; i++) {
2510 sack = tp->sackblks[i];
2511 if (sack.start == 0 && sack.end == 0)
2512 continue;
2513 temp[j++] = sack;
2514 }
2515 tp->rcv_numsacks = j;
2516 for (i = 1; i < tp->rcv_numsacks; i++)
2517 tp->sackblks[i] = temp[i];
2518 } else {
2519 if (tp->rcv_numsacks < MAX_SACK_BLKS)
2520 tp->rcv_numsacks++;
2521 for (i = tp->rcv_numsacks-1; i > 0; i--)
2522 tp->sackblks[i] = tp->sackblks[i-1];
2523 }
2524 tp->sackblks[0] = firstsack;
2525 return;
2526 }
2527
2528
2529
2530
2531
2532 void
2533 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen)
2534 {
2535 int tmp_olen;
2536 u_char *tmp_cp;
2537 struct sackhole *cur, *p, *temp;
2538
2539 if (!tp->sack_enable)
2540 return;
2541
2542 if ((th->th_flags & TH_ACK) == 0)
2543 return;
2544
2545 if (SEQ_LT(th->th_ack, tp->snd_una) ||
2546 SEQ_GT(th->th_ack, tp->snd_max))
2547 return;
2548
2549 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
2550 return;
2551
2552 tmp_cp = cp + 2;
2553 tmp_olen = optlen - 2;
2554 tcpstat.tcps_sack_rcv_opts++;
2555 if (tp->snd_numholes < 0)
2556 tp->snd_numholes = 0;
2557 if (tp->t_maxseg == 0)
2558 panic("tcp_sack_option");
2559 while (tmp_olen > 0) {
2560 struct sackblk sack;
2561
2562 bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq));
2563 NTOHL(sack.start);
2564 bcopy(tmp_cp + sizeof(tcp_seq),
2565 (char *) &(sack.end), sizeof(tcp_seq));
2566 NTOHL(sack.end);
2567 tmp_olen -= TCPOLEN_SACK;
2568 tmp_cp += TCPOLEN_SACK;
2569 if (SEQ_LEQ(sack.end, sack.start))
2570 continue;
2571 if (SEQ_LEQ(sack.end, tp->snd_una))
2572 continue;
2573 #if defined(TCP_SACK) && defined(TCP_FACK)
2574
2575 if (SEQ_GT(sack.end, tp->snd_fack))
2576 tp->snd_fack = sack.end;
2577 #endif
2578 if (SEQ_GT(th->th_ack, tp->snd_una)) {
2579 if (SEQ_LT(sack.start, th->th_ack))
2580 continue;
2581 }
2582 if (SEQ_GT(sack.end, tp->snd_max))
2583 continue;
2584 if (tp->snd_holes == NULL) {
2585 tp->snd_holes = (struct sackhole *)
2586 pool_get(&sackhl_pool, PR_NOWAIT);
2587 if (tp->snd_holes == NULL) {
2588
2589 goto done;
2590 }
2591 cur = tp->snd_holes;
2592 cur->start = th->th_ack;
2593 cur->end = sack.start;
2594 cur->rxmit = cur->start;
2595 cur->next = NULL;
2596 tp->snd_numholes = 1;
2597 tp->rcv_lastsack = sack.end;
2598
2599
2600
2601
2602 cur->dups = min(tcprexmtthresh,
2603 ((sack.end - cur->end)/tp->t_maxseg));
2604 if (cur->dups < 1)
2605 cur->dups = 1;
2606 continue;
2607 }
2608
2609 p = cur = tp->snd_holes;
2610 while (cur) {
2611 if (SEQ_LEQ(sack.end, cur->start))
2612
2613 break;
2614 if (SEQ_GEQ(sack.start, cur->end)) {
2615
2616 cur->dups++;
2617 if (((sack.end - cur->end)/tp->t_maxseg) >=
2618 tcprexmtthresh)
2619 cur->dups = tcprexmtthresh;
2620 p = cur;
2621 cur = cur->next;
2622 continue;
2623 }
2624 if (SEQ_LEQ(sack.start, cur->start)) {
2625
2626 #if defined(TCP_SACK) && defined(TCP_FACK)
2627 if (SEQ_GT(sack.end, cur->rxmit))
2628 tp->retran_data -=
2629 tcp_seq_subtract(cur->rxmit,
2630 cur->start);
2631 else
2632 tp->retran_data -=
2633 tcp_seq_subtract(sack.end,
2634 cur->start);
2635 #endif
2636 if (SEQ_GEQ(sack.end, cur->end)) {
2637
2638 if (p != cur) {
2639 p->next = cur->next;
2640 pool_put(&sackhl_pool, cur);
2641 cur = p->next;
2642 } else {
2643 cur = cur->next;
2644 pool_put(&sackhl_pool, p);
2645 p = cur;
2646 tp->snd_holes = p;
2647 }
2648 tp->snd_numholes--;
2649 continue;
2650 }
2651
2652 cur->start = sack.end;
2653 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
2654 p = cur;
2655 cur = cur->next;
2656 continue;
2657 }
2658
2659 if (SEQ_GEQ(sack.end, cur->end)) {
2660 #if defined(TCP_SACK) && defined(TCP_FACK)
2661 if (SEQ_GT(cur->rxmit, sack.start))
2662 tp->retran_data -=
2663 tcp_seq_subtract(cur->rxmit,
2664 sack.start);
2665 #endif
2666 cur->end = sack.start;
2667 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
2668 cur->dups++;
2669 if (((sack.end - cur->end)/tp->t_maxseg) >=
2670 tcprexmtthresh)
2671 cur->dups = tcprexmtthresh;
2672 p = cur;
2673 cur = cur->next;
2674 continue;
2675 }
2676 if (SEQ_LT(cur->start, sack.start) &&
2677 SEQ_GT(cur->end, sack.end)) {
2678
2679
2680
2681
2682 temp = (struct sackhole *)
2683 pool_get(&sackhl_pool, PR_NOWAIT);
2684 if (temp == NULL)
2685 goto done;
2686 #if defined(TCP_SACK) && defined(TCP_FACK)
2687 if (SEQ_GT(cur->rxmit, sack.end))
2688 tp->retran_data -=
2689 tcp_seq_subtract(sack.end,
2690 sack.start);
2691 else if (SEQ_GT(cur->rxmit, sack.start))
2692 tp->retran_data -=
2693 tcp_seq_subtract(cur->rxmit,
2694 sack.start);
2695 #endif
2696 temp->next = cur->next;
2697 temp->start = sack.end;
2698 temp->end = cur->end;
2699 temp->dups = cur->dups;
2700 temp->rxmit = SEQ_MAX(cur->rxmit, temp->start);
2701 cur->end = sack.start;
2702 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
2703 cur->dups++;
2704 if (((sack.end - cur->end)/tp->t_maxseg) >=
2705 tcprexmtthresh)
2706 cur->dups = tcprexmtthresh;
2707 cur->next = temp;
2708 p = temp;
2709 cur = p->next;
2710 tp->snd_numholes++;
2711 }
2712 }
2713
2714 if (SEQ_LT(tp->rcv_lastsack, sack.start)) {
2715
2716
2717
2718
2719 temp = (struct sackhole *)
2720 pool_get(&sackhl_pool, PR_NOWAIT);
2721 if (temp == NULL)
2722 goto done;
2723 temp->start = tp->rcv_lastsack;
2724 temp->end = sack.start;
2725 temp->dups = min(tcprexmtthresh,
2726 ((sack.end - sack.start)/tp->t_maxseg));
2727 if (temp->dups < 1)
2728 temp->dups = 1;
2729 temp->rxmit = temp->start;
2730 temp->next = 0;
2731 p->next = temp;
2732 tp->rcv_lastsack = sack.end;
2733 tp->snd_numholes++;
2734 }
2735 }
2736 done:
2737 #if defined(TCP_SACK) && defined(TCP_FACK)
2738
2739
2740
2741
2742 tp->retran_data = 0;
2743 cur = tp->snd_holes;
2744 while (cur) {
2745 tp->retran_data += cur->rxmit - cur->start;
2746 cur = cur->next;
2747 }
2748 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, tp->snd_fack) +
2749 tp->retran_data;
2750 #endif
2751
2752 return;
2753 }
2754
2755
2756
2757
2758
2759
2760 void
2761 tcp_del_sackholes(tp, th)
2762 struct tcpcb *tp;
2763 struct tcphdr *th;
2764 {
2765 if (tp->sack_enable && tp->t_state != TCPS_LISTEN) {
2766
2767 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ?
2768 th->th_ack : tp->snd_una;
2769 struct sackhole *cur = tp->snd_holes;
2770 struct sackhole *prev;
2771 while (cur)
2772 if (SEQ_LEQ(cur->end, lastack)) {
2773 prev = cur;
2774 cur = cur->next;
2775 pool_put(&sackhl_pool, prev);
2776 tp->snd_numholes--;
2777 } else if (SEQ_LT(cur->start, lastack)) {
2778 cur->start = lastack;
2779 if (SEQ_LT(cur->rxmit, cur->start))
2780 cur->rxmit = cur->start;
2781 break;
2782 } else
2783 break;
2784 tp->snd_holes = cur;
2785 }
2786 }
2787
2788
2789
2790
2791 void
2792 tcp_clean_sackreport(tp)
2793 struct tcpcb *tp;
2794 {
2795 int i;
2796
2797 tp->rcv_numsacks = 0;
2798 for (i = 0; i < MAX_SACK_BLKS; i++)
2799 tp->sackblks[i].start = tp->sackblks[i].end=0;
2800
2801 }
2802
2803
2804
2805
2806
2807
2808 int
2809 tcp_sack_partialack(tp, th)
2810 struct tcpcb *tp;
2811 struct tcphdr *th;
2812 {
2813 if (SEQ_LT(th->th_ack, tp->snd_last)) {
2814
2815 TCP_TIMER_DISARM(tp, TCPT_REXMT);
2816 tp->t_rtttime = 0;
2817 #ifndef TCP_FACK
2818
2819
2820
2821
2822
2823 if (tp->snd_cwnd > (th->th_ack - tp->snd_una)) {
2824 tp->snd_cwnd -= th->th_ack - tp->snd_una;
2825 tp->snd_cwnd += tp->t_maxseg;
2826 } else
2827 tp->snd_cwnd = tp->t_maxseg;
2828 #endif
2829 return (1);
2830 }
2831 return (0);
2832 }
2833 #endif
2834
2835
2836
2837
2838
2839
2840
2841 void
2842 tcp_pulloutofband(so, urgent, m, off)
2843 struct socket *so;
2844 u_int urgent;
2845 struct mbuf *m;
2846 int off;
2847 {
2848 int cnt = off + urgent - 1;
2849
2850 while (cnt >= 0) {
2851 if (m->m_len > cnt) {
2852 char *cp = mtod(m, caddr_t) + cnt;
2853 struct tcpcb *tp = sototcpcb(so);
2854
2855 tp->t_iobc = *cp;
2856 tp->t_oobflags |= TCPOOB_HAVEDATA;
2857 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2858 m->m_len--;
2859 return;
2860 }
2861 cnt -= m->m_len;
2862 m = m->m_next;
2863 if (m == 0)
2864 break;
2865 }
2866 panic("tcp_pulloutofband");
2867 }
2868
2869
2870
2871
2872
2873 void
2874 tcp_xmit_timer(tp, rtt)
2875 struct tcpcb *tp;
2876 short rtt;
2877 {
2878 short delta;
2879 short rttmin;
2880
2881 if (rtt < 0)
2882 rtt = 0;
2883 else if (rtt > TCP_RTT_MAX)
2884 rtt = TCP_RTT_MAX;
2885
2886 tcpstat.tcps_rttupdated++;
2887 if (tp->t_srtt != 0) {
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897 delta = (rtt << TCP_RTT_BASE_SHIFT) -
2898 (tp->t_srtt >> TCP_RTT_SHIFT);
2899 if ((tp->t_srtt += delta) <= 0)
2900 tp->t_srtt = 1 << TCP_RTT_BASE_SHIFT;
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911 if (delta < 0)
2912 delta = -delta;
2913 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
2914 if ((tp->t_rttvar += delta) <= 0)
2915 tp->t_rttvar = 1 << TCP_RTT_BASE_SHIFT;
2916 } else {
2917
2918
2919
2920
2921
2922 tp->t_srtt = (rtt + 1) << (TCP_RTT_SHIFT + TCP_RTT_BASE_SHIFT);
2923 tp->t_rttvar = (rtt + 1) <<
2924 (TCP_RTTVAR_SHIFT + TCP_RTT_BASE_SHIFT - 1);
2925 }
2926 tp->t_rtttime = 0;
2927 tp->t_rxtshift = 0;
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940 rttmin = min(max(rtt + 2, tp->t_rttmin), TCPTV_REXMTMAX);
2941 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX);
2942
2943
2944
2945
2946
2947
2948
2949
2950 tp->t_softerror = 0;
2951 }
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977 int
2978 tcp_mss(tp, offer)
2979 struct tcpcb *tp;
2980 int offer;
2981 {
2982 struct rtentry *rt;
2983 struct ifnet *ifp;
2984 int mss, mssopt;
2985 int iphlen;
2986 struct inpcb *inp;
2987
2988 inp = tp->t_inpcb;
2989
2990 mssopt = mss = tcp_mssdflt;
2991
2992 rt = in_pcbrtentry(inp);
2993
2994 if (rt == NULL)
2995 goto out;
2996
2997 ifp = rt->rt_ifp;
2998
2999 switch (tp->pf) {
3000 #ifdef INET6
3001 case AF_INET6:
3002 iphlen = sizeof(struct ip6_hdr);
3003 break;
3004 #endif
3005 case AF_INET:
3006 iphlen = sizeof(struct ip);
3007 break;
3008 default:
3009
3010 goto out;
3011 }
3012
3013 #ifdef RTV_MTU
3014
3015
3016
3017
3018 if (rt->rt_rmx.rmx_mtu) {
3019
3020
3021
3022
3023 if (tp->pf == AF_INET6 && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
3024
3025
3026
3027
3028
3029 mss = IPV6_MMTU - iphlen - sizeof(struct ip6_frag) -
3030 sizeof(struct tcphdr);
3031 } else
3032 mss = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
3033 } else
3034 #endif
3035 if (!ifp)
3036
3037
3038
3039
3040
3041 goto out;
3042 else if (ifp->if_flags & IFF_LOOPBACK)
3043 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3044 else if (tp->pf == AF_INET) {
3045 if (ip_mtudisc)
3046 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3047 else if (inp && in_localaddr(inp->inp_faddr))
3048 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3049 }
3050 #ifdef INET6
3051 else if (tp->pf == AF_INET6) {
3052
3053
3054
3055
3056 mss = IN6_LINKMTU(ifp) - iphlen - sizeof(struct tcphdr);
3057 }
3058 #endif
3059
3060
3061 if (offer != -1) {
3062 #ifndef INET6
3063 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3064 #else
3065 if (tp->pf == AF_INET6)
3066 mssopt = IN6_LINKMTU(ifp) - iphlen -
3067 sizeof(struct tcphdr);
3068 else
3069 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3070 #endif
3071
3072 mssopt = max(tcp_mssdflt, mssopt);
3073 }
3074
3075 out:
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086 if (offer > 0)
3087 tp->t_peermss = offer;
3088 if (tp->t_peermss)
3089 mss = min(mss, max(tp->t_peermss, 216));
3090
3091
3092 mss = max(mss, 64);
3093
3094
3095
3096
3097
3098
3099
3100
3101 tp->t_maxopd = mss;
3102
3103 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3104 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
3105 mss -= TCPOLEN_TSTAMP_APPA;
3106 #ifdef TCP_SIGNATURE
3107 if (tp->t_flags & TF_SIGNATURE)
3108 mss -= TCPOLEN_SIGLEN;
3109 #endif
3110
3111 if (offer == -1) {
3112
3113 tp->t_flags &= ~TF_PMTUD_PEND;
3114 tp->t_pmtud_mtu_sent = 0;
3115 tp->t_pmtud_mss_acked = 0;
3116 if (mss < tp->t_maxseg) {
3117
3118
3119
3120
3121
3122 tp->snd_cwnd = ulmax((tp->snd_cwnd / tp->t_maxseg) *
3123 mss, mss);
3124 }
3125 } else if (tcp_do_rfc3390) {
3126
3127 tp->snd_cwnd = ulmin(4 * mss, ulmax(2 * mss, 4380));
3128 } else
3129 tp->snd_cwnd = mss;
3130
3131 tp->t_maxseg = mss;
3132
3133 return (offer != -1 ? mssopt : mss);
3134 }
3135
3136 u_int
3137 tcp_hdrsz(struct tcpcb *tp)
3138 {
3139 u_int hlen;
3140
3141 switch (tp->pf) {
3142 #ifdef INET6
3143 case AF_INET6:
3144 hlen = sizeof(struct ip6_hdr);
3145 break;
3146 #endif
3147 case AF_INET:
3148 hlen = sizeof(struct ip);
3149 break;
3150 default:
3151 hlen = 0;
3152 break;
3153 }
3154 hlen += sizeof(struct tcphdr);
3155
3156 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3157 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
3158 hlen += TCPOLEN_TSTAMP_APPA;
3159 #ifdef TCP_SIGNATURE
3160 if (tp->t_flags & TF_SIGNATURE)
3161 hlen += TCPOLEN_SIGLEN;
3162 #endif
3163 return (hlen);
3164 }
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176 void
3177 tcp_mss_update(tp)
3178 struct tcpcb *tp;
3179 {
3180 int mss;
3181 u_long bufsize;
3182 struct rtentry *rt;
3183 struct socket *so;
3184
3185 so = tp->t_inpcb->inp_socket;
3186 mss = tp->t_maxseg;
3187
3188 rt = in_pcbrtentry(tp->t_inpcb);
3189
3190 if (rt == NULL)
3191 return;
3192
3193 bufsize = so->so_snd.sb_hiwat;
3194 if (bufsize < mss) {
3195 mss = bufsize;
3196
3197 tcp_mss(tp, mss);
3198 } else {
3199 bufsize = roundup(bufsize, mss);
3200 if (bufsize > sb_max)
3201 bufsize = sb_max;
3202 (void)sbreserve(&so->so_snd, bufsize);
3203 }
3204
3205 bufsize = so->so_rcv.sb_hiwat;
3206 if (bufsize > mss) {
3207 bufsize = roundup(bufsize, mss);
3208 if (bufsize > sb_max)
3209 bufsize = sb_max;
3210 (void)sbreserve(&so->so_rcv, bufsize);
3211 }
3212
3213 }
3214
3215 #if defined (TCP_SACK)
3216
3217
3218
3219
3220
3221
3222 int
3223 tcp_newreno(tp, th)
3224 struct tcpcb *tp;
3225 struct tcphdr *th;
3226 {
3227 if (SEQ_LT(th->th_ack, tp->snd_last)) {
3228
3229
3230
3231
3232
3233
3234 tcp_seq onxt = tp->snd_nxt;
3235 u_long ocwnd = tp->snd_cwnd;
3236 TCP_TIMER_DISARM(tp, TCPT_REXMT);
3237 tp->t_rtttime = 0;
3238 tp->snd_nxt = th->th_ack;
3239
3240
3241
3242
3243 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3244 (void) tcp_output(tp);
3245 tp->snd_cwnd = ocwnd;
3246 if (SEQ_GT(onxt, tp->snd_nxt))
3247 tp->snd_nxt = onxt;
3248
3249
3250
3251
3252 if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3253 tp->snd_cwnd -= th->th_ack - tp->snd_una;
3254 else
3255 tp->snd_cwnd = 0;
3256 tp->snd_cwnd += tp->t_maxseg;
3257
3258 return 1;
3259 }
3260 return 0;
3261 }
3262 #endif
3263
3264 int
3265 tcp_mss_adv(struct ifnet *ifp, int af)
3266 {
3267 int mss = 0;
3268 int iphlen;
3269
3270 switch (af) {
3271 case AF_INET:
3272 if (ifp != NULL)
3273 mss = ifp->if_mtu;
3274 iphlen = sizeof(struct ip);
3275 break;
3276 #ifdef INET6
3277 case AF_INET6:
3278 if (ifp != NULL)
3279 mss = IN6_LINKMTU(ifp);
3280 iphlen = sizeof(struct ip6_hdr);
3281 break;
3282 #endif
3283 }
3284 mss = mss - iphlen - sizeof(struct tcphdr);
3285 return (max(mss, tcp_mssdflt));
3286 }
3287
3288
3289
3290
3291
3292
3293 u_long syn_cache_count;
3294 u_int32_t syn_hash1, syn_hash2;
3295
3296 #define SYN_HASH(sa, sp, dp) \
3297 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
3298 ((u_int32_t)(sp)))^syn_hash2)))
3299 #ifndef INET6
3300 #define SYN_HASHALL(hash, src, dst) \
3301 do { \
3302 hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \
3303 ((struct sockaddr_in *)(src))->sin_port, \
3304 ((struct sockaddr_in *)(dst))->sin_port); \
3305 } while ( 0)
3306 #else
3307 #define SYN_HASH6(sa, sp, dp) \
3308 ((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
3309 (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
3310 & 0x7fffffff)
3311
3312 #define SYN_HASHALL(hash, src, dst) \
3313 do { \
3314 switch ((src)->sa_family) { \
3315 case AF_INET: \
3316 hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \
3317 ((struct sockaddr_in *)(src))->sin_port, \
3318 ((struct sockaddr_in *)(dst))->sin_port); \
3319 break; \
3320 case AF_INET6: \
3321 hash = SYN_HASH6(&((struct sockaddr_in6 *)(src))->sin6_addr, \
3322 ((struct sockaddr_in6 *)(src))->sin6_port, \
3323 ((struct sockaddr_in6 *)(dst))->sin6_port); \
3324 break; \
3325 default: \
3326 hash = 0; \
3327 } \
3328 } while (0)
3329 #endif
3330
3331 #define SYN_CACHE_RM(sc) \
3332 do { \
3333 (sc)->sc_flags |= SCF_DEAD; \
3334 TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket, \
3335 (sc), sc_bucketq); \
3336 (sc)->sc_tp = NULL; \
3337 LIST_REMOVE((sc), sc_tpq); \
3338 tcp_syn_cache[(sc)->sc_bucketidx].sch_length--; \
3339 timeout_del(&(sc)->sc_timer); \
3340 syn_cache_count--; \
3341 } while (0)
3342
3343 #define SYN_CACHE_PUT(sc) \
3344 do { \
3345 if ((sc)->sc_ipopts) \
3346 (void) m_free((sc)->sc_ipopts); \
3347 if ((sc)->sc_route4.ro_rt != NULL) \
3348 RTFREE((sc)->sc_route4.ro_rt); \
3349 timeout_set(&(sc)->sc_timer, syn_cache_reaper, (sc)); \
3350 timeout_add(&(sc)->sc_timer, 0); \
3351 } while (0)
3352
3353 struct pool syn_cache_pool;
3354
3355
3356
3357
3358
3359 #define SYN_CACHE_TIMER_ARM(sc) \
3360 do { \
3361 TCPT_RANGESET((sc)->sc_rxtcur, \
3362 TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN, \
3363 TCPTV_REXMTMAX); \
3364 if (!timeout_initialized(&(sc)->sc_timer)) \
3365 timeout_set(&(sc)->sc_timer, syn_cache_timer, (sc)); \
3366 timeout_add(&(sc)->sc_timer, (sc)->sc_rxtcur * (hz / PR_SLOWHZ)); \
3367 } while (0)
3368
3369 #define SYN_CACHE_TIMESTAMP(sc) tcp_now + (sc)->sc_modulate
3370
3371 void
3372 syn_cache_init()
3373 {
3374 int i;
3375
3376
3377 for (i = 0; i < tcp_syn_cache_size; i++)
3378 TAILQ_INIT(&tcp_syn_cache[i].sch_bucket);
3379
3380
3381 pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
3382 "synpl", NULL);
3383 }
3384
3385 void
3386 syn_cache_insert(sc, tp)
3387 struct syn_cache *sc;
3388 struct tcpcb *tp;
3389 {
3390 struct syn_cache_head *scp;
3391 struct syn_cache *sc2;
3392 int s;
3393
3394
3395
3396
3397
3398 if (syn_cache_count == 0) {
3399 syn_hash1 = arc4random();
3400 syn_hash2 = arc4random();
3401 }
3402
3403 SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
3404 sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
3405 scp = &tcp_syn_cache[sc->sc_bucketidx];
3406
3407
3408
3409
3410
3411 s = splsoftnet();
3412 if (scp->sch_length >= tcp_syn_bucket_limit) {
3413 tcpstat.tcps_sc_bucketoverflow++;
3414
3415
3416
3417
3418 sc2 = TAILQ_FIRST(&scp->sch_bucket);
3419 #ifdef DIAGNOSTIC
3420
3421
3422
3423
3424 if (sc2 == NULL)
3425 panic("syn_cache_insert: bucketoverflow: impossible");
3426 #endif
3427 SYN_CACHE_RM(sc2);
3428 SYN_CACHE_PUT(sc2);
3429 } else if (syn_cache_count >= tcp_syn_cache_limit) {
3430 struct syn_cache_head *scp2, *sce;
3431
3432 tcpstat.tcps_sc_overflowed++;
3433
3434
3435
3436
3437
3438
3439
3440
3441 scp2 = scp;
3442 if (TAILQ_EMPTY(&scp2->sch_bucket)) {
3443 sce = &tcp_syn_cache[tcp_syn_cache_size];
3444 for (++scp2; scp2 != scp; scp2++) {
3445 if (scp2 >= sce)
3446 scp2 = &tcp_syn_cache[0];
3447 if (! TAILQ_EMPTY(&scp2->sch_bucket))
3448 break;
3449 }
3450 #ifdef DIAGNOSTIC
3451
3452
3453
3454
3455 if (scp2 == scp)
3456 panic("syn_cache_insert: cacheoverflow: "
3457 "impossible");
3458 #endif
3459 }
3460 sc2 = TAILQ_FIRST(&scp2->sch_bucket);
3461 SYN_CACHE_RM(sc2);
3462 SYN_CACHE_PUT(sc2);
3463 }
3464
3465
3466
3467
3468 sc->sc_rxttot = 0;
3469 sc->sc_rxtshift = 0;
3470 SYN_CACHE_TIMER_ARM(sc);
3471
3472
3473 LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
3474
3475
3476 TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq);
3477 scp->sch_length++;
3478 syn_cache_count++;
3479
3480 tcpstat.tcps_sc_added++;
3481 splx(s);
3482 }
3483
3484
3485
3486
3487
3488
3489 void
3490 syn_cache_timer(void *arg)
3491 {
3492 struct syn_cache *sc = arg;
3493 int s;
3494
3495 s = splsoftnet();
3496 if (sc->sc_flags & SCF_DEAD) {
3497 splx(s);
3498 return;
3499 }
3500
3501 if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) {
3502
3503 goto dropit;
3504 }
3505
3506
3507
3508
3509
3510
3511 sc->sc_rxttot += sc->sc_rxtcur;
3512 if (sc->sc_rxttot >= tcptv_keep_init)
3513 goto dropit;
3514
3515 tcpstat.tcps_sc_retransmitted++;
3516 (void) syn_cache_respond(sc, NULL);
3517
3518
3519 sc->sc_rxtshift++;
3520 SYN_CACHE_TIMER_ARM(sc);
3521
3522 splx(s);
3523 return;
3524
3525 dropit:
3526 tcpstat.tcps_sc_timed_out++;
3527 SYN_CACHE_RM(sc);
3528 SYN_CACHE_PUT(sc);
3529 splx(s);
3530 }
3531
3532 void
3533 syn_cache_reaper(void *arg)
3534 {
3535 struct syn_cache *sc = arg;
3536 int s;
3537
3538 s = splsoftnet();
3539 pool_put(&syn_cache_pool, (sc));
3540 splx(s);
3541 return;
3542 }
3543
3544
3545
3546
3547
3548
3549 void
3550 syn_cache_cleanup(tp)
3551 struct tcpcb *tp;
3552 {
3553 struct syn_cache *sc, *nsc;
3554 int s;
3555
3556 s = splsoftnet();
3557
3558 for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
3559 nsc = LIST_NEXT(sc, sc_tpq);
3560
3561 #ifdef DIAGNOSTIC
3562 if (sc->sc_tp != tp)
3563 panic("invalid sc_tp in syn_cache_cleanup");
3564 #endif
3565 SYN_CACHE_RM(sc);
3566 SYN_CACHE_PUT(sc);
3567 }
3568
3569 LIST_INIT(&tp->t_sc);
3570
3571 splx(s);
3572 }
3573
3574
3575
3576
3577 struct syn_cache *
3578 syn_cache_lookup(src, dst, headp)
3579 struct sockaddr *src;
3580 struct sockaddr *dst;
3581 struct syn_cache_head **headp;
3582 {
3583 struct syn_cache *sc;
3584 struct syn_cache_head *scp;
3585 u_int32_t hash;
3586 int s;
3587
3588 SYN_HASHALL(hash, src, dst);
3589
3590 scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
3591 *headp = scp;
3592 s = splsoftnet();
3593 for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL;
3594 sc = TAILQ_NEXT(sc, sc_bucketq)) {
3595 if (sc->sc_hash != hash)
3596 continue;
3597 if (!bcmp(&sc->sc_src, src, src->sa_len) &&
3598 !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
3599 splx(s);
3600 return (sc);
3601 }
3602 }
3603 splx(s);
3604 return (NULL);
3605 }
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630 struct socket *
3631 syn_cache_get(src, dst, th, hlen, tlen, so, m)
3632 struct sockaddr *src;
3633 struct sockaddr *dst;
3634 struct tcphdr *th;
3635 unsigned int hlen, tlen;
3636 struct socket *so;
3637 struct mbuf *m;
3638 {
3639 struct syn_cache *sc;
3640 struct syn_cache_head *scp;
3641 struct inpcb *inp = NULL;
3642 struct tcpcb *tp = 0;
3643 struct mbuf *am;
3644 int s;
3645 struct socket *oso;
3646
3647 s = splsoftnet();
3648 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3649 splx(s);
3650 return (NULL);
3651 }
3652
3653
3654
3655
3656
3657 if ((th->th_ack != sc->sc_iss + 1) ||
3658 SEQ_LEQ(th->th_seq, sc->sc_irs) ||
3659 SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
3660 (void) syn_cache_respond(sc, m);
3661 splx(s);
3662 return ((struct socket *)(-1));
3663 }
3664
3665
3666 SYN_CACHE_RM(sc);
3667 splx(s);
3668
3669
3670
3671
3672
3673
3674
3675 oso = so;
3676 so = sonewconn(so, SS_ISCONNECTED);
3677 if (so == NULL)
3678 goto resetandabort;
3679
3680 inp = sotoinpcb(oso);
3681 #ifdef IPSEC
3682
3683
3684
3685
3686
3687 {
3688 struct inpcb *newinp = (struct inpcb *)so->so_pcb;
3689 bcopy(inp->inp_seclevel, newinp->inp_seclevel,
3690 sizeof(inp->inp_seclevel));
3691 newinp->inp_secrequire = inp->inp_secrequire;
3692 if (inp->inp_ipo != NULL) {
3693 newinp->inp_ipo = inp->inp_ipo;
3694 inp->inp_ipo->ipo_ref_count++;
3695 }
3696 if (inp->inp_ipsec_remotecred != NULL) {
3697 newinp->inp_ipsec_remotecred = inp->inp_ipsec_remotecred;
3698 inp->inp_ipsec_remotecred->ref_count++;
3699 }
3700 if (inp->inp_ipsec_remoteauth != NULL) {
3701 newinp->inp_ipsec_remoteauth
3702 = inp->inp_ipsec_remoteauth;
3703 inp->inp_ipsec_remoteauth->ref_count++;
3704 }
3705 }
3706 #endif
3707 #ifdef INET6
3708
3709
3710
3711
3712 {
3713 int flags = inp->inp_flags;
3714 struct inpcb *oldinpcb = inp;
3715
3716 inp = (struct inpcb *)so->so_pcb;
3717 inp->inp_flags |= (flags & INP_IPV6);
3718 if ((inp->inp_flags & INP_IPV6) != 0) {
3719 inp->inp_ipv6.ip6_hlim =
3720 oldinpcb->inp_ipv6.ip6_hlim;
3721 }
3722 }
3723 #else
3724 inp = (struct inpcb *)so->so_pcb;
3725 #endif
3726
3727 inp->inp_lport = th->th_dport;
3728 switch (src->sa_family) {
3729 #ifdef INET6
3730 case AF_INET6:
3731 inp->inp_laddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
3732 break;
3733 #endif
3734 case AF_INET:
3735
3736 inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
3737 inp->inp_options = ip_srcroute();
3738 if (inp->inp_options == NULL) {
3739 inp->inp_options = sc->sc_ipopts;
3740 sc->sc_ipopts = NULL;
3741 }
3742 break;
3743 }
3744 in_pcbrehash(inp);
3745
3746
3747
3748
3749 if (src->sa_family == AF_INET)
3750 inp->inp_route = sc->sc_route4;
3751 #ifdef INET6
3752 else
3753 inp->inp_route6 = sc->sc_route6;
3754 #endif
3755 sc->sc_route4.ro_rt = NULL;
3756
3757 am = m_get(M_DONTWAIT, MT_SONAME);
3758 if (am == NULL)
3759 goto resetandabort;
3760 am->m_len = src->sa_len;
3761 bcopy(src, mtod(am, caddr_t), src->sa_len);
3762
3763 switch (src->sa_family) {
3764 case AF_INET:
3765
3766 if (inp->inp_flags & INP_IPV6) {
3767 (void) m_free(am);
3768 goto resetandabort;
3769 }
3770 if (in_pcbconnect(inp, am)) {
3771 (void) m_free(am);
3772 goto resetandabort;
3773 }
3774 break;
3775 #ifdef INET6
3776 case AF_INET6:
3777 if (in6_pcbconnect(inp, am)) {
3778 (void) m_free(am);
3779 goto resetandabort;
3780 }
3781 break;
3782 #endif
3783 }
3784 (void) m_free(am);
3785
3786 tp = intotcpcb(inp);
3787 tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
3788 if (sc->sc_request_r_scale != 15) {
3789 tp->requested_s_scale = sc->sc_requested_s_scale;
3790 tp->request_r_scale = sc->sc_request_r_scale;
3791 tp->snd_scale = sc->sc_requested_s_scale;
3792 tp->rcv_scale = sc->sc_request_r_scale;
3793 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
3794 }
3795 if (sc->sc_flags & SCF_TIMESTAMP)
3796 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
3797
3798 tp->t_template = tcp_template(tp);
3799 if (tp->t_template == 0) {
3800 tp = tcp_drop(tp, ENOBUFS);
3801 so = NULL;
3802 m_freem(m);
3803 goto abort;
3804 }
3805 #ifdef TCP_SACK
3806 tp->sack_enable = sc->sc_flags & SCF_SACK_PERMIT;
3807 #endif
3808
3809 tp->ts_modulate = sc->sc_modulate;
3810 tp->iss = sc->sc_iss;
3811 tp->irs = sc->sc_irs;
3812 tcp_sendseqinit(tp);
3813 #if defined (TCP_SACK) || defined(TCP_ECN)
3814 tp->snd_last = tp->snd_una;
3815 #endif
3816 #if defined(TCP_SACK) && defined(TCP_FACK)
3817 tp->snd_fack = tp->snd_una;
3818 tp->retran_data = 0;
3819 tp->snd_awnd = 0;
3820 #endif
3821 #ifdef TCP_ECN
3822 if (sc->sc_flags & SCF_ECN_PERMIT) {
3823 tp->t_flags |= TF_ECN_PERMIT;
3824 tcpstat.tcps_ecn_accepts++;
3825 }
3826 #endif
3827 #ifdef TCP_SACK
3828 if (sc->sc_flags & SCF_SACK_PERMIT)
3829 tp->t_flags |= TF_SACK_PERMIT;
3830 #endif
3831 #ifdef TCP_SIGNATURE
3832 if (sc->sc_flags & SCF_SIGNATURE)
3833 tp->t_flags |= TF_SIGNATURE;
3834 #endif
3835 tcp_rcvseqinit(tp);
3836 tp->t_state = TCPS_SYN_RECEIVED;
3837 tp->t_rcvtime = tcp_now;
3838 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init);
3839 tcpstat.tcps_accepts++;
3840
3841 tcp_mss(tp, sc->sc_peermaxseg);
3842 if (sc->sc_peermaxseg)
3843 tcp_mss_update(tp);
3844
3845 if (sc->sc_rxtshift > 0)
3846 tp->snd_cwnd = tp->t_maxseg;
3847 tp->snd_wl1 = sc->sc_irs;
3848 tp->rcv_up = sc->sc_irs + 1;
3849
3850
3851
3852
3853
3854 tp->snd_up = tp->snd_una;
3855 tp->snd_max = tp->snd_nxt = tp->iss+1;
3856 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
3857 if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
3858 tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
3859 tp->last_ack_sent = tp->rcv_nxt;
3860
3861 tcpstat.tcps_sc_completed++;
3862 SYN_CACHE_PUT(sc);
3863 return (so);
3864
3865 resetandabort:
3866 tcp_respond(NULL, mtod(m, caddr_t), m, (tcp_seq)0, th->th_ack, TH_RST);
3867 abort:
3868 if (so != NULL)
3869 (void) soabort(so);
3870 SYN_CACHE_PUT(sc);
3871 tcpstat.tcps_sc_aborted++;
3872 return ((struct socket *)(-1));
3873 }
3874
3875
3876
3877
3878
3879
3880
3881 void
3882 syn_cache_reset(src, dst, th)
3883 struct sockaddr *src;
3884 struct sockaddr *dst;
3885 struct tcphdr *th;
3886 {
3887 struct syn_cache *sc;
3888 struct syn_cache_head *scp;
3889 int s = splsoftnet();
3890
3891 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3892 splx(s);
3893 return;
3894 }
3895 if (SEQ_LT(th->th_seq, sc->sc_irs) ||
3896 SEQ_GT(th->th_seq, sc->sc_irs+1)) {
3897 splx(s);
3898 return;
3899 }
3900 SYN_CACHE_RM(sc);
3901 splx(s);
3902 tcpstat.tcps_sc_reset++;
3903 SYN_CACHE_PUT(sc);
3904 }
3905
3906 void
3907 syn_cache_unreach(src, dst, th)
3908 struct sockaddr *src;
3909 struct sockaddr *dst;
3910 struct tcphdr *th;
3911 {
3912 struct syn_cache *sc;
3913 struct syn_cache_head *scp;
3914 int s;
3915
3916 s = splsoftnet();
3917 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3918 splx(s);
3919 return;
3920 }
3921
3922 if (ntohl (th->th_seq) != sc->sc_iss) {
3923 splx(s);
3924 return;
3925 }
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
3936 sc->sc_flags |= SCF_UNREACH;
3937 splx(s);
3938 return;
3939 }
3940
3941 SYN_CACHE_RM(sc);
3942 splx(s);
3943 tcpstat.tcps_sc_unreach++;
3944 SYN_CACHE_PUT(sc);
3945 }
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961 int
3962 syn_cache_add(src, dst, th, iphlen, so, m, optp, optlen, oi, issp)
3963 struct sockaddr *src;
3964 struct sockaddr *dst;
3965 struct tcphdr *th;
3966 unsigned int iphlen;
3967 struct socket *so;
3968 struct mbuf *m;
3969 u_char *optp;
3970 int optlen;
3971 struct tcp_opt_info *oi;
3972 tcp_seq *issp;
3973 {
3974 struct tcpcb tb, *tp;
3975 long win;
3976 struct syn_cache *sc;
3977 struct syn_cache_head *scp;
3978 struct mbuf *ipopts;
3979
3980 tp = sototcpcb(so);
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991 win = sbspace(&so->so_rcv);
3992 if (win > TCP_MAXWIN)
3993 win = TCP_MAXWIN;
3994
3995 #ifdef TCP_SIGNATURE
3996 if (optp || (tp->t_flags & TF_SIGNATURE)) {
3997 #else
3998 if (optp) {
3999 #endif
4000 tb.pf = tp->pf;
4001 #ifdef TCP_SACK
4002 tb.sack_enable = tp->sack_enable;
4003 #endif
4004 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
4005 #ifdef TCP_SIGNATURE
4006 if (tp->t_flags & TF_SIGNATURE)
4007 tb.t_flags |= TF_SIGNATURE;
4008 #endif
4009 tb.t_state = TCPS_LISTEN;
4010 if (tcp_dooptions(&tb, optp, optlen, th, m, iphlen, oi))
4011 return (0);
4012 } else
4013 tb.t_flags = 0;
4014
4015 switch (src->sa_family) {
4016 #ifdef INET
4017 case AF_INET:
4018
4019
4020
4021 ipopts = ip_srcroute();
4022 break;
4023 #endif
4024 default:
4025 ipopts = NULL;
4026 }
4027
4028
4029
4030
4031
4032
4033 if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
4034 tcpstat.tcps_sc_dupesyn++;
4035 if (ipopts) {
4036
4037
4038
4039
4040 if (sc->sc_ipopts)
4041 (void) m_free(sc->sc_ipopts);
4042 sc->sc_ipopts = ipopts;
4043 }
4044 sc->sc_timestamp = tb.ts_recent;
4045 if (syn_cache_respond(sc, m) == 0) {
4046 tcpstat.tcps_sndacks++;
4047 tcpstat.tcps_sndtotal++;
4048 }
4049 return (1);
4050 }
4051
4052 sc = pool_get(&syn_cache_pool, PR_NOWAIT);
4053 if (sc == NULL) {
4054 if (ipopts)
4055 (void) m_free(ipopts);
4056 return (0);
4057 }
4058
4059
4060
4061
4062
4063 bzero(sc, sizeof(struct syn_cache));
4064 bzero(&sc->sc_timer, sizeof(sc->sc_timer));
4065 bcopy(src, &sc->sc_src, src->sa_len);
4066 bcopy(dst, &sc->sc_dst, dst->sa_len);
4067 sc->sc_flags = 0;
4068 sc->sc_ipopts = ipopts;
4069 sc->sc_irs = th->th_seq;
4070
4071 #ifdef TCP_COMPAT_42
4072 tcp_iss += TCP_ISSINCR/2;
4073 sc->sc_iss = tcp_iss;
4074 #else
4075 sc->sc_iss = issp ? *issp : arc4random();
4076 #endif
4077 sc->sc_peermaxseg = oi->maxseg;
4078 sc->sc_ourmaxseg = tcp_mss_adv(m->m_flags & M_PKTHDR ?
4079 m->m_pkthdr.rcvif : NULL, sc->sc_src.sa.sa_family);
4080 sc->sc_win = win;
4081 sc->sc_timestamp = tb.ts_recent;
4082 if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) ==
4083 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) {
4084 sc->sc_flags |= SCF_TIMESTAMP;
4085 sc->sc_modulate = arc4random();
4086 }
4087 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
4088 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
4089 sc->sc_requested_s_scale = tb.requested_s_scale;
4090 sc->sc_request_r_scale = 0;
4091 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
4092 TCP_MAXWIN << sc->sc_request_r_scale <
4093 so->so_rcv.sb_hiwat)
4094 sc->sc_request_r_scale++;
4095 } else {
4096 sc->sc_requested_s_scale = 15;
4097 sc->sc_request_r_scale = 15;
4098 }
4099 #ifdef TCP_ECN
4100
4101
4102
4103 if (tcp_do_ecn &&
4104 (th->th_flags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR))
4105 sc->sc_flags |= SCF_ECN_PERMIT;
4106 #endif
4107 #ifdef TCP_SACK
4108
4109
4110
4111
4112 if (tb.sack_enable && (tb.t_flags & TF_SACK_PERMIT))
4113 sc->sc_flags |= SCF_SACK_PERMIT;
4114 #endif
4115 #ifdef TCP_SIGNATURE
4116 if (tb.t_flags & TF_SIGNATURE)
4117 sc->sc_flags |= SCF_SIGNATURE;
4118 #endif
4119 sc->sc_tp = tp;
4120 if (syn_cache_respond(sc, m) == 0) {
4121 syn_cache_insert(sc, tp);
4122 tcpstat.tcps_sndacks++;
4123 tcpstat.tcps_sndtotal++;
4124 } else {
4125 SYN_CACHE_PUT(sc);
4126 tcpstat.tcps_sc_dropped++;
4127 }
4128 return (1);
4129 }
4130
4131 int
4132 syn_cache_respond(sc, m)
4133 struct syn_cache *sc;
4134 struct mbuf *m;
4135 {
4136 struct route *ro;
4137 u_int8_t *optp;
4138 int optlen, error;
4139 u_int16_t tlen;
4140 struct ip *ip = NULL;
4141 #ifdef INET6
4142 struct ip6_hdr *ip6 = NULL;
4143 #endif
4144 struct tcphdr *th;
4145 u_int hlen;
4146 struct inpcb *inp;
4147
4148 switch (sc->sc_src.sa.sa_family) {
4149 case AF_INET:
4150 hlen = sizeof(struct ip);
4151 ro = &sc->sc_route4;
4152 break;
4153 #ifdef INET6
4154 case AF_INET6:
4155 hlen = sizeof(struct ip6_hdr);
4156 ro = (struct route *)&sc->sc_route6;
4157 break;
4158 #endif
4159 default:
4160 if (m)
4161 m_freem(m);
4162 return (EAFNOSUPPORT);
4163 }
4164
4165
4166 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
4167 #ifdef TCP_SACK
4168 ((sc->sc_flags & SCF_SACK_PERMIT) ? 4 : 0) +
4169 #endif
4170 #ifdef TCP_SIGNATURE
4171 ((sc->sc_flags & SCF_SIGNATURE) ? TCPOLEN_SIGLEN : 0) +
4172 #endif
4173 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
4174
4175 tlen = hlen + sizeof(struct tcphdr) + optlen;
4176
4177
4178
4179
4180 if (m)
4181 m_freem(m);
4182 #ifdef DIAGNOSTIC
4183 if (max_linkhdr + tlen > MCLBYTES)
4184 return (ENOBUFS);
4185 #endif
4186 MGETHDR(m, M_DONTWAIT, MT_DATA);
4187 if (m && max_linkhdr + tlen > MHLEN) {
4188 MCLGET(m, M_DONTWAIT);
4189 if ((m->m_flags & M_EXT) == 0) {
4190 m_freem(m);
4191 m = NULL;
4192 }
4193 }
4194 if (m == NULL)
4195 return (ENOBUFS);
4196
4197
4198 m->m_data += max_linkhdr;
4199 m->m_len = m->m_pkthdr.len = tlen;
4200 m->m_pkthdr.rcvif = NULL;
4201 memset(mtod(m, u_char *), 0, tlen);
4202
4203 switch (sc->sc_src.sa.sa_family) {
4204 case AF_INET:
4205 ip = mtod(m, struct ip *);
4206 ip->ip_dst = sc->sc_src.sin.sin_addr;
4207 ip->ip_src = sc->sc_dst.sin.sin_addr;
4208 ip->ip_p = IPPROTO_TCP;
4209 th = (struct tcphdr *)(ip + 1);
4210 th->th_dport = sc->sc_src.sin.sin_port;
4211 th->th_sport = sc->sc_dst.sin.sin_port;
4212 break;
4213 #ifdef INET6
4214 case AF_INET6:
4215 ip6 = mtod(m, struct ip6_hdr *);
4216 ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
4217 ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
4218 ip6->ip6_nxt = IPPROTO_TCP;
4219
4220 th = (struct tcphdr *)(ip6 + 1);
4221 th->th_dport = sc->sc_src.sin6.sin6_port;
4222 th->th_sport = sc->sc_dst.sin6.sin6_port;
4223 break;
4224 #endif
4225 default:
4226 th = NULL;
4227 }
4228
4229 th->th_seq = htonl(sc->sc_iss);
4230 th->th_ack = htonl(sc->sc_irs + 1);
4231 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
4232 th->th_flags = TH_SYN|TH_ACK;
4233 #ifdef TCP_ECN
4234
4235 if (tcp_do_ecn && (sc->sc_flags & SCF_ECN_PERMIT))
4236 th->th_flags |= TH_ECE;
4237 #endif
4238 th->th_win = htons(sc->sc_win);
4239
4240
4241
4242
4243 optp = (u_int8_t *)(th + 1);
4244 *optp++ = TCPOPT_MAXSEG;
4245 *optp++ = 4;
4246 *optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
4247 *optp++ = sc->sc_ourmaxseg & 0xff;
4248
4249 #ifdef TCP_SACK
4250
4251 if (sc->sc_flags & SCF_SACK_PERMIT) {
4252 *((u_int32_t *)optp) = htonl(TCPOPT_SACK_PERMIT_HDR);
4253 optp += 4;
4254 }
4255 #endif
4256
4257 if (sc->sc_request_r_scale != 15) {
4258 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
4259 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
4260 sc->sc_request_r_scale);
4261 optp += 4;
4262 }
4263
4264 if (sc->sc_flags & SCF_TIMESTAMP) {
4265 u_int32_t *lp = (u_int32_t *)(optp);
4266
4267 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
4268 *lp++ = htonl(SYN_CACHE_TIMESTAMP(sc));
4269 *lp = htonl(sc->sc_timestamp);
4270 optp += TCPOLEN_TSTAMP_APPA;
4271 }
4272
4273 #ifdef TCP_SIGNATURE
4274 if (sc->sc_flags & SCF_SIGNATURE) {
4275 union sockaddr_union src, dst;
4276 struct tdb *tdb;
4277
4278 bzero(&src, sizeof(union sockaddr_union));
4279 bzero(&dst, sizeof(union sockaddr_union));
4280 src.sa.sa_len = sc->sc_src.sa.sa_len;
4281 src.sa.sa_family = sc->sc_src.sa.sa_family;
4282 dst.sa.sa_len = sc->sc_dst.sa.sa_len;
4283 dst.sa.sa_family = sc->sc_dst.sa.sa_family;
4284
4285 switch (sc->sc_src.sa.sa_family) {
4286 case 0:
4287 #ifdef INET
4288 case AF_INET:
4289 src.sin.sin_addr = mtod(m, struct ip *)->ip_src;
4290 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst;
4291 break;
4292 #endif
4293 #ifdef INET6
4294 case AF_INET6:
4295 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src;
4296 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst;
4297 break;
4298 #endif
4299 }
4300
4301 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP);
4302 if (tdb == NULL) {
4303 if (m)
4304 m_freem(m);
4305 return (EPERM);
4306 }
4307
4308
4309 *(optp++) = TCPOPT_SIGNATURE;
4310 *(optp++) = TCPOLEN_SIGNATURE;
4311
4312 if (tcp_signature(tdb, sc->sc_src.sa.sa_family, m, th,
4313 hlen, 0, optp) < 0) {
4314 if (m)
4315 m_freem(m);
4316 return (EINVAL);
4317 }
4318 optp += 16;
4319
4320
4321
4322
4323 *optp++ = TCPOPT_NOP;
4324 *optp++ = TCPOPT_EOL;
4325 }
4326 #endif
4327
4328
4329 switch (sc->sc_src.sa.sa_family) {
4330 case AF_INET:
4331 ip->ip_len = htons(tlen - hlen);
4332 th->th_sum = 0;
4333 th->th_sum = in_cksum(m, tlen);
4334 break;
4335 #ifdef INET6
4336 case AF_INET6:
4337 ip6->ip6_plen = htons(tlen - hlen);
4338 th->th_sum = 0;
4339 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
4340 break;
4341 #endif
4342 }
4343
4344
4345 inp = sc->sc_tp ? sc->sc_tp->t_inpcb : NULL;
4346
4347
4348
4349
4350
4351 switch (sc->sc_src.sa.sa_family) {
4352 #ifdef INET
4353 case AF_INET:
4354 ip->ip_len = htons(tlen);
4355 ip->ip_ttl = inp ? inp->inp_ip.ip_ttl : ip_defttl;
4356
4357 break;
4358 #endif
4359 #ifdef INET6
4360 case AF_INET6:
4361 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
4362 ip6->ip6_vfc |= IPV6_VERSION;
4363 ip6->ip6_plen = htons(tlen - hlen);
4364
4365
4366 break;
4367 #endif
4368 }
4369
4370 switch (sc->sc_src.sa.sa_family) {
4371 #ifdef INET
4372 case AF_INET:
4373 error = ip_output(m, sc->sc_ipopts, ro,
4374 (ip_mtudisc ? IP_MTUDISC : 0),
4375 (struct ip_moptions *)NULL, inp);
4376 break;
4377 #endif
4378 #ifdef INET6
4379 case AF_INET6:
4380 ip6->ip6_hlim = in6_selecthlim(NULL,
4381 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
4382
4383 error = ip6_output(m, NULL , (struct route_in6 *)ro, 0,
4384 (struct ip6_moptions *)0, NULL, NULL);
4385 break;
4386 #endif
4387 default:
4388 error = EAFNOSUPPORT;
4389 break;
4390 }
4391 return (error);
4392 }