2 * Copyright (c) 2002, 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2002, 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2002, 2003, 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
84 * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.38 2003/05/21 04:46:41 cjc Exp $
85 * $DragonFly: src/sys/netinet/tcp_input.c,v 1.46 2004/12/21 02:54:15 hsu Exp $
88 #include "opt_ipfw.h" /* for ipfw_fwd */
89 #include "opt_inet6.h"
90 #include "opt_ipsec.h"
91 #include "opt_tcpdebug.h"
92 #include "opt_tcp_input.h"
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/kernel.h>
97 #include <sys/sysctl.h>
98 #include <sys/malloc.h>
100 #include <sys/proc.h> /* for proc0 declaration */
101 #include <sys/protosw.h>
102 #include <sys/socket.h>
103 #include <sys/socketvar.h>
104 #include <sys/syslog.h>
105 #include <sys/in_cksum.h>
107 #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */
108 #include <machine/stdarg.h>
111 #include <net/route.h>
113 #include <netinet/in.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip.h>
116 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
117 #include <netinet/in_var.h>
118 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
119 #include <netinet/in_pcb.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/ip6.h>
122 #include <netinet/icmp6.h>
123 #include <netinet6/nd6.h>
124 #include <netinet6/ip6_var.h>
125 #include <netinet6/in6_pcb.h>
126 #include <netinet/tcp.h>
127 #include <netinet/tcp_fsm.h>
128 #include <netinet/tcp_seq.h>
129 #include <netinet/tcp_timer.h>
130 #include <netinet/tcp_var.h>
131 #include <netinet6/tcp6_var.h>
132 #include <netinet/tcpip.h>
135 #include <netinet/tcp_debug.h>
137 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
138 struct tcphdr tcp_savetcp
;
142 #include <netproto/ipsec/ipsec.h>
143 #include <netproto/ipsec/ipsec6.h>
147 #include <netinet6/ipsec.h>
148 #include <netinet6/ipsec6.h>
149 #include <netproto/key/key.h>
152 MALLOC_DEFINE(M_TSEGQ
, "tseg_qent", "TCP segment queue entry");
155 static int log_in_vain
= 0;
156 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
157 &log_in_vain
, 0, "Log all incoming TCP connections");
159 static int blackhole
= 0;
160 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
, CTLFLAG_RW
,
161 &blackhole
, 0, "Do not send RST when dropping refused connections");
163 int tcp_delack_enabled
= 1;
164 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
, CTLFLAG_RW
,
165 &tcp_delack_enabled
, 0,
166 "Delay ACK to try and piggyback it onto a data packet");
168 #ifdef TCP_DROP_SYNFIN
169 static int drop_synfin
= 0;
170 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, drop_synfin
, CTLFLAG_RW
,
171 &drop_synfin
, 0, "Drop TCP packets with SYN+FIN set");
174 static int tcp_do_limitedtransmit
= 1;
175 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, limitedtransmit
, CTLFLAG_RW
,
176 &tcp_do_limitedtransmit
, 0, "Enable RFC 3042 (Limited Transmit)");
178 static int tcp_do_early_retransmit
= 1;
179 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, earlyretransmit
, CTLFLAG_RW
,
180 &tcp_do_early_retransmit
, 0, "Early retransmit");
182 static int tcp_do_rfc3390
= 1;
183 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3390
, CTLFLAG_RW
,
185 "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
187 static int tcp_do_eifel_detect
= 1;
188 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, eifel
, CTLFLAG_RW
,
189 &tcp_do_eifel_detect
, 0, "Eifel detection algorithm (RFC 3522)");
192 * Define as tunable for easy testing with SACK on and off.
193 * Warning: do not change setting in the middle of an existing active TCP flow,
194 * else strange things might happen to that flow.
197 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack
, CTLFLAG_RW
,
198 &tcp_do_sack
, 0, "Enable SACK Algorithms");
200 int tcp_do_smartsack
= 1;
201 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, smartsack
, CTLFLAG_RW
,
202 &tcp_do_smartsack
, 0, "Enable Smart SACK Algorithms");
204 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
, 0,
205 "TCP Segment Reassembly Queue");
207 int tcp_reass_maxseg
= 0;
208 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, maxsegments
, CTLFLAG_RD
,
209 &tcp_reass_maxseg
, 0,
210 "Global maximum number of TCP Segments in Reassembly Queue");
212 int tcp_reass_qsize
= 0;
213 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, cursegments
, CTLFLAG_RD
,
215 "Global number of TCP Segments currently in Reassembly Queue");
217 static int tcp_reass_overflows
= 0;
218 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
, CTLFLAG_RD
,
219 &tcp_reass_overflows
, 0,
220 "Global number of TCP Segment Reassembly Queue Overflows");
222 static void tcp_dooptions(struct tcpopt
*, u_char
*, int, boolean_t
);
223 static void tcp_pulloutofband(struct socket
*,
224 struct tcphdr
*, struct mbuf
*, int);
225 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *,
227 static void tcp_xmit_timer(struct tcpcb
*, int);
228 static void tcp_newreno_partial_ack(struct tcpcb
*, struct tcphdr
*, int);
229 static void tcp_sack_rexmt(struct tcpcb
*, struct tcphdr
*);
231 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
233 #define ND6_HINT(tp) \
235 if ((tp) && (tp)->t_inpcb && \
236 ((tp)->t_inpcb->inp_vflag & INP_IPV6) && \
237 (tp)->t_inpcb->in6p_route.ro_rt) \
238 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
245 * Indicate whether this ack should be delayed. We can delay the ack if
246 * - delayed acks are enabled and
247 * - there is no delayed ack timer in progress and
248 * - our last ack wasn't a 0-sized window. We never want to delay
249 * the ack that opens up a 0-sized window.
251 #define DELAY_ACK(tp) \
252 (tcp_delack_enabled && !callout_pending(tp->tt_delack) && \
253 !(tp->t_flags & TF_RXWIN0SENT))
255 #define acceptable_window_update(tp, th, tiwin) \
256 (SEQ_LT(tp->snd_wl1, th->th_seq) || \
257 (tp->snd_wl1 == th->th_seq && \
258 (SEQ_LT(tp->snd_wl2, th->th_ack) || \
259 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))
262 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
)
265 struct tseg_qent
*p
= NULL
;
266 struct tseg_qent
*te
;
267 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
271 * Call with th == NULL after become established to
272 * force pre-ESTABLISHED data up to user socket.
278 * Limit the number of segments in the reassembly queue to prevent
279 * holding on to too many segments (and thus running out of mbufs).
280 * Make sure to let the missing segment through which caused this
281 * queue. Always keep one global queue entry spare to be able to
282 * process the missing segment.
284 if (th
->th_seq
!= tp
->rcv_nxt
&&
285 tcp_reass_qsize
+ 1 >= tcp_reass_maxseg
) {
286 tcp_reass_overflows
++;
287 tcpstat
.tcps_rcvmemdrop
++;
289 /* no SACK block to report */
290 tp
->reportblk
.rblk_start
= tp
->reportblk
.rblk_end
;
294 /* Allocate a new queue entry. */
295 MALLOC(te
, struct tseg_qent
*, sizeof(struct tseg_qent
), M_TSEGQ
,
296 M_INTWAIT
| M_NULLOK
);
298 tcpstat
.tcps_rcvmemdrop
++;
300 /* no SACK block to report */
301 tp
->reportblk
.rblk_start
= tp
->reportblk
.rblk_end
;
307 * Find a segment which begins after this one does.
309 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
310 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
316 * If there is a preceding segment, it may provide some of
317 * our data already. If so, drop the data from the incoming
318 * segment. If it provides all of our data, drop us.
323 /* conversion to int (in i) handles seq wraparound */
324 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
325 if (i
> 0) { /* overlaps preceding segment */
326 tp
->t_flags
|= (TF_DUPSEG
| TF_ENCLOSESEG
);
327 /* enclosing block starts w/ preceding segment */
328 tp
->encloseblk
.rblk_start
= p
->tqe_th
->th_seq
;
330 /* preceding encloses incoming segment */
331 tp
->encloseblk
.rblk_end
= p
->tqe_th
->th_seq
+
333 tcpstat
.tcps_rcvduppack
++;
334 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
339 * Try to present any queued data
340 * at the left window edge to the user.
341 * This is needed after the 3-WHS
344 goto present
; /* ??? */
349 /* incoming segment end is enclosing block end */
350 tp
->encloseblk
.rblk_end
= th
->th_seq
+ *tlenp
+
351 ((th
->th_flags
& TH_FIN
) != 0);
352 /* trim end of reported D-SACK block */
353 tp
->reportblk
.rblk_end
= th
->th_seq
;
356 tcpstat
.tcps_rcvoopack
++;
357 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
360 * While we overlap succeeding segments trim them or,
361 * if they are completely covered, dequeue them.
364 int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
365 tcp_seq qend
= q
->tqe_th
->th_seq
+ q
->tqe_len
;
366 struct tseg_qent
*nq
;
370 if (!(tp
->t_flags
& TF_DUPSEG
)) { /* first time through */
371 tp
->t_flags
|= (TF_DUPSEG
| TF_ENCLOSESEG
);
372 tp
->encloseblk
= tp
->reportblk
;
373 /* report trailing duplicate D-SACK segment */
374 tp
->reportblk
.rblk_start
= q
->tqe_th
->th_seq
;
376 if ((tp
->t_flags
& TF_ENCLOSESEG
) &&
377 SEQ_GT(qend
, tp
->encloseblk
.rblk_end
)) {
378 /* extend enclosing block if one exists */
379 tp
->encloseblk
.rblk_end
= qend
;
381 if (i
< q
->tqe_len
) {
382 q
->tqe_th
->th_seq
+= i
;
388 nq
= LIST_NEXT(q
, tqe_q
);
389 LIST_REMOVE(q
, tqe_q
);
396 /* Insert the new segment queue entry into place. */
399 te
->tqe_len
= *tlenp
;
401 /* check if can coalesce with following segment */
402 if (q
!= NULL
&& (th
->th_seq
+ *tlenp
== q
->tqe_th
->th_seq
)) {
403 tcp_seq tend
= te
->tqe_th
->th_seq
+ te
->tqe_len
;
405 te
->tqe_len
+= q
->tqe_len
;
406 if (q
->tqe_th
->th_flags
& TH_FIN
)
407 te
->tqe_th
->th_flags
|= TH_FIN
;
408 m_cat(te
->tqe_m
, q
->tqe_m
);
409 tp
->encloseblk
.rblk_end
= tend
;
411 * When not reporting a duplicate segment, use
412 * the larger enclosing block as the SACK block.
414 if (!(tp
->t_flags
& TF_DUPSEG
))
415 tp
->reportblk
.rblk_end
= tend
;
416 LIST_REMOVE(q
, tqe_q
);
422 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
424 /* check if can coalesce with preceding segment */
425 if (p
->tqe_th
->th_seq
+ p
->tqe_len
== th
->th_seq
) {
426 p
->tqe_len
+= te
->tqe_len
;
427 m_cat(p
->tqe_m
, te
->tqe_m
);
428 tp
->encloseblk
.rblk_start
= p
->tqe_th
->th_seq
;
430 * When not reporting a duplicate segment, use
431 * the larger enclosing block as the SACK block.
433 if (!(tp
->t_flags
& TF_DUPSEG
))
434 tp
->reportblk
.rblk_start
= p
->tqe_th
->th_seq
;
438 LIST_INSERT_AFTER(p
, te
, tqe_q
);
443 * Present data to user, advancing rcv_nxt through
444 * completed sequence space.
446 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
448 q
= LIST_FIRST(&tp
->t_segq
);
449 if (q
== NULL
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
)
451 tp
->rcv_nxt
+= q
->tqe_len
;
452 if (!(tp
->t_flags
& TF_DUPSEG
)) {
453 /* no SACK block to report since ACK advanced */
454 tp
->reportblk
.rblk_start
= tp
->reportblk
.rblk_end
;
456 /* no enclosing block to report since ACK advanced */
457 tp
->t_flags
&= ~TF_ENCLOSESEG
;
458 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
459 LIST_REMOVE(q
, tqe_q
);
460 KASSERT(LIST_EMPTY(&tp
->t_segq
) ||
461 LIST_FIRST(&tp
->t_segq
)->tqe_th
->th_seq
!= tp
->rcv_nxt
,
462 ("segment not coalesced"));
463 if (so
->so_state
& SS_CANTRCVMORE
)
466 sbappendstream(&so
->so_rcv
, q
->tqe_m
);
475 * TCP input routine, follows pages 65-76 of the
476 * protocol specification dated September, 1981 very closely.
480 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
482 struct mbuf
*m
= *mp
;
483 struct in6_ifaddr
*ia6
;
485 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), IPPROTO_DONE
);
488 * draft-itojun-ipv6-tcp-to-anycast
489 * better place to put this in?
491 ia6
= ip6_getdstifaddr(m
);
492 if (ia6
&& (ia6
->ia6_flags
& IN6_IFF_ANYCAST
)) {
495 ip6
= mtod(m
, struct ip6_hdr
*);
496 icmp6_error(m
, ICMP6_DST_UNREACH
, ICMP6_DST_UNREACH_ADDR
,
497 offsetof(struct ip6_hdr
, ip6_dst
));
498 return (IPPROTO_DONE
);
501 tcp_input(m
, *offp
, proto
);
502 return (IPPROTO_DONE
);
507 tcp_input(struct mbuf
*m
, ...)
512 struct ip
*ip
= NULL
;
514 struct inpcb
*inp
= NULL
;
519 struct tcpcb
*tp
= NULL
;
521 struct socket
*so
= 0;
523 boolean_t ourfinisacked
, needoutput
= FALSE
;
526 struct tcpopt to
; /* options in this segment */
527 struct rmxp_tao
*taop
; /* pointer to our TAO cache entry */
528 struct rmxp_tao tao_noncached
; /* in case there's no cached entry */
529 struct sockaddr_in
*next_hop
= NULL
;
530 int rstreason
; /* For badport_bandlim accounting purposes */
532 struct ip6_hdr
*ip6
= NULL
;
536 const boolean_t isipv6
= FALSE
;
543 off0
= __va_arg(ap
, int);
544 proto
= __va_arg(ap
, int);
547 tcpstat
.tcps_rcvtotal
++;
549 /* Grab info from and strip MT_TAG mbufs prepended to the chain. */
550 while (m
->m_type
== MT_TAG
) {
551 if (m
->_m_tag_id
== PACKET_TAG_IPFORWARD
)
552 next_hop
= (struct sockaddr_in
*)m
->m_hdr
.mh_data
;
557 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? TRUE
: FALSE
;
561 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
562 ip6
= mtod(m
, struct ip6_hdr
*);
563 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
564 if (in6_cksum(m
, IPPROTO_TCP
, off0
, tlen
)) {
565 tcpstat
.tcps_rcvbadsum
++;
568 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off0
);
571 * Be proactive about unspecified IPv6 address in source.
572 * As we use all-zero to indicate unbounded/unconnected pcb,
573 * unspecified IPv6 address can be used to confuse us.
575 * Note that packets with unspecified IPv6 destination is
576 * already dropped in ip6_input.
578 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
584 * Get IP and TCP header together in first mbuf.
585 * Note: IP leaves IP header in first mbuf.
587 if (off0
> sizeof(struct ip
)) {
589 off0
= sizeof(struct ip
);
591 /* already checked and pulled up in ip_demux() */
592 KASSERT(m
->m_len
>= sizeof(struct tcpiphdr
),
593 ("TCP header not in one mbuf: m->m_len %d", m
->m_len
));
594 ip
= mtod(m
, struct ip
*);
595 ipov
= (struct ipovly
*)ip
;
596 th
= (struct tcphdr
*)((caddr_t
)ip
+ off0
);
599 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
600 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
601 th
->th_sum
= m
->m_pkthdr
.csum_data
;
603 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
605 htonl(m
->m_pkthdr
.csum_data
+
608 th
->th_sum
^= 0xffff;
611 * Checksum extended TCP header and data.
613 len
= sizeof(struct ip
) + tlen
;
614 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
615 ipov
->ih_len
= (u_short
)tlen
;
616 ipov
->ih_len
= htons(ipov
->ih_len
);
617 th
->th_sum
= in_cksum(m
, len
);
620 tcpstat
.tcps_rcvbadsum
++;
624 /* Re-initialization for later version check */
625 ip
->ip_v
= IPVERSION
;
630 * Check that TCP offset makes sense,
631 * pull out TCP options and adjust length. XXX
633 off
= th
->th_off
<< 2;
634 /* already checked and pulled up in ip_demux() */
635 KASSERT(off
>= sizeof(struct tcphdr
) && off
<= tlen
,
636 ("bad TCP data offset %d (tlen %d)", off
, tlen
));
637 tlen
-= off
; /* tlen is used instead of ti->ti_len */
638 if (off
> sizeof(struct tcphdr
)) {
640 IP6_EXTHDR_CHECK(m
, off0
, off
, );
641 ip6
= mtod(m
, struct ip6_hdr
*);
642 th
= (struct tcphdr
*)((caddr_t
)ip6
+ off0
);
644 /* already pulled up in ip_demux() */
645 KASSERT(m
->m_len
>= sizeof(struct ip
) + off
,
646 ("TCP header and options not in one mbuf: "
647 "m_len %d, off %d", m
->m_len
, off
));
649 optlen
= off
- sizeof(struct tcphdr
);
650 optp
= (u_char
*)(th
+ 1);
652 thflags
= th
->th_flags
;
654 #ifdef TCP_DROP_SYNFIN
656 * If the drop_synfin option is enabled, drop all packets with
657 * both the SYN and FIN bits set. This prevents e.g. nmap from
658 * identifying the TCP/IP stack.
660 * This is a violation of the TCP specification.
662 if (drop_synfin
&& (thflags
& (TH_SYN
| TH_FIN
)) == (TH_SYN
| TH_FIN
))
667 * Convert TCP protocol specific fields to host format.
669 th
->th_seq
= ntohl(th
->th_seq
);
670 th
->th_ack
= ntohl(th
->th_ack
);
671 th
->th_win
= ntohs(th
->th_win
);
672 th
->th_urp
= ntohs(th
->th_urp
);
675 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
676 * until after ip6_savecontrol() is called and before other functions
677 * which don't want those proto headers.
678 * Because ip6_savecontrol() is going to parse the mbuf to
679 * search for data to be passed up to user-land, it wants mbuf
680 * parameters to be unchanged.
681 * XXX: the call of ip6_savecontrol() has been obsoleted based on
682 * latest version of the advanced API (20020110).
684 drop_hdrlen
= off0
+ off
;
687 * Locate pcb for segment.
690 /* IPFIREWALL_FORWARD section */
691 if (next_hop
!= NULL
&& !isipv6
) { /* IPv6 support is not there yet */
693 * Transparently forwarded. Pretend to be the destination.
694 * already got one like this?
696 cpu
= mycpu
->gd_cpuid
;
697 inp
= in_pcblookup_hash(&tcbinfo
[cpu
],
698 ip
->ip_src
, th
->th_sport
,
699 ip
->ip_dst
, th
->th_dport
,
700 0, m
->m_pkthdr
.rcvif
);
703 * It's new. Try to find the ambushing socket.
707 * The rest of the ipfw code stores the port in
709 * (The IP address is still in network order.)
711 in_port_t dport
= next_hop
->sin_port
?
712 htons(next_hop
->sin_port
) :
715 cpu
= tcp_addrcpu(ip
->ip_src
.s_addr
, th
->th_sport
,
716 next_hop
->sin_addr
.s_addr
, dport
);
717 inp
= in_pcblookup_hash(&tcbinfo
[cpu
],
718 ip
->ip_src
, th
->th_sport
,
719 next_hop
->sin_addr
, dport
,
720 1, m
->m_pkthdr
.rcvif
);
724 inp
= in6_pcblookup_hash(&tcbinfo
[0],
725 &ip6
->ip6_src
, th
->th_sport
,
726 &ip6
->ip6_dst
, th
->th_dport
,
727 1, m
->m_pkthdr
.rcvif
);
729 cpu
= mycpu
->gd_cpuid
;
730 inp
= in_pcblookup_hash(&tcbinfo
[cpu
],
731 ip
->ip_src
, th
->th_sport
,
732 ip
->ip_dst
, th
->th_dport
,
733 1, m
->m_pkthdr
.rcvif
);
738 * If the state is CLOSED (i.e., TCB does not exist) then
739 * all data in the incoming segment is discarded.
740 * If the TCB exists but is in CLOSED state, it is embryonic,
741 * but should either do a listen or a connect soon.
746 char dbuf
[INET6_ADDRSTRLEN
+2], sbuf
[INET6_ADDRSTRLEN
+2];
748 char dbuf
[4 * sizeof "123"], sbuf
[4 * sizeof "123"];
752 strcat(dbuf
, ip6_sprintf(&ip6
->ip6_dst
));
755 strcat(sbuf
, ip6_sprintf(&ip6
->ip6_src
));
758 strcpy(dbuf
, inet_ntoa(ip
->ip_dst
));
759 strcpy(sbuf
, inet_ntoa(ip
->ip_src
));
761 switch (log_in_vain
) {
763 if (!(thflags
& TH_SYN
))
767 "Connection attempt to TCP %s:%d "
768 "from %s:%d flags:0x%02x\n",
769 dbuf
, ntohs(th
->th_dport
), sbuf
,
770 ntohs(th
->th_sport
), thflags
);
779 if (thflags
& TH_SYN
)
788 rstreason
= BANDLIM_RST_CLOSEDPORT
;
794 if (ipsec6_in_reject_so(m
, inp
->inp_socket
)) {
795 ipsec6stat
.in_polvio
++;
799 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
800 ipsecstat
.in_polvio
++;
807 if (ipsec6_in_reject(m
, inp
))
810 if (ipsec4_in_reject(m
, inp
))
817 rstreason
= BANDLIM_RST_CLOSEDPORT
;
820 if (tp
->t_state
<= TCPS_CLOSED
)
823 /* Unscale the window into a 32-bit value. */
824 if (!(thflags
& TH_SYN
))
825 tiwin
= th
->th_win
<< tp
->snd_scale
;
829 so
= inp
->inp_socket
;
832 if (so
->so_options
& SO_DEBUG
) {
833 ostate
= tp
->t_state
;
835 bcopy(ip6
, tcp_saveipgen
, sizeof(*ip6
));
837 bcopy(ip
, tcp_saveipgen
, sizeof(*ip
));
842 bzero(&to
, sizeof(to
));
844 if (so
->so_options
& SO_ACCEPTCONN
) {
845 struct in_conninfo inc
;
848 inc
.inc_isipv6
= (isipv6
== TRUE
);
851 inc
.inc6_faddr
= ip6
->ip6_src
;
852 inc
.inc6_laddr
= ip6
->ip6_dst
;
853 inc
.inc6_route
.ro_rt
= NULL
; /* XXX */
855 inc
.inc_faddr
= ip
->ip_src
;
856 inc
.inc_laddr
= ip
->ip_dst
;
857 inc
.inc_route
.ro_rt
= NULL
; /* XXX */
859 inc
.inc_fport
= th
->th_sport
;
860 inc
.inc_lport
= th
->th_dport
;
863 * If the state is LISTEN then ignore segment if it contains
864 * a RST. If the segment contains an ACK then it is bad and
865 * send a RST. If it does not contain a SYN then it is not
866 * interesting; drop it.
868 * If the state is SYN_RECEIVED (syncache) and seg contains
869 * an ACK, but not for our SYN/ACK, send a RST. If the seg
870 * contains a RST, check the sequence number to see if it
871 * is a valid reset segment.
873 if ((thflags
& (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
) {
874 if ((thflags
& (TH_RST
| TH_ACK
| TH_SYN
)) == TH_ACK
) {
875 if (!syncache_expand(&inc
, th
, &so
, m
)) {
877 * No syncache entry, or ACK was not
878 * for our SYN/ACK. Send a RST.
880 tcpstat
.tcps_badsyn
++;
881 rstreason
= BANDLIM_RST_OPENPORT
;
886 * Could not complete 3-way handshake,
887 * connection is being closed down, and
888 * syncache will free mbuf.
892 * Socket is created in state SYN_RECEIVED.
893 * Continue processing segment.
898 * This is what would have happened in
899 * tcp_output() when the SYN,ACK was sent.
901 tp
->snd_up
= tp
->snd_una
;
902 tp
->snd_max
= tp
->snd_nxt
= tp
->iss
+ 1;
903 tp
->last_ack_sent
= tp
->rcv_nxt
;
905 * XXX possible bug - it doesn't appear that tp->snd_wnd is unscaled
906 * until the _second_ ACK is received:
907 * rcv SYN (set wscale opts) --> send SYN/ACK, set snd_wnd = window.
908 * rcv ACK, calculate tiwin --> process SYN_RECEIVED, determine wscale,
909 * move to ESTAB, set snd_wnd to tiwin.
911 tp
->snd_wnd
= tiwin
; /* unscaled */
914 if (thflags
& TH_RST
) {
915 syncache_chkrst(&inc
, th
);
918 if (thflags
& TH_ACK
) {
919 syncache_badack(&inc
);
920 tcpstat
.tcps_badsyn
++;
921 rstreason
= BANDLIM_RST_OPENPORT
;
928 * Segment's flags are (SYN) or (SYN | FIN).
932 * If deprecated address is forbidden,
933 * we do not accept SYN to deprecated interface
934 * address to prevent any new inbound connection from
935 * getting established.
936 * When we do not accept SYN, we send a TCP RST,
937 * with deprecated source address (instead of dropping
938 * it). We compromise it as it is much better for peer
939 * to send a RST, and RST will be the final packet
942 * If we do not forbid deprecated addresses, we accept
943 * the SYN packet. RFC2462 does not suggest dropping
945 * If we decipher RFC2462 5.5.4, it says like this:
946 * 1. use of deprecated addr with existing
947 * communication is okay - "SHOULD continue to be
949 * 2. use of it with new communication:
950 * (2a) "SHOULD NOT be used if alternate address
951 * with sufficient scope is available"
952 * (2b) nothing mentioned otherwise.
953 * Here we fall into (2b) case as we have no choice in
954 * our source address selection - we must obey the peer.
956 * The wording in RFC2462 is confusing, and there are
957 * multiple description text for deprecated address
958 * handling - worse, they are not exactly the same.
959 * I believe 5.5.4 is the best one, so we follow 5.5.4.
961 if (isipv6
&& !ip6_use_deprecated
) {
962 struct in6_ifaddr
*ia6
;
964 if ((ia6
= ip6_getdstifaddr(m
)) &&
965 (ia6
->ia6_flags
& IN6_IFF_DEPRECATED
)) {
967 rstreason
= BANDLIM_RST_OPENPORT
;
973 * If it is from this socket, drop it, it must be forged.
974 * Don't bother responding if the destination was a broadcast.
976 if (th
->th_dport
== th
->th_sport
) {
978 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
982 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
987 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
989 * Note that it is quite possible to receive unicast
990 * link-layer packets with a broadcast IP address. Use
991 * in_broadcast() to find them.
993 if (m
->m_flags
& (M_BCAST
| M_MCAST
))
996 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
997 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
1000 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
1001 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
1002 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
1003 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
1007 * SYN appears to be valid; create compressed TCP state
1008 * for syncache, or perform t/tcp connection.
1010 if (so
->so_qlen
<= so
->so_qlimit
) {
1011 tcp_dooptions(&to
, optp
, optlen
, TRUE
);
1012 if (!syncache_add(&inc
, &to
, th
, &so
, m
))
1016 * Entry added to syncache, mbuf used to
1017 * send SYN,ACK packet.
1021 * Segment passed TAO tests.
1023 inp
= sotoinpcb(so
);
1024 tp
= intotcpcb(inp
);
1025 tp
->snd_wnd
= tiwin
;
1026 tp
->t_starttime
= ticks
;
1027 tp
->t_state
= TCPS_ESTABLISHED
;
1030 * If there is a FIN, or if there is data and the
1031 * connection is local, then delay SYN,ACK(SYN) in
1032 * the hope of piggy-backing it on a response
1033 * segment. Otherwise must send ACK now in case
1034 * the other side is slow starting.
1036 if (DELAY_ACK(tp
) &&
1037 ((thflags
& TH_FIN
) ||
1039 ((isipv6
&& in6_localaddr(&inp
->in6p_faddr
)) ||
1040 (!isipv6
&& in_localaddr(inp
->inp_faddr
)))))) {
1041 callout_reset(tp
->tt_delack
, tcp_delacktime
,
1042 tcp_timer_delack
, tp
);
1043 tp
->t_flags
|= TF_NEEDSYN
;
1045 tp
->t_flags
|= (TF_ACKNOW
| TF_NEEDSYN
);
1047 tcpstat
.tcps_connects
++;
1055 /* should not happen - syncache should pick up these connections */
1056 KASSERT(tp
->t_state
!= TCPS_LISTEN
, ("tcp_input: TCPS_LISTEN state"));
1059 * Segment received on connection.
1060 * Reset idle time and keep-alive timer.
1062 tp
->t_rcvtime
= ticks
;
1063 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
1064 callout_reset(tp
->tt_keep
, tcp_keepidle
, tcp_timer_keep
, tp
);
1068 * XXX this is tradtitional behavior, may need to be cleaned up.
1070 tcp_dooptions(&to
, optp
, optlen
, (thflags
& TH_SYN
) != 0);
1071 if (thflags
& TH_SYN
) {
1072 if (to
.to_flags
& TOF_SCALE
) {
1073 tp
->t_flags
|= TF_RCVD_SCALE
;
1074 tp
->requested_s_scale
= to
.to_requested_s_scale
;
1076 if (to
.to_flags
& TOF_TS
) {
1077 tp
->t_flags
|= TF_RCVD_TSTMP
;
1078 tp
->ts_recent
= to
.to_tsval
;
1079 tp
->ts_recent_age
= ticks
;
1081 if (to
.to_flags
& (TOF_CC
| TOF_CCNEW
))
1082 tp
->t_flags
|= TF_RCVD_CC
;
1083 if (to
.to_flags
& TOF_MSS
)
1084 tcp_mss(tp
, to
.to_mss
);
1086 * Only set the TF_SACK_PERMITTED per-connection flag
1087 * if we got a SACK_PERMITTED option from the other side
1088 * and the global tcp_do_sack variable is true.
1090 if (tcp_do_sack
&& (to
.to_flags
& TOF_SACK_PERMITTED
))
1091 tp
->t_flags
|= TF_SACK_PERMITTED
;
1095 * Header prediction: check for the two common cases
1096 * of a uni-directional data xfer. If the packet has
1097 * no control flags, is in-sequence, the window didn't
1098 * change and we're not retransmitting, it's a
1099 * candidate. If the length is zero and the ack moved
1100 * forward, we're the sender side of the xfer. Just
1101 * free the data acked & wake any higher level process
1102 * that was blocked waiting for space. If the length
1103 * is non-zero and the ack didn't move, we're the
1104 * receiver side. If we're getting packets in-order
1105 * (the reassembly queue is empty), add the data to
1106 * the socket buffer and note that we need a delayed ack.
1107 * Make sure that the hidden state-flags are also off.
1108 * Since we check for TCPS_ESTABLISHED above, it can only
1111 if (tp
->t_state
== TCPS_ESTABLISHED
&&
1112 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
)) == TH_ACK
&&
1113 !(tp
->t_flags
& (TF_NEEDSYN
| TF_NEEDFIN
)) &&
1114 (!(to
.to_flags
& TOF_TS
) ||
1115 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
1117 * Using the CC option is compulsory if once started:
1118 * the segment is OK if no T/TCP was negotiated or
1119 * if the segment has a CC option equal to CCrecv
1121 ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) != (TF_REQ_CC
|TF_RCVD_CC
) ||
1122 ((to
.to_flags
& TOF_CC
) && to
.to_cc
== tp
->cc_recv
)) &&
1123 th
->th_seq
== tp
->rcv_nxt
&&
1124 tp
->snd_nxt
== tp
->snd_max
) {
1127 * If last ACK falls within this segment's sequence numbers,
1128 * record the timestamp.
1129 * NOTE that the test is modified according to the latest
1130 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1132 if ((to
.to_flags
& TOF_TS
) &&
1133 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
1134 tp
->ts_recent_age
= ticks
;
1135 tp
->ts_recent
= to
.to_tsval
;
1139 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
1140 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
1141 tp
->snd_cwnd
>= tp
->snd_wnd
&&
1142 !IN_FASTRECOVERY(tp
)) {
1144 * this is a pure ack for outstanding data.
1146 ++tcpstat
.tcps_predack
;
1148 * "bad retransmit" recovery
1150 * If Eifel detection applies, then
1151 * it is deterministic, so use it
1152 * unconditionally over the old heuristic.
1153 * Otherwise, fall back to the old heuristic.
1155 if (tcp_do_eifel_detect
&&
1156 (to
.to_flags
& TOF_TS
) && to
.to_tsecr
&&
1157 (tp
->t_flags
& TF_FIRSTACCACK
)) {
1158 /* Eifel detection applicable. */
1159 if (to
.to_tsecr
< tp
->t_rexmtTS
) {
1160 tcp_revert_congestion_state(tp
);
1161 ++tcpstat
.tcps_eifeldetected
;
1163 } else if (tp
->t_rxtshift
== 1 &&
1164 ticks
< tp
->t_badrxtwin
) {
1165 tcp_revert_congestion_state(tp
);
1166 ++tcpstat
.tcps_rttdetected
;
1168 tp
->t_flags
&= ~(TF_FIRSTACCACK
|
1169 TF_FASTREXMT
| TF_EARLYREXMT
);
1171 * Recalculate the retransmit timer / rtt.
1173 * Some machines (certain windows boxes)
1174 * send broken timestamp replies during the
1175 * SYN+ACK phase, ignore timestamps of 0.
1177 if ((to
.to_flags
& TOF_TS
) && to
.to_tsecr
) {
1179 ticks
- to
.to_tsecr
+ 1);
1180 } else if (tp
->t_rtttime
&&
1181 SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
1183 ticks
- tp
->t_rtttime
);
1185 tcp_xmit_bandwidth_limit(tp
, th
->th_ack
);
1186 acked
= th
->th_ack
- tp
->snd_una
;
1187 tcpstat
.tcps_rcvackpack
++;
1188 tcpstat
.tcps_rcvackbyte
+= acked
;
1189 sbdrop(&so
->so_snd
, acked
);
1190 tp
->snd_recover
= th
->th_ack
- 1;
1191 tp
->snd_una
= th
->th_ack
;
1194 * Update window information.
1196 if (tiwin
!= tp
->snd_wnd
&&
1197 acceptable_window_update(tp
, th
, tiwin
)) {
1198 /* keep track of pure window updates */
1199 if (tp
->snd_wl2
== th
->th_ack
&&
1200 tiwin
> tp
->snd_wnd
)
1201 tcpstat
.tcps_rcvwinupd
++;
1202 tp
->snd_wnd
= tiwin
;
1203 tp
->snd_wl1
= th
->th_seq
;
1204 tp
->snd_wl2
= th
->th_ack
;
1205 if (tp
->snd_wnd
> tp
->max_sndwnd
)
1206 tp
->max_sndwnd
= tp
->snd_wnd
;
1209 ND6_HINT(tp
); /* some progress has been done */
1211 * If all outstanding data are acked, stop
1212 * retransmit timer, otherwise restart timer
1213 * using current (possibly backed-off) value.
1214 * If process is waiting for space,
1215 * wakeup/selwakeup/signal. If data
1216 * are ready to send, let tcp_output
1217 * decide between more output or persist.
1219 if (tp
->snd_una
== tp
->snd_max
)
1220 callout_stop(tp
->tt_rexmt
);
1221 else if (!callout_active(tp
->tt_persist
))
1222 callout_reset(tp
->tt_rexmt
,
1224 tcp_timer_rexmt
, tp
);
1227 if (so
->so_snd
.sb_cc
)
1231 } else if (tiwin
== tp
->snd_wnd
&&
1232 th
->th_ack
== tp
->snd_una
&&
1233 LIST_EMPTY(&tp
->t_segq
) &&
1234 tlen
<= sbspace(&so
->so_rcv
)) {
1236 * this is a pure, in-sequence data packet
1237 * with nothing on the reassembly queue and
1238 * we have enough buffer space to take it.
1240 ++tcpstat
.tcps_preddat
;
1241 tp
->rcv_nxt
+= tlen
;
1242 tcpstat
.tcps_rcvpack
++;
1243 tcpstat
.tcps_rcvbyte
+= tlen
;
1244 ND6_HINT(tp
); /* some progress has been done */
1246 * Add data to socket buffer.
1248 if (so
->so_state
& SS_CANTRCVMORE
) {
1251 m_adj(m
, drop_hdrlen
); /* delayed header drop */
1252 sbappendstream(&so
->so_rcv
, m
);
1257 * This code is responsible for most of the ACKs
1258 * the TCP stack sends back after receiving a data
1259 * packet. Note that the DELAY_ACK check fails if
1260 * the delack timer is already running, which results
1261 * in an ack being sent every other packet (which is
1264 if (DELAY_ACK(tp
)) {
1265 callout_reset(tp
->tt_delack
, tcp_delacktime
,
1266 tcp_timer_delack
, tp
);
1268 tp
->t_flags
|= TF_ACKNOW
;
1269 if (!(tp
->t_flags
& TF_ONOUTPUTQ
)) {
1270 tp
->t_flags
|= TF_ONOUTPUTQ
;
1271 tp
->tt_cpu
= mycpu
->gd_cpuid
;
1273 &tcpcbackq
[tp
->tt_cpu
],
1282 * Calculate amount of space in receive window,
1283 * and then do TCP input processing.
1284 * Receive window is amount of space in rcv queue,
1285 * but not less than advertised window.
1287 recvwin
= sbspace(&so
->so_rcv
);
1290 tp
->rcv_wnd
= imax(recvwin
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
1292 switch (tp
->t_state
) {
1294 * If the state is SYN_RECEIVED:
1295 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1297 case TCPS_SYN_RECEIVED
:
1298 if ((thflags
& TH_ACK
) &&
1299 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
1300 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1301 rstreason
= BANDLIM_RST_OPENPORT
;
1307 * If the state is SYN_SENT:
1308 * if seg contains an ACK, but not for our SYN, drop the input.
1309 * if seg contains a RST, then drop the connection.
1310 * if seg does not contain SYN, then drop it.
1311 * Otherwise this is an acceptable SYN segment
1312 * initialize tp->rcv_nxt and tp->irs
1313 * if seg contains ack then advance tp->snd_una
1314 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1315 * arrange for segment to be acked (eventually)
1316 * continue processing rest of data/controls, beginning with URG
1319 if ((taop
= tcp_gettaocache(&inp
->inp_inc
)) == NULL
) {
1320 taop
= &tao_noncached
;
1321 bzero(taop
, sizeof(*taop
));
1324 if ((thflags
& TH_ACK
) &&
1325 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
1326 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
1328 * If we have a cached CCsent for the remote host,
1329 * hence we haven't just crashed and restarted,
1330 * do not send a RST. This may be a retransmission
1331 * from the other side after our earlier ACK was lost.
1332 * Our new SYN, when it arrives, will serve as the
1335 if (taop
->tao_ccsent
!= 0)
1338 rstreason
= BANDLIM_UNLIMITED
;
1342 if (thflags
& TH_RST
) {
1343 if (thflags
& TH_ACK
)
1344 tp
= tcp_drop(tp
, ECONNREFUSED
);
1347 if (!(thflags
& TH_SYN
))
1349 tp
->snd_wnd
= th
->th_win
; /* initial send window */
1350 tp
->cc_recv
= to
.to_cc
; /* foreign CC */
1352 tp
->irs
= th
->th_seq
;
1354 if (thflags
& TH_ACK
) {
1356 * Our SYN was acked. If segment contains CC.ECHO
1357 * option, check it to make sure this segment really
1358 * matches our SYN. If not, just drop it as old
1359 * duplicate, but send an RST if we're still playing
1360 * by the old rules. If no CC.ECHO option, make sure
1361 * we don't get fooled into using T/TCP.
1363 if (to
.to_flags
& TOF_CCECHO
) {
1364 if (tp
->cc_send
!= to
.to_ccecho
) {
1365 if (taop
->tao_ccsent
!= 0)
1368 rstreason
= BANDLIM_UNLIMITED
;
1373 tp
->t_flags
&= ~TF_RCVD_CC
;
1374 tcpstat
.tcps_connects
++;
1376 /* Do window scaling on this connection? */
1377 if ((tp
->t_flags
& (TF_RCVD_SCALE
| TF_REQ_SCALE
)) ==
1378 (TF_RCVD_SCALE
| TF_REQ_SCALE
)) {
1379 tp
->snd_scale
= tp
->requested_s_scale
;
1380 tp
->rcv_scale
= tp
->request_r_scale
;
1382 /* Segment is acceptable, update cache if undefined. */
1383 if (taop
->tao_ccsent
== 0)
1384 taop
->tao_ccsent
= to
.to_ccecho
;
1386 tp
->rcv_adv
+= tp
->rcv_wnd
;
1387 tp
->snd_una
++; /* SYN is acked */
1388 callout_stop(tp
->tt_rexmt
);
1390 * If there's data, delay ACK; if there's also a FIN
1391 * ACKNOW will be turned on later.
1393 if (DELAY_ACK(tp
) && tlen
!= 0)
1394 callout_reset(tp
->tt_delack
, tcp_delacktime
,
1395 tcp_timer_delack
, tp
);
1397 tp
->t_flags
|= TF_ACKNOW
;
1399 * Received <SYN,ACK> in SYN_SENT[*] state.
1401 * SYN_SENT --> ESTABLISHED
1402 * SYN_SENT* --> FIN_WAIT_1
1404 tp
->t_starttime
= ticks
;
1405 if (tp
->t_flags
& TF_NEEDFIN
) {
1406 tp
->t_state
= TCPS_FIN_WAIT_1
;
1407 tp
->t_flags
&= ~TF_NEEDFIN
;
1410 tp
->t_state
= TCPS_ESTABLISHED
;
1411 callout_reset(tp
->tt_keep
, tcp_keepidle
,
1412 tcp_timer_keep
, tp
);
1416 * Received initial SYN in SYN-SENT[*] state =>
1417 * simultaneous open. If segment contains CC option
1418 * and there is a cached CC, apply TAO test.
1419 * If it succeeds, connection is * half-synchronized.
1420 * Otherwise, do 3-way handshake:
1421 * SYN-SENT -> SYN-RECEIVED
1422 * SYN-SENT* -> SYN-RECEIVED*
1423 * If there was no CC option, clear cached CC value.
1425 tp
->t_flags
|= TF_ACKNOW
;
1426 callout_stop(tp
->tt_rexmt
);
1427 if (to
.to_flags
& TOF_CC
) {
1428 if (taop
->tao_cc
!= 0 &&
1429 CC_GT(to
.to_cc
, taop
->tao_cc
)) {
1431 * update cache and make transition:
1432 * SYN-SENT -> ESTABLISHED*
1433 * SYN-SENT* -> FIN-WAIT-1*
1435 taop
->tao_cc
= to
.to_cc
;
1436 tp
->t_starttime
= ticks
;
1437 if (tp
->t_flags
& TF_NEEDFIN
) {
1438 tp
->t_state
= TCPS_FIN_WAIT_1
;
1439 tp
->t_flags
&= ~TF_NEEDFIN
;
1441 tp
->t_state
= TCPS_ESTABLISHED
;
1442 callout_reset(tp
->tt_keep
,
1447 tp
->t_flags
|= TF_NEEDSYN
;
1449 tp
->t_state
= TCPS_SYN_RECEIVED
;
1451 /* CC.NEW or no option => invalidate cache */
1453 tp
->t_state
= TCPS_SYN_RECEIVED
;
1459 * Advance th->th_seq to correspond to first data byte.
1460 * If data, trim to stay within window,
1461 * dropping FIN if necessary.
1464 if (tlen
> tp
->rcv_wnd
) {
1465 todrop
= tlen
- tp
->rcv_wnd
;
1469 tcpstat
.tcps_rcvpackafterwin
++;
1470 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1472 tp
->snd_wl1
= th
->th_seq
- 1;
1473 tp
->rcv_up
= th
->th_seq
;
1475 * Client side of transaction: already sent SYN and data.
1476 * If the remote host used T/TCP to validate the SYN,
1477 * our data will be ACK'd; if so, enter normal data segment
1478 * processing in the middle of step 5, ack processing.
1479 * Otherwise, goto step 6.
1481 if (thflags
& TH_ACK
)
1487 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1488 * if segment contains a SYN and CC [not CC.NEW] option:
1489 * if state == TIME_WAIT and connection duration > MSL,
1490 * drop packet and send RST;
1492 * if SEG.CC > CCrecv then is new SYN, and can implicitly
1493 * ack the FIN (and data) in retransmission queue.
1494 * Complete close and delete TCPCB. Then reprocess
1495 * segment, hoping to find new TCPCB in LISTEN state;
1497 * else must be old SYN; drop it.
1498 * else do normal processing.
1502 case TCPS_TIME_WAIT
:
1503 if ((thflags
& TH_SYN
) &&
1504 (to
.to_flags
& TOF_CC
) && tp
->cc_recv
!= 0) {
1505 if (tp
->t_state
== TCPS_TIME_WAIT
&&
1506 (ticks
- tp
->t_starttime
) > tcp_msl
) {
1507 rstreason
= BANDLIM_UNLIMITED
;
1510 if (CC_GT(to
.to_cc
, tp
->cc_recv
)) {
1517 break; /* continue normal processing */
1521 * States other than LISTEN or SYN_SENT.
1522 * First check the RST flag and sequence number since reset segments
1523 * are exempt from the timestamp and connection count tests. This
1524 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1525 * below which allowed reset segments in half the sequence space
1526 * to fall though and be processed (which gives forged reset
1527 * segments with a random sequence number a 50 percent chance of
1528 * killing a connection).
1529 * Then check timestamp, if present.
1530 * Then check the connection count, if present.
1531 * Then check that at least some bytes of segment are within
1532 * receive window. If segment begins before rcv_nxt,
1533 * drop leading data (and SYN); if nothing left, just ack.
1536 * If the RST bit is set, check the sequence number to see
1537 * if this is a valid reset segment.
1539 * In all states except SYN-SENT, all reset (RST) segments
1540 * are validated by checking their SEQ-fields. A reset is
1541 * valid if its sequence number is in the window.
1542 * Note: this does not take into account delayed ACKs, so
1543 * we should test against last_ack_sent instead of rcv_nxt.
1544 * The sequence number in the reset segment is normally an
1545 * echo of our outgoing acknowledgement numbers, but some hosts
1546 * send a reset with the sequence number at the rightmost edge
1547 * of our receive window, and we have to handle this case.
1548 * If we have multiple segments in flight, the intial reset
1549 * segment sequence numbers will be to the left of last_ack_sent,
1550 * but they will eventually catch up.
1551 * In any case, it never made sense to trim reset segments to
1552 * fit the receive window since RFC 1122 says:
1553 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1555 * A TCP SHOULD allow a received RST segment to include data.
1558 * It has been suggested that a RST segment could contain
1559 * ASCII text that encoded and explained the cause of the
1560 * RST. No standard has yet been established for such
1563 * If the reset segment passes the sequence number test examine
1565 * SYN_RECEIVED STATE:
1566 * If passive open, return to LISTEN state.
1567 * If active open, inform user that connection was refused.
1568 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1569 * Inform user that connection was reset, and close tcb.
1570 * CLOSING, LAST_ACK STATES:
1573 * Drop the segment - see Stevens, vol. 2, p. 964 and
1576 if (thflags
& TH_RST
) {
1577 if (SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
1578 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
1579 switch (tp
->t_state
) {
1581 case TCPS_SYN_RECEIVED
:
1582 so
->so_error
= ECONNREFUSED
;
1585 case TCPS_ESTABLISHED
:
1586 case TCPS_FIN_WAIT_1
:
1587 case TCPS_FIN_WAIT_2
:
1588 case TCPS_CLOSE_WAIT
:
1589 so
->so_error
= ECONNRESET
;
1591 tp
->t_state
= TCPS_CLOSED
;
1592 tcpstat
.tcps_drops
++;
1601 case TCPS_TIME_WAIT
:
1609 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1610 * and it's less than ts_recent, drop it.
1612 if ((to
.to_flags
& TOF_TS
) && tp
->ts_recent
!= 0 &&
1613 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
1615 /* Check to see if ts_recent is over 24 days old. */
1616 if ((int)(ticks
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
1618 * Invalidate ts_recent. If this segment updates
1619 * ts_recent, the age will be reset later and ts_recent
1620 * will get a valid value. If it does not, setting
1621 * ts_recent to zero will at least satisfy the
1622 * requirement that zero be placed in the timestamp
1623 * echo reply when ts_recent isn't valid. The
1624 * age isn't reset until we get a valid ts_recent
1625 * because we don't want out-of-order segments to be
1626 * dropped when ts_recent is old.
1630 tcpstat
.tcps_rcvduppack
++;
1631 tcpstat
.tcps_rcvdupbyte
+= tlen
;
1632 tcpstat
.tcps_pawsdrop
++;
1641 * If T/TCP was negotiated and the segment doesn't have CC,
1642 * or if its CC is wrong then drop the segment.
1643 * RST segments do not have to comply with this.
1645 if ((tp
->t_flags
& (TF_REQ_CC
|TF_RCVD_CC
)) == (TF_REQ_CC
|TF_RCVD_CC
) &&
1646 (!(to
.to_flags
& TOF_CC
) || tp
->cc_recv
!= to
.to_cc
))
1650 * In the SYN-RECEIVED state, validate that the packet belongs to
1651 * this connection before trimming the data to fit the receive
1652 * window. Check the sequence number versus IRS since we know
1653 * the sequence numbers haven't wrapped. This is a partial fix
1654 * for the "LAND" DoS attack.
1656 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
1657 rstreason
= BANDLIM_RST_OPENPORT
;
1661 todrop
= tp
->rcv_nxt
- th
->th_seq
;
1663 if (TCP_DO_SACK(tp
)) {
1664 /* Report duplicate segment at head of packet. */
1665 tp
->reportblk
.rblk_start
= th
->th_seq
;
1666 tp
->reportblk
.rblk_end
= th
->th_seq
+ tlen
;
1667 if (thflags
& TH_FIN
)
1668 ++tp
->reportblk
.rblk_end
;
1669 if (SEQ_GT(tp
->reportblk
.rblk_end
, tp
->rcv_nxt
))
1670 tp
->reportblk
.rblk_end
= tp
->rcv_nxt
;
1671 tp
->t_flags
|= (TF_DUPSEG
| TF_SACKLEFT
| TF_ACKNOW
);
1673 if (thflags
& TH_SYN
) {
1683 * Following if statement from Stevens, vol. 2, p. 960.
1685 if (todrop
> tlen
||
1686 (todrop
== tlen
&& !(thflags
& TH_FIN
))) {
1688 * Any valid FIN must be to the left of the window.
1689 * At this point the FIN must be a duplicate or out
1690 * of sequence; drop it.
1695 * Send an ACK to resynchronize and drop any data.
1696 * But keep on processing for RST or ACK.
1698 tp
->t_flags
|= TF_ACKNOW
;
1700 tcpstat
.tcps_rcvduppack
++;
1701 tcpstat
.tcps_rcvdupbyte
+= todrop
;
1703 tcpstat
.tcps_rcvpartduppack
++;
1704 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
1706 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
1707 th
->th_seq
+= todrop
;
1709 if (th
->th_urp
> todrop
)
1710 th
->th_urp
-= todrop
;
1718 * If new data are received on a connection after the
1719 * user processes are gone, then RST the other end.
1721 if ((so
->so_state
& SS_NOFDREF
) &&
1722 tp
->t_state
> TCPS_CLOSE_WAIT
&& tlen
) {
1724 tcpstat
.tcps_rcvafterclose
++;
1725 rstreason
= BANDLIM_UNLIMITED
;
1730 * If segment ends after window, drop trailing data
1731 * (and PUSH and FIN); if nothing left, just ACK.
1733 todrop
= (th
->th_seq
+ tlen
) - (tp
->rcv_nxt
+ tp
->rcv_wnd
);
1735 tcpstat
.tcps_rcvpackafterwin
++;
1736 if (todrop
>= tlen
) {
1737 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
1739 * If a new connection request is received
1740 * while in TIME_WAIT, drop the old connection
1741 * and start over if the sequence numbers
1742 * are above the previous ones.
1744 if (thflags
& TH_SYN
&&
1745 tp
->t_state
== TCPS_TIME_WAIT
&&
1746 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1751 * If window is closed can only take segments at
1752 * window edge, and have to drop data and PUSH from
1753 * incoming segments. Continue processing, but
1754 * remember to ack. Otherwise, drop segment
1757 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
1758 tp
->t_flags
|= TF_ACKNOW
;
1759 tcpstat
.tcps_rcvwinprobe
++;
1763 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
1766 thflags
&= ~(TH_PUSH
| TH_FIN
);
1770 * If last ACK falls within this segment's sequence numbers,
1771 * record its timestamp.
1772 * NOTE that the test is modified according to the latest
1773 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1775 if ((to
.to_flags
& TOF_TS
) && SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
1776 tp
->ts_recent_age
= ticks
;
1777 tp
->ts_recent
= to
.to_tsval
;
1781 * If a SYN is in the window, then this is an
1782 * error and we send an RST and drop the connection.
1784 if (thflags
& TH_SYN
) {
1785 tp
= tcp_drop(tp
, ECONNRESET
);
1786 rstreason
= BANDLIM_UNLIMITED
;
1791 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1792 * flag is on (half-synchronized state), then queue data for
1793 * later processing; else drop segment and return.
1795 if (!(thflags
& TH_ACK
)) {
1796 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
1797 (tp
->t_flags
& TF_NEEDSYN
))
1806 switch (tp
->t_state
) {
1808 * In SYN_RECEIVED state, the ACK acknowledges our SYN, so enter
1809 * ESTABLISHED state and continue processing.
1810 * The ACK was checked above.
1812 case TCPS_SYN_RECEIVED
:
1814 tcpstat
.tcps_connects
++;
1816 /* Do window scaling? */
1817 if ((tp
->t_flags
& (TF_RCVD_SCALE
| TF_REQ_SCALE
)) ==
1818 (TF_RCVD_SCALE
| TF_REQ_SCALE
)) {
1819 tp
->snd_scale
= tp
->requested_s_scale
;
1820 tp
->rcv_scale
= tp
->request_r_scale
;
1823 * Upon successful completion of 3-way handshake,
1824 * update cache.CC if it was undefined, pass any queued
1825 * data to the user, and advance state appropriately.
1827 if ((taop
= tcp_gettaocache(&inp
->inp_inc
)) != NULL
&&
1829 taop
->tao_cc
= tp
->cc_recv
;
1833 * SYN-RECEIVED -> ESTABLISHED
1834 * SYN-RECEIVED* -> FIN-WAIT-1
1836 tp
->t_starttime
= ticks
;
1837 if (tp
->t_flags
& TF_NEEDFIN
) {
1838 tp
->t_state
= TCPS_FIN_WAIT_1
;
1839 tp
->t_flags
&= ~TF_NEEDFIN
;
1841 tp
->t_state
= TCPS_ESTABLISHED
;
1842 callout_reset(tp
->tt_keep
, tcp_keepidle
,
1843 tcp_timer_keep
, tp
);
1846 * If segment contains data or ACK, will call tcp_reass()
1847 * later; if not, do so now to pass queued data to user.
1849 if (tlen
== 0 && !(thflags
& TH_FIN
))
1850 tcp_reass(tp
, NULL
, NULL
, NULL
);
1854 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1855 * ACKs. If the ack is in the range
1856 * tp->snd_una < th->th_ack <= tp->snd_max
1857 * then advance tp->snd_una to th->th_ack and drop
1858 * data from the retransmission queue. If this ACK reflects
1859 * more up to date window information we update our window information.
1861 case TCPS_ESTABLISHED
:
1862 case TCPS_FIN_WAIT_1
:
1863 case TCPS_FIN_WAIT_2
:
1864 case TCPS_CLOSE_WAIT
:
1867 case TCPS_TIME_WAIT
:
1869 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
1870 if (TCP_DO_SACK(tp
))
1871 tcp_sack_update_scoreboard(tp
, &to
);
1872 if (tlen
!= 0 || tiwin
!= tp
->snd_wnd
) {
1876 tcpstat
.tcps_rcvdupack
++;
1877 if (!callout_active(tp
->tt_rexmt
) ||
1878 th
->th_ack
!= tp
->snd_una
) {
1883 * We have outstanding data (other than
1884 * a window probe), this is a completely
1885 * duplicate ack (ie, window info didn't
1886 * change), the ack is the biggest we've
1887 * seen and we've seen exactly our rexmt
1888 * threshhold of them, so assume a packet
1889 * has been dropped and retransmit it.
1890 * Kludge snd_nxt & the congestion
1891 * window so we send only this one
1894 if (IN_FASTRECOVERY(tp
)) {
1895 if (TCP_DO_SACK(tp
)) {
1896 /* No artifical cwnd inflation. */
1897 tcp_sack_rexmt(tp
, th
);
1900 * Dup acks mean that packets
1901 * have left the network
1902 * (they're now cached at the
1903 * receiver) so bump cwnd by
1904 * the amount in the receiver
1905 * to keep a constant cwnd
1906 * packets in the network.
1908 tp
->snd_cwnd
+= tp
->t_maxseg
;
1911 } else if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
1914 } else if (++tp
->t_dupacks
== tcprexmtthresh
) {
1915 tcp_seq old_snd_nxt
;
1919 if (tcp_do_eifel_detect
&&
1920 (tp
->t_flags
& TF_RCVD_TSTMP
)) {
1921 tcp_save_congestion_state(tp
);
1922 tp
->t_flags
|= TF_FASTREXMT
;
1925 * We know we're losing at the current
1926 * window size, so do congestion avoidance:
1927 * set ssthresh to half the current window
1928 * and pull our congestion window back to the
1931 win
= min(tp
->snd_wnd
, tp
->snd_cwnd
) / 2 /
1935 tp
->snd_ssthresh
= win
* tp
->t_maxseg
;
1936 ENTER_FASTRECOVERY(tp
);
1937 tp
->snd_recover
= tp
->snd_max
;
1938 callout_stop(tp
->tt_rexmt
);
1940 old_snd_nxt
= tp
->snd_nxt
;
1941 tp
->snd_nxt
= th
->th_ack
;
1942 tp
->snd_cwnd
= tp
->t_maxseg
;
1944 ++tcpstat
.tcps_sndfastrexmit
;
1945 tp
->snd_cwnd
= tp
->snd_ssthresh
;
1946 tp
->rexmt_high
= tp
->snd_nxt
;
1947 if (SEQ_GT(old_snd_nxt
, tp
->snd_nxt
))
1948 tp
->snd_nxt
= old_snd_nxt
;
1949 KASSERT(tp
->snd_limited
<= 2,
1950 ("tp->snd_limited too big"));
1951 if (TCP_DO_SACK(tp
))
1952 tcp_sack_rexmt(tp
, th
);
1954 tp
->snd_cwnd
+= tp
->t_maxseg
*
1955 (tp
->t_dupacks
- tp
->snd_limited
);
1956 } else if (tcp_do_limitedtransmit
) {
1957 u_long oldcwnd
= tp
->snd_cwnd
;
1958 tcp_seq oldsndmax
= tp
->snd_max
;
1959 /* outstanding data */
1960 uint32_t ownd
= tp
->snd_max
- tp
->snd_una
;
1963 #define iceildiv(n, d) (((n)+(d)-1) / (d))
1965 KASSERT(tp
->t_dupacks
== 1 ||
1967 ("dupacks not 1 or 2"));
1968 if (tp
->t_dupacks
== 1)
1969 tp
->snd_limited
= 0;
1970 tp
->snd_cwnd
= ownd
+
1971 (tp
->t_dupacks
- tp
->snd_limited
) *
1974 tp
->snd_cwnd
= oldcwnd
;
1975 sent
= tp
->snd_max
- oldsndmax
;
1976 if (sent
> tp
->t_maxseg
) {
1977 KASSERT((tp
->t_dupacks
== 2 &&
1978 tp
->snd_limited
== 0) ||
1979 (sent
== tp
->t_maxseg
+ 1 &&
1980 tp
->t_flags
& TF_SENTFIN
),
1982 KASSERT(sent
<= tp
->t_maxseg
* 2,
1983 ("sent too many segments"));
1984 tp
->snd_limited
= 2;
1985 tcpstat
.tcps_sndlimited
+= 2;
1986 } else if (sent
> 0) {
1988 ++tcpstat
.tcps_sndlimited
;
1989 } else if (tcp_do_early_retransmit
&&
1990 (tcp_do_eifel_detect
&&
1991 (tp
->t_flags
& TF_RCVD_TSTMP
)) &&
1992 ownd
< 4 * tp
->t_maxseg
&&
1993 tp
->t_dupacks
+ 1 >=
1994 iceildiv(ownd
, tp
->t_maxseg
) &&
1995 (!TCP_DO_SACK(tp
) ||
1996 ownd
<= tp
->t_maxseg
||
1997 tcp_sack_has_sacked(&tp
->scb
,
1998 ownd
- tp
->t_maxseg
))) {
1999 ++tcpstat
.tcps_sndearlyrexmit
;
2000 tp
->t_flags
|= TF_EARLYREXMT
;
2001 goto fastretransmit
;
2007 KASSERT(SEQ_GT(th
->th_ack
, tp
->snd_una
), ("th_ack <= snd_una"));
2009 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
2011 * Detected optimistic ACK attack.
2012 * Force slow-start to de-synchronize attack.
2014 tp
->snd_cwnd
= tp
->t_maxseg
;
2016 tcpstat
.tcps_rcvacktoomuch
++;
2020 * If we reach this point, ACK is not a duplicate,
2021 * i.e., it ACKs something we sent.
2023 if (tp
->t_flags
& TF_NEEDSYN
) {
2025 * T/TCP: Connection was half-synchronized, and our
2026 * SYN has been ACK'd (so connection is now fully
2027 * synchronized). Go to non-starred state,
2028 * increment snd_una for ACK of SYN, and check if
2029 * we can do window scaling.
2031 tp
->t_flags
&= ~TF_NEEDSYN
;
2033 /* Do window scaling? */
2034 if ((tp
->t_flags
& (TF_RCVD_SCALE
| TF_REQ_SCALE
)) ==
2035 (TF_RCVD_SCALE
| TF_REQ_SCALE
)) {
2036 tp
->snd_scale
= tp
->requested_s_scale
;
2037 tp
->rcv_scale
= tp
->request_r_scale
;
2042 acked
= th
->th_ack
- tp
->snd_una
;
2043 tcpstat
.tcps_rcvackpack
++;
2044 tcpstat
.tcps_rcvackbyte
+= acked
;
2046 if (tcp_do_eifel_detect
&& acked
> 0 &&
2047 (to
.to_flags
& TOF_TS
) && (to
.to_tsecr
!= 0) &&
2048 (tp
->t_flags
& TF_FIRSTACCACK
)) {
2049 /* Eifel detection applicable. */
2050 if (to
.to_tsecr
< tp
->t_rexmtTS
) {
2051 ++tcpstat
.tcps_eifeldetected
;
2052 tcp_revert_congestion_state(tp
);
2053 if (tp
->t_rxtshift
== 1 &&
2054 ticks
>= tp
->t_badrxtwin
)
2055 ++tcpstat
.tcps_rttcantdetect
;
2057 } else if (tp
->t_rxtshift
== 1 && ticks
< tp
->t_badrxtwin
) {
2059 * If we just performed our first retransmit,
2060 * and the ACK arrives within our recovery window,
2061 * then it was a mistake to do the retransmit
2062 * in the first place. Recover our original cwnd
2063 * and ssthresh, and proceed to transmit where we
2066 tcp_revert_congestion_state(tp
);
2067 ++tcpstat
.tcps_rttdetected
;
2071 * If we have a timestamp reply, update smoothed
2072 * round trip time. If no timestamp is present but
2073 * transmit timer is running and timed sequence
2074 * number was acked, update smoothed round trip time.
2075 * Since we now have an rtt measurement, cancel the
2076 * timer backoff (cf., Phil Karn's retransmit alg.).
2077 * Recompute the initial retransmit timer.
2079 * Some machines (certain windows boxes) send broken
2080 * timestamp replies during the SYN+ACK phase, ignore
2083 if ((to
.to_flags
& TOF_TS
) && (to
.to_tsecr
!= 0))
2084 tcp_xmit_timer(tp
, ticks
- to
.to_tsecr
+ 1);
2085 else if (tp
->t_rtttime
&& SEQ_GT(th
->th_ack
, tp
->t_rtseq
))
2086 tcp_xmit_timer(tp
, ticks
- tp
->t_rtttime
);
2087 tcp_xmit_bandwidth_limit(tp
, th
->th_ack
);
2090 * If no data (only SYN) was ACK'd,
2091 * skip rest of ACK processing.
2096 /* Stop looking for an acceptable ACK since one was received. */
2097 tp
->t_flags
&= ~(TF_FIRSTACCACK
| TF_FASTREXMT
| TF_EARLYREXMT
);
2099 if (acked
> so
->so_snd
.sb_cc
) {
2100 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
2101 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
2102 ourfinisacked
= TRUE
;
2104 sbdrop(&so
->so_snd
, acked
);
2105 tp
->snd_wnd
-= acked
;
2106 ourfinisacked
= FALSE
;
2111 * Update window information.
2112 * Don't look at window if no ACK:
2113 * TAC's send garbage on first SYN.
2115 if (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
2116 (tp
->snd_wl1
== th
->th_seq
&&
2117 (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
2118 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)))) {
2119 /* keep track of pure window updates */
2120 if (tlen
== 0 && tp
->snd_wl2
== th
->th_ack
&&
2121 tiwin
> tp
->snd_wnd
)
2122 tcpstat
.tcps_rcvwinupd
++;
2123 tp
->snd_wnd
= tiwin
;
2124 tp
->snd_wl1
= th
->th_seq
;
2125 tp
->snd_wl2
= th
->th_ack
;
2126 if (tp
->snd_wnd
> tp
->max_sndwnd
)
2127 tp
->max_sndwnd
= tp
->snd_wnd
;
2131 tp
->snd_una
= th
->th_ack
;
2132 if (TCP_DO_SACK(tp
))
2133 tcp_sack_update_scoreboard(tp
, &to
);
2134 if (IN_FASTRECOVERY(tp
)) {
2135 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
)) {
2136 EXIT_FASTRECOVERY(tp
);
2139 * If the congestion window was inflated
2140 * to account for the other side's
2141 * cached packets, retract it.
2143 * Window inflation should have left us
2144 * with approximately snd_ssthresh outstanding
2145 * data. But, in case we would be inclined
2146 * to send a burst, better do it using
2149 if (!TCP_DO_SACK(tp
))
2150 tp
->snd_cwnd
= tp
->snd_ssthresh
;
2152 if (SEQ_GT(th
->th_ack
+ tp
->snd_cwnd
,
2153 tp
->snd_max
+ 2 * tp
->t_maxseg
))
2155 (tp
->snd_max
- tp
->snd_una
) +
2158 if (TCP_DO_SACK(tp
)) {
2159 tp
->snd_max_rexmt
= tp
->snd_max
;
2160 tcp_sack_rexmt(tp
, th
);
2162 tcp_newreno_partial_ack(tp
, th
, acked
);
2168 * When new data is acked, open the congestion window.
2169 * If the window gives us less than ssthresh packets
2170 * in flight, open exponentially (maxseg per packet).
2171 * Otherwise open linearly: maxseg per window
2172 * (maxseg^2 / cwnd per packet).
2174 u_int cw
= tp
->snd_cwnd
;
2177 if (cw
> tp
->snd_ssthresh
)
2178 incr
= tp
->t_maxseg
* tp
->t_maxseg
/ cw
;
2180 incr
= tp
->t_maxseg
;
2181 tp
->snd_cwnd
= min(cw
+incr
, TCP_MAXWIN
<<tp
->snd_scale
);
2182 tp
->snd_recover
= th
->th_ack
- 1;
2184 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
2185 tp
->snd_nxt
= tp
->snd_una
;
2188 * If all outstanding data is acked, stop retransmit
2189 * timer and remember to restart (more output or persist).
2190 * If there is more data to be acked, restart retransmit
2191 * timer, using current (possibly backed-off) value.
2193 if (th
->th_ack
== tp
->snd_max
) {
2194 callout_stop(tp
->tt_rexmt
);
2196 } else if (!callout_active(tp
->tt_persist
))
2197 callout_reset(tp
->tt_rexmt
, tp
->t_rxtcur
,
2198 tcp_timer_rexmt
, tp
);
2200 switch (tp
->t_state
) {
2202 * In FIN_WAIT_1 STATE in addition to the processing
2203 * for the ESTABLISHED state if our FIN is now acknowledged
2204 * then enter FIN_WAIT_2.
2206 case TCPS_FIN_WAIT_1
:
2207 if (ourfinisacked
) {
2209 * If we can't receive any more
2210 * data, then closing user can proceed.
2211 * Starting the timer is contrary to the
2212 * specification, but if we don't get a FIN
2213 * we'll hang forever.
2215 if (so
->so_state
& SS_CANTRCVMORE
) {
2216 soisdisconnected(so
);
2217 callout_reset(tp
->tt_2msl
, tcp_maxidle
,
2218 tcp_timer_2msl
, tp
);
2220 tp
->t_state
= TCPS_FIN_WAIT_2
;
2225 * In CLOSING STATE in addition to the processing for
2226 * the ESTABLISHED state if the ACK acknowledges our FIN
2227 * then enter the TIME-WAIT state, otherwise ignore
2231 if (ourfinisacked
) {
2232 tp
->t_state
= TCPS_TIME_WAIT
;
2233 tcp_canceltimers(tp
);
2234 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2235 if (tp
->cc_recv
!= 0 &&
2236 (ticks
- tp
->t_starttime
) < tcp_msl
)
2237 callout_reset(tp
->tt_2msl
,
2238 tp
->t_rxtcur
* TCPTV_TWTRUNC
,
2239 tcp_timer_2msl
, tp
);
2241 callout_reset(tp
->tt_2msl
, 2 * tcp_msl
,
2242 tcp_timer_2msl
, tp
);
2243 soisdisconnected(so
);
2248 * In LAST_ACK, we may still be waiting for data to drain
2249 * and/or to be acked, as well as for the ack of our FIN.
2250 * If our FIN is now acknowledged, delete the TCB,
2251 * enter the closed state and return.
2254 if (ourfinisacked
) {
2261 * In TIME_WAIT state the only thing that should arrive
2262 * is a retransmission of the remote FIN. Acknowledge
2263 * it and restart the finack timer.
2265 case TCPS_TIME_WAIT
:
2266 callout_reset(tp
->tt_2msl
, 2 * tcp_msl
,
2267 tcp_timer_2msl
, tp
);
2274 * Update window information.
2275 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2277 if ((thflags
& TH_ACK
) &&
2278 acceptable_window_update(tp
, th
, tiwin
)) {
2279 /* keep track of pure window updates */
2280 if (tlen
== 0 && tp
->snd_wl2
== th
->th_ack
&&
2281 tiwin
> tp
->snd_wnd
)
2282 tcpstat
.tcps_rcvwinupd
++;
2283 tp
->snd_wnd
= tiwin
;
2284 tp
->snd_wl1
= th
->th_seq
;
2285 tp
->snd_wl2
= th
->th_ack
;
2286 if (tp
->snd_wnd
> tp
->max_sndwnd
)
2287 tp
->max_sndwnd
= tp
->snd_wnd
;
2292 * Process segments with URG.
2294 if ((thflags
& TH_URG
) && th
->th_urp
&&
2295 !TCPS_HAVERCVDFIN(tp
->t_state
)) {
2297 * This is a kludge, but if we receive and accept
2298 * random urgent pointers, we'll crash in
2299 * soreceive. It's hard to imagine someone
2300 * actually wanting to send this much urgent data.
2302 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
2303 th
->th_urp
= 0; /* XXX */
2304 thflags
&= ~TH_URG
; /* XXX */
2305 goto dodata
; /* XXX */
2308 * If this segment advances the known urgent pointer,
2309 * then mark the data stream. This should not happen
2310 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2311 * a FIN has been received from the remote side.
2312 * In these states we ignore the URG.
2314 * According to RFC961 (Assigned Protocols),
2315 * the urgent pointer points to the last octet
2316 * of urgent data. We continue, however,
2317 * to consider it to indicate the first octet
2318 * of data past the urgent section as the original
2319 * spec states (in one of two places).
2321 if (SEQ_GT(th
->th_seq
+ th
->th_urp
, tp
->rcv_up
)) {
2322 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
2323 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
2324 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
2325 if (so
->so_oobmark
== 0)
2326 so
->so_state
|= SS_RCVATMARK
;
2328 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
2331 * Remove out of band data so doesn't get presented to user.
2332 * This can happen independent of advancing the URG pointer,
2333 * but if two URG's are pending at once, some out-of-band
2334 * data may creep in... ick.
2336 if (th
->th_urp
<= (u_long
)tlen
&&
2337 !(so
->so_options
& SO_OOBINLINE
)) {
2338 /* hdr drop is delayed */
2339 tcp_pulloutofband(so
, th
, m
, drop_hdrlen
);
2343 * If no out of band data is expected,
2344 * pull receive urgent pointer along
2345 * with the receive window.
2347 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
2348 tp
->rcv_up
= tp
->rcv_nxt
;
2353 * Process the segment text, merging it into the TCP sequencing queue,
2354 * and arranging for acknowledgment of receipt if necessary.
2355 * This process logically involves adjusting tp->rcv_wnd as data
2356 * is presented to the user (this happens in tcp_usrreq.c,
2357 * case PRU_RCVD). If a FIN has already been received on this
2358 * connection then we just ignore the text.
2360 if ((tlen
|| (thflags
& TH_FIN
)) && !TCPS_HAVERCVDFIN(tp
->t_state
)) {
2361 m_adj(m
, drop_hdrlen
); /* delayed header drop */
2363 * Insert segment which includes th into TCP reassembly queue
2364 * with control block tp. Set thflags to whether reassembly now
2365 * includes a segment with FIN. This handles the common case
2366 * inline (segment is the next to be received on an established
2367 * connection, and the queue is empty), avoiding linkage into
2368 * and removal from the queue and repetition of various
2370 * Set DELACK for segments received in order, but ack
2371 * immediately when segments are out of order (so
2372 * fast retransmit can work).
2374 if (th
->th_seq
== tp
->rcv_nxt
&&
2375 LIST_EMPTY(&tp
->t_segq
) &&
2376 TCPS_HAVEESTABLISHED(tp
->t_state
)) {
2378 callout_reset(tp
->tt_delack
, tcp_delacktime
,
2379 tcp_timer_delack
, tp
);
2381 tp
->t_flags
|= TF_ACKNOW
;
2382 tp
->rcv_nxt
+= tlen
;
2383 thflags
= th
->th_flags
& TH_FIN
;
2384 tcpstat
.tcps_rcvpack
++;
2385 tcpstat
.tcps_rcvbyte
+= tlen
;
2387 if (so
->so_state
& SS_CANTRCVMORE
)
2390 sbappendstream(&so
->so_rcv
, m
);
2393 if (!(tp
->t_flags
& TF_DUPSEG
)) {
2394 /* Initialize SACK report block. */
2395 tp
->reportblk
.rblk_start
= th
->th_seq
;
2396 tp
->reportblk
.rblk_end
= th
->th_seq
+ tlen
+
2397 ((thflags
& TH_FIN
) != 0);
2399 thflags
= tcp_reass(tp
, th
, &tlen
, m
);
2400 tp
->t_flags
|= TF_ACKNOW
;
2404 * Note the amount of data that peer has sent into
2405 * our window, in order to estimate the sender's
2408 len
= so
->so_rcv
.sb_hiwat
- (tp
->rcv_adv
- tp
->rcv_nxt
);
2415 * If FIN is received ACK the FIN and let the user know
2416 * that the connection is closing.
2418 if (thflags
& TH_FIN
) {
2419 if (!TCPS_HAVERCVDFIN(tp
->t_state
)) {
2422 * If connection is half-synchronized
2423 * (ie NEEDSYN flag on) then delay ACK,
2424 * so it may be piggybacked when SYN is sent.
2425 * Otherwise, since we received a FIN then no
2426 * more input can be expected, send ACK now.
2428 if (DELAY_ACK(tp
) && (tp
->t_flags
& TF_NEEDSYN
))
2429 callout_reset(tp
->tt_delack
, tcp_delacktime
,
2430 tcp_timer_delack
, tp
);
2432 tp
->t_flags
|= TF_ACKNOW
;
2436 switch (tp
->t_state
) {
2438 * In SYN_RECEIVED and ESTABLISHED STATES
2439 * enter the CLOSE_WAIT state.
2441 case TCPS_SYN_RECEIVED
:
2442 tp
->t_starttime
= ticks
;
2444 case TCPS_ESTABLISHED
:
2445 tp
->t_state
= TCPS_CLOSE_WAIT
;
2449 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2450 * enter the CLOSING state.
2452 case TCPS_FIN_WAIT_1
:
2453 tp
->t_state
= TCPS_CLOSING
;
2457 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2458 * starting the time-wait timer, turning off the other
2461 case TCPS_FIN_WAIT_2
:
2462 tp
->t_state
= TCPS_TIME_WAIT
;
2463 tcp_canceltimers(tp
);
2464 /* Shorten TIME_WAIT [RFC-1644, p.28] */
2465 if (tp
->cc_recv
!= 0 &&
2466 (ticks
- tp
->t_starttime
) < tcp_msl
) {
2467 callout_reset(tp
->tt_2msl
,
2468 tp
->t_rxtcur
* TCPTV_TWTRUNC
,
2469 tcp_timer_2msl
, tp
);
2470 /* For transaction client, force ACK now. */
2471 tp
->t_flags
|= TF_ACKNOW
;
2474 callout_reset(tp
->tt_2msl
, 2 * tcp_msl
,
2475 tcp_timer_2msl
, tp
);
2476 soisdisconnected(so
);
2480 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2482 case TCPS_TIME_WAIT
:
2483 callout_reset(tp
->tt_2msl
, 2 * tcp_msl
,
2484 tcp_timer_2msl
, tp
);
2490 if (so
->so_options
& SO_DEBUG
)
2491 tcp_trace(TA_INPUT
, ostate
, tp
, tcp_saveipgen
, &tcp_savetcp
, 0);
2495 * Return any desired output.
2497 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
))
2503 * Generate an ACK dropping incoming segment if it occupies
2504 * sequence space, where the ACK reflects our state.
2506 * We can now skip the test for the RST flag since all
2507 * paths to this code happen after packets containing
2508 * RST have been dropped.
2510 * In the SYN-RECEIVED state, don't send an ACK unless the
2511 * segment we received passes the SYN-RECEIVED ACK test.
2512 * If it fails send a RST. This breaks the loop in the
2513 * "LAND" DoS attack, and also prevents an ACK storm
2514 * between two listening ports that have been sent forged
2515 * SYN segments, each with the source address of the other.
2517 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
2518 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
2519 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
2520 rstreason
= BANDLIM_RST_OPENPORT
;
2524 if (so
->so_options
& SO_DEBUG
)
2525 tcp_trace(TA_DROP
, ostate
, tp
, tcp_saveipgen
, &tcp_savetcp
, 0);
2528 tp
->t_flags
|= TF_ACKNOW
;
2534 * Generate a RST, dropping incoming segment.
2535 * Make ACK acceptable to originator of segment.
2536 * Don't bother to respond if destination was broadcast/multicast.
2538 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
| M_MCAST
))
2541 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2542 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
2545 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2546 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2547 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2548 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
2551 /* IPv6 anycast check is done at tcp6_input() */
2554 * Perform bandwidth limiting.
2557 if (badport_bandlim(rstreason
) < 0)
2562 if (tp
== NULL
|| (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
2563 tcp_trace(TA_DROP
, ostate
, tp
, tcp_saveipgen
, &tcp_savetcp
, 0);
2565 if (thflags
& TH_ACK
)
2566 /* mtod() below is safe as long as hdr dropping is delayed */
2567 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
2570 if (thflags
& TH_SYN
)
2572 /* mtod() below is safe as long as hdr dropping is delayed */
2573 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+ tlen
,
2574 (tcp_seq
)0, TH_RST
| TH_ACK
);
2580 * Drop space held by incoming segment and return.
2583 if (tp
== NULL
|| (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
2584 tcp_trace(TA_DROP
, ostate
, tp
, tcp_saveipgen
, &tcp_savetcp
, 0);
2591 * Parse TCP options and place in tcpopt.
2594 tcp_dooptions(struct tcpopt
*to
, u_char
*cp
, int cnt
, boolean_t is_syn
)
2599 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
2601 if (opt
== TCPOPT_EOL
)
2603 if (opt
== TCPOPT_NOP
)
2609 if (optlen
< 2 || optlen
> cnt
)
2614 if (optlen
!= TCPOLEN_MAXSEG
)
2618 to
->to_flags
|= TOF_MSS
;
2619 bcopy(cp
+ 2, &to
->to_mss
, sizeof(to
->to_mss
));
2620 to
->to_mss
= ntohs(to
->to_mss
);
2623 if (optlen
!= TCPOLEN_WINDOW
)
2627 to
->to_flags
|= TOF_SCALE
;
2628 to
->to_requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
2630 case TCPOPT_TIMESTAMP
:
2631 if (optlen
!= TCPOLEN_TIMESTAMP
)
2633 to
->to_flags
|= TOF_TS
;
2634 bcopy(cp
+ 2, &to
->to_tsval
, sizeof(to
->to_tsval
));
2635 to
->to_tsval
= ntohl(to
->to_tsval
);
2636 bcopy(cp
+ 6, &to
->to_tsecr
, sizeof(to
->to_tsecr
));
2637 to
->to_tsecr
= ntohl(to
->to_tsecr
);
2640 if (optlen
!= TCPOLEN_CC
)
2642 to
->to_flags
|= TOF_CC
;
2643 bcopy(cp
+ 2, &to
->to_cc
, sizeof(to
->to_cc
));
2644 to
->to_cc
= ntohl(to
->to_cc
);
2647 if (optlen
!= TCPOLEN_CC
)
2651 to
->to_flags
|= TOF_CCNEW
;
2652 bcopy(cp
+ 2, &to
->to_cc
, sizeof(to
->to_cc
));
2653 to
->to_cc
= ntohl(to
->to_cc
);
2656 if (optlen
!= TCPOLEN_CC
)
2660 to
->to_flags
|= TOF_CCECHO
;
2661 bcopy(cp
+ 2, &to
->to_ccecho
, sizeof(to
->to_ccecho
));
2662 to
->to_ccecho
= ntohl(to
->to_ccecho
);
2664 case TCPOPT_SACK_PERMITTED
:
2665 if (optlen
!= TCPOLEN_SACK_PERMITTED
)
2669 to
->to_flags
|= TOF_SACK_PERMITTED
;
2672 if ((optlen
- 2) & 0x07) /* not multiple of 8 */
2674 to
->to_nsackblocks
= (optlen
- 2) / 8;
2675 to
->to_sackblocks
= (struct raw_sackblock
*) (cp
+ 2);
2676 to
->to_flags
|= TOF_SACK
;
2677 for (i
= 0; i
< to
->to_nsackblocks
; i
++) {
2678 struct raw_sackblock
*r
= &to
->to_sackblocks
[i
];
2680 r
->rblk_start
= ntohl(r
->rblk_start
);
2681 r
->rblk_end
= ntohl(r
->rblk_end
);
2691 * Pull out of band byte out of a segment so
2692 * it doesn't appear in the user's data queue.
2693 * It is still reflected in the segment length for
2694 * sequencing purposes.
2695 * "off" is the delayed to be dropped hdrlen.
2698 tcp_pulloutofband(struct socket
*so
, struct tcphdr
*th
, struct mbuf
*m
, int off
)
2700 int cnt
= off
+ th
->th_urp
- 1;
2703 if (m
->m_len
> cnt
) {
2704 char *cp
= mtod(m
, caddr_t
) + cnt
;
2705 struct tcpcb
*tp
= sototcpcb(so
);
2708 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
2709 bcopy(cp
+ 1, cp
, m
->m_len
- cnt
- 1);
2711 if (m
->m_flags
& M_PKTHDR
)
2720 panic("tcp_pulloutofband");
2724 * Collect new round-trip time estimate
2725 * and update averages and current timeout.
2728 tcp_xmit_timer(struct tcpcb
*tp
, int rtt
)
2732 tcpstat
.tcps_rttupdated
++;
2734 if (tp
->t_srtt
!= 0) {
2736 * srtt is stored as fixed point with 5 bits after the
2737 * binary point (i.e., scaled by 8). The following magic
2738 * is equivalent to the smoothing algorithm in rfc793 with
2739 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2740 * point). Adjust rtt to origin 0.
2742 delta
= ((rtt
- 1) << TCP_DELTA_SHIFT
)
2743 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
2745 if ((tp
->t_srtt
+= delta
) <= 0)
2749 * We accumulate a smoothed rtt variance (actually, a
2750 * smoothed mean difference), then set the retransmit
2751 * timer to smoothed rtt + 4 times the smoothed variance.
2752 * rttvar is stored as fixed point with 4 bits after the
2753 * binary point (scaled by 16). The following is
2754 * equivalent to rfc793 smoothing with an alpha of .75
2755 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2756 * rfc793's wired-in beta.
2760 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
2761 if ((tp
->t_rttvar
+= delta
) <= 0)
2763 if (tp
->t_rttbest
> tp
->t_srtt
+ tp
->t_rttvar
)
2764 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
2767 * No rtt measurement yet - use the unsmoothed rtt.
2768 * Set the variance to half the rtt (so our first
2769 * retransmit happens at 3*rtt).
2771 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
2772 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
2773 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
2779 * the retransmit should happen at rtt + 4 * rttvar.
2780 * Because of the way we do the smoothing, srtt and rttvar
2781 * will each average +1/2 tick of bias. When we compute
2782 * the retransmit timer, we want 1/2 tick of rounding and
2783 * 1 extra tick because of +-1/2 tick uncertainty in the
2784 * firing of the timer. The bias will give us exactly the
2785 * 1.5 tick we need. But, because the bias is
2786 * statistical, we have to test that we don't drop below
2787 * the minimum feasible timer (which is 2 ticks).
2789 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
2790 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
);
2793 * We received an ack for a packet that wasn't retransmitted;
2794 * it is probably safe to discard any error indications we've
2795 * received recently. This isn't quite right, but close enough
2796 * for now (a route might have failed after we sent a segment,
2797 * and the return path might not be symmetrical).
2799 tp
->t_softerror
= 0;
2803 * Determine a reasonable value for maxseg size.
2804 * If the route is known, check route for mtu.
2805 * If none, use an mss that can be handled on the outgoing
2806 * interface without forcing IP to fragment; if bigger than
2807 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2808 * to utilize large mbufs. If no route is found, route has no mtu,
2809 * or the destination isn't local, use a default, hopefully conservative
2810 * size (usually 512 or the default IP max size, but no more than the mtu
2811 * of the interface), as we can't discover anything about intervening
2812 * gateways or networks. We also initialize the congestion/slow start
2813 * window to be a single segment if the destination isn't local.
2814 * While looking at the routing entry, we also initialize other path-dependent
2815 * parameters from pre-set or cached values in the routing entry.
2817 * Also take into account the space needed for options that we
2818 * send regularly. Make maxseg shorter by that amount to assure
2819 * that we can send maxseg amount of data even when the options
2820 * are present. Store the upper limit of the length of options plus
2823 * NOTE that this routine is only called when we process an incoming
2824 * segment, for outgoing segments only tcp_mssopt is called.
2826 * In case of T/TCP, we call this routine during implicit connection
2827 * setup as well (offer = -1), to initialize maxseg from the cached
2831 tcp_mss(struct tcpcb
*tp
, int offer
)
2837 struct inpcb
*inp
= tp
->t_inpcb
;
2839 struct rmxp_tao
*taop
;
2840 int origoffer
= offer
;
2842 boolean_t isipv6
= ((inp
->inp_vflag
& INP_IPV6
) ? TRUE
: FALSE
);
2843 size_t min_protoh
= isipv6
?
2844 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
) :
2845 sizeof(struct tcpiphdr
);
2847 const boolean_t isipv6
= FALSE
;
2848 const size_t min_protoh
= sizeof(struct tcpiphdr
);
2852 rt
= tcp_rtlookup6(&inp
->inp_inc
);
2854 rt
= tcp_rtlookup(&inp
->inp_inc
);
2856 tp
->t_maxopd
= tp
->t_maxseg
=
2857 (isipv6
? tcp_v6mssdflt
: tcp_mssdflt
);
2861 so
= inp
->inp_socket
;
2863 taop
= rmx_taop(rt
->rt_rmx
);
2865 * Offer == -1 means that we didn't receive SYN yet,
2866 * use cached value in that case;
2869 offer
= taop
->tao_mssopt
;
2871 * Offer == 0 means that there was no MSS on the SYN segment,
2872 * in this case we use tcp_mssdflt.
2875 offer
= (isipv6
? tcp_v6mssdflt
: tcp_mssdflt
);
2878 * Sanity check: make sure that maxopd will be large
2879 * enough to allow some data on segments even is the
2880 * all the option space is used (40bytes). Otherwise
2881 * funny things may happen in tcp_output.
2883 offer
= max(offer
, 64);
2884 taop
->tao_mssopt
= offer
;
2887 * While we're here, check if there's an initial rtt
2888 * or rttvar. Convert from the route-table units
2889 * to scaled multiples of the slow timeout timer.
2891 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
)) {
2893 * XXX the lock bit for RTT indicates that the value
2894 * is also a minimum value; this is subject to time.
2896 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
2897 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ hz
);
2898 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (hz
* TCP_RTT_SCALE
));
2899 tp
->t_rttbest
= tp
->t_srtt
+ TCP_RTT_SCALE
;
2900 tcpstat
.tcps_usedrtt
++;
2901 if (rt
->rt_rmx
.rmx_rttvar
) {
2902 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
2903 (RTM_RTTUNIT
/ (hz
* TCP_RTTVAR_SCALE
));
2904 tcpstat
.tcps_usedrttvar
++;
2906 /* default variation is +- 1 rtt */
2908 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
2910 TCPT_RANGESET(tp
->t_rxtcur
,
2911 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
2912 tp
->t_rttmin
, TCPTV_REXMTMAX
);
2915 * if there's an mtu associated with the route, use it
2916 * else, use the link mtu.
2918 if (rt
->rt_rmx
.rmx_mtu
)
2919 mss
= rt
->rt_rmx
.rmx_mtu
- min_protoh
;
2922 mss
= nd_ifinfo
[rt
->rt_ifp
->if_index
].linkmtu
-
2924 if (!in6_localaddr(&inp
->in6p_faddr
))
2925 mss
= min(mss
, tcp_v6mssdflt
);
2927 mss
= ifp
->if_mtu
- min_protoh
;
2928 if (!in_localaddr(inp
->inp_faddr
))
2929 mss
= min(mss
, tcp_mssdflt
);
2932 mss
= min(mss
, offer
);
2934 * maxopd stores the maximum length of data AND options
2935 * in a segment; maxseg is the amount of data in a normal
2936 * segment. We need to store this value (maxopd) apart
2937 * from maxseg, because now every segment carries options
2938 * and thus we normally have somewhat less data in segments.
2943 * In case of T/TCP, origoffer==-1 indicates, that no segments
2944 * were received yet. In this case we just guess, otherwise
2945 * we do the same as before T/TCP.
2947 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_NOOPT
)) == TF_REQ_TSTMP
&&
2949 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
2950 mss
-= TCPOLEN_TSTAMP_APPA
;
2951 if ((tp
->t_flags
& (TF_REQ_CC
| TF_NOOPT
)) == TF_REQ_CC
&&
2953 (tp
->t_flags
& TF_RCVD_CC
) == TF_RCVD_CC
))
2954 mss
-= TCPOLEN_CC_APPA
;
2956 #if (MCLBYTES & (MCLBYTES - 1)) == 0
2958 mss
&= ~(MCLBYTES
-1);
2961 mss
= mss
/ MCLBYTES
* MCLBYTES
;
2964 * If there's a pipesize, change the socket buffer
2965 * to that size. Make the socket buffers an integral
2966 * number of mss units; if the mss is larger than
2967 * the socket buffer, decrease the mss.
2970 if ((bufsize
= rt
->rt_rmx
.rmx_sendpipe
) == 0)
2972 bufsize
= so
->so_snd
.sb_hiwat
;
2976 bufsize
= roundup(bufsize
, mss
);
2977 if (bufsize
> sb_max
)
2979 if (bufsize
> so
->so_snd
.sb_hiwat
)
2980 sbreserve(&so
->so_snd
, bufsize
, so
, NULL
);
2985 if ((bufsize
= rt
->rt_rmx
.rmx_recvpipe
) == 0)
2987 bufsize
= so
->so_rcv
.sb_hiwat
;
2988 if (bufsize
> mss
) {
2989 bufsize
= roundup(bufsize
, mss
);
2990 if (bufsize
> sb_max
)
2992 if (bufsize
> so
->so_rcv
.sb_hiwat
)
2993 sbreserve(&so
->so_rcv
, bufsize
, so
, NULL
);
2997 * Set the slow-start flight size depending on whether this
2998 * is a local network or not.
3001 tp
->snd_cwnd
= min(4 * mss
, max(2 * mss
, 4380));
3002 else if ((isipv6
&& in6_localaddr(&inp
->in6p_faddr
)) ||
3003 (!isipv6
&& in_localaddr(inp
->inp_faddr
)))
3004 tp
->snd_cwnd
= mss
* ss_fltsz_local
;
3006 tp
->snd_cwnd
= mss
* ss_fltsz
;
3008 if (rt
->rt_rmx
.rmx_ssthresh
) {
3010 * There's some sort of gateway or interface
3011 * buffer limit on the path. Use this to set
3012 * the slow start threshhold, but set the
3013 * threshold to no less than 2*mss.
3015 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
3016 tcpstat
.tcps_usedssthresh
++;
3021 * Determine the MSS option to send on an outgoing SYN.
3024 tcp_mssopt(struct tcpcb
*tp
)
3029 ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) ? TRUE
: FALSE
);
3030 int min_protoh
= isipv6
?
3031 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
) :
3032 sizeof(struct tcpiphdr
);
3034 const boolean_t isipv6
= FALSE
;
3035 const size_t min_protoh
= sizeof(struct tcpiphdr
);
3039 rt
= tcp_rtlookup6(&tp
->t_inpcb
->inp_inc
);
3041 rt
= tcp_rtlookup(&tp
->t_inpcb
->inp_inc
);
3043 return (isipv6
? tcp_v6mssdflt
: tcp_mssdflt
);
3045 return (rt
->rt_ifp
->if_mtu
- min_protoh
);
3049 * When a partial ack arrives, force the retransmission of the
3050 * next unacknowledged segment. Do not exit Fast Recovery.
3052 * Implement the Slow-but-Steady variant of NewReno by restarting the
3053 * the retransmission timer. Turn it off here so it can be restarted
3054 * later in tcp_output().
3057 tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
, int acked
)
3059 tcp_seq old_snd_nxt
= tp
->snd_nxt
;
3060 u_long ocwnd
= tp
->snd_cwnd
;
3062 callout_stop(tp
->tt_rexmt
);
3064 tp
->snd_nxt
= th
->th_ack
;
3065 /* Set snd_cwnd to one segment beyond acknowledged offset. */
3066 tp
->snd_cwnd
= tp
->t_maxseg
;
3067 tp
->t_flags
|= TF_ACKNOW
;
3069 if (SEQ_GT(old_snd_nxt
, tp
->snd_nxt
))
3070 tp
->snd_nxt
= old_snd_nxt
;
3071 /* partial window deflation */
3072 tp
->snd_cwnd
= ocwnd
- acked
+ tp
->t_maxseg
;
3076 * In contrast to the Slow-but-Steady NewReno variant,
3077 * we do not reset the retransmission timer for SACK retransmissions,
3078 * except when retransmitting snd_una.
3081 tcp_sack_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
)
3083 uint32_t pipe
, seglen
;
3086 tcp_seq old_snd_nxt
= tp
->snd_nxt
;
3087 u_long ocwnd
= tp
->snd_cwnd
;
3088 int nseg
= 0; /* consecutive new segments */
3089 #define MAXBURST 4 /* limit burst of new packets on partial ack */
3092 pipe
= tcp_sack_compute_pipe(tp
);
3093 while ((tcp_seq_diff_t
)(ocwnd
- pipe
) >= (tcp_seq_diff_t
)tp
->t_maxseg
&&
3094 (!tcp_do_smartsack
|| nseg
< MAXBURST
) &&
3095 tcp_sack_nextseg(tp
, &nextrexmt
, &seglen
, &lostdup
)) {
3097 tcp_seq old_snd_max
;
3100 if (nextrexmt
== tp
->snd_max
) ++nseg
;
3101 tp
->snd_nxt
= nextrexmt
;
3102 tp
->snd_cwnd
= nextrexmt
- tp
->snd_una
+ seglen
;
3103 old_snd_max
= tp
->snd_max
;
3104 if (nextrexmt
== tp
->snd_una
)
3105 callout_stop(tp
->tt_rexmt
);
3106 error
= tcp_output(tp
);
3109 sent
= tp
->snd_nxt
- nextrexmt
;
3114 tcpstat
.tcps_sndsackpack
++;
3115 tcpstat
.tcps_sndsackbyte
+= sent
;
3116 if (SEQ_LT(nextrexmt
, old_snd_max
) &&
3117 SEQ_LT(tp
->rexmt_high
, tp
->snd_nxt
))
3118 tp
->rexmt_high
= seq_min(tp
->snd_nxt
, old_snd_max
);
3120 if (SEQ_GT(old_snd_nxt
, tp
->snd_nxt
))
3121 tp
->snd_nxt
= old_snd_nxt
;
3122 tp
->snd_cwnd
= ocwnd
;